内核线程绑定CPU

内核线程绑定到CPU上执行测试代码:

From 12f206e044f467919180aa26148eaacd82964ece Mon Sep 17 00:00:00 2001
From: Chen Jun <[email protected]>
Date: Thu, 7 Dec 2017 08:24:52 -0800
Subject: [PATCH] bind kthread on cpu Test

---
 arch/arm64/Kconfig |  2 +-
 fs/proc/uptime.c   | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 6df1e6e..ecbb44b 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -580,7 +580,7 @@ config NR_CPUS
    int "Maximum number of CPUs (2-4096)"
    range 2 4096
    # These have to remain sorted largest to smallest
-   default "64"
+   default "8"

 config HOTPLUG_CPU
    bool "Support for hot-pluggable CPUs"
diff --git a/fs/proc/uptime.c b/fs/proc/uptime.c
index 33de567..c237260 100644
--- a/fs/proc/uptime.c
+++ b/fs/proc/uptime.c
@@ -6,6 +6,54 @@
 #include 
 #include 
 #include 
+#include 
+#include 
+
+static DECLARE_COMPLETION(thread_done);
+static atomic_long_t count = ATOMIC64_INIT(0);
+
+static int thread_func(void *arg)
+{
+   int cpu;
+   long long val;
+
+   cpu = *(int *)arg;
+   pr_err("%s start on cpu%d\n", __func__, cpu);
+
+   msleep(10000);
+   pr_err("%s end on cpu%d\n", __func__, cpu);
+
+   if ((val = atomic64_inc_return(&count)) >= NR_CPUS) {
+       pr_err("%s %d end atomic %lld\n", __func__, cpu, val);
+       complete(&thread_done);
+   }
+
+   return 0;
+}
+
+static void self_test(void)
+{
+   int cpu, ret;
+   struct task_struct *kthread[8] = {0};
+
+   init_completion(&thread_done);
+   memset(&count, 0, sizeof(count));
+
+   for_each_online_cpu(cpu) {
+       kthread[cpu] = kthread_create_on_cpu(thread_func, &cpu, cpu, "cjthread/%d");
+       if (IS_ERR(kthread[cpu])) {
+           pr_err("fail to kthread_create_on_cpu\n");
+           return ;
+       }
+   }
+
+   for_each_online_cpu(cpu) {
+       ret = wake_up_process(kthread[cpu]);
+       pr_err("wake_up_process %d ret %d\n", cpu, ret);
+   }
+   wait_for_completion(&thread_done);
+   pr_err("%s %d completed\n", __func__, __LINE__);
+}

 static int uptime_proc_show(struct seq_file *m, void *v)
 {
@@ -16,6 +64,7 @@ static int uptime_proc_show(struct seq_file *m, void *v)
    u32 rem;
    int i;

+   self_test();
    idletime = 0;
    for_each_possible_cpu(i)
        idletime += (__force u64) kcpustat_cpu(i).cpustat[CPUTIME_IDLE];
-- 
2.7.4

你可能感兴趣的:(Linux)