static void test_io_close(int with_timer, bool from_path) { #define chunks 4 #define READSIZE (512*1024) unsigned int i; const char *path = LARGE_FILE; int fd = open(path, O_RDONLY); if (fd == -1) { if (errno == ENOENT) { test_skip("Large file not found"); return; } test_errno("open", errno, 0); test_stop(); } #ifdef F_GLOBAL_NOCACHE if (fcntl(fd, F_GLOBAL_NOCACHE, 1) == -1) { test_errno("fcntl F_GLOBAL_NOCACHE", errno, 0); test_stop(); } #endif struct stat sb; if (fstat(fd, &sb)) { test_errno("fstat", errno, 0); test_stop(); } const size_t size = (size_t)sb.st_size / chunks; const int expected_error = with_timer? ECANCELED : 0; dispatch_source_t t = NULL; dispatch_group_t g = dispatch_group_create(); dispatch_group_enter(g); void (^cleanup_handler)(int error) = ^(int error) { test_errno("create error", error, 0); dispatch_group_leave(g); close(fd); }; dispatch_io_t io; if (!from_path) { io = dispatch_io_create(DISPATCH_IO_RANDOM, fd, dispatch_get_global_queue(0, 0), cleanup_handler); } else { #if DISPATCHTEST_IO_PATH io = dispatch_io_create_with_path(DISPATCH_IO_RANDOM, path, O_RDONLY, 0, dispatch_get_global_queue(0, 0), cleanup_handler); #endif } dispatch_io_set_high_water(io, READSIZE); if (with_timer == 1) { dispatch_io_set_low_water(io, READSIZE); dispatch_io_set_interval(io, 2 * NSEC_PER_SEC, DISPATCH_IO_STRICT_INTERVAL); } else if (with_timer == 2) { t = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0,0)); dispatch_retain(io); dispatch_source_set_event_handler(t, ^{ dispatch_io_close(io, DISPATCH_IO_STOP); dispatch_source_cancel(t); });
void finalize(void *ctxt) { test_ptr_null("finalizer ran", NULL); test_ptr("correct context", ctxt, ctxt_magic); test_stop(); }
void work(void *context) { UNREFERENCED_PARAMETER(context); test_stop(); exit(0); }
void test_timer(void) { dispatch_test_start("Dispatch Update Timer"); dispatch_queue_t main_q = dispatch_get_main_queue(); //test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue()); __block int i = 0; struct timeval start_time; gettimeofday(&start_time, NULL); dispatch_source_t s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q); test_ptr_notnull("dispatch_source_create", s); dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), NSEC_PER_SEC, 0); dispatch_source_set_cancel_handler(s, ^{ struct timeval end_time; gettimeofday(&end_time, NULL); // Make sure we actually managed to adjust the interval // duration. Seven one second ticks would blow past // this. test_long_less_than("total duration", end_time.tv_sec - start_time.tv_sec, 3); test_stop(); dispatch_release(s); });
void timer_handler(void* context) { struct timeval now_tv; double now; double goal; double jitter; double drift; gettimeofday(&now_tv, NULL); now = now_tv.tv_sec + ((double)now_tv.tv_usec / (double)USEC_PER_SEC); if (first == 0) { first = now; } goal = first + interval_d * count; jitter = goal - now; drift = jitter - last_jitter; count += dispatch_source_get_data(timer); jittersum += jitter; driftsum += drift; printf("%4d: jitter %f, drift %f\n", count, jitter, drift); if (count >= target) { test_double_less_than("average jitter", fabs(jittersum) / (double)count, 0.0001); test_double_less_than("average drift", fabs(driftsum) / (double)count, 0.0001); test_stop(); } last_jitter = jitter; }
static void done(void *arg /*__unused */) { (void)arg; sleep(1); test_stop(); }
static bool dispatch_test_check_evfilt_vm(void) { int kq = kqueue(); assert(kq != -1); struct kevent ke = { .filter = EVFILT_VM, .flags = EV_ADD|EV_ENABLE|EV_RECEIPT, .fflags = NOTE_VM_PRESSURE, }; int r = kevent(kq, &ke, 1, &ke, 1, NULL); close(kq); return !(r > 0 && ke.flags & EV_ERROR && ke.data == ENOTSUP); } static void cleanup(void) { dispatch_source_cancel(vm_source); dispatch_release(vm_source); dispatch_release(vm_queue); int32_t pc = 0, i; for (i = 0; i < max_page_count; ++i) { if (pages[i]) { pc++; free(pages[i]); } } if (pc) { log_msg("Freed %ldMB\n", pg2mb(pc)); } free(pages); test_stop(); }
void fini(void *cxt) { test_ptr_notnull("finalizer ran", cxt); if (cxt == tweedledum) { test_stop(); } }
static void writer(void *ctxt) { if (--w_count == 0) { if (r_count == 0) { test_stop(); } } }
static void reader(void *ctxt) { if (dispatch_atomic_dec(&r_count) == 0) { if (r_count == 0) { test_stop(); } } }
static void cancel_handler(void* context) { struct timeval end_time; UNREFERENCED_PARAMETER(context); gettimeofday(&end_time, NULL); // Make sure we actually managed to adjust the interval // duration. Seven one second ticks would blow past // this. test_long_less_than("total duration", end_time.tv_sec - start_time.tv_sec, 3); test_ptr_notnull("finalizer ran", timer); test_stop(); }
static void writer(void *ctxt) { size_t w = __sync_add_and_fetch(&writers, 1), *m = (size_t *)ctxt; if (w > *m) *m = w; usleep(10000); size_t busy = BUSY; while (busy--) if (readers) __sync_add_and_fetch(&crw, 1); if (__sync_sub_and_fetch(&w_count, 1) == 0) { if (r_count == 0) { dispatch_async(dispatch_get_main_queue(), ^{test_stop();}); }
void test_fin(void *cxt) { fprintf(stderr, "Called back every %llu us on average\n", (delay/count)/NSEC_PER_USEC); test_long_less_than("Frequency", 1, ceil((double)delay/(count*interval))); int i; for (i = 0; i < N; i++) { dispatch_source_cancel(t[i]); dispatch_release(t[i]); } dispatch_resume(q); dispatch_release(q); dispatch_release(g); test_ptr("finalizer ran", cxt, cxt); test_stop(); }
int main(void) { test_start("Dispatch Suspend Timer"); dispatch_queue_t main_q = dispatch_get_main_queue(); test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue()); __block int i = 0; __block int j = 0; dispatch_source_attr_t attr = dispatch_source_attr_create(); test_ptr_notnull("dispatch_source_attr_create", attr); dispatch_source_attr_set_finalizer(attr, ^(dispatch_source_t ds) { test_ptr_notnull("finalizer ran", ds); if (ds == tweedledum) test_stop(); });
void cpubusy(void* context) { intptr_t *count = context; intptr_t iterdone; long idx; for (idx = 0; idx < LOOP_COUNT; ++idx) { if (done) break; } if ((iterdone = dispatch_atomic_dec(&iterations)) == 0) { dispatch_atomic_inc(&done); dispatch_atomic_inc(count); histogram(); test_stop(); exit(0); } else if (iterdone > 0) { dispatch_atomic_inc(count); } }
int main(void) { test_start("Dispatch Update Timer"); dispatch_queue_t main_q = dispatch_get_main_queue(); test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue()); __block int i = 0; struct timeval start_time; gettimeofday(&start_time, NULL); dispatch_source_attr_t attr = dispatch_source_attr_create(); dispatch_source_attr_set_finalizer(attr, ^(dispatch_source_t ds) { struct timeval end_time; gettimeofday(&end_time, NULL); // Make sure we actually managed to adjust the interval // duration. Seven one second ticks would blow past // this. test_long_less_than("total duration", end_time.tv_sec - start_time.tv_sec, 3); test_ptr_notnull("finalizer ran", ds); test_stop(); });
int main(void) { test_start("Dispatch Source Timer, bit 31"); dispatch_queue_t main_q = dispatch_get_main_queue(); test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue()); __block int i = 0; struct timeval start_time; gettimeofday(&start_time, NULL); dispatch_source_attr_t attr = dispatch_source_attr_create(); dispatch_source_attr_set_finalizer(attr, ^(dispatch_source_t ds) { struct timeval end_time; gettimeofday(&end_time, NULL); test_ptr_notnull("finalizer ran", ds); // XXX: check, s/b 2.0799... seconds, which is <4 seconds // when it could end on a bad boundry. test_long_less_than("needs to finish faster than 4 seconds", end_time.tv_sec - start_time.tv_sec, 4); // And it has to take at least two seconds... test_long_less_than("can't finish faster than 2 seconds", 1, end_time.tv_sec - start_time.tv_sec); test_stop(); });
int main(void) { dispatch_test_start("Dispatch VM Pressure test"); // rdar://problem/7000945 if (!dispatch_test_check_evfilt_vm()) { test_skip("EVFILT_VM not supported"); test_stop(); return 0; } initial = time(NULL); uint64_t memsize = _dispatch_get_memory_size(); max_page_count = MIN(memsize, MAXMEM) / ALLOC_SIZE; pages = calloc(max_page_count, sizeof(char*)); vm_queue = dispatch_queue_create("VM Pressure", NULL); vm_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VM, 0, DISPATCH_VM_PRESSURE, vm_queue); dispatch_source_set_event_handler(vm_source, ^{ if (!page_count) { // Too much memory pressure already to start the test test_skip("Memory pressure at start of test"); cleanup(); } if (__sync_add_and_fetch(&handler_call_count, 1) != NOTIFICATIONS) { log_msg("Ignoring vm pressure notification\n"); interval = 1; return; } test_long("dispatch_source_get_data()", dispatch_source_get_data(vm_source), NOTE_VM_PRESSURE); int32_t i, pc = page_count + 1; for (i = 0; i < pc && pages[i]; ++i) { free(pages[i]); pages[i] = NULL; } log_msg("Freed %ldMB\n", pg2mb(i)); });
int main(int argc, char** argv) { struct hostent *he; int sockfd, clientfd; struct sockaddr_in addr1, addr2, server; socklen_t addr2len; socklen_t addr1len; pid_t clientid; const char *path = "/usr/share/dict/words"; int read_fd, fd; if (argc == 2) { // Client dispatch_test_start(NULL); if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { test_errno("Client-socket()", errno, 0); test_stop(); } if ((he = gethostbyname("localhost")) == NULL) { fprintf(stderr, "Client-gethostbyname() failed\n"); test_stop(); } memcpy(&server.sin_addr, he->h_addr_list[0], he->h_length); server.sin_family = AF_INET; server.sin_port = atoi(argv[1]); fprintf(stderr, "Client-connecting on port ... %d\n", server.sin_port); if (connect(sockfd, (struct sockaddr *)&server, sizeof(server))) { test_errno("client-connect()", errno, 0); test_stop(); } // Read from the socket and compare the contents are what we expect fd = open(path, O_RDONLY); if (fd == -1) { test_errno("client-open", errno, 0); test_stop(); } #ifdef F_NOCACHE if (fcntl(fd, F_NOCACHE, 1)) { test_errno("client-fcntl F_NOCACHE", errno, 0); test_stop(); } #else // investigate what the impact of lack of file cache disabling has // for this test #endif struct stat sb; if (fstat(fd, &sb)) { test_errno("client-fstat", errno, 0); test_stop(); } size_t size = sb.st_size; __block dispatch_data_t g_d1 = dispatch_data_empty; __block dispatch_data_t g_d2 = dispatch_data_empty; __block int g_error = 0; dispatch_group_t g = dispatch_group_create(); dispatch_group_enter(g); dispatch_read(fd, size, dispatch_get_global_queue(0, 0), ^(dispatch_data_t d1, int error) { test_errno("Client-dict-read error", error, 0); test_long("Client-dict-dispatch data size", dispatch_data_get_size(d1), size); dispatch_retain(d1); g_d1 = d1; dispatch_group_leave(g); });
void done(void *arg) { UNREFERENCED_PARAMETER(arg); Sleep(1000); test_stop(); }
void test_fin(void *cxt) { test_ptr("test_fin run", cxt, cxt); test_stop(); }
static void finalizer(void *ctxt) { test_ptr("finalizer ran", ctxt, ctxt_magic); test_stop(); }
void done(void *arg /*__unused */) { sleep(1); test_stop(); }
__block int i = 0; struct timeval start_time; gettimeofday(&start_time, NULL); static dispatch_source_t ds; ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, mainq); assert(ds); dispatch_source_set_event_handler(ds, ^{ assert(i < 1); printf("%d\n", i++); }); dispatch_source_set_timer(ds, DISPATCH_TIME_NOW, interval, 0); dispatch_resume(ds); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1*NSEC_PER_SEC), dispatch_get_main_queue(), ^{ test_stop(); }); } int main(void) { test_timer(); dispatch_main(); return 0; }
void test_continue_or_stop (void) { outputLongLine ("Do you want to start some workers? "); if (askYesNo ('Y')) test_continue (); else test_stop (); }
void main_menu (void) { unsigned long choice; mloop: if (THREAD_KILL) return; printf ("\t Main Menu\n"); loop: printf ("\n"); printf ("\t 1. Test/Primenet\n"); printf ("\t 2. Test/Worker threads\n"); printf ("\t 3. Test/Status\n"); if (WORKER_THREADS_ACTIVE && active_workers_count () < WORKER_THREADS_ACTIVE) printf ("\t 4. Test/Continue or Stop\n"); else if (!WORKER_THREADS_ACTIVE || WORKER_THREADS_STOPPING) printf ("\t 4. Test/Continue\n"); else printf ("\t 4. Test/Stop\n"); printf ("\t 5. Test/Exit\n"); printf ("\t 6. Advanced/Test\n"); printf ("\t 7. Advanced/Time\n"); printf ("\t 8. Advanced/P-1\n"); printf ("\t 9. Advanced/ECM\n"); printf ("\t10. Advanced/Manual Communication\n"); printf ("\t11. Advanced/Unreserve Exponent\n"); printf ("\t12. Advanced/Quit Gimps\n"); printf ("\t13. Options/CPU\n"); printf ("\t14. Options/Preferences\n"); printf ("\t15. Options/Torture Test\n"); printf ("\t16. Options/Benchmark\n"); printf ("\t17. Help/About\n"); printf ("\t18. Help/About PrimeNet Server\n"); printf ("Your choice: "); choice = get_number (0); if (choice <= 0 || choice >= 19) { printf ("\t Invalid choice\n"); goto loop; } /* Display the main menu and switch off the users choice */ printf ("\n"); switch (choice) { /* Test/Primenet dialog */ case 1: test_primenet (); break; /* Test/User Information dialog */ case 2: test_worker_threads (); break; /* Test/Status message */ case 3: test_status (); askOK (); break; /* Test/Continue or Stop or Test/Continue or Test/Stop */ case 4: if (WORKER_THREADS_ACTIVE && active_workers_count () < WORKER_THREADS_ACTIVE) test_continue_or_stop (); else if (NUM_WORKER_THREADS > 1 && active_workers_count () < WORKER_THREADS_ACTIVE - 1) test_continue (); else if (!WORKER_THREADS_ACTIVE || WORKER_THREADS_STOPPING) { while (WORKER_THREADS_STOPPING) Sleep (50); linuxContinue ("Another mprime is running.\n", ALL_WORKERS, FALSE); } else if (active_workers_count () > 1) test_stop (); else stop_workers_for_escape (); break; /* Test/Exit */ case 5: { int counter = 0; if (WORKER_THREADS_ACTIVE && !WORKER_THREADS_STOPPING) stop_workers_for_escape (); while (WORKER_THREADS_STOPPING) { if (counter++ % 100 == 0) printf ("Waiting for worker threads to stop.\n"); Sleep (50); } } return; /* Advanced/Test dialog */ case 6: advanced_test (); break; /* Advanced/Time dialog */ case 7: advanced_time (); break; /* Advanced/P-1 dialog */ case 8: advanced_pminus1 (); break; /* Advanced/ECM dialog */ case 9: advanced_ecm (); break; /* Advanced/Manual Communication dialog */ case 10: advanced_manualcomm (); break; /* Advanced/Unreserve exponent dialog */ case 11: advanced_unreserve (); break; /* Advanced/Quit Gimps dialog */ case 12: advanced_quit (); break; /* Options/CPU dialog */ case 13: options_cpu (); break; /* Options/Preferences dialog */ case 14: options_preferences (); break; /* Options/Torture Test */ case 15: torture (); askOK (); break; /* Options/Benchmark Test */ case 16: LaunchBench (); askOK (); break; /* Help/About */ case 17: help_about (); break; /* Help/About PrimeNet Server */ case 18: help_about_server (); break; } goto mloop; }