Statistics
| Branch: | Revision:

napa-baselibs / monl / measure_manager.h @ a1a78de4

History | View | Annotate | Download (8.36 KB)

1 956892f0 ArpadBakay
/***************************************************************************
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 _MEASURE_MANAGER_H
21
#define _MEASURE_MANAGER_H
22
23
#include <string>
24
#include <map>
25
#include "mon_measure.h"
26
#include "measure_plugin.h"
27
#include "measure_dispatcher.h"
28
29
#include "grapes_log.h"
30
31
32
#define MAX_NUM_MEASURES_INSTANTIATED 1000000
33
34
typedef std::map<MeasurementId, class MeasurePlugin *> MonMeasureMapId;
35
typedef std::map<std::string, class MeasurePlugin *> MonMeasureMapName;
36
typedef std::map<MonHandler,class MonMeasure *> MeasureMap;
37
38
class MeasureManager {
39
protected:
40
        /* List of available measures */
41
        MonMeasureMapId mMonMeasuresId;
42
        MonMeasureMapName mMonMeasuresName;
43
44
        /* defualts */
45
        void *default_repo_client;
46
        char* default_channel;
47
        char** peer_name;
48
49
        class MeasureDispatcher cDispatcher;
50
        
51
        /* Currently installed instances */
52
        MeasureMap mMeasureInstances;
53
        int next_measure_handler;
54
        
55
        /* Used to load a MeasurePlugin. If the Id or name has been already used it returns a negative value, *
56
         * 0 otherwise. */
57
        int loadMeasurePlugin(MeasurePlugin *plugin) {
58
                if(mMonMeasuresId.find(plugin->getId()) == mMonMeasuresId.end() &&
59
                        mMonMeasuresName.find(plugin->getName()) == mMonMeasuresName.end())
60
                {
61
                        mMonMeasuresId[plugin->getId()] = plugin;
62
                        mMonMeasuresName[plugin->getName()] = plugin;
63
                        return EOK;
64
                } else
65
                        return -EINVAL;
66
        };
67
68
        int loadMeasurePlugins(void);
69
70
        /* Inserts new instance. Returns handler */
71
         MonHandler insertNewInstance(MonMeasure *m) {
72
                int new_mh;
73
                /* For memory efficiency we use a hash container and a circular buffer */
74
                /* check if next_measure_handler is not in use (went around once) */
75
                if(mMeasureInstances.find(next_measure_handler) != mMeasureInstances.end())
76
                        return -ENOMEM;
77
                new_mh = next_measure_handler;
78
                mMeasureInstances[new_mh] = m;
79
80
                /* update next_measure_handler */
81
                next_measure_handler++;
82
                if(next_measure_handler == MAX_NUM_MEASURES_INSTANTIATED)
83
                        next_measure_handler = 0;
84
85
                return new_mh;
86
        }
87
        /* checks handler */
88
        bool isValidMonHandler(MonHandler mh) {
89
                if(mMeasureInstances.find(mh) != mMeasureInstances.end())
90
                        return true;
91
                else
92
                        return false;
93
        }
94
        
95
        friend class MonMeasure;
96
        friend class MeasureDispatcher;
97
public:
98
        /* Constructor */
99
        MeasureManager() {
100
                default_repo_client = NULL;
101
                default_channel = NULL;
102
                next_measure_handler = 0;
103
                cDispatcher.mm = this;
104
                peer_name = new char*;
105
                *peer_name =  NULL;
106
                loadMeasurePlugins();
107
        };
108
109
        MeasureManager(void *repo) {
110
                default_repo_client = repo;
111
                default_channel = NULL;
112
                next_measure_handler = 0;
113
                cDispatcher.mm = this;
114
                peer_name = new char*;
115
                *peer_name =  NULL;
116
                loadMeasurePlugins();
117
        };
118
119
        /* Destructor */
120
        ~MeasureManager() {
121
                /* Free list of available measures */
122
                for(MonMeasureMapId::reverse_iterator it = mMonMeasuresId.rbegin();
123
                        it !=mMonMeasuresId.rend(); ++it)
124
                        delete it->second;
125
                /* Free instances */
126
                for(MeasureMap::iterator it = mMeasureInstances.begin();
127
                        it != mMeasureInstances.end(); ++it)
128
                        monDestroyMeasure(it->first);
129
                if(default_channel)
130
                        delete[] default_channel;
131
                if(*peer_name != NULL)
132
                        delete[] *peer_name;
133
                delete peer_name;
134
        }
135
136
        /* Returns a map container with all the measures which can be instantiated and used */
137
        MonMeasureMapId& monListMeasure() {
138
                return mMonMeasuresId;
139
        };
140
141
        /* Creates an instance of a measure specified through the name of the measure
142
         * returns handler of the new instance to be used subsequently with the functions below */
143
        int monCreateMeasureName(MeasurementName name, MeasurementCapabilities mc) {
144
                std::string t(name);
145
                MonMeasure *m;
146
                int ret;
147
                MonHandler mh;
148
149
                if(mMonMeasuresName.find(t) == mMonMeasuresName.end())
150
                        return -EINVAL;
151
152
                m = mMonMeasuresName[t]->createMeasure(mc, &cDispatcher);
153
154
                ret = m->setFlags(mc);
155
                if(ret != EOK)
156
                        goto error;
157
158
                ret = mh = insertNewInstance(m);
159
                if(mh < 0)
160
                        goto error;
161
162
                m->mh_local = mh;
163
                return mh;
164
165
        error:
166
                delete m;
167
                return ret;
168
        };
