Skip to content

Commit a789c98

Browse files
committed
Core: Support stat for io and sched events.
1 parent 8ad1141 commit a789c98

File tree

3 files changed

+177
-3
lines changed

3 files changed

+177
-3
lines changed

event.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@
4747
#include <sys/epoll.h>
4848
#endif
4949

50+
// Global stat.
51+
#ifdef DEBUG
52+
unsigned long long _st_stat_epoll = 0;
53+
unsigned long long _st_stat_epoll_zero = 0;
54+
unsigned long long _st_stat_epoll_shake = 0;
55+
unsigned long long _st_stat_epoll_spin = 0;
56+
#endif
57+
5058
#if defined(USE_POLL) && !defined(MD_HAVE_POLL)
5159
/* Force poll usage if explicitly asked for it */
5260
#define MD_HAVE_POLL
@@ -1205,15 +1213,29 @@ ST_HIDDEN void _st_epoll_dispatch(void)
12051213
int events, op;
12061214
short revents;
12071215

1216+
#ifdef DEBUG
1217+
++_st_stat_epoll;
1218+
#endif
1219+
12081220
if (_ST_SLEEPQ == NULL) {
12091221
timeout = -1;
12101222
} else {
12111223
min_timeout = (_ST_SLEEPQ->due <= _ST_LAST_CLOCK) ? 0 : (_ST_SLEEPQ->due - _ST_LAST_CLOCK);
12121224
timeout = (int) (min_timeout / 1000);
12131225

12141226
// At least wait 1ms when <1ms, to avoid epoll_wait spin loop.
1215-
if (min_timeout > 0 && timeout == 0) {
1216-
timeout = 1;
1227+
if (timeout == 0) {
1228+
#ifdef DEBUG
1229+
++_st_stat_epoll_zero;
1230+
#endif
1231+
1232+
if (min_timeout > 0) {
1233+
#ifdef DEBUG
1234+
++_st_stat_epoll_shake;
1235+
#endif
1236+
1237+
timeout = 1;
1238+
}
12171239
}
12181240
}
12191241

@@ -1240,6 +1262,12 @@ ST_HIDDEN void _st_epoll_dispatch(void)
12401262
/* Check for I/O operations */
12411263
nfd = epoll_wait(_st_epoll_data->epfd, _st_epoll_data->evtlist, _st_epoll_data->evtlist_size, timeout);
12421264

1265+
#ifdef DEBUG
1266+
if (nfd <= 0) {
1267+
++_st_stat_epoll_spin;
1268+
}
1269+
#endif
1270+
12431271
if (nfd > 0) {
12441272
for (i = 0; i < nfd; i++) {
12451273
osfd = _st_epoll_data->evtlist[i].data.fd;

io.c

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@
5252
#include <errno.h>
5353
#include "common.h"
5454

55+
// Global stat.
56+
#ifdef DEBUG
57+
unsigned long long _st_stat_recvfrom = 0;
58+
unsigned long long _st_stat_recvfrom_eagain = 0;
59+
unsigned long long _st_stat_sendto = 0;
60+
unsigned long long _st_stat_sendto_eagain = 0;
61+
unsigned long long _st_stat_read = 0;
62+
unsigned long long _st_stat_read_eagain = 0;
63+
unsigned long long _st_stat_readv = 0;
64+
unsigned long long _st_stat_readv_eagain = 0;
65+
unsigned long long _st_stat_writev = 0;
66+
unsigned long long _st_stat_writev_eagain = 0;
67+
unsigned long long _st_stat_recvmsg = 0;
68+
unsigned long long _st_stat_recvmsg_eagain = 0;
69+
unsigned long long _st_stat_sendmsg = 0;
70+
unsigned long long _st_stat_sendmsg_eagain = 0;
71+
unsigned long long _st_stat_sendmmsg = 0;
72+
unsigned long long _st_stat_sendmmsg_eagain = 0;
73+
#endif
5574

5675
#if EAGAIN != EWOULDBLOCK
5776
#define _IO_NOT_READY_ERROR ((errno == EAGAIN) || (errno == EWOULDBLOCK))
@@ -437,12 +456,21 @@ int st_connect(_st_netfd_t *fd, const struct sockaddr *addr, int addrlen, st_uti
437456
ssize_t st_read(_st_netfd_t *fd, void *buf, size_t nbyte, st_utime_t timeout)
438457
{
439458
ssize_t n;
459+
460+
#ifdef DEBUG
461+
++_st_stat_read;
462+
#endif
440463

441464
while ((n = read(fd->osfd, buf, nbyte)) < 0) {
442465
if (errno == EINTR)
443466
continue;
444467
if (!_IO_NOT_READY_ERROR)
445468
return -1;
469+
470+
#ifdef DEBUG
471+
++_st_stat_read_eagain;
472+
#endif
473+
446474
/* Wait until the socket becomes readable */
447475
if (st_netfd_poll(fd, POLLIN, timeout) < 0)
448476
return -1;
@@ -470,12 +498,21 @@ int st_read_resid(_st_netfd_t *fd, void *buf, size_t *resid, st_utime_t timeout)
470498
ssize_t st_readv(_st_netfd_t *fd, const struct iovec *iov, int iov_size, st_utime_t timeout)
471499
{
472500
ssize_t n;
501+
502+
#ifdef DEBUG
503+
++_st_stat_readv;
504+
#endif
473505

474506
while ((n = readv(fd->osfd, iov, iov_size)) < 0) {
475507
if (errno == EINTR)
476508
continue;
477509
if (!_IO_NOT_READY_ERROR)
478510
return -1;
511+
512+
#ifdef DEBUG
513+
++_st_stat_readv_eagain;
514+
#endif
515+
479516
/* Wait until the socket becomes readable */
480517
if (st_netfd_poll(fd, POLLIN, timeout) < 0)
481518
return -1;
@@ -572,6 +609,10 @@ ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size, st_uti
572609
nleft = nbyte;
573610
tmp_iov = (struct iovec *) iov; /* we promise not to modify iov */
574611
iov_cnt = iov_size;
612+
613+
#ifdef DEBUG
614+
++_st_stat_writev;
615+
#endif
575616

576617
while (nleft > 0) {
577618
if (iov_cnt == 1) {
@@ -616,6 +657,11 @@ ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size, st_uti
616657
tmp_iov[iov_cnt].iov_len = iov[index].iov_len;
617658
}
618659
}
660+
661+
#ifdef DEBUG
662+
++_st_stat_writev_eagain;
663+
#endif
664+
619665
/* Wait until the socket becomes writable */
620666
if (st_netfd_poll(fd, POLLOUT, timeout) < 0) {
621667
rv = -1;
@@ -633,6 +679,10 @@ ssize_t st_writev(_st_netfd_t *fd, const struct iovec *iov, int iov_size, st_uti
633679
int st_writev_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size, st_utime_t timeout)
634680
{
635681
ssize_t n;
682+
683+
#ifdef DEBUG
684+
++_st_stat_writev;
685+
#endif
636686

637687
while (*iov_size > 0) {
638688
if (*iov_size == 1)
@@ -659,6 +709,11 @@ int st_writev_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size, st_utime
659709
(*iov)->iov_base = (char *) (*iov)->iov_base + n;
660710
(*iov)->iov_len -= n;
661711
}
712+
713+
#ifdef DEBUG
714+
++_st_stat_writev_eagain;
715+
#endif
716+
662717
/* Wait until the socket becomes writable */
663718
if (st_netfd_poll(fd, POLLOUT, timeout) < 0)
664719
return -1;
@@ -674,12 +729,21 @@ int st_writev_resid(_st_netfd_t *fd, struct iovec **iov, int *iov_size, st_utime
674729
int st_recvfrom(_st_netfd_t *fd, void *buf, int len, struct sockaddr *from, int *fromlen, st_utime_t timeout)
675730
{
676731
int n;
677-
732+
733+
#ifdef DEBUG
734+
++_st_stat_recvfrom;
735+
#endif
736+
678737
while ((n = recvfrom(fd->osfd, buf, len, 0, from, (socklen_t *)fromlen)) < 0) {
679738
if (errno == EINTR)
680739
continue;
681740
if (!_IO_NOT_READY_ERROR)
682741
return -1;
742+
743+
#ifdef DEBUG
744+
++_st_stat_recvfrom_eagain;
745+
#endif
746+
683747
/* Wait until the socket becomes readable */
684748
if (st_netfd_poll(fd, POLLIN, timeout) < 0)
685749
return -1;
@@ -692,12 +756,21 @@ int st_recvfrom(_st_netfd_t *fd, void *buf, int len, struct sockaddr *from, int
692756
int st_sendto(_st_netfd_t *fd, const void *msg, int len, const struct sockaddr *to, int tolen, st_utime_t timeout)
693757
{
694758
int n;
759+
760+
#ifdef DEBUG
761+
++_st_stat_sendto;
762+
#endif
695763

696764
while ((n = sendto(fd->osfd, msg, len, 0, to, tolen)) < 0) {
697765
if (errno == EINTR)
698766
continue;
699767
if (!_IO_NOT_READY_ERROR)
700768
return -1;
769+
770+
#ifdef DEBUG
771+
++_st_stat_sendto_eagain;
772+
#endif
773+
701774
/* Wait until the socket becomes writable */
702775
if (st_netfd_poll(fd, POLLOUT, timeout) < 0)
703776
return -1;
@@ -710,12 +783,21 @@ int st_sendto(_st_netfd_t *fd, const void *msg, int len, const struct sockaddr *
710783
int st_recvmsg(_st_netfd_t *fd, struct msghdr *msg, int flags, st_utime_t timeout)
711784
{
712785
int n;
786+
787+
#ifdef DEBUG
788+
++_st_stat_recvmsg;
789+
#endif
713790

714791
while ((n = recvmsg(fd->osfd, msg, flags)) < 0) {
715792
if (errno == EINTR)
716793
continue;
717794
if (!_IO_NOT_READY_ERROR)
718795
return -1;
796+
797+
#ifdef DEBUG
798+
++_st_stat_recvmsg_eagain;
799+
#endif
800+
719801
/* Wait until the socket becomes readable */
720802
if (st_netfd_poll(fd, POLLIN, timeout) < 0)
721803
return -1;
@@ -728,12 +810,21 @@ int st_recvmsg(_st_netfd_t *fd, struct msghdr *msg, int flags, st_utime_t timeou
728810
int st_sendmsg(_st_netfd_t *fd, const struct msghdr *msg, int flags, st_utime_t timeout)
729811
{
730812
int n;
813+
814+
#ifdef DEBUG
815+
++_st_stat_sendmsg;
816+
#endif
731817

732818
while ((n = sendmsg(fd->osfd, msg, flags)) < 0) {
733819
if (errno == EINTR)
734820
continue;
735821
if (!_IO_NOT_READY_ERROR)
736822
return -1;
823+
824+
#ifdef DEBUG
825+
++_st_stat_sendmsg_eagain;
826+
#endif
827+
737828
/* Wait until the socket becomes writable */
738829
if (st_netfd_poll(fd, POLLOUT, timeout) < 0)
739830
return -1;
@@ -749,6 +840,10 @@ int st_sendmmsg(st_netfd_t fd, struct st_mmsghdr *msgvec, unsigned int vlen, int
749840
int left;
750841
struct mmsghdr *p;
751842

843+
#ifdef DEBUG
844+
++_st_stat_sendmmsg;
845+
#endif
846+
752847
left = (int)vlen;
753848
while (left > 0) {
754849
p = (struct mmsghdr*)msgvec + (vlen - left);
@@ -758,6 +853,11 @@ int st_sendmmsg(st_netfd_t fd, struct st_mmsghdr *msgvec, unsigned int vlen, int
758853
continue;
759854
if (!_IO_NOT_READY_ERROR)
760855
break;
856+
857+
#ifdef DEBUG
858+
++_st_stat_sendmmsg_eagain;
859+
#endif
860+
761861
/* Wait until the socket becomes writable */
762862
if (st_netfd_poll(fd, POLLOUT, timeout) < 0)
763863
break;

sched.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@
5252
#include <valgrind/valgrind.h>
5353
#endif
5454

55+
// Global stat.
56+
#ifdef DEBUG
57+
unsigned long long _st_stat_sched_15ms = 0;
58+
unsigned long long _st_stat_sched_20ms = 0;
59+
unsigned long long _st_stat_sched_25ms = 0;
60+
unsigned long long _st_stat_sched_30ms = 0;
61+
unsigned long long _st_stat_sched_35ms = 0;
62+
unsigned long long _st_stat_sched_40ms = 0;
63+
unsigned long long _st_stat_sched_80ms = 0;
64+
unsigned long long _st_stat_sched_160ms = 0;
65+
unsigned long long _st_stat_sched_s = 0;
66+
67+
unsigned long long _st_stat_thread_run = 0;
68+
unsigned long long _st_stat_thread_idle = 0;
69+
#endif
70+
5571

5672
/* Global data */
5773
_st_vp_t _st_this_vp; /* This VP */
@@ -118,10 +134,18 @@ void _st_vp_schedule(void)
118134
_st_thread_t *thread;
119135

120136
if (_ST_RUNQ.next != &_ST_RUNQ) {
137+
#ifdef DEBUG
138+
++_st_stat_thread_run;
139+
#endif
140+
121141
/* Pull thread off of the run queue */
122142
thread = _ST_THREAD_PTR(_ST_RUNQ.next);
123143
_ST_DEL_RUNQ(thread);
124144
} else {
145+
#ifdef DEBUG
146+
++_st_stat_thread_idle;
147+
#endif
148+
125149
/* If there are no threads to run, switch to the idle thread */
126150
thread = _st_this_vp.idle_thread;
127151
}
@@ -482,6 +506,28 @@ void _st_vp_check_clock(void)
482506
now = st_utime();
483507
elapsed = now < _ST_LAST_CLOCK? 0 : now - _ST_LAST_CLOCK; // Might step back.
484508
_ST_LAST_CLOCK = now;
509+
510+
#ifdef DEBUG
511+
if (elapsed <= 10000) {
512+
++_st_stat_sched_15ms;
513+
} else if (elapsed <= 21000) {
514+
++_st_stat_sched_20ms;
515+
} else if (elapsed <= 25000) {
516+
++_st_stat_sched_25ms;
517+
} else if (elapsed <= 30000) {
518+
++_st_stat_sched_30ms;
519+
} else if (elapsed <= 35000) {
520+
++_st_stat_sched_35ms;
521+
} else if (elapsed <= 40000) {
522+
++_st_stat_sched_40ms;
523+
} else if (elapsed <= 80000) {
524+
++_st_stat_sched_80ms;
525+
} else if (elapsed <= 160000) {
526+
++_st_stat_sched_160ms;
527+
} else {
528+
++_st_stat_sched_s;
529+
}
530+
#endif
485531

486532
if (_st_curr_time && now - _st_last_tset > 999000) {
487533
_st_curr_time = time(NULL);

0 commit comments

Comments
 (0)