File: bing_stats.c

package info (click to toggle)
bing 1.3.5-5
  • links: PTS
  • area: main
  • in suites: bookworm, sid
  • size: 456 kB
  • sloc: ansic: 3,774; makefile: 51
file content (268 lines) | stat: -rw-r--r-- 8,617 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 *		  Unofficial release 1.3.0
 *			B I N G
 *
 * This file implements the data gathering and analysis algorithms
 * that are specific to bing.
 *
 */

/* $Id: bing_stats.c,v 1.9 1999/10/23 21:56:06 fgouget Exp $ */

#include <stdio.h>
#include <malloc.h>
#include <errno.h>
#include <float.h>

#include "bing_stats.h"

/* (!!) for debug, remove... */
#include "bing_misc.h"

/*
 * Now the bing stats
 */

int rtt_stats_init(rtt_stats_t *rtt_stats)
{
    rtt_stats->nb_samples=0;
    rtt_stats->nb_dropped=0;

    rtt_stats->nb_bits=0;

    rtt_stats->state=RTT_STATE_HINVALID;
    rtt_stats->min_rtt=DBL_MAX;
#ifdef _DEBUG
    rtt_stats->min_rtt_sav=DBL_MAX;
#endif
    rtt_stats->sum_rtt=0;
    rtt_stats->max_rtt=DBL_MIN;

    rtt_stats->samples_size=0;
    rtt_stats->samples=NULL;
    return 0;
}

int rtt_law_init(rtt_law_t *rtt_law, int nb_sizes)
{
    int s;

    rtt_law->rtt_stats=calloc(nb_sizes,sizeof(*rtt_law->rtt_stats));
    for (s=0;s<nb_sizes;s++) {
        rtt_stats_init(&rtt_law->rtt_stats[s]);
    }

    rtt_law->nb_samples=0;
    rtt_law->nb_dropped=0;

    rtt_law->nb_hinvalid=nb_sizes;
    rtt_law->nb_linvalid=0;
    rtt_law->nb_redo=0;

    linreg_init(&rtt_law->host_reg);
    linreg_init(&rtt_law->link_reg);

    return 0;
}

/* (!!) note that rtt_law_add is not updating the linear regression anymore */
int rtt_law_add(rtt_law_t* rtt_law, int index, int size, double rtt)
{
    /* Check the parameters */
    if (rtt_law==NULL) {
        errno=EINVAL;
        return -1;
    }

    if (size<0) {
        rtt_law->rtt_stats[index].nb_dropped++;
        rtt_law->nb_dropped++;
    } else {
        rtt_stats_t* rtt_stats;

        rtt_stats=&rtt_law->rtt_stats[index];
        if (rtt_stats->nb_bits==0) {
             rtt_stats->nb_bits=8*size;
        } else if (rtt_stats->nb_bits!=8*size) {
            /* (!!) oups, do something more, this is really wrong */
            printf("the packet size has changed: %d!=%d\n",8*size,rtt_stats->nb_bits);
            return -1;
        }

        /* Add this new sample */
#if 0
        if (rtt_stats->nb_samples==rtt_stats->samples_size) {
            rtt_stats->samples_size+=10;
            rtt_stats->samples=realloc(rtt_stats->samples,
                sizeof(*rtt_stats)*(rtt_stats->samples_size));
        }
        rtt_stats->samples[rtt_stats->nb_samples++]=rtt;
#endif
        rtt_law->nb_samples++;

        /* Update the statistics */
        rtt_stats->sum_rtt+=rtt;
        if (rtt<rtt_stats->min_rtt) {
            if (rtt_stats->state==RTT_STATE_HINVALID) {
#if 0
                if (rtt_stats->min_rtt_sav!=DBL_MAX)
                    printf("validating %d: %9.7f -> %9.7f (min was %9.7f)\n",index,rtt_stats->min_rtt_sav,rtt,rtt_stats->min_rtt);
#endif
                rtt_law_set_rtt_state(rtt_law,index,RTT_STATE_OK,0);
            }
            rtt_stats->min_rtt=rtt;
            return 1;
        } else if (rtt>rtt_stats->max_rtt) {
            rtt_stats->max_rtt=rtt;
        }
    }

    return 0;
}

/**
 * Sets the state of the specified RTT measurement and updates 
 * the rtt_law_t level statistics accordingly.
 *
 * @param rtt_law the host,method on which to operate
 * @param s the index of the RTT to modify
 * @param state the new state of the RTT, one of the RTT_STATE_* values.
 *        To unset the RTT_STATE_LINVALID1 or RTT_STATE_LINVALID2 flags 
 *        use respectively RTT_STATE_OK|RTT_STATE_LINVALID1 and 
 *        RTT_STATE_OK|RTT_STATE_LINVALID2
 * @param max_rtt if state is RTT_STATE_HINVALID this is the RTT value 
 *        to attain before the RTT is considered valid again. Otherwise 
 *        this parameter is not significant
 * @return the previous state of the RTT
 */
