aboutsummaryrefslogtreecommitdiff
path: root/src/proone-stress.c
blob: 8487a71e63bcce786e2bec54fe21668f627a14ad (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <assert.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/random.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <signal.h>
#include <errno.h>

#include <mbedtls/ctr_drbg.h>
#include <mbedtls/entropy.h>

#include "util_ct.h"
#include "util_rt.h"
#include "llist.h"
#include "mbedtls.h"


typedef struct {
	bool good;
} shared_t;

typedef struct {
	uint64_t nb_cycles;
	uint64_t last_cycles;
} shared_ctx_t;

typedef struct {
	mbedtls_entropy_context ent;
	mbedtls_ctr_drbg_context ctx;	
} priv_ctx_t;

shared_t *shared = NULL;
shared_ctx_t *ctx_arr = NULL;
struct timespec last_report;
size_t pagesize;
unsigned int nb_pages_min = 1;
unsigned int nb_pages_max = 1;
pid_t parent;
unsigned int nproc;

static void child_main (shared_ctx_t *ctx);
static void handle_signal (const int sn);
static void report (shared_ctx_t *arr);
static void child_signal_handler (const int sn);
static void sendall(const int sn);

int main (const int argc, const char **args) {
	static const size_t ALIGNED_SHARED_SIZE = prne_malign_to(sizeof(shared_t), 8);
#define END_ON_ERR(retval, val, fname, eq)\
	if ((eq && retval != val) || (!eq && retval == val)) {\
		perror(fname);\
		exit_code = 1;\
		goto END;\
	}

	unsigned int nb_proc_started = 0;
	int exit_code = 0;
	int zfd = -1;
	size_t shm_size;
	uint8_t *shm_ptr = MAP_FAILED;
	bool is_parent = true;
	
	parent = getpid();

	{
		const long ps = sysconf(_SC_PAGESIZE);

		if (ps < 0) {
			perror("sysconf(_SC_PAGESIZE) returned error");
			abort();
		}
		else if (ps == 0) {
			fprintf(stderr, "sysconf(_SC_PAGESIZE) returned 0");
			abort();
		}
		else {
			pagesize = (size_t)ps;
		}

		assert(pagesize % sizeof(unsigned long) == 0);
	}
	
	if (argc < 2) {
		fprintf(stderr,
			"Usage: %s <nproc> [page num range]\n"
			"\t<nproc>: number of processes to spawn\n"
			"\t[page num range]: number of page to use. '1-5' for 1 to 5 pages, '1' for just one page. Defaults to '1'\n",
			args[0]);
		return 2;
	}
	if (sscanf(args[1], "%u", &nproc) != 1 || nproc == 0) {
		fprintf(stderr, "Invalid <nproc> value %u\n", nproc);
		return 2;
	}
	if (argc >= 3) {
		if (sscanf(args[2], "%u-%u", &nb_pages_min, &nb_pages_max) == 2) {
			if (nb_pages_min > nb_pages_max || nb_pages_min == 0) {
				goto DROP;
			}
		}
		else if (sscanf(args[2], "%u", &nb_pages_min) == 1) {
			if (nb_pages_min == 0) {
				goto DROP;
			}
			nb_pages_max = nb_pages_min;
		}
		else {
			goto DROP;
		}

		if (false) {
DROP:
			fprintf(stderr, "Invalid page num range.\n");
			return 2;
		}
	}

	// init mem
	shm_size = ALIGNED_SHARED_SIZE + sizeof(shared_ctx_t) * nproc;
	zfd = open("/dev/zero", O_RDWR);
	if (zfd < 0) {
		perror("open()");
		exit_code = 1;
		goto END;
	}
	shm_ptr = mmap(NULL, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, zfd, 0);
	END_ON_ERR(shm_ptr, MAP_FAILED, "mmap()", false);
	close(zfd);
	zfd = -1;	

	// prep shared
	shared = (shared_t*)shm_ptr;
	shm_ptr += ALIGNED_SHARED_SIZE;
	shared->good = true;

	ctx_arr = (shared_ctx_t*)shm_ptr;

	for (unsigned int i = 0; i < nproc; i += 1) {
		const pid_t f_ret = fork();

		if (f_ret < 0) {
			perror("fork()");
			exit_code = 1;
			goto END;
		}
		else if (f_ret == 0) {
			struct sigaction sa;

			memzero(&sa, sizeof(struct sigaction));
			sa.sa_handler = child_signal_handler;
			sigaction(SIGINT, &sa, NULL);

			is_parent = false;

			child_main(ctx_arr + i);
			goto END;
		}
		else {
			nb_proc_started += 1;
		}
	}

	{
		struct sigaction sa;

		memzero(&sa, sizeof(struct sigaction));
		sa.sa_handler = handle_signal;

		sigaction(SIGINT, &sa, NULL);
		sigaction(SIGTERM, &sa, NULL);
		sigaction(SIGCHLD, &sa, NULL);
		sigaction(SIGUSR1, &sa, NULL);
	}

	clock_gettime(CLOCK_MONOTONIC, &last_report);

	
	while (shared->good) {
		pause();
	}

END:
	sigaction(SIGCHLD, NULL, NULL);

	if (is_parent) {
		if (shm_ptr != MAP_FAILED) {
			shared->good = false;

			if (nb_proc_started > 0) {
				printf("Reaping child processes ...\n");

				for (unsigned int i = 0; i < nb_proc_started; i += 1) {
					wait(NULL);
				}
			}

			munmap(shm_ptr, shm_size);
		}
		if (zfd >= 0) {
			close(zfd);
		}
	}

	return exit_code;
}

static void handle_signal (const int sn) {
	const int save_errno = errno;

	switch (sn) {
	case SIGINT:
	case SIGTERM:
		shared->good = false;
		if (sn == SIGTERM) {
			sendall(SIGTERM);
		}
		sigaction(SIGINT, NULL, NULL);
		sigaction(SIGTERM, NULL, NULL);		
		break;
	case SIGCHLD:
		if (shared->good) {
			fprintf(stderr, "Death of a child!\n");
			kill(0, SIGABRT);
			raise(SIGABRT);
		}
		break;
	case SIGUSR1:
		report(ctx_arr);
		break;
	}

	errno = save_errno;
}

static void report (shared_ctx_t *arr) {
	struct timespec ts_now, ts_delta;
	const time_t now = time(NULL);
	uint64_t sum_delta = 0, sum_all = 0;

	clock_gettime(CLOCK_MONOTONIC, &ts_now);

	printf(
		"=== proone-stress ===\n"
		"%s",
		ctime(&now));
	for (unsigned int i = 0; i < nproc; i += 1) {
		shared_ctx_t *ctx = arr + i;
		uint64_t delta;

		delta = ctx->nb_cycles - ctx->last_cycles;
		sum_delta += delta;
		sum_all += ctx->nb_cycles;
		ctx->last_cycles = ctx->nb_cycles;

		printf("%"PRIu64" ", delta);
	}

	ts_delta = prne_sub_timespec(ts_now, last_report);
	printf(
		"\n"
		"Delta: %"PRIu64"\n"
		"Sum: %"PRIu64"\n"
		"Last report: %"PRIdMAX".%03lds ago\n"
		"Thoughput: %.1lf events per second\n",
		sum_delta,
		sum_all,
		(intmax_t)ts_delta.tv_sec,
		ts_delta.tv_nsec / 1000000,
		(double)sum_delta / prne_real_timespec(ts_delta));

	fflush(stdout);

	last_report = ts_now;
}

static void do_cycle (priv_ctx_t *priv_ctx, shared_ctx_t *ctx) {
	unsigned int pages;
	unsigned long *arr;
	size_t nb_elements;

	assert(mbedtls_ctr_drbg_random(&priv_ctx->ctx, (unsigned char*)&pages, sizeof(pages)) == 0);

	pages = (pages % (nb_pages_max - nb_pages_min + 1)) + nb_pages_min;
	nb_elements = pagesize / sizeof(unsigned long) * pages;

	arr = (unsigned long*)prne_malloc(pagesize, pages);
	assert(mbedtls_ctr_drbg_random(&priv_ctx->ctx, (unsigned char*)arr, 2 * sizeof(unsigned int)) == 0);

	for (size_t i = 2; i < nb_elements; i += 1) {
		arr[i] = arr[i - 2] + arr[i - 1];
	}

	prne_free(arr);
}

static void child_main (shared_ctx_t *ctx) {
	priv_ctx_t priv_ctx;

	prne_mbedtls_entropy_init(&priv_ctx.ent);
	mbedtls_ctr_drbg_init(&priv_ctx.ctx);
	assert(mbedtls_ctr_drbg_seed(&priv_ctx.ctx, mbedtls_entropy_func, &priv_ctx.ent, NULL, 0) == 0);
	
	while (shared->good) {
		do_cycle(&priv_ctx, ctx);
		ctx->nb_cycles += 1;
	}
}

static void child_signal_handler (const int sn) {}

static void sendall(const int sn) {
	for (unsigned int i = 0; i < nproc; i += 1) {

	}
}