public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: torvalds@linuxfoundation.org
Cc: dhowells@redhat.com, linux-kernel@vger.kernel.org,
	linux-fsdevel@vger.kernel.org
Subject: My just-shovel-data-through-for-X-amount-of-time test
Date: Fri, 13 Sep 2019 14:06:39 +0100	[thread overview]
Message-ID: <25718.1568379999@warthog.procyon.org.uk> (raw)
In-Reply-To: <25289.1568379639@warthog.procyon.org.uk>

/*
 * Benchmark a pipe by seeing how many 511-byte writes can be stuffed through
 * it in a certain amount of time.  Compile with -lm.
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <math.h>
#include <sys/wait.h>
#include <sys/mman.h>

static char buf1[4096] __attribute__((aligned(4096)));
static char buf2[4096] __attribute__((aligned(4096)));
static unsigned long long *results;

static volatile int stop;
void sigalrm(int sig)
{
	stop = 1;
}

static __attribute__((noreturn))
void producer(int fd, int i)
{
	unsigned long long l = 0;
	ssize_t n;

	signal(SIGALRM, sigalrm);
	alarm(1);

	while (!stop) {
		n = write(fd, buf1, 511);
		if (n == -1) {
			perror("read");
			exit(1);
		}

		l += n;
	}

	results[i] = l;
	exit(0);
}

static __attribute__((noreturn))
void consumer(int fd)
{
	unsigned long long l = 0;
	ssize_t n;

	for (;;) {
		n = read(fd, buf2, 4096);
		if (n == 0)
			break;
		if (n == -1) {
			perror("read");
			exit(1);
		}

		l += n;
	}

	exit(0);
}

unsigned long long stddev(const unsigned long long *vals, int nvals)
{
	double sum = 0.0, mean, sd = 0.0;
	int i;

	for (i = 0; i < nvals; i++)
		sum += vals[i];

	mean = sum / nvals;
	for (i = 0; i < nvals; i++)
		sd += pow(vals[i] - mean, 2);
	return sqrt(sd / 10);
}

int main()
{
	unsigned long long t = 0;
	int ex = 0, i;

	results = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
	if (results == MAP_FAILED) {
		perror("mmap");
		exit(1);
	}

	for (i = 0; i < 16 && ex == 0; i++) {
		pid_t prod, con;
		int pfd[2], wt;

		if (pipe2(pfd, 0) < 0) {
			perror("pipe");
			exit(1);
		}

		con = fork();
		switch (con) {
		case -1:
			perror("fork/c");
			exit(1);
		case 0:
			close(pfd[1]);
			consumer(pfd[0]);
		default:
			break;
		}

		prod = fork();
		switch (prod) {
		case -1:
			perror("fork/p");
			exit(1);
		case 0:
			close(pfd[0]);
			producer(pfd[1], i);
		default:
			break;
		}

		close(pfd[0]);
		close(pfd[1]);

		for (;;) {
			errno = 0;
			wait(&wt);
			if (errno == ECHILD)
				break;
			if (!WIFEXITED(wt) || WEXITSTATUS(wt) != 0)
				ex = 1;
		}

		printf("WR[%02u]: %12llu\n", i, results[i]);
		t += results[i];
	}

	printf("total : %12llu\n", t);
	printf("avg   : %12llu\n", t / i);
	printf("stddev: %12llu\n", stddev(results, i));
	exit(ex);
}

  reply	other threads:[~2019-09-13 13:06 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-13 13:00 [RFC][PATCH] pipe: Convert ring to head/tail David Howells
2019-09-13 13:06 ` David Howells [this message]
2019-09-15 14:59 ` Will Deacon
2019-09-17 13:51   ` David Howells
2019-09-17 17:07     ` Will Deacon
2019-09-18 15:43       ` Do we need to correct barriering in circular-buffers.rst? David Howells
2019-09-18 16:48         ` Linus Torvalds
2019-09-19 13:59           ` David Howells
2019-09-19 15:59             ` Linus Torvalds
2019-09-23 14:49             ` Peter Zijlstra
2019-09-27  9:51               ` Andrea Parri
2019-09-27 12:49                 ` Peter Zijlstra
2019-09-27 15:57                   ` Peter Zijlstra
2019-09-27 20:43                   ` Nick Desaulniers
2019-09-27 21:58                     ` Nick Desaulniers
2019-09-30  9:33                     ` Peter Zijlstra
2019-09-30 11:54                       ` Peter Zijlstra
2019-09-30 12:02                         ` Peter Zijlstra

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=25718.1568379999@warthog.procyon.org.uk \
    --to=dhowells@redhat.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@linuxfoundation.org \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox