Debian Codegraph @

psutil/arch/freebsd/cpu.c

raw
1 /*
2 * Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7 /*
8 System-wide CPU related functions.
9 Original code was refactored and moved from psutil/arch/freebsd/specific.c
10 in 2020 (and was moved in there previously already) from cset.
11 a4c0a0eb0d2a872ab7a45e47fcf37ef1fde5b012
12 For reference, here's the git history with original(ish) implementations:
13 - CPU stats: fb0154ef164d0e5942ac85102ab660b8d2938fbb
14 - CPU freq: 459556dd1e2979cdee22177339ced0761caf4c83
15 - CPU cores: e0d6d7865df84dc9a1d123ae452fd311f79b1dde
16 */
17
18
19 #include <Python.h>
20 #include <sys/sysctl.h>
21 #include <devstat.h>
22
23 #include "../../arch/all/init.h"
24
25
26 PyObject *
27 psutil_per_cpu_times(PyObject *self, PyObject *args) {
28 int maxcpus;
29 int mib[2];
30 int ncpu;
31 size_t size;
32 PyObject *py_retlist = PyList_New(0);
33 PyObject *py_cputime = NULL;
34
35 if (py_retlist == NULL)
36 return NULL;
37
38 // retrieve the number of CPUs currently online
39 mib[0] = CTL_HW;
40 mib[1] = HW_NCPU;
41 if (psutil_sysctl(mib, 2, &ncpu, sizeof(ncpu)) != 0) {
42 goto error;
43 }
44
45 // allocate buffer dynamically based on actual CPU count
46 long (*cpu_time)[CPUSTATES] = malloc(ncpu * sizeof(*cpu_time));
47 if (!cpu_time) {
48 PyErr_NoMemory();
49 goto error;
50 }
51
52 // get per-cpu times using ncpu count
53 size = ncpu * sizeof(*cpu_time);
54 if (psutil_sysctlbyname("kern.cp_times", cpu_time, size) == -1) {
55 free(cpu_time);
56 goto error;
57 }
58
59 for (int i = 0; i < ncpu; i++) {
60 py_cputime = Py_BuildValue(
61 "(ddddd)",
62 (double)cpu_time[i][CP_USER] / CLOCKS_PER_SEC,
63 (double)cpu_time[i][CP_NICE] / CLOCKS_PER_SEC,
64 (double)cpu_time[i][CP_SYS] / CLOCKS_PER_SEC,
65 (double)cpu_time[i][CP_IDLE] / CLOCKS_PER_SEC,
66 (double)cpu_time[i][CP_INTR] / CLOCKS_PER_SEC);
67 if (!py_cputime) {
68 free(cpu_time);
69 goto error;
70 }
71 if (PyList_Append(py_retlist, py_cputime)) {
72 Py_DECREF(py_cputime);
73 free(cpu_time);
74 goto error;
75 }
76 Py_DECREF(py_cputime);
77 }
78
79 free(cpu_time);
80 return py_retlist;
81
82 error:
83 Py_XDECREF(py_cputime);
84 Py_DECREF(py_retlist);
85 return NULL;
86 }
87
88
89 PyObject *
90 psutil_cpu_topology(PyObject *self, PyObject *args) {
91 char *topology = NULL;
92 size_t size = 0;
93 PyObject *py_str;
94
95 if (psutil_sysctlbyname_malloc(
96 "kern.sched.topology_spec", &topology, &size) != 0)
97 {
98 psutil_debug("ignore sysctlbyname('kern.sched.topology_spec') error");
99 Py_RETURN_NONE;
100 }
101
102 py_str = Py_BuildValue("s", topology);
103 free(topology);
104 return py_str;
105 }
106
107
108 PyObject *
109 psutil_cpu_stats(PyObject *self, PyObject *args) {
110 unsigned int v_soft;
111 unsigned int v_intr;
112 unsigned int v_syscall;
113 unsigned int v_trap;
114 unsigned int v_swtch;
115 size_t size = sizeof(v_soft);
116
117 if (psutil_sysctlbyname("vm.stats.sys.v_soft", &v_soft, size) != 0)
118 return NULL;
119 if (psutil_sysctlbyname("vm.stats.sys.v_intr", &v_intr, size) != 0)
120 return NULL;
121 if (psutil_sysctlbyname("vm.stats.sys.v_syscall", &v_syscall, size) != 0)
122 return NULL;
123 if (psutil_sysctlbyname("vm.stats.sys.v_trap", &v_trap, size) != 0)
124 return NULL;
125 if (psutil_sysctlbyname("vm.stats.sys.v_swtch", &v_swtch, size) != 0)
126 return NULL;
127
128 return Py_BuildValue(
129 "IIIII",
130 v_swtch, // ctx switches
131 v_intr, // interrupts
132 v_soft, // software interrupts
133 v_syscall, // syscalls
134 v_trap // traps
135 );
136 }
137
138
139 /*
140 * Return frequency information of a given CPU.
141 * As of Dec 2018 only CPU 0 appears to be supported and all other
142 * cores match the frequency of CPU 0.
143 */
144 PyObject *
145 psutil_cpu_freq(PyObject *self, PyObject *args) {
146 int current;
147 int core;
148 char sensor[26];
149 char available_freq_levels[1000];
150 size_t size;
151
152 if (! PyArg_ParseTuple(args, "i", &core))
153 return NULL;
154
155 // https://www.unix.com/man-page/FreeBSD/4/cpufreq/
156 size = sizeof(current);
157 sprintf(sensor, "dev.cpu.%d.freq", core);
158 if (psutil_sysctlbyname(sensor, &current, size) != 0)
159 goto error;
160
161 // In case of failure, an empty string is returned.
162 size = sizeof(available_freq_levels);
163 sprintf(sensor, "dev.cpu.%d.freq_levels", core);
164 if (psutil_sysctlbyname(sensor, &available_freq_levels, size) != 0)
165 psutil_debug("cpu freq levels failed (ignored)");
166
167 return Py_BuildValue("is", current, available_freq_levels);
168
169 error:
170 if (errno == ENOENT)
171 PyErr_SetString(PyExc_NotImplementedError, "unable to read frequency");
172 else
173 PyErr_SetFromErrno(PyExc_OSError);
174 return NULL;
175 }