MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / latencyAddSample

Function latencyAddSample

src/latency.cpp:115–145  ·  view source on GitHub ↗

Add the specified sample to the specified time series "event". * This function is usually called via latencyAddSampleIfNeeded(), that * is a macro that only adds the sample if the latency is higher than * g_pserver->latency_monitor_threshold. */

Source from the content-addressed store, hash-verified

113 * is a macro that only adds the sample if the latency is higher than
114 * g_pserver->latency_monitor_threshold. */
115void latencyAddSample(const char *event, mstime_t latency) {
116 struct latencyTimeSeries *ts = (latencyTimeSeries*)dictFetchValue(g_pserver->latency_events,event);
117 time_t now = time(NULL);
118 int prev;
119
120 /* Create the time series if it does not exist. */
121 if (ts == NULL) {
122 ts = (latencyTimeSeries*)zmalloc(sizeof(*ts), MALLOC_SHARED);
123 ts->idx = 0;
124 ts->max = 0;
125 memset(ts->samples,0,sizeof(ts->samples));
126 dictAdd(g_pserver->latency_events,zstrdup(event),ts);
127 }
128
129 if (latency > ts->max) ts->max = latency;
130
131 /* If the previous sample is in the same second, we update our old sample
132 * if this latency is > of the old one, or just return. */
133 prev = (ts->idx + LATENCY_TS_LEN - 1) % LATENCY_TS_LEN;
134 if (ts->samples[prev].time == now) {
135 if (latency > ts->samples[prev].latency)
136 ts->samples[prev].latency = latency;
137 return;
138 }
139
140 ts->samples[ts->idx].time = time(NULL);
141 ts->samples[ts->idx].latency = latency;
142
143 ts->idx++;
144 if (ts->idx == LATENCY_TS_LEN) ts->idx = 0;
145}
146
147/* Reset data for the specified event, or all the events data if 'event' is
148 * NULL.

Callers 1

RM_LatencyAddSampleFunction · 0.85

Calls 4

dictFetchValueFunction · 0.85
zmallocFunction · 0.85
zstrdupFunction · 0.85
dictAddFunction · 0.70

Tested by

no test coverage detected