Skip to content

Commit

Permalink
ST: Refine performance when epoll_wait(0)
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Feb 3, 2021
1 parent ec01f94 commit a913d9c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
17 changes: 17 additions & 0 deletions trunk/3rdparty/st-srs/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,11 @@ ST_HIDDEN int _st_epoll_pollset_add(struct pollfd *pds, int npds)
return 0;
}

#ifdef DEBUG
unsigned long long _g_st_nn_epoll_wait = 0;
unsigned long long _g_st_nn_epoll_wait_zero = 0;
#endif

ST_HIDDEN void _st_epoll_dispatch(void)
{
st_utime_t min_timeout;
Expand All @@ -1210,6 +1215,11 @@ ST_HIDDEN void _st_epoll_dispatch(void)
} else {
min_timeout = (_ST_SLEEPQ->due <= _ST_LAST_CLOCK) ? 0 : (_ST_SLEEPQ->due - _ST_LAST_CLOCK);
timeout = (int) (min_timeout / 1000);

// At least wait 1ms when <1ms, to avoid epoll_wait spin loop.
if (min_timeout > 0 && timeout == 0) {
timeout = 1;
}
}

if (_st_epoll_data->pid != getpid()) {
Expand All @@ -1235,6 +1245,13 @@ ST_HIDDEN void _st_epoll_dispatch(void)
/* Check for I/O operations */
nfd = epoll_wait(_st_epoll_data->epfd, _st_epoll_data->evtlist, _st_epoll_data->evtlist_size, timeout);

#ifdef DEBUG
++_g_st_nn_epoll_wait;
if (timeout <= 0 && min_timeout > 0) {
++_g_st_nn_epoll_wait_zero;
}
#endif

if (nfd > 0) {
for (i = 0; i < nfd; i++) {
osfd = _st_epoll_data->evtlist[i].data.fd;
Expand Down
31 changes: 30 additions & 1 deletion trunk/src/app/srs_app_rtc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,13 @@ SrsRtcConnection* SrsRtcServer::find_session_by_username(const std::string& user
return dynamic_cast<SrsRtcConnection*>(conn);
}

extern unsigned long long _g_st_nn_epoll_wait;
extern unsigned long long _g_st_nn_epoll_wait_zero;
unsigned long long _g_st_nn_epoll_wait_prev = 0;
unsigned long long _g_st_nn_epoll_wait_zero_prev = 0;
srs_utime_t _g_st_stat = 0;
srs_utime_t _g_st_stat_zero = 0;

srs_error_t SrsRtcServer::notify(int type, srs_utime_t interval, srs_utime_t tick)
{
srs_error_t err = srs_success;
Expand Down Expand Up @@ -630,12 +637,34 @@ srs_error_t SrsRtcServer::notify(int type, srs_utime_t interval, srs_utime_t tic
return err;
}

int diff_ps = 0;
srs_utime_t duration = srs_get_system_time() - _g_st_stat;
if (duration > 0) {
diff_ps = (_g_st_nn_epoll_wait - _g_st_nn_epoll_wait_prev) * SRS_UTIME_SECONDS / duration;
_g_st_nn_epoll_wait_prev = _g_st_nn_epoll_wait;
_g_st_stat = srs_get_system_time();
}

int diff_zero_bs = 0;
srs_utime_t duration_zero = srs_get_system_time() - _g_st_stat_zero;
if (duration_zero > 0) {
diff_zero_bs = (_g_st_nn_epoll_wait_zero - _g_st_nn_epoll_wait_zero_prev) * SRS_UTIME_SECONDS / duration_zero;
_g_st_nn_epoll_wait_zero_prev = _g_st_nn_epoll_wait_zero;
_g_st_stat_zero = srs_get_system_time();
}

float epoll_rate = 0;
if (diff_ps > 0) {
epoll_rate = 100.0 * diff_zero_bs / diff_ps;
}

// Show statistics for RTC server.
SrsProcSelfStat* u = srs_get_self_proc_stat();
// Resident Set Size: number of pages the process has in real memory.
int memory = (int)(u->rss * 4 / 1024);
// TODO: FIXME: Show more data for RTC server.
srs_trace("RTC: Server conns=%u, cpu=%.2f%%, rss=%dMB", nn_rtc_conns, u->percent * 100, memory);
srs_trace("RTC: Server conns=%u, cpu=%.2f%%, rss=%dMB, epoll=%dps,%dps,%.2f%%",
nn_rtc_conns, u->percent * 100, memory, diff_ps, diff_zero_bs, epoll_rate);

return err;
}
Expand Down

2 comments on commit a913d9c

@winlinvip
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@winlinvip
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See cfddc8f

Please sign in to comment.