xref: /freebsd/contrib/unbound/daemon/stats.h (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
1 /*
2  * daemon/stats.h - collect runtime performance indicators.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file describes the data structure used to collect runtime performance
40  * numbers. These 'statistics' may be of interest to the operator.
41  */
42 
43 #ifndef DAEMON_STATS_H
44 #define DAEMON_STATS_H
45 #include "util/timehist.h"
46 #include <ldns/buffer.h>
47 struct worker;
48 struct config_file;
49 struct comm_point;
50 struct comm_reply;
51 struct edns_data;
52 
53 /** number of qtype that is stored for in array */
54 #define STATS_QTYPE_NUM 256
55 /** number of qclass that is stored for in array */
56 #define STATS_QCLASS_NUM 256
57 /** number of rcodes in stats */
58 #define STATS_RCODE_NUM 16
59 /** number of opcodes in stats */
60 #define STATS_OPCODE_NUM 16
61 
62 /** per worker statistics */
63 struct server_stats {
64 	/** number of queries from clients received. */
65 	size_t num_queries;
66 	/** number of queries that had a cache-miss. */
67 	size_t num_queries_missed_cache;
68 	/** number of prefetch queries - cachehits with prefetch */
69 	size_t num_queries_prefetch;
70 
71 	/**
72 	 * Sum of the querylistsize of the worker for
73 	 * every query that missed cache. To calculate average.
74 	 */
75 	size_t sum_query_list_size;
76 	/** max value of query list size reached. */
77 	size_t max_query_list_size;
78 
79 	/** Extended stats below (bool) */
80 	int extended;
81 
82 	/** qtype stats */
83 	size_t qtype[STATS_QTYPE_NUM];
84 	/** bigger qtype values not in array */
85 	size_t qtype_big;
86 	/** qclass stats */
87 	size_t qclass[STATS_QCLASS_NUM];
88 	/** bigger qclass values not in array */
89 	size_t qclass_big;
90 	/** query opcodes */
91 	size_t qopcode[STATS_OPCODE_NUM];
92 	/** number of queries over TCP */
93 	size_t qtcp;
94 	/** number of queries over IPv6 */
95 	size_t qipv6;
96 	/** number of queries with QR bit */
97 	size_t qbit_QR;
98 	/** number of queries with AA bit */
99 	size_t qbit_AA;
100 	/** number of queries with TC bit */
101 	size_t qbit_TC;
102 	/** number of queries with RD bit */
103 	size_t qbit_RD;
104 	/** number of queries with RA bit */
105 	size_t qbit_RA;
106 	/** number of queries with Z bit */
107 	size_t qbit_Z;
108 	/** number of queries with AD bit */
109 	size_t qbit_AD;
110 	/** number of queries with CD bit */
111 	size_t qbit_CD;
112 	/** number of queries with EDNS OPT record */
113 	size_t qEDNS;
114 	/** number of queries with EDNS with DO flag */
115 	size_t qEDNS_DO;
116 	/** answer rcodes */
117 	size_t ans_rcode[STATS_RCODE_NUM];
118 	/** answers with pseudo rcode 'nodata' */
119 	size_t ans_rcode_nodata;
120 	/** answers that were secure (AD) */
121 	size_t ans_secure;
122 	/** answers that were bogus (withheld as SERVFAIL) */
123 	size_t ans_bogus;
124 	/** rrsets marked bogus by validator */
125 	size_t rrset_bogus;
126 	/** unwanted traffic received on server-facing ports */
127 	size_t unwanted_replies;
128 	/** unwanted traffic received on client-facing ports */
129 	size_t unwanted_queries;
130 
131 	/** histogram data exported to array
132 	 * if the array is the same size, no data is lost, and
133 	 * if all histograms are same size (is so by default) then
134 	 * adding up works well. */
135 	size_t hist[NUM_BUCKETS_HIST];
136 };
137 
138 /**
139  * Statistics to send over the control pipe when asked
140  * This struct is made to be memcpied, sent in binary.
141  */
142 struct stats_info {
143 	/** the thread stats */
144 	struct server_stats svr;
145 
146 	/** mesh stats: current number of states */
147 	size_t mesh_num_states;
148 	/** mesh stats: current number of reply (user) states */
149 	size_t mesh_num_reply_states;
150 	/** mesh stats: number of reply states overwritten with a new one */
151 	size_t mesh_jostled;
152 	/** mesh stats: number of incoming queries dropped */
153 	size_t mesh_dropped;
154 	/** mesh stats: replies sent */
155 	size_t mesh_replies_sent;
156 	/** mesh stats: sum of waiting times for the replies */
157 	struct timeval mesh_replies_sum_wait;
158 	/** mesh stats: median of waiting times for replies (in sec) */
159 	double mesh_time_median;
160 };
161 
162 /**
163  * Initialize server stats to 0.
164  * @param stats: what to init (this is alloced by the caller).
165  * @param cfg: with extended statistics option.
166  */
167 void server_stats_init(struct server_stats* stats, struct config_file* cfg);
168 
169 /** add query if it missed the cache */
170 void server_stats_querymiss(struct server_stats* stats, struct worker* worker);
171 
172 /** add query if was cached and also resulted in a prefetch */
173 void server_stats_prefetch(struct server_stats* stats, struct worker* worker);
174 
175 /** display the stats to the log */
176 void server_stats_log(struct server_stats* stats, struct worker* worker,
177 	int threadnum);
178 
179 /**
180  * Obtain the stats info for a given thread. Uses pipe to communicate.
181  * @param worker: the worker that is executing (the first worker).
182  * @param who: on who to get the statistics info.
183  * @param s: the stats block to fill in.
184  * @param reset: if stats can be reset.
185  */
186 void server_stats_obtain(struct worker* worker, struct worker* who,
187 	struct stats_info* s, int reset);
188 
189 /**
190  * Compile stats into structure for this thread worker.
191  * Also clears the statistics counters (if that is set by config file).
192  * @param worker: the worker to compile stats for, also the executing worker.
193  * @param s: stats block.
194  * @param reset: if true, depending on config stats are reset.
195  * 	if false, statistics are not reset.
196  */
197 void server_stats_compile(struct worker* worker, struct stats_info* s,
198 	int reset);
199 
200 /**
201  * Send stats over comm tube in reply to query cmd
202  * @param worker: this worker.
203  * @param reset: if true, depending on config stats are reset.
204  * 	if false, statistics are not reset.
205  */
206 void server_stats_reply(struct worker* worker, int reset);
207 
208 /**
209  * Addup stat blocks.
210  * @param total: sum of the two entries.
211  * @param a: to add to it.
212  */
213 void server_stats_add(struct stats_info* total, struct stats_info* a);
214 
215 /**
216  * Add stats for this query
217  * @param stats: the stats
218  * @param c: commpoint with type and buffer.
219  * @param qtype: query type
220  * @param qclass: query class
221  * @param edns: edns record
222  * @param repinfo: reply info with remote address
223  */
224 void server_stats_insquery(struct server_stats* stats, struct comm_point* c,
225 	uint16_t qtype, uint16_t qclass, struct edns_data* edns,
226 	struct comm_reply* repinfo);
227 
228 /**
229  * Add rcode for this query.
230  * @param stats: the stats
231  * @param buf: buffer with rcode. If buffer is length0: not counted.
232  */
233 void server_stats_insrcode(struct server_stats* stats, ldns_buffer* buf);
234 
235 #endif /* DAEMON_STATS_H */
236