Statistics.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef __STATISTICS_H_2F8C97C9EC89D7__
00041 #define __STATISTICS_H_2F8C97C9EC89D7__
00042
00043
00044 #include <string.h>
00045
00046 #ifndef __AVR__
00047 #include "boost/thread/mutex.hpp"
00048 #endif
00049
00050
00051 namespace famouso {
00052 namespace mw {
00053 namespace afp {
00054 namespace defrag {
00055
00056
00071 struct FragmentStats {
00073 unsigned int expected;
00074
00076 unsigned int currently_expected;
00077
00078
00079
00081 unsigned int received;
00082
00084 unsigned int incorrect_header;
00085
00087 unsigned int outdated;
00088
00090 unsigned int used;
00091
00093 unsigned int duplicates;
00094
00096 FragmentStats() {
00097 reset();
00098 }
00099
00101 void reset() {
00102 memset(this, 0, sizeof(*this));
00103 }
00104 };
00105
00106
00114 struct EventStats {
00115
00117 unsigned int complete;
00118
00120 unsigned int incomplete;
00121
00123 EventStats() {
00124 reset();
00125 }
00126
00128 void reset() {
00129 memset(this, 0, sizeof(*this));
00130 }
00131 };
00132
00133
00134
00139 template <class Tag>
00140 class Statistics {
00141
00142
00143 static FragmentStats & fragment() {
00144 static FragmentStats s;
00145 return s;
00146 }
00147
00148 static EventStats & event() {
00149 static EventStats s;
00150 return s;
00151 }
00152
00153 #ifndef __AVR__
00154 static boost::mutex & mutex() {
00155 static boost::mutex m;
00156 return m;
00157 }
00158 #endif
00159
00160 static void inc(unsigned int & val) {
00161 #ifndef __AVR__
00162 mutex().lock();
00163 val++;
00164 mutex().unlock();
00165 #else
00166 val++;
00167 #endif
00168 }
00169
00170 static void add(unsigned int & val, unsigned int to_add) {
00171 #ifndef __AVR__
00172 mutex().lock();
00173 val += to_add;
00174 mutex().unlock();
00175 #else
00176 val += to_add;
00177 #endif
00178 }
00179
00180 static void sub(unsigned int & val, unsigned int to_sub) {
00181 #ifndef __AVR__
00182 mutex().lock();
00183 val -= to_sub;
00184 mutex().unlock();
00185 #else
00186 val -= to_sub;
00187 #endif
00188 }
00189
00190 public:
00191
00193 static void fragment_received() {
00194 inc(fragment().received);
00195 }
00196
00198 static void fragment_incorrect_header() {
00199 inc(fragment().incorrect_header);
00200 }
00201
00203 static void fragment_outdated() {
00204 inc(fragment().outdated);
00205 }
00206
00208 static void fragment_duplicate() {
00209 inc(fragment().duplicates);
00210 }
00211
00213 static void fragment_used() {
00214 inc(fragment().used);
00215 }
00216
00218 static void fragments_expected(unsigned int n) {
00219 add(fragment().expected, n);
00220 }
00221
00223 static void fragments_currently_expected_add(unsigned int n) {
00224 add(fragment().currently_expected, n);
00225 }
00226
00228 static void fragments_currently_expected_sub(unsigned int n) {
00229 sub(fragment().currently_expected, n);
00230 }
00231
00232
00234 static void event_complete() {
00235 inc(event().complete);
00236 }
00237
00239 static void event_incomplete() {
00240 inc(event().incomplete);
00241 }
00242
00243
00245 static void get_fragment_stats(FragmentStats & fs) {
00246 #ifndef __AVR__
00247 mutex().lock();
00248 memcpy(&fs, &fragment(), sizeof(fs));
00249 mutex().unlock();
00250 #else
00251 memcpy(&fs, &fragment(), sizeof(fs));
00252 #endif
00253 }
00254
00256 static void get_event_stats(EventStats & es) {
00257 #ifndef __AVR__
00258 mutex().lock();
00259 memcpy(&es, &event(), sizeof(es));
00260 mutex().unlock();
00261 #else
00262 memcpy(&es, &event(), sizeof(es));
00263 #endif
00264 }
00265 };
00266
00267
00268
00273 class NoStatistics {
00274 public:
00275
00277 static void fragment_received() {}
00278
00280 static void fragment_incorrect_header() {}
00281
00283 static void fragment_outdated() {}
00284
00286 static void fragment_duplicate() {}
00287
00289 static void fragment_used() {}
00290
00292 static void fragments_expected(unsigned int n) {}
00293
00295 static void fragments_currently_expected_add(unsigned int n) {}
00296
00298 static void fragments_currently_expected_sub(unsigned int n) {}
00299
00300
00302 static void event_complete() {}
00303
00305 static void event_incomplete() {}
00306
00307
00309 static void get_fragment_stats(FragmentStats & fs) {
00310 }
00311
00313 static void get_event_stats(EventStats & es) {
00314 }
00315 };
00316
00317
00318
00319 }
00320 }
00321 }
00322 }
00323
00324
00325 #endif // __STATISTICS_H_2F8C97C9EC89D7__
00326