]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/lscpu-cpu.c
2 * SPDX-License-Identifier: GPL-2.0-or-later
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * Copyright (C) 2020 Karel Zak <kzak@redhat.com>
13 struct lscpu_cpu
*lscpu_new_cpu(int id
)
15 struct lscpu_cpu
*cpu
;
17 cpu
= xcalloc(1, sizeof(struct lscpu_cpu
));
27 DBG(CPU
, ul_debugobj(cpu
, "alloc"));
31 void lscpu_ref_cpu(struct lscpu_cpu
*cpu
)
37 void lscpu_unref_cpu(struct lscpu_cpu
*cpu
)
42 if (--cpu
->refcount
<= 0) {
43 DBG(CPU
, ul_debugobj(cpu
, " freeing #%d", cpu
->logical_id
));
44 lscpu_unref_cputype(cpu
->type
);
46 free(cpu
->dynamic_mhz
);
47 free(cpu
->static_mhz
);
55 * Create and initialize array with CPU structs according to @cpuset.
57 int lscpu_create_cpus(struct lscpu_cxt
*cxt
, cpu_set_t
*cpuset
, size_t setsize
)
63 cxt
->npossibles
= CPU_COUNT_S(setsize
, cpuset
);
64 cxt
->cpus
= xcalloc(1, cxt
->npossibles
* sizeof(struct lscpu_cpu
*));
66 for (n
= 0, i
= 0; n
< (size_t) cxt
->maxcpus
&& i
< cxt
->npossibles
; n
++) {
67 if (CPU_ISSET_S(n
, setsize
, cpuset
))
68 cxt
->cpus
[i
++] = lscpu_new_cpu(n
);
74 int lscpu_cpu_set_type(struct lscpu_cpu
*cpu
, struct lscpu_cputype
*type
)
76 if (cpu
->type
== type
)
79 lscpu_unref_cputype(cpu
->type
);
81 lscpu_ref_cputype(type
);
83 DBG(CPU
, ul_debugobj(cpu
, "cputype set to %s", type
? type
->vendor
: NULL
));
87 /* don't forget lscpu_ref_cpu() ! */
88 struct lscpu_cpu
*lscpu_get_cpu(struct lscpu_cxt
*cxt
, int logical_id
)
92 for (i
= 0; i
< cxt
->npossibles
; i
++) {
93 struct lscpu_cpu
*cpu
= cxt
->cpus
[i
];
95 if (cpu
&& cpu
->logical_id
== logical_id
)