Statistics
| Branch: | Revision:

napa-baselibs / monl / plugins / forecaster_measure.cpp @ 507372bb

History | View | Annotate | Download (5.24 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
#include "forecaster_measure.h"
20
#include "napa_log.h"
21
#include <sys/time.h>
22
#include <limits.h>
23

    
24
#ifdef WIN32
25
#define random(x) rand(x)
26
#endif
27

    
28

    
29

    
30
ForecasterMeasure::ForecasterMeasure(class MeasurePlugin *m, MeasurementCapabilities mc, class MeasureDispatcher *md): MonMeasure(m,mc,md) {
31
}
32

    
33
ForecasterMeasure::~ForecasterMeasure() {
34
}
35

    
36
void ForecasterMeasure::init() {
37
        if(pkt != NULL)
38
                delete[] pkt;
39
        pkt = new char[(int) param_values[P_FORECASTER_PAYLOAD_SIZE]];
40

    
41
        p_num = 0;
42
        p_below = 0;
43
        p_above = 0;
44
        min_delay = NAN;
45
        avg_pause = FORECASTER_MTU / param_values[P_FORECASTER_RATE] * 8000.0;// [us]
46
}
47

    
48
void ForecasterMeasure::stop() {
49
        r_tx_list[R_AVAILABLE_BW_FORECASTER] = r_rx_list[R_AVAILABLE_BW_FORECASTER] = NAN;
50
}
51

    
52
int ForecasterMeasure::paramChange(MonParameterType ph, MonParameterValue p){
53
        switch(ph) {
54
                case P_FORECASTER_PAYLOAD_SIZE:
55
                        param_values[P_FORECASTER_PAYLOAD_SIZE] = p;
56
                        if(pkt != NULL)
57
                                delete[] pkt;
58
                        pkt = new char[(int) param_values[P_FORECASTER_PAYLOAD_SIZE]];
59
                        avg_pause = param_values[P_FORECASTER_PAYLOAD_SIZE] / param_values[P_FORECASTER_RATE] * 8000.0;
60
                        break;
61
                case P_FORECASTER_RATE:
62
                        param_values[P_FORECASTER_RATE] = p;
63
                        avg_pause = param_values[P_FORECASTER_PAYLOAD_SIZE] / param_values[P_FORECASTER_RATE] * 8000.0;
64
                        break;
65
                case P_FORECASTER_PKT_TH:
66
                        param_values[P_FORECASTER_PKT_TH] = p;
67
                        break;
68
                case P_CAPPROBE_DELAY_TH:
69
                        param_values[P_CAPPROBE_DELAY_TH] = p;
70
                        break;
71
                case P_FORECASTER_RELATIVE:
72
                        param_values[P_FORECASTER_RELATIVE] = p;
73
                        break;
74
                default:
75
                        return -EINVAL;
76
        }
77
        return EOK;
78
}
79

    
80
double ForecasterMeasure::gen_pause_exp(double avg_pause){
81
        double p = random();
82
        /*turn this into a random number between 0 and 1...*/
83
        double f = p / (double) INT_MAX;
84

    
85
        return -1 * avg_pause * log(1-f);
86
}
87

    
88
void ForecasterMeasure::Run() {
89
        double pause;
90
        if(flags & REMOTE)
91
                return;
92

    
93
        struct timeval *tv = (struct timeval*) pkt;
94
        gettimeofday(tv,NULL);
95
        sendOobData(pkt, param_values[P_FORECASTER_PAYLOAD_SIZE]);
96

    
97
        struct timeval next = {0,0};
98

    
99
        pause = gen_pause_exp(avg_pause);
100
        next.tv_sec = (unsigned long)pause / 1000000;
101
        next.tv_usec = (unsigned long)fmod(pause, 100000.0);
102

    
103
        scheduleNextIn(&next);
104
}
105

    
106
void ForecasterMeasure::receiveOobData(char *buf, int buf_len, result *r) {
107
        if(flags & REMOTE) { //remote
108
                result ab;
109
                ab = computeAvailableBandwidth(r);
110
                if(!isnan(ab))
111
                        sendOobData((char *) &ab, sizeof(ab));
112
        } else { //local
113
                result *ab = (result *) pkt;
114
                newSample(*ab);
115
        }
116
}
117

    
118
result ForecasterMeasure::RxPkt(result *r,ExecutionList *el) {
119
        if(!(flags & REMOTE)) //local: nothing to do
120
                return NAN;
121
        
122
        return computeAvailableBandwidth(r);
123
}
124

    
125
result ForecasterMeasure::computeAvailableBandwidth(result *r) {
126
        p_num++;
127

    
128
        if(r[R_CORRECTED_DELAY] < min_delay || isnan(min_delay)) {
129
                min_delay = r[R_CORRECTED_DELAY];
130
        }
131
        
132
        if (r[R_CORRECTED_DELAY] < min_delay +  param_values[P_FORECASTER_DELAY_TH] / 1000000.0) {
133
                p_below++;
134
        }
135

    
136
        if(p_num >= param_values[P_FORECASTER_PKT_TH]) {
137

    
138
                if(param_values[P_FORECASTER_RELATIVE])
139
                        r_rx_list[R_AVAILABLE_BW_FORECASTER] = (double) p_below / (double) p_num * 100;
140
                else
141
                        r_rx_list[R_AVAILABLE_BW_FORECASTER] = (double) p_below / (double) p_num * r_rx_list[R_CAPACITY_CAPPROBE];
142

    
143
                p_num = p_below = 0;
144
                min_delay = NAN;
145

    
146
                char dbg[512];
147
                snprintf(dbg, sizeof(dbg), "Ts: %f Ab: %f", r[R_RECEIVE_TIME], r_rx_list[R_AVAILABLE_BW_FORECASTER]);
148
                debugOutput(dbg);
149

    
150
                return r_rx_list[R_AVAILABLE_BW_FORECASTER];
151
        }
152
        return NAN;
153
}
154

    
155
ForecasterMeasurePlugin::ForecasterMeasurePlugin() {
156
        /* Initialise properties: MANDATORY! */
157
        name = "Available Bandwidth (forecaster)";
158
        desc = "The available bandwidth in kbit/s computed using forecaster";
159
        id = AVAILABLE_BW_FORECASTER;
160
        /* end of mandatory properties */
161
        addParameter(new MinParameter("Rate","The rate at which we probe the path in kbit/s",0,1000), P_FORECASTER_RATE);
162
        addParameter(new MinParameter("Packet Threshold","Update available bandwidth each # pkts",0,100), P_FORECASTER_PKT_TH);
163
        addParameter(new MinParameter("Delay Threshold","Delay threshold tolerance [us]",0,100), P_FORECASTER_DELAY_TH);
164
        addParameter(new MinParameter("Payload","The size of the payload for injected packets [bytes]",sizeof(uint32_t), FORECASTER_MTU), P_FORECASTER_PAYLOAD_SIZE);
165
        addParameter(new MinMaxParameter("Relative","Chooses between absolute (0) [kbit/s] and relative (1) [%]", 0, 1, 0), P_FORECASTER_RELATIVE);
166
}