per_CPU 变量,顾名思义就是每一个cpu都是一个独立的值,互不影响。
1 定义:
static DEFINE_PER_CPU(long, my_counter) = 0;
2 自增自减操作.
this_cpu_inc(my_counter);
this_cpu_dec(my_counter);
注意 get_cpu 和 put_cpu 必须成对出现.
3 写操作:
this_cpu_write(my_counter,8);
4 读操作:
int a = this_cpu_read(my_counter);
3 获取所有CPU上的总值
int total = 0;
int cpu;
pr_info("a3 my_close\n");
for_each_possible_cpu(cpu)
{
int tmp= per_cpu(my_counter, cpu);
pr_info("cpu: %d,has %d\n",cpu,tmp);
total +=tmp;
}
pr_info("total access: %d\n", total);
以下是所有this_cpu的函数:
this_cpu_read(pcp)
this_cpu_write(pcp, val)
this_cpu_add(pcp, val)
this_cpu_and(pcp, val)
this_cpu_or(pcp, val)
this_cpu_add_return(pcp, val)
this_cpu_xchg(pcp, nval)
this_cpu_cmpxchg(pcp, oval, nval)
this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
this_cpu_sub(pcp, val)
this_cpu_inc(pcp)
this_cpu_dec(pcp)
this_cpu_sub_return(pcp, val)
this_cpu_inc_return(pcp)
this_cpu_dec_return(pcp)