All pastes #1959 Raw Edit

Cycles to copy one cold page

public c v1 · immutable
#1959 ·published 2004-11-07 19:27 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/mman.h>#define rdtsc(low,high) \	__asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))#define rdtscl(low) \	__asm__ __volatile__("rdtsc" : "=a" (low) : : "edx") /* p4 has a 512KB cache == 128 pages */#define PAGE_SIZE	4096#define CACHE_SIZE	(128 * PAGE_SIZE)	int main(int argc, char **argv){	void *to, *from;	void *cache_clear;	unsigned long a, b;	int i;	cache_clear = mmap(NULL, CACHE_SIZE, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);	to = mmap(NULL, 4096, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);	from = mmap(NULL, 4096, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);	if (cache_clear == MAP_FAILED || to == MAP_FAILED || from == MAP_FAILED)		exit(1);	memset(to, PAGE_SIZE, 1);	memset(from, PAGE_SIZE, 1);	/* hit DCACHE with hammer */	for (i = 0; i < 5; i++)		memset(cache_clear, i, CACHE_SIZE); /*	iopl(3);	asm("cli");*/		rdtscl(a);	memcpy(to, from, PAGE_SIZE);	rdtscl(b);//	asm("sti");		/* 	 * never mind the high cycle count, we'll notice if it	 * looks completely bogus 	 */	printf("took %lu cycles to copy a page\n", b - a);		return 0;}