summaryrefslogtreecommitdiff
path: root/minix/commands/sprofdiff/sprofdiff.c
blob: 8009561445a8ed1e3abc8c0635337d3efc0f3acd (plain)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include <assert.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <minix/type.h>

#include "tdist.h"

/* user-configurable settings */
#define DEBUG 0

#define PROC_NAME_WIDTH 10

#define SYMBOL_NAME_WIDTH 24

/* types */
#define SYMBOL_HASHTAB_SIZE 1024

#define SYMBOL_NAME_SIZE 52

struct symbol_count {
	unsigned long sum;
	unsigned long long sum2;
	unsigned long min;
	unsigned long max;
};

enum symbol_class {
	sc_total,
	sc_idle,
	sc_system,
	sc_user,
	sc_process,
	sc_symbol
};

struct symbol_info {
	struct symbol_info *next;
	struct symbol_info *hashtab_next;
	char binary[PROC_NAME_LEN];
	char name[SYMBOL_NAME_SIZE];
	struct symbol_count count[2];
	long diff;
	enum symbol_class class;
};

/* global variables */
static unsigned n1, n2;
static struct symbol_info *symbols;
static struct symbol_info *symbol_hashtab[SYMBOL_HASHTAB_SIZE];

/* prototypes */
static double compute_sig(double avg1, double var1, double avg2, double var2);
static void compute_stats(const struct symbol_count *count, unsigned n,
	double *avg, double *var);
static void load_file(const char *path, int count_index);
static void *malloc_checked(size_t size);
static void print_report(void);
static void print_report_line(const struct symbol_info *symbol);
static int read_line(FILE *file, const char *path, int line, char *binary,
	char *name, unsigned long *samples);
static enum symbol_class symbol_classify(const char *binary, const char *name);
static unsigned string_hash(const char *s, size_t size);
static struct symbol_info *symbol_find_or_add(const char *binary,
	const char *name);
static unsigned symbol_hash(const char *binary, const char *name);
static int symbol_qsort_compare(const void *p1, const void *p2);
static void symbol_tally(const char *binary, const char *name,
	unsigned long samples, int count_index);
static unsigned symbols_count(void);
static void usage(const char *argv0);

#define MALLOC_CHECKED(type, count) \
	((type *) malloc_checked(sizeof(type) * (count)))

#if DEBUG
#define dprintf(...) do { 						\
	fprintf(stderr, "debug(%s:%d): ", __FUNCTION__, __LINE__); 	\
	fprintf(stderr, __VA_ARGS__); 					\
} while(0)
#else
#define dprintf(...)
#endif

int main(int argc, char **argv) {
	int i;

#ifdef DEBUG
	/* disable buffering so the output mixes correctly */
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);
#endif

	if (argc < 3) usage(argv[0]);

	/* load left-hand files */
	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "-r") == 0) {
			i++;
			break;
		}
		if (argc == 3 && i == 2) break;
		load_file(argv[i], 0);
		n1++;
	}

	/* load right-hand files */
	for (; i < argc; i++) {
		load_file(argv[i], 1);
		n2++;
	}

	if (n1 < 1 || n2 < 1) usage(argv[0]);

	/* report analysis results */
	print_report();
	return 0;
}

static double compute_sig(double avg1, double var1, double avg2, double var2) {
	double df, t, var;

	/* prevent division by zero with lack of variance */
	var = var1 / n1 + var2 / n2;
	if (var <= 0 || n1 <= 1 || n2 <= 1) return -1;

	/* do we have enough degrees of freedom? */
	df = var * var / (
		var1 * var1 / (n1 * n1 * (n1 - 1)) +
		var2 * var2 / (n2 * n2 * (n2 - 1)));
	if (df < 1) return -1;

	/* perform t-test */
	t = (avg1 - avg2) / sqrt(var);
	return student_t_p_2tail(t, df);
}

static void compute_stats(const struct symbol_count *count, unsigned n,
	double *avg, double *var) {
	double sum;

	assert(count);
	assert(avg);
	assert(var);

	sum = count->sum;
	if (n < 1) {
		*avg = 0;
	} else {
		*avg = sum / n;
	}

	if (n < 2) {
		*var = 0;
	} else {
		*var = (count->sum2 - sum * sum / n) / (n - 1);
	}
}

static void load_file(const char *path, int count_index) {
	char binary[PROC_NAME_LEN];
	FILE *file;
	int line;
	char name[SYMBOL_NAME_SIZE];
	unsigned long samples;

	assert(path);
	assert(count_index == 0 || count_index == 1);

	file = fopen(path, "r");
	if (!file) {
		fprintf(stderr, "error: cannot open \"%s\": %s\n",
			path, strerror(errno));
		exit(1);
	}

	line = 1;
	while (read_line(file, path, line++, binary, name, &samples)) {
		symbol_tally(binary, name, samples, count_index);
	}

	fclose(file);
}

