static void stack_stats_cb(struct ev_loop *loop, ev_timer *w, int revents) { struct stack_config *scfg = w->data; struct uinet_tcpstat stat; int num_open_sockets = 0; size_t len; ev_tstamp timestamp; if (scfg->first_stats) { scfg->first_stats_time = ev_now(loop); scfg->first_stats = 0; } uinet_gettcpstat(scfg->uinst, &stat); len = sizeof(num_open_sockets); uinet_sysctlbyname(scfg->uinst, "kern.ipc.numopensockets", (char *)&num_open_sockets, &len, NULL, 0, NULL, 0); timestamp = ev_now(loop) - scfg->first_stats_time; #define PRINT_TCPSTAT(s) printf("%.6f %s = %llu\n", timestamp, #s, (unsigned long long)stat.s) printf("%.6f num_sockets = %u\n", timestamp, num_open_sockets); PRINT_TCPSTAT(tcps_connects); PRINT_TCPSTAT(tcps_closed); #undef PRINT_TCPSTAT }
scheduler_impl_t::wait_result_t scheduler_impl_t::wait_queue(spinlock_t* queue_lock, duration_t* timeout) { ev_timer timer_timeout; watcher_data_t watcher_data(FIBER_IMPL); deferred_unlock_t deferred(queue_lock); ev_tstamp start_wait; if(timeout) { start_wait = ev_now(ev_loop_); ev_init((ev_watcher*)&timer_timeout, switch_to_cb); ev_timer_set(&timer_timeout, timeout->count(), 0.0); ev_timer_start(ev_loop_, &timer_timeout); timer_timeout.data = &watcher_data; } FIBER_IMPL->yield(&deferred); unlink_activate(FIBER_IMPL); if(timeout) { *timeout -= duration_t(ev_now(ev_loop_) - start_wait); ev_timer_stop(ev_loop_, &timer_timeout); } if(timeout && (watcher_data.events & EV_TIMER)) { return TIMEDOUT; } else { return READY; } }
Socket::Socket(struct ev_loop* loop, int fd, int af) : loop_(loop), watcher_(loop), timer_(loop), startedAt_(ev_now(loop)), lastActivityAt_(ev_now(loop)), fd_(fd), addressFamily_(af), secure_(false), state_(Operational), mode_(None), tcpCork_(false), remoteIP_(), remotePort_(0), localIP_(), localPort_(), callback_(nullptr), callbackData_(0) { #ifndef NDEBUG setLogging(false); static std::atomic<unsigned long long> id(0); setLoggingPrefix("Socket(%d, %s:%d)", ++id, remoteIP().c_str(), remotePort()); #endif TRACE("created. fd:%d, local(%s:%d)", fd_, localIP().c_str(), localPort()); watcher_.set<Socket, &Socket::io>(this); timer_.set<Socket, &Socket::timeout>(this); }
static void time_restart(pa_time_event *ev, const struct timeval *tv) { ev_tstamp timeout = timeval_to_stamp(tv); lem_debug("resetting to %f seconds", timeout - ev_now(LEM)); ev->tv.tv_sec = tv->tv_sec; ev->tv.tv_usec = tv->tv_usec; ev_timer_stop(LEM_ &ev->w); ev_timer_set(&ev->w, timeout - ev_now(LEM), 0); ev_timer_start(LEM_ &ev->w); }
tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) : v(v_), si(si_) { set<tcp_connection, &tcp_connection::tcpv4_ev> (this); last_activity = ev_now (); r_pkt = 0; w_pkt = 0; tos = -1; fd = fd_; #if ENABLE_HTTP_PROXY proxy_req = 0; #endif if (fd < 0) { active = true; state = IDLE; } else { active = false; state = ESTABLISHED; start (fd, EV_READ); } }
ssize_t buffer_recv(struct Buffer *buffer, int sockfd, int flags) { ssize_t bytes; struct iovec iov[2]; struct msghdr msg; /* coalesce when reading into an empty buffer */ if (buffer->len == 0) buffer->head = 0; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_iov = iov; msg.msg_iovlen = setup_write_iov(buffer, iov, 0); msg.msg_control = NULL; msg.msg_controllen = 0; msg.msg_flags = 0; bytes = recvmsg(sockfd, &msg, flags); buffer->last_recv = ev_now(EV_DEFAULT); if (bytes > 0) advance_write_position(buffer, bytes); return bytes; }
static void stdout_callback (EV_P_ ev_io *w, int revents) { if ( 0 < data_size ) { if ( data_size != write( STDOUT_FILENO, &data[ 0 ], data_size ) ) { print_timer(); fprintf(stderr, "{ \"posix_time\": %f, \"exit_status\": \"Error\", \"msg\": \"Error writing to stdout\" }\n", ev_time()); exit(data_size); } bytes_out += data_size; data_size = 0; } if ( 0 == data_size ) { // Switch to read mode ev_tstamp now = ev_now( loop ); mode = READING; stdin_pipe.timer_start = now; if ( 0 < stdout_pipe.timer_start ) stdout_pipe.time_waiting += now - stdout_pipe.timer_start; ev_io_init (&stdin_pipe.watcher, stdin_callback, STDIN_FILENO, EV_READ); ev_io_start( loop, &stdin_pipe.watcher ); ev_io_stop( loop, &stdout_pipe.watcher ); } }
void timeout_cb (struct ev_loop *loop, ev_timer *w, int revents) { ev_tstamp ts = ev_now (loop); void *zsock = w->data; zmq_send(zsock, &ts, sizeof(ts), 0); zmq_send(zsock, &ts, sizeof(ts), 0); }
ssize_t Socket::read(Buffer& result) { ssize_t nread = 0; lastActivityAt_.update(ev_now(loop_)); for (;;) { if (result.capacity() - result.size() < 256) { result.reserve(std::max(4096ul, static_cast<std::size_t>(result.size() * 1.5))); } size_t nbytes = result.capacity() - result.size(); ssize_t rv = ::read(fd_, result.end(), nbytes); if (rv <= 0) { TRACE("read(): rv=%ld -> %ld: %s", rv, result.size(), strerror(errno)); return nread != 0 ? nread : rv; } else { TRACE("read() -> %ld", rv); nread += rv; result.resize(result.size() + rv); if (static_cast<std::size_t>(rv) < nbytes) { return nread; } } } }
static void ugh_subreq_wcb_connect(EV_P_ ev_io *w, int tev) { ugh_subreq_t *r = aux_memberof(ugh_subreq_t, wev_connect, w); if (EV_READ & tev) { int optval = 0; socklen_t optlen = sizeof(optval); if (0 > getsockopt(w->fd, SOL_SOCKET, SO_ERROR, &optval, &optlen)) { optval = errno; } ugh_subreq_del(r, UGH_UPSTREAM_FT_ERROR, optval); return; } ev_io_stop(loop, &r->wev_connect); ev_timer_stop(loop, &r->wev_timeout_connect); r->connection_time = ev_now(loop) - r->response_time; ev_io_start(loop, &r->wev_send); }
static void IOcurl_abort_shutdown_callback(struct ev_loop *loop, ev_cleanup *watcher, int revents) { CURLMcode msta; AsyncIO *IO = watcher->data; if (IO == NULL) return; SetEVState(IO, eCurlShutdown); IO->CitContext->lastcmd = IO->Now = ev_now(event_base); EVCURL_syslog(LOG_DEBUG, "EVENT Curl: %s\n", __FUNCTION__); curl_slist_free_all(IO->HttpReq.headers); IO->HttpReq.headers = NULL; msta = curl_multi_remove_handle(global.mhnd, IO->HttpReq.chnd); if (msta) { EVCURL_syslog(LOG_ERR, "EVCURL: warning problem detaching completed handle " "from curl multi: %s\n", curl_multi_strerror(msta)); } curl_easy_cleanup(IO->HttpReq.chnd); IO->HttpReq.chnd = NULL; ev_cleanup_stop(event_base, &IO->abort_by_shutdown); ev_io_stop(event_base, &IO->recv_event); ev_io_stop(event_base, &IO->send_event); assert(IO->ShutdownAbort); IO->ShutdownAbort(IO); }
int event_pending (struct event *ev, short events, struct timeval *tv) { short revents = 0; dLOOPev; if (ev->ev_events & EV_SIGNAL) { /* sig */ if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) revents |= EV_SIGNAL; } else if (ev->ev_events & (EV_READ | EV_WRITE)) { /* io */ if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) revents |= ev->ev_events & (EV_READ | EV_WRITE); } if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) { revents |= EV_TIMEOUT; if (tv) EV_TV_SET (tv, ev_now (EV_A)); /* not sure if this is right :) */ } return events & revents; }
void CNetBackend::socket_check_cb(ev::timer &w, int32 revents) { if ( EV_ERROR & revents ) { GERROR() << "check socket connect timeout call back error:" << strerror(errno) << "\n"; w.stop(); abort_work( "socket_check_cb fail" ); } int64 _now = ev_now(loop); CTcpSocket *psocket = null; vector<CTcpSocket *>::iterator itr = m_tcp_sockets.begin(); while ( itr != m_tcp_sockets.end() ) { psocket = *itr; itr ++; if ( !psocket ) //this fd not use continue; if ( ES_CONNECTED == psocket->get_socket_status() && _now - psocket->get_connect_time() > DISCONNECT_TIMEOUT ) { psocket->close_socket(); //该状态未与游戏服建立连接,可以直接关闭 psocket->set_socket_status( ES_NONE ); } } }
void CNetBackend::add_socket( int32 fd,const struct sockaddr_in address ) { if ( fd > MAX_FD ) { GERROR() << "add_socket MAX_FD limit\n"; return; } while ( (fd > static_cast<int32>(m_tcp_sockets.size()-1)) && (MAX_FD > m_tcp_sockets.size()) ) //-1数组下标从0开始 { m_tcp_sockets.resize( DEFAULT_SOCKETS,null ); GWARNING() << "socket vector resize occured\n"; } CTcpSocket *psocket = m_tcp_sockets[fd]; if ( null == psocket ) { m_tcp_sockets[fd] = new CTcpSocket();//该构造函数初始化变量 psocket = m_tcp_sockets[fd]; } psocket->uninit_socket(); //clean old data psocket->set_socket_fd( fd ); psocket->set_socket_address( address ); psocket->init_socket(); psocket->set_socket_status( ES_CONNECTED ); psocket->set_connect_time( ev_now(loop) ); //use ev_now() instead of time() psocket->start(); }
/* * DNS UDP socket activity callback */ static void resolv_sock_cb(struct ev_loop *loop, struct ev_io *w, int revents) { struct dns_ctx *ctx = (struct dns_ctx *)w->data; if (revents & EV_READ) dns_ioevent(ctx, ev_now(loop)); }
SslSocket::SslSocket(SslDriver *driver, struct ev_loop *loop, int fd, int af) : x0::Socket(loop, fd, af), #ifndef XZERO_NDEBUG ctime_(ev_now(loop)), #endif driver_(driver), context_(nullptr), session_() { TRACE("SslSocket()"); setSecure(true); setState(Handshake); GNUTLS_CHECK( gnutls_init(&session_, GNUTLS_SERVER) ); gnutls_handshake_set_post_client_hello_function(session_, &SslSocket::onClientHello); gnutls_certificate_server_set_request(session_, GNUTLS_CERT_REQUEST); gnutls_dh_set_prime_bits(session_, 1024); gnutls_session_enable_compatibility_mode(session_); gnutls_session_set_ptr(session_, this); gnutls_transport_set_ptr(session_, reinterpret_cast<gnutls_transport_ptr_t>(handle())); driver_->initialize(this); }
void wcb_recv(EV_P_ ev_io *w, int tev) { client_t *c = aux_memberof(client_t, wev_recv, w); char buf [4096]; int nb = aux_unix_recv(w->fd, buf, 4096); double time = ev_now(loop) - c->ts; times_sum += time; times_count++; if (times_max < time) times_max = time; if (times_min > time) times_min = time; /* fprintf(stderr, "recv fd=%d %f (%d bytes) %.*s\n", w->fd, ev_now(loop) - c->ts, nb, nb, buf); */ /* if (nb != 57) fprintf(stderr, "recv error %d bytes\n", nb); */ if (0 > nb) { if (errno == EAGAIN) { return; } fprintf(stderr, "recv error fd=%d (%d: %s)\n", w->fd, errno, strerror(errno)); client_del(c); return; } }
static void stdin_callback (EV_P_ ev_io *w, int revents) { if ( 0 == data_size ) { if ( 0 >= ( data_size = read( STDIN_FILENO, &data[ 0 ], BUFFER_SIZE ) ) ) { print_timer(); if ( 0 == data_size ) fprintf(stderr, "{ \"posix_time\": %f, \"exit_status\": \"Success\", \"msg\": \"End of file reached\" }\n", ev_time()); else fprintf(stderr, "{ \"posix_time\": %f, \"exit_status\": \"Error\", \"msg\": \"Error reading from stdin\", \"errno\": %d }\n", ev_time(), errno); exit(data_size); } } if( 0 < data_size ) { // switch to write mode ev_tstamp now = ev_now( loop ); mode = WRITING; stdout_pipe.timer_start = now; if( 0 < stdin_pipe.timer_start ) stdin_pipe.time_waiting += now - stdin_pipe.timer_start; ev_io_init (&stdout_pipe.watcher, stdout_callback, STDOUT_FILENO, EV_WRITE); ev_io_start( loop, &stdout_pipe.watcher ); // start stdout ev_io_stop( loop, &stdin_pipe.watcher ); // stop stdin } }
int event_pending (struct event *ev, short events, struct timeval *tv) { short revents = 0; dLOOPev; if (ev->ev_events & EV_SIGNAL) { /* sig */ if (ev_is_active (&ev->iosig.sig) || ev_is_pending (&ev->iosig.sig)) revents |= EV_SIGNAL; } else if (ev->ev_events & (EV_READ | EV_WRITE)) { /* io */ if (ev_is_active (&ev->iosig.io) || ev_is_pending (&ev->iosig.io)) revents |= ev->ev_events & (EV_READ | EV_WRITE); } if (ev->ev_events & EV_TIMEOUT || ev_is_active (&ev->to) || ev_is_pending (&ev->to)) { revents |= EV_TIMEOUT; if (tv) { ev_tstamp at = ev_now (EV_A); tv->tv_sec = (long)at; tv->tv_usec = (long)((at - (ev_tstamp)tv->tv_sec) * 1e6); } } return events & revents; }
static AuthFileData* auth_file_get_data(liWorker *wrk, AuthFile *f) { ev_tstamp now = ev_now(wrk->loop); AuthFileData *data = NULL; g_mutex_lock(f->lock); if (f->ttl != 0 && now >= f->next_check) { struct stat st; f->next_check = now + f->ttl; if (-1 != stat(f->path->str, &st) && st.st_mtime >= f->last_stat - 1) { g_mutex_unlock(f->lock); /* update without lock held */ data = auth_file_load(wrk->srv, f); g_mutex_lock(f->lock); if (NULL != data) { auth_file_data_release(f->data); f->data = data; } } f->last_stat = now; } data = f->data; if (NULL != data) g_atomic_int_inc(&data->refcount); g_mutex_unlock(f->lock); return data; }
static int utils_updatenow(lua_State *T) { ev_now_update(LEM); lua_pushnumber(T, (lua_Number)ev_now(LEM)); return 1; }
void fiber_value(struct fbr_context *fiber_context, void *_arg) { struct client_context *cc; struct my_value *value; struct me_cli_value *mv; int retval; ev_tstamp t1, t2, diff; cc = fbr_container_of(fiber_context, struct client_context, fbr); for (;;) { value = new_value(cc); record_value(cc, value); assert(value->buf); assert(value->buf->ptr); for (;;) { if (value->nreceived > 0) break; mv = me_cli_value_new(cc->conn); mv->data = (uint8_t *)value->buf->ptr; mv->data_len = value->buf->size; value->nsent++; ev_now_update(cc->loop); t1 = ev_now(cc->loop); retval = me_cli_value_submit(mv, cc->args_info.instance_timeout_arg); if (0 == retval) { value->latency = mv->latency; cc->last_iid = mv->iid; t2 = ev_now(cc->loop); diff = cc->args_info.each_arg - (t2 - t1); if (diff > 0) fbr_sleep(&cc->fbr, diff); if (value->nreceived > 0) break; next_value(cc, value, mv->iid); me_cli_value_processed(mv); me_cli_value_dispose(mv); break; } me_cli_value_processed(mv); me_cli_value_dispose(mv); cc->stats.timeouts++; assert(value->buf); assert(value->buf->ptr); } } }
static void stat_update_cb(struct ev_loop *loop) { ev_tstamp now = ev_now(loop); if (now - last > 1.0) { send_traffic_stat(tx, rx); last = now; } }
void zsock_cb(struct ev_loop *loop, ev_zsock_t *wz, int revents) { ev_tstamp ts_recv = ev_now (loop); ev_tstamp ts_send; zmq_recv(wz->zsock, &ts_send, sizeof(ts_send), 0); printf("%f\n", ts_recv - ts_send); }
/* * DNS timeout callback */ static void resolv_timeout_cb(struct ev_loop *loop, struct ev_timer *w, int revents) { struct dns_ctx *ctx = (struct dns_ctx *)w->data; if (revents & EV_TIMER) { dns_timeouts(ctx, 30, ev_now(loop)); } }
static void DBQueueEventAddCallback(EV_P_ ev_async *w, int revents) { CitContext *Ctx; long IOID = -1; long count = 0;; ev_tstamp Now; HashList *q; void *v; HashPos *It; long len; const char *Key; /* get the control command... */ pthread_mutex_lock(&DBEventQueueMutex); if (DBInboundEventQueues[0] == DBInboundEventQueue) { DBInboundEventQueue = DBInboundEventQueues[1]; q = DBInboundEventQueues[0]; } else { DBInboundEventQueue = DBInboundEventQueues[0]; q = DBInboundEventQueues[1]; } pthread_mutex_unlock(&DBEventQueueMutex); Now = ev_now (event_db); It = GetNewHashPos(q, 0); while (GetNextHashPos(q, It, &len, &Key, &v)) { IOAddHandler *h = v; eNextState rc; count ++; if (h->IO->ID == 0) h->IO->ID = EvIDSource++; IOID = h->IO->ID; if (h->IO->StartDB == 0.0) h->IO->StartDB = Now; h->IO->CitContext->lastcmd = h->IO->Now = Now; SetEVState(h->IO, eDBAttach); Ctx = h->IO->CitContext; become_session(Ctx); ev_cleanup_start(event_db, &h->IO->db_abort_by_shutdown); rc = h->EvAttch(h->IO); switch (rc) { case eAbort: ShutDownDBCLient(h->IO); default: break; } } DeleteHashPos(&It); DeleteHashContent(&q); EVQ_syslog(LOG_DEBUG, "%s CC[%ld] DBEVENT Q Add %ld done.", IOSTR, IOID, count); }
void perf_snap_finish(ME_P_ struct perf_snap *snap) { ev_tstamp diff; assert(0 < snap->start); ev_now_update(mctx->loop); diff = ev_now(mctx->loop) - snap->start; snap->total += diff; snap->encounters++; snap->start = 0; }
static void _prepare_cb(UNUSED EV_P_ UNUSED ev_prepare *w, UNUSED int revents) { mnbtrie_node_t *node; mrkthr_ctx_t *ctx = NULL; if (!(mrkthr_flags & CO_FLAG_SHUTDOWN)) { timecounter_now = (uint64_t)(ev_now(the_loop) * 1000000000.); #ifdef TRACE_VERBOSE CTRACE(FRED("Sifting sleepq ...")); #endif /* this will make sure there are no expired ctxes in the sleepq */ poller_sift_sleepq(); /* get the first to wake sleeping mrkthr */ if ((node = BTRIE_MIN(&the_sleepq)) != NULL) { ev_tstamp secs; ctx = node->value; assert(ctx != NULL); if (ctx->expire_ticks > timecounter_now) { secs = (ev_tstamp)(ctx->expire_ticks - timecounter_now) / 1000000000.; } else { /* * some time has elapsed after the call to * sift_sleepq() that made an event expire. */ secs = 0.00000095367431640625; } #ifdef TRACE_VERBOSE CTRACE("wait %f", secs); #endif etimer.repeat = secs; ev_timer_again(the_loop, &etimer); } else { #ifdef TRACE_VERBOSE CTRACE("no wait"); #endif //etimer.repeat = 1.00000095367431640625; //etimer.repeat = INFINITY; etimer.repeat = 59.0; /* <MAX_BLOCKTIME */ ev_timer_again(the_loop, &etimer); //ev_timer_stop(the_loop, &etimer); //ev_unref(the_loop); } } else { CTRACE("breaking the loop"); ev_break(the_loop, EVBREAK_ALL); } //CTRACE("revents=%08x", revents); }
static struct pa_time_event * time_new(pa_mainloop_api *a, const struct timeval *tv, pa_time_event_cb_t cb, void *userdata) { struct pa_time_event *ev = lem_xmalloc(sizeof(struct pa_time_event)); ev_tstamp timeout = timeval_to_stamp(tv); (void)a; lem_debug("after = %f seconds", timeout - ev_now(LEM)); ev->w.data = userdata; ev->tv.tv_sec = tv->tv_sec; ev->tv.tv_usec = tv->tv_usec; ev->cb = cb; ev->destroy = NULL; ev_timer_init(&ev->w, time_handler, timeout - ev_now(LEM), 0); ev_timer_start(LEM_ &ev->w); return ev; }
static void trigger_cb(struct ev_loop *loop, ev_periodic *ep, int revents) { if (EV_ERROR & revents) { fgaj_r("invalid event"); return; } // TODO: shutdown flon-dispatcher flon_trigger(ev_now(loop)); //fgaj_d("sdc: %zu", scan_dir_count); if (scan_dir_count > 0) scan_dir(); }