aboutsummaryrefslogtreecommitdiff
path: root/topology.c
blob: 6f5699ddd4bbf551636d07e8da744ef576702fc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*****************************************************************************
 * Copyright (C) 2013, Linaro Limited.
 *
 * This file is part of cpu_topology.
 *
 * All rights reserved.
 *
 * Contributors:
 *     Shaojie Sun <shaojie.sun@linaro.org> (Hislicon technologies)
 *       - initial API and implementation
 *
 *****************************************************************************/

#define  _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/stat.h>

#include "list.h"
#include "utils.h"
#include "topology.h"
#include "idlestat.h"

struct cpu_topology g_cpu_topo_list;

struct topology_info {
	int physical_id;
	int core_id;
	int cpu_id;
};

struct list_info {
	struct list_head hlist;
	int id;
};

struct list_head *check_exist_from_head(struct list_head *head, int id)
{
	struct list_head *tmp;

	list_for_each(tmp, head) {
		if (id == ((struct list_info *)tmp)->id)
			return tmp;
	}

	return NULL;
}

struct list_head *check_pos_from_head(struct list_head *head, int id)
{
	struct list_head *tmp;

	list_for_each(tmp, head) {
		if (id < ((struct list_info *)tmp)->id)
			break;
	}

	return tmp->prev;
}

int add_topo_info(struct cpu_topology *topo_list, struct topology_info *info)
{
	struct cpu_physical *s_phy;
	struct cpu_core     *s_core;
	struct cpu_cpu      *s_cpu = NULL;
	struct list_head    *ptr;

	/* add cpu physical info */
	ptr = check_exist_from_head(&topo_list->physical_head,
					info->physical_id);
	if (!ptr) {
		s_phy = calloc(sizeof(struct cpu_physical), 1);
		if (!s_phy)
			return -1;

		s_phy->core_num = 0;
		s_phy->physical_id = info->physical_id;
		INIT_LIST_HEAD(&s_phy->core_head);

		ptr = check_pos_from_head(&topo_list->physical_head,
						s_phy->physical_id);
		list_add(&s_phy->list_physical, ptr);
		topo_list->physical_num++;
	} else {
		s_phy = list_entry(ptr, struct cpu_physical,
						list_physical);
	}

	/* add cpu core info */
	ptr = check_exist_from_head(&s_phy->core_head, info->core_id);
	if (!ptr) {
		s_core = calloc(sizeof(struct cpu_core), 1);
		if (!s_core)
			return -1;

		s_core->cpu_num = 0;
		s_core->is_ht = false;
		s_core->core_id = info->core_id;
		INIT_LIST_HEAD(&s_core->cpu_head);

		ptr = check_pos_from_head(&s_phy->core_head,
						s_core->core_id);
		list_add(&s_core->list_core, ptr);
		s_phy->core_num++;

	} else {
		s_core = list_entry(ptr, struct cpu_core, list_core);
	}

	/* add cpu info */
	ptr = check_exist_from_head(&s_core->cpu_head, info->cpu_id);
	if (!ptr) {
		s_cpu = calloc(sizeof(struct cpu_cpu), 1);
		if (!s_core)
			return -1;

		s_cpu->cpu_id = info->cpu_id;

		ptr = check_pos_from_head(&s_core->cpu_head, s_cpu->cpu_id);
		list_add(&s_cpu->list_cpu, ptr);
		s_core->cpu_num++;
		if (s_core->cpu_num > 1)
			s_core->is_ht = true;
	}

	return 0;
}

void free_cpu_cpu_list(struct list_head *head)
{
	struct cpu_cpu *lcpu, *n;

	list_for_each_entry_safe(lcpu, n, head, list_cpu) {
		list_del(&lcpu->list_cpu);
		free(lcpu);
	}
}

void free_cpu_core_list(struct list_head *head)
{
	struct cpu_core *lcore, *n;

	list_for_each_entry_safe(lcore, n, head, list_core) {
		free_cpu_cpu_list(&lcore->cpu_head);
		list_del(&lcore->list_core);
		free(lcore);
	}
}

void free_cpu_topology(struct list_head *head)
{
	struct cpu_physical *lphysical, *n;

	list_for_each_entry_safe(lphysical, n, head, list_physical) {
		free_cpu_core_list(&lphysical->core_head);
		list_del(&lphysical->list_physical);
		free(lphysical);
	}
}

