Re: [Qemu-devel] [PATCH RFC] virtio: put last seen used index into ring itself

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Rusty Russell
Date: Wednesday, May 19, 2010 - 10:08 pm

On Thu, 20 May 2010 02:31:50 pm Rusty Russell wrote:

This time for sure!

#define _GNU_SOURCE
#include <unistd.h>
#include <sched.h>
#include <err.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/mman.h>

/* We share memory via an mmap. */
struct counter {
	unsigned int cacheline1;
	char pad[256];
	unsigned int cacheline2;
};

#define MAX_BOUNCES 100000000

enum mode {
	SHARE,
	UNSHARE,
	LOCKSHARE,
	LOCKUNSHARE,
};

int main(int argc, char *argv[])
{
	cpu_set_t cpuset;
	volatile struct counter *counter;
	struct timeval start, stop;
	bool child;
	unsigned int count;
	uint64_t usec;
	enum mode mode;

	if (argc != 4)
		errx(1, "Usage: cachebounce share|unshare|lockshare|lockunshare <cpu0> <cpu1>");

	if (strcmp(argv[1], "share") == 0)
		mode = SHARE;
	else if (strcmp(argv[1], "unshare") == 0)
		mode = UNSHARE;
	else if (strcmp(argv[1], "lockshare") == 0)
		mode = LOCKSHARE;
	else if (strcmp(argv[1], "lockunshare") == 0)
		mode = LOCKSHARE;
	else
		errx(1, "Usage: cachebounce share|unshare|lockshare|lockunshare <cpu0> <cpu1>");

	CPU_ZERO(&cpuset);

	counter = mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
	if (counter == MAP_FAILED)
		err(1, "Mapping page");

	/* Fault it in. */
	counter->cacheline1 = counter->cacheline2 = 0;

	child = (fork() == 0);

	CPU_SET(atoi(argv[2 + child]), &cpuset);
	if (sched_setaffinity(getpid(), sizeof(cpu_set_t), &cpuset) != 0)
		err(1, "Calling sched_setaffinity()");

	gettimeofday(&start, NULL);

	if (child) {
		count = 1;
		switch (mode) {
		case SHARE:
			while (count < MAX_BOUNCES) {
				/* Spin waiting for other side to change it. */
				while (counter->cacheline1 != count);
				count++;
				counter->cacheline1 = count;
				count++;
			}
			break;
		case UNSHARE:
			while (count < MAX_BOUNCES) {
				/* Spin waiting for other side to change it. */
				while (counter->cacheline1 != count);
				count++;
				counter->cacheline2 = count;
				count++;
			}
			break;
		case LOCKSHARE:
			while (count < MAX_BOUNCES) {
				/* Spin waiting for other side to change it. */
				while (__sync_val_compare_and_swap(&counter->cacheline1, count, count+1)
				       != count);
				count += 2;
			}
			break;
		case LOCKUNSHARE:
			while (count < MAX_BOUNCES) {
				/* Spin waiting for other side to change it. */
				while (counter->cacheline1 != count);
				__sync_val_compare_and_swap(&counter->cacheline2, count, count+1);
				count += 2;
			}
			break;
		}
	} else {
		count = 0;
		switch (mode) {
		case SHARE:
			while (count < MAX_BOUNCES) {
				/* Spin waiting for other side to change it. */
				while (counter->cacheline1 != count);
				count++;
				counter->cacheline1 = count;
				count++;
			}
			break;
		case UNSHARE:
			while (count < MAX_BOUNCES) {
				/* Spin waiting for other side to change it. */
				while (counter->cacheline2 != count);
				count++;
				counter->cacheline1 = count;
				count++;
			}
			break;
		case LOCKSHARE:
			while (count < MAX_BOUNCES) {
				/* Spin waiting for other side to change it. */
				while (__sync_val_compare_and_swap(&counter->cacheline1, count, count+1)
				       != count);
				count += 2;
			}
			break;
		case LOCKUNSHARE:
			while (count < MAX_BOUNCES) {
				/* Spin waiting for other side to change it. */
				while (counter->cacheline2 != count);
				__sync_val_compare_and_swap(&counter->cacheline1, count, count+1);
				count += 2;
			}
			break;
		}
	}
	gettimeofday(&stop, NULL);
	usec = (stop.tv_sec * 1000000LL + stop.tv_usec)
		- (start.tv_sec * 1000000LL + start.tv_usec);
	printf("CPU %s: %s cacheline: %llu usec\n", argv[2+child], argv[1], usec);
	return 0;
}

--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH RFC] virtio: put last seen used index into ring itself, Michael S. Tsirkin, (Wed May 5, 1:58 pm)
Re: [PATCH RFC] virtio: put last seen used index into ring ..., Michael S. Tsirkin, (Wed May 5, 11:19 pm)
Re: [PATCH RFC] virtio: put last seen used index into ring ..., Michael S. Tsirkin, (Sun May 9, 2:06 pm)
Re: [PATCH RFC] virtio: put last seen used index into ring ..., Michael S. Tsirkin, (Tue May 11, 12:48 pm)
Re: [Qemu-devel] [PATCH RFC] virtio: put last seen used in ..., Michael S. Tsirkin, (Tue May 11, 12:52 pm)
Re: [Qemu-devel] [PATCH RFC] virtio: put last seen used in ..., Michael S. Tsirkin, (Wed May 19, 3:33 pm)
Re: [Qemu-devel] [PATCH RFC] virtio: put last seen used in ..., Rusty Russell, (Wed May 19, 10:08 pm)
Re: [Qemu-devel] [PATCH RFC] virtio: put last seen used in ..., Michael S. Tsirkin, (Thu May 20, 3:04 am)
Re: [Qemu-devel] [PATCH RFC] virtio: put last seen used in ..., Michael S. Tsirkin, (Sun May 23, 8:31 am)
Re: [Qemu-devel] [PATCH RFC] virtio: put last seen used in ..., Michael S. Tsirkin, (Sun May 23, 8:51 am)
Re: [Qemu-devel] [PATCH RFC] virtio: put last seen used in ..., Michael S. Tsirkin, (Sun May 23, 8:56 am)
Re: [Qemu-devel] [PATCH RFC] virtio: put last seen used in ..., Michael S. Tsirkin, (Sun May 23, 10:28 am)
Re: [Qemu-devel] [PATCH RFC] virtio: put last seen used in ..., Michael S. Tsirkin, (Mon May 24, 1:05 am)