per-CPU变量
per-CPU变量是一种存在与每个CPU本地的变量,对于每一种per-CPU变量,每个CPU在本地都有一份它的副本。
per-CPU变量的优点
- 多处理器系统(smp)中无需考虑与其他处理器的竞争问题(并非绝对的)
- 可以利用处理器本地的cache硬件,提高访问速度
per-CPU变量的分类
按照分配内存空间的类型来看,有两种:
- 静态per-CPU变量
- 动态per-CPU变量
静态的per-CPU变量在编译期就静态分配,动态per-CPU变量在程序运行时动态分配。
per-CPU变量的实现机制
静态per-CPU变量
相关宏
90 #ifndef PER_CPU_BASE_SECTION
91 #ifdef CONFIG_SMP
92 #define PER_CPU_BASE_SECTION ".data..percpu"
93 #else
94 #define PER_CPU_BASE_SECTION ".data"
95 #endif
96 #endif
定义和声明
4/*
5 * Base implementations of per-CPU variable declarations and definitions, where
6 * the section in which the variable is to be placed is provided by the
7 * 'sec' argument. This may be used to affect the parameters governing the
8 * variable's storage.
9 *
/*
per-CPU变量声明和定义的基础实现,变量被放置的section由sec参数提供,这被用于影响管理参数变量的存储方式
注意!用于声明和定义的sections必须匹配,以免由于编译器生成错误的代码来访问该节而发生链接错误。
*/
10 * NOTE! The sections for the DECLARE and for the DEFINE must match, lest
11 * linkage errors occur due the compiler generating the wrong code to access
12 * that section.
13 */
14#define __PCPU_ATTRS(sec) \
15 __percpu __attribute__((section(PER_CPU_BASE_SECTION sec))) \
16 PER_CPU_ATTRIBUTES
17
18#define __PCPU_DUMMY_ATTRS \
19 __attribute__((section(".discard"), unused))
20#ifdef HAVE_MODEL_SMALL_ATTRIBUTE
21# define PER_CPU_ATTRIBUTES __attribute__(