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 struct cpu_map *cpu_map__read(FILE *file)
43 struct cpu_map *cpus = NULL;
45 int *tmp_cpus = NULL, *tmp;
53 n = fscanf(file, "%u%c", &cpu, &sep);
57 int new_max = nr_cpus + cpu - prev - 1;
59 if (new_max >= max_entries) {
60 max_entries = new_max + MAX_NR_CPUS / 2;
61 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
68 tmp_cpus[nr_cpus++] = prev;
70 if (nr_cpus == max_entries) {
71 max_entries += MAX_NR_CPUS;
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)
98 struct cpu_map *cpus = NULL;
101 onlnf = fopen("/sys/devices/system/cpu/online", "r");
103 return cpu_map__default_new();
105 cpus = cpu_map__read(onlnf);
110 struct cpu_map *cpu_map__new(const char *cpu_list)
112 struct cpu_map *cpus = NULL;
113 unsigned long start_cpu, end_cpu = 0;
116 int *tmp_cpus = NULL, *tmp;
120 return cpu_map__read_all_cpu_map();
122 if (!isdigit(*cpu_list))
125 while (isdigit(*cpu_list)) {
127 start_cpu = strtoul(cpu_list, &p, 0);
128 if (start_cpu >= INT_MAX
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++) {
147 /* check for duplicates */
148 for (i = 0; i < nr_cpus; i++)
149 if (tmp_cpus[i] == (int)start_cpu)
152 if (nr_cpus == max_entries) {
153 max_entries += MAX_NR_CPUS;
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();
177 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
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");
188 struct cpu_map *cpu_map__dummy_new(void)
190 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
200 void cpu_map__delete(struct cpu_map *map)