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)
95 /* The space of all files is partitioned evenly based on the number of
96 * threads. Pick a file out of our particular partition. */
97 int thread_num, thread_count;
98 if (ts->thread_num >= write_threads) {
100 thread_num = ts->thread_num - write_threads;
101 thread_count = opt_threads - write_threads;
104 thread_num = ts->thread_num;
105 thread_count = write_threads;
108 int n = get_random(opt_filecount / thread_count);
109 n += thread_num * (opt_filecount / thread_count);
110 int n1 = n / opt_dirsize, n2 = n % opt_dirsize;
112 sprintf(filename, "%d/%d", n1, n2);
114 if (ts->thread_num >= write_threads) {
116 FILE *f = fopen(filename, "rb");
118 fprintf(stderr, "fopen(%s): %m\n", filename);
123 while (fread(buf, 1, sizeof(buf), f) > 0) { }
127 pthread_mutex_lock(&ts->lock);
129 ts->read_time += (end - start) / 1e9;
130 ts->read_time2 += sq((end - start) / 1e9);
131 pthread_mutex_unlock(&ts->lock);
134 FILE *f = fopen(filename, "wb");
136 fprintf(stderr, "fopen(%s): %m\n", filename);
141 int bytes_left = opt_filesize;
142 while (bytes_left > 0) {
143 size_t written = fwrite(buf, 1,
144 bytes_left < sizeof(buf)
145 ? bytes_left : sizeof(buf),
149 bytes_left -= written;
154 pthread_mutex_lock(&ts->lock);
156 ts->write_time += (end - start) / 1e9;
157 ts->write_time2 += sq((end - start) / 1e9);
158 pthread_mutex_unlock(&ts->lock);
162 void *benchmark_thread(void *arg)
164 struct thread_state *ts = (struct thread_state *)arg;
166 int target_delay = (opt_threads / opt_ops) * 1e6;
169 int64_t start = now_hires();
171 int64_t end = now_hires();
173 int elapsed = (end - start) / 1000;
174 if (elapsed < target_delay)
175 sleep_micros(target_delay - elapsed);
181 void launch_thread(int i)
183 memset(&threads[i], 0, sizeof(struct thread_state));
184 threads[i].thread_num = i;
185 pthread_mutex_init(&threads[i].lock, NULL);
186 if (pthread_create(&threads[i].thread, NULL, benchmark_thread, &threads[i]) != 0) {
187 fprintf(stderr, "Error launching thread!\n");
192 void wait_thread(int n)
195 pthread_join(threads[n].thread, &result);
198 void reset_stats(int print, double duration)
200 int read_count = 0, write_count = 0;
201 double read_time = 0, write_time = 0, read_time2 = 0, write_time2 = 0;
203 for (int i = 0; i < opt_threads; i++) {
204 pthread_mutex_lock(&threads[i].lock);
205 read_count += threads[i].read_count;
206 write_count += threads[i].write_count;
207 read_time += threads[i].read_time;
208 write_time += threads[i].write_time;
209 read_time2 += threads[i].read_time2;
210 write_time2 += threads[i].write_time2;
211 threads[i].read_count = threads[i].write_count = 0;
212 threads[i].read_time = threads[i].write_time = 0;
213 threads[i].read_time2 = threads[i].write_time2 = 0;
214 pthread_mutex_unlock(&threads[i].lock);
218 printf("read: [%g, %f, %f]\n",
219 read_count / duration, read_time / read_count,
220 stddev(read_time, read_time2, read_count));
221 printf("write: [%g, %f, %f]\n",
222 write_count / duration, write_time / write_count,
223 stddev(write_time, write_time2, write_count));
229 int main(int argc, char *argv[])
232 fprintf(stderr, "Usage: TODO\n");
236 opt_filesize = atoi(argv[1]);
237 opt_filecount = atoi(argv[2]);
238 opt_writeratio = atof(argv[3]);
239 opt_threads = atoi(argv[4]);
240 opt_duration = atoi(argv[5]);
241 opt_ops = atof(argv[6]);
242 opt_intervals = atoi(argv[7]);
243 opt_dirsize = atoi(argv[8]);
247 start_time = now_hires();
249 /* Partition threads into those that should do reads and those for writes,
250 * as close as possible to the desired allocation. */
251 write_threads = (int)round(opt_threads * opt_writeratio);
252 fprintf(stderr, "Using %d threads for reads, %d for writes\n",
253 opt_threads - write_threads, write_threads);
255 for (int i = 0; i < opt_threads; i++) {
259 for (int i = 0; i < opt_intervals; i++) {
260 sleep_micros(opt_duration * 1000000 / opt_intervals);
261 reset_stats(1, (double)opt_duration / opt_intervals);