Statistics
| Branch: | Revision:

napa-baselibs / monl / plugins / forecaster_measure.cpp @ 5f3adef4

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

    
24

    
25
ForecasterMeasure::ForecasterMeasure(class MeasurePlugin *m, MeasurementCapabilities mc, class MeasureDispatcher *md): MonMeasure(m,mc,md) {
26
}
27

    
28
ForecasterMeasure::~ForecasterMeasure() {
29
}
30

    
31
void ForecasterMeasure::init() {
32
        if(pkt != NULL)
33
                delete[] pkt;
34
        pkt = new char[(int) param_values[P_FORECASTER_PAYLOAD_SIZE]];
35

    
36
        p_num = 0;
37
        p_below = 0;
38
        p_above = 0;
39
        min_delay = NAN;
40
        avg_pause = FORECASTER_MTU / param_values[P_FORECASTER_RATE] * 8000.0;// [us]
41
}
42

    
43
void ForecasterMeasure::stop() {
44
        r_tx_list[R_AVAILABLE_BW_FORECASTER] = r_rx_list[R_AVAILABLE_BW_FORECASTER] = NAN;
45
}
46

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

    
75
double ForecasterMeasure::gen_pause_exp(double avg_pause){
76
        double p = random();
77
        /*turn this into a random number between 0 and 1...*/
78
        double f = p / (double) INT_MAX;
79

    
80
        return -1 * avg_pause * log(1-f);
81
}
82

    
83
void ForecasterMeasure::Run() {
84
        double pause;
85
        if(flags & REMOTE)
86
                return;
87

    
88
        struct timeval *tv = (struct timeval*) pkt;
89
        gettimeofday(tv,NULL);
90
        sendOobData(pkt, param_values[P_FORECASTER_PAYLOAD_SIZE]);
91

    
92
        struct timeval next = {0,0};
93

    
94
        pause = gen_pause_exp(avg_pause);
95
        next.tv_sec = (unsigned long)pause / 1000000;
96
        next.tv_usec = (unsigned long)fmod(pause, 100000.0);
97

    
98
        scheduleNextIn(&next);
99
}
100

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

    
113
result ForecasterMeasure::RxPkt(result *r,ExecutionList *el) {
114
        if(!(flags & REMOTE)) //local: nothing to do
115
                return NAN;
116
        
117
        return computeAvailableBandwidth(r);
118
}
119

    
120
result ForecasterMeasure::computeAvailableBandwidth(result *r) {
121
        p_num++;
122

    
123
        if(r[R_CORRECTED_DELAY] < min_delay || isnan(min_delay)) {
124
                min_delay = r[R_CORRECTED_DELAY];
125
        }
126
        
127
        if (r[R_CORRECTED_DELAY] < min_delay +  param_values[P_FORECASTER_DELAY_TH] / 1000000.0) {
128
                p_below++;
129
        }
130

    
131
        if(p_num >= param_values[P_FORECASTER_PKT_TH]) {
132

    
133
                if(param_values[P_FORECASTER_RELATIVE])
134
                        r_rx_list[R_AVAILABLE_BW_FORECASTER] = (double) p_below / (double) p_num * 100;
135
                else
136
                        r_rx_list[R_AVAILABLE_BW_FORECASTER] = (double) p_below / (double) p_num * r_rx_list[R_CAPACITY_CAPPROBE];
137

    
138
                p_num = p_below = 0;
139
                min_delay = NAN;
140

    
141
                char dbg[512];
142
                snprintf(dbg, sizeof(dbg), "Ts: %f Ab: %f", r[R_RECEIVE_TIME], r_rx_list[R_AVAILABLE_BW_FORECASTER]);
143
                debugOutput(dbg);
144

    
145
                return r_rx_list[R_AVAILABLE_BW_FORECASTER];
146
        }
147
        return NAN;
148
}
149

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