shithub: sysbench

ref: a1c8da91f18c8bd3cdc7ce36c70538969e0c5602
dir: /libc.c/

View raw version
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "bench.h"

// Prevent inlining of libc functions
extern void *memcpy(void *dest, const void *src, size_t n) __attribute__((noinline));
extern void *memmove(void *dest, const void *src, size_t n) __attribute__((noinline));
extern int memcmp(const void *s1, const void *s2, size_t n) __attribute__((noinline));
extern void *memset(void *s, int c, size_t n) __attribute__((noinline));

char *dst;
char *src;

enum {
	Same,
	Start,
	Mid,
	End,
};

void
benchmemcpy(B *b, int sz)
{
	int i;

	for(i = 0; i < b->N; i++)
		memcpy(dst, src, sz);
}

void
benchmemmovedisjoint(B *b, int sz)
{
	int i;

	for(i = 0; i < b->N; i++)
		memmove(dst, src, sz);
}

void
benchmemmoveoverlap(B *b, int sz)
{
	int i;

	for(i = 0; i < b->N; i++)
		memmove(dst, src+1, sz);
}

void
benchmemcmp(B *b, int sz, int where)
{
	int i;

	memset(src, 0xea, sz);
	memset(dst, 0xea, sz);
	switch(where){
	case Start:
		dst[0] = 1;
		break;
	case Mid:
		dst[sz/2] = 1;
		break;
	case End:
		dst[sz-1] = 1;
		break;
	}
	for(i = 0; i < b->N; i++)
		memcmp(dst, src, sz);
}



void benchmemcpy_0b(B *b) { benchmemcpy(b, 0); }
void benchmemmovedisjoint_0b(B *b) { benchmemmovedisjoint(b, 0); }
void benchmemmoveoverlap_0b(B *b) { benchmemmoveoverlap(b, 0); }
void benchmemcmp_16k(B *b) { benchmemcmp(b, 0, Same); }

void benchmemcpy_16b(B *b) { benchmemcpy(b, 16); }
void benchmemmoveoverlap_16b(B *b) { benchmemmovedisjoint(b, 16); }
void benchmemmovedisjoint_16b(B *b) { benchmemmoveoverlap(b, 16); }
void benchmemcmpsame_16b(B *b) { benchmemcmp(b, 16, Same); }
void benchmemcmpdiffhead_16b(B *b) { benchmemcmp(b, 16, Start); }
void benchmemcmpdiffmid_16b(B *b) { benchmemcmp(b, 16, Mid); }
void benchmemcmpdifftail_16b(B *b) { benchmemcmp(b, 16, End); }

void benchmemcpy_16k(B *b) { benchmemcpy(b, 16*KiB); }
void benchmemmoveoverlap_16k(B *b) { benchmemmovedisjoint(b, 16*KiB); }
void benchmemmovedisjoint_16k(B *b) { benchmemmoveoverlap(b, 16*KiB); }
void benchmemcmpsame_16k(B *b) { benchmemcmp(b, 16*KiB, Same); }
void benchmemcmpdiffhead_16k(B *b) { benchmemcmp(b, 16*KiB, Start); }
void benchmemcmpdiffmid_16k(B *b) { benchmemcmp(b, 16*KiB, Mid); }
void benchmemcmpdifftail_16k(B *b) { benchmemcmp(b, 16*KiB, End); }

void benchmemcpy_16m(B *b) { benchmemcpy(b, 16*MiB); }
void benchmemmoveoverlap_16m(B *b) { benchmemmovedisjoint(b, 16*MiB); }
void benchmemmovedisjoint_16m(B *b) { benchmemmoveoverlap(b, 16*MiB); }
void benchmemcmpsame_16m(B *b) { benchmemcmp(b, 16*MiB, Same); }
void benchmemcmpdiffhead_16m(B *b) { benchmemcmp(b, 16*MiB, Start); }
void benchmemcmpdiffmid_16m(B *b) { benchmemcmp(b, 16*MiB, Mid); }
void benchmemcmpdifftail_16m(B *b) { benchmemcmp(b, 16*MiB, End); }

void benchmemcpy_1g(B *b) { benchmemcpy(b, 1*GiB); }
void benchmemmoveoverlap_16g(B *b) { benchmemmovedisjoint(b, 1*GiB); }
void benchmemmovedisjoint_16g(B *b) { benchmemmoveoverlap(b, 1*GiB); }
void benchmemcmpsame_1g(B *b) { benchmemcmp(b, 1*GiB, Same); }
void benchmemcmpdiffhead_1g(B *b) { benchmemcmp(b, 1*GiB, Start); }
void benchmemcmpdiffmid_1g(B *b) { benchmemcmp(b, 1*GiB, Mid); }
void benchmemcmpdifftail_1g(B *b) { benchmemcmp(b, 1*GiB, End); }

void
main(int argc, char **argv)
{
	benchinit(argc, argv);

	if((src = malloc(1*GiB + KiB)) == NULL) {
		fprintf(stderr, "malloc failed\n");
		exit(1);
	}
	if((dst = malloc(1*GiB + KiB)) == NULL) {
		fprintf(stderr, "malloc failed\n");
		exit(1);
	}

	printf("== small nops (0 b) ==\n");
	BM(benchmemcpy_0b);
	BM(benchmemmovedisjoint_16b);
	BM(benchmemmoveoverlap_16b);
	BM(benchmemcmp_16k);

	printf("== small memory ops (16b) ==\n");
	BM(benchmemcpy_16b);
	BM(benchmemmoveoverlap_16b);
	BM(benchmemmovedisjoint_16b);
	BM(benchmemcmpsame_16b);
	BM(benchmemcmpdiffhead_16b);
	BM(benchmemcmpdiffmid_16b);
	BM(benchmemcmpdifftail_16b);

	printf("== midsize memory ops (16k) ==\n");
	BM(benchmemcpy_16k);
	BM(benchmemmoveoverlap_16k);
	BM(benchmemmovedisjoint_16k);
	BM(benchmemcmpsame_16k);
	BM(benchmemcmpdiffhead_16k);
	BM(benchmemcmpdiffmid_16k);
	BM(benchmemcmpdifftail_16k);

	printf("== large memory ops (16m) ==\n");
	BM(benchmemcpy_16m);
	BM(benchmemmoveoverlap_16m);
	BM(benchmemmovedisjoint_16m);
	BM(benchmemcmpsame_16m);
	BM(benchmemcmpdiffhead_16m);
	BM(benchmemcmpdiffmid_16m);
	BM(benchmemcmpdifftail_16m);

	printf("== huge memory ops (1g) ==\n");
	BM(benchmemcpy_1g);
	BM(benchmemmoveoverlap_16g);
	BM(benchmemmovedisjoint_16g);
	BM(benchmemcmpsame_1g);
	BM(benchmemcmpdiffhead_1g);
	BM(benchmemcmpdiffmid_1g);
	BM(benchmemcmpdifftail_1g);

	exit(0);
}