void Random::initState(UInt64 seed) { tinymt64_t * tinymt = (tinymt64_t *)malloc(sizeof(tinymt64_t)); tinymt->mat1 = 0x7a840f50; tinymt->mat2 = 0xf3d8fcf6; tinymt->tmat = 0x9746beffffbffffeULL; tinymt64_init(tinymt, seed); randState = tinymt; }
int main(int argc, char * argv[]) { tinymt64_t tinymt; tinymt.mat1 = strtoul("2406486510", NULL, 16); tinymt.mat2 = strtoul("4235788063", NULL, 16); tinymt.tmat = strtoul("932445695", NULL, 16); int seed = 1; uint64_t seed_array[5]; if (argc >= 5) { seed = strtol(argv[4], NULL, 0); } printf("tinymt64 0x%08"PRIx32, tinymt.mat1); printf(" 0x%08"PRIx32, tinymt.mat2); printf(" 0x%016"PRIx64, tinymt.tmat); printf(" seed = %d\n", seed); tinymt64_init(&tinymt, seed); printf("64-bit unsigned integers r, where 0 <= r < 2^64\n"); for (int i = 0; i < 10; i++) { for (int j = 0; j < 3; j++) { printf("%20"PRIu64" ", tinymt64_generate_uint64(&tinymt)); } printf("\n"); } printf("init_by_array {%d}\n", seed); seed_array[0] = seed; tinymt64_init_by_array(&tinymt, seed_array, 1); printf("double numbers r, where 0.0 <= r < 1.0\n"); for (int i = 0; i < 12; i++) { for (int j = 0; j < 4; j++) { printf("%.15f ", tinymt64_generate_double(&tinymt)); } printf("\n"); } printf("double numbers r, where 1.0 <= r < 2.0\n"); for (int i = 0; i < 12; i++) { for (int j = 0; j < 4; j++) { printf("%.15f ", tinymt64_generate_double12(&tinymt)); } printf("\n"); } printf("double numbers r, where 0.0 < r <= 1.0\n"); for (int i = 0; i < 12; i++) { for (int j = 0; j < 4; j++) { printf("%.15f ", tinymt64_generate_doubleOC(&tinymt)); } printf("\n"); } printf("double numbers r, where 0.0 <= r < 1.0\n"); for (int i = 0; i < 12; i++) { for (int j = 0; j < 4; j++) { printf("%.15f ", tinymt64_generate_doubleOO(&tinymt)); } printf("\n"); } }
int main(int argc, char **argv) { struct addrinfo *addrs, *addr; char *host = "127.0.0.1"; char *port = "1337"; int rc; // for checking that the server's up char poke[SHA_LENGTH * 2]; tinymt64_t rando; if (parse_args(&cfg, &host, &port, argc, argv)) { usage(); exit(1); } struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM }; if ((rc = getaddrinfo(host, port, &hints, &addrs)) != 0) { const char *msg = gai_strerror(rc); fprintf(stderr, "unable to resolve %s:%s: %s\n", host, port, msg); exit(1); } for (addr = addrs; addr != NULL; addr = addr->ai_next) { int fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); if (fd == -1) continue; rc = connect(fd, addr->ai_addr, addr->ai_addrlen); tinymt64_init(&rando, time_us()); random_hash(&rando, poke); if (rc == 0 && write(fd, poke, SHA_LENGTH * 2) == SHA_LENGTH * 2) { read(fd, poke, SHA_LENGTH * 2); close(fd); break; } close(fd); } if (addr == NULL) { char *msg = strerror(errno); fprintf(stderr, "unable to connect to %s:%s: %s\n", host, port, msg); exit(1); } signal(SIGPIPE, SIG_IGN); signal(SIGINT, SIG_IGN); cfg.addr = *addr; pthread_mutex_init(&statistics.mutex, NULL); statistics.latency = stats_alloc(SAMPLES); statistics.requests = stats_alloc(SAMPLES); thread *threads = zcalloc(cfg.threads * sizeof(thread)); uint64_t connections = cfg.connections / cfg.threads; uint64_t stop_at = time_us() + (cfg.duration * 1000000); for (uint64_t i = 0; i < cfg.threads; i++) { thread *t = &threads[i]; t->connections = connections; t->stop_at = stop_at; if (pthread_create(&t->thread, NULL, &thread_main, t)) { char *msg = strerror(errno); fprintf(stderr, "unable to create thread %"PRIu64" %s\n", i, msg); exit(2); } } struct sigaction sa = { .sa_handler = handler, .sa_flags = 0, }; sigfillset(&sa.sa_mask); sigaction(SIGINT, &sa, NULL); char *time = format_time_s(cfg.duration); printf("Running %s test @ %s:%s\n", time, host, port); printf(" %"PRIu64" threads and %"PRIu64" connections\n", cfg.threads, cfg.connections); uint64_t start = time_us(); uint64_t complete = 0; uint64_t bytes = 0; errors errors = { 0 }; for (uint64_t i = 0; i < cfg.threads; i++) { thread *t = &threads[i]; pthread_join(t->thread, NULL); complete += t->complete; bytes += t->bytes; errors.connect += t->errors.connect; errors.handshake += t->errors.handshake; errors.read += t->errors.read; errors.validate += t->errors.validate; errors.write += t->errors.write; errors.timeout += t->errors.timeout; } uint64_t runtime_us = time_us() - start; long double runtime_s = runtime_us / 1000000.0; long double req_per_s = complete / runtime_s; long double bytes_per_s = bytes / runtime_s; print_stats_header(); print_stats("Latency", statistics.latency, format_time_us); print_stats("Req/Sec", statistics.requests, format_metric); if (cfg.latency) print_stats_latency(statistics.latency); char *runtime_msg = format_time_us(runtime_us); printf(" %"PRIu64" requests in %s, %sB read\n", complete, runtime_msg, format_binary(bytes)); if (errors.connect || errors.read || errors.write || errors.timeout) { printf(" Socket errors: connect %d, read %d, write %d, timeout %d\n", errors.connect, errors.read, errors.write, errors.timeout); } if (errors.handshake) { printf(" Bad handshakes from server: %d\n", errors.handshake); } if (errors.validate) { printf(" %d proofs failed verification.\n", errors.validate); } printf("Requests/sec: %9.2Lf\n", req_per_s); printf("Transfer/sec: %10sB\n", format_binary(bytes_per_s)); return 0; } void *thread_main(void *arg) { thread *thread = arg; aeEventLoop *loop = aeCreateEventLoop(10 + cfg.connections * 3); thread->cs = zmalloc(thread->connections * sizeof(connection)); thread->loop = loop; tinymt64_init(&thread->rand, time_us()); thread->latency = stats_alloc(100000); connection *c = thread->cs; for (uint64_t i = 0; i < thread->connections; i++, c++) { c->thread = thread; random_hash(&thread->rand, c->hash); connect_socket(thread, c); } aeCreateTimeEvent(loop, CALIBRATE_DELAY_MS, calibrate, thread, NULL); aeCreateTimeEvent(loop, TIMEOUT_INTERVAL_MS, check_timeouts, thread, NULL); thread->start = time_us(); aeMain(loop); aeDeleteEventLoop(loop); zfree(thread->cs); uint64_t max = thread->latency->max; stats_free(thread->latency); pthread_mutex_lock(&statistics.mutex); for (uint64_t i = 0; i < thread->missed; i++) { stats_record(statistics.latency, max); } pthread_mutex_unlock(&statistics.mutex); return NULL; }
/* seed random number generator */ void MVM_proc_seed(MVMThreadContext *tc, MVMint64 seed) { /* Seed our one, plus the normal C srand for libtommath. */ tinymt64_init(tc->rand_state, (MVMuint64)seed); srand((MVMuint32)seed); }