EMANE  1.2.1
wmmmanager.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013,2016 - Adjacent Link, LLC, Bridgewater New Jersey
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * * Neither the name of Adjacent Link LLC nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "wmmmanager.h"
34 #include "maclayer.h"
35 #include "utils.h"
36 
38  PlatformServiceProvider * pPlatformService,
39  MACLayer *pMACLayer):
40  id_{id},
41  pPlatformService_{pPlatformService},
42  pMACLayler_{pMACLayer},
43  u8NumCategories_{1}
44 {
45  totalUtilizationVector_.resize(u8NumCategories_);
46  localUtilizationVector_.resize(u8NumCategories_);
47 
48  // reset
49  resetCounters();
50 }
51 
52 
53 
55 { }
56 
57 
58 
59 
60 void
62  const Microseconds & durationMicroseconds)
63 {
64  if(u8Category > u8NumCategories_)
65  {
66  LOGGER_VERBOSE_LOGGING(pPlatformService_->logService(),
68  "MACI %03hu %s::%s: invalid category %hhu, max is %hhu, ignore",
69  id_,
70  "WMMManager",
71  __func__,
72  u8Category,
73  u8NumCategories_);
74  }
75  else
76  {
77  // total sum for this index
78  totalUtilizationVector_[u8Category] += durationMicroseconds;
79 
80  // save the total
81  totalUtilizationMicroseconds_ += durationMicroseconds;
82 
83  LOGGER_VERBOSE_LOGGING(pPlatformService_->logService(),
85  "MACI %03hu %s::%s: category %hhu, duration %lf",
86  id_,
87  "WMMManager",
88  __func__,
89  u8Category,
90  std::chrono::duration_cast<DoubleSeconds>(durationMicroseconds).count());
91  }
92 }
93 
94 
95 
96 void
98  const Microseconds & durationMicroseconds)
99 {
100  if(u8Category > u8NumCategories_)
101  {
102  LOGGER_VERBOSE_LOGGING(pPlatformService_->logService(),
103  ERROR_LEVEL,
104  "MACI %03hu %s::%s: invalid category %hhu, max is %hhu, ignore",
105  id_,
106  "WMMManager",
107  __func__,
108  u8Category,
109  u8NumCategories_);
110  }
111  else
112  {
113  // local sum for this index
114  localUtilizationVector_[u8Category] += durationMicroseconds;
115 
116  // total sum for this index
117  totalUtilizationVector_[u8Category] += durationMicroseconds;
118 
119  // save the total
120  totalUtilizationMicroseconds_ += durationMicroseconds;
121 
122  LOGGER_VERBOSE_LOGGING(pPlatformService_->logService(),
123  DEBUG_LEVEL,
124  "MACI %03hu %s::%s: category %hhu, duration %lf",
125  id_,
126  "WMMManager",
127  __func__,
128  u8Category,
129  std::chrono::duration_cast<DoubleSeconds>(durationMicroseconds).count());
130  }
131 }
132 
133 
134 
135 void
137 {
138  if(u8NumCategories_ != u8NumCategories)
139  {
140  LOGGER_VERBOSE_LOGGING(pPlatformService_->logService(),
141  DEBUG_LEVEL,
142  "MACI %03hu %s::%s: change num categories from %hhu to %hhu",
143  id_,
144  "WMMManager",
145  __func__,
146  u8NumCategories_,
147  u8NumCategories);
148 
149  // resize
150  totalUtilizationVector_.resize(u8NumCategories);
151  localUtilizationVector_.resize(u8NumCategories);
152 
153  u8NumCategories_ = u8NumCategories;
154 
155  // reset ALL values
156  resetCounters();
157  }
158 }
159 
160 
161 void
162 EMANE::Models::IEEE80211ABG::WMMManager::resetCounters()
163 {
164  for(auto & iter : totalUtilizationVector_)
165  {
166  iter = Microseconds::zero();
167  }
168 
169  for(auto & iter : localUtilizationVector_)
170  {
171  iter = Microseconds::zero();
172  }
173 
174  totalUtilizationMicroseconds_ = Microseconds::zero();
175 }
176 
177 
178 
181 {
182  // initialize to <total = 0.0, local = 0.0>
183  UtilizationRatioVector vec(u8NumCategories_, UtilizationRatioPair(0.0f, 0.0f));
184 
185  // check for divide by 0
186  if((totalUtilizationMicroseconds_ > Microseconds::zero()) && (deltaTMicroseconds > Microseconds::zero()))
187  {
188  // get activity ratio (used / interval)
189  float fActivityRatio{getRatio(totalUtilizationMicroseconds_, deltaTMicroseconds)};
190 
191  // clamp it
192  if(fActivityRatio > 1.0f)
193  {
194  fActivityRatio = 1.0f;
195  }
196 
197  // get the utilization ratio(s)
198  for(std::uint8_t u8Category = 0; u8Category < u8NumCategories_; ++u8Category)
199  {
200  // set total ratio
201  vec[u8Category].first = getRatio(totalUtilizationVector_[u8Category], totalUtilizationMicroseconds_) * fActivityRatio;
202 
203  // check for divide by 0
204  if(totalUtilizationVector_[u8Category] > Microseconds::zero())
205  {
206  // set local
207  vec[u8Category].second = getRatio(localUtilizationVector_[u8Category], totalUtilizationVector_[u8Category]);
208  }
209  else
210  {
211  vec[u8Category].second = 0.0f;
212  }
213 
214  LOGGER_VERBOSE_LOGGING(pPlatformService_->logService(),
215  DEBUG_LEVEL,
216  "MACI %03hu %s::%s: category %hhu, [total_bw %lf, ratio %f], "
217  "usage [total %f, %3.2f%%]",
218  id_,
219  "WMMManager",
220  __func__,
221  u8Category,
222  vec[u8Category].first,
223  vec[u8Category].second,
224  std::chrono::duration_cast<DoubleSeconds>(totalUtilizationMicroseconds_).count(),
225  fActivityRatio * 100.0f);
226  }
227  }
228 
229  // clear counters
230  resetCounters();
231 
232  return vec;
233 }
UtilizationRatioVector getUtilizationRatios(const Microseconds &deltaTMicroseconds)
Definition: wmmmanager.cc:180
#define LOGGER_VERBOSE_LOGGING(logger, level, fmt, args...)
WMMManager(NEMId id, PlatformServiceProvider *pPlatformService, MACLayer *pMACLayer)
Definition: wmmmanager.cc:37
std::vector< UtilizationRatioPair > UtilizationRatioVector
Definition: wmmmanager.h:61
void updateLocalActivity(std::uint8_t u8Category, const Microseconds &durationMicroseconds)
Definition: wmmmanager.cc:97
The PlatformServiceProvider interface provides access to emulator services.
std::chrono::microseconds Microseconds
Definition: types.h:45
std::chrono::duration< double > DoubleSeconds
Definition: types.h:47
std::uint16_t NEMId
Definition: types.h:52
float getRatio(const EMANE::Microseconds &d1, const EMANE::Microseconds d2)
Definition: utils.h:65
void setNumCategories(const std::uint8_t u8NumCategories)
Definition: wmmmanager.cc:136
virtual LogServiceProvider & logService()=0
std::pair< float, float > UtilizationRatioPair
Definition: wmmmanager.h:59
void updateTotalActivity(std::uint8_t u8Category, const Microseconds &durationMicroseconds)
Definition: wmmmanager.cc:61