sampler: move cpu notifier logic to the separate file 44/170144/2
authorVyacheslav Cherkashin <v.cherkashin@samsung.com>
Wed, 7 Feb 2018 15:25:38 +0000 (18:25 +0300)
committerVyacheslav Cherkashin <v.cherkashin@samsung.com>
Wed, 14 Feb 2018 10:57:16 +0000 (13:57 +0300)
Preparatory commit for add cpu/hotplug support

Change-Id: I2c62892c30a2793472569cec5453a350bf6c4b54
Signed-off-by: Vyacheslav Cherkashin <v.cherkashin@samsung.com>
modules/sampler/Kbuild
modules/sampler/cpu_notifier.h [moved from modules/sampler/swap_sampler_errors.h with 62% similarity]
modules/sampler/cpu_notifier_hot.c [new file with mode: 0644]
modules/sampler/swap_sampler_module.c

index f948655..5fee897 100644 (file)
@@ -2,7 +2,7 @@ EXTRA_CFLAGS := $(extra_cflags)
 KBUILD_EXTRA_SYMBOLS = $(src)/../writer/Module.symvers
 
 obj-m := swap_sampler.o
-swap_sampler-y := swap_sampler_module.o
+swap_sampler-y := swap_sampler_module.o cpu_notifier_hot.o
 
 ifdef CONFIG_HIGH_RES_TIMERS
     swap_sampler-y += sampler_hrtimer.o
similarity index 62%
rename from modules/sampler/swap_sampler_errors.h
rename to modules/sampler/cpu_notifier.h
index d673b4d..3d487a1 100644 (file)
@@ -1,9 +1,4 @@
-/**
- * @file sampler/swap_sampler_errors.h
- * @author Alexander Aksenov <a.aksenov@samsung.com>
- *
- * @section LICENSE
- *
+/*
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  *
- * @section COPYRIGHT
+ * Copyright (C) Samsung Electronics, 2018
  *
- * Copyright (C) Samsung Electronics, 2013
+ * 2018         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
  *
- * @section DESCRIPTION
- *
- * Sampler error codes.
  */
 
+#ifndef _NSP_H
+#define _NSP_H
 
-/**
- * @enum _swap_sampler_errors
- * @brief Sampler errors.
- */
-enum _swap_sampler_errors {
-       E_SS_SUCCESS = 0,           /**< Success. */
-       E_SS_WRONG_QUANTUM = 1      /**< Wrong timer quantum set. */
-};
+
+int cpu_notifier_start(int (*cpu_setup)(unsigned int cpu),
+                      int (*cpu_shutdown)(unsigned int cpu));
+void cpu_notifier_stop(void);
+
+
+#endif /* _NSP_H */
diff --git a/modules/sampler/cpu_notifier_hot.c b/modules/sampler/cpu_notifier_hot.c
new file mode 100644 (file)
index 0000000..ab4f82a
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) Samsung Electronics, 2018
+ *
+ * 2018         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ */
+
+
+#include <linux/cpu.h>
+#include <linux/notifier.h>
+#include "cpu_notifier.h"
+#include "kernel_operations.h"
+
+
+static int (*g_cpu_setup)(unsigned int cpu);
+static int (*g_cpu_shutdown)(unsigned int cpu);
+static int g_cpu_status[NR_CPUS];
+
+static int do_cpu_setup(unsigned int cpu)
+{
+       WARN_ON(g_cpu_status[cpu]);
+       g_cpu_status[cpu] = 1;
+
+       return g_cpu_setup(cpu);
+}
+
+static int do_cpu_shutdown(unsigned int cpu)
+{
+       int ret = 0;
+       if (g_cpu_status[cpu]) {
+               ret = g_cpu_shutdown(cpu);
+               g_cpu_status[cpu] = 0;
+       }
+
+       return ret;
+}
+
+static int cpu_notify(struct notifier_block *self,
+                     unsigned long action, void *hcpu)
+{
+       unsigned int cpu = (long)hcpu;
+
+       switch (action) {
+       case CPU_ONLINE:
+       case CPU_ONLINE_FROZEN:
+               do_cpu_setup(cpu);
+               break;
+       case CPU_DEAD:
+       case CPU_DEAD_FROZEN:
+               do_cpu_shutdown(cpu);
+               break;
+       }
+
+       return NOTIFY_OK;
+}
+
+static struct notifier_block cpu_notifier = {
+       .notifier_call = cpu_notify,
+};
+
+
+void cpu_notifier_stop(void)
+{
+       unsigned int cpu;
+
+       get_online_cpus();
+       unregister_hotcpu_notifier(&cpu_notifier);
+       for_each_possible_cpu(cpu)
+               do_cpu_shutdown(cpu);
+
+       g_cpu_shutdown = NULL;
+       g_cpu_setup = NULL;
+       put_online_cpus();
+}
+
+int cpu_notifier_start(int (*cpu_setup)(unsigned int cpu),
+                      int (*cpu_shutdown)(unsigned int cpu))
+{
+       int ret;
+       unsigned int cpu;
+       BUG_ON(g_cpu_setup || g_cpu_shutdown);
+
+       get_online_cpus();
+       g_cpu_setup = cpu_setup;
+       g_cpu_shutdown = cpu_shutdown;
+       ret = register_hotcpu_notifier(&cpu_notifier);
+       if (ret) {
+               g_cpu_shutdown = NULL;
+               g_cpu_setup = NULL;
+               print_err("Error of register_hotcpu_notifier(), ret=%d\n", ret);
+               goto out;
+       }
+
+       /* call cpu_setup() for online cpus */
+       for_each_online_cpu(cpu) {
+               ret = do_cpu_setup(cpu);
+               if (ret) {
+                       cpu_notifier_stop();
+                       break;
+               }
+       }
+out:
+       put_online_cpus();
+       return ret;
+}
index 9ab9dca..1b22646 100644 (file)
  */
 
 #include <linux/ptrace.h>
