aboutsummaryrefslogtreecommitdiff
path: root/comparison_report.c
blob: a52cb60638ac791d2aec8d91ecdaef9c4b7cfaaf (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
/*
 *  comparison_report.c
 *
 *  Copyright (C) 2014, Linaro Limited.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * Contributors:
 *     Tuukka Tikkanen <tuukka.tikkanen@linaro.org>
 *
 */
#include <float.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <malloc.h>
#include <math.h>
#include <assert.h>
#include "report_ops.h"
#include "idlestat.h"
#include "utils.h"
#include "compiler.h"

struct compare_report_data {
	struct cpuidle_cstate *curr_cstate_baseline;
	struct cpufreq_pstate *curr_pstate_baseline;
};

static void display_factored_time_delta(double time, int align)
{
	char buffer[128];

	if (fabs(time) < 1000.0) {
		snprintf(buffer, sizeof(buffer), "%+.0lfus", time);
		printf("%*s", align, buffer);
	}
	else if (fabs(time) < 1000000.0) {
		snprintf(buffer, sizeof(buffer), "%+.1lfms", time / 1000.0);
		printf("%*s", align, buffer);
	}
	else if (fabs(time) < 100000000000.0) {
		snprintf(buffer, sizeof(buffer), "%+.1lfs", time / 1000000.0);
		printf("%*s", align, buffer);
	}
	else
		printf("%.*s", align, "                            ");
}

static void display_int_delta(int value, int align)
{
	printf(value ? " %+*d |" : " %*d |", align, value);
}


static int compare_check_options(struct program_options *options)
{
	if (options->baseline_filename == NULL) {
  		fprintf(stderr,
			"Error: Comparison report requires baseline trace\n");
		return -1;
	}

	return 0;
}

static void * compare_alloc_data(UNUSED struct program_options *options)
{
	struct compare_report_data *ret = calloc(sizeof(*ret), 1);

	if (ret == NULL)
		return ptrerror(__func__);

	return ret;
}

static void compare_release_data(void *data)
{
	free(data);
}


static void compare_cstate_single_state(struct cpuidle_cstate *c,
					void *report_data)
{
	struct cpuidle_cstate empty;
	struct cpuidle_cstate diff;
	struct cpuidle_cstate *b;
	struct compare_report_data *rdata;

	assert(report_data != NULL);
	rdata = report_data;

	/*
	 * On entry, either current state or baseline state might be NULL:
	 * c = NULL implies this state exists only in baseline
	 * curr_baseline = NULL implies this state exists only in trace
	 * It should never occur that both c and current baseline are NULL.
	 *
	 * If c is NULL, set c to point to empty state and alias the name
	 * in baseline.
	 *
	 * If no current baseline exists, use empty state as baseline.
	 */
	b = rdata->curr_cstate_baseline;
	rdata->curr_cstate_baseline = NULL;

	assert(c != NULL || b != NULL);

	if (c == NULL) {
		memset(&empty, 0, sizeof(empty));
		empty.name = b->name;
		c = &empty;
	}

	if (b == NULL) {
		memset(&empty, 0, sizeof(empty));
		b = &empty;
	}

	diff.min_time = c->min_time - b->min_time;
	diff.max_time = c->max_time - b->max_time;
	diff.avg_time = c->avg_time - b->avg_time;
	diff.duration = c->duration - b->duration;
	diff.nrdata = c->nrdata - b->nrdata;
	diff.early_wakings = c->early_wakings - b->early_wakings;
	diff.late_wakings = c->late_wakings - b->late_wakings;

	printf("| %8s | ", c->name);
	display_factored_time(c->min_time == DBL_MAX ? 0. :
				      c->min_time, 8);
	printf(" | ");
	display_factored_time(c->max_time, 8);
	printf(" | ");
	display_factored_time(c->avg_time, 8);
	printf(" | ");
	display_factored_time(c->duration, 8);
	printf(" | ");
	printf("%5d | %5d | %5d |\n", c->nrdata, c->early_wakings, c->late_wakings);
	/* Delta */
	printf("|          | ");
	display_factored_time_delta(diff.min_time, 8);
	printf(" | ");
	display_factored_time_delta(diff.max_time, 8);
	printf(" | ");
	display_factored_time_delta(diff.avg_time, 8);
	printf(" | ");
	display_factored_time_delta(diff.duration, 8);
	printf(" |");
	display_int_delta(diff.nrdata, 5);
	display_int_delta(diff.early_wakings, 5);
	display_int_delta(diff.late_wakings, 5);
	printf("\n");
}

static void compare_set_baseline_cstate(struct cpuidle_cstate *b,
					void *report_data)
{
	struct compare_report_data *rdata;

	assert(report_data != NULL);
	rdata = report_data;

	/* Process previous state if trace did not have data */
	if (rdata->curr_cstate_baseline)
		compare_cstate_single_state(NULL, report_data);

	if (b == NULL)
		return;

	rdata->curr_cstate_baseline = b;
}


static void compare_cstate_end_cpu(void *report_data)
{
	compare_set_baseline_cstate(NULL, report_data);
}


static void compare_pstate_single_freq(struct cpufreq_pstate *p,
				       void *report_data)
{
	struct cpufreq_pstate empty;
	struct cpufreq_pstate diff;
	struct cpufreq_pstate *b;
	struct compare_report_data *rdata;

	assert(report_data != NULL);
	rdata = report_data;

	/*
	 * On entry, either current state or baseline state might be NULL:
	 * p = NULL implies this state exists only in baseline
	 * curr_baseline = NULL implies this state exists only in trace
	 * It should never occur that both p and current baseline are NULL.
	 *
	 * If p is NULL, set p to point to empty state and copy the frequency
	 * from baseline.
	 *
	 * If no current baseline exists, use empty state as baseline.
	 */
	b = rdata->curr_pstate_baseline;
	rdata->curr_pstate_baseline = NULL;

	assert(p != NULL || b != NULL);

	if (p == NULL) {
		memset(&empty, 0, sizeof(empty));
		empty.freq = b->freq;
		p = &empty;
	}

	if (b == NULL) {
		memset(&empty, 0, sizeof(empty));
		b = &empty;
	}

	diff.min_time = p->min_time - b->min_time;
	diff.max_time = p->max_time - b->max_time;
	diff.avg_time = p->avg_time - b->avg_time;
	diff.duration = p->duration - b->duration;
	diff.count = p->count - b->count;

	printf("| ");
	display_factored_freq(p->freq, 8);
	printf(" | ");
	display_factored_time(p->min_time == DBL_MAX ? 0. : p->min_time, 8);
	printf(" | ");
	display_factored_time(p->max_time, 8);
	printf(" | ");
	display_factored_time(p->avg_time, 8);
	printf(" | ");
	display_factored_time(p->duration, 8);
	printf(" | %5d |\n", p->count);

	printf("|          | ");
	display_factored_time_delta(diff.min_time, 8);
	printf(" | ");
	display_factored_time_delta(diff.max_time, 8);
	printf(" | ");
	display_factored_time_delta(diff.avg_time, 8);
	printf(" | ");
	display_factored_time_delta(diff.duration, 8);
	printf(" |");
	display_int_delta(diff.count, 5);
	printf("\n");
}

static void compare_set_baseline_pstate(struct cpufreq_pstate *b,
					void *report_data)
{
	struct compare_report_data *rdata;

	assert(report_data != NULL);
	rdata = report_data;

	/* Process previous state if trace did not have data */
	if (rdata->curr_pstate_baseline)
		compare_pstate_single_freq(NULL, report_data);

	if (b == NULL)
		return;

	rdata->curr_pstate_baseline = b;
}


static void compare_pstate_end_cpu(void *report_data)
{
	compare_set_baseline_pstate(NULL, report_data);
}


static int copy_ops_from_default(struct report_ops *);

static struct report_ops comparison_report_ops = {
	.name = "comparison",
	.prepare = copy_ops_from_default,
	.check_options = compare_check_options,
	.allocate_report_data = compare_alloc_data,
	.release_report_data = compare_release_data,

	.cstate_baseline_state = compare_set_baseline_cstate,
	.cstate_single_state = compare_cstate_single_state,
	.cstate_end_cpu = compare_cstate_end_cpu,

	.pstate_baseline_freq = compare_set_baseline_pstate,
	.pstate_single_freq = compare_pstate_single_freq,
	.pstate_end_cpu = compare_pstate_end_cpu,
};

static int copy_ops_from_default(struct report_ops *self)
{
	struct report_ops *def = get_report_ops("default");

	assert(self == &comparison_report_ops);

	if (is_err(def)) {
		fprintf(stderr,
			"Comparison report: cannot copy ops from default\n");
		return -1;
	}

	comparison_report_ops.check_output = def->check_output;

	comparison_report_ops.open_report_file = def->open_report_file;
	comparison_report_ops.close_report_file = def->close_report_file;

	comparison_report_ops.cstate_table_header = def->cstate_table_header;
	comparison_report_ops.cstate_table_footer = def->cstate_table_footer;
	comparison_report_ops.cstate_cpu_header = def->cstate_cpu_header;

	comparison_report_ops.pstate_table_header = def->pstate_table_header;
	comparison_report_ops.pstate_table_footer = def->pstate_table_footer;
	comparison_report_ops.pstate_cpu_header = def->pstate_cpu_header;

	comparison_report_ops.wakeup_table_header = def->wakeup_table_header;
	comparison_report_ops.wakeup_table_footer = def->wakeup_table_footer;
	comparison_report_ops.wakeup_cpu_header = def->wakeup_cpu_header;
	comparison_report_ops.wakeup_single_irq = def->wakeup_single_irq;
	comparison_report_ops.wakeup_end_cpu = def->wakeup_end_cpu;

	return 0;
}

EXPORT_REPORT_OPS(comparison);