7 static struct cpu_map
*cpu_map__default_new(void)
12 nr_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
16 cpus
= malloc(sizeof(*cpus
) + nr_cpus
* sizeof(int));
19 for (i
= 0; i
< nr_cpus
; ++i
)
28 static struct cpu_map
*cpu_map__trim_new(int nr_cpus
, int *tmp_cpus
)
30 size_t payload_size
= nr_cpus
* sizeof(int);
31 struct cpu_map
*cpus
= malloc(sizeof(*cpus
) + payload_size
);
35 memcpy(cpus
->map
, tmp_cpus
, payload_size
);
41 static struct cpu_map
*cpu_map__read_all_cpu_map(void)
43 struct cpu_map
*cpus
= NULL
;
46 int *tmp_cpus
= NULL
, *tmp
;
51 onlnf
= fopen("/sys/devices/system/cpu/online", "r");
53 return cpu_map__default_new();
58 n
= fscanf(onlnf
, "%u%c", &cpu
, &sep
);
62 int new_max
= nr_cpus
+ cpu
- prev
- 1;
64 if (new_max
>= max_entries
) {
65 max_entries
= new_max
+ MAX_NR_CPUS
/ 2;
66 tmp
= realloc(tmp_cpus
, max_entries
* sizeof(int));
73 tmp_cpus
[nr_cpus
++] = prev
;
75 if (nr_cpus
== max_entries
) {
76 max_entries
+= MAX_NR_CPUS
;
77 tmp
= realloc(tmp_cpus
, max_entries
* sizeof(int));
83 tmp_cpus
[nr_cpus
++] = cpu
;
84 if (n
== 2 && sep
== '-')
88 if (n
== 1 || sep
== '\n')
93 cpus
= cpu_map__trim_new(nr_cpus
, tmp_cpus
);
95 cpus
= cpu_map__default_new();
102 struct cpu_map
*cpu_map__new(const char *cpu_list
)
104 struct cpu_map
*cpus
= NULL
;
105 unsigned long start_cpu
, end_cpu
= 0;
108 int *tmp_cpus
= NULL
, *tmp
;
112 return cpu_map__read_all_cpu_map();
114 if (!isdigit(*cpu_list
))
117 while (isdigit(*cpu_list
)) {
119 start_cpu
= strtoul(cpu_list
, &p
, 0);
120 if (start_cpu
>= INT_MAX
121 || (*p
!= '\0' && *p
!= ',' && *p
!= '-'))
127 end_cpu
= strtoul(cpu_list
, &p
, 0);
129 if (end_cpu
>= INT_MAX
|| (*p
!= '\0' && *p
!= ','))
132 if (end_cpu
< start_cpu
)
138 for (; start_cpu
<= end_cpu
; start_cpu
++) {
139 /* check for duplicates */
140 for (i
= 0; i
< nr_cpus
; i
++)
141 if (tmp_cpus
[i
] == (int)start_cpu
)
144 if (nr_cpus
== max_entries
) {
145 max_entries
+= MAX_NR_CPUS
;
146 tmp
= realloc(tmp_cpus
, max_entries
* sizeof(int));
151 tmp_cpus
[nr_cpus
++] = (int)start_cpu
;
160 cpus
= cpu_map__trim_new(nr_cpus
, tmp_cpus
);
162 cpus
= cpu_map__default_new();
169 struct cpu_map
*cpu_map__dummy_new(void)
171 struct cpu_map
*cpus
= malloc(sizeof(*cpus
) + sizeof(int));
181 void cpu_map__delete(struct cpu_map
*map
)