END_TEST /* * odd size (10) */ START_TEST(test_ring_buffer3) { qb_ringbuffer_t *t; int32_t i; char v[] = "1234567891"; char out[32]; ssize_t l; size_t len = strlen(v) + 1; t = qb_rb_open("test3", 10, QB_RB_FLAG_CREATE | QB_RB_FLAG_OVERWRITE, 0); fail_if(t == NULL); for (i = 0; i < 9000; i++) { l = qb_rb_chunk_write(t, v, len); ck_assert_int_eq(l, len); } for (i = 0; i < 2000; i++) { l = qb_rb_chunk_read(t, (void *)out, 32, 0); if (l < 0) { /* no more to read */ break; } ck_assert_int_eq(l, len); ck_assert_str_eq(v, out); } qb_rb_close(t); }
END_TEST START_TEST(test_ring_buffer4) { qb_ringbuffer_t *t; char data[] = "1234567891"; int32_t i; char *new_data; ssize_t l; t = qb_rb_open("test4", 10, QB_RB_FLAG_CREATE | QB_RB_FLAG_OVERWRITE, 0); fail_if(t == NULL); for (i = 0; i < 2000; i++) { l = qb_rb_chunk_write(t, data, strlen(data)); ck_assert_int_eq(l, strlen(data)); if (i == 0) { data[0] = 'b'; } } for (i = 0; i < 2000; i++) { l = qb_rb_chunk_peek(t, (void **)&new_data, 0); if (l == 0) { break; } ck_assert_int_eq(l, strlen(data)); qb_rb_chunk_reclaim(t); } qb_rb_close(t); }
int32_t main(int32_t argc, char *argv[]) { const char *options = "vh"; int32_t opt; int32_t verbose = 0; while ((opt = getopt(argc, argv, options)) != -1) { switch (opt) { case 'v': verbose++; break; case 'h': default: show_usage(argv[0]); exit(0); break; } } signal(SIGINT, sigterm_handler); qb_log_init("rbwriter", LOG_USER, LOG_EMERG); qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE); qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*", LOG_INFO + verbose); qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE); rb = qb_rb_open("tester", ONE_MEG * 3, QB_RB_FLAG_SHARED_PROCESS, 0); do_throughput_benchmark(); qb_rb_close(rb); return EXIT_SUCCESS; }
static void bmc_connect(void) { rb = qb_rb_open("tester", BUFFER_CHUNK_SIZE * 3, QB_RB_FLAG_SHARED_PROCESS, 0); if (rb == NULL) { qb_perror(LOG_ERR, "failed to create ringbuffer"); exit(1); } }
static void _blackbox_reload(int32_t target) { struct qb_log_target *t = qb_log_target_get(target); if (t->instance == NULL) { return; } qb_rb_close(t->instance); t->instance = qb_rb_open(t->filename, t->size, QB_RB_FLAG_CREATE | QB_RB_FLAG_OVERWRITE, 0); }
int32_t qb_log_blackbox_open(struct qb_log_target *t) { if (t->size < 1024) { return -EINVAL; } snprintf(t->filename, PATH_MAX, "%s-%d-blackbox", t->name, getpid()); t->instance = qb_rb_open(t->filename, t->size, QB_RB_FLAG_CREATE | QB_RB_FLAG_OVERWRITE, 0); if (t->instance == NULL) { return -errno; } t->logger = NULL; t->vlogger = _blackbox_vlogger; t->reload = _blackbox_reload; t->close = _blackbox_close; return 0; }
END_TEST /* * nice size (int64) */ START_TEST(test_ring_buffer2) { qb_ringbuffer_t *t; int32_t i; int64_t v = 7891034; int64_t *new_data; ssize_t l; t = qb_rb_open("test2", 200 * sizeof(int64_t), QB_RB_FLAG_CREATE, 0); fail_if(t == NULL); for (i = 0; i < 200; i++) { l = qb_rb_chunk_write(t, &v, sizeof(v)); ck_assert_int_eq(l, sizeof(v)); } for (i = 0; i < 100; i++) { l = qb_rb_chunk_peek(t, (void **)&new_data, 0); ck_assert_int_eq(l, sizeof(v)); fail_unless(v == *new_data); qb_rb_chunk_reclaim(t); } for (i = 0; i < 100; i++) { l = qb_rb_chunk_write(t, &v, sizeof(v)); ck_assert_int_eq(l, sizeof(v)); } for (i = 0; i < 100; i++) { l = qb_rb_chunk_peek(t, (void **)&new_data, 0); if (l == 0) { /* no more to read */ break; } ck_assert_int_eq(l, sizeof(v)); fail_unless(v == *new_data); qb_rb_chunk_reclaim(t); } qb_rb_close(t); }
qb_ringbuffer_t * qb_rb_create_from_file(int32_t fd, uint32_t flags) { ssize_t n_read; size_t n_required; size_t total_read = 0; uint32_t read_pt; uint32_t write_pt; struct qb_ringbuffer_s *rb; uint32_t word_size = 0; uint32_t version = 0; uint32_t hash = 0; uint32_t calculated_hash = 0; if (fd < 0) { return NULL; } /* * 1. word size */ n_required = sizeof(uint32_t); n_read = read(fd, &word_size, n_required); if (n_read != n_required) { qb_util_perror(LOG_ERR, "Unable to read blackbox file header"); return NULL; } total_read += n_read; /* * 2. 3. read & write pointers */ n_read = read(fd, &write_pt, sizeof(uint32_t)); assert(n_read == sizeof(uint32_t)); total_read += n_read; n_read = read(fd, &read_pt, sizeof(uint32_t)); assert(n_read == sizeof(uint32_t)); total_read += n_read; /* * 4. version */ n_required = sizeof(uint32_t); n_read = read(fd, &version, n_required); if (n_read != n_required) { qb_util_perror(LOG_ERR, "Unable to read blackbox file header"); return NULL; } total_read += n_read; /* * 5. Hash */ n_required = sizeof(uint32_t); n_read = read(fd, &hash, n_required); if (n_read != n_required) { qb_util_perror(LOG_ERR, "Unable to read blackbox file header"); return NULL; } total_read += n_read; calculated_hash = word_size + write_pt + read_pt + version; if (hash != calculated_hash) { qb_util_log(LOG_ERR, "Corrupt blackbox: File header hash (%d) does not match calculated hash (%d)", hash, calculated_hash); return NULL; } else if (version != QB_RB_FILE_HEADER_VERSION) { qb_util_log(LOG_ERR, "Wrong file header version. Expected %d got %d", QB_RB_FILE_HEADER_VERSION, version); return NULL; } /* * 6. data */ n_required = (word_size * sizeof(uint32_t)); /* * qb_rb_open adds QB_RB_CHUNK_MARGIN + 1 to the requested size. */ rb = qb_rb_open("create_from_file", n_required - (QB_RB_CHUNK_MARGIN + 1), QB_RB_FLAG_CREATE | QB_RB_FLAG_NO_SEMAPHORE, 0); if (rb == NULL) { return NULL; } rb->shared_hdr->read_pt = read_pt; rb->shared_hdr->write_pt = write_pt; n_read = read(fd, rb->shared_data, n_required); if (n_read < 0) { qb_util_perror(LOG_ERR, "Unable to read blackbox file data"); goto cleanup_fail; } total_read += n_read; if (n_read != n_required) { qb_util_log(LOG_WARNING, "read %zd bytes, but expected %zu", n_read, n_required); goto cleanup_fail; } qb_util_log(LOG_DEBUG, "read total of: %zd", total_read); print_header(rb); return rb; cleanup_fail: qb_rb_close(rb); return NULL; }