More reworking of microbenchmark handling of reads/writes and file layout.
[bluesky.git] / microbench / mixedbench.c
index 7996f1d..a93d5f9 100644 (file)
@@ -29,6 +29,8 @@
 int opt_filesize, opt_filecount, opt_threads, opt_duration, opt_intervals, opt_dirsize;
 double opt_writeratio, opt_ops;
 
+int write_threads;
+
 struct thread_state {
     pthread_t thread;
     pthread_mutex_t lock;
@@ -90,18 +92,30 @@ void benchmark_op(struct thread_state *ts)
 
     start = now_hires();
 
-    char filename[256];
-    int n = get_random(opt_filecount);
-    int n1 = n / opt_dirsize, n2 = n % opt_dirsize;
-    sprintf(filename, "t%d/%d/%d", ts->thread_num, n1, n2);
+    /* 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;
+    }
 
-    double r = get_random(1000000) / 1e6;
+    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 (r >= opt_writeratio) {
+    if (ts->thread_num >= write_threads) {
         /* Read */
         FILE *f = fopen(filename, "rb");
         if (f == NULL) {
-            perror("fopen");
+            fprintf(stderr, "fopen(%s): %m\n", filename);
             return;
         }
 
@@ -119,7 +133,7 @@ void benchmark_op(struct thread_state *ts)
         /* Write */
         FILE *f = fopen(filename, "wb");
         if (f == NULL) {
-            perror("fopen");
+            fprintf(stderr, "fopen(%s): %m\n", filename);
             return;
         }
 
@@ -196,6 +210,7 @@ void reset_stats(int print, double duration)
         write_time2 += threads[i].write_time2;
         threads[i].read_count = threads[i].write_count = 0;
         threads[i].read_time = threads[i].write_time = 0;
+        threads[i].read_time2 = threads[i].write_time2 = 0;
         pthread_mutex_unlock(&threads[i].lock);
     }
 
@@ -231,6 +246,12 @@ int main(int argc, char *argv[])
 
     start_time = now_hires();
 
+    /* Partition threads into those that should do reads and those for writes,
+     * as close as possible to the desired allocation. */
+    write_threads = (int)round(opt_threads * opt_writeratio);
+    fprintf(stderr, "Using %d threads for reads, %d for writes\n",
+            opt_threads - write_threads, write_threads);
+
     for (int i = 0; i < opt_threads; i++) {
         launch_thread(i);
     }