1 #ifndef LIBOMP_TEST_AFFINITY_H
2 #define LIBOMP_TEST_AFFINITY_H
11 typedef struct affinity_mask_t
{
17 // VE's sched_getaffinity put garbage if the buffer is too big. 512 is
18 // a good number to make all tests run correctly.
19 #define AFFINITY_MAX_CPUS 512
21 #define AFFINITY_MAX_CPUS (32 * 64)
24 // Operating system affinity mask API
25 static void affinity_mask_zero(affinity_mask_t
*mask
) {
26 CPU_ZERO_S(mask
->setsize
, mask
->set
);
29 static affinity_mask_t
*affinity_mask_alloc() {
30 size_t setsize
= CPU_ALLOC_SIZE(AFFINITY_MAX_CPUS
);
31 cpu_set_t
*set
= CPU_ALLOC(AFFINITY_MAX_CPUS
);
32 affinity_mask_t
*retval
= (affinity_mask_t
*)malloc(sizeof(affinity_mask_t
));
33 retval
->setsize
= setsize
;
35 affinity_mask_zero(retval
);
39 static void affinity_mask_free(affinity_mask_t
*mask
) { CPU_FREE(mask
->set
); }
41 static void affinity_mask_copy(affinity_mask_t
*dest
,
42 const affinity_mask_t
*src
) {
43 memcpy(dest
->set
, src
->set
, dest
->setsize
);
46 static void affinity_mask_set(affinity_mask_t
*mask
, int cpu
) {
47 CPU_SET_S(cpu
, mask
->setsize
, mask
->set
);
50 static void affinity_mask_clr(affinity_mask_t
*mask
, int cpu
) {
51 CPU_CLR_S(cpu
, mask
->setsize
, mask
->set
);
54 static int affinity_mask_isset(const affinity_mask_t
*mask
, int cpu
) {
55 return CPU_ISSET_S(cpu
, mask
->setsize
, mask
->set
);
58 static int affinity_mask_count(const affinity_mask_t
*mask
) {
59 return CPU_COUNT_S(mask
->setsize
, mask
->set
);
62 static int affinity_mask_equal(const affinity_mask_t
*mask1
,
63 const affinity_mask_t
*mask2
) {
64 return CPU_EQUAL_S(mask1
->setsize
, mask1
->set
, mask2
->set
);
67 static void get_thread_affinity(affinity_mask_t
*mask
) {
68 if (sched_getaffinity(0, mask
->setsize
, mask
->set
) != 0) {
69 perror("sched_getaffinity()");
74 static void set_thread_affinity(const affinity_mask_t
*mask
) {
75 if (sched_setaffinity(0, mask
->setsize
, mask
->set
) != 0) {
76 perror("sched_setaffinity()");
81 static void affinity_update_snprintf_values(char **ptr
, size_t *remaining
,
82 size_t n
, size_t *retval
) {
83 if (n
> *remaining
&& *remaining
> 0) {
93 static size_t affinity_mask_snprintf(char *buf
, size_t bufsize
,
94 const affinity_mask_t
*mask
) {
95 int cpu
, need_comma
, begin
, end
;
98 size_t remaining
= bufsize
;
101 n
= snprintf(ptr
, remaining
, "%c", '{');
102 affinity_update_snprintf_values(&ptr
, &remaining
, n
, &retval
);
105 for (cpu
= 0; cpu
< AFFINITY_MAX_CPUS
; cpu
++) {
106 if (!affinity_mask_isset(mask
, cpu
))
109 n
= snprintf(ptr
, remaining
, "%c", ',');
110 affinity_update_snprintf_values(&ptr
, &remaining
, n
, &retval
);
113 // Find end of range (inclusive end)
114 for (end
= begin
+ 1; end
< AFFINITY_MAX_CPUS
; ++end
) {
115 if (!affinity_mask_isset(mask
, end
))
120 if (end
- begin
>= 2) {
121 n
= snprintf(ptr
, remaining
, "%d-%d", begin
, end
);
122 affinity_update_snprintf_values(&ptr
, &remaining
, n
, &retval
);
123 } else if (end
- begin
== 1) {
124 n
= snprintf(ptr
, remaining
, "%d,%d", begin
, end
);
125 affinity_update_snprintf_values(&ptr
, &remaining
, n
, &retval
);
126 } else if (end
- begin
== 0) {
127 n
= snprintf(ptr
, remaining
, "%d", begin
);
128 affinity_update_snprintf_values(&ptr
, &remaining
, n
, &retval
);
133 n
= snprintf(ptr
, remaining
, "%c", '}');
134 affinity_update_snprintf_values(&ptr
, &remaining
, n
, &retval
);