More reworking of microbenchmark handling of reads/writes and file layout.
[bluesky.git] / microbench / mixedbench.c
index 0447fd1..a93d5f9 100644 (file)
@@ -92,16 +92,30 @@ void benchmark_op(struct thread_state *ts)
 
     start = now_hires();
 
-    char filename[256];
-    int n = get_random(opt_filecount);
+    /* The space of all files is partitioned evenly based on the number of
+     * threads.  Pick a file out of our particular partition. */
+    int thread_num, thread_count;
+    if (ts->thread_num >= write_threads) {
+        /* Read */
+        thread_num = ts->thread_num - write_threads;
+        thread_count = opt_threads - write_threads;
+    } else {
+        /* Write */
+        thread_num = ts->thread_num;
+        thread_count = write_threads;
+    }
+
+    int n = get_random(opt_filecount / thread_count);
+    n += thread_num * (opt_filecount / thread_count);
     int n1 = n / opt_dirsize, n2 = n % opt_dirsize;
+    char filename[256];
+    sprintf(filename, "%d/%d", n1, n2);
 
     if (ts->thread_num >= write_threads) {
         /* Read */
-        sprintf(filename, "t%d/%d/%d", ts->thread_num - write_threads, n1, n2);
         FILE *f = fopen(filename, "rb");
         if (f == NULL) {
-            perror("fopen");
+            fprintf(stderr, "fopen(%s): %m\n", filename);
             return;
         }
 
@@ -117,10 +131,9 @@ void benchmark_op(struct thread_state *ts)
         pthread_mutex_unlock(&ts->lock);
     } else {
         /* Write */
-        sprintf(filename, "t%d/%d/%d", ts->thread_num, n1, n2);
         FILE *f = fopen(filename, "wb");
         if (f == NULL) {
-            perror("fopen");
+            fprintf(stderr, "fopen(%s): %m\n", filename);
             return;
         }