8 static struct cpu_map *cpu_map__default_new(void)
13 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
17 cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
20 for (i = 0; i < nr_cpus; ++i)
29 static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
31 size_t payload_size = nr_cpus * sizeof(int);
32 struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
36 memcpy(cpus->map, tmp_cpus, payload_size);
42 struct cpu_map *cpu_map__read(FILE *file)
44 struct cpu_map *cpus = NULL;
46 int *tmp_cpus = NULL, *tmp;
54 n = fscanf(file, "%u%c", &cpu, &sep);
58 int new_max = nr_cpus + cpu - prev - 1;
60 if (new_max >= max_entries) {
61 max_entries = new_max + MAX_NR_CPUS / 2;
62 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
69 tmp_cpus[nr_cpus++] = prev;
71 if (nr_cpus == max_entries) {
72 max_entries += MAX_NR_CPUS;
73 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
79 tmp_cpus[nr_cpus++] = cpu;
80 if (n == 2 && sep == '-')
84 if (n == 1 || sep == '\n')
89 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
91 cpus = cpu_map__default_new();
97 static struct cpu_map *cpu_map__read_all_cpu_map(void)
99 struct cpu_map *cpus = NULL;
102 onlnf = fopen("/sys/devices/system/cpu/online", "r");
104 return cpu_map__default_new();
106 cpus = cpu_map__read(onlnf);
111 struct cpu_map *cpu_map__new(const char *cpu_list)
113 struct cpu_map *cpus = NULL;
114 unsigned long start_cpu, end_cpu = 0;
117 int *tmp_cpus = NULL, *tmp;
121 return cpu_map__read_all_cpu_map();
123 if (!isdigit(*cpu_list))
126 while (isdigit(*cpu_list)) {
128 start_cpu = strtoul(cpu_list, &p, 0);
129 if (start_cpu >= INT_MAX
130 || (*p != '\0' && *p != ',' && *p != '-'))
136 end_cpu = strtoul(cpu_list, &p, 0);
138 if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
141 if (end_cpu < start_cpu)
147 for (; start_cpu <= end_cpu; start_cpu++) {
148 /* check for duplicates */
149 for (i = 0; i < nr_cpus; i++)
150 if (tmp_cpus[i] == (int)start_cpu)
153 if (nr_cpus == max_entries) {
154 max_entries += MAX_NR_CPUS;
155 tmp = realloc(tmp_cpus, max_entries * sizeof(int));
160 tmp_cpus[nr_cpus++] = (int)start_cpu;
169 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
171 cpus = cpu_map__default_new();
178 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
181 size_t printed = fprintf(fp, "%d cpu%s: ",
182 map->nr, map->nr > 1 ? "s" : "");
183 for (i = 0; i < map->nr; ++i)
184 printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]);
186 return printed + fprintf(fp, "\n");
189 struct cpu_map *cpu_map__dummy_new(void)
191 struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
201 void cpu_map__delete(struct cpu_map *map)
206 int cpu_map__get_socket(struct cpu_map *map, int idx)
218 mnt = sysfs_find_mountpoint();
223 "%s/devices/system/cpu/cpu%d/topology/physical_package_id",
226 fp = fopen(path, "r");
229 ret = fscanf(fp, "%d", &cpu);
231 return ret == 1 ? cpu : -1;
234 int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
236 struct cpu_map *sock;
240 sock = calloc(1, sizeof(*sock) + nr * sizeof(int));
244 for (cpu = 0; cpu < nr; cpu++) {
245 s1 = cpu_map__get_socket(cpus, cpu);
246 for (s2 = 0; s2 < sock->nr; s2++) {
247 if (s1 == sock->map[s2])
250 if (s2 == sock->nr) {
251 sock->map[sock->nr] = s1;