aboutsummaryrefslogtreecommitdiffstats
path: root/sys-utils/lscpu-cpu.c
blob: 78d9404f130860c54248a98ce9db845397417cb0 (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
#include "lscpu-api.h"

struct lscpu_cpu *lscpu_new_cpu(void)
{
	struct lscpu_cpu *cpu;

	cpu = xcalloc(1, sizeof(struct lscpu_cpu));
	cpu->refcount = 1;
	cpu->logical_id = -1;

	DBG(CPU, ul_debugobj(cpu, "alloc"));
	return cpu;
}

void lscpu_ref_cpu(struct lscpu_cpu *cpu)
{
	if (cpu)
		cpu->refcount++;
}

void lscpu_unref_cpu(struct lscpu_cpu *cpu)
{
	if (!cpu)
		return;

	if (--cpu->refcount <= 0) {
		DBG(CPU, ul_debugobj(cpu, "  freeing"));
		lscpu_unref_cputype(cpu->type);
		free(cpu->dynamic_mhz);
		free(cpu->static_mhz);
		free(cpu->mhz);
		free(cpu);
	}
}

int lscpu_add_cpu(struct lscpu_cxt *cxt,
		  struct lscpu_cpu *cpu,
		  struct lscpu_cputype *ct)
{
	struct lscpu_cputype *type;

	/* make sure the type exists */
	if (ct)
		type = lscpu_add_cputype(cxt, ct);
	else
		type = lscpu_cputype_get_default(cxt);

	cxt->cpus = xrealloc(cxt->cpus, (cxt->ncpus + 1)
				* sizeof(struct lscpu_cpu *));

	cxt->cpus[cxt->ncpus] = cpu;
	cxt->ncpus++;
	lscpu_ref_cpu(cpu);

	if (type) {
		cpu->type = type;
		lscpu_ref_cputype(type);
		type->ncpus++;
	}

	return 0;
}

int lscpu_cpus_apply_type(struct lscpu_cxt *cxt, struct lscpu_cputype *type)
{
	size_t i;

	for (i = 0; i < cxt->ncpus; i++) {
		struct lscpu_cpu *cpu = cxt->cpus[i];

		if (!cpu->type) {
			cpu->type = type;
			lscpu_ref_cputype(type);
			type->ncpus++;
		}
	}
	return 0;
}


int lscpu_read_topolgy_ids(struct lscpu_cxt *cxt)
{
	struct path_cxt *sys = cxt->syscpu;
	size_t i;

	for (i = 0; i < cxt->ncpus; i++) {
		struct lscpu_cpu *cpu = cxt->cpus[i];
		int num = cpu->logical_id;

		if (ul_path_readf_s32(sys, &cpu->coreid, "cpu%d/topology/core_id", num) != 0)
			cpu->coreid = -1;
		if (ul_path_readf_s32(sys, &cpu->socketid, "cpu%d/topology/physical_package_id", num) != 0)
			cpu->socketid = -1;
		if (ul_path_readf_s32(sys, &cpu->bookid, "cpu%d/topology/book_id", num) != 0)
			cpu->bookid = -1;
		if (ul_path_readf_s32(sys, &cpu->drawerid, "cpu%d/topology/drawer_id", num) != 0)
			cpu->drawerid = -1;
	}

	return 0;
}