-
Notifications
You must be signed in to change notification settings - Fork 3
/
linux.c
106 lines (83 loc) · 2.83 KB
/
linux.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
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
/* -*- mode: C; c-file-style: "k&r"; -*-
*---------------------------------------------------------------------------*
*
* Copyright (c) 2000, Johan Bengtsson
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*---------------------------------------------------------------------------*/
#include "machdep.h"
static int proc_fd = -1;
int init_machdep(pid_t process)
{
char filename[64];
sprintf(filename, "/proc/%d/stat", (int)process);
proc_fd = open(filename, O_RDONLY);
return (proc_fd != -1);
}
int get_sample(struct memtime_info *info)
{
static char buffer[2048];
char *tmp;
int i, utime, stime;
long int vsize, rss;
int rc;
lseek(proc_fd, 0, SEEK_SET);
rc = read(proc_fd, buffer, 2048);
if (rc == -1)
return 0;
*(buffer + rc) = '\0';
for (i=0, tmp=buffer; i < 13; i++)
tmp = strchr(tmp + 1, ' ');
sscanf(tmp + 1, "%d %d", &utime, &stime);
for (/* empty */; i < 22; i++)
tmp = strchr(tmp + 1, ' ');
sscanf(tmp + 1, "%ld %ld", &vsize, &rss);
info->utime_ms = utime * (1000 / HZ);
info->stime_ms = stime * (1000 / HZ);
info->vsize_kb = vsize / 1024;
//printf("%ld\n", vsize);
info->rss_kb = (rss * getpagesize()) / 1024;
return 1;
}
unsigned int get_time()
{
struct timeval now;
struct timezone dummy;
int rc;
rc = gettimeofday(&now, &dummy);
if (rc == -1) {
return 0;
}
return (now.tv_sec * 1000) + (now.tv_usec / 1000);
}
int set_mem_limit(long int maxbytes)
{
struct rlimit rl;
long int softlimit = (long int)maxbytes*0.95;
rl.rlim_cur = softlimit;
rl.rlim_max = maxbytes;
return setrlimit(RLIMIT_AS, &rl);
}
int set_cpu_limit(long int maxseconds)
{
struct rlimit rl;
rl.rlim_cur=maxseconds;
rl.rlim_max=maxseconds;
return setrlimit(RLIMIT_CPU,&rl);
}