mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: posix_spawn vs fork+exec test program
Date: Wed, 3 Jun 2015 23:14:47 -0400	[thread overview]
Message-ID: <20150604031447.GA25336@brightrain.aerifal.cx> (raw)

[-- 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;
}

                 reply	other threads:[~2015-06-04  3:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150604031447.GA25336@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).