Debian Codegraph @

psutil/arch/netbsd/cpu.c

raw
1 /*
2 * Copyright (c) 2009, Giampaolo Rodola', Landry Breuil.
3 * All rights reserved.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include <Python.h>
9 #include <sys/sched.h>
10 #include <sys/sysctl.h>
11 #include <uvm/uvm_extern.h>
12
13 #include "../../arch/all/init.h"
14
15
16 /*
17 CPU related functions. Original code was refactored and moved from
18 psutil/arch/netbsd/specific.c in 2023 (and was moved in there previously
19 already) from cset 84219ad. For reference, here's the git history with
20 original(ish) implementations:
21 - per CPU times: 312442ad2a5b5d0c608476c5ab3e267735c3bc59 (Jan 2016)
22 - CPU stats: a991494e4502e1235ebc62b5ba450287d0dedec0 (Jan 2016)
23 */
24
25
26 PyObject *
27 psutil_cpu_stats(PyObject *self, PyObject *args) {
28 struct uvmexp_sysctl uv;
29 int uvmexp_mib[] = {CTL_VM, VM_UVMEXP2};
30
31 if (psutil_sysctl(uvmexp_mib, 2, &uv, sizeof(uv)) != 0)
32 return NULL;
33 return Py_BuildValue(
34 "IIIIIII",
35 uv.swtch, // ctx switches
36 uv.intrs, // interrupts - XXX always 0, will be determined via /proc
37 uv.softs, // soft interrupts
38 uv.syscalls, // syscalls - XXX always 0
39 uv.traps, // traps
40 uv.faults, // faults
41 uv.forks // forks
42 );
43 }
44
45
46 PyObject *
47 psutil_per_cpu_times(PyObject *self, PyObject *args) {
48 int mib[3];
49 int ncpu;
50 size_t len;
51 size_t size;
52 int i;
53 PyObject *py_cputime = NULL;
54 PyObject *py_retlist = PyList_New(0);
55
56 if (py_retlist == NULL)
57 return NULL;
58
59 // retrieve the number of cpus
60 mib[0] = CTL_HW;
61 mib[1] = HW_NCPU;
62 if (psutil_sysctl(mib, 2, &ncpu, sizeof(ncpu)) != 0)
63 goto error;
64
65 uint64_t cpu_time[CPUSTATES];
66
67 for (i = 0; i < ncpu; i++) {
68 // per-cpu info
69 mib[0] = CTL_KERN;
70 mib[1] = KERN_CP_TIME;
71 mib[2] = i;
72 if (psutil_sysctl(mib, 3, &cpu_time, sizeof(cpu_time)) != 0)
73 goto error;
74 py_cputime = Py_BuildValue(
75 "(ddddd)",
76 (double)cpu_time[CP_USER] / CLOCKS_PER_SEC,
77 (double)cpu_time[CP_NICE] / CLOCKS_PER_SEC,
78 (double)cpu_time[CP_SYS] / CLOCKS_PER_SEC,
79 (double)cpu_time[CP_IDLE] / CLOCKS_PER_SEC,
80 (double)cpu_time[CP_INTR] / CLOCKS_PER_SEC
81 );
82 if (!py_cputime)
83 goto error;
84 if (PyList_Append(py_retlist, py_cputime))
85 goto error;
86 Py_DECREF(py_cputime);
87 }
88
89 return py_retlist;
90
91 error:
92 Py_XDECREF(py_cputime);
93 Py_DECREF(py_retlist);
94 return NULL;
95 }