-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpuidmodule.cpp
163 lines (116 loc) · 3.46 KB
/
cpuidmodule.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Copyright (c) 2017-2023 Fernando Pelliccioni
*/
#include <Python.h>
#include "cpuid.h"
// ---------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
PyObject* cpuid_native_get_cpuid(PyObject* self, PyObject* args) {
unsigned int py_leaf;
if ( ! PyArg_ParseTuple(args, "I", &py_leaf))
return NULL;
unsigned int eax, ebx, ecx, edx;
int res = __get_cpuid(py_leaf, &eax, &ebx, &ecx, &edx);
return Py_BuildValue("iIIII", res, eax, ebx, ecx, edx);
}
PyObject* cpuid_native_get_cpuid_count(PyObject* self, PyObject* args) {
unsigned int py_leaf;
unsigned int py_subleaf;
if ( ! PyArg_ParseTuple(args, "II", &py_leaf, &py_subleaf))
return NULL;
unsigned int eax, ebx, ecx, edx;
int res = __get_cpuid_count(py_leaf, py_subleaf, &eax, &ebx, &ecx, &edx);
return Py_BuildValue("iIIII", res, eax, ebx, ecx, edx);
}
PyObject* cpuid_native_xgetbv(PyObject* self, PyObject* args) {
int py_ctr;
if ( ! PyArg_ParseTuple(args, "I", &py_ctr)) {
return NULL;
}
uint64_t res = xgetbv(py_ctr);
return Py_BuildValue("K", res);
}
PyObject* cpuid_native_rdtscp(PyObject* self, PyObject* args) {
unsigned int ui;
uint64_t res = rdtscp(&ui);
return Py_BuildValue("(KI)", res, ui);
}
// ---------------------------------------------------------
static
PyMethodDef CpuidNativeMethods[] = {
{"get_cpuid", cpuid_native_get_cpuid, METH_VARARGS, "..."},
{"get_cpuid_count", cpuid_native_get_cpuid_count, METH_VARARGS, "..."},
{"xgetbv", cpuid_native_xgetbv, METH_VARARGS, "..."},
{"rdtscp", cpuid_native_rdtscp, METH_VARARGS, "..."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
struct module_state {
PyObject *error;
};
#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
#else //PY_MAJOR_VERSION >= 3
#define GETSTATE(m) (&_state)
static struct module_state _state;
#endif //PY_MAJOR_VERSION >= 3
#if PY_MAJOR_VERSION >= 3
static
int myextension_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(GETSTATE(m)->error);
return 0;
}
static
int myextension_clear(PyObject *m) {
Py_CLEAR(GETSTATE(m)->error);
return 0;
}
static
struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"cpuid_native",
NULL,
sizeof(struct module_state),
CpuidNativeMethods,
NULL,
myextension_traverse,
myextension_clear,
NULL
};
#define INITERROR return NULL
PyMODINIT_FUNC
PyInit_cpuid_native(void)
#else /* PY_MAJOR_VERSION >= 3 */
#define INITERROR return
void /*PyMODINIT_FUNC*/
initcpuid_native(void)
#endif /* PY_MAJOR_VERSION >= 3 */
{
// Make sure the GIL has been created since we need to acquire it in our
// callback to safely call into the python application.
if (! PyEval_ThreadsInitialized()) {
PyEval_InitThreads();
}
#if PY_MAJOR_VERSION >= 3
PyObject *module = PyModule_Create(&moduledef);
#else
PyObject *module = Py_InitModule("cpuid_native", CpuidNativeMethods);
// (void) Py_InitModule("cpuid_native", CpuidNativeMethods);
#endif
if (module == NULL) {
INITERROR;
}
struct module_state *st = GETSTATE(module);
st->error = PyErr_NewException((char*)"myextension.Error", NULL, NULL);
if (st->error == NULL) {
Py_DECREF(module);
INITERROR;
}
#if PY_MAJOR_VERSION >= 3
return module;
#endif
}
#ifdef __cplusplus
} // extern "C"
#endif