169
170
        int monCreateMeasureId(MeasurementId id, MeasurementCapabilities mc) {
171
                MonMeasure *m;
172
                int ret;
173
                MonHandler mh;
174
175
                if(mMonMeasuresId.find(id) == mMonMeasuresId.end())
176
                        return -EINVAL;
177
178
                m = mMonMeasuresId[id]->createMeasure(mc, &cDispatcher);
179
180
                ret = m->setFlags(mc);
181
                if(ret != EOK)
182
                        goto error;
183
184
                ret = mh = insertNewInstance(m);
185
                if(mh < 0)
186
                        goto error;
187
188
                m->mh_local = mh;
189
190
                return mh;
191
192
        error:
193
                delete m;
194
                return ret;
195
        };
196
197
        /* Destroys a specific measure instance */
198
        int monDestroyMeasure(MonHandler mh) {
199
                /* find it */
200
                if(!isValidMonHandler(mh))
201
                        return -EINVAL;
202
203
                /* deactivate it */
204
                cDispatcher.deactivateMeasure(mMeasureInstances[mh]);
205
206
                delete mMeasureInstances[mh];
207
                mMeasureInstances.erase(mh);
208
209
                return EOK;
210
        };
211
212
        MeasurementId monMeasureHandlerToId (MonHandler mh) {
213
                /* find it */
214
                if(!isValidMonHandler(mh))
215
                        return -EINVAL;
216
                
217
                return mMeasureInstances[mh]->measure_plugin->getId();
218
        }
219
220
        std::vector<class MonParameter *>* monListParameters(MonHandler mh) {
221
                if(!isValidMonHandler(mh))
222
                        return NULL;
223
                return &(mMeasureInstances[mh]->listParameters());
224
        };
225
226
        int monSetParameter(MonHandler mh, int pid, MonParameterValue p) {
227
                if(!isValidMonHandler(mh))
228
                        return -EINVAL;
229
                return mMeasureInstances[mh]->setParameter(pid,p);
230
        };
231
232
        MonParameterValue monGetParameter(MonHandler mh, size_t pid) {
233
                if(!isValidMonHandler(mh))
234
                        return NAN;
235
                return mMeasureInstances[mh]->getParameter(pid);
236
        };
237
238
        MeasurementStatus monGetStatus(MonHandler mh) {
239
                if(!isValidMonHandler(mh))
240
                        return ERROR;
241
                return mMeasureInstances[mh]->getStatus();
242
        };
243
244
         MeasurementCapabilities monGetMeasurementCaps(MonHandler mh) {
245
                if(!isValidMonHandler(mh))
246
                        return -EINVAL;
247
                return mMeasureInstances[mh]->getFlags();
248
        }
249
250
        int monActivateMeasure(MonHandler mh, SocketId sid, MsgType mt) {
251
                if(!isValidMonHandler(mh))
252
                        return -EINVAL;
253
                return cDispatcher.activateMeasure(mMeasureInstances[mh], sid, mt);
254
        }
255
256
        int monDeactivateMeasure(MonHandler mh) {
257
                if(!isValidMonHandler(mh))
258
                        return -EINVAL;
259
                return cDispatcher.deactivateMeasure(mMeasureInstances[mh]);        
260
        }
261
262
        size_t getMeasuresCount(void) {
263
                return mMonMeasuresId.size();
264
        };
265
266
        void cbRxPkt(void *arg) {
267
                cDispatcher.cbRxPkt(arg);
268
        };
269
270
        void cbRxData(void *arg) {
271
                cDispatcher.cbRxData(arg);
272
        };
273
274
        void cbTxPkt(void *arg) {
275
                cDispatcher.cbTxPkt(arg);
276
        };
277
278
        void cbTxData(void *arg) {
279
                cDispatcher.cbTxData(arg);
280
        };
281
282
        int cbHdrPkt(SocketId sid, MsgType mt) {
283
                return cDispatcher.cbHdrPkt(sid, mt);
284
        };
285
286
        int cbHdrData(SocketId sid, MsgType mt) {
287
                return cDispatcher.cbHdrData(sid, mt);
288
        };
289
290
        void receiveCtrlMsg(SocketId sid, MsgType mt, char *buffer, int buflen){
291
                cDispatcher.receiveCtrlMsg(sid, mt, buffer, buflen);
292
        };
293
294
        int monPublishStatisticalType(MonHandler mh, const char* publishing_name, const char *channel, enum stat_types st[], int length, void *repo_client) {
295
                if(repo_client == NULL)
296
                        repo_client = default_repo_client;
297
298
                if(repo_client == NULL)
299
                        return -EFAILED;
300
301
                if(channel == NULL)
302
                        channel = default_channel;
303
304
                if(!isValidMonHandler(mh))
305
                        return -EINVAL;
306
307
                return mMeasureInstances[mh]->setPublishStatisticalType(publishing_name, channel, st, length, repo_client);
308
        };
309
310
        result monRetrieveResult(MonHandler mh, enum stat_types st) {
311
                if(!isValidMonHandler(mh))
312
                        return NAN;
313
314
                if(mMeasureInstances[mh]->rb == NULL)
315
                        return NAN;
316
        
317
                return mMeasureInstances[mh]->rb->stats[st];
318
        };
319
320
        int monNewSample(MonHandler mh, result r) {
321
                if(!isValidMonHandler(mh))
322
                        return -EINVAL;
323
324
                if(mMeasureInstances[mh]->flags != 0)
325
                        return -EINVAL;
326
327
                return mMeasureInstances[mh]->newSample(r);
328
        };
329
330
        void monParseConfig(void *);
331
332 a1a78de4 CsabaKiraly
        int monSetPeerName(const char *pn);
333 956892f0 ArpadBakay
};
334
335
#endif /* _MEASURE_MANAGER_H */