【cpumask】

cpumask

首先我们来分析一下定义在cpumask.h中 结构体cpumask_t

  1. typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
  2. #define DECLARE_BITMAP(name,bits) \
  3. unsigned long name[BITS_TO_LONGS(bits)]
  4. #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
  5. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))

假设我们当前使用的cpu核数为24,即NR_CPUS=24,sizeof(long)=8,BITS_PER_BYTE=8,则在DIV_ROUND_UP(n,d)中, n = 24,d=64,宏的展开结果为(24+64-1)/64 = 1,DECLARE_BITMAP(name,bits) 展开后即为 unsigned long name[1],最后cpumask即为:

struct cpumask{

unsigned long bits[1];

};

绕了好大一个圈,就定义了一个unsigned long bits[1],想想就明白了,64位机器上,一个long有64个bit,而只有24个核,所以一个long足够表示了。

在linux内核中,cpu_possible_mask 位图,用来表示系统中的CPU,每颗处理器对应其中一位,
cpu_online_mask 位图,用来当前处于工作状态的CPU,每颗处理器对应其中一位

猜你喜欢

转载自blog.csdn.net/feifei_csdn/article/details/80845644