tiny optimization in socket.c; on histograms and timers protect the whole operation with mutex
Costa Tsaousis (ktsaou) committed
Apr 28, 2017 at 02:59 UTC
e8451850e369d1c69362be5b56f7a3520953aec7
2 files changed
+27
-37
src/socket.c
+22
-32
@@ -902,34 +902,22 @@ void poll_events(LISTEN_SOCKETS *sockets
902
struct pollfd *pf = &p.fds[i];
903
struct pollinfo *pi = &p.inf[i];
904
int fd = pf->fd;
905
+ short int revents = pf->revents;
906
+ pf->revents = 0;
907
908
if(unlikely(fd == -1)) {
909
debug(D_POLLFD, "POLLFD: LISTENER: ignoring slot %zu, it does not have an fd", i);
910
continue;
911
}
912
911
- if(pf->revents & POLLIN || pf->revents & POLLPRI) {
912
- // receiving data
913
-
914
- debug(D_POLLFD, "POLLFD: LISTENER: processing POLLIN events for slot %zu (events = %d, revents = %d)", i, pf->events, pf->revents);
915
-
916
- pf->revents = 0;
917
-
918
- if(likely(pi->flags & POLLINFO_FLAG_CLIENT_SOCKET)) {
919
- // read data from client TCP socket
920
-
921
- debug(D_POLLFD, "POLLFD: LISTENER: reading data from TCP client slot %zu (fd %d)", i, fd);
913
+ debug(D_POLLFD, "POLLFD: LISTENER: processing events for slot %zu (events = %d, revents = %d)", i, pf->events, revents);
914
923
- if (p.rcv_callback(fd, pi->socktype, pi->data, &pf->events) == -1) {
924
- poll_close_fd(&p, pi);
925
- continue;
926
- }
927
- }
915
+ if(revents & POLLIN || revents & POLLPRI) {
916
+ // receiving data
917
918
if(likely(pi->flags & POLLINFO_FLAG_SERVER_SOCKET)) {
919
// new connection
931
-
932
- debug(D_POLLFD, "POLLFD: LISTENER: accepting connections from slot %zu (fd %d)", i, fd);
920
+ // debug(D_POLLFD, "POLLFD: LISTENER: accepting connections from slot %zu (fd %d)", i, fd);
921
922
switch(pi->socktype) {
923
case SOCK_STREAM: {
@@ -978,15 +966,20 @@ void poll_events(LISTEN_SOCKETS *sockets
966
}
967
}
968
}
981
- }
982
-
983
- if(unlikely(pf->revents & POLLOUT)) {
984
- // sending data
969
986
- debug(D_POLLFD, "POLLFD: LISTENER: processing POLLOUT events for slot %zu (events = %d, revents = %d)", i, pf->events, pf->revents);
970
+ if(likely(pi->flags & POLLINFO_FLAG_CLIENT_SOCKET)) {
971
+ // read data from client TCP socket
972
+ debug(D_POLLFD, "POLLFD: LISTENER: reading data from TCP client slot %zu (fd %d)", i, fd);
973
988
- pf->revents = 0;
974
+ if (p.rcv_callback(fd, pi->socktype, pi->data, &pf->events) == -1) {
975
+ poll_close_fd(&p, pi);
976
+ continue;
977
+ }
978
+ }
979
+ }
980
981
+ if(unlikely(revents & POLLOUT)) {
982
+ // sending data
983
debug(D_POLLFD, "POLLFD: LISTENER: sending data to socket on slot %zu (fd %d)", i, fd);
984
985
if (p.snd_callback(fd, pi->socktype, pi->data, &pf->events) == -1) {
@@ -995,23 +988,20 @@ void poll_events(LISTEN_SOCKETS *sockets
988
}
989
}
990
998
- if(unlikely(pf->revents & POLLERR)) {
999
- debug(D_POLLFD, "POLLFD: LISTENER: processing POLLERR events for slot %zu (events = %d, revents = %d)", i, pf->events, pf->revents);
1000
- error("POLLFD: LISTENER: processing POLLERR events for slot %zu (events = %d, revents = %d)", i, pf->events, pf->revents);
991
+ if(unlikely(revents & POLLERR)) {
992
+ error("POLLFD: LISTENER: processing POLLERR events for slot %zu (events = %d, revents = %d)", i, pf->events, revents);
993
poll_close_fd(&p, pi);
994
continue;
995
}
996
1005
- if(unlikely(pf->revents & POLLHUP)) {
1006
- debug(D_POLLFD, "POLLFD: LISTENER: processing POLLHUP events for slot %zu (events = %d, revents = %d)", i, pf->events, pf->revents);
997
+ if(unlikely(revents & POLLHUP)) {
998
error("POLLFD: LISTENER: processing POLLHUP events for slot %zu (events = %d, revents = %d)", i, pf->events, pf->revents);
999
poll_close_fd(&p, pi);
1000
continue;
1001
}
1002
1012
- if(unlikely(pf->revents & POLLNVAL)) {
1013
- debug(D_POLLFD, "POLLFD: LISTENER: processing POLLNVAP events for slot %zu (events = %d, revents = %d)", i, pf->events, pf->revents);
1014
- error("POLLFD: LISTENER: processing POLLNVAP events for slot %zu (events = %d, revents = %d)", i, pf->events, pf->revents);
1003
+ if(unlikely(revents & POLLNVAL)) {
1004
+ error("POLLFD: LISTENER: processing POLLNVAP events for slot %zu (events = %d, revents = %d)", i, pf->events, revents);
1005
poll_close_fd(&p, pi);
1006
continue;
1007
}
src/statsd.c
+5
-5
@@ -687,7 +687,7 @@ static int statsd_snd_callback(int fd, int socktype, void *data, short int *even
687
(void)socktype;
688
(void)data;
689
(void)events;
690
-
690
+
691
error("STATSD: snd_callback() called, but we never requested to send data to statsd clients.");
692
return -1;
693
}
@@ -910,6 +910,8 @@ static inline void statsd_chart_from_set(STATSD_METRIC *m) {
910
}
911
912
static inline void statsd_chart_from_timer_or_histogram(STATSD_METRIC *m, char *dim, char *family, char *units) {
913
+ netdata_mutex_lock(&m->histogram.mutex);
914
+
915
if(unlikely(!m->st)) {
916
char type[RRD_ID_LENGTH_MAX + 1], id[RRD_ID_LENGTH_MAX + 1];
917
statsd_get_metric_type_and_id(m, type, id, dim, RRD_ID_LENGTH_MAX);
@@ -939,8 +941,6 @@ static inline void statsd_chart_from_timer_or_histogram(STATSD_METRIC *m, char *
941
else rrdset_next(m->st);
942
943
if(m->count && !m->reset) {
942
- netdata_mutex_lock(&m->histogram.mutex);
943
-
944
size_t len = m->histogram.ext->used;
945
long double *series = m->histogram.ext->values;
946
sort_series(series, len);
@@ -949,8 +949,6 @@ static inline void statsd_chart_from_timer_or_histogram(STATSD_METRIC *m, char *
949
m->histogram.ext->last_max = (collected_number)roundl(series[len - 1] * 1000.0);
950
m->last = (collected_number)roundl(average(series, len) * 1000);
951
m->histogram.ext->last_pct = (collected_number)roundl(average(series, (size_t)floor((double)len * statsd.histogram_percentile / 100.0)) * 1000);
952
-
953
- netdata_mutex_unlock(&m->histogram.mutex);
952
}
953
954
m->reset = 1;
@@ -963,6 +961,8 @@ static inline void statsd_chart_from_timer_or_histogram(STATSD_METRIC *m, char *
961
rrddim_set_by_pointer(m->st, m->rd_count, (collected_number)m->events);
962
963
rrdset_done(m->st);
964
+
965
+ netdata_mutex_unlock(&m->histogram.mutex);
966
}
967
968