Esempio n. 1
0
void initTimeEvent(struct aeEventLoop* eventLoop) {
    struct time_ev_priv_data* background_time_ev_data = zmalloc(sizeof(struct time_ev_priv_data));
    if (NULL == background_time_ev_data)
    {
        ERRLOG("memory is not enough.");
        exit(1);
    }
    background_time_ev_data->id = 0;
    if (aeCreateTimeEvent(eventLoop, 1, tBackgroundTask, background_time_ev_data, tEventFinalizerProc) == AE_ERR) {
        ERRLOG("Can't create the tBackgroundTask time event.");
        exit(1);
    }

    struct time_ev_priv_data* echo_task_data = zmalloc(sizeof(struct time_ev_priv_data));
    if (NULL == echo_task_data)
    {
        ERRLOG("memory is not enough.");
        exit(1);
    }
    echo_task_data->id = 1;
    if (aeCreateTimeEvent(eventLoop, 1, tEchoTask, echo_task_data, tEventFinalizerProc) == AE_ERR) {
        ERRLOG("Can't create the tEchoTask time event.");
        exit(1);
    }
}
Esempio n. 2
0
void closeFd(int fd, aeEventLoop* el) {
	aeDeleteTimeEvent(el, time_event_id);
	if(client_fd != -1){
		aeDeleteFileEvent(el, client_fd, AE_READABLE);
		close(client_fd);
		client_fd = -1;
	}
	if(read_fd != -1){
		aeDeleteFileEvent(el, read_fd, AE_READABLE);
		close(read_fd);
		read_fd = -1;
	}
	if(master_fd != -1){
		aeDeleteFileEvent(el, master_fd, AE_READABLE);
		close(master_fd);
		master_fd = -1;
	}
	if (listen_fd != -1) {
		aeDeleteFileEvent(el, listen_fd, AE_READABLE);
		close(listen_fd);
		listen_fd = -1;
	}
	state = START_STATE;
	buf_master_cmd.len = 0;
	buf_slave_answer.len = 0;
	clear_queue(&q);
	if((time_event_id = aeCreateTimeEvent(el, 1,timeEvent , 0, 0)) == AE_ERR)
	{
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "Unrecoverable error creating time event errno = %d at line %d in file %s",errno,  __LINE__, __FILE__);
	}
}
Esempio n. 3
0
int create_tracker_service()
{
	if((g_tracker_service.sockfd = sock_server(g_tracker_service.server_port)) < 0)
		return -1;
	
	if (tcpsetserveropt(g_tracker_service.sockfd, g_tracker_service.network_timeout) != 0)
	{
		return -2;
	}
	
	if((g_tracker_service.eventLoop = (aeEventLoop*)aeCreateEventLoop()) == NULL)
	{
		logError(	"file :"__FILE__",line :%d"\
			"create_tracker_service CreateEventLoop failed.");
			return -3;
	}

	aeCreateTimeEvent(g_tracker_service.eventLoop,g_tracker_service.check_storage_pool_millisec,check_storage_pool,NULL,NULL);
	
	if(aeCreateFileEvent(g_tracker_service.eventLoop, g_tracker_service.sockfd, AE_READABLE,accept_tcp,NULL) != AE_OK)
	{
		logError(	"file :"__FILE__",line :%d"\
			"create_tracker_service CreateFileEvent failed.");
			return -4;
	}
	return 0;
}
Esempio n. 4
0
int main()
{

	printf("Start\n");

	signal(SIGINT, StopServer);

	//初始化网络事件循环
	g_event_loop = aeCreateEventLoop(1024*10);

	//设置监听事件
	int fd = anetTcpServer(g_err_string, PORT, NULL);
	if( ANET_ERR == fd )
		fprintf(stderr, "Open port %d error: %s\n", PORT, g_err_string);
	if( aeCreateFileEvent(g_event_loop, fd, AE_READABLE, AcceptTcpHandler, NULL) == AE_ERR )
		fprintf(stderr, "Unrecoverable error creating server.ipfd file event.");

	//设置定时事件
	aeCreateTimeEvent(g_event_loop, 1, PrintTimer, NULL, NULL);

	//开启事件循环
	aeMain(g_event_loop);

	//删除事件循环
	aeDeleteEventLoop(g_event_loop);

	printf("End\n");
	return 0;
}
Esempio n. 5
0
int create_storage_service()
{
	if((g_storage_service.sockfd = sock_server(g_storage_service.heart_beat.port)) < 0)
		return -1;
	
	if (tcpsetserveropt(g_storage_service.sockfd, g_storage_service.network_timeout) != 0)
	{
		return -2;
	}
	
	if((g_storage_service.eventLoop = (aeEventLoop*)aeCreateEventLoop()) == NULL)
	{
		logError(	"file :"__FILE__",line :%d"\
			"create_storage_service CreateEventLoop failed.");
		exit(1);
	}

	aeCreateTimeEvent(g_storage_service.eventLoop,g_storage_service.heart_beat_millisec,heart_beat_init,g_storage_service.nio_pool.nio_node,NULL);

	if(aeCreateFileEvent(g_storage_service.eventLoop, g_storage_service.sockfd, AE_READABLE,accept_tcp,NULL) != AE_OK)
	{
		logError(	"file :"__FILE__",line :%d"\
			"create_storage_service CreateFileEvent failed.");
		exit(1);
	}
	return 0;
}
Esempio n. 6
0
void initServer(){

    server.mainthread = pthread_self();
    server.clients = listCreate();
    server.el = aeCreateEventLoop();

    if (server.port != 0) {
        server.ipfd = anetTcpServer(server.neterr,server.port,server.bindaddr);
        if (server.ipfd == ANET_ERR) {
            redisLog(REDIS_WARNING, "Opening port %d: %s",
                server.port, server.neterr);
            exit(1);
        }
    }
//    if (server.unixsocket != NULL) {
//        unlink(server.unixsocket); /* don't care if this fails */
//        server.sofd = anetUnixServer(server.neterr,server.unixsocket,server.unixsocketperm);
//        if (server.sofd == ANET_ERR) {
//            redisLog(REDIS_WARNING, "Opening socket: %s", server.neterr);
//            exit(1);
//        }
//    }
    if (server.ipfd < 0 && server.sofd < 0) {
        redisLog(REDIS_WARNING, "Configured to not listen anywhere, exiting.");
        exit(1);
    }

    aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL);
    if (server.ipfd > 0 && aeCreateFileEvent(server.el,server.ipfd,AE_READABLE,
        acceptTcpHandler,NULL) == AE_ERR) oom("creating file event");
