mailing list of musl libc
 help / color / mirror / code / Atom feed
* posix_spawn vs fork+exec test program
@ 2015-06-04  3:14 Rich Felker
  0 siblings, 0 replies; only message in thread
From: Rich Felker @ 2015-06-04  3:14 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 642 bytes --]

The attached test program can be used to measure the performance of
posix_spawn vs fork+exec under simulated memory conditions in the
parent. The options it takes are:

-n bytes	memory to allocate
-r		fragment memory into alternating ro/rw vmas
-d		dirty allocated memory
-f		use fork+exec (instead of default posix_spawn)

At -n 200000000 -r -d, I've observed a nearly 300x performance
difference.

As (somewhat) expected, with large -n but neither -r or -d, fork+exec
performs well -- all pages are COW zero pages and easily forked. But
if the pages are dirty, or if there are lots of vmas (even non-dirty
ones), fork gets very slow.

Rich

[-- Attachment #2: spawntime.c --]
[-- Type: text/plain, Size: 1752 bytes --]

#define _POSIX_C_SOURCE 200809L
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdint.h>

extern char **environ;

static void bloat_up(size_t n, int frag, int dirty)
{
	size_t pg = sysconf(_SC_PAGESIZE);
	size_t i;
	if (frag) {
		n /= pg;
		for (i=0; i<n; i++) {
			char *p = mmap(0, pg, PROT_READ | (i%2 ? PROT_WRITE : 0), MAP_PRIVATE | MAP_ANON, -1, 0);
			if (dirty && i%2) *p = 42;
		}
	} else {
		void *p = mmap(0, n, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
		if (dirty) memset(p, 42, n);
	}
}

int main(int argc, char *argv[])
{
	struct timespec t1, t2;
	pid_t pid;
	clock_gettime(CLOCK_MONOTONIC, &t2);
	if (read(3, &t1, sizeof t1)<0) {
		int b, use_fork=0, do_dirty=0, do_frag=0;
		size_t use_mem=0;
		while ((b=getopt(argc,argv,"drfn:"))!=EOF) switch(b) {
		case 'd':
			do_dirty = 1;
			break;
		case 'r':
			do_frag = 1;
			break;
		case 'f':
			use_fork = 1;
			break;
		case 'n':
			use_mem = strtol(optarg, 0, 0);
			break;
		default:
			return 1;
		}
		FILE *f = tmpfile();
		struct timespec *t0 = mmap(0, sizeof *t0, PROT_READ|PROT_WRITE, MAP_SHARED, 3, 0);
		char *prog = strchr(argv[0], '/') ? argv[0] : "/proc/self/exe";
		ftruncate(3, sizeof *t0);
		bloat_up(use_mem, do_frag, do_dirty);
		t0->tv_sec = 0;
		clock_gettime(CLOCK_MONOTONIC, t0);
		if (!use_fork) {
			posix_spawn(&pid, prog, 0, 0, (char *[]){prog, 0}, environ);
		} else if (!(pid=fork())) {
			execve(prog, (char *[]){prog, 0}, environ);
			_exit(1);
		}
		waitpid(pid, 0, 0);
		return 0;
	}
	t2.tv_sec -= t1.tv_sec;
	if ((t2.tv_nsec -= t1.tv_nsec) < 0) {
		t2.tv_nsec += 1000000000;
		t2.tv_sec--;
	}
	printf("spawn time: %jd.%.9d\n", (intmax_t)t2.tv_sec, (int)t2.tv_nsec);
	return 0;
}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2015-06-04  3:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-04  3:14 posix_spawn vs fork+exec test program Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).