-
Notifications
You must be signed in to change notification settings - Fork 2
/
netstat.c
205 lines (173 loc) · 3.22 KB
/
netstat.c
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
/*
* netstat.c
*
*/
#include "copyright2.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <math.h>
#include <errno.h>
#include "netrek.h"
/*
* #define NETDEBUG
*/
static int lastread;
static int dead;
static int start;
static int counter;
static int sum, n; /* total since last reset */
static double s2;
static int M, var;
static double sd;
static int suml, nl; /* total since last death */
static double s2l;
static int Ml, varl;
static double sdl;
static int nf; /* network failures */
static char nfthresh_s[8] = NETSTAT_DF_NFT_S;
static int nfthresh = NETSTAT_DF_NFT;
void
ns_init(v)
int v;
{
start = v;
sum = n = 0;
s2 = 0.;
sd = 0.;
suml = nl = 0;
s2l = 0.;
nf = 0;
sdl = 0.;
}
void
ns_record_update(count)
int count;
{
int now;
int et;
static int lastupdateSpeed;
static int lasttime = -1;
et = 1000 / updates_per_second; /* expected time */
if (!me)
return;
now = mstime();
if (lasttime < (et + et / 4) && now - lastread < et / 2) {
#ifdef NETDEBUG
printf("skipping %d\n", now - lastread);
#endif
return;
}
/* wait a few updates to stabilize */
if (start) {
start--;
lastread = now;
return;
}
/* wait a few updates to stabilize */
if (me->p_status != PALIVE) {
#ifdef NETDEBUG
printf("waiting after death...\n");
#endif
dead = 5;
lastread = now;
suml = nl = 0;
s2l = 0;
return;
} else if (dead) {
dead--;
lastread = now;
return;
}
/* reset if we change updates */
if (updates_per_second != lastupdateSpeed) {
ns_init(3);
lastupdateSpeed = updates_per_second;
lastread = now;
return;
}
lastupdateSpeed = updates_per_second;
lasttime = now - lastread;
if (lasttime >= nfthresh) {
nf++; /* network failure */
updateLMeter();
#ifdef NETDEBUG
printf("network failure: %d\n", lasttime);;
#endif
} else {
counter++;
ns_do_stat(lasttime, counter);
#ifdef NETDEBUG
printf("%d\n", lasttime);
#endif
}
lastread = now;
}
void
ns_do_stat(v, c)
int v;
int c;
{
int uf;
n++;
nl++;
sum += v;
suml += v;
s2 += (v * v);
s2l += (v * v);
if (n <= 1 || nl <= 1)
return;
uf = (updates_per_second * 10) / netstatfreq;
if (uf == 0)
uf = 1;
if ((c % uf) == 0) {
M = sum / n;
var = (s2 - (int)(M * sum)) / (int)(n - 1);
sd = (int) sqrt((double) var);
Ml = suml / nl;
varl = (s2l - (int)(Ml * suml)) / (int)(nl - 1);
sdl = (int) sqrt((double) varl);
updateLMeter();
}
}
double
ns_get_tstat()
{
return sd;
}
double
ns_get_lstat()
{
return sdl;
}
int
ns_get_nfailures()
{
return nf;
}
char *
ns_get_nfthresh_s()
{
return nfthresh_s;
}
void
ns_set_nfthresh_s(s)
char *s;
{
strcpy(nfthresh_s, s);
}
int
ns_get_nfthresh()
{
return nfthresh;
}
void
ns_set_nfthresh(v)
int v;
{
nfthresh = v;
}