reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
GeneralCongestionManager.cxx
Go to the documentation of this file.
2
4#include "rutil/Lock.hxx"
5#include "rutil/Logger.hxx"
6
7#define RESIPROCATE_SUBSYSTEM Subsystem::STATS
8
9namespace resip
10{
11GeneralCongestionManager::GeneralCongestionManager(MetricType defaultMetric,
12 uint32_t defaultMaxTolerance) :
13 mDefaultMetric(defaultMetric),
14 mDefaultMaxTolerance(defaultMaxTolerance)
15{
16 // !bwc! TODO allow these to be configured.
17 mRejectionThresholds[NORMAL]=0;
18 mRejectionThresholds[REJECTING_NEW_WORK]=80;
19 mRejectionThresholds[REJECTING_NON_ESSENTIAL]=100;
20}
21
24
25void
27 MetricType metric,
28 uint32_t maxTolerance)
29{
30 Lock lock(mFifosMutex);
31 FifoInfo info;
32 info.fifo=fifo;
33 info.metric=metric;
34 info.maxTolerance=maxTolerance;
35 mFifos.push_back(info);
36 fifo->setRole((uint8_t)mFifos.size()-1);
37}
38
39void
41{
42 Lock lock(mFifosMutex);
43 if(fifo->getRole() < mFifos.size())
44 {
45 mFifos[fifo->getRole()].fifo=0;
46 }
47}
48
49bool
51 const resip::Data& fifoDescription,
52 MetricType metric,
53 uint32_t maxTolerance )
54{
55 Lock lock(mFifosMutex);
56 for(std::vector<FifoInfo>::iterator i=mFifos.begin(); i!=mFifos.end(); ++i)
57 {
58 if(i->fifo && // ensure fifo isn't 0'd out from unregister call
59 (fifoDescription.empty() || isEqualNoCase(i->fifo->getDescription(), fifoDescription)))
60 {
61 i->maxTolerance=UINT_MAX; // Set temporarily to UINT_MAX, so that we don't inadvertantly reject a request while the metric and tolerance are being changed.
62 i->metric=metric;
63 i->maxTolerance=maxTolerance;
64 if(!fifoDescription.empty()) return true;
65 }
66 }
67 return false || fifoDescription.empty();
68}
69
72{
73 Lock lock(mFifosMutex);
74 return getRejectionBehaviorInternal(fifo);
75}
76
78GeneralCongestionManager::getRejectionBehaviorInternal(const FifoStatsInterface *fifo) const
79{
80 // !bwc! We need to also keep an eye on memory usage, and push back if it
81 // looks like we're going to start hitting swap sometime soon.
82
83 uint16_t percent=getCongestionPercentInternal(fifo);
84
85 // .bwc. We exit this sooner the more congested we are.
86 if(percent > mRejectionThresholds[REJECTING_NON_ESSENTIAL])
87 {
89 }
90 else if(percent > mRejectionThresholds[REJECTING_NEW_WORK])
91 {
92 return REJECTING_NEW_WORK;
93 }
94 else
95 {
96 return NORMAL;
97 }
98}
99
100void
102{
103 Lock lock(mFifosMutex);
104 WarningLog(<<"FIFO STATISTICS");
105 for(std::vector<FifoInfo>::const_iterator i=mFifos.begin();
106 i!=mFifos.end();++i)
107 {
108 if(i->fifo)
109 {
110 const FifoStatsInterface& fifo=*(i->fifo);
111 Data buffer;
112 DataStream strm(buffer);
113 encodeFifoStats(fifo, strm);
114 WarningLog(<< buffer);
115 }
116 }
117}
118
121{
122 Lock lock(mFifosMutex);
123 for(std::vector<FifoInfo>::const_iterator i=mFifos.begin();
124 i!=mFifos.end();++i)
125 {
126 if(i->fifo)
127 {
128 const FifoStatsInterface& fifo=*(i->fifo);
129 encodeFifoStats(fifo, strm);
130 strm << std::endl;
131 }
132 }
133 strm.flush();
134 return strm;
135}
136
139{
140 Lock lock(mFifosMutex);
141 return getCongestionPercentInternal(fifo);
142}
143
145GeneralCongestionManager::getCongestionPercentInternal(const FifoStatsInterface* fifo) const
146{
147 if(fifo->getRole() >= mFifos.size())
148 {
149 resip_assert(0);
150 return 0;
151 }
152
153 const FifoInfo& info = mFifos[fifo->getRole()];
154 resip_assert(info.fifo==fifo);
155 switch(info.metric)
156 {
157 case SIZE:
158 return resipIntDiv(100*(uint16_t)fifo->getCountDepth(),info.maxTolerance);
159 case TIME_DEPTH:
160 return resipIntDiv(100*(uint32_t)(fifo->getTimeDepth()),info.maxTolerance);
161 case WAIT_TIME:
162 return resipIntDiv(100*(uint32_t)(fifo->expectedWaitTimeMilliSec()),info.maxTolerance);
163 default:
164 resip_assert(0);
165 return 0;
166 }
167 return 0;
168}
169
171GeneralCongestionManager::encodeFifoStats(const FifoStatsInterface& fifoStats, EncodeStream& strm) const
172{
173 CongestionManager::RejectionBehavior behavior = getRejectionBehaviorInternal(&fifoStats);
174 const FifoInfo& info = mFifos[fifoStats.getRole()];
175 strm <<fifoStats.getDescription()
176 << ": Size=" << fifoStats.getCountDepth()
177 << " TimeDepth(sec)=" << fifoStats.getTimeDepth()
178 << " ExpWait(msec)=" << fifoStats.expectedWaitTimeMilliSec()
179 << " AvgSvcTime(usec)="
180 << fifoStats.averageServiceTimeMicroSec()
181 << " Metric="
182 << (info.metric == WAIT_TIME ? "WAIT_TIME" :
183 info.metric == TIME_DEPTH ? "TIME_DEPTH" :
184 "SIZE")
185 << " MaxTolerance="
186 << info.maxTolerance
187 << " CurBehavior="
188 << (behavior == NORMAL ? "NORMAL" :
189 behavior == REJECTING_NEW_WORK ? "REJECTING_NEW_WORK" :
190 "REJECTING_NON_ESSENTIAL");
191 strm.flush();
192 return strm;
193}
194
195}
196
197/* ====================================================================
198 * The Vovida Software License, Version 1.0
199 *
200 * Redistribution and use in source and binary forms, with or without
201 * modification, are permitted provided that the following conditions
202 * are met:
203 *
204 * 1. Redistributions of source code must retain the above copyright
205 * notice, this list of conditions and the following disclaimer.
206 *
207 * 2. Redistributions in binary form must reproduce the above copyright
208 * notice, this list of conditions and the following disclaimer in
209 * the documentation and/or other materials provided with the
210 * distribution.
211 *
212 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
213 * and "Vovida Open Communication Application Library (VOCAL)" must
214 * not be used to endorse or promote products derived from this
215 * software without prior written permission. For written
216 * permission, please contact vocal@vovida.org.
217 *
218 * 4. Products derived from this software may not be called "VOCAL", nor
219 * may "VOCAL" appear in their name, without prior written
220 * permission of Vovida Networks, Inc.
221 *
222 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
223 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
224 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
225 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
226 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
227 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
228 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
229 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
230 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
231 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
233 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
234 * DAMAGE.
235 *
236 * ====================================================================
237 *
238 * This software consists of voluntary contributions made by Vovida
239 * Networks, Inc. and many individuals on behalf of Vovida Networks,
240 * Inc. For more information on Vovida Networks, Inc., please see
241 * <http://www.vovida.org/>.
242 *
243 */
#define WarningLog(args_)
Definition Logger.hxx:134
#define resip_assert(x)
This file needs to work for both C and C++ code.
@ REJECTING_NEW_WORK
Overloaded, should refuse new work.
@ NORMAL
Not rejecting any work.
@ REJECTING_NON_ESSENTIAL
Rejecting all work that is non-essential to the health of the system (ie, if dropping something is li...
An iostream that operates on an existing Data.
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
bool empty() const noexcept
Checks whether the Data is empty.
Definition Data.hxx:591
Interface for providing metrics on FIFOs, primarily used by CongestionManager.
virtual time_t getTimeDepth() const =0
Returns the difference in time between the youngest and oldest item in the FIFO in seconds.
virtual size_t getCountDepth() const =0
Returns the number of elements in the FIFO.
virtual time_t expectedWaitTimeMilliSec() const =0
Returns the expected time it will take to service all messages currently in the queue (in milli-secon...
void setRole(uint8_t role)
virtual EncodeStream & encodeCurrentState(EncodeStream &strm) const
Get the current state of all monitored fifos.to a stream.
virtual void logCurrentState() const
Log the current state of all monitored fifos.
virtual void unregisterFifo(resip::FifoStatsInterface *fifo)
Remove a fifo from the collection of fifos monitored by this CongestionManager.
virtual RejectionBehavior getRejectionBehavior(const FifoStatsInterface *fifo) const
Gets the current RejectionBehavior for this fifo, based only on the congestion-state of this fifo (ie...
virtual uint16_t getCongestionPercent(const FifoStatsInterface *fifo) const
Returns the percent of maximum tolerances that this queue is at.
virtual void registerFifo(resip::FifoStatsInterface *fifo, MetricType metric, uint32_t maxTolerance)
Add a fifo to the collection of fifos monitored by this CongestionManager, with a specified metric an...
MetricType
Enumeration of congestion metric types this class supports.
@ WAIT_TIME
The expected service time (the time it takes for new items to be serviced) This is the recommended me...
@ SIZE
The current size of the fifo.
@ TIME_DEPTH
The age of the oldest item in the fifo.
virtual bool updateFifoTolerances(const resip::Data &fifoDescription, MetricType metric, uint32_t maxTolerance)
Update the metric type and tolerances of a given fifo that the GeneralCongestionManager is already aw...
dcm! – add UnusedChecking(_enum) below;
bool isEqualNoCase(const Data &left, const Data &right)
Definition Data.hxx:1116
const _Tp1 resipIntDiv(const _Tp1 &__a, const _Tp2 &__b)
Definition compat.hxx:206
unsigned short uint16_t
Definition Stun.hxx:30
std::unique_lock< Mutex > Lock
A convenience class to lock a Lockable (such as a Mutex) on object creation, and unlock on destructio...
Definition Lock.hxx:28
unsigned int uint32_t
Definition Stun.hxx:34
unsigned char uint8_t
Definition Stun.hxx:29
#define EncodeStream
unsigned short uint16_t
Definition stdint.h:84
unsigned int uint32_t
Definition stdint.h:85