// Writes the given number of records of random size (up to kMaxRecordSize) and // random data to the specified log. static void writer_thread(void* arg) { writer_thread_args* args = (writer_thread_args*)arg; // Maximum number of times to spin between writes. static const int MAX_SPIN_COUNT = 50; int records_written = 0; if (VERBOSE) { printf(" Writer %d starting\n", args->index); } while (records_written < args->num_records) { records_written += write_records_to_log(args->index, args->record_size, args->num_records - records_written, MAX_SPIN_COUNT); if (records_written < args->num_records) { // Ran out of log space. Sleep for a bit and let the reader catch up. // This should never happen for circular logs. if (VERBOSE) { printf( " Writer %d stalled due to out-of-space: %d out of %d " "written\n", args->index, records_written, args->num_records); } gpr_sleep_until(GRPC_TIMEOUT_MILLIS_TO_DEADLINE(10)); } } // Done. Decrement count and signal. gpr_mu_lock(args->mu); (*args->count)--; gpr_cv_signal(args->done); if (VERBOSE) { printf(" Writer %d done\n", args->index); } gpr_mu_unlock(args->mu); }
/* Writes the given number of records of random size (up to kMaxRecordSize) and random data to the specified log. */ static void writer_thread(void *arg) { writer_thread_args *args = (writer_thread_args *)arg; /* Maximum number of times to spin between writes. */ static const int32_t MAX_SPIN_COUNT = 50; int records_written = 0; printf(" Writer: %d\n", args->index); while (records_written < args->num_records) { records_written += write_records_to_log(args->index, args->record_size, args->num_records - records_written, MAX_SPIN_COUNT); if (records_written < args->num_records) { /* Ran out of log space. Sleep for a bit and let the reader catch up. This should never happen for circular logs. */ printf(" Writer stalled due to out-of-space: %d out of %d written\n", records_written, args->num_records); gpr_sleep_until(GRPC_TIMEOUT_MILLIS_TO_DEADLINE(10)); } } /* Done. Decrement count and signal. */ gpr_mu_lock(args->mu); (*args->count)--; gpr_cv_broadcast(args->done); printf(" Writer done: %d\n", args->index); gpr_mu_unlock(args->mu); }
/* Fills the log and verifies data. If 'no fragmentation' is true, records are sized such that CENSUS_LOG_2_MAX_RECORD_SIZE is a multiple of record size. If not a circular log, verifies that the number of records written match the number of records read. */ static void fill_log(size_t log_size, int no_fragmentation, int circular_log) { int size; int32_t records_written; int32_t usable_space; int32_t records_read; if (no_fragmentation) { int log2size = rand() % (CENSUS_LOG_2_MAX_RECORD_SIZE + 1); size = (1 << log2size); } else { while (1) { size = 1 + (rand() % CENSUS_LOG_MAX_RECORD_SIZE); if (CENSUS_LOG_MAX_RECORD_SIZE % size) { break; } } } printf(" Fill record size: %d\n", size); records_written = write_records_to_log( 0 /* writer id */, size, (log_size / size) * 2, 0 /* spin count */); usable_space = min_usable_space(log_size, size); GPR_ASSERT(records_written * size >= usable_space); records_read = perform_read_iteration(size); if (!circular_log) { GPR_ASSERT(records_written == records_read); } assert_log_empty(); }
// Fills the log and verifies data. If 'no fragmentation' is true, records // are sized such that CENSUS_LOG_2_MAX_RECORD_SIZE is a multiple of record // size. If not a circular log, verifies that the number of records written // match the number of records read. static void fill_log(size_t log_size, int no_fragmentation, int circular_log) { size_t size; if (no_fragmentation) { int log2size = rand() % (CENSUS_LOG_2_MAX_RECORD_SIZE + 1); size = ((size_t)1 << log2size); } else { while (1) { size = 1 + ((size_t)rand() % CENSUS_LOG_MAX_RECORD_SIZE); if (CENSUS_LOG_MAX_RECORD_SIZE % size) { break; } } } int records_written = write_records_to_log(0 /* writer id */, size, (int)((log_size / size) * 2), 0 /* spin count */); int records_read = perform_read_iteration(size); if (!circular_log) { GPR_ASSERT(records_written == records_read); } assert_log_empty(); }