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)
12 * Interval count (how many times to report results during the run)
23 #include <sys/types.h>
28 int opt_filesize, opt_filecount, opt_threads, opt_duration, opt_intervals;
29 double opt_writeratio, opt_ops;
35 int read_count, write_count;
36 double read_time, write_time, read_time2, write_time2;
39 static int64_t start_time;
41 #define MAX_THREADS 128
42 struct thread_state threads[MAX_THREADS];
44 static double sq(double x)
49 static double stddev(double x, double x2, int n)
53 return sqrt((x2 / n - sq(x / n)) * n / (n - 1));
60 if (clock_gettime(CLOCK_REALTIME, &time) != 0) {
61 perror("clock_gettime");
65 return (int64_t)(time.tv_sec) * 1000000000 + time.tv_nsec;
68 int get_random(int range)
70 return random() % range;
73 void sleep_micros(int duration)
79 req.tv_sec = duration / 1000000;
80 req.tv_nsec = (duration % 1000000) * 1000;
82 while (nanosleep(&req, &req) < 0 && errno == EINTR)
86 void benchmark_op(struct thread_state *ts)
93 sprintf(filename, "t%d/%d", ts->thread_num, get_random(opt_filecount));
95 double r = get_random(1000000) / 1e6;
97 if (r >= opt_writeratio) {
99 FILE *f = fopen(filename, "rb");
106 while (fread(buf, 1, sizeof(buf), f) > 0) { }
110 pthread_mutex_lock(&ts->lock);
112 ts->read_time += (end - start) / 1e9;
113 ts->read_time2 += sq((end - start) / 1e9);
114 pthread_mutex_unlock(&ts->lock);
117 FILE *f = fopen(filename, "wb");
124 int bytes_left = opt_filesize;
125 while (bytes_left > 0) {
126 size_t written = fwrite(buf, 1,
127 bytes_left < sizeof(buf)
128 ? bytes_left : sizeof(buf),
132 bytes_left -= written;
137 pthread_mutex_lock(&ts->lock);
139 ts->write_time += (end - start) / 1e9;
140 ts->write_time2 += sq((end - start) / 1e9);
141 pthread_mutex_unlock(&ts->lock);
145 void *benchmark_thread(void *arg)
147 struct thread_state *ts = (struct thread_state *)arg;
149 int target_delay = (opt_threads / opt_ops) * 1e6;
152 int64_t start = now_hires();
154 int64_t end = now_hires();
156 int elapsed = (end - start) / 1000;
157 if (elapsed < target_delay)
158 sleep_micros(target_delay - elapsed);
164 void launch_thread(int i)
166 memset(&threads[i], 0, sizeof(struct thread_state));
167 threads[i].thread_num = i;
168 pthread_mutex_init(&threads[i].lock, NULL);
169 if (pthread_create(&threads[i].thread, NULL, benchmark_thread, &threads[i]) != 0) {
170 fprintf(stderr, "Error launching thread!\n");
175 void wait_thread(int n)
178 pthread_join(threads[n].thread, &result);
181 void reset_stats(int print, double duration)
183 int read_count = 0, write_count = 0;
184 double read_time = 0, write_time = 0, read_time2 = 0, write_time2 = 0;
186 for (int i = 0; i < opt_threads; i++) {
187 pthread_mutex_lock(&threads[i].lock);
188 read_count += threads[i].read_count;
189 write_count += threads[i].write_count;
190 read_time += threads[i].read_time;
191 write_time += threads[i].write_time;
192 read_time2 += threads[i].read_time2;
193 write_time2 += threads[i].write_time2;
194 threads[i].read_count = threads[i].write_count = 0;
195 threads[i].read_time = threads[i].write_time = 0;
196 pthread_mutex_unlock(&threads[i].lock);
200 printf("read: [%g, %f, %f]\n",
201 read_count / duration, read_time / read_count,
202 stddev(read_time, read_time2, read_count));
203 printf("write: [%g, %f, %f]\n",
204 write_count / duration, write_time / write_count,
205 stddev(write_time, write_time2, write_count));
211 int main(int argc, char *argv[])
214 fprintf(stderr, "Usage: TODO\n");
218 opt_filesize = atoi(argv[1]);
219 opt_filecount = atoi(argv[2]);
220 opt_writeratio = atof(argv[3]);
221 opt_threads = atoi(argv[4]);
222 opt_duration = atoi(argv[5]);
223 opt_ops = atof(argv[6]);
224 opt_intervals = atoi(argv[7]);
228 start_time = now_hires();
230 for (int i = 0; i < opt_threads; i++) {
234 for (int i = 0; i < opt_intervals; i++) {
235 sleep_micros(opt_duration * 1000000 / opt_intervals);
236 reset_stats(1, (double)opt_duration / opt_intervals);