日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

Linux per-cpu机制

發(fā)布時(shí)間:2023/12/31 linux 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux per-cpu机制 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Linux操作系統(tǒng),特別是針對(duì)SMP或者NUMA架構(gòu)的多CPU系統(tǒng)的時(shí)候,描述每個(gè)CPU的私有數(shù)據(jù)的時(shí)候,Linux操作系統(tǒng)提供了per_cpu機(jī)制。?

1.1???? 定義

per_cpu機(jī)制就是讓每個(gè)CPU都有自己的私有數(shù)據(jù)段,便于保護(hù)與訪問(wèn)。

相關(guān)宏定義在include/linux/percpu-defs.h文件中:

/*?????

?* Normal declaration and definition macros.

?*/

#define DECLARE_PER_CPU_SECTION(type, name, sec)??????????????????????? \

??????? extern __PCPU_ATTRS(sec) __typeof__(type) name

?

#define DEFINE_PER_CPU_SECTION(type, name, sec)???????????????????????? \

??????? __PCPU_ATTRS(sec) PER_CPU_DEF_ATTRIBUTES??????????????????????? \

??????? __typeof__(type) name

#endif

?

/*

?* Variant on the per-CPU variable declaration/definition theme used for

?* ordinary per-CPU variables.

?*/

#define DECLARE_PER_CPU(type, name)???????????????????????????????????? \

??????? DECLARE_PER_CPU_SECTION(type, name, "")

?

#define DEFINE_PER_CPU(type, name)????????????????????????????????????? \

??????? DEFINE_PER_CPU_SECTION(type, name, "")

include/linux/percpu-defs.h文件

#define __PCPU_ATTRS(sec)?????????????????????????????????????????????? \

??????? __percpu __attribute__((section(PER_CPU_BASE_SECTION sec)))???? \

??????? PER_CPU_ATTRIBUTES

這個(gè)__PCPU_ATTRS是每個(gè)CPU變量聲明和定義的最基本實(shí)現(xiàn)。總之就是創(chuàng)建CPU的一個(gè)私有變量。其中#define PER_CPU_BASE_SECTION ".data..percpu"定義其存放的數(shù)據(jù)段,定義在arch/ia64/include/asm/percpu.h文件中。

1.2???? 示例

例如定義描述每個(gè)IA64CPU信息的數(shù)據(jù)結(jié)構(gòu)變量ia64_cpu_info, 在文件

arch/ia64/include/asm/processor.hDEFINE_PER_CPU,定義這種私有數(shù)據(jù),放在特定的數(shù)據(jù)段中。

DECLARE_PER_CPU(struct cpuinfo_ia64, ia64_cpu_info);

在文件arch/ia64/include/asm/processor.h中,定義了結(jié)構(gòu)體cpuinfo_ia64。其中定義了CPU類型,硬件BUG標(biāo)志, CPU狀態(tài)等。

struct cpuinfo_ia64 {

??????? unsigned int softirq_pending;??

??????? unsigned long itm_delta;??????? /* # of clock cycles between clock ticks */

??? ????unsigned long itm_next;???????? /* interval timer mask value to use for next clock tick */

??????? unsigned long nsec_per_cyc;???? /* (1000000000<<IA64_NSEC_PER_CYC_SHIFT)/itc_freq */

??????? unsigned long unimpl_va_mask;?? /* mask of unimplemented virtual address bits (from PAL) */

??????? unsigned long unimpl_pa_mask;?? /* mask of unimplemented physical address bits (from PAL) */

??????? unsigned long itc_freq;???????? /* frequency of ITC counter */

??????? unsigned long proc_freq;??????? /* frequency of processor */

??????? unsigned long cyc_per_usec;???? /* itc_freq/1000000 */

??????? unsigned long ptce_base;

??????? unsigned int ptce_count[2];

??????? unsigned int ptce_stride[2];

??????? struct task_struct *ksoftirqd;? /* kernel softirq daemon for this CPU */

?

#ifdef CONFIG_SMP

??????? unsigned long loops_per_jiffy;

??????? int cpu;

??????? unsigned int socket_id; /* physical processor socket id */

??????? unsigned short core_id; /* core id */

??????? unsigned short thread_id; /* thread id */

????? ??unsigned short num_log; /* Total number of logical processors on

???????????????????????????????? * this socket that were successfully booted */

??????? unsigned char cores_per_socket; /* Cores per processor socket */

??????? unsigned char threads_per_core; /* Threads per core */

#endif?

???????

??????? /* CPUID-derived information: */

??????? unsigned long ppn;

??????? unsigned long features;

??????? unsigned char number;

??????? unsigned char revision;

??????? unsigned char model;

??????? unsigned char family;

??????? unsigned char archrev;

??????? char vendor[16];

??????? char *model_name;

?

#ifdef CONFIG_NUMA

??????? struct ia64_node_data *node_data;

#endif

};

1.3???? 初始化

  在start_kernel函數(shù)中調(diào)用執(zhí)行函數(shù)setup_per_cpu_areas( ),其會(huì)將.data.percpu中的數(shù)據(jù)拷貝到每個(gè)CPU的數(shù)據(jù)段中,每個(gè)CPU一份。?其中CPU n 對(duì)應(yīng)的專有數(shù)據(jù)區(qū)的首地址為_(kāi)_per_cpu_offset[n]。?

1.4???? 讀取變量

在不同的內(nèi)核版本中,略有差異,這里取的是最新的2018/3/19日的主線版本中代碼。include/linux/percpu-defs.h

#define get_cpu_var(var)??????????????????????????????????????????????? \

(*({?????????????????????????????????????????????????????????? ?????????\

??????? preempt_disable();????????????????????????????????????????????? \

??????? this_cpu_ptr(&var);???????????????????????????????????????????? \

}))

?

總結(jié)

以上是生活随笔為你收集整理的Linux per-cpu机制的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。