Statistics
| Branch: | Revision:

napa-baselibs / monl / mon_measure.h @ d1e0f2ca

History | View | Annotate | Download (8.05 KB)

1
/***************************************************************************
2
 *   Copyright (C) 2009 by Robert Birke
3
 *   robert.birke@polito.it
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9

10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14

15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
18
 ***********************************************************************/
19

    
20
#ifndef _MON_MEASURE_HH_
21
#define _MON_MEASURE_HH_
22

    
23
#include <vector>
24
#include <bitset>
25
#include <map>
26
#include "mon_parameter.h"
27
#include "measure_plugin.h"
28
#include "ids.h"
29
#include "mon.h"
30
#include <math.h>
31
#include "result_buffer.h"
32
#include "ml.h"
33

    
34
#include "grapes_log.h"
35

    
36
#include <fstream>
37

    
38
/************************** monMeasure ********************************/
39

    
40
/* Measures can be:
41
 * - IN_BAND, OUT_OF_BAND, BOTH
42
 * - PACKET,DATA, BOTH
43
 * This is stored in a bitset called caps. If the bit is set the
44
 * corresponding type is true. flags contians how a particular instance
45
 * is used */
46

    
47
#pragma pack(push)  /* push current alignment to stack */
48
#pragma pack(1)     /* set alignment to 1 byte boundary */
49
struct res_mh_pair {
50
        result res;
51
        MonHandler mh;
52
};
53
#pragma pack(pop)   /* restore original alignment from stack */
54

    
55
typedef std::map<MeasurementId, class MonMeasure*> ExecutionList;
56

    
57
class MonMeasure {
58
protected:
59
        bool auto_loaded;
60

    
61
        /* Bitmask defining the capabilities of this instance of the measure */
62
        MeasurementCapabilities flags;
63

    
64
        /* Pointer to the MeasurePlugin class describing this measure */
65
        class MeasurePlugin *measure_plugin;
66

    
67
        /* Status of the measurement instance */
68
        MeasurementStatus status;
69

    
70
        MonHandler mh_remote;
71

    
72
        uint8_t dst_socketid[SOCKETID_SIZE];
73
        MsgType  msg_type;
74

    
75
        int used_counter;
76

    
77
        ResultBuffer *rb;
78

    
79
        /* Parameter values */
80
        MonParameterValue *param_values;
81

    
82

    
83
        void debugInit(const char *);
84
        void debugStop();
85
        std::fstream output_file;
86

    
87
        int paramChangeDefault(MonParameterType ph, MonParameterValue p) {
88
                if(ph == P_WINDOW_SIZE && rb != NULL)
89
                        return rb->resizeBuffer((int)p);
90
                if(ph == P_INIT_NAN_ZERO && rb != NULL)
91
                        return rb->init();
92
                if(ph == P_DEBUG_FILE) {
93
                        if(param_values[P_DEBUG_FILE] == p)
94
                                return EOK;
95
                        if(p == 1)
96
                                debugInit(measure_plugin->name.c_str());
97
                        if(p == 0)
98
                                debugStop();
99
                        return EOK;
100
                }
101
                if(ph < P_LAST_DEFAULT_PARAM)
102
                        return EOK;
103
                return paramChange(ph,p);
104
        }
105

    
106
        result *r_rx_list;
107
        result *r_tx_list;
108

    
109
        /* called when the measure is put on an execution list */
110
        /* should be used to reinitialise the measurement in subsequent uses */
111
        virtual void init() {};
112
        virtual void stop() {};
113

    
114

    
115
        friend class MeasureManager;
116
        friend class MeasureDispatcher;
117
        friend class ResultBuffer;
118
public:
119

    
120
        void debugOutput(char *out);
121

    
122
        class MeasureDispatcher *ptrDispatcher;
123
        MonHandler mh_local;
124

    
125
        /* Functions */
126
        /* Constructor: MUST BE CALLED BY DERIVED CLASSES*/
127
        MonMeasure(class MeasurePlugin *mp, MeasurementCapabilities mc, class MeasureDispatcher *ptrDisp);
128

    
129
        virtual ~MonMeasure() {
130
                delete[] param_values;
131
                if(rb != NULL)
132
                        delete rb;
133
        };
134

    
135
        /* Get vector of MonParameterValues (pointers to the MonParameterValue instances) */
136
        std::vector<class MonParameter* >& listParameters(void) {
137
                return measure_plugin->params;
138
        };
139

    
140
        /* Set MonParameterValue value */
141
        int setParameter(size_t pid, MonParameterValue p) {
142
                if(pid < measure_plugin->params.size() && measure_plugin->params[pid]->validator(p) == EOK) {
143
                        MonParameterValue pt;
144
                        pt = param_values[pid];
145
                        param_values[pid] = p;
146
                        if(paramChangeDefault(pid,p) == EOK) {
147
                                return EOK;
148
                        }
149
                        param_values[pid] = pt;
150
                }
151
                return -EINVAL;
152
        };
153

    
154
        /* Get MonParameterValue value */
155
        MonParameterValue getParameter(size_t pid) {
156
                if(pid >= measure_plugin->params.size())
157
                        return NAN;
158
                return param_values[pid];
159
        };
160

    
161
        MeasurementStatus getStatus() {
162
        return status;
163
        }
164

    
165
        /* Get flags */
166
        MeasurementCapabilities getFlags() {
167
                return flags;
168
        }
169

    
170
        /* Set flags */
171
        int setFlags(MeasurementCapabilities f) {
172
                if(f == measure_plugin->getCaps() && f == 0)
173
                        return EOK;
174
                /*check that f is a subset of Caps */
175
                if(f & ~(measure_plugin->getCaps() | REMOTE | TXLOC | RXLOC | TXREM | RXREM))
176
                        return -ERANGE;
177
                /* Check packet vs chunk */
178
                if(!((f & PACKET)^(f & DATA)))
179
                        return -ERANGE;
180
                /* check inband vs out-of-band */
181
                if(f & TIMER_BASED)
182
                        flags |= TIMER_BASED | IN_BAND | OUT_OF_BAND;
183
                else 
184
                        if(!((f & IN_BAND)^(f & OUT_OF_BAND)))
185
                                return -ERANGE;
186
                /* check execution combinations (if not remote) */
187
                if(f & REMOTE)
188
                        return EOK;
189
                switch(f & (TXONLY | RXONLY | TXRXUNI | TXRXBI)) {
190
                case TXONLY:
191
                                flags |= f | TXLOC;
192
                                break;
193
                case RXONLY:
194
                                flags |= f | RXLOC;
195
                                break;
196
                case TXRXBI:
197
                                flags |= f | TXREM | RXLOC;
198
                case TXRXUNI:
199
                                flags |= f | TXLOC | RXREM;
200
                                break;
201
                default:
202
                                return -ERANGE;
203
                }
204
                return EOK;
205
        }
206

    
207
        int setPublishStatisticalType(const char * publishing_name, const char *channel, enum stat_types st[], int length, void *repo_client) {
208
                if(rb == NULL)
209
                {
210
                        info("rb == NULL");
211
                        return -EINVAL;
212
                }
213

    
214
                int result = rb->setPublishStatisticalType(publishing_name, channel, st, length, repo_client);
215

    
216
                return result;
217
        };
218

    
219
        void defaultInit() {
220
                init();
221
                if(param_values[P_DEBUG_FILE] == 1.0)
222
                        debugInit(measure_plugin->name.c_str());
223
        };
224

    
225
        void defaultStop() {
226
                stop();
227
                if(param_values[P_DEBUG_FILE] == 1.0)
228
                        debugStop();
229
        };
230

    
231
        /* called when  changes in parameter values happen */
232
        virtual int paramChange(MonParameterType ph, MonParameterValue p) {return EOK;};
233

    
234

    
235
        void RxPktLocal(ExecutionList *el) {
236
                if(r_rx_list == NULL)
237
                        return;
238
                newSample(RxPkt(r_rx_list, el));
239
        };
240
        void RxPktRemote(struct res_mh_pair *rmp, int &i, ExecutionList *el) {
241
                if(r_rx_list == NULL)
242
                        return;
243
                result r = RxPkt(r_rx_list, el);
244
                if(!isnan(r)) {
245
                        rmp[i].res = r;
246
                        rmp[i].mh = mh_remote;
247
                        i++;
248
                }
249
        };
250

    
251
        void RxDataLocal(ExecutionList *el) {
252
                if(r_rx_list == NULL)
253
                        return;
254
                newSample(RxData(r_rx_list,el));
255
        };
256
        void RxDataRemote(struct res_mh_pair *rmp, int &i, ExecutionList *el) {
257
                if(r_rx_list == NULL)
258
                        return;
259
                result r = RxData(r_rx_list, el);
260
                if(!isnan(r)) {
261
                        rmp[i].res = r;
262
                        rmp[i].mh = mh_remote;
263
                        i++;
264
                }
265
        };
266

    
267
        void TxPktLocal(ExecutionList *el) {
268
                if(r_tx_list == NULL)
269
                        return;
270
                newSample(TxPkt(r_tx_list, el));
271
        };
272
        void TxPktRemote(struct res_mh_pair *rmp, int &i, ExecutionList *el) {
273
                if(r_tx_list == NULL)
274
                        return;
275
                result r = TxPkt(r_tx_list, el);
276
                if(!isnan(r)) {
277
                        rmp[i].res = r;
278
                        rmp[i].mh = mh_remote;
279
                        i++;
280
                }
281
        };
282

    
283
        void TxDataLocal(ExecutionList *el)  {
284
                if(r_tx_list == NULL)
285
                        return;
286
                newSample(TxData(r_tx_list, el));
287
        };
288
        void TxDataRemote(struct res_mh_pair *rmp, int &i, ExecutionList *el) {
289
                if(r_tx_list == NULL)
290
                        return;
291
                result r = TxData(r_tx_list, el);
292
                if(!isnan(r)) {
293
                        rmp[i].res = r;
294
                        rmp[i].mh = mh_remote;
295
                        i++;
296
                }
297
        };
298

    
299
        int newSample(result r) {
300
                if(!isnan(r) && rb != NULL) {
301
                        rb->newSample(r);
302
                        return EOK;
303
                }
304
                else
305
                        return -EINVAL;
306
        };
307

    
308
        //If OOB this function is called upon scheduled event
309
        virtual void Run() {return;};
310
        virtual void receiveOobData(char *buf, int buf_len, result *r) {return;};
311
        int scheduleNextIn(struct timeval *tv);
312
        
313

    
314
        int sendOobData(char* buf, int buf_len);
315

    
316
        /* Functions processing a message (packet and data) and extracting the measure to be stored */
317
        virtual result RxPkt(result *r, ExecutionList *) {return NAN;};
318
        virtual result RxData(result *r, ExecutionList *) {return NAN;};
319
        virtual result TxPkt(result *r, ExecutionList *) {return NAN;};
320
        virtual result TxData(result *r, ExecutionList *) {return NAN;};
321
        
322
        //void OutOfBandcb(HANDLE *event_handle, void *arg) {return;};
323
};
324

    
325
#endif  /* _MON_MEASURE_HH_ */