File: sys.c

package info (click to toggle)
miniasm 0.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 632 kB
  • sloc: ansic: 2,528; perl: 120; sh: 66; makefile: 29
file content (46 lines) | stat: -rw-r--r-- 803 bytes parent folder | download | duplicates (8)
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
#include <sys/resource.h>
#include <sys/time.h>
#include <stdio.h>

static double realtime0;

double sys_cputime()
{
	struct rusage r;
	getrusage(RUSAGE_SELF, &r);
	return r.ru_utime.tv_sec + r.ru_stime.tv_sec + 1e-6 * (r.ru_utime.tv_usec + r.ru_stime.tv_usec);
}

double sys_realtime()
{
	struct timeval tp;
	struct timezone tzp;
	gettimeofday(&tp, &tzp);
	return (tp.tv_sec + tp.tv_usec * 1e-6) - realtime0;
}

void sys_liftrlimit()
{
#ifdef __linux__
	struct rlimit r;
	getrlimit(RLIMIT_AS, &r);
	r.rlim_cur = r.rlim_max;
	setrlimit(RLIMIT_AS, &r);
#endif
}

void sys_init()
{
	sys_liftrlimit();
	realtime0 = sys_realtime();
}

const char *sys_timestamp()
{
	static char buf[256];
	double rt, ct;
	rt = sys_realtime();
	ct = sys_cputime();
	snprintf(buf, 255, "%.3f*%.2f", rt, ct/rt);
	return buf;
}