linux -- per-CPU变量

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__(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值