static void *malloc_checked(size_t size) {
	void *p;
	if (!size) return NULL;
	p = malloc(size);
	if (!p) {
		fprintf(stderr, "error: malloc cannot allocate %lu bytes: %s\n",
			(unsigned long) size, strerror(errno));
		exit(-1);
	}
	return p;
}

static void print_report(void) {
	unsigned i, index, symbol_count;
	struct symbol_info *symbol, **symbol_list;

	/* list the symbols in an array for sorting */
	symbol_count = symbols_count();
	symbol_list = MALLOC_CHECKED(struct symbol_info *, symbol_count);
	index = 0;
	for (symbol = symbols; symbol; symbol = symbol->next) {
		symbol_list[index++] = symbol;

		/* sort by difference in average, multiply both sides by
		 * n1 * n2 to avoid division
		 */
		symbol->diff = (long) (symbol->count[1].sum * n1) -
			(long) (symbol->count[0].sum * n2);
	}
	assert(index == symbol_count);

	/* sort symbols  */
	qsort(symbol_list, symbol_count, sizeof(struct symbol_info *),
		symbol_qsort_compare);

	printf("%-*s %-*s ------avg------ ----stdev----    diff sig\n",
		PROC_NAME_WIDTH, "binary", SYMBOL_NAME_WIDTH, "symbol");
	printf("%-*s    left   right   left  right\n",
		PROC_NAME_WIDTH + SYMBOL_NAME_WIDTH + 1, "");
	printf("\n");
	for (i = 0; i < symbol_count; i++) {
		if (i > 0 && symbol_list[i]->class >= sc_process &&
			symbol_list[i]->class != symbol_list[i - 1]->class) {
			printf("\n");
		}
		print_report_line(symbol_list[i]);
	}
	printf("\n");
	printf("significance levels (two-tailed):\n");
	printf("  *    p < 0.05\n");
	printf("  **   p < 0.01\n");
	printf("  ***  p < 0.001\n");
	free(symbol_list);
}

static void print_report_line(const struct symbol_info *symbol) {
	double avg1, avg2, p, var1, var2;

	/* compute statistics; t is Welch's t, which is a t-test that allows
	 * for unpaired samples with unequal variance; df is the degrees of
	 * freedom as given by the Welch-Satterthwaite equation
	 */
	compute_stats(&symbol->count[0], n1, &avg1, &var1);
	compute_stats(&symbol->count[1], n2, &avg2, &var2);
	p = compute_sig(avg1, var1, avg2, var2);

	/* list applicable values */
	assert(PROC_NAME_WIDTH <= PROC_NAME_LEN);
	assert(SYMBOL_NAME_WIDTH <= SYMBOL_NAME_SIZE);
	printf("%-*.*s %-*.*s",
		PROC_NAME_WIDTH, PROC_NAME_WIDTH, symbol->binary,
		SYMBOL_NAME_WIDTH, SYMBOL_NAME_WIDTH, symbol->name);
	if (symbol->count[0].sum > 0) {
		printf("%8.0f", avg1);
	} else {
		printf("        ");
	}
	if (symbol->count[1].sum > 0) {
		printf("%8.0f", avg2);
	} else {
		printf("        ");
	}
	if (symbol->count[0].sum > 0 && n1 >= 2) {
		printf("%7.0f", sqrt(var1));
	} else {
		printf("       ");
	}
	if (symbol->count[1].sum > 0 && n2 >= 2) {
		printf("%7.0f", sqrt(var2));
	} else {
		printf("       ");
	}
	printf("%8.0f ", avg2 - avg1);
	if (p >= 0) {
		if (p <= 0.05) printf("*");
		if (p <= 0.01) printf("*");
		if (p <= 0.001) printf("*");
	}
	printf("\n");
}

static int read_line(FILE *file, const char *path, int line, char *binary, 
	char *name, unsigned long *samples) {
	int c, index;

	assert(file);
	assert(binary);
	assert(name);
	assert(samples);

	c = fgetc(file);
	if (c == EOF) return 0;

	/* read binary name, truncating if necessary */
	index = 0;
	while (c != '\t' && c != '\n') {
		if (index < PROC_NAME_LEN) binary[index++] = c;
		c = fgetc(file);
	}
	if (index < PROC_NAME_LEN) binary[index] = 0;

	/* read tab */
	if (c != '\t') {
		fprintf(stderr, "error: garbage %d after binary name "
			"(\"%s\", line %d)\n", c, path, line);
		exit(1);
	}
	c = fgetc(file);

	/* read symbol name, truncating if necessary */
	index = 0;
	while (c != '\t' && c != '\n') {
		if (index < SYMBOL_NAME_SIZE) name[index++] = c;
		c = fgetc(file);
	}
	if (index < SYMBOL_NAME_SIZE) name[index] = 0;

	/* read tab */
	if (c != '\t') {
		fprintf(stderr, "error: garbage %d after symbol name "
			"(\"%s\", line %d)\n", c, path, line);
		exit(1);
	}
	c = fgetc(file);

	/* read number of samples */
	*samples = 0;
	while (c >= '0' && c <= '9') {
		*samples = *samples * 10 + (c - '0');
		c = fgetc(file);
	}

	/* read newline */
	if (c != '\n') {
		fprintf(stderr, "error: garbage %d after sample count "
			"(\"%s\", line %d)\n", c, path, line);
		exit(1);
	}
	return 1;
}

