1238106Sdes/*
2238106Sdes * daemon/stats.h - collect runtime performance indicators.
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * This file describes the data structure used to collect runtime performance
40238106Sdes * numbers. These 'statistics' may be of interest to the operator.
41238106Sdes */
42238106Sdes
43238106Sdes#ifndef DAEMON_STATS_H
44238106Sdes#define DAEMON_STATS_H
45238106Sdes#include "util/timehist.h"
46238106Sdesstruct worker;
47238106Sdesstruct config_file;
48238106Sdesstruct comm_point;
49238106Sdesstruct comm_reply;
50238106Sdesstruct edns_data;
51269257Sdesstruct sldns_buffer;
52238106Sdes
53238106Sdes/** number of qtype that is stored for in array */
54238106Sdes#define STATS_QTYPE_NUM 256
55238106Sdes/** number of qclass that is stored for in array */
56238106Sdes#define STATS_QCLASS_NUM 256
57238106Sdes/** number of rcodes in stats */
58238106Sdes#define STATS_RCODE_NUM 16
59238106Sdes/** number of opcodes in stats */
60238106Sdes#define STATS_OPCODE_NUM 16
61238106Sdes
62238106Sdes/** per worker statistics */
63238106Sdesstruct server_stats {
64238106Sdes	/** number of queries from clients received. */
65238106Sdes	size_t num_queries;
66238106Sdes	/** number of queries that had a cache-miss. */
67238106Sdes	size_t num_queries_missed_cache;
68238106Sdes	/** number of prefetch queries - cachehits with prefetch */
69238106Sdes	size_t num_queries_prefetch;
70238106Sdes
71238106Sdes	/**
72238106Sdes	 * Sum of the querylistsize of the worker for
73238106Sdes	 * every query that missed cache. To calculate average.
74238106Sdes	 */
75238106Sdes	size_t sum_query_list_size;
76238106Sdes	/** max value of query list size reached. */
77238106Sdes	size_t max_query_list_size;
78238106Sdes
79238106Sdes	/** Extended stats below (bool) */
80238106Sdes	int extended;
81238106Sdes
82238106Sdes	/** qtype stats */
83238106Sdes	size_t qtype[STATS_QTYPE_NUM];
84238106Sdes	/** bigger qtype values not in array */
85238106Sdes	size_t qtype_big;
86238106Sdes	/** qclass stats */
87238106Sdes	size_t qclass[STATS_QCLASS_NUM];
88238106Sdes	/** bigger qclass values not in array */
89238106Sdes	size_t qclass_big;
90238106Sdes	/** query opcodes */
91238106Sdes	size_t qopcode[STATS_OPCODE_NUM];
92238106Sdes	/** number of queries over TCP */
93238106Sdes	size_t qtcp;
94238106Sdes	/** number of queries over IPv6 */
95238106Sdes	size_t qipv6;
96238106Sdes	/** number of queries with QR bit */
97238106Sdes	size_t qbit_QR;
98238106Sdes	/** number of queries with AA bit */
99238106Sdes	size_t qbit_AA;
100238106Sdes	/** number of queries with TC bit */
101238106Sdes	size_t qbit_TC;
102238106Sdes	/** number of queries with RD bit */
103238106Sdes	size_t qbit_RD;
104238106Sdes	/** number of queries with RA bit */
105238106Sdes	size_t qbit_RA;
106238106Sdes	/** number of queries with Z bit */
107238106Sdes	size_t qbit_Z;
108238106Sdes	/** number of queries with AD bit */
109238106Sdes	size_t qbit_AD;
110238106Sdes	/** number of queries with CD bit */
111238106Sdes	size_t qbit_CD;
112238106Sdes	/** number of queries with EDNS OPT record */
113238106Sdes	size_t qEDNS;
114238106Sdes	/** number of queries with EDNS with DO flag */
115238106Sdes	size_t qEDNS_DO;
116238106Sdes	/** answer rcodes */
117238106Sdes	size_t ans_rcode[STATS_RCODE_NUM];
118238106Sdes	/** answers with pseudo rcode 'nodata' */
119238106Sdes	size_t ans_rcode_nodata;
120238106Sdes	/** answers that were secure (AD) */
121238106Sdes	size_t ans_secure;
122238106Sdes	/** answers that were bogus (withheld as SERVFAIL) */
123238106Sdes	size_t ans_bogus;
124238106Sdes	/** rrsets marked bogus by validator */
125238106Sdes	size_t rrset_bogus;
126238106Sdes	/** unwanted traffic received on server-facing ports */
127238106Sdes	size_t unwanted_replies;
128238106Sdes	/** unwanted traffic received on client-facing ports */
129238106Sdes	size_t unwanted_queries;
130238106Sdes
131238106Sdes	/** histogram data exported to array
132238106Sdes	 * if the array is the same size, no data is lost, and
133238106Sdes	 * if all histograms are same size (is so by default) then
134238106Sdes	 * adding up works well. */
135238106Sdes	size_t hist[NUM_BUCKETS_HIST];
136238106Sdes};
137238106Sdes
138238106Sdes/**
139238106Sdes * Statistics to send over the control pipe when asked
140238106Sdes * This struct is made to be memcpied, sent in binary.
141238106Sdes */
142238106Sdesstruct stats_info {
143238106Sdes	/** the thread stats */
144238106Sdes	struct server_stats svr;
145238106Sdes
146238106Sdes	/** mesh stats: current number of states */
147238106Sdes	size_t mesh_num_states;
148238106Sdes	/** mesh stats: current number of reply (user) states */
149238106Sdes	size_t mesh_num_reply_states;
150238106Sdes	/** mesh stats: number of reply states overwritten with a new one */
151238106Sdes	size_t mesh_jostled;
152238106Sdes	/** mesh stats: number of incoming queries dropped */
153238106Sdes	size_t mesh_dropped;
154238106Sdes	/** mesh stats: replies sent */
155238106Sdes	size_t mesh_replies_sent;
156238106Sdes	/** mesh stats: sum of waiting times for the replies */
157238106Sdes	struct timeval mesh_replies_sum_wait;
158238106Sdes	/** mesh stats: median of waiting times for replies (in sec) */
159238106Sdes	double mesh_time_median;
160238106Sdes};
161238106Sdes
162238106Sdes/**
163238106Sdes * Initialize server stats to 0.
164238106Sdes * @param stats: what to init (this is alloced by the caller).
165238106Sdes * @param cfg: with extended statistics option.
166238106Sdes */
167238106Sdesvoid server_stats_init(struct server_stats* stats, struct config_file* cfg);
168238106Sdes
169238106Sdes/** add query if it missed the cache */
170238106Sdesvoid server_stats_querymiss(struct server_stats* stats, struct worker* worker);
171238106Sdes
172238106Sdes/** add query if was cached and also resulted in a prefetch */
173238106Sdesvoid server_stats_prefetch(struct server_stats* stats, struct worker* worker);
174238106Sdes
175238106Sdes/** display the stats to the log */
176238106Sdesvoid server_stats_log(struct server_stats* stats, struct worker* worker,
177238106Sdes	int threadnum);
178238106Sdes
179238106Sdes/**
180238106Sdes * Obtain the stats info for a given thread. Uses pipe to communicate.
181238106Sdes * @param worker: the worker that is executing (the first worker).
182238106Sdes * @param who: on who to get the statistics info.
183238106Sdes * @param s: the stats block to fill in.
184238106Sdes * @param reset: if stats can be reset.
185238106Sdes */
186238106Sdesvoid server_stats_obtain(struct worker* worker, struct worker* who,
187238106Sdes	struct stats_info* s, int reset);
188238106Sdes
189238106Sdes/**
190238106Sdes * Compile stats into structure for this thread worker.
191238106Sdes * Also clears the statistics counters (if that is set by config file).
192238106Sdes * @param worker: the worker to compile stats for, also the executing worker.
193238106Sdes * @param s: stats block.
194238106Sdes * @param reset: if true, depending on config stats are reset.
195238106Sdes * 	if false, statistics are not reset.
196238106Sdes */
197238106Sdesvoid server_stats_compile(struct worker* worker, struct stats_info* s,
198238106Sdes	int reset);
199238106Sdes
200238106Sdes/**
201238106Sdes * Send stats over comm tube in reply to query cmd
202238106Sdes * @param worker: this worker.
203238106Sdes * @param reset: if true, depending on config stats are reset.
204238106Sdes * 	if false, statistics are not reset.
205238106Sdes */
206238106Sdesvoid server_stats_reply(struct worker* worker, int reset);
207238106Sdes
208238106Sdes/**
209238106Sdes * Addup stat blocks.
210238106Sdes * @param total: sum of the two entries.
211238106Sdes * @param a: to add to it.
212238106Sdes */
213238106Sdesvoid server_stats_add(struct stats_info* total, struct stats_info* a);
214238106Sdes
215238106Sdes/**
216238106Sdes * Add stats for this query
217238106Sdes * @param stats: the stats
218238106Sdes * @param c: commpoint with type and buffer.
219238106Sdes * @param qtype: query type
220238106Sdes * @param qclass: query class
221238106Sdes * @param edns: edns record
222238106Sdes * @param repinfo: reply info with remote address
223238106Sdes */
224238106Sdesvoid server_stats_insquery(struct server_stats* stats, struct comm_point* c,
225238106Sdes	uint16_t qtype, uint16_t qclass, struct edns_data* edns,
226238106Sdes	struct comm_reply* repinfo);
227238106Sdes
228238106Sdes/**
229238106Sdes * Add rcode for this query.
230238106Sdes * @param stats: the stats
231238106Sdes * @param buf: buffer with rcode. If buffer is length0: not counted.
232238106Sdes */
233269257Sdesvoid server_stats_insrcode(struct server_stats* stats, struct sldns_buffer* buf);
234238106Sdes
235238106Sdes#endif /* DAEMON_STATS_H */
236