Fix standard deviation estimation.
[bluesky.git] / microbench / mixedbench.c
1 /* A simple file system workload generator.
2  *
3  * Reads and writes a number of files in the current working directory.
4  *
5  * Command-line arguments:
6  *   File size (bytes)
7  *   File count
8  *   Write fraction (0.0 - 1.0)
9  *   Threads
10  *   Benchmark duration (seconds)
11  *   Target operations per second (aggregate across all threads)
12  *   Interval count (how many times to report results during the run)
13  *   Directory size (number of files per numbered subdirectory)
14  */
15
16 #include <errno.h>
17 #include <inttypes.h>
18 #include <pthread.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <math.h>
28
29 int opt_filesize, opt_filecount, opt_threads, opt_duration, opt_intervals, opt_dirsize;
30 double opt_writeratio, opt_ops;
31
32 struct thread_state {
33     pthread_t thread;
34     pthread_mutex_t lock;
35     int thread_num;
36     int read_count, write_count;
37     double read_time, write_time, read_time2, write_time2;
38 };
39
40 static int64_t start_time;
41
42 #define MAX_THREADS 128
43 struct thread_state threads[MAX_THREADS];
44
45 static double sq(double x)
46 {
47     return x * x;
48 }
49
50 static double stddev(double x, double x2, int n)
51 {
52     if (n < 2)
53         return 0;
54     return sqrt((x2 / n - sq(x / n)) * n / (n - 1));
55 }
56
57 int64_t now_hires()
58 {
59     struct timespec time;
60
61     if (clock_gettime(CLOCK_REALTIME, &time) != 0) {
62         perror("clock_gettime");
63         return 0;
64     }
65
66     return (int64_t)(time.tv_sec) * 1000000000 + time.tv_nsec;
67 }
68
69 int get_random(int range)
70 {
71     return random() % range;
72 }
73
74 void sleep_micros(int duration)
75 {
76     struct timespec req;
77     if (duration <= 0)
78         return;
79
80     req.tv_sec = duration / 1000000;
81     req.tv_nsec = (duration % 1000000) * 1000;
82
83     while (nanosleep(&req, &req) < 0 && errno == EINTR)
84         ;
85 }
86
87 void benchmark_op(struct thread_state *ts)
88 {
89     int64_t start, end;
90
91     start = now_hires();
92
93     char filename[256];
94     int n = get_random(opt_filecount);
95     int n1 = n / opt_dirsize, n2 = n % opt_dirsize;
96     sprintf(filename, "t%d/%d/%d", ts->thread_num, n1, n2);
97
98     double r = get_random(1000000) / 1e6;
99
100     if (r >= opt_writeratio) {
101         /* Read */
102         FILE *f = fopen(filename, "rb");
103         if (f == NULL) {
104             perror("fopen");
105             return;
106         }
107
108         char buf[65536];
109         while (fread(buf, 1, sizeof(buf), f) > 0) { }
110         fclose(f);
111
112         end = now_hires();
113         pthread_mutex_lock(&ts->lock);
114         ts->read_count++;
115         ts->read_time += (end - start) / 1e9;
116         ts->read_time2 += sq((end - start) / 1e9);
117         pthread_mutex_unlock(&ts->lock);
118     } else {
119         /* Write */
120         FILE *f = fopen(filename, "wb");
121         if (f == NULL) {
122             perror("fopen");
123             return;
124         }
125
126         char buf[65536];
127         int bytes_left = opt_filesize;
128         while (bytes_left > 0) {
129             size_t written = fwrite(buf, 1,
130                                     bytes_left < sizeof(buf)
131                                      ? bytes_left : sizeof(buf),
132                                     f);
133             if (ferror(f))
134                 return;
135             bytes_left -= written;
136         }
137         fclose(f);
138
139         end = now_hires();
140         pthread_mutex_lock(&ts->lock);
141         ts->write_count++;
142         ts->write_time += (end - start) / 1e9;
143         ts->write_time2 += sq((end - start) / 1e9);
144         pthread_mutex_unlock(&ts->lock);
145     }
146 }
147
148 void *benchmark_thread(void *arg)
149 {
150     struct thread_state *ts = (struct thread_state *)arg;
151
152     int target_delay = (opt_threads / opt_ops) * 1e6;
153
154     while (1) {
155         int64_t start = now_hires();
156         benchmark_op(ts);
157         int64_t end = now_hires();
158
159         int elapsed = (end - start) / 1000;
160         if (elapsed < target_delay)
161             sleep_micros(target_delay - elapsed);
162     }
163
164     return NULL;
165 }
166
167 void launch_thread(int i)
168 {
169     memset(&threads[i], 0, sizeof(struct thread_state));
170     threads[i].thread_num = i;
171     pthread_mutex_init(&threads[i].lock, NULL);
172     if (pthread_create(&threads[i].thread, NULL, benchmark_thread, &threads[i]) != 0) {
173         fprintf(stderr, "Error launching thread!\n");
174         exit(1);
175     }
176 }
177
178 void wait_thread(int n)
179 {
180     void *result;
181     pthread_join(threads[n].thread, &result);
182 }
183
184 void reset_stats(int print, double duration)
185 {
186     int read_count = 0, write_count = 0;
187     double read_time = 0, write_time = 0, read_time2 = 0, write_time2 = 0;
188
189     for (int i = 0; i < opt_threads; i++) {
190         pthread_mutex_lock(&threads[i].lock);
191         read_count += threads[i].read_count;
192         write_count += threads[i].write_count;
193         read_time += threads[i].read_time;
194         write_time += threads[i].write_time;
195         read_time2 += threads[i].read_time2;
196         write_time2 += threads[i].write_time2;
197         threads[i].read_count = threads[i].write_count = 0;
198         threads[i].read_time = threads[i].write_time = 0;
199         threads[i].read_time2 = threads[i].write_time2 = 0;
200         pthread_mutex_unlock(&threads[i].lock);
201     }
202
203     if (print) {
204         printf("read: [%g, %f, %f]\n",
205                read_count / duration, read_time / read_count,
206                stddev(read_time, read_time2, read_count));
207         printf("write: [%g, %f, %f]\n",
208                write_count / duration, write_time / write_count,
209                stddev(write_time, write_time2, write_count));
210         printf("\n");
211         fflush(stdout);
212     }
213 }
214
215 int main(int argc, char *argv[])
216 {
217     if (argc != 9) {
218         fprintf(stderr, "Usage: TODO\n");
219         return 1;
220     }
221
222     opt_filesize = atoi(argv[1]);
223     opt_filecount = atoi(argv[2]);
224     opt_writeratio = atof(argv[3]);
225     opt_threads = atoi(argv[4]);
226     opt_duration = atoi(argv[5]);
227     opt_ops = atof(argv[6]);
228     opt_intervals = atoi(argv[7]);
229     opt_dirsize = atoi(argv[8]);
230
231     srandom(time(NULL));
232
233     start_time = now_hires();
234
235     for (int i = 0; i < opt_threads; i++) {
236         launch_thread(i);
237     }
238
239     for (int i = 0; i < opt_intervals; i++) {
240         sleep_micros(opt_duration * 1000000 / opt_intervals);
241         reset_stats(1, (double)opt_duration / opt_intervals);
242     }
243
244     return 0;
245 }