File: util.c

package info (click to toggle)
echoping 6.0.2-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 9,296 kB
  • ctags: 515
  • sloc: sh: 8,951; ansic: 3,352; makefile: 170
file content (262 lines) | stat: -rw-r--r-- 6,034 bytes parent folder | download | duplicates (5)
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
/* Most of it stolen from Pierre Beyssac's bing */

/* $Id: util.c 377 2007-03-12 20:48:05Z bortz $ */

#include "echoping.h"

#define STATES 32

#include <time.h>
#include <ctype.h>

char           *
random_string(unsigned length)
{

	char           *state = (char *) malloc(sizeof(char) * STATES);
	char           *result = (char *) malloc(length + 1);
	int             i, number;
	unsigned        seed = (unsigned) time((time_t *) NULL);

	/* printf ("Seed is %u\n", seed); */

	/* Initialize random generator */
	(void) initstate(seed, state, STATES);

	for (i = 0; i < length; i++) {
		number = (random() % 94) + 33;
		/* printf ("Number for %d is %d\n", i, number); */
		result[i] = (char) number;
	}
	result[length] = '\0';

	/* printf ("Result is %s\n", result); */

	return result;

}

char           *
to_upper(char *input)
{
	int             c;
	char           *result;
	result = (char *) malloc(strlen(input));
	for (c = 0; c < strlen(input); c++)
		result[c] = toupper((int)input[c]);
	result[strlen(input)] = '\0';
	return result;
}

/*
 * tvsub -- Subtract 2 timeval structs:  out = out - in. Out is assumed to be
 * >= in. Comes from the bing program. 
 */
void
tvsub(out, in)
	struct timeval *out, *in;
{
	if ((out->tv_usec -= in->tv_usec) < 0) {
		--out->tv_sec;
		out->tv_usec += 1000000;
	}
	out->tv_sec -= in->tv_sec;
}

/* tvadd -- Adds 2 timeval structs:  out = out + in. */
void
tvadd(out, in)
	struct timeval *out, *in;
{
	if ((out->tv_usec += in->tv_usec) >= 1000000) {
		++out->tv_sec;
		out->tv_usec -= 1000000;
	}
	out->tv_sec += in->tv_sec;
}

/* tvavg -- Averages a timeval struct */
void
tvavg(out, number)
	struct timeval *out;
	int             number;
{
	double          result;
	/* 
	 * out->tv_sec = out->tv_sec/number; out->tv_usec =
	 * out->tv_usec/number; 
	 */
	result = (1000000 * out->tv_sec + out->tv_usec) / number;
	/* printf ("Result of average is %f\n", result) */ ;
	out->tv_sec = (long) (result / 1000000);
	out->tv_usec = (long) (result - (out->tv_sec * 1000000));
}

/* tvstddev -- Computes the standard deviation of a set of results */
void
tvstddev(out, number, average, results)
	struct timeval *out;
	int             number;
	struct timeval  average;
	struct result  *results;
{
	int             i;
	struct timeval  result = null_timeval;
	struct timeval  avg = null_timeval;
#ifdef DEBUG
	struct timeval  var = null_timeval;
#endif
	struct timeval  large, small;
	double          d_offset, d_square, d_variance = 0;
	*out = null_timeval;
	for (i = 0; i < number; i++) {
		if (results[i].valid == 1) {
			result = results[i].timevalue;
#ifdef DEBUG
			printf("DEBUG: Value is %f (average is %f)\n", tv2double
			       (result), tv2double(average));
#endif
			avg = average;
			if (tvcmp(&result, &avg) == -1) {
				small = result;
				large = avg;
			} else {
				large = result;
				small = avg;
			}
			tvsub(&large, &small);
#ifdef DEBUG
			printf("abs offset is %f\n", tv2double(large));
#endif
			d_offset = tv2double(large);
			d_square = d_offset * d_offset;
			d_variance += d_square;
#ifdef DEBUG
			printf("variance is now %f\n", tv2double(var));
#endif
		}
	}
	result = double2tv(sqrt(d_variance / (double) number));
	out->tv_sec = result.tv_sec;
	out->tv_usec = result.tv_usec;
}


 /* tvstddevavg -- Computes the average of values within a set of results where the
  * sample is within the given number of standard deviations from the average */
/* TODO: IWBN to return the number of excluded outliers */
void
tvstddevavg(out, number, average, results, n_stddev)
	struct timeval *out;	/* contains std dev on entry */
	int             number;
	struct timeval  average;
	struct result  *results;
	double          n_stddev;
{
	int             i, valid = 0;
	struct timeval  result;	/* working value */
	struct timeval  var = null_timeval;	/* result accumulator */
	double          x;
	double          maxdev = tv2double(*out) * n_stddev;

	if (tvcmp(out, &null_timeval) == 0) {
		/* if the SD is 0 then we just return the average */
		*out = average;
		return;
	}

	for (i = 0; i < number; i++) {
		if (results[i].valid == 1) {
			result = results[i].timevalue;
			tvsub(&result, &average);
			/* printf ("value is %f (stddev is %f)\n", tv2double
			 * (result), tv2double (stddev)); */
			/* ensure that result (difference to average) is absolute
			 * value */
			if (tvcmp(&result, &null_timeval) == -1) {
				result = average;
				tvsub(&result, &results[i].timevalue);
			}
			x = tv2double(result);
			/* printf("value is %g maxdev %g\n",x,maxdev); */
			if (x <= maxdev) {
				/* deviation is less than stddev */
				tvadd(&var, &results[i].timevalue);
				valid++;
			} else {
				/* printf("dropped\n"); */
			}
		}
	}
	/* printf ("total is %f in %d samples\n", tv2double (var), valid); */
	if (valid > 0) {
		*out = double2tv(tv2double(var) / valid);
	} else {
		*out = null_timeval;
	}

}

/* tvcmp -- Compares two timeval structs */
int
tvcmp(left, right)
	struct timeval *left, *right;
{
	if (left->tv_sec < right->tv_sec) {
		return -1;
	}
	if (left->tv_sec > right->tv_sec) {
		return 1;
	}
	if (left->tv_usec < right->tv_usec) {
		return -1;
	}
	if (left->tv_usec > right->tv_usec) {
		return 1;
	}
	return 0;

}

/* tvmin */
void
tvmin(champion, challenger)
	struct timeval *champion, *challenger;
{
	if (tvcmp(champion, challenger) == 1) {
		champion->tv_sec = challenger->tv_sec;
		champion->tv_usec = challenger->tv_usec;
	}
}

/* tvmax */
void
tvmax(champion, challenger)
	struct timeval *champion, *challenger;
{
	if (tvcmp(champion, challenger) == -1) {
		champion->tv_sec = challenger->tv_sec;
		champion->tv_usec = challenger->tv_usec;
	}
}

double
tv2double(tv)
	struct timeval  tv;
{
	double          result;
	result =
	    (((((double) tv.tv_sec) * 1000000.0) + (double) tv.tv_usec) / 1000000.0);
	/* printf ("Double is %9.3f\n", result); */
	return result;
}

struct timeval
double2tv(x)
	double          x;
{
	struct timeval  result;
	result.tv_sec = (int) (x);
	result.tv_usec = (int) ((x - result.tv_sec) * 1000000);
	return result;
}