Debian Codegraph @

psutil/arch/openbsd/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/sysctl.h>
10 #include <sys/sched.h> // for CPUSTATES & CP_*
11
12 #include "../../arch/all/init.h"
13
14
15 PyObject *
16 psutil_per_cpu_times(PyObject *self, PyObject *args) {
17 int mib[3];
18 int ncpu;
19 size_t len;
20 int i;
21 PyObject *py_retlist = PyList_New(0);
22 PyObject *py_cputime = NULL;
23
24 if (py_retlist == NULL)
25 return NULL;
26
27 // retrieve the number of cpus
28 mib[0] = CTL_HW;
29 mib[1] = HW_NCPU;
30 if (psutil_sysctl(mib, 2, &ncpu, sizeof(ncpu)) != 0)
31 goto error;
32
33 uint64_t cpu_time[CPUSTATES];
34
35 for (i = 0; i < ncpu; i++) {
36 mib[0] = CTL_KERN;
37 mib[1] = KERN_CPTIME2;
38 mib[2] = i;
39
40 if (psutil_sysctl(mib, 3, &cpu_time, sizeof(cpu_time)) != 0)
41 goto error;
42
43 py_cputime = Py_BuildValue(
44 "(ddddd)",
45 (double)cpu_time[CP_USER] / CLOCKS_PER_SEC,
46 (double)cpu_time[CP_NICE] / CLOCKS_PER_SEC,
47 (double)cpu_time[CP_SYS] / CLOCKS_PER_SEC,
48 (double)cpu_time[CP_IDLE] / CLOCKS_PER_SEC,
49 (double)cpu_time[CP_INTR] / CLOCKS_PER_SEC);
50 if (!py_cputime)
51 goto error;
52 if (PyList_Append(py_retlist, py_cputime))
53 goto error;
54 Py_DECREF(py_cputime);
55 }
56
57 return py_retlist;
58
59 error:
60 Py_XDECREF(py_cputime);
61 Py_DECREF(py_retlist);
62 return NULL;
63 }
64
65
66 PyObject *
67 psutil_cpu_stats(PyObject *self, PyObject *args) {
68 struct uvmexp uv;
69 int uvmexp_mib[] = {CTL_VM, VM_UVMEXP};
70
71 if (psutil_sysctl(uvmexp_mib, 2, &uv, sizeof(uv)) !=0)
72 return NULL;
73
74 return Py_BuildValue(
75 "IIIIIII",
76 uv.swtch, // ctx switches
77 uv.intrs, // interrupts - XXX always 0, will be determined via /proc
78 uv.softs, // soft interrupts
79 uv.syscalls, // syscalls - XXX always 0
80 uv.traps, // traps
81 uv.faults, // faults
82 uv.forks // forks
83 );
84 }
85
86
87 PyObject *
88 psutil_cpu_freq(PyObject *self, PyObject *args) {
89 int freq;
90 int mib[2] = {CTL_HW, HW_CPUSPEED};
91
92 // On VirtualBox I get "sysctl hw.cpuspeed=2593" (never changing),
93 // which appears to be expressed in Mhz.
94 if (psutil_sysctl(mib, 2, &freq, sizeof(freq)) != 0)
95 return NULL;
96
97 return Py_BuildValue("i", freq);
98 }