-
Notifications
You must be signed in to change notification settings - Fork 1
/
load_simulator.c
49 lines (40 loc) · 1.08 KB
/
load_simulator.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
44
45
46
47
48
#include <Python.h>
#include <sys/time.h>
#include <sys/resource.h>
#define MILLION 1000000
static PyObject *
simulate_load(PyObject *self, PyObject *args) {
long cpu_time_ms;
long mem_bytes;
char *p;
struct rusage r;
int j;
if (!PyArg_ParseTuple(args, "LL", &cpu_time_ms, &mem_bytes)) {
return NULL;
}
p = malloc(mem_bytes);
while(1) {
if (getrusage(RUSAGE_SELF, &r) == -1) {
printf("getrusage() failed");
exit(1);
}
long total_cpu_time_us = r.ru_utime.tv_sec * MILLION + r.ru_utime.tv_usec +
r.ru_stime.tv_sec * MILLION + r.ru_stime.tv_usec;
if (total_cpu_time_us / 1000 >= cpu_time_ms)
break;
for (long i = 0; i < MILLION; i++) {
j++;
}
}
return Py_BuildValue("s#", p, mem_bytes);
}
static PyMethodDef LoadSimulatorMethods[] =
{
{"simulate_load", simulate_load, METH_VARARGS, "simulate load"},
{ NULL, NULL, 0, NULL }
};
PyMODINIT_FUNC
initload_simulator(void)
{
(void) Py_InitModule("load_simulator", LoadSimulatorMethods);
}