Commits

Wang Yuanxuan  committed 15f5cc6

mmapbench

  • Participants
  • Parent commits be9b0b4

Comments (0)

Files changed (5)

File mmapbench/Makefile

+CC=gcc
+CFLAGS=-O2 -Wall -pthread
+
+default: mmapbench
+
+forkbench: mmapbench.o
+	$(CC) -o $@ $^ $(CFLAGS)
+
+clean:
+	rm -rf *.o
+	rm -rf mmapbench

File mmapbench/bench.h

+#ifndef _BENCH_H_
+#define _BENCH_H_
+
+#include <sched.h>
+
+static int
+affinity_set(int cpu)
+{
+    cpu_set_t cpuset;
+    CPU_ZERO(&cpuset);
+    CPU_SET(cpu, &cpuset);
+    return sched_setaffinity(0, sizeof(cpuset), &cpuset);
+}
+
+static uint64_t
+get_cpu_freq(void)
+{
+    FILE *fd;
+    uint64_t freq = 0;
+    float freqf = 0;
+    char *line = NULL;
+    size_t len = 0;
+
+    fd = fopen("/proc/cpuinfo", "r");
+    if (!fd) {
+	fprintf(stderr, "failed to get cpu frequecy\n");
+	perror(NULL);
+	return freq;
+    }
+
+    while (getline(&line, &len, fd) != EOF) {
+	if (sscanf(line, "cpu MHz\t: %f", &freqf) == 1) {
+	    freqf = freqf * 1000000UL;
+	    freq = (uint64_t)freqf;
+	    break;
+	}
+    }
+
+    fclose(fd);
+    return freq;
+}
+
+static uint64_t
+read_tsc(void)
+{
+    uint32_t a, d;
+    __asm __volatile("rdtsc" : "=a" (a), "=d" (d));
+    return ((uint64_t) a) | (((uint64_t) d) << 32);
+}
+
+typedef uint64_t atomic_t;
+
+#define LOCK_PREFIX \
+		".section .smp_locks,\"a\"\n"	\
+		"  .align 4\n"			\
+		"  .long 661f\n" /* address */	\
+		".previous\n"			\
+	       	"661:\n\tlock; "
+
+static __inline__ void
+atomic_add(int i, uint64_t *v)
+{
+	__asm__ __volatile__(
+		LOCK_PREFIX "addl %1,%0"
+		:"+m" (v)
+		:"ir" (i));
+}
+
+#endif /* _BENCH_H_ */

File mmapbench/config.h

+#ifndef _CONFIG_H_
+#define _CONFIG_H_
+
+#define PATH_PREFIX "/tmp/_dirs/"
+#define NR_SUBDIRS  20
+#define NR_ALLDIRS  (NR_SUBDIRS*NR_SUBDIRS*NR_SUBDIRS)
+
+#endif /* _CONFIG_H_ */

File mmapbench/genfile.sh

+#!/bin/sh
+
+dd if=/dev/zero of=share.dat bs=1M count=500
+
+# dd if=/dev/urandom of=share.dat bs=1M count=500

File mmapbench/mmapbench.c

+/**
+ * @file   mmap.c
+ * @author Wang Yuanxuan <zellux@gmail.com>
+ * @date   Fri Jan  8 21:23:31 2010
+ * 
+ * @brief  An implementation of mmap bench mentioned in OSMark paper
+ * 
+ * 
+ */
+
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <sys/mman.h>
+#include <stdint.h>
+#include <unistd.h>
+
+#include "config.h"
+#include "bench.h"
+
+/* int nbufs = 128000; */
+int nbufs = 64000;
+char *shared_area = NULL;
+int flag[32];
+int ncores = 4;
+char *filename = "share.dat";
+
+void *
+worker(void *args)
+{
+    int id = (long) args;
+    int ret = 0;
+    int i;
+
+    affinity_set(id);
+
+    for (i = 0; i < nbufs; i++)
+        ret += shared_area[i *4096];
+    
+    //printf("potato_test: thread#%d done.\n", core);
+
+    return (void *) (long) ret;
+}
+
+int
+main(int argc, char **argv)
+{
+    int i, fd;
+    pthread_t tid[32];
+    uint64_t start, end, usec;
+
+    for (i = 0; i < ncores; i++) {
+        flag[i] = 0;
+    }
+
+    if (argc > 1) {
+        ncores = atoi(argv[1]);
+    }
+
+    fd = open(filename, O_RDONLY);
+    shared_area = mmap(0, (1 + nbufs) * 4096, PROT_READ, MAP_PRIVATE, fd, 0);
+    
+    start = read_tsc();
+    for (i = 0; i < ncores; i++) {
+        pthread_create(&tid[i], NULL, worker, (void *) (long) i);
+    }
+
+    for (i = 0; i < ncores; i++) {
+        pthread_join(tid[i], NULL);
+    }
+    
+    end = read_tsc();
+    usec = (end - start) * 1000000 / get_cpu_freq();
+    printf("usec: %ld\t\n", usec);
+
+    close(fd);
+    return 0;
+}