summaryrefslogtreecommitdiffhomepage
path: root/tests/tools/spawn.c
blob: bff74ed5ef5fc8e007bf9d3deea0810d274f5105 (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
/**
 * @file spawn.c
 * @brief Wall and CPU cost of starting a program N times.
 *
 * `bench/le-init.sh` cannot use a shell loop for this: on z/OS the shell
 * is a 31-bit LE program, so its own child would pay a different LE
 * price than a 64-bit start, and the difference would be lost.  Here
 * only one child exists at a time, so its cost is the interval between
 * two clock readings, and the CPU it was charged is the difference in
 * `times()` child time.
 *
 *   spawn prog n [NAME=VALUE ...]
 *
 * prints two numbers, milliseconds per start: wall, then child CPU.  A
 * child that does not exit 0 stops the run: the timing would be of a
 * failure, not of a start.  Any NAME=VALUE after the count is set in the
 * child only, before the exec -- `_CEE_RUNOPTS` is read by the child's
 * LE, and setting it here instead would change this process too.
 *
 * The child's environment is built by hand and handed to `execve`,
 * because this process is itself an LE program: `putenv` changes the C
 * library's `environ`, and LE keeps its own copy, so a `putenv` here is
 * silently not passed on.  (`_CEE_RUNOPTS` exported from a non-LE shell
 * does reach a child; that is why the mistake is invisible from a shell.)
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/times.h>

extern char **environ;

static double ms_between(struct timespec a, struct timespec b) {
    return (double)(b.tv_sec - a.tv_sec) * 1000.0 +
           (double)(b.tv_nsec - a.tv_nsec) / 1000000.0;
}

int main(int argc, char **argv) {
    if (argc < 3) {
        fprintf(stderr, "usage: spawn prog n [NAME=VALUE ...]\n");
        return 2;
    }
    long n = strtol(argv[2], NULL, 10);
    if (n <= 0) {
        fprintf(stderr, "spawn: n must be positive\n");
        return 2;
    }

    /* environ, plus one slot per NAME=VALUE, plus the NULL. */
    int nenv = 0;
    while (environ[nenv] != NULL)
        nenv++;
    char **envp = calloc((size_t)nenv + (size_t)(argc - 3) + 1u, sizeof *envp);
    if (envp == NULL) {
        fprintf(stderr, "spawn: out of memory\n");
        return 1;
    }
    for (int i = 0; i < nenv; i++)
        envp[i] = environ[i];
    int ne = nenv;
    for (int k = 3; k < argc; k++) {
        char *eq = strchr(argv[k], '=');
        size_t len = eq == NULL ? 0 : (size_t)(eq - argv[k]) + 1u;
        int done = 0;
        for (int i = 0; eq != NULL && i < nenv; i++) {
            if (strncmp(envp[i], argv[k], len) == 0) {
                envp[i] = argv[k];
                done = 1;
                break;
            }
        }
        if (!done)
            envp[ne++] = argv[k];
    }

    struct timespec t0, t1;
    struct tms tm;
    clock_gettime(CLOCK_MONOTONIC, &t0);
    for (long i = 0; i < n; i++) {
        pid_t pid = fork();
        if (pid < 0) {
            perror("fork");
            return 1;
        }
        if (pid == 0) {
            char *cargv[2] = {argv[1], NULL};
            execve(argv[1], cargv, envp);
            perror(argv[1]);
            _exit(127);
        }
        int status;
        if (waitpid(pid, &status, 0) < 0) {
            perror("waitpid");
            return 1;
        }
        if (!WIFEXITED(status)) {
            fprintf(stderr, "spawn: %s did not exit (status %d)\n", argv[1],
                    status);
            return 1;
        }
        if (WEXITSTATUS(status) != 0) {
            fprintf(stderr, "spawn: %s exited %d\n", argv[1],
                    WEXITSTATUS(status));
            return 1;
        }
    }
    clock_gettime(CLOCK_MONOTONIC, &t1);
    if (times(&tm) == (clock_t)-1) {
        perror("times");
        return 1;
    }

    double wall = ms_between(t0, t1) / (double)n;
    double cpu = (double)(tm.tms_cutime + tm.tms_cstime) * 1000.0 /
                 (double)sysconf(_SC_CLK_TCK) / (double)n;
    printf("%.2f %.2f\n", wall, cpu);
    return 0;
}