-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhog.c
116 lines (91 loc) · 2.38 KB
/
hog.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
107
108
109
110
111
112
113
114
115
116
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <limits.h>
#define DEFAULT_CHECKPOINTS 64
static int term_requested = 0;
void sig_handler(int signo) {
if (signo == SIGTERM) {
term_requested = 1;
}
}
int assign_env_pos_long(char* var_name, long* dst) {
char* env_val = getenv(var_name);
if (env_val == NULL) {
return 0;
}
char* endptr = NULL;
*dst = strtol(env_val, &endptr, 10);
if ((*dst < 0) || (endptr == NULL) || (*endptr != 0)) {
fprintf(stderr, "hog: invalid value for %s: %s\n",
var_name, env_val);
return 1;
}
return 0;
}
int main(int argc, char* argv[]) {
if (signal(SIGTERM, sig_handler) == SIG_ERR) {
perror("register SIGTERM handler");
return 1;
}
long hog_max_bytes = LONG_MAX;
if (assign_env_pos_long("HOG_MAX_BYTES", &hog_max_bytes)) {
return 1;
}
long hog_wait_seconds = 0;
if (assign_env_pos_long("HOG_WAIT_SECONDS", &hog_wait_seconds)) {
return 1;
}
long hog_goal_seconds = 0;
if (assign_env_pos_long("HOG_GOAL_SECONDS", &hog_goal_seconds)) {
return 1;
}
long hog_checkpoints = DEFAULT_CHECKPOINTS;
if (assign_env_pos_long("HOG_CHECKPOINTS", &hog_checkpoints)) {
return 1;
}
fprintf(stdout, "hog: hog up to %ld bytes over %ld second(s) with %ld checkpoint(s)\n",
hog_max_bytes, hog_goal_seconds, hog_checkpoints);
fflush(stdout);
if (hog_wait_seconds > 0) {
fprintf(stdout, "hog: wait: %ld second(s)\n", hog_wait_seconds);
fflush(stdout);
sleep(hog_wait_seconds);
}
fprintf(stdout, "hog: start\n");
fflush(stdout);
long page_size = sysconf(_SC_PAGESIZE);
long hogged_bytes = 0;
long hogged_checkpoints = 0;
long next_checkpoint_bytes = 0;
while(1) {
if (term_requested > 0) {
fprintf(stdout, "hog: exit: SIGTERM\n");
fflush(stdout);
break;
}
if (hogged_bytes > hog_max_bytes) {
fprintf(stdout, "hog: done\n");
fflush(stdout);
break;
}
if ((hog_checkpoints > 0) && (hogged_bytes >= next_checkpoint_bytes)) {
next_checkpoint_bytes += hog_max_bytes / hog_checkpoints;
fprintf(stdout, "hog: checkpoint %ld: %ld bytes\n",
hogged_checkpoints, hogged_bytes);
fflush(stdout);
if (hog_goal_seconds > 0) {
usleep(1000 * 1000 * hog_goal_seconds / hog_checkpoints);
}
hogged_checkpoints += 1;
}
char* range = (char*) malloc(page_size);
if (range) {
range[0] = 0;
}
hogged_bytes += page_size;
}
return 0;
}