Skip to content
interwq edited this page Feb 26, 2013 · 2 revisions

PERCPU macro usage:

(both in user and kernel)

definition:

PERCPU(type, name) //defines a cache-aligned per cpu variable, which can be a structure
PERCPU_ATTR(attr, type, name) // with attribute

for definition of external variables, please see PERCPU_DECL and PERCPU_EXTERN in src/kernel/include/shared/cos_types.h for details.

reference:

PERCPU_GET(name) // returns the address of the variable on the current cpu
PERCPU_GET_TARGET(name, target) // returns the address of the variable on the TARGET cpu
/* It touches remote data so involves cache migration, which isn't free and could limit scalability */

example:

struct test {
	int a,b;
};

PERCPU(struct test, percpu_var);
 
int access_test() {

    struct test *ptr = PERCPU_GET(percpu_var);

    int a = ptr->a;

    ptr->b = 2;

}