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));
28 static struct cpu_map *cpu_map__trim_new(
int nr_cpus,
int *tmp_cpus)
35 memcpy(cpus->
map, tmp_cpus, payload_size);
53 n = fscanf(file,
"%u%c", &cpu, &sep);
57 int new_max = nr_cpus + cpu - prev - 1;
59 if (new_max >= max_entries) {
61 tmp = realloc(tmp_cpus, max_entries *
sizeof(
int));
68 tmp_cpus[nr_cpus++] =
prev;
70 if (nr_cpus == max_entries) {
72 tmp = realloc(tmp_cpus, max_entries *
sizeof(
int));
78 tmp_cpus[nr_cpus++] =
cpu;
79 if (n == 2 && sep ==
'-')
83 if (n == 1 || sep ==
'\n')
88 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
90 cpus = cpu_map__default_new();
96 static struct cpu_map *cpu_map__read_all_cpu_map(
void)
101 onlnf = fopen(
"/sys/devices/system/cpu/online",
"r");
103 return cpu_map__default_new();
113 unsigned long start_cpu, end_cpu = 0;
120 return cpu_map__read_all_cpu_map();
127 start_cpu = strtoul(cpu_list, &p, 0);
129 || (*p !=
'\0' && *p !=
',' && *p !=
'-'))
135 end_cpu = strtoul(cpu_list, &p, 0);
137 if (end_cpu >=
INT_MAX || (*p !=
'\0' && *p !=
','))
140 if (end_cpu < start_cpu)
146 for (; start_cpu <= end_cpu; start_cpu++) {
149 if (tmp_cpus[i] == (
int)start_cpu)
152 if (nr_cpus == max_entries) {
154 tmp = realloc(tmp_cpus, max_entries *
sizeof(
int));
159 tmp_cpus[nr_cpus++] = (
int)start_cpu;
168 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
170 cpus = cpu_map__default_new();
180 size_t printed =
fprintf(fp,
"%d cpu%s: ",
181 map->
nr, map->
nr > 1 ?
"s" :
"");
182 for (i = 0; i < map->
nr; ++
i)
183 printed +=
fprintf(fp,
"%s%d", i ?
", " :
"", map->
map[i]);
185 return printed +
fprintf(fp,
"\n");