-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdtscp_module.c
43 lines (36 loc) · 899 Bytes
/
rdtscp_module.c
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
#include <Python.h>
#include <stdint.h>
static inline uint64_t _rdtscp(void)
{
unsigned long _hi, _lo;
asm volatile("rdtscp" : "=a"(_lo), "=d"(_hi));
uint64_t val = (((uint64_t)_hi) << 32 | ((uint64_t)_lo));
return val;
}
static PyObject* do_rdtscp(PyObject *self, PyObject *args)
{
uint64_t res;
res = _rdtscp();
return Py_BuildValue("l", res);
}
static PyMethodDef rdtscp_module_methods[] = {
{
"rdtscp",
do_rdtscp,
METH_NOARGS,
"C Extension for Python3 to call RDTSCP()"
},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef rdtscp_module_definition = {
PyModuleDef_HEAD_INIT,
"rdtscp_module",
"C Extension for Python3 to call RDTSCP()",
-1,
rdtscp_module_methods
};
PyMODINIT_FUNC PyInit_rdtscp_module(void)
{
Py_Initialize();
return PyModule_Create(&rdtscp_module_definition);
}