1 /* A simple file system workload generator.
3 * Reads and writes a number of files in the current working directory.
5 * Command-line arguments:
8 * Write fraction (0.0 - 1.0)
10 * Benchmark duration (seconds)
11 * Target operations per second (aggregate across all threads)
22 #include <sys/types.h>
27 int opt_filesize, opt_filecount, opt_threads, opt_duration;
28 double opt_writeratio, opt_ops;
34 int read_count, write_count;
35 double read_time, write_time, read_time2, write_time2;
38 static int64_t start_time;
40 #define MAX_THREADS 128
41 struct thread_state threads[MAX_THREADS];
43 static double sq(double x)
48 static double stddev(double x, double x2, int n)
52 return sqrt((x2 / n - sq(x / n)) * n / (n - 1));
59 if (clock_gettime(CLOCK_REALTIME, &time) != 0) {
60 perror("clock_gettime");
64 return (int64_t)(time.tv_sec) * 1000000000 + time.tv_nsec;
67 int get_random(int range)
69 return random() % range;
72 void sleep_micros(int duration)
78 req.tv_sec = duration / 1000000;
79 req.tv_nsec = (duration % 1000000) * 1000;
81 while (nanosleep(&req, &req) < 0 && errno == EINTR)
85 void benchmark_op(struct thread_state *ts)
92 sprintf(filename, "t%d/%d", ts->thread_num, get_random(opt_filecount));
94 double r = get_random(1000000) / 1e6;
96 if (r >= opt_writeratio) {
98 FILE *f = fopen(filename, "rb");
105 while (fread(buf, 1, sizeof(buf), f) > 0) { }
109 pthread_mutex_lock(&ts->lock);
111 ts->read_time += (end - start) / 1e9;
112 ts->read_time2 += sq((end - start) / 1e9);
113 pthread_mutex_unlock(&ts->lock);
116 FILE *f = fopen(filename, "wb");
123 int bytes_left = opt_filesize;
124 while (bytes_left > 0) {
125 size_t written = fwrite(buf, 1,
126 bytes_left < sizeof(buf)
127 ? bytes_left : sizeof(buf),
131 bytes_left -= written;
136 pthread_mutex_lock(&ts->lock);
138 ts->write_time += (end - start) / 1e9;
139 ts->write_time2 += sq((end - start) / 1e9);
140 pthread_mutex_unlock(&ts->lock);
144 void *benchmark_thread(void *arg)
146 struct thread_state *ts = (struct thread_state *)arg;
148 int target_delay = (opt_threads / opt_ops) * 1e6;
151 int64_t start = now_hires();
153 int64_t end = now_hires();
155 int elapsed = (end - start) / 1000;
156 if (elapsed < target_delay)
157 sleep_micros(target_delay - elapsed);
163 void launch_thread(int i)
165 memset(&threads[i], 0, sizeof(struct thread_state));
166 threads[i].thread_num = i;
167 pthread_mutex_init(&threads[i].lock, NULL);
168 if (pthread_create(&threads[i].thread, NULL, benchmark_thread, &threads[i]) != 0) {
169 fprintf(stderr, "Error launching thread!\n");
174 void wait_thread(int n)
177 pthread_join(threads[n].thread, &result);
180 void reset_stats(int print, double duration)
182 int read_count = 0, write_count = 0;
183 double read_time = 0, write_time = 0, read_time2 = 0, write_time2 = 0;
185 for (int i = 0; i < opt_threads; i++) {
186 pthread_mutex_lock(&threads[i].lock);
187 read_count += threads[i].read_count;
188 write_count += threads[i].write_count;
189 read_time += threads[i].read_time;
190 write_time += threads[i].write_time;
191 read_time2 += threads[i].read_time2;
192 write_time2 += threads[i].write_time2;
193 threads[i].read_count = threads[i].write_count = 0;
194 threads[i].read_time = threads[i].write_time = 0;
195 pthread_mutex_unlock(&threads[i].lock);
199 printf("read: [%g, %f, %f]\n",
200 read_count / duration, read_time / read_count,
201 stddev(read_time, read_time2, read_count));
202 printf("write: [%g, %f, %f]\n",
203 write_count / duration, write_time / write_count,
204 stddev(write_time, write_time2, write_count));
209 int main(int argc, char *argv[])
212 fprintf(stderr, "Usage: TODO\n");
216 opt_filesize = atoi(argv[1]);
217 opt_filecount = atoi(argv[2]);
218 opt_writeratio = atof(argv[3]);
219 opt_threads = atoi(argv[4]);
220 opt_duration = atoi(argv[5]);
221 opt_ops = atof(argv[6]);
225 start_time = now_hires();
227 for (int i = 0; i < opt_threads; i++) {
231 for (int i = 0; i < 4; i++) {
232 sleep_micros(opt_duration * 1000000 / 4);
233 reset_stats(1, opt_duration / 4.0);