static unsigned string_hash(const char *s, size_t size) {
	unsigned result = 0;

	assert(s);

	while (*s && size-- > 0) {
		result = result * 31 + *(s++);
	}
	return result;
}

static enum symbol_class symbol_classify(const char *binary, const char *name) {
	if (strncmp(binary, "(total)", PROC_NAME_LEN) == 0) return sc_total;
	if (strncmp(binary, "(idle)", PROC_NAME_LEN) == 0) return sc_idle;
	if (strncmp(binary, "(system)", PROC_NAME_LEN) == 0) return sc_system;
	if (strncmp(binary, "(user)", PROC_NAME_LEN) == 0) return sc_user;
	if (strncmp(name, "(total)", SYMBOL_NAME_SIZE) == 0) return sc_process;
	return sc_symbol;
}

static struct symbol_info *symbol_find_or_add(const char *binary,
	const char *name) {
	struct symbol_info **ptr, *symbol;

	assert(binary);
	assert(name);

	/* look up symbol in hash table */
	ptr = &symbol_hashtab[symbol_hash(binary, name) % SYMBOL_HASHTAB_SIZE];
	while ((symbol = *ptr)) {
		if (strncmp(symbol->binary, binary, PROC_NAME_LEN) == 0 &&
			strncmp(symbol->name, name, SYMBOL_NAME_SIZE) == 0) {
			return symbol;
		}
		ptr = &symbol->hashtab_next;
	}

	/* unknown symbol, add it */
	*ptr = symbol = MALLOC_CHECKED(struct symbol_info, 1);
	memset(symbol, 0, sizeof(struct symbol_info));
	strncpy(symbol->binary, binary, PROC_NAME_LEN);
	strncpy(symbol->name, name, SYMBOL_NAME_SIZE);
	symbol->count[0].min = ~0UL;
	symbol->count[1].min = ~0UL;
	symbol->class = symbol_classify(binary, name);

	/* also add to linked list */
	symbol->next = symbols;
	symbols = symbol;
	return symbol;
}

static unsigned symbol_hash(const char *binary, const char *name) {
	return string_hash(binary, PROC_NAME_LEN) +
		string_hash(name, SYMBOL_NAME_SIZE);
}

static int symbol_qsort_compare(const void *p1, const void *p2) {
	int r;
	const struct symbol_info *s1, *s2;

	assert(p1);
	assert(p2);
	s1 = *(const struct symbol_info **) p1;
	s2 = *(const struct symbol_info **) p2;
	assert(s1);
	assert(s2);

	/* totals come first */
	if (s1->class < s2->class) return -1;
	if (s1->class > s2->class) return 1;

	/* sort by difference in average */
	if (s1->diff < s2->diff) return -1;
	if (s1->diff > s2->diff) return 1;

	/* otherwise, by name */
	r = strncmp(s1->binary, s2->binary, PROC_NAME_LEN);
	if (r) return r;

	return strncmp(s1->name, s2->name, SYMBOL_NAME_SIZE);
}

static void symbol_tally(const char *binary, const char *name,
	unsigned long samples, int count_index) {
	struct symbol_count *count;
	struct symbol_info *symbol;

	/* look up or add symbol */
	symbol = symbol_find_or_add(binary, name);

	/* update count */
	count = &symbol->count[count_index];
	count->sum += samples;
	count->sum2 += (unsigned long long) samples * samples;
	if (count->min > samples) count->min = samples;
	if (count->max < samples) count->max = samples;
}

static unsigned symbols_count(void) {
	int count = 0;
	const struct symbol_info *symbol;

	for (symbol = symbols; symbol; symbol = symbol->next) {
		count++;
	}
	return count;
}

static void usage(const char *argv0) {
	printf("usage:\n");
	printf("  %s leftfile rightfile\n", argv0);
	printf("  %s leftfile... -r rightfile...\n", argv0);
	printf("\n");
	printf("sprofdiff compares the sprofile information from multiple\n");
	printf("output files of sprofalyze -d.\n");
	exit(1);
}