10 #define NUM_ITERATIONS 10000000
12 static volatile int wait_;
13 static long num_threads_;
16 void *worker (void *arg) {
18 // Wait for all the worker threads to be ready.
22 for (int i = 0; i < NUM_ITERATIONS/num_threads_; ++i) {
23 unsigned r = nbd_rand();
26 sprintf(key_str, "%X", key);
28 ll_add(ll_, key_str, strlen(key_str) + 1, 1);
30 ll_remove(ll_, key_str, strlen(key_str) + 1);
39 int main (int argc, char **argv) {
41 //lwt_set_trace_level("m0l0");
43 char* program_name = argv[0];
44 pthread_t thread[MAX_NUM_THREADS];
47 fprintf(stderr, "Usage: %s num_threads\n", program_name);
55 num_threads_ = strtol(argv[1], NULL, 10);
57 fprintf(stderr, "%s: Invalid argument for number of threads\n", program_name);
60 if (num_threads_ <= 0) {
61 fprintf(stderr, "%s: Number of threads must be at least 1\n", program_name);
64 if (num_threads_ > MAX_NUM_THREADS) {
65 fprintf(stderr, "%s: Number of threads cannot be more than %d\n", program_name, MAX_NUM_THREADS);
72 struct timeval tv1, tv2;
73 gettimeofday(&tv1, NULL);
77 for (int i = 0; i < num_threads_; ++i) {
78 int rc = nbd_thread_create(thread + i, i, worker, (void*)(size_t)i);
79 if (rc != 0) { perror("pthread_create"); return rc; }
82 for (int i = 0; i < num_threads_; ++i) {
83 pthread_join(thread[i], NULL);
86 gettimeofday(&tv2, NULL);
87 int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
89 printf("Th:%ld Time:%dms\n", num_threads_, ms);