void klog_setup(klog_options_st *options, klog_metrics_st *metrics) { size_t nbuf = KLOG_NBUF; char *filename = NULL; log_info("Set up the %s module", KLOG_MODULE_NAME); if (klog_init) { log_warn("%s has already been setup, overwrite", KLOG_MODULE_NAME); log_destroy(&klogger); } klog_metrics = metrics; if (options != NULL) { filename = option_str(&options->klog_file); klog_backup = option_str(&options->klog_backup); if (klog_backup != NULL) { size_t nbyte = strnlen(klog_backup, PATH_MAX + 1); if (nbyte > PATH_MAX) { log_crit("klog file path too long"); goto error; } strncpy(backup_path, klog_backup, PATH_MAX); klog_backup = backup_path; } nbuf = option_uint(&options->klog_nbuf); klog_sample = option_uint(&options->klog_sample); if (klog_sample == 0) { log_crit("klog sample rate cannot be 0 - divide by zero"); goto error; } klog_max = option_uint(&options->klog_max); } if (filename == NULL) { /* no klog filename provided, do not log */ klog_enabled = false; return; } klogger = log_create(filename, nbuf); if (klogger == NULL) { log_crit("Could not create klogger!"); goto error; } klog_enabled = true; klog_init = true; return; error: log_destroy(&klogger); exit(EX_CONFIG); }
void slab_setup(slab_options_st *options, slab_metrics_st *metrics) { char *profile_str = SLAB_PROFILE; log_info("set up the %s module", SLAB_MODULE_NAME); if (slab_init) { log_warn("%s has already been set up, re-creating", SLAB_MODULE_NAME); slab_teardown(); } log_verb("Slab header size: %d, item header size: %d", SLAB_HDR_SIZE, ITEM_HDR_SIZE); slab_metrics = metrics; if (options != NULL) { slab_size = option_uint(&options->slab_size); slab_mem = option_uint(&options->slab_mem); prealloc = option_bool(&options->slab_prealloc); evict_opt = option_uint(&options->slab_evict_opt); use_freeq = option_bool(&options->slab_use_freeq); profile_str = option_str(&options->slab_profile); item_min = option_uint(&options->slab_item_min); item_max = option_uint(&options->slab_item_max); item_growth = option_fpn(&options->slab_item_growth); max_ttl = option_uint(&options->slab_item_max_ttl); use_cas = option_bool(&options->slab_use_cas); hash_power = option_uint(&options->slab_hash_power); } hash_table = hashtable_create(hash_power); if (hash_table == NULL) { log_crit("Could not create hash table"); goto error; } if (_slab_heapinfo_setup() != CC_OK) { log_crit("Could not setup slab heap info"); goto error; } if (_slab_profile_setup(profile_str) != CC_OK) { log_crit("Could not setup slab profile"); goto error; } if (_slab_slabclass_setup() != CC_OK) { log_crit("Could not setup slabclasses"); goto error; } slab_init = true; return; error: slab_teardown(); exit(EX_CONFIG); }
void core_admin_setup(admin_options_st *options) { struct tcp_conn *c; struct timeout tick; char *host = ADMIN_HOST; char *port = ADMIN_PORT; int timeout = ADMIN_TIMEOUT; int nevent = ADMIN_NEVENT; uint64_t tick_ms = ADMIN_TW_TICK; size_t cap = ADMIN_TW_CAP; size_t ntick = ADMIN_TW_NTICK; log_info("set up the %s module", ADMIN_MODULE_NAME); if (admin_init) { log_warn("admin has already been setup, re-creating"); core_admin_teardown(); } if (options != NULL) { host = option_str(&options->admin_host); port = option_str(&options->admin_port); timeout = option_uint(&options->admin_timeout); nevent = option_uint(&options->admin_nevent); tick_ms = option_uint(&options->admin_tw_tick); cap = option_uint(&options->admin_tw_cap); ntick = option_uint(&options->admin_tw_ntick); } ctx->timeout = timeout; ctx->evb = event_base_create(nevent, _admin_event); if (ctx->evb == NULL) { log_crit("failed to set up admin thread; could not create event " "base for control plane"); goto error; } hdl->accept = (channel_accept_fn)tcp_accept; hdl->reject = (channel_reject_fn)tcp_reject; hdl->open = (channel_open_fn)tcp_listen; hdl->term = (channel_term_fn)tcp_close; hdl->recv = (channel_recv_fn)tcp_recv; hdl->send = (channel_send_fn)tcp_send; hdl->rid = (channel_id_fn)tcp_read_id; hdl->wid = (channel_id_fn)tcp_write_id; admin_sock = buf_sock_borrow(); if (admin_sock == NULL) { log_crit("failed to set up admin thread; could not get buf_sock"); goto error; } admin_sock->hdl = hdl; if (CC_OK != getaddr(&admin_ai, host, port)) { log_crit("failed to resolve address for admin host & port"); goto error; } c = admin_sock->ch; if (!hdl->open(admin_ai, c)) { log_crit("admin connection setup failed"); goto error; } c->level = CHANNEL_META; event_add_read(ctx->evb, hdl->rid(c), admin_sock); timeout_set_ms(&tick, tick_ms); tw = timing_wheel_create(&tick, cap, ntick); if (tw == NULL) { log_crit("create timing wheel failed"); goto error; } timing_wheel_start(tw); admin_init = true; return; error: core_admin_teardown(); exit(EX_CONFIG); }
static void setup(void) { char *fname = NULL; uint64_t intvl; if (atexit(teardown) != 0) { log_stderr("cannot register teardown procedure with atexit()"); exit(EX_OSERR); /* only failure comes from NOMEM */ } /* Setup logging first */ log_setup(&stats.log); if (debug_setup(&setting.debug) != CC_OK) { log_stderr("debug log setup failed"); exit(EX_CONFIG); } /* setup top-level application options */ if (option_bool(&setting.ds.daemonize)) { daemonize(); } fname = option_str(&setting.ds.pid_filename); if (fname != NULL) { /* to get the correct pid, call create_pidfile after daemonize */ create_pidfile(fname); } /* setup library modules */ buf_setup(&setting.buf, &stats.buf); dbuf_setup(&setting.dbuf, &stats.dbuf); event_setup(&stats.event); sockio_setup(&setting.sockio, &stats.sockio); tcp_setup(&setting.tcp, &stats.tcp); timing_wheel_setup(&stats.timing_wheel); /* setup pelikan modules */ time_setup(&setting.time); procinfo_setup(&stats.procinfo); request_setup(&setting.request, &stats.request); response_setup(&setting.response, &stats.response); parse_setup(&stats.parse_req, NULL); compose_setup(NULL, &stats.compose_rsp); slab_setup(&setting.slab, &stats.slab); process_setup(&setting.process, &stats.process); admin_process_setup(); core_admin_setup(&setting.admin); core_server_setup(&setting.server, &stats.server); core_worker_setup(&setting.worker, &stats.worker); /* adding recurring events to maintenance/admin thread */ intvl = option_uint(&setting.ds.dlog_intvl); if (core_admin_register(intvl, debug_log_flush, NULL) == NULL) { log_stderr("Could not register timed event to flush debug log"); goto error; } return; error: if (fname != NULL) { remove_pidfile(fname); } /* since we registered teardown with atexit, it'll be called upon exit */ exit(EX_CONFIG); }
void core_server_setup(server_options_st *options, server_metrics_st *metrics) { struct tcp_conn *c; char *host = SERVER_HOST; char *port = SERVER_PORT; int timeout = SERVER_TIMEOUT; int nevent = SERVER_NEVENT; log_info("set up the %s module", SERVER_MODULE_NAME); if (server_init) { log_warn("server has already been setup, re-creating"); core_server_teardown(); } server_metrics = metrics; if (options != NULL) { host = option_str(&options->server_host); port = option_str(&options->server_port); timeout = option_uint(&options->server_timeout); nevent = option_uint(&options->server_nevent); } ctx->timeout = timeout; ctx->evb = event_base_create(nevent, _server_event); if (ctx->evb == NULL) { log_crit("failed to setup server core; could not create event_base"); goto error; } hdl->accept = (channel_accept_fn)tcp_accept; hdl->reject = (channel_reject_fn)tcp_reject; hdl->open = (channel_open_fn)tcp_listen; hdl->term = (channel_term_fn)tcp_close; hdl->recv = (channel_recv_fn)tcp_recv; hdl->send = (channel_send_fn)tcp_send; hdl->rid = (channel_id_fn)tcp_read_id; hdl->wid = (channel_id_fn)tcp_write_id; /** * Here we give server socket a buf_sock purely because it is difficult to * write code in the core event loop that would accommodate different types * of structs at the moment. However, this doesn't have to be the case in * the future. We can choose to wrap different types in a common header- * one that contains a type field and a pointer to the actual struct, or * define common fields, like how posix sockaddr structs are used. */ server_sock = buf_sock_borrow(); if (server_sock == NULL) { log_crit("failed to setup server core; could not get buf_sock"); goto error; } server_sock->hdl = hdl; if (CC_OK != getaddr(&server_ai, host, port)) { log_crit("failed to resolve address for admin host & port"); goto error; } c = server_sock->ch; if (!hdl->open(server_ai, c)) { log_crit("server connection setup failed"); goto error; } c->level = CHANNEL_META; event_add_read(ctx->evb, hdl->rid(c), server_sock); server_init = true; return; error: core_server_teardown(); exit(EX_CONFIG); }