int rtt_law_set_rtt_state(rtt_law_t *rtt_law, int s, int state, double max_rtt)
{
    int ostate;

    ostate=rtt_law->rtt_stats[s].state;
    if (state==ostate) {
        if ((state==RTT_STATE_HINVALID) && (rtt_law->rtt_stats[s].min_rtt>=max_rtt))
            rtt_law->rtt_stats[s].min_rtt=max_rtt;
        return state;
    }

    switch (state) {
    case RTT_STATE_OK:
        if (ostate==RTT_STATE_HINVALID)
            rtt_law->nb_hinvalid--;
        else if ((ostate & RTT_STATE_MASK)==RTT_STATE_LINVALID) {
            rtt_law->nb_linvalid--;
        } else
            rtt_law->nb_redo--;
        rtt_law->rtt_stats[s].state=state;
        break;
    case RTT_STATE_OK|RTT_STATE_LINVALID1:
        /* Should only be called if we are already in a the RTT_STATE_LINVALID state */
        if (ostate==(RTT_STATE_LINVALID|RTT_STATE_LINVALID1)) {
            rtt_law->nb_linvalid--;
            rtt_law->rtt_stats[s].state=RTT_STATE_OK;
	} else {
            rtt_law->rtt_stats[s].state&=~RTT_STATE_LINVALID1;
	}
        break;
    case RTT_STATE_OK|RTT_STATE_LINVALID2:
        /* Should only be called if we are already in a the RTT_STATE_LINVALID state */
        if (ostate==(RTT_STATE_LINVALID|RTT_STATE_LINVALID2)) {
            rtt_law->nb_linvalid--;
            rtt_law->rtt_stats[s].state=RTT_STATE_OK;
	} else {
            rtt_law->rtt_stats[s].state&=~RTT_STATE_LINVALID2;
	}
        break;
    case RTT_STATE_HINVALID:
        if (ostate==RTT_STATE_REDO)
            rtt_law->nb_redo--;
        else if ((ostate & RTT_STATE_MASK)==RTT_STATE_LINVALID) {
            rtt_law->nb_linvalid--;
        }
        rtt_law->nb_hinvalid++;
#ifdef _DEBUG
        /* printf("invalidating %d %9.7f > %9.7f\n",s,rtt_law->rtt_stats[s].min_rtt,max_rtt); */
        rtt_law->rtt_stats[s].min_rtt_sav=rtt_law->rtt_stats[s].min_rtt;
#endif
        rtt_law->rtt_stats[s].min_rtt=max_rtt;
        rtt_law->rtt_stats[s].state=state;
        break;
    case RTT_STATE_LINVALID|RTT_STATE_LINVALID1:
        if (ostate==RTT_STATE_REDO)
            rtt_law->nb_redo--;
        if ((ostate & RTT_STATE_MASK)!=RTT_STATE_LINVALID)
            rtt_law->nb_linvalid++;
        rtt_law->rtt_stats[s].state=RTT_STATE_LINVALID | (rtt_law->rtt_stats[s].state & ~RTT_STATE_MASK) | RTT_STATE_LINVALID1;
        break;
    case RTT_STATE_LINVALID|RTT_STATE_LINVALID2:
        if (ostate==RTT_STATE_REDO)
            rtt_law->nb_redo--;
        if ((ostate & RTT_STATE_MASK)!=RTT_STATE_LINVALID)
            rtt_law->nb_linvalid++;
        rtt_law->rtt_stats[s].state=RTT_STATE_LINVALID | (rtt_law->rtt_stats[s].state & ~RTT_STATE_MASK) | RTT_STATE_LINVALID2;
        break;
    case RTT_STATE_REDO:
        if (ostate==RTT_STATE_OK) {
            rtt_law->nb_redo++;
        }
        rtt_law->rtt_stats[s].state=state;
        break;
    }
    return ostate;
}


int method_init_probe(bp_handle* probe, int method)
{
    /* nothing to do it seems */
    return 0;
}

int method_do_probe(bp_handle* probe_handle, host_stats_t* host, int packet_size, rtt_law_t* rtt_law, int index)
{
    bp_probedata_t probe;
    int res;

    /*printf("method_do_probe target=%s port=%d ttl=%d size=%d\n",host_addr2name(&host->address,0),SOCKADDR_IN(&host->address)->sin_port,host->ttl,packet_size);*/
    res=do_probe(probe_handle,
        &host->address,
        packet_size,
        host->ttl,&probe);
    if (probe.contents!=NULL)
        free(probe.contents);
    switch (res) {
    case BP_RES_TTL_EXCEEDED:
        /* (!!) this is really bad because we should have the right rtt */
        printf("(!!) BP_RES_TTL_EXCEEDED\n");fflush(stdout);
        break;
    case BP_RES_HIT:
        /* update the stats about this host */
        /* (!!) is it packet_size or probe->size or does it depend on the method ? */
        return rtt_law_add(rtt_law,index,2*probe.size,probe.rtt);
        break;
    case BP_RES_TIMEOUT:
        /* this is something to expect */
        printf("method_do_probe *time out*\n");fflush(stdout);
        rtt_law_add(rtt_law,index,-1,0.0);
        break;
    case -1:
        printf("method_do_probe *failed*\n");fflush(stdout);
        /* (!!) we should display an error message, this is not supposed to happen */
        /*(!!)fprintf(stderr,"%s: error: could not send probe of size %d to %s\n",
            tool_name,packet_size,
            host->name,
            host_addr2name(&host->address,0)); / * (!!) is it 0 here ? */
        printf("(!!) could not send probe\n");fflush(stdout);
        break;
    default:
        /* (!!) display more details, there may also be more cases to handle */
        /*(!!)fprintf(stderr,"%s: error: failed probe of size %d to %s\n",
            tool_name,packet_size,
            host->name,
            host_addr2name(&host->address,0)); / * (!!) is it 0 here ? */
        printf("(!!) unknown result %d\n",res);fflush(stdout);
        break;
    }
    return 0;
}