]>
Commit | Line | Data |
---|---|---|
17c0af35 AJ |
1 | /* |
2 | * Copyright (C) 1996-2023 The Squid Software Foundation and contributors | |
3 | * | |
4 | * Squid software is distributed under GPLv2+ license and includes | |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
7 | */ | |
8 | ||
ff9d9458 FC |
9 | #ifndef SQUID_SRC_COMM_INCOMING_H |
10 | #define SQUID_SRC_COMM_INCOMING_H | |
17c0af35 AJ |
11 | |
12 | #if USE_POLL || USE_SELECT | |
13 | ||
14 | #include "SquidConfig.h" | |
15 | #include "StatHist.h" | |
16 | ||
17 | namespace Comm | |
18 | { | |
19 | ||
20 | /** | |
21 | * Automatic tuning for incoming requests. | |
22 | * | |
23 | * INCOMING sockets are the listening ports for transport protocols. | |
24 | * We need to check these fairly regularly, but how often? When the | |
25 | * load increases, we want to check the incoming sockets more often. | |
26 | * If we have a lot of one protocol incoming, then we need to check | |
27 | * those sockets more than others. | |
28 | * | |
29 | * \copydoc Comm::Incoming::check() | |
30 | * | |
31 | * \copydoc Comm::Incoming::finishPolling() | |
32 | * | |
33 | * Caveats: | |
34 | * | |
35 | * \copydoc Comm::Incoming::Factor | |
36 | * | |
37 | * \copydoc Comm::Incoming::MaxInterval | |
38 | */ | |
39 | class Incoming | |
40 | { | |
41 | public: | |
42 | #if !defined(INCOMING_FACTOR) | |
43 | #define INCOMING_FACTOR 5 | |
44 | #endif | |
45 | /** | |
46 | * The higher the INCOMING_FACTOR, the slower the algorithm will | |
47 | * respond to load spikes/increases/decreases in demand. A value | |
48 | * between 3 and 8 is recommended. | |
49 | */ | |
50 | static const int Factor = INCOMING_FACTOR; | |
51 | ||
52 | /** | |
53 | * Magic upper limit on interval. | |
54 | * At the largest value the cache will effectively be idling. | |
55 | */ | |
56 | static const int MaxInterval = (256 << Factor); | |
57 | ||
58 | // TODO replace with constructor initialization | |
59 | void init(int n) { nMaximum = n; history.enumInit(n); } | |
60 | ||
61 | /** | |
62 | * Preparation for polling incoming sockets. | |
63 | * | |
64 | * \param n the number of relevant listening FDs currently open. | |
65 | * | |
66 | * \return whether it is possible to check with poll(2)/select(2). | |
67 | */ | |
68 | bool startPolling(int n) { ioEvents = 0; return (n > 0); } | |
69 | ||
70 | /** | |
71 | * Finalize and update records when incoming sockets polled. | |
72 | * | |
73 | * The new interval is calculated as the current interval, | |
74 | * plus what we would like to see as an average number of events, | |
75 | * minus the number of events just processed. | |
76 | */ | |
77 | void finishPolling(int, SquidConfig::CommIncoming::Measure &); | |
78 | ||
79 | /** | |
80 | * Every time we check incoming sockets, we count how many new | |
81 | * messages or connections were processed. This is used to adjust | |
82 | * the interval for the next iteration. | |
83 | * | |
84 | * \return whether it is time to check incoming sockets. | |
85 | */ | |
86 | bool check() { return (++ioEvents > (interval >> Factor)); } | |
87 | ||
88 | /* | |
89 | * How many normal I/O events to process before checking | |
90 | * incoming sockets again. | |
91 | * | |
92 | * \note We store the interval multiplied by a factor of | |
93 | * (2^Factor) to have some pseudo-floating | |
94 | * point precision. | |
95 | */ | |
96 | int interval = (16 << Factor); | |
97 | ||
98 | /** History of I/O events timing on listening ports. | |
99 | * | |
100 | * You can see the current values of the interval's, | |
101 | * as well as histograms of 'incoming_events' in the cache | |
102 | * manager 'comm_*_incoming' reports. | |
103 | */ | |
104 | StatHist history; | |
105 | ||
106 | private: | |
107 | /** | |
108 | * Count of normal I/O events processed since last call to | |
109 | * startPolling(). When ioEvents > interval, it is time to | |
110 | * check incoming sockets again. | |
111 | */ | |
112 | int ioEvents = 0; | |
113 | ||
114 | /** | |
115 | * Maximum value to record for number of I/O events within | |
116 | * an interval. Set using init(N). | |
117 | */ | |
118 | int nMaximum = 0; | |
119 | }; | |
120 | ||
121 | } // namespace Comm | |
122 | ||
123 | #endif /* USE_POLL || USE_SELECT */ | |
ff9d9458 | 124 | #endif /* SQUID_SRC_COMM_INCOMING_H */ |