blob: f4ec71bd1a7c8b4e443b0988e5eff466059fa702 (
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
|
/* fk3.c: fork() cost against the storage the parent holds.
*
* p[0] is read after the loop so the allocation cannot be optimised
* away. Usage: ./fk3 [megabytes] [forks]; the cost is child and parent
* CPU, so it is the fork, not the wait, that is being charged.
*
* See bench/README.md and notes/investigation.md.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/times.h>
#ifndef CLK_TCK
#define CLK_TCK 100 /* z/OS reports 100; the divisor below assumes it */
#endif
int main(int argc, char **argv) {
long mb = argc > 1 ? atol(argv[1]) : 0;
int n = argc > 2 ? atoi(argv[2]) : 100;
char *p = nullptr;
if (mb > 0) {
p = malloc((size_t)mb * 1024 * 1024);
if (!p) {
printf("malloc failed\n");
return 1;
}
memset(p, 'x', (size_t)mb * 1024 * 1024);
}
struct tms a, b;
times(&a);
for (int i = 0; i < n; i++) {
pid_t c = fork();
if (c == 0)
_exit(0);
int st;
waitpid(c, &st, 0);
}
times(&b);
clock_t t = (b.tms_utime - a.tms_utime) + (b.tms_stime - a.tms_stime) +
(b.tms_cutime - a.tms_cutime) + (b.tms_cstime - a.tms_cstime);
printf(" %4ld MB: %6.2f s (%5.2f ms per fork)", mb, (double)t / 100.0,
(double)t / 100.0 * 1000.0 / n);
if (p)
printf(" [p[0]=%c]", p[0]);
printf("\n");
return 0;
}
|