Work on a parallel read benchmark.
authorMichael Vrable <mvrable@cs.ucsd.edu>
Fri, 29 Jan 2010 22:20:41 +0000 (14:20 -0800)
committerMichael Vrable <mvrable@cs.ucsd.edu>
Fri, 29 Jan 2010 22:20:41 +0000 (14:20 -0800)
microbench/readbench.c [new file with mode: 0644]

diff --git a/microbench/readbench.c b/microbench/readbench.c
new file mode 100644 (file)
index 0000000..88eb888
--- /dev/null
@@ -0,0 +1,108 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <unistd.h>
+#include <errno.h>
+#include <time.h>
+#include <pthread.h>
+
+struct thread_state {
+    pthread_t thread;
+    int thread_num;
+    long long timestamp;
+};
+
+#define MAX_THREADS 128
+struct thread_state threads[MAX_THREADS];
+
+int64_t now_hires()
+{
+    struct timespec time;
+
+    if (clock_gettime(CLOCK_REALTIME, &time) != 0) {
+        perror("clock_gettime");
+        return 0;
+    }
+
+    return (int64_t)(time.tv_sec) * 1000000000 + time.tv_nsec;
+}
+
+#if 0
+void bench_write()
+{
+    FILE *f = fopen("writetest", "wb");
+    if (f == NULL) {
+        perror("fopen");
+        return;
+    }
+
+    char buf[4096];
+    for (int i = 0; i < 16; i++) {
+        int64_t start, end;
+        start = now_hires();
+        rewind(f);
+        fwrite(buf, 1, sizeof(buf), f);
+        fflush(f);
+        end = now_hires();
+        printf("Pass %d: Time = %"PRIi64"\n", i, end - start);
+    }
+}
+#endif
+
+void *benchmark_thread(void *arg)
+{
+    struct thread_state *ts = (struct thread_state *)arg;
+
+    char namebuf[64];
+    sprintf(namebuf, "file-%d", ts->thread_num);
+    printf("Opening %s\n", namebuf);
+
+    int64_t start, end;
+
+    start = now_hires();
+    FILE *f = fopen(namebuf, "rb");
+    if (f == NULL) {
+        perror("fopen");
+        return NULL;
+    }
+
+    char buf[4096];
+    fread(buf, 1, sizeof(buf), f);
+
+    end = now_hires();
+    printf("Thread %d: Time = %"PRIi64"\n", ts->thread_num, end - start);
+
+    fclose(f);
+
+    return NULL;
+}
+
+void launch_thread(int i)
+{
+    threads[i].thread_num = i;
+    printf("Launching thread %d...\n", i);
+    if (pthread_create(&threads[i].thread, NULL, benchmark_thread, &threads[i]) != 0) {
+        fprintf(stderr, "Error launching thread!\n");
+        exit(1);
+    }
+}
+
+void wait_thread(int n)
+{
+    void *result;
+    pthread_join(threads[n].thread, &result);
+}
+
+int main(int argc, char *argv[])
+{
+    for (int i = 0; i < 8; i++) {
+        launch_thread(i);
+    }
+
+    for (int i = 0; i < 8; i++) {
+        wait_thread(i);
+    }
+
+    return 0;
+}