cosmopolitan/examples/spawn_bench.c
Jōshin 6e6fc38935
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)

I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.

My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.

It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.

fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 10:38:00 -07:00

142 lines
4.2 KiB
C

#if 0
/*─────────────────────────────────────────────────────────────────╗
│ To the extent possible under law, Justine Tunney has waived │
│ all copyright and related or neighboring rights to this file, │
│ as it is written in the following disclaimers: │
│ • http://unlicense.org/ │
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
╚─────────────────────────────────────────────────────────────────*/
#endif
#include "libc/atomic.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/timespec.h"
#include "libc/calls/weirdtypes.h"
#include "libc/mem/mem.h"
#include "libc/proc/posix_spawn.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/clock.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
#define ITERATIONS 10
_Alignas(128) int a;
_Alignas(128) int b;
_Alignas(128) atomic_int lock;
static struct timespec SubtractTime(struct timespec a, struct timespec b) {
a.tv_sec -= b.tv_sec;
if (a.tv_nsec < b.tv_nsec) {
a.tv_nsec += 1000000000;
a.tv_sec--;
}
a.tv_nsec -= b.tv_nsec;
return a;
}
static time_t ToNanoseconds(struct timespec ts) {
return ts.tv_sec * 1000000000 + ts.tv_nsec;
}
static char *Ithoa(char p[27], unsigned long x) {
char m[26];
unsigned i;
i = 0;
do {
m[i++] = x % 10 + '0';
x = x / 10;
} while (x);
for (;;) {
*p++ = m[--i];
if (!i)
break;
if (!(i % 3))
*p++ = ',';
}
*p = '\0';
return p;
}
#define BENCH(name, x) \
{ \
int i; \
char ibuf[27]; \
struct timespec t1, t2; \
clock_gettime(CLOCK_REALTIME, &t1); \
for (i = 0; i < ITERATIONS; ++i) { \
x; \
} \
clock_gettime(CLOCK_REALTIME, &t2); \
Ithoa(ibuf, ToNanoseconds(SubtractTime(t2, t1)) / ITERATIONS); \
printf("%-50s %16s ns\n", name, ibuf); \
}
void PosixSpawnWait(const char *prog) {
int ws, err, pid;
char *args[] = {(char *)prog, 0};
char *envs[] = {0};
if ((err = posix_spawn(&pid, prog, NULL, NULL, args, envs))) {
perror(prog);
exit(1);
}
if (waitpid(pid, &ws, 0) == -1) {
perror("waitpid");
exit(1);
}
if (!(WIFEXITED(ws) && WEXITSTATUS(ws) == 42)) {
puts("bad exit status");
exit(1);
}
}
void ForkExecWait(const char *prog) {
int ws;
if (!fork()) {
execve(prog, (char *[]){(char *)prog, 0}, (char *[]){0});
_Exit(127);
}
if (wait(&ws) == -1) {
perror("wait");
exit(1);
}
if (!(WIFEXITED(ws) && WEXITSTATUS(ws) == 42)) {
puts("bad exit status");
exit(1);
}
}
char *FillMemory(char *p, long n) {
return memset(p, -1, n);
}
char *(*pFillMemory)(char *, long) = FillMemory;
int main(int argc, char *argv[]) {
long n;
void *p;
const char *prog;
if (argc <= 1) {
prog = "tiny64";
} else {
prog = argv[1];
}
BENCH("fork() + exec(tiny)", ForkExecWait(prog));
BENCH("posix_spawn(tiny)", PosixSpawnWait(prog));
n = 128L * 1024 * 1024;
p = pFillMemory(
mmap(0, n, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0), n);
BENCH("mmap(128mb, MAP_SHARED) + fork() + exec(tiny)", ForkExecWait(prog));
BENCH("mmap(128mb, MAP_SHARED) + posix_spawn(tiny)", PosixSpawnWait(prog));
munmap(p, n);
n = 128L * 1024 * 1024;
p = pFillMemory(malloc(n), n);
BENCH("malloc(128mb) + fork() + exec(tiny)", ForkExecWait(prog));
BENCH("malloc(128mb) + posix_spawn(tiny)", PosixSpawnWait(prog));
free(p);
}