grn_rc grn_com_event_init(grn_ctx *ctx, grn_com_event *ev, int max_nevents, int data_size) { ev->max_nevents = max_nevents; if ((ev->hash = grn_hash_create(ctx, NULL, sizeof(grn_sock), data_size, 0))) { MUTEX_INIT(ev->mutex); COND_INIT(ev->cond); GRN_COM_QUEUE_INIT(&ev->recv_old); ev->msg_handler = NULL; memset(&(ev->curr_edge_id), 0, sizeof(grn_com_addr)); ev->acceptor = NULL; ev->opaque = NULL; #ifndef USE_SELECT # ifdef USE_EPOLL if ((ev->events = GRN_MALLOC(sizeof(struct epoll_event) * max_nevents))) { if ((ev->epfd = epoll_create(max_nevents)) != -1) { goto exit; } else { SERR("epoll_create"); } GRN_FREE(ev->events); } # else /* USE_EPOLL */ # ifdef USE_KQUEUE if ((ev->events = GRN_MALLOC(sizeof(struct kevent) * max_nevents))) { if ((ev->kqfd = kqueue()) != -1) { goto exit; } else { SERR("kqueue"); } GRN_FREE(ev->events); } # else /* USE_KQUEUE */ if ((ev->events = GRN_MALLOC(sizeof(struct pollfd) * max_nevents))) { goto exit; } # endif /* USE_KQUEUE*/ # endif /* USE_EPOLL */ grn_hash_close(ctx, ev->hash); ev->hash = NULL; ev->events = NULL; #else /* USE_SELECT */ goto exit; #endif /* USE_SELECT */ } exit : return ctx->rc; }
static void grn_com_receiver(grn_ctx *ctx, grn_com *com) { grn_com_event *ev = com->ev; ERRCLR(ctx); if (ev->acceptor == com) { grn_com *ncs; grn_sock fd = accept(com->fd, NULL, NULL); if (fd == -1) { if (errno == EMFILE) { grn_com_event_stop_accept(ctx, ev); } else { SOERR("accept"); } return; } if (grn_com_event_add(ctx, ev, fd, GRN_COM_POLLIN, (grn_com **)&ncs)) { grn_sock_close(fd); return; } ncs->has_sid = 0; ncs->closed = 0; ncs->opaque = NULL; GRN_COM_QUEUE_INIT(&ncs->new_); // GRN_LOG(ctx, GRN_LOG_NOTICE, "accepted (%d)", fd); return; } else { grn_msg *msg = (grn_msg *)grn_msg_open(ctx, com, &ev->recv_old); grn_com_recv(ctx, msg->u.peer, &msg->header, (grn_obj *)msg); if (msg->u.peer /* is_edge_request(msg)*/) { grn_memcpy(&msg->edge_id, &ev->curr_edge_id, sizeof(grn_com_addr)); if (!com->has_sid) { com->has_sid = 1; com->sid = ev->curr_edge_id.sid++; } msg->edge_id.sid = com->sid; } msg->acceptor = ev->acceptor; ev->msg_handler(ctx, (grn_obj *)msg); } }
static int do_client() { int rc = -1; char *buf; grn_thread thread; struct timeval tvb, tve; grn_com_header sheader; grn_ctx ctx_, *ctx = &ctx_; grn_ctx_init(ctx, 0); GRN_COM_QUEUE_INIT(&fsessions); sessions = grn_hash_create(ctx, NULL, sizeof(grn_sock), sizeof(session), 0); sheader.proto = GRN_COM_PROTO_GQTP; sheader.qtype = 0; sheader.keylen = 0; sheader.level = 0; sheader.flags = 0; sheader.status = 0; sheader.opaque = 0; sheader.cas = 0; if ((buf = GRN_MALLOC(BUFSIZE))) { if (!grn_com_event_init(ctx, &ev, 1000, sizeof(grn_com))) { ev.msg_handler = msg_handler; if (!THREAD_CREATE(thread, receiver, NULL)) { int cnt = 0; gettimeofday(&tvb, NULL); lprint(ctx, "begin: max_concurrency=%d max_tp=%d", max_con, max_tp); while (fgets(buf, BUFSIZE, stdin)) { uint32_t size = strlen(buf) - 1; session *s = session_alloc(ctx, dests + (cnt++ % dest_cnt)); if (s) { gettimeofday(&s->tv, NULL); s->n_query++; s->query_id = ++nsent; s->n_sessions = (nsent - nrecv); switch (proto) { case 'H' : case 'h' : if (grn_com_send_text(ctx, s->com, buf, size, 0)) { fprintf(stderr, "grn_com_send_text failed\n"); } s->stat = 2; /* lprint(ctx, "sent %04d %04d %d", s->n_query, s->query_id, s->com->fd); */ break; default : if (grn_com_send(ctx, s->com, &sheader, buf, size, 0)) { fprintf(stderr, "grn_com_send failed\n"); } break; } } else { fprintf(stderr, "grn_com_copen failed\n"); } for (;;) { gettimeofday(&tve, NULL); if ((nrecv < max_tp * (tve.tv_sec - tvb.tv_sec)) && (nsent - nrecv) < max_con) { break; } /* lprint(ctx, "s:%d r:%d", nsent, nrecv); */ usleep(1000); } if (!(nsent % 1000)) { lprint(ctx, " : %d", nsent); } } done = 1; pthread_join(thread, NULL); gettimeofday(&tve, NULL); { double qps; uint64_t etime = (tve.tv_sec - tvb.tv_sec); etime *= 1000000; etime += (tve.tv_usec - tvb.tv_usec); qps = (double)nsent * 1000000 / etime; lprint(ctx, "end : n=%d min=%d max=%d avg=%d qps=%f etime=%d.%06d", nsent, etime_min, etime_max, (int)(etime_amount / nsent), qps, etime / 1000000, etime % 1000000); } { session *s; GRN_HASH_EACH(ctx, sessions, id, NULL, NULL, &s, { session_close(ctx, s); }); } rc = 0; } else {