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;
38 int read_count, write_count;
39 double read_time, write_time, read_time2, write_time2;
42 static int64_t start_time;
44 #define MAX_THREADS 128
45 struct thread_state threads[MAX_THREADS];
47 static double sq(double x)
52 static double stddev(double x, double x2, int n)
56 return sqrt((x2 / n - sq(x / n)) * n / (n - 1));
63 if (clock_gettime(CLOCK_REALTIME, &time) != 0) {
64 perror("clock_gettime");
68 return (int64_t)(time.tv_sec) * 1000000000 + time.tv_nsec;
71 int get_random(int range)
73 return random() % range;
76 void sleep_micros(int duration)
82 req.tv_sec = duration / 1000000;
83 req.tv_nsec = (duration % 1000000) * 1000;
85 while (nanosleep(&req, &req) < 0 && errno == EINTR)
89 void benchmark_op(struct thread_state *ts)
96 int n = get_random(opt_filecount);
97 int n1 = n / opt_dirsize, n2 = n % opt_dirsize;
99 if (ts->thread_num >= write_threads) {
101 sprintf(filename, "t%d/%d/%d", ts->thread_num - write_threads, n1, n2);
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 sprintf(filename, "t%d/%d/%d", ts->thread_num, n1, n2);
121 FILE *f = fopen(filename, "wb");
128 int bytes_left = opt_filesize;
129 while (bytes_left > 0) {
130 size_t written = fwrite(buf, 1,
131 bytes_left < sizeof(buf)
132 ? bytes_left : sizeof(buf),
136 bytes_left -= written;
141 pthread_mutex_lock(&ts->lock);
143 ts->write_time += (end - start) / 1e9;
144 ts->write_time2 += sq((end - start) / 1e9);
145 pthread_mutex_unlock(&ts->lock);
149 void *benchmark_thread(void *arg)
151 struct thread_state *ts = (struct thread_state *)arg;
153 int target_delay = (opt_threads / opt_ops) * 1e6;
156 int64_t start = now_hires();
158 int64_t end = now_hires();
160 int elapsed = (end - start) / 1000;
161 if (elapsed < target_delay)
162 sleep_micros(target_delay - elapsed);
168 void launch_thread(int i)
170 memset(&threads[i], 0, sizeof(struct thread_state));
171 threads[i].thread_num = i;
172 pthread_mutex_init(&threads[i].lock, NULL);
173 if (pthread_create(&threads[i].thread, NULL, benchmark_thread, &threads[i]) != 0) {
174 fprintf(stderr, "Error launching thread!\n");
179 void wait_thread(int n)
182 pthread_join(threads[n].thread, &result);
185 void reset_stats(int print, double duration)
187 int read_count = 0, write_count = 0;
188 double read_time = 0, write_time = 0, read_time2 = 0, write_time2 = 0;
190 for (int i = 0; i < opt_threads; i++) {
191 pthread_mutex_lock(&threads[i].lock);
192 read_count += threads[i].read_count;
193 write_count += threads[i].write_count;
194 read_time += threads[i].read_time;
195 write_time += threads[i].write_time;
196 read_time2 += threads[i].read_time2;
197 write_time2 += threads[i].write_time2;
198 threads[i].read_count = threads[i].write_count = 0;
199 threads[i].read_time = threads[i].write_time = 0;
200 threads[i].read_time2 = threads[i].write_time2 = 0;
201 pthread_mutex_unlock(&threads[i].lock);
205 printf("read: [%g, %f, %f]\n",
206 read_count / duration, read_time / read_count,
207 stddev(read_time, read_time2, read_count));
208 printf("write: [%g, %f, %f]\n",
209 write_count / duration, write_time / write_count,
210 stddev(write_time, write_time2, write_count));
216 int main(int argc, char *argv[])
219 fprintf(stderr, "Usage: TODO\n");
223 opt_filesize = atoi(argv[1]);
224 opt_filecount = atoi(argv[2]);
225 opt_writeratio = atof(argv[3]);
226 opt_threads = atoi(argv[4]);
227 opt_duration = atoi(argv[5]);
228 opt_ops = atof(argv[6]);
229 opt_intervals = atoi(argv[7]);
230 opt_dirsize = atoi(argv[8]);
234 start_time = now_hires();
236 /* Partition threads into those that should do reads and those for writes,
237 * as close as possible to the desired allocation. */
238 write_threads = (int)round(opt_threads * opt_writeratio);
239 fprintf(stderr, "Using %d threads for reads, %d for writes\n",
240 opt_threads - write_threads, write_threads);
242 for (int i = 0; i < opt_threads; i++) {
246 for (int i = 0; i < opt_intervals; i++) {
247 sleep_micros(opt_duration * 1000000 / opt_intervals);
248 reset_stats(1, (double)opt_duration / opt_intervals);