Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[261633]: utils / ophelp.c Maximize Restore History

Download this file

ophelp.c    517 lines (431 with data), 12.7 kB

  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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/**
* @file ophelp.c
* Print out PMC event information
*
* @remark Copyright 2002 OProfile authors
* @remark Read the file COPYING
*
* @author John Levon
* @author Philippe Elie
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "op_version.h"
#include "op_events.h"
#include "op_popt.h"
#include "op_cpufreq.h"
#include "op_hw_config.h"
#include "op_string.h"
#include "op_alloc_counter.h"
#include "op_parse_event.h"
#include "op_libiberty.h"
static char const ** chosen_events;
struct parsed_event * parsed_events;
static op_cpu cpu_type = CPU_NO_GOOD;
static char * cpu_string;
static int callgraph_depth;
static poptContext optcon;
/// return the Hamming weight (number of set bits)
static size_t hweight(size_t mask)
{
size_t count = 0;
while (mask) {
mask &= mask - 1;
count++;
}
return count;
}
static void do_arch_specific_event_help(struct op_event * event)
{
switch (cpu_type) {
case CPU_PPC64_CELL:
printf("Group %u :", event->val / 100);
break;
default:
break;
}
}
/**
* help_for_event - output event name and description
* @param i event number
*
* output an help string for the event @i
*/
static void help_for_event(struct op_event * event)
{
uint i, j;
uint mask;
size_t nr_counters;
do_arch_specific_event_help(event);
nr_counters = op_get_nr_counters(cpu_type);
printf("%s", event->name);
printf(": (counter: ");
mask = event->counter_mask;
if (hweight(mask) == nr_counters) {
printf("all");
} else {
for (i = 0; i < CHAR_BIT * sizeof(event->counter_mask); ++i) {
if (mask & (1 << i)) {
printf("%d", i);
mask &= ~(1 << i);
if (mask)
printf(", ");
}
}
}
printf(")\n\t%s (min count: %d)\n", event->desc, event->min_count);
if (strcmp(event->unit->name, "zero")) {
printf("\tUnit masks (default 0x%x)\n",
event->unit->default_mask);
printf("\t----------\n");
for (j = 0; j < event->unit->num; j++) {
printf("\t0x%.2x: %s\n",
event->unit->um[j].value,
event->unit->um[j].desc);
}
}
}
static void check_event(struct parsed_event * pev,
struct op_event const * event)
{
int ret;
int min_count;
int const callgraph_min_count_scale = 15;
if (!event) {
fprintf(stderr, "No event named %s is available.\n",
pev->name);
exit(EXIT_FAILURE);
}
ret = op_check_events(0, event->val, pev->unit_mask, cpu_type);
if (ret & OP_INVALID_UM) {
fprintf(stderr, "Invalid unit mask 0x%x for event %s\n",
pev->unit_mask, pev->name);
exit(EXIT_FAILURE);
}
min_count = event->min_count;
if (callgraph_depth)
min_count *= callgraph_min_count_scale;
if (pev->count < min_count) {
fprintf(stderr, "Count %d for event %s is below the "
"minimum %d\n", pev->count, pev->name, min_count);
exit(EXIT_FAILURE);
}
}
static void resolve_events(void)
{
size_t count;
size_t i, j;
size_t * counter_map;
size_t nr_counters = op_get_nr_counters(cpu_type);
struct op_event const * selected_events[nr_counters];
count = parse_events(parsed_events, nr_counters, chosen_events);
if (count > nr_counters) {
fprintf(stderr, "Not enough hardware counters.\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < count; ++i) {
for (j = i + 1; j < count; ++j) {
struct parsed_event * pev1 = &parsed_events[i];
struct parsed_event * pev2 = &parsed_events[j];
if (!strcmp(pev1->name, pev2->name) &&
pev1->count == pev2->count &&
pev1->unit_mask == pev2->unit_mask &&
pev1->kernel == pev2->kernel &&
pev1->user == pev2->user) {
fprintf(stderr, "All events must be distinct.\n");
exit(EXIT_FAILURE);
}
}
}
for (i = 0; i < count; ++i) {
struct parsed_event * pev = &parsed_events[i];
selected_events[i] = find_event_by_name(pev->name);
check_event(pev, selected_events[i]);
}
counter_map = map_event_to_counter(selected_events, count, cpu_type);
if (!counter_map) {
fprintf(stderr, "Couldn't allocate hardware counters for the selected events.\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < count; ++i)
printf("%d ", (unsigned int) counter_map[i]);
printf("\n");
free(counter_map);
}
static void show_unit_mask(void)
{
struct op_event * event;
size_t count;
size_t nr_counter = op_get_nr_counters(cpu_type);
count = parse_events(parsed_events, nr_counter, chosen_events);
if (count > 1) {
fprintf(stderr, "More than one event specified.\n");
exit(EXIT_FAILURE);
}
event = find_event_by_name(parsed_events[0].name);
if (!event) {
fprintf(stderr, "No such event found.\n");
exit(EXIT_FAILURE);
}
printf("%d\n", event->unit->default_mask);
}
static void show_default_event(void)
{
struct op_default_event_descr descr;
op_default_event(cpu_type, &descr);
if (descr.name[0] == '\0')
return;
printf("%s:%lu:%lu:1:1\n", descr.name, descr.count, descr.um);
}
static int show_vers;
static int get_cpu_type;
static int check_events;
static int unit_mask;
static int get_default_event;
static struct poptOption options[] = {
{ "cpu-type", 'c', POPT_ARG_STRING, &cpu_string, 0,
"use the given CPU type", "cpu type", },
{ "check-events", 'e', POPT_ARG_NONE, &check_events, 0,
"check the given event descriptions for validity", NULL, },
{ "unit-mask", 'u', POPT_ARG_NONE, &unit_mask, 0,
"default unit mask for the given event", NULL, },
{ "get-cpu-type", 'r', POPT_ARG_NONE, &get_cpu_type, 0,
"show the auto-detected CPU type", NULL, },
{ "get-default-event", 'd', POPT_ARG_NONE, &get_default_event, 0,
"get the default event", NULL, },
{ "callgraph", '\0', POPT_ARG_INT, &callgraph_depth, 0,
"use this callgraph depth", "callgraph depth", },
{ "version", 'v', POPT_ARG_NONE, &show_vers, 0,
"show version", NULL, },
POPT_AUTOHELP
{ NULL, 0, 0, NULL, 0, NULL, NULL, },
};
/**
* get_options - process command line
* @param argc program arg count
* @param argv program arg array
*
* Process the arguments, fatally complaining on error.
*/
static void get_options(int argc, char const * argv[])
{
optcon = op_poptGetContext(NULL, argc, argv, options, 0);
if (show_vers)
show_version(argv[0]);
/* non-option, must be a valid event name or event specs */
chosen_events = poptGetArgs(optcon);
/* don't free the context now, we need chosen_events */
}
/** make valgrind happy */
static void cleanup(void)
{
int i;
for (i = 0; i < op_get_nr_counters(cpu_type); ++i) {
if (parsed_events[i].name)
free(parsed_events[i].name);
}
op_free_events();
if (optcon)
poptFreeContext(optcon);
if (parsed_events)
free(parsed_events);
}
int main(int argc, char const * argv[])
{
struct list_head * events;
struct list_head * pos;
char const * pretty;
size_t nr_counter;
atexit(cleanup);
get_options(argc, argv);
/* usefull for testing purpose to allow to force the cpu type
* with --cpu-type */
if (cpu_string) {
cpu_type = op_get_cpu_number(cpu_string);
} else {
cpu_type = op_get_cpu_type();
}
if (cpu_type == CPU_NO_GOOD) {
fprintf(stderr, "cpu_type '%s' is not valid\n",
cpu_string ? cpu_string : "unset");
exit(EXIT_FAILURE);
}
nr_counter = op_get_nr_counters(cpu_type);
parsed_events = xcalloc(nr_counter, sizeof(struct parsed_event));
pretty = op_get_cpu_type_str(cpu_type);
if (get_cpu_type) {
printf("%s\n", pretty);
exit(EXIT_SUCCESS);
}
if (get_default_event) {
show_default_event();
exit(EXIT_SUCCESS);
}
if (cpu_type == CPU_TIMER_INT) {
if (!check_events)
printf("Using timer interrupt.\n");
exit(EXIT_SUCCESS);
}
events = op_events(cpu_type);
if (!chosen_events && (unit_mask || check_events)) {
fprintf(stderr, "No events given.\n");
exit(EXIT_FAILURE);
}
if (unit_mask) {
show_unit_mask();
exit(EXIT_SUCCESS);
}
if (check_events) {
resolve_events();
exit(EXIT_SUCCESS);
}
/* without --check-events, the only argument must be an event name */
if (chosen_events && chosen_events[0]) {
if (chosen_events[1]) {
fprintf(stderr, "Too many arguments.\n");
exit(EXIT_FAILURE);
}
list_for_each(pos, events) {
struct op_event * event = list_entry(pos, struct op_event, event_next);
if (strcmp(event->name, chosen_events[0]) == 0) {
char const * map = find_mapping_for_event(event->val, cpu_type);
if (map) {
printf("%d %s\n", event->val, map);
} else {
printf("%d\n", event->val);
}
exit(EXIT_SUCCESS);
}
}
fprintf(stderr, "No such event \"%s\"\n", chosen_events[0]);
exit(EXIT_FAILURE);
}
/* default: list all events */
printf("oprofile: available events for CPU type \"%s\"\n\n", pretty);
switch (cpu_type) {
case CPU_HAMMER:
break;
case CPU_ATHLON:
printf ("See AMD document x86 optimisation guide (22007.pdf), Appendix D\n\n");
break;
case CPU_PPRO:
case CPU_PII:
case CPU_PIII:
case CPU_P6_MOBILE:
case CPU_P4:
case CPU_P4_HT2:
case CPU_CORE:
case CPU_CORE_2:
printf("See Intel Architecture Developer's Manual Volume 3, Appendix A and\n"
"Intel Architecture Optimization Reference Manual (730795-001)\n\n");
break;
case CPU_IA64:
case CPU_IA64_1:
case CPU_IA64_2:
printf("See Intel Itanium Processor Reference Manual\n"
"for Software Development (Document 245320-003),\n"
"Intel Itanium Processor Reference Manual\n"
"for Software Optimization (Document 245473-003),\n"
"Intel Itanium 2 Processor Reference Manual\n"
"for Software Development and Optimization (Document 251110-001)\n\n");
break;
case CPU_AXP_EV4:
case CPU_AXP_EV5:
case CPU_AXP_PCA56:
case CPU_AXP_EV6:
case CPU_AXP_EV67:
printf("See Alpha Architecture Reference Manual\n"
"ftp://ftp.compaq.com/pub/products/alphaCPUdocs/alpha_arch_ref.pdf\n");
break;
case CPU_ARM_XSCALE1:
case CPU_ARM_XSCALE2:
printf("See Intel XScale Core Developer's Manual\n"
"Chapter 8 Performance Monitoring\n");
break;
break;
case CPU_PPC64_POWER4:
case CPU_PPC64_POWER5:
case CPU_PPC64_POWER6:
case CPU_PPC64_POWER5p:
case CPU_PPC64_970:
case CPU_PPC64_970MP:
printf("Obtain PowerPC64 processor documentation at:\n"
"http://www-306.ibm.com/chips/techlib/techlib.nsf/productfamilies/PowerPC\n");
break;
case CPU_PPC64_CELL:
printf("Obtain Cell Broadband Engine documentation at:\n"
"http://www-306.ibm.com/chips/techlib/techlib.nsf/products/Cell_Broadband_Engine\n");
break;
case CPU_MIPS_20K:
printf("See Programming the MIPS64 20Kc Processor Core User's "
"manual available from www.mips.com\n");
break;
case CPU_MIPS_24K:
printf("See Programming the MIPS32 24K Core "
"available from www.mips.com\n");
break;
case CPU_MIPS_25K:
printf("See Programming the MIPS64 25Kf Processor Core User's "
"manual available from www.mips.com\n");
break;
case CPU_MIPS_34K:
printf("See Programming the MIPS32 34K Core Family "
"available from www.mips.com\n");
break;
case CPU_MIPS_5K:
printf("See Programming the MIPS64 5K Processor Core Family "
"Software User's manual available from www.mips.com\n");
break;
case CPU_MIPS_R10000:
case CPU_MIPS_R12000:
printf("See NEC R10000 / R12000 User's Manual\n"
"http://www.necelam.com/docs/files/U10278EJ3V0UM00.pdf\n");
break;
case CPU_MIPS_RM7000:
printf("See RM7000 Family User Manual "
"available from www.pmc-sierra.com\n");
break;
case CPU_MIPS_RM9000:
printf("See RM9000x2 Family User Manual "
"available from www.pmc-sierra.com\n");
break;
case CPU_MIPS_SB1:
case CPU_MIPS_VR5432:
printf("See NEC VR5443 User's Manual, Volume 1\n"
"http://www.necelam.com/docs/files/1375_V1.pdf\n");
break;
case CPU_MIPS_VR5500:
printf("See NEC R10000 / R12000 User's Manual\n"
"http://www.necel.com/nesdis/image/U16677EJ3V0UM00.pdf\n");
break;
case CPU_PPC_E500:
case CPU_PPC_E500_2:
printf("See PowerPC e500 Core Complex Reference Manual\n"
"Chapter 7: Performance Monitor\n"
"Downloadable from http://www.freescale.com\n");
break;
case CPU_PPC_7450:
printf("See MPC7450 RISC Microprocessor Family Reference "
"Manual\n"
"Chapter 11: Performance Monitor\n"
"Downloadable from http://www.freescale.com\n");
break;
case CPU_RTC:
break;
// don't use default, if someone add a cpu he wants a compiler warning
// if he forgets to handle it here.
case CPU_TIMER_INT:
case CPU_NO_GOOD:
case MAX_CPU_TYPE:
printf("%d is not a valid processor type.\n", cpu_type);
break;
}
list_for_each(pos, events) {
struct op_event * event = list_entry(pos, struct op_event, event_next);
help_for_event(event);
}
return EXIT_SUCCESS;
}