概述
内核线程绑定到CPU上执行测试代码:
From 12f206e044f467919180aa26148eaacd82964ece Mon Sep 17 00:00:00 2001
From: Chen Jun <675816156@qq.com>
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 <linux/time.h>
#include <linux/kernel_stat.h>
#include <linux/cputime.h>
+#include <linux/kthread.h>
+#include <linux/delay.h>
+
+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%dn", __func__, cpu);
+
+ msleep(10000);
+ pr_err("%s end on cpu%dn", __func__, cpu);
+
+ if ((val = atomic64_inc_return(&count)) >= NR_CPUS) {
+ pr_err("%s %d end atomic %lldn", __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_cpun");
+ return ;
+ }
+ }
+
+ for_each_online_cpu(cpu) {
+ ret = wake_up_process(kthread[cpu]);
+ pr_err("wake_up_process %d ret %dn", cpu, ret);
+ }
+ wait_for_completion(&thread_done);
+ pr_err("%s %d completedn", __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
最后
以上就是幸福曲奇为你收集整理的内核线程绑定CPU的全部内容,希望文章能够帮你解决内核线程绑定CPU所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复