読者です 読者をやめる 読者になる 読者になる

φ(.. )メモシテオコウ on_each_cpu()の単なるめも

kernel linux

on_each_cpu()の単なるめも

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/cpumask.h>
#include <linux/smp.h>

MODULE_DESCRIPTION("hello module");
MODULE_AUTHOR("masami256");
MODULE_LICENSE("GPL");

static atomic_t count;

static void 
foobar(void *info)
{
	printk(KERN_INFO "CPU[%d] hello world\n", smp_processor_id());
	atomic_inc(&count);
}

static int
hello_init(void)
{
	printk(KERN_INFO "num_online_cpus()    : %d\n", num_online_cpus());
	printk(KERN_INFO "num_possible_cpus()  : %d\n", num_possible_cpus());

	on_each_cpu(&foobar, NULL, 0);

	while (atomic_read(&count) != num_online_cpus())
		cpu_relax();

	printk(KERN_INFO "Done\n");

	return 0;
}

static void
hello_cleanup(void)
{
}

module_init(hello_init);
module_exit(hello_cleanup);
masami@saga:~/codes/hello$ sudo journalctl -k
~ 略 ~
Nov 27 19:09:56 saga kernel: num_online_cpus()    : 8
Nov 27 19:09:56 saga kernel: num_possible_cpus()  : 8
Nov 27 19:09:56 saga kernel: CPU[4] hello world
Nov 27 19:09:56 saga kernel: CPU[2] hello world
Nov 27 19:09:56 saga kernel: CPU[3] hello world
Nov 27 19:09:56 saga kernel: CPU[6] hello world
Nov 27 19:09:56 saga kernel: CPU[7] hello world
Nov 27 19:09:56 saga kernel: CPU[0] hello world
Nov 27 19:09:56 saga kernel: CPU[1] hello world
Nov 27 19:09:56 saga kernel: CPU[5] hello world
Nov 27 19:09:56 saga kernel: Done

なるほど。