-#include <linux/jiffies.h>
-#include <linux/sched.h>
-#include <linux/notifier.h>
-#include <linux/cpu.h>
 #include <linux/module.h>
 #include <master/swap_initializer.h>
 #include "swap_sampler_module.h"
-#include "swap_sampler_errors.h"
 #include "kernel_operations.h"
 #include "sampler_timers.h"
+#include "cpu_notifier.h"
 
 
-static BLOCKING_NOTIFIER_HEAD(swap_sampler_notifier_list);
 static swap_sample_cb_t sampler_cb;
 
 static restart_ret swap_timer_restart(swap_timer *timer)
@@ -51,69 +46,44 @@ static restart_ret swap_timer_restart(swap_timer *timer)
        return sampler_timers_restart(timer);
 }
 
-static int swap_timer_start(void)
+static int cpu_setup(unsigned int cpu)
 {
-       get_online_cpus();
-       sampler_timers_set_run();
-
-       on_each_cpu(sampler_timers_start, swap_timer_restart, 1);
-       put_online_cpus();
-
-       return E_SS_SUCCESS;
-}
-
-static void swap_timer_stop(void)
-{
-       int cpu;
-
-       get_online_cpus();
-
-       for_each_online_cpu(cpu)
-               sampler_timers_stop(cpu);
-       sampler_timers_set_stop();
-       put_online_cpus();
+       smp_call_function_single(cpu, sampler_timers_start,
+                                swap_timer_restart, 1);
+       return 0;
 }
 
-static int swap_cpu_notify(struct notifier_block *self,
-                                   unsigned long action, void *hcpu)
+static int cpu_shutdown(unsigned int cpu)
 {
-       long cpu = (long) hcpu;
-
-       switch (action) {
-       case CPU_ONLINE:
-       case CPU_ONLINE_FROZEN:
-               smp_call_function_single(cpu, sampler_timers_start,
-                                swap_timer_restart, 1);
-               break;
-       case CPU_DEAD:
-       case CPU_DEAD_FROZEN:
-               sampler_timers_stop(cpu);
-               break;
-       }
-
-       return NOTIFY_OK;
+       sampler_timers_stop(cpu);
+       return 0;
 }
 
-static struct notifier_block __refdata swap_cpu_notifier = {
-       .notifier_call = swap_cpu_notify,
-};
-
 static int do_swap_sampler_start(unsigned int timer_quantum)
 {
+       int ret;
        if (timer_quantum <= 0)
                return -EINVAL;
 
        sampler_timers_set_quantum(timer_quantum);
-       swap_timer_start();
+       sampler_timers_set_run();
 
-       return 0;
+       ret = cpu_notifier_start(cpu_setup, cpu_shutdown);
+       if (ret) {
+               print_err("Cannot start sampler, ret=%d\n", ret);
+               sampler_timers_set_stop();
+       }
+
+       return ret;
 }
 
 static void do_swap_sampler_stop(void)
 {
-       swap_timer_stop();
+       cpu_notifier_stop();
+       sampler_timers_set_stop();
 }
 
+
 static DEFINE_MUTEX(mutex_run);
 static int sampler_run;
 
@@ -159,7 +129,7 @@ int swap_sampler_stop(void)
 
        mutex_lock(&mutex_run);
        if (sampler_run == 0) {
-               printk(KERN_INFO "energy profiling is not running!\n");
+               pr_warn("sampler profiling is not running!\n");
                ret = -EINVAL;
                goto unlock;
        }
@@ -176,16 +146,9 @@ EXPORT_SYMBOL_GPL(swap_sampler_stop);
 
 static int sampler_init(void)
 {
-       int retval;
-
-       retval = register_hotcpu_notifier(&swap_cpu_notifier);
-       if (retval) {
-               print_err("Error of register_hotcpu_notifier()\n");
-               return retval;
-       }
+       BUG_ON(sampler_run);
 
        print_msg("Sample ininitialization success\n");
-
        return 0;
 }
 
@@ -193,8 +156,7 @@ static void sampler_uninit(void)
 {
        if (sampler_run)
                do_swap_sampler_stop();
-
-       unregister_hotcpu_notifier(&swap_cpu_notifier);
+       sampler_run = 0;
 
        print_msg("Sampler uninitialized\n");
 }