76215f0e73257354bd8d9f8d27fa025152c7c05e
[anna.git] / source / core / oam / CounterScope.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <string>
10
11 #include <anna/core/tracing/Logger.hpp>
12
13 #include <anna/xml/Node.hpp>
14 #include <anna/xml/Attribute.hpp>
15
16 #include <anna/timex/Engine.hpp>
17
18 #include <anna/core/oam/CounterScope.hpp>
19 #include <anna/core/oam/Counter.hpp>
20
21 using namespace std;
22 using namespace anna;
23
24 #define test_range(index) \
25    if (index < 0 || index >= MaxCounter) { \
26       string msg (asString ()); \
27       msg += functions::asText (" | Index: ", index); \
28       msg += functions::asText (" | Out of range [0, MaxCounter): ", index); \
29       throw RuntimeException (msg, __FILE__, __LINE__); \
30    }
31
32 #define test_instance(index) \
33    if (a_counters [index] == NULL) { \
34       string msg (asString ()); \
35       msg += functions::asText (" | CounterId: ", index); \
36       msg += " | Counter Id is not defined"; \
37       throw RuntimeException (msg, __FILE__, __LINE__); \
38    }
39
40
41 oam::CounterScope::~CounterScope() {
42   for(int i = 0; i < MaxCounter; i ++) {
43     if(a_counters [i] != NULL)
44       delete a_counters [i];
45   }
46 }
47
48 void oam::CounterScope::create(const int counter, const char* name)
49 throw(RuntimeException) {
50   test_range(counter);
51
52   if(a_counters [counter] != NULL) {
53     string msg("Counter '");
54     msg += name;
55     msg += "' (";
56     msg += functions::asString(counter);
57     msg += ") already registered as ";
58     msg += a_counters [counter]->asString();
59     throw RuntimeException(msg, ANNA_FILE_LOCATION);
60   }
61
62   a_counters [counter] = new Counter(*this, counter, name);
63   LOGDEBUG(
64     string msg("Creation | ");
65     msg += asString();
66     msg += " | ";
67     msg += a_counters [counter]->asString();
68     Logger::write(Logger::Debug, msg, ANNA_FILE_LOCATION)
69   );
70 }
71
72 oam::Counter::type_t oam::CounterScope::increment(const int counter, const oam::Counter::type_t value)
73 throw(RuntimeException) {
74   test_range(counter);
75   test_instance(counter);
76   a_counters [counter]->a_value += value;
77   a_counters [counter]->a_accValue += value;
78   LOGINFORMATION(a_counters [counter]->debug());
79   return a_counters [counter]->a_value;
80 }
81
82 oam::Counter::type_t oam::CounterScope::assign(const int counter, const oam::Counter::type_t value)
83 throw(RuntimeException) {
84   test_range(counter);
85   test_instance(counter);
86   a_counters [counter]->a_value = value;
87   a_counters [counter]->a_accValue = value;
88   LOGINFORMATION(a_counters [counter]->debug());
89   return value;
90 }
91
92 oam::Counter::type_t oam::CounterScope::getValue(const int counter) const
93 throw(RuntimeException) {
94   test_range(counter);
95   test_instance(counter);
96   return a_counters [counter]->a_value;
97 }
98
99 U64 oam::CounterScope::getAccValue(const int counter) const
100 throw(RuntimeException) {
101   test_range(counter);
102   test_instance(counter);
103   return a_counters [counter]->a_accValue;
104 }
105
106 int oam::CounterScope::resetAccValues() throw(RuntimeException) {
107   int result = 0; // affected counters
108
109   for(int ii = 0; ii < MaxCounter; ii ++)
110     if(a_counters [ii])
111       result += (a_counters [ii]->resetAcc()) ? 1 : 0;
112
113   return result;
114 }
115
116 const oam::Counter* oam::CounterScope::getCounter(const int counter) const
117 throw(RuntimeException) {
118   test_range(counter);
119   test_instance(counter);
120   return a_counters [counter];
121 }
122
123 string oam::CounterScope::asString() const
124 throw() {
125   string result("oam::CounterScope { Id: ");
126   result += functions::asString(a_id);
127   result += " | Name: ";
128   result += a_name;
129   return result += " }";
130 }
131
132 xml::Node* oam::CounterScope::asXML(xml::Node* parent) const
133 throw(RuntimeException) {
134   xml::Node* result = parent->createChild("Scope");
135   xml::Node* counter;
136
137   for(int ii = 0; ii < MaxCounter; ii ++) {
138     if(a_counters [ii] == NULL)
139       continue;
140
141     counter = result->createChild("Counter");
142     counter->createAttribute("Id", ii);
143     counter->createAttribute("Name", a_counters [ii]->getName());
144     counter->createAttribute("Value", a_counters [ii]->getValue());
145     counter->createAttribute("AccValue", a_counters [ii]->getAccumulatedValue());
146   }
147
148   return result;
149 }
150