int output_topo_info(struct cpu_topology *topo_list)
{
	struct cpu_physical *s_phy;
	struct cpu_core     *s_core;
	struct cpu_cpu      *s_cpu;

	list_for_each_entry(s_phy, &topo_list->physical_head, list_physical) {
		printf("cluster%c:\n", s_phy->physical_id + 'A');
		list_for_each_entry(s_core, &s_phy->core_head, list_core) {
			printf("\tcore%d\n", s_core->core_id);
			list_for_each_entry(s_cpu, &s_core->cpu_head, list_cpu)
				printf("\t\tcpu%d\n", s_cpu->cpu_id);
		}
	}

	return 0;
}

int outfile_topo_info(FILE *f, struct cpu_topology *topo_list)
{
	struct cpu_physical *s_phy;
	struct cpu_core     *s_core;
	struct cpu_cpu      *s_cpu;

	list_for_each_entry(s_phy, &topo_list->physical_head, list_physical) {
		fprintf(f, "cluster%c:\n", s_phy->physical_id + 'A');
		list_for_each_entry(s_core, &s_phy->core_head, list_core) {
			fprintf(f, "\tcore%d\n", s_core->core_id);
			list_for_each_entry(s_cpu, &s_core->cpu_head, list_cpu)
				fprintf(f, "\t\tcpu%d\n", s_cpu->cpu_id);
		}
	}

	return 0;
}

struct cpu_cpu *find_cpu_point(struct cpu_topology *topo_list, int cpuid)
{
	struct cpu_physical *s_phy;
	struct cpu_core     *s_core;
	struct cpu_cpu      *s_cpu;

	list_for_each_entry(s_phy, &topo_list->physical_head, list_physical)
		list_for_each_entry(s_core, &s_phy->core_head, list_core)
			list_for_each_entry(s_cpu, &s_core->cpu_head, list_cpu)
				if (s_cpu->cpu_id == cpuid)
					return s_cpu;

	return NULL;
}

static inline int read_topology_cb(char *path, struct topology_info *info)
{
	file_read_value(path, "core_id", "%d", &info->core_id);
	file_read_value(path, "physical_package_id", "%d", &info->physical_id);

	return 0;
}

typedef int (*folder_filter_t)(const char *name);

static int cpu_filter_cb(const char *name)
{
	/* let's ignore some directories in order to avoid to be
	 * pulled inside the sysfs circular symlinks mess/hell
	 * (choose the word which fit better)*/
	if (!strcmp(name, "cpuidle"))
		return 1;

	if (!strcmp(name, "cpufreq"))
		return 1;

	return 0;
}

/*
 * This function will browse the directory structure and build a
 * reflecting the content of the directory tree.
 *
 * @path   : the root node of the folder
 * @filter : a callback to filter out the directories
 * Returns 0 on success, -1 otherwise
 */
static int topo_folder_scan(char *path, folder_filter_t filter)
{
	DIR *dir, *dir_topology;
	char *basedir, *newpath;
	struct dirent dirent, *direntp;
	struct stat s;
	int ret = 0;

	dir = opendir(path);
	if (!dir) {
		printf("error: unable to open directory %s\n", path);
		return -1;
	}

	ret = asprintf(&basedir, "%s", path);
	if (ret < 0)
		return -1;

	while (!readdir_r(dir, &dirent, &direntp)) {

		if (!direntp)
			break;

		if (direntp->d_name[0] == '.')
			continue;

		if (filter && filter(direntp->d_name))
			continue;

		if (!strstr(direntp->d_name, "cpu"))
			continue;

		ret = asprintf(&newpath, "%s/%s/%s", basedir,
				direntp->d_name, "topology");
		if (ret < 0)
			goto out_free_basedir;

		ret = stat(newpath, &s);
		if (ret)
			goto out_free_newpath;

		if (S_ISDIR(s.st_mode) || (S_ISLNK(s.st_mode))) {
			struct topology_info cpu_info;

			dir_topology = opendir(path);
			if (!dir_topology)
				continue;

			read_topology_cb(newpath, &cpu_info);
			sscanf(direntp->d_name, "cpu%d", &cpu_info.cpu_id);
			add_topo_info(&g_cpu_topo_list, &cpu_info);
		}

	out_free_newpath:
		free(newpath);

		if (ret)
			break;
	}

out_free_basedir:
	free(basedir);

	closedir(dir);

	return ret;
}


int init_cpu_topo_info(void)
{
	INIT_LIST_HEAD(&g_cpu_topo_list.physical_head);
	g_cpu_topo_list.physical_num = 0;

	return 0;
}

