hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ResourceUsageManager.cpp
Go to the documentation of this file.
2 #include "HandlerTypes.hpp"
3 
4 namespace HCE
5 {
6 namespace handlers
7 {
8 //-----------------------------------------------------------------------------
9 void ResourceUsageManager::add(const std::string& identity, ResourceUsageCollection& collection, const ResourceUsageData& resourceUsageData, size_t maxSize)
10 {
11  collection.push_back(resourceUsageData);
12  if (maxSize && maxSize < collection.size())
13  {
14  while (maxSize < collection.size())
15  {
16  collection.pop_front();
17  }
18  }
19  weights.insert(std::map<std::string, Weights>::value_type(identity, Weights()));
20 }
21 //-----------------------------------------------------------------------------
22 void ResourceUsageManager::add(const std::string& identity, ResourceUsageCollection& collection, const std::string& resources, size_t maxSize)
23 {
25  add(identity, collection, converter.toObject().cast<HCE::handlers::ResourceUsageData>(), maxSize);
26 }
27 //-----------------------------------------------------------------------------
29 {
30  ResourceUsageData resourceUsageData;
31  size_t count = 0;
32  double diff = abs(weights[identity].last-weights[identity].prev);
33 
34  std::stringstream outMsg;
35  if (diff != weights[identity].diff)
36  {
37  outMsg << "calculateAverage (" << diff << " vs. " << weights[identity].diff << ") identity: " << identity << " steps:{ ";
38 
39  for(std::vector<size_t>::reverse_iterator iter=steps.rbegin();iter!=steps.rend();++iter)
40  outMsg << (*iter) << " ";
41  }
42 
43  if (diff > weights[identity].diff*2)
44  {
45  count = nextStep(identity);
46  }
47  else
48  {
49  count = prevStep(identity);
50  }
51 
52  if (diff != weights[identity].diff)
53  {
54  outMsg << "} [" << weights[identity].stepIndex << "] = " << count;
55  logger.log(NODE_LOG_MODE_TRACE) << outMsg.str() << flush;
56  }
57 
58  resourceUsageData = averageValue(collection, count);
59 
60  weights[identity].diff = diff;
61  weights[identity].prev = weights[identity].last;
62 
63  return resourceUsageData;
64 }
65 //-----------------------------------------------------------------------------
67 {
68  ResourceUsageData resourceUsageData;
69  size_t index = 0;
70  for (ResourceUsageCollection::reverse_iterator iter=collection.rbegin();iter!=collection.rend();++iter)
71  {
72  resourceUsageData += (*iter);
73  if (++index >= count)
74  break;
75  }
76  resourceUsageData /= (count < collection.size())?count:collection.size();
77  return resourceUsageData;
78 }
79 //-----------------------------------------------------------------------------
80 bool ResourceUsageManager::isAllowedRange(const std::string& identity, double maxAllowedRange)
81 {
82  bool result = false;
83 
84  std::vector<double> listWeights = weights[identity].listWeights;
85 
86  std::stringstream outMsg;
87  outMsg << "WEIGHTS RANGE: ";
88  for (size_t i=0;i<listWeights.size();++i)
89  outMsg << listWeights[i] << " ";
90  outMsg << "\t";
91 
92  if (listWeights.size())
93  {
94  auto range = std::minmax_element(listWeights.begin(),listWeights.end());
95 
96  outMsg << "MIN: " << (*range.first) << "\tMAX: " << (*range.second);
97 
98  if (*range.second)
99  {
100  double resourceUsageRange = (*range.second-*range.first)*100/(*range.second);
101 
102  outMsg << "\tmaxAllowedRange: " << maxAllowedRange << "\tresourceUsageRange: " << resourceUsageRange;
103 
104  if (maxAllowedRange > resourceUsageRange && resourceUsageRange)
105  {
106  result = true;
107  }
108  }
109  }
110  logger.log(NODE_LOG_MODE_TRACE) << outMsg.str() << flush;
111  return result;
112 }
113 //-----------------------------------------------------------------------------
115 : maxSize(0), steps(), weights(), logger(logger_)
116 {
117  setMaxSize(MAX_SIZE);
118 }
119 //-----------------------------------------------------------------------------
120 void ResourceUsageManager::setMaxSize(size_t maxSize_)
121 {
122  maxSize=maxSize_;
123  steps.clear();
124  size_t index = maxSize;
125  while(index > 0)
126  {
127  steps.push_back(index);
128  index /= 2;
129  }
130 }
131 //-----------------------------------------------------------------------------
132 size_t ResourceUsageManager::nextStep(const std::string& identity)
133 {
134  if (weights[identity].stepIndex+1 < steps.size())
135  ++(weights[identity].stepIndex);
136  return steps[weights[identity].stepIndex];
137 }
138 //-----------------------------------------------------------------------------
139 size_t ResourceUsageManager::prevStep(const std::string& identity)
140 {
141  if (weights[identity].stepIndex > 0)
142  --(weights[identity].stepIndex);
143  return steps[weights[identity].stepIndex];
144 }
145 //-----------------------------------------------------------------------------
146 void ResourceUsageManager::setLastWeight(const std::string& identity, double weight)
147 {
148  weights[identity].last = weight;
149 }
150 //-----------------------------------------------------------------------------
151 double ResourceUsageManager::getLastWeight(const std::string& identity)
152 {
153  double result = 0.0;
154  std::map<std::string, Weights>::iterator iter = weights.find(identity);
155  if (iter != weights.end())
156  result = (*iter).second.last;
157  return result;
158 }
159 //-----------------------------------------------------------------------------
160 void ResourceUsageManager::setListWeights(const std::string& identity, const std::vector<double>& listWeights)
161 {
162  weights[identity].listWeights = listWeights;
163 }
164 //-----------------------------------------------------------------------------
165 std::vector<double> ResourceUsageManager::getListWeights(const std::string& identity)
166 {
167  std::vector<double> listWeights;
168  std::map<std::string, Weights>::iterator iter = weights.find(identity);
169  if (iter != weights.end())
170  listWeights = (*iter).second.listWeights;
171  return listWeights;
172 }
173 //-----------------------------------------------------------------------------
174 //-----------------------------------------------------------------------------
175 } // end namespace handlers
176 } // end namespace HCE