1 | /* latency.h -- latency monitor API header file |
2 | * See latency.c for more information. |
3 | * |
4 | * ---------------------------------------------------------------------------- |
5 | * |
6 | * Copyright (c) 2014, Salvatore Sanfilippo <antirez at gmail dot com> |
7 | * All rights reserved. |
8 | * |
9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions are met: |
11 | * |
12 | * * Redistributions of source code must retain the above copyright notice, |
13 | * this list of conditions and the following disclaimer. |
14 | * * Redistributions in binary form must reproduce the above copyright |
15 | * notice, this list of conditions and the following disclaimer in the |
16 | * documentation and/or other materials provided with the distribution. |
17 | * * Neither the name of Redis nor the names of its contributors may be used |
18 | * to endorse or promote products derived from this software without |
19 | * specific prior written permission. |
20 | * |
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
25 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
31 | * POSSIBILITY OF SUCH DAMAGE. |
32 | */ |
33 | |
34 | #ifndef __LATENCY_H |
35 | #define __LATENCY_H |
36 | |
37 | #define LATENCY_TS_LEN 160 /* History length for every monitored event. */ |
38 | |
39 | /* Representation of a latency sample: the sampling time and the latency |
40 | * observed in milliseconds. */ |
41 | struct latencySample { |
42 | int32_t time; /* We don't use time_t to force 4 bytes usage everywhere. */ |
43 | uint32_t latency; /* Latency in milliseconds. */ |
44 | }; |
45 | |
46 | /* The latency time series for a given event. */ |
47 | struct latencyTimeSeries { |
48 | int idx; /* Index of the next sample to store. */ |
49 | uint32_t max; /* Max latency observed for this event. */ |
50 | struct latencySample samples[LATENCY_TS_LEN]; /* Latest history. */ |
51 | }; |
52 | |
53 | /* Latency statistics structure. */ |
54 | struct latencyStats { |
55 | uint32_t all_time_high; /* Absolute max observed since latest reset. */ |
56 | uint32_t avg; /* Average of current samples. */ |
57 | uint32_t min; /* Min of current samples. */ |
58 | uint32_t max; /* Max of current samples. */ |
59 | uint32_t mad; /* Mean absolute deviation. */ |
60 | uint32_t samples; /* Number of non-zero samples. */ |
61 | time_t period; /* Number of seconds since first event and now. */ |
62 | }; |
63 | |
64 | void latencyMonitorInit(void); |
65 | void latencyAddSample(const char *event, mstime_t latency); |
66 | |
67 | /* Latency monitoring macros. */ |
68 | |
69 | /* Start monitoring an event. We just set the current time. */ |
70 | #define latencyStartMonitor(var) if (server.latency_monitor_threshold) { \ |
71 | var = mstime(); \ |
72 | } else { \ |
73 | var = 0; \ |
74 | } |
75 | |
76 | /* End monitoring an event, compute the difference with the current time |
77 | * to check the amount of time elapsed. */ |
78 | #define latencyEndMonitor(var) if (server.latency_monitor_threshold) { \ |
79 | var = mstime() - var; \ |
80 | } |
81 | |
82 | /* Add the sample only if the elapsed time is >= to the configured threshold. */ |
83 | #define latencyAddSampleIfNeeded(event,var) \ |
84 | if (server.latency_monitor_threshold && \ |
85 | (var) >= server.latency_monitor_threshold) \ |
86 | latencyAddSample((event),(var)); |
87 | |
88 | /* Remove time from a nested event. */ |
89 | #define latencyRemoveNestedEvent(event_var,nested_var) \ |
90 | event_var += nested_var; |
91 | |
92 | #endif /* __LATENCY_H */ |
93 | |