int read_sysfs_cpu_topo(void)
{
	topo_folder_scan("/sys/devices/system/cpu", cpu_filter_cb);

	return 0;
}

int read_cpu_topo_info(FILE *f, char *buf)
{
	int ret = 0;
	struct topology_info cpu_info;
	bool is_ht = false;
	char pid;

	do {
		ret = sscanf(buf, "cluster%c", &pid);
		if (!ret)
			break;

		cpu_info.physical_id = pid - 'A';

		fgets(buf, BUFSIZE, f);
		do {
			ret = sscanf(buf, "\tcore%u", &cpu_info.core_id);
			if (ret) {
				is_ht = true;
				fgets(buf, BUFSIZE, f);
			} else {
				ret = sscanf(buf, "\tcpu%u", &cpu_info.cpu_id);
				if (ret)
					is_ht = false;
				else
					break;
			}

			do {
				if (!is_ht) {
					ret = sscanf(buf, "\tcpu%u", &cpu_info.cpu_id);
					cpu_info.core_id = cpu_info.cpu_id;
				} else {
					ret = sscanf(buf, "\t\tcpu%u", &cpu_info.cpu_id);
				}

				if (!ret)
					break;

				add_topo_info(&g_cpu_topo_list, &cpu_info);

				fgets(buf, BUFSIZE, f);
			} while (1);
		} while (1);
	} while (1);

	/* output_topo_info(&g_cpu_topo_list); */

	return 0;
}

int release_cpu_topo_info(void)
{
	/* free alloced memory */
	free_cpu_topology(&g_cpu_topo_list.physical_head);

	return 0;
}

int output_cpu_topo_info(FILE *f)
{
	outfile_topo_info(f, &g_cpu_topo_list);

	return 0;
}

int establish_idledata_to_topo(struct cpuidle_datas *datas)
{
	struct cpu_physical *s_phy;
	struct cpu_core     *s_core;
	struct cpu_cpu      *s_cpu;
	int    i;
	int    has_topo = 0;

	for (i = 0; i < datas->nrcpus; i++) {
		s_cpu = find_cpu_point(&g_cpu_topo_list, i);
		if (s_cpu) {
			s_cpu->cstates = &datas->cstates[i];
			has_topo = 1;
		}
	}

	if (!has_topo)
		return -1;

	list_for_each_entry(s_phy, &g_cpu_topo_list.physical_head, list_physical)
		list_for_each_entry(s_core, &s_phy->core_head, list_core)
			s_core->cstates = core_cluster_data(s_core);

	list_for_each_entry(s_phy, &g_cpu_topo_list.physical_head, list_physical)
		s_phy->cstates = physical_cluster_data(s_phy);

	return 0;
}

int dump_cpu_topo_info(int state, int count,
	int (*dump)(struct cpuidle_cstates *, int,  int, char *))
{
	struct cpu_physical *s_phy;
	struct cpu_core     *s_core;
	struct cpu_cpu      *s_cpu;
	char   tmp[30];
	int    tab = 0;

	list_for_each_entry(s_phy, &g_cpu_topo_list.physical_head, list_physical) {
		sprintf(tmp, "cluster%c", s_phy->physical_id + 'A');
		dump(s_phy->cstates, state, count, tmp);

		list_for_each_entry(s_core, &s_phy->core_head, list_core) {
			if (s_core->is_ht) {
				sprintf(tmp, "\tcore%d", s_core->core_id);
				dump(s_core->cstates, state, count, tmp);

				tab = 1;
			} else {
				tab = 0;
			}

			list_for_each_entry(s_cpu, &s_core->cpu_head, list_cpu) {
				sprintf(tmp, "%scpu%d", tab ? "\t\t" : "\t", s_cpu->cpu_id);
				dump(s_cpu->cstates, state, count, tmp);
			}
		}
	}

	return 0;
}

int release_cpu_topo_cstates(void)
{
	struct cpu_physical *s_phy;
	struct cpu_core     *s_core;

	list_for_each_entry(s_phy, &g_cpu_topo_list.physical_head, list_physical) {
		free(s_phy->cstates);
		s_phy->cstates = NULL;
		list_for_each_entry(s_core, &s_phy->core_head, list_core)
			if (s_core->is_ht) {
				free(s_core->cstates);
				s_core->cstates = NULL;
			}
	}

	return 0;
}