//    if (server.sofd > 0 && aeCreateFileEvent(server.el,server.sofd,AE_READABLE,
//        acceptUnixHandler,NULL) == AE_ERR) oom("creating file event");
}
Esempio n. 7
0
static command_manager *
cmd_mgr_create (aeEventLoop * el, redis_pool * pool, cluster_conf * conf,
		async_chan * my_async, array_async_chans * worker_asyncs,
		sbuf_hdr * shared_stream, int type)
{
  command_manager *mgr;

  mgr = zmalloc (sizeof (command_manager));
  mgr->el = el;
  mgr->cron_teid = aeCreateTimeEvent (el, 1000, cmd_cron, mgr, NULL);
  mgr->hz = GW_DEFAULT_HZ;
  mgr->cronloops = 0;
  mgr->pool = pool;
  mgr->commands = dictCreate (&commandTableDictType, NULL);
  populate_command_table (mgr->commands, type);
  mgr->conf = conf;
  mgr->my_async = my_async;
  mgr->worker_asyncs = worker_asyncs;
  mgr->shared_stream = shared_stream;
  mgr->idx_helper = index_helper_create (INDEX_HELPER_INIT_SIZE);
  memset (&mgr->stat, 0, sizeof (mgr->stat));

  // Memory pool
  mgr->mp_cmdctx =
    mempool_create (sizeof (struct command_context),
		    MEMPOOL_DEFAULT_POOL_SIZE);

  return mgr;
}
Esempio n. 8
0
int main()
{
    int ret = 0;
    server_fd = aeCreateServer(SERVERADDR, SERVERPORT);
    if(server_fd < 0) goto err;
    
    client_fd = aeConnectServer(SERVERADDR, SERVERPORT);
    if(client_fd < 0) goto err;

    loop = aeCreateEventLoop(10240);
    
    //create server thread
    pthread_t pid1;
    if(pthread_create(&pid1,NULL, server_worker,NULL) != 0){
        fprintf(stderr,"pthread create failed\n");
        goto err;
    }
    
    //create client thread
    pthread_t pid2;
    if(pthread_create(&pid2,NULL, client_worker,NULL) != 0){
        fprintf(stderr,"pthread create failed\n");
        goto err;
    }

    //add file event
    ret = aeCreateFileEvent(loop, server_fd, AE_READABLE, aeFileReadWrite, NULL);
    if(ret == AE_ERR){
        fprintf(stderr,"aeCreateFileEvent failed\n");
        goto err;
    }

    //add time event
    aeCreateTimeEvent(loop, 1000, aeTimeCallback1, NULL);
    aeCreateTimeEvent(loop, 2000, aeTimeCallback2, NULL);
    aeCreateTimeEvent(loop, 3000, aeTimeCallback3, NULL);

    //wait thread exit
    pthread_join(pid1,NULL);
    pthread_join(pid2,NULL);

err:
    aeDeleteEventLoop(loop);
    aeClose(server_fd);
    aeClose(client_fd);
    return 0;
}
Esempio n. 9
0
void DXDB_initServer() {                   //printf("DXDB_initServer\n");
    server.alc.RestClient         = createClient(-1);
    server.alc.RestClient->flags |= REDIS_LUA_CLIENT;
    aeCreateTimeEvent(server.el, 1, luaCronTimeProc, NULL, NULL);
    initX_DB_Range(); initAccessCommands(); init_six_bit_strings();
    init_DXDB_PersistentStorageItems(INIT_MAX_NUM_TABLES, INIT_MAX_NUM_INDICES);
    initServer_Extra();
}
Esempio n. 10
0
static int watch_ipcam_link_clear(struct aeEventLoop *loop, long long id, void *clientData)
{
	debug_print("clear");
	pthread_mutex_lock(&IPCAM_DEV_MUTEX);
	clear_all_dev_online(IPCAM_DEV);
	pthread_mutex_unlock(&IPCAM_DEV_MUTEX);
	aeCreateTimeEvent(loop, CHECK_IPCAM_CYCLE * 1000, watch_ipcam_link_test, NULL, NULL);
	return -1;
}
Esempio n. 11
0
int main(int argc, char **argv)
{
	int ret;
#if _LINUX_
	aeEventLoop *loop;
#endif

	SSRC = getpid();
	IPCAM_DEV = create_empty_ipcam_link();

#if !_LINUX_
	pthread_t deal_msg_pid;
	pthread_t deal_console_input_pid;
	pthread_t maintain_ipcam_link_pid;

	ret = pthread_create(&deal_console_input_pid, 0, 
				deal_console_input, NULL);
	if (ret) {
		debug_print("pthread_create failed");
		exit(errno);
	}

	ret = pthread_create(&deal_msg_pid, 0, 
				recv_msg_from_ipcam, NULL);
	if (ret) {
		debug_print("pthread_create failed");
		exit(errno);
	}

	ret = pthread_create(&maintain_ipcam_link_pid, 0, 
				maintain_ipcam_link, NULL);
	if (ret) {
		debug_print("pthread_create failed");
		exit(errno);
	}

	pthread_join(maintain_ipcam_link_pid, NULL);
	pthread_join(deal_msg_pid, NULL);
	pthread_join(deal_console_input_pid, NULL);
#else
	int pc_server_fd;

	pc_server_fd = init_server_UDP_fd(PC_SERVER_PORT, "0.0.0.0");
	assert(pc_server_fd > 0);
	loop = aeCreateEventLoop();
	fprintf(stdout, "\033[01;32mipc_shell> \033[0m");
	fflush(stdout);
	ret = aeCreateFileEvent(loop, STDIN_FILENO, AE_READABLE, dealcmd, NULL);
	assert(ret != AE_ERR);
	ret = aeCreateFileEvent(loop, pc_server_fd, AE_READABLE, dealnet, NULL);
	assert(ret != AE_ERR);
	aeCreateTimeEvent(loop, CHECK_IPCAM_CYCLE * 1000, watch_ipcam_link_clear, NULL, NULL);
	aeMain(loop);
#endif
	return 0;
}
Esempio n. 12
0
int main(int argc, char** argv)
{
	aeEventLoop *el = aeCreateEventLoop(100);
	if(aeCreateTimeEvent(el, 1000, timerEvent, NULL, NULL) == AE_ERR) {
		printf("Can't create the serverCron time event.");
		return 1;
	}
	aeMain(el);
	aeDeleteEventLoop(el);
}
Esempio n. 13
0
static void retry_connect()
{
    data_stream = NULL;

    /* retry connect in 10 seconds */
    if (aeCreateTimeEvent(el, 10000, dispatch_connect, NULL, NULL) == AE_ERR) {
        Log(WARNING, "failed to create data server connect event");
        return;
    }
}
Esempio n. 14
0
int data_connect(const char *ip)
{
    strcpy(host, ip);

    if (aeCreateTimeEvent(el, 0, dispatch_connect, NULL, NULL) == AE_ERR) {
        Log(WARNING, "failed to create data connect event");
        return -1;
    }

    return 0;
}
Esempio n. 15
0
int start_tubii_readout(long long milliseconds)
{
    if (tubii_readout_id != AE_ERR) {
        sprintf(tubii_err, "TUBii: readout already running!");
        return -1;
    }
    //if(getDataReadout() == 0) return 0;

    // set up read out event
    if ((tubii_readout_id = aeCreateTimeEvent(el, milliseconds, tubii_readout, NULL, NULL)) == AE_ERR) {
        sprintf(tubii_err, "failed to set up tubii readout");
        return -1;
    }

    return 0;
}
Esempio n. 16
0
static int calibrate(aeEventLoop *loop, long long id, void *data) {
    thread *thread = data;

    uint64_t elapsed_ms = (time_us() - thread->start) / 1000;
    uint64_t req_per_ms = thread->requests / elapsed_ms;

    if (!req_per_ms) return CALIBRATE_DELAY_MS / 2;

    thread->rate  = (req_per_ms * SAMPLE_INTERVAL_MS) / 10;
    thread->start = time_us();
    thread->requests = 0;
    stats_reset(thread->latency);

    aeCreateTimeEvent(loop, SAMPLE_INTERVAL_MS, sample_rate, thread, NULL);

    return AE_NOMORE;
}
Esempio n. 17
0
int main(int argc, char **argv){
	setbuf(stdout,NULL);
	printf("Start\n");

	signal(SIGINT, StopServer);

	//初始化网络事件循环
	g_event_loop = aeCreateEventLoop(1024*10);
	
	long int start_time = getCurrentTime();
	/*//设置监听事件
	int i;
	for(i=0; i<10; i++){
		int fd = anetTcpConnect(NULL, argv[1], atoi(argv[2]));
		if( ANET_ERR == fd ){
			fprintf(stderr, "connect error: %s\n", PORT, NULL);
		}
		if(anetNonBlock(NULL, fd) == ANET_ERR){
			fprintf(stderr, "set nonblock error: %d\n", fd);
		}
		
		
		WriteHandler(g_event_loop, fd, NULL, 0);
	}*/

	
	//设置定时事件
	aeCreateTimeEvent(g_event_loop, 5000, PrintTimer, NULL, NULL);

	//开启事件循环
	aeMain(g_event_loop);

	//删除事件循环
	aeDeleteEventLoop(g_event_loop);

	/*printf("End\n");
	long int end_time = getCurrentTime();
	double elapsed = (double)(end_time-start_time);
	printf("test_time : %0.2f s\n", elapsed/1000);
	printf("total_req : %d\n", total_req);
	printf("total_res : %d\n", total_res);*/
	return 0;
}
Esempio n. 18
0
static void *
worker_thread (void *arg)
{
  workerArg *wa = (workerArg *) arg;
  int ret;
  int flags;
  workerState ws;

  // set read flag to be nonblock 
  flags = fcntl (wa->rfd, F_GETFL, 0);
  assert (flags != -1);
  ret = fcntl (wa->rfd, F_SETFL, flags | O_NONBLOCK);
  assert (ret != -1);

  init_worker_state (&ws);
  ws.arg = wa;
  ws.size = wa->size;
  ws.tps = wa->tps;
  ws.el = aeCreateEventLoop (2048);
  assert (ws.el != NULL);

  ret =
    aeCreateFileEvent (ws.el, ws.arg->rfd, AE_READABLE, command_handler, &ws);
  assert (ret != AE_ERR);

  ws.teid = aeCreateTimeEvent (ws.el, 1, worker_cron, &ws, NULL);
  assert (ws.teid != -1LL);

  aeMain (ws.el);

  sb_clear (&ws.cin);
  sb_clear (&ws.in);
  sb_clear (&ws.out);
  if (ws.teid != -1LL)
    {
      aeDeleteTimeEvent (ws.el, ws.teid);
    }
  if (ws.el != NULL)
    {
      aeDeleteEventLoop (ws.el);
    }
  return NULL;
}
Esempio n. 19
0
int main(int argc, char **argv)
{
	(void) argc;
	(void) argv;

	_svr.bindaddr = HOST;
	_svr.port = PORT;

	_svr.db = silokatana_open();
	_svr.el = aeCreateEventLoop(11024);
	_svr.fd = anetTcpServer(_svr.neterr, _svr.port, _svr.bindaddr);
	if (_svr.fd == ANET_ERR) {
		__PANIC("openning port error #%d:%s", _svr.port, _svr.neterr);
		exit(1);
	}

	if (anetNonBlock(_svr.neterr, _svr.fd) == ANET_ERR) {
		__ERROR("set nonblock #%s",_svr.neterr);
		exit(1);
	}

	aeCreateTimeEvent(_svr.el, 3000, server_cron, NULL, NULL);

	if (aeCreateFileEvent(_svr.el, _svr.fd, AE_READABLE, accept_handler, NULL) == AE_ERR) 
		__ERROR("creating file event");

	__INFO("siloserver starting, port:%d, pid:%d", PORT, (long)getpid());
	printf("%s", _ascii_logo);

	aeMain(_svr.el);

	__ERROR("oops,exit");
	aeDeleteEventLoop(_svr.el);
	silokatana_close(_svr.db);

	return 1;
}
int main(int argc, const char **argv) {
    int i;
    char *data, *cmd;
    int len;

    client c;

    signal(SIGHUP, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);

    config.numclients = 50;
    config.requests = 10000;
    config.liveclients = 0;
    config.el = aeCreateEventLoop(1024*10);
    aeCreateTimeEvent(config.el,1,showThroughput,NULL,NULL);
    config.keepalive = 1;
    config.datasize = 3;
    config.pipeline = 1;
    config.randomkeys = 0;
    config.randomkeys_keyspacelen = 0;
    config.quiet = 0;
    config.csv = 0;
    config.loop = 0;
    config.idlemode = 0;
    config.latency = NULL;
    config.clients = listCreate();
    config.hostip = "127.0.0.1";
    config.hostport = 6379;
    config.hostsocket = NULL;
    config.tests = NULL;
    config.dbnum = 0;

    i = parseOptions(argc,argv);
    argc -= i;
    argv += i;

//    printf ("SET PAGE POOL SIZE\n");
//    set_page_pool_size(config.requests);
    config.latency = zmalloc(sizeof(long long)*config.requests);

    if (config.keepalive == 0) {
        printf("WARNING: keepalive disabled, you probably need 'echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse' for Linux and 'sudo sysctl -w net.inet.tcp.msl=1000' for Mac OS X in order to use a lot of clients/requests\n");
    }

    if (config.idlemode) {
        printf("Creating %d idle connections and waiting forever (Ctrl+C when done)\n", config.numclients);
        c = createClient("",0,NULL); /* will never receive a reply */
        createMissingClients(c);
        aeMain(config.el);
        /* and will wait for every */
    }

    /* Run benchmark with command in the remainder of the arguments. */
    if (argc) {
        sds title = sdsnew(argv[0]);
        for (i = 1; i < argc; i++) {
            title = sdscatlen(title, " ", 1);
            title = sdscatlen(title, (char*)argv[i], strlen(argv[i]));
        }

        do {
            len = redisFormatCommandArgv(&cmd,argc,argv,NULL);
            benchmark(title,cmd,len);
            free(cmd);
        } while(config.loop);

        return 0;
    }

    /* Run default benchmark suite. */
    do {
        data = zmalloc(config.datasize+1);
        memset(data,'x',config.datasize);
        data[config.datasize] = '\0';

        if (test_is_selected("ping_inline") || test_is_selected("ping"))
            benchmark("PING_INLINE","PING\r\n",6);

        if (test_is_selected("ping_mbulk") || test_is_selected("ping")) {
            len = redisFormatCommand(&cmd,"PING");
            benchmark("PING_BULK",cmd,len);
            free(cmd);
        }

        if (test_is_selected("set")) {
            len = redisFormatCommand(&cmd,"SET key:__rand_int__ %s",data);
            benchmark("SET",cmd,len);
            free(cmd);
        }

        if (test_is_selected("get")) {
            len = redisFormatCommand(&cmd,"GET key:__rand_int__");
            benchmark("GET",cmd,len);
            free(cmd);
        }

        if (test_is_selected("incr")) {
            len = redisFormatCommand(&cmd,"INCR counter:__rand_int__");
            benchmark("INCR",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lpush")) {
            len = redisFormatCommand(&cmd,"LPUSH mylist %s",data);
            benchmark("LPUSH",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lpop")) {
            len = redisFormatCommand(&cmd,"LPOP mylist");
            benchmark("LPOP",cmd,len);
            free(cmd);
        }

        if (test_is_selected("sadd")) {
            len = redisFormatCommand(&cmd,
                "SADD myset element:__rand_int__");
            benchmark("SADD",cmd,len);
            free(cmd);
        }

        if (test_is_selected("spop")) {
            len = redisFormatCommand(&cmd,"SPOP myset");
            benchmark("SPOP",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lrange") ||
            test_is_selected("lrange_100") ||
            test_is_selected("lrange_300") ||
            test_is_selected("lrange_500") ||
            test_is_selected("lrange_600"))
        {
            len = redisFormatCommand(&cmd,"LPUSH mylist %s",data);
            benchmark("LPUSH (needed to benchmark LRANGE)",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lrange") || test_is_selected("lrange_100")) {
            len = redisFormatCommand(&cmd,"LRANGE mylist 0 99");
            benchmark("LRANGE_100 (first 100 elements)",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lrange") || test_is_selected("lrange_300")) {
            len = redisFormatCommand(&cmd,"LRANGE mylist 0 299");
            benchmark("LRANGE_300 (first 300 elements)",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lrange") || test_is_selected("lrange_500")) {
            len = redisFormatCommand(&cmd,"LRANGE mylist 0 449");
            benchmark("LRANGE_500 (first 450 elements)",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lrange") || test_is_selected("lrange_600")) {
            len = redisFormatCommand(&cmd,"LRANGE mylist 0 599");
            benchmark("LRANGE_600 (first 600 elements)",cmd,len);
            free(cmd);
        }

        if (test_is_selected("mset")) {
            const char *argv[21];
            argv[0] = "MSET";
            for (i = 1; i < 21; i += 2) {
                argv[i] = "key:__rand_int__";
                argv[i+1] = data;
            }
            len = redisFormatCommandArgv(&cmd,21,argv,NULL);
            benchmark("MSET (10 keys)",cmd,len);
            free(cmd);
        }

        if (!config.csv) printf("\n");
    } while(config.loop);

    return 0;
}
Esempio n. 21
0
static int dispatch_data_thread_init(dispatch_data_thread *ddt, char *test_target_groups, int connections)
{
    int i, j, k;

    ddt->id = 0;
    ddt->thread_id = 0;
    ddt->el = NULL;
    ddt->hz = 10;
    ddt->cronloops = 0;
    ddt->datas = NULL;
    ddt->rdatas = NULL;
    ddt->abgs = NULL;
    ddt->pause = 0;
    ddt->count_wait_for_reply = 0;
    ddt->reply_total_count_per_cycle = 0;
    ddt->reply_type_err_count_per_cycle = 0;

    ddt->el = aeCreateEventLoop(200);
    if (ddt->el == NULL) {
        return VRT_ERROR;
    }

    ddt->datas = dmtqueue_create();
    if (ddt->datas == NULL) {
        return VRT_ERROR;
    }

    if (dmtqueue_init_with_lockqueue(ddt->datas, NULL) != 0) {
        return VRT_ERROR;
    }

    ddt->rdatas = dlistCreate();
    if (ddt->rdatas == NULL) {
        return VRT_ERROR;
    }
    
    ddt->abgs = abtest_groups_create(test_target_groups);
    if (ddt->abgs == NULL) {
        return VRT_ERROR;
    }

    /* Init connection context for each server */
    for (i = 0; i < darray_n(ddt->abgs); i ++) {
        abtest_group *abg = darray_get(ddt->abgs, i);
        for (j = 0; j < darray_n(&abg->abtest_servers); j ++) {
            abtest_server *abs = darray_get(&abg->abtest_servers, j);
            abs->conn_contexts = darray_create(connections, sizeof(conn_context));
            for (k = 0; k < connections; k ++) {
                conn_context *cc = darray_push(abs->conn_contexts);
                if (dispatch_conn_context_init(cc,abs->host,abs->port) != VRT_OK) {
                    return VRT_ERROR;
                }
                cc->actx->data = ddt;
                redisAeAttach(ddt->el, cc->actx);
                redisAsyncSetConnectCallback(cc->actx,connect_callback);
                redisAsyncSetDisconnectCallback(cc->actx,disconnect_callback);
            }
        }
    }

    if (aeCreateTimeEvent(ddt->el, 1, dispatch_data_thread_cron, ddt, NULL) == AE_ERR) {
        return VRT_ERROR;
    }
    
    return VRT_OK;
}
Esempio n. 22
0
static void
createRedisSocket ()
{
  int j;

  if (server.port != 0 &&
      listenToPort (server.port, server.ipfd,
		    &server.ipfd_count) == REDIS_ERR)
    {
      redisLog (REDIS_WARNING, "Opening port %d: %s", server.port,
		server.neterr);
      exit (1);
    }
  if (server.unixsocket != NULL)
    {
      unlink (server.unixsocket);	/* don't care if this fails */
      server.sofd = anetUnixServer (server.neterr, server.unixsocket,
				    server.unixsocketperm,
				    server.tcp_backlog);
      if (server.sofd == ANET_ERR)
	{
	  redisLog (REDIS_WARNING, "Opening socket: %s", server.neterr);
	  exit (1);
	}
    }
  if (server.ipfd_count == 0 && server.sofd < 0)
    {
      redisLog (REDIS_WARNING, "Configured to not listen anywhere, exiting.");
      exit (1);
    }

  if (aeCreateTimeEvent (server.el, 1, serverCron, NULL, NULL) == AE_ERR)
    {
      redisPanic ("Can't create the serverCron time event.");
      exit (1);
    }

  /* Create an event handler for accepting new connections in TCP and Unix
   * domain sockets. */
  for (j = 0; j < server.ipfd_count; j++)
    {
      if (aeCreateFileEvent (server.el, server.ipfd[j], AE_READABLE,
			     acceptTcpHandler, NULL) == AE_ERR)
	{
	  redisPanic ("Unrecoverable error creating server.ipfd file event.");
	}
    }

  if (server.sofd > 0
      && aeCreateFileEvent (server.el, server.sofd, AE_READABLE,
			    acceptUnixHandler, NULL) == AE_ERR)
    redisPanic ("Unrecoverable error creating server.sofd file event.");

  if (server.aof_state == REDIS_AOF_ON)
    {
      server.aof_fd = open (server.aof_filename,
			    O_WRONLY | O_APPEND | O_CREAT, 0644);
      if (server.aof_fd == -1)
	{
	  redisLog (REDIS_WARNING, "Can't open the append-only file: %s",
		    strerror (errno));
	  exit (1);
	}
    }
  if (server.ipfd_count > 0)
    redisLog (REDIS_NOTICE,
	      "The server is now ready to accept connections on port %d",
	      server.port);
  if (server.sofd > 0)
    redisLog (REDIS_NOTICE,
	      "The server is now ready to accept connections at %s",
	      server.unixsocket);
}
Esempio n. 23
0
int main(int argc, char **argv)
{
    config.daemonize = 0;
    config.logserver = "minard";
    config.dataserver = "192.168.80.100";//"192.168.80.1";
    config.logfile = "";
    config.loglevel = NOTICE;

    parseOptions(argc, argv);

    strcpy(logfile, config.logfile);
    verbosity = config.loglevel;

    if (config.daemonize) daemonize();

    signal(SIGPIPE, SIG_IGN);
    signal(SIGINT, sigint_handler);

    Log(NOTICE, "tubii server started");

    el = aeCreateEventLoop(100);

    if ((aeCreateTimeEvent(el, 0, printSkipped, NULL, NULL)) == AE_ERR) {
        LogRaw(WARNING, "failed to set up printSkipped()");
    }

    startLogServer(config.logserver, "tubii");

    initServer(el, 4001, commandTable, sizeof(commandTable)/sizeof(struct command));

    /* set up the dispatch_connect event which will try to connect to the
     * data stream server. If it can't connect, it will retry every 10
     * seconds. */
    if (data_connect(config.dataserver)) {
        Log(WARNING, "failed to set up data stream");
        return 1;
    }

    auto_init();

    /* start tubii readout */
    if (start_tubii_readout(1000)) {
        //Log(WARNING, tubii_err);
        return 1;
    }

    /* set up status event */
    if (aeCreateTimeEvent(el, 0, tubii_status, NULL, NULL) == AE_ERR) {
        Log(WARNING, "failed to set up status tubii");
        return 1;
    }

    /* enter the main event loop */
    el->stop = 0;
    while (!el->stop) {
        if (el->beforesleep != NULL)
            el->beforesleep(el);
        aeProcessEvents(el, AE_ALL_EVENTS);
    }

    Log(NOTICE, "ctrl-c caught. flushing buffers...");

    time_t now = time(NULL);
    while (time(NULL) < now + 1) {
        if (aeProcessEvents(el, AE_FILE_EVENTS | AE_DONT_WAIT) == 0) break;
    }

    aeDeleteEventLoop(el);

    return 0;
}
Esempio n. 24
0
/* Main */
int
clusterMain (int argc, char **argv)
{
  long long teid;
  redisLog (REDIS_WARNING, "Server started, Redis version " REDIS_VERSION);
#ifdef __linux__
  linuxOvercommitMemoryWarning ();
#endif
  loadDataFromDisk ();
  server.last_bgsave_seqnum = server.smr_seqnum;

  /* Warning the user about suspicious maxmemory setting. */
  if (server.maxmemory > 0 && server.maxmemory < 1024 * 1024)
    {
      redisLog (REDIS_WARNING,
		"WARNING: You specified a maxmemory value that is less than 1MB (current value is %llu bytes). Are you sure this is what you really want?",
		server.maxmemory);
    }

  smrConnect ();

  /* initializeCron for handling sigterm */
  teid = aeCreateTimeEvent (server.el, 1, initializeCron, NULL, NULL);
  aeMain (server.el);
  aeDeleteTimeEvent (server.el, teid);

  if (server.need_rckpt)
    {
      redisLog (REDIS_NOTICE,
		"Need more checkpoint from %s:%d", server.ckpt_host,
		server.ckpt_port);
      smrDisconnect ();
      server.smr_init_flags = SMR_INIT_RCKPT;
      if (getdump
	  (server.ckpt_host, server.ckpt_port, server.rdb_filename,
	   "0-8191", REDIS_GETDUMP_DEFAULT_NET_LIMIT_MB) != REDIS_OK)
	{
	  exit (1);
	}

      emptyDb (NULL);
      loadDataFromDisk ();
      server.last_bgsave_seqnum = server.smr_seqnum;

      smrConnect ();
      aeMain (server.el);
    }

  if (!server.is_ready)
    {
      redisLog (REDIS_WARNING, "Invalid initialization state");
      exit (1);
    }

  if (server.last_bgsave_seqnum)
    {
      if (smr_seq_ckpted (server.smr_conn, server.last_bgsave_seqnum) != 0)
	{
	  redisLog (REDIS_WARNING,
		    "Failed to notify checkpointed sequence to smr");
	  exit (1);
	}
      else
	{
	  redisLog (REDIS_NOTICE,
		    "Checkpointed sequence is sent to SMR, seqnum:%lld",
		    server.last_bgsave_seqnum);
	}
    }

  server.smr_seqnum_reset = 0;
  server.last_catchup_check_mstime = mstime ();
  server.smr_init_flags = SMR_INIT_CATCHUP_PHASE1;
  if (smr_catchup (server.smr_conn) == -1)
    {
      redisLog (REDIS_WARNING, "Failed to catchup errno(%d)", errno);
      exit (1);
    }
  server.smr_init_flags = SMR_INIT_CATCHUP_PHASE2;
  checkSmrCatchup ();
  aeSetBeforeSleepProc (server.el, beforeSleep);
  aeMain (server.el);
  aeDeleteEventLoop (server.el);
  exit (0);
}
void easysocketbenchmark::timmerevent(){
	aeCreateTimeEvent(this->g_event_loop, this->test_time*60*1000, easysocketbenchmark::handletimmer, this, NULL);
}
Esempio n. 26
0
void mx_init_daemon()
{
    struct linger ling = {0, 0};
    struct sockaddr_in addr;
    int flags = 1;

    mx_daemon->log_fd = fopen(mx_daemon->log_file, "a+");
    if (!mx_daemon->log_file) {
        fprintf(stderr, "[failed] failed to open log file\n");
        exit(-1);
    }

    mx_daemon->fd = socket(AF_INET, SOCK_STREAM, 0);
    if (mx_daemon->fd == -1) {
        mx_write_log(mx_log_error, "Unable create listening server socket");
        exit(-1);
    }

    if (mx_set_nonblocking(mx_daemon->fd) == -1) {
        mx_write_log(mx_log_error, "Unable set socket to non-blocking");
        exit(-1);
    }

    setsockopt(mx_daemon->fd, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags));
    setsockopt(mx_daemon->fd, SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof(flags));
    setsockopt(mx_daemon->fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
#if !defined(TCP_NOPUSH)
    setsockopt(mx_daemon->fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags));
#endif

    addr.sin_family = AF_INET;
    addr.sin_port = htons(mx_daemon->port);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    if (bind(mx_daemon->fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
        mx_write_log(mx_log_error, "Unable bind socket");
        close(mx_daemon->fd);
        exit(-1);
    }

    if (listen(mx_daemon->fd, 1024) == -1) {
        mx_write_log(mx_log_error, "Unable listen socket");
        close(mx_daemon->fd);
        exit(-1);
    }

    mx_daemon->event = aeCreateEventLoop();
    if (!mx_daemon->event) {
        mx_write_log(mx_log_error, "Unable create EventLoop");
        exit(-1);
    }

    mx_daemon->table = hash_alloc(32);
    if (!mx_daemon->table) {
        mx_write_log(mx_log_error, "Unable create HashTable");
        exit(-1);
    }

    mx_daemon->delay_queue = mx_queue_create("__delay__", sizeof("__delay__") - 1);
    if (!mx_daemon->table) {
        mx_write_log(mx_log_error, "Unable create delay queue");
        exit(-1);
    }
    
    mx_daemon->recycle = mx_recycle_create();
    if (!mx_daemon->recycle) {
        mx_write_log(mx_log_error, "Unable create recycle");
        exit(-1);
    }

    if (aeCreateFileEvent(mx_daemon->event, mx_daemon->fd,
            AE_READABLE, mx_accept_connection, NULL) == -1) {
        mx_write_log(mx_log_error, "Unable create accpet file event");
        exit(-1);
    }

    aeCreateTimeEvent(mx_daemon->event, 1, mx_core_timer, NULL, NULL);

    time(&mx_current_time);

    return;
}
Esempio n. 27
0
int main(int argc, const char **argv) {
    int i;
    char *data, *cmd;
    int len;

    client c;

#ifdef _WIN32
    w32initWinSock();
#endif

    signal(SIGHUP, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);

    config.numclients = 50;
    config.requests = 10000;
    config.liveclients = 0;
    config.el = aeCreateEventLoop();
    aeCreateTimeEvent(config.el,1,showThroughput,NULL,NULL);
    config.keepalive = 1;
    config.datasize = 3;
    config.randomkeys = 0;
    config.randomkeys_keyspacelen = 0;
    config.quiet = 0;
    config.loop = 0;
    config.idlemode = 0;
    config.latency = NULL;
    config.clients = listCreate();
    config.hostip = "127.0.0.1";
    config.hostport = 6379;
    config.hostsocket = NULL;

    i = parseOptions(argc,argv);
    argc -= i;
    argv += i;

    config.latency = zmalloc(sizeof(long long)*config.requests);

    if (config.keepalive == 0) {
        printf("WARNING: keepalive disabled, you probably need 'echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse' for Linux and 'sudo sysctl -w net.inet.tcp.msl=1000' for Mac OS X in order to use a lot of clients/requests\n");
    }

    if (config.idlemode) {
        printf("Creating %d idle connections and waiting forever (Ctrl+C when done)\n", config.numclients);
        c = createClient("",0); /* will never receive a reply */
        createMissingClients(c);
        aeMain(config.el);
        /* and will wait for every */
    }

    /* Run benchmark with command in the remainder of the arguments. */
    if (argc) {
        sds title = sdsnew(argv[0]);
        for (i = 1; i < argc; i++) {
            title = sdscatlen(title, " ", 1);
            title = sdscatlen(title, (char*)argv[i], strlen(argv[i]));
        }

        do {
            len = redisFormatCommandArgv(&cmd,argc,argv,NULL);
            benchmark(title,cmd,len);
            free(cmd);
        } while(config.loop);

        return 0;
    }

    /* Run default benchmark suite. */
    do {
        data = zmalloc(config.datasize+1);
        memset(data,'x',config.datasize);
        data[config.datasize] = '\0';

        benchmark("PING (inline)","PING\r\n",6);

        len = redisFormatCommand(&cmd,"PING");
        benchmark("PING",cmd,len);
        free(cmd);

        const char *argv[21];
        argv[0] = "MSET";
        for (i = 1; i < 21; i += 2) {
            argv[i] = "foo:rand:000000000000";
            argv[i+1] = data;
        }
        len = redisFormatCommandArgv(&cmd,21,argv,NULL);
        benchmark("MSET (10 keys)",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"SET foo:rand:000000000000 %s",data);
        benchmark("SET",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"GET foo:rand:000000000000");
        benchmark("GET",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"INCR counter:rand:000000000000");
        benchmark("INCR",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"LPUSH mylist %s",data);
        benchmark("LPUSH",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"LPOP mylist");
        benchmark("LPOP",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"SADD myset counter:rand:000000000000");
        benchmark("SADD",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"SPOP myset");
        benchmark("SPOP",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"LPUSH mylist %s",data);
        benchmark("LPUSH (again, in order to bench LRANGE)",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"LRANGE mylist 0 99");
        benchmark("LRANGE (first 100 elements)",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"LRANGE mylist 0 299");
        benchmark("LRANGE (first 300 elements)",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"LRANGE mylist 0 449");
        benchmark("LRANGE (first 450 elements)",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"LRANGE mylist 0 599");
        benchmark("LRANGE (first 600 elements)",cmd,len);
        free(cmd);

        printf("\n");
    } while(config.loop);

#ifdef _WIN32
    WSACleanup();
#endif
    return 0;
}
Esempio n. 28
0
int main(int argc, char **argv) {
    struct addrinfo *addrs, *addr;
    char *host = "127.0.0.1";
    char *port = "1337";
    int rc;
    // for checking that the server's up
    char poke[SHA_LENGTH * 2];
    tinymt64_t rando;

    if (parse_args(&cfg, &host, &port, argc, argv)) {
        usage();
        exit(1);
    }

    struct addrinfo hints = {
        .ai_family   = AF_UNSPEC,
        .ai_socktype = SOCK_STREAM
    };

    if ((rc = getaddrinfo(host, port, &hints, &addrs)) != 0) {
        const char *msg = gai_strerror(rc);
        fprintf(stderr, "unable to resolve %s:%s: %s\n", host, port, msg);
        exit(1);
    }

    for (addr = addrs; addr != NULL; addr = addr->ai_next) {
        int fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
        if (fd == -1) continue;
        rc = connect(fd, addr->ai_addr, addr->ai_addrlen);
        tinymt64_init(&rando, time_us());
        random_hash(&rando, poke);
        if (rc == 0 && write(fd, poke, SHA_LENGTH * 2) == SHA_LENGTH * 2) {
            read(fd, poke, SHA_LENGTH * 2);
            close(fd);
            break;
        }
        close(fd);
    }

    if (addr == NULL) {
        char *msg = strerror(errno);
        fprintf(stderr, "unable to connect to %s:%s: %s\n", host, port, msg);
        exit(1);
    }

    signal(SIGPIPE, SIG_IGN);
    signal(SIGINT,  SIG_IGN);
    cfg.addr = *addr;

    pthread_mutex_init(&statistics.mutex, NULL);
    statistics.latency  = stats_alloc(SAMPLES);
    statistics.requests = stats_alloc(SAMPLES);

    thread *threads = zcalloc(cfg.threads * sizeof(thread));
    uint64_t connections = cfg.connections / cfg.threads;
    uint64_t stop_at     = time_us() + (cfg.duration * 1000000);

    for (uint64_t i = 0; i < cfg.threads; i++) {
        thread *t = &threads[i];
        t->connections = connections;
        t->stop_at     = stop_at;

        if (pthread_create(&t->thread, NULL, &thread_main, t)) {
            char *msg = strerror(errno);
            fprintf(stderr, "unable to create thread %"PRIu64" %s\n", i, msg);
            exit(2);
        }
    }

    struct sigaction sa = {
        .sa_handler = handler,
        .sa_flags   = 0,
    };
    sigfillset(&sa.sa_mask);
    sigaction(SIGINT, &sa, NULL);

    char *time = format_time_s(cfg.duration);
    printf("Running %s test @ %s:%s\n", time, host, port);
    printf("  %"PRIu64" threads and %"PRIu64" connections\n", cfg.threads, cfg.connections);

    uint64_t start    = time_us();
    uint64_t complete = 0;
    uint64_t bytes    = 0;
    errors errors     = { 0 };

    for (uint64_t i = 0; i < cfg.threads; i++) {
        thread *t = &threads[i];
        pthread_join(t->thread, NULL);

        complete += t->complete;
        bytes    += t->bytes;

        errors.connect   += t->errors.connect;
        errors.handshake += t->errors.handshake;
        errors.read      += t->errors.read;
        errors.validate  += t->errors.validate;
        errors.write     += t->errors.write;
        errors.timeout   += t->errors.timeout;
    }

    uint64_t runtime_us = time_us() - start;
    long double runtime_s   = runtime_us / 1000000.0;
    long double req_per_s   = complete   / runtime_s;
    long double bytes_per_s = bytes      / runtime_s;

    print_stats_header();
    print_stats("Latency", statistics.latency, format_time_us);
    print_stats("Req/Sec", statistics.requests, format_metric);
    if (cfg.latency) print_stats_latency(statistics.latency);

    char *runtime_msg = format_time_us(runtime_us);

    printf("  %"PRIu64" requests in %s, %sB read\n", complete, runtime_msg, format_binary(bytes));
    if (errors.connect || errors.read || errors.write || errors.timeout) {
        printf("  Socket errors: connect %d, read %d, write %d, timeout %d\n",
               errors.connect, errors.read, errors.write, errors.timeout);
    }

    if (errors.handshake) {
        printf("  Bad handshakes from server: %d\n", errors.handshake);
    }

    if (errors.validate) {
        printf("  %d proofs failed verification.\n", errors.validate);
    }

    printf("Requests/sec: %9.2Lf\n", req_per_s);
    printf("Transfer/sec: %10sB\n", format_binary(bytes_per_s));

    return 0;
}

void *thread_main(void *arg) {
    thread *thread = arg;

    aeEventLoop *loop = aeCreateEventLoop(10 + cfg.connections * 3);
    thread->cs   = zmalloc(thread->connections * sizeof(connection));
    thread->loop = loop;
    tinymt64_init(&thread->rand, time_us());
    thread->latency = stats_alloc(100000);

    connection *c = thread->cs;

    for (uint64_t i = 0; i < thread->connections; i++, c++) {
        c->thread = thread;
        random_hash(&thread->rand, c->hash);
        connect_socket(thread, c);
    }

    aeCreateTimeEvent(loop, CALIBRATE_DELAY_MS, calibrate, thread, NULL);
    aeCreateTimeEvent(loop, TIMEOUT_INTERVAL_MS, check_timeouts, thread, NULL);

    thread->start = time_us();
    aeMain(loop);

    aeDeleteEventLoop(loop);
    zfree(thread->cs);

    uint64_t max = thread->latency->max;
    stats_free(thread->latency);

    pthread_mutex_lock(&statistics.mutex);
    for (uint64_t i = 0; i < thread->missed; i++) {
        stats_record(statistics.latency, max);
    }
    pthread_mutex_unlock(&statistics.mutex);

    return NULL;
}