static int io_write_impl(struct io_user_data *ud) { struct nb_worker *worker = ud->worker; if (nb.is_done) return 1; /** * The request can be unset if it is first call of io_write * or if all requests already sent and need to rewind list of * requests. */ if (ud->request == NULL) { nb_workload_reset(&worker->workload); ud->request = nb_workload_fetch(&worker->workload); if (ud->request == NULL) return 1; } else { /* If the previous request deleted a tuple then reinsert it. */ if (ud->prev_type == NB_DELETE) { nb.db->replace(&worker->db, &worker->keyv); worker->workload.requested++; ud->prev_type = NB_INSERT; nb_history_add(&worker->history, RT_WRITE); return 0; } } worker->key->generate(&worker->keyv, worker->workload.count); ud->request->_do(&worker->db, &worker->keyv); ud->request->requested++; worker->workload.requested++; nb_history_add(&worker->history, ud->request->type == NB_SELECT ? RT_READ : RT_WRITE); ud->prev_type = ud->request->type; ud->request = nb_workload_fetch(&worker->workload); return 0; }
static void *nb_worker(void *ptr) { struct nb_worker *worker = ptr; nb_worker_init(); nb.db->init(&worker->db, nb.opts.value_size); if (nb.db->connect(&worker->db, &nb.opts) == -1) return NULL; struct io_user_data userdata; userdata.worker = worker; userdata.request = NULL; userdata.prev_type = NB_INSERT; if (nb.opts.request_batch_count) { int rc = 0; do { int i = 0; for (; i < nb.opts.request_batch_count; ++i) { rc = io_write_impl(&userdata); if (rc) break; } worker->db.dif->recv(&worker->db, i, NULL, process_latency, &userdata); nb_history_add(&worker->history, RT_MISS); nb_history_avg(&worker->history); pthread_mutex_lock(&nb.stats.lock_stats); nb_statistics_set(&nb.stats, worker->id, &worker->history.Savg); pthread_mutex_unlock(&nb.stats.lock_stats); } while (!rc); } else { struct async_io_if io_if = {io_msg_len, io_write, io_recv_from_buf}; struct async_io *io_object; int sock = nb.db->get_fd(&worker->db); if (nb.opts.rps != 0) { io_object = async_io_new_rps(sock, &io_if, nb.opts.rps, &userdata); } else { io_object = async_io_new(nb.db->get_fd(&worker->db), &io_if, &userdata); } if (io_object == NULL) goto error; async_io_start(io_object); async_io_delete(io_object); } error: return NULL; }
int main(void) { char *line; int ret; nb_history_init(4); nb_history_load("history.txt"); while ((ret = nb_get_line("hello> ", &line)) != NB_EXIT) { if (ret == NB_ERROR) dprintf(2, "%s\n", nb_get_error()); else if (ret != NB_INTERRUPT && line[0] != '\0' && line[0] != '/') { printf("echo: '%s'\n", line); nb_history_add(line); } free(line); } nb_history_save("history.txt"); nb_history_free(); return (0); }