static void check_bitwise_copy(void) { unsigned int n_loops; int src_ofs; int dst_ofs; int n_bits; n_loops = 0; for (n_bits = 0; n_bits <= 64; n_bits++) { for (src_ofs = 0; src_ofs < 64 - n_bits; src_ofs++) { for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) { ovs_be64 src = htonll(random_uint64()); ovs_be64 dst = htonll(random_uint64()); ovs_be64 orig_dst = dst; ovs_be64 expect; if (n_bits == 64) { expect = dst; } else { uint64_t mask = (UINT64_C(1) << n_bits) - 1; expect = orig_dst & ~htonll(mask << dst_ofs); expect |= htonll(((ntohll(src) >> src_ofs) & mask) << dst_ofs); } bitwise_copy(&src, sizeof src, src_ofs, &dst, sizeof dst, dst_ofs, n_bits); if (expect != dst) { fprintf(stderr,"copy_bits(0x%016"PRIx64",8,%d, " "0x%016"PRIx64",8,%d, %d) yielded 0x%016"PRIx64" " "instead of the expected 0x%016"PRIx64"\n", ntohll(src), src_ofs, ntohll(orig_dst), dst_ofs, n_bits, ntohll(dst), ntohll(expect)); abort(); } n_loops++; } } } if (n_loops != sum_of_squares(64)) { abort(); } }
static double random_double(void) { // random between 0.0 - 1.0 const double r = (double)random_uint64(); const double rd = r / RAND64_MAX_D; return rd; }
static inline ssize_t do_random_rw(const int fd, const size_t mask, uint8_t * const buf, const bool is_write, const uint64_t cap) { const size_t size = (mask + 1u); const off_t off = (random_uint64() % (cap - size)) & (~mask); const uint64_t t0 = debug_time_usec(); const ssize_t r = is_write?pwrite(fd, buf, size, off):pread(fd, buf, size, off); assert((typeof(size))r == size); const uint64_t dt = debug_diff_usec(t0); latency_record(dt, latency); return r; }
static void check_bitwise_one(void) { unsigned int n_loops; int dst_ofs; int n_bits; n_loops = 0; for (n_bits = 0; n_bits <= 64; n_bits++) { for (dst_ofs = 0; dst_ofs < 64 - n_bits; dst_ofs++) { ovs_be64 dst = htonll(random_uint64()); ovs_be64 orig_dst = dst; ovs_be64 expect; if (n_bits == 64) { expect = htonll(UINT64_MAX); } else { uint64_t mask = (UINT64_C(1) << n_bits) - 1; expect = orig_dst | htonll(mask << dst_ofs); } bitwise_one(&dst, sizeof dst, dst_ofs, n_bits); if (expect != dst) { fprintf(stderr,"bitwise_one(0x%016"PRIx64",8,%d, %d) " "yielded 0x%016"PRIx64" " "instead of the expected 0x%016"PRIx64"\n", ntohll(orig_dst), dst_ofs, n_bits, ntohll(dst), ntohll(expect)); abort(); } n_loops++; } } if (n_loops != 64 * (64 + 1) / 2) { abort(); } }
rcl_status init_state(const char* owner, uint32_t length, uint64_t verifier, state_t* state_id) { uint64_t id; struct client_state* state; rcl_status error = RCL_OK; char owner_buffer[256]; memset(owner_buffer, 0, 256); strncpy(owner_buffer, owner, min(255, length)); log_print(log_notice, "Initializing new state:\n\towner: %s\n\t" \ "verifier: %#" PRIx64 "", owner_buffer, verifier); state = malloc(sizeof(struct client_state)); if (!state) return RCL_HOST_RESOURCE; state->owner = malloc(length); if (!state->owner) { error = RCL_HOST_RESOURCE; goto out_alloc; } memcpy(state->owner, owner, length); state->owner_length = length; state->verifier = verifier; state->cluster_state = cluster_state; state->last_sequence = 0; error = cl_init_state(state); if (error) goto out_alloc; pthread_mutex_init(&state->lock, NULL); vector_init(&state->buffers, sizeof(struct buffer_state*)); vector_init(&state->programs, sizeof(struct program_state*)); vector_init(&state->kernels, sizeof(struct kernel_state*)); pthread_mutex_lock(&states_lock); do { id = random_uint64(); } while (bst_insert(&states_tree, &state->node, id)); state->state_id = id; state->expire_epoch = current_epoch; list_add(renewed_states, &state->expire); pthread_mutex_unlock(&states_lock); *state_id = id; error = relay_init_state(state, owner, length, verifier); if (error) quit_state(id, 1); return error; out_alloc: if (state) free(state->owner); free(state); return error; }