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)
13 * Directory size (number of files per numbered subdirectory)
24 #include <sys/types.h>
29 int opt_filesize, opt_filecount, opt_threads, opt_duration, opt_intervals, opt_dirsize;
30 double opt_writeratio, opt_ops;
36 int read_count, write_count;
37 double read_time, write_time, read_time2, write_time2;
40 static int64_t start_time;
42 #define MAX_THREADS 128
43 struct thread_state threads[MAX_THREADS];
45 static double sq(double x)
50 static double stddev(double x, double x2, int n)
54 return sqrt((x2 / n - sq(x / n)) * n / (n - 1));
61 if (clock_gettime(CLOCK_REALTIME, &time) != 0) {
62 perror("clock_gettime");
66 return (int64_t)(time.tv_sec) * 1000000000 + time.tv_nsec;
69 int get_random(int range)
71 return random() % range;
74 void sleep_micros(int duration)
80 req.tv_sec = duration / 1000000;
81 req.tv_nsec = (duration % 1000000) * 1000;
83 while (nanosleep(&req, &req) < 0 && errno == EINTR)
87 void benchmark_op(struct thread_state *ts)
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);
98 double r = get_random(1000000) / 1e6;
100 if (r >= opt_writeratio) {
102 FILE *f = fopen(filename, "rb");
109 while (fread(buf, 1, sizeof(buf), f) > 0) { }
113 pthread_mutex_lock(&ts->lock);
115 ts->read_time += (end - start) / 1e9;
116 ts->read_time2 += sq((end - start) / 1e9);
117 pthread_mutex_unlock(&ts->lock);
120 FILE *f = fopen(filename, "wb");
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),
135 bytes_left -= written;
140 pthread_mutex_lock(&ts->lock);
142 ts->write_time += (end - start) / 1e9;
143 ts->write_time2 += sq((end - start) / 1e9);
144 pthread_mutex_unlock(&ts->lock);
148 void *benchmark_thread(void *arg)
150 struct thread_state *ts = (struct thread_state *)arg;
152 int target_delay = (opt_threads / opt_ops) * 1e6;
155 int64_t start = now_hires();
157 int64_t end = now_hires();
159 int elapsed = (end - start) / 1000;
160 if (elapsed < target_delay)
161 sleep_micros(target_delay - elapsed);
167 void launch_thread(int i)
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");
178 void wait_thread(int n)
181 pthread_join(threads[n].thread, &result);
184 void reset_stats(int print, double duration)
186 int read_count = 0, write_count = 0;
187 double read_time = 0, write_time = 0, read_time2 = 0, write_time2 = 0;
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 pthread_mutex_unlock(&threads[i].lock);
203 printf("read: [%g, %f, %f]\n",
204 read_count / duration, read_time / read_count,
205 stddev(read_time, read_time2, read_count));
206 printf("write: [%g, %f, %f]\n",
207 write_count / duration, write_time / write_count,
208 stddev(write_time, write_time2, write_count));
214 int main(int argc, char *argv[])
217 fprintf(stderr, "Usage: TODO\n");
221 opt_filesize = atoi(argv[1]);
222 opt_filecount = atoi(argv[2]);
223 opt_writeratio = atof(argv[3]);
224 opt_threads = atoi(argv[4]);
225 opt_duration = atoi(argv[5]);
226 opt_ops = atof(argv[6]);
227 opt_intervals = atoi(argv[7]);
228 opt_dirsize = atoi(argv[8]);
232 start_time = now_hires();
234 for (int i = 0; i < opt_threads; i++) {
238 for (int i = 0; i < opt_intervals; i++) {
239 sleep_micros(opt_duration * 1000000 / opt_intervals);
240 reset_stats(1, (double)opt_duration / opt_intervals);