summaryrefslogtreecommitdiffstats
path: root/sys-utils/lscpu-cpu.c
blob: 6250cf70ccda11239d0a01ff4b040e67e96186a0 (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
#include "lscpu.h"

struct lscpu_cpu *lscpu_new_cpu(int id)
{
	struct lscpu_cpu *cpu;

	cpu = xcalloc(1, sizeof(struct lscpu_cpu));
	cpu->refcount = 1;
	cpu->logical_id = id;
	cpu->coreid = -1;
	cpu->socketid = -1;
	cpu->bookid = -1;
	cpu->bookid = -1;
	cpu->address = -1;
	cpu->configured = -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 #%d", cpu->logical_id));
		lscpu_unref_cputype(cpu->type);
		cpu->type = NULL;
		free(cpu->dynamic_mhz);
		free(cpu->static_mhz);
		free(cpu->mhz);
		free(cpu->bogomips);
		free(cpu);
	}
}

/*
 * Create and initialize array with CPU structs according to @cpuset.
 */
int lscpu_create_cpus(struct lscpu_cxt *cxt, cpu_set_t *cpuset, size_t setsize)
{
	size_t n, i;

	assert(!cxt->cpus);

	cxt->npossibles = CPU_COUNT_S(setsize, cpuset);
	cxt->cpus = xcalloc(1, cxt->npossibles * sizeof(struct lscpu_cpu *));

	for (n = 0, i = 0; n < (size_t) cxt->maxcpus && i < cxt->npossibles; n++) {
		if (CPU_ISSET_S(n, setsize, cpuset))
			cxt->cpus[i++] = lscpu_new_cpu(n);
	}

	return 0;
}

int lscpu_cpu_set_type(struct lscpu_cpu *cpu, struct lscpu_cputype *type)
{
	if (cpu->type == type)
		return 0;

	lscpu_unref_cputype(cpu->type);
	cpu->type = type;
	lscpu_ref_cputype(type);

	DBG(CPU, ul_debugobj(cpu, "cputype set to %s", type ? type->vendor : NULL));
	return 0;
}

/* don't forget lscpu_ref_cpu() ! */
struct lscpu_cpu *lscpu_get_cpu(struct lscpu_cxt *cxt, int logical_id)
{
	size_t i;

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

		if (cpu && cpu->logical_id == logical_id)
			return cpu;
	}

	return NULL;
}