我的问题主要是关于硬件,特别是英特尔 i5-2500K CPU英特尔将其描述为具有
核心数量 4
线程数量 4
Linux 显示了 4 个处理器:
$ cat /proc/cpuinfo | grep ^processor
processor : 0
processor : 1
processor : 2
processor : 3
尽管如此,我还是写了一个小内核模块来显示 8 个处理器:
$ cat show_cpus_mod.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/version.h>
#define CLASS_NAME "show_cpus_mod"
#define dbg( format, arg... ) do { if ( debug ) pr_info( CLASS_NAME ": %s: " format , __FUNCTION__ , ## arg ); } while ( 0 )
#define err( format, arg... ) pr_err( CLASS_NAME ": " format, ## arg )
#define info( format, arg... ) pr_info( CLASS_NAME ": " format, ## arg )
#define warn( format, arg... ) pr_warn( CLASS_NAME ": " format, ## arg )
MODULE_DESCRIPTION( "shows all cpus" );
MODULE_VERSION( "0.1" );
MODULE_LICENSE( "GPL" );
MODULE_AUTHOR( "author <[email protected]>" );
static int show_cpus_mod_init( void ) {
int cpu;
info( "Start loading module show_cpus_mod.\n" );
for_each_possible_cpu( cpu ) {
info( "cpu = %d\n", cpu );
}
return 0;
}
static void show_cpus_mod_exit( void ) {
info( "Module show_cpus_mod unloaded\n" );
}
module_init( show_cpus_mod_init );
module_exit( show_cpus_mod_exit );
建筑:
$ cat Makefile
CURRENT = $(shell uname -r)
KDIR = /lib/modules/$(CURRENT)/build
PWD = $(shell pwd)
TARGET = show_cpus_mod
obj-m := $(TARGET).o
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
@rm -f *.o .*.cmd .*.flags *.mod.c *.order
@rm -f .*.*.cmd *.symvers *~ *.*~ TODO.*
@rm -fR .tmp*
@rm -rf .tmp_versions
插入:
# make
# cp show_cpus_mod.ko /lib/modules/4.14.0-kali3-amd64/
# depmod
# modprobe show_cpus_mod
系统日志:
localhost kernel: [67596.578805] show_cpus_mod: Start loading module show_cpus_mod.
localhost kernel: [67596.578808] show_cpus_mod: cpu = 0
localhost kernel: [67596.578809] show_cpus_mod: cpu = 1
localhost kernel: [67596.578810] show_cpus_mod: cpu = 2
localhost kernel: [67596.578811] show_cpus_mod: cpu = 3
localhost kernel: [67596.578811] show_cpus_mod: cpu = 4
localhost kernel: [67596.578812] show_cpus_mod: cpu = 5
localhost kernel: [67596.578812] show_cpus_mod: cpu = 6
localhost kernel: [67596.578813] show_cpus_mod: cpu = 7
localhost kernel: [67607.725738] show_cpus_mod: Module show_cpus_mod unloaded
我在英特尔的描述中遗漏了什么?为什么是8?或者我的内核模块有什么问题?
答案1
您应该使用for_each_online_cpu
orfor_each_present_cpu
代替for_each_possible_cpu
.这将分别限制真正在线或存在的 CPU 的输出。