Skip to content

Commit c4937a9

Browse files
kliang2Ingo Molnar
authored andcommitted
perf tools: handle PERF_RECORD_LOST_SAMPLES
This patch modifies the perf tool to handle the new RECORD type, PERF_RECORD_LOST_SAMPLES. The number of lost-sample events is stored in .nr_events[PERF_RECORD_LOST_SAMPLES]. The exact number of samples which the kernel dropped is stored in total_lost_samples. When the percentage of dropped samples is greater than 5%, a warning is printed. Here are some examples: Eg 1, Recording different frequently-occurring events is safe with the patch. Only a very low drop rate is associated with such actions. $ perf record -e '{cycles:p,instructions:p}' -c 20003 --no-time ~/tchain ~/tchain $ perf report -D | tail SAMPLE events: 120243 MMAP2 events: 5 LOST_SAMPLES events: 24 FINISHED_ROUND events: 15 cycles:p stats: TOTAL events: 59348 SAMPLE events: 59348 instructions:p stats: TOTAL events: 60895 SAMPLE events: 60895 $ perf report --stdio --group # To display the perf.data header info, please use --header/--header-only options. # # # Total Lost Samples: 24 # # Samples: 120K of event 'anon group { cycles:p, instructions:p }' # Event count (approx.): 24048600000 # # Overhead Command Shared Object Symbol # ................ ........... ................ .................................. # 99.74% 99.86% tchain_edit tchain_edit [.] f3 0.09% 0.02% tchain_edit tchain_edit [.] f2 0.04% 0.00% tchain_edit [kernel.vmlinux] [k] ixgbe_read_reg Eg 2, Recording the same thing multiple times can lead to high drop rate, but it is not a useful configuration. $ perf record -e '{cycles:p,cycles:p}' -c 20003 --no-time ~/tchain Warning: Processed 600592 samples and lost 99.73% samples! [perf record: Woken up 148 times to write data] [perf record: Captured and wrote 36.922 MB perf.data (1206322 samples)] [perf record: Woken up 1 times to write data] [perf record: Captured and wrote 0.121 MB perf.data (1629 samples)] Signed-off-by: Kan Liang <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: Andrew Morton <[email protected]> Cc: H. Peter Anvin <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: [email protected] Cc: [email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent f38b0db commit c4937a9

File tree

7 files changed

+59
-0
lines changed

7 files changed

+59
-0
lines changed

tools/perf/builtin-report.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ static int perf_evlist__tty_browse_hists(struct perf_evlist *evlist,
320320
{
321321
struct perf_evsel *pos;
322322

323+
fprintf(stdout, "#\n# Total Lost Samples: %lu\n#\n", evlist->stats.total_lost_samples);
323324
evlist__for_each(evlist, pos) {
324325
struct hists *hists = evsel__hists(pos);
325326
const char *evname = perf_evsel__name(pos);

tools/perf/util/event.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static const char *perf_event__names[] = {
2525
[PERF_RECORD_SAMPLE] = "SAMPLE",
2626
[PERF_RECORD_AUX] = "AUX",
2727
[PERF_RECORD_ITRACE_START] = "ITRACE_START",
28+
[PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
2829
[PERF_RECORD_HEADER_ATTR] = "ATTR",
2930
[PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
3031
[PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
@@ -712,6 +713,14 @@ int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
712713
return machine__process_itrace_start_event(machine, event);
713714
}
714715

716+
int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
717+
union perf_event *event,
718+
struct perf_sample *sample,
719+
struct machine *machine)
720+
{
721+
return machine__process_lost_samples_event(machine, event, sample);
722+
}
723+
715724
size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
716725
{
717726
return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",

tools/perf/util/event.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ struct lost_event {
5252
u64 lost;
5353
};
5454

55+
struct lost_samples_event {
56+
struct perf_event_header header;
57+
u64 lost;
58+
};
59+
5560
/*
5661
* PERF_FORMAT_ENABLED | PERF_FORMAT_RUNNING | PERF_FORMAT_ID
5762
*/
@@ -235,6 +240,12 @@ enum auxtrace_error_type {
235240
* total_lost tells exactly how many events the kernel in fact lost, i.e. it is
236241
* the sum of all struct lost_event.lost fields reported.
237242
*
243+
* The kernel discards mixed up samples and sends the number in a
244+
* PERF_RECORD_LOST_SAMPLES event. The number of lost-samples events is stored
245+
* in .nr_events[PERF_RECORD_LOST_SAMPLES] while total_lost_samples tells
246+
* exactly how many samples the kernel in fact dropped, i.e. it is the sum of
247+
* all struct lost_samples_event.lost fields reported.
248+
*
238249
* The total_period is needed because by default auto-freq is used, so
239250
* multipling nr_events[PERF_EVENT_SAMPLE] by a frequency isn't possible to get
240251
* the total number of low level events, it is necessary to to sum all struct
@@ -244,6 +255,7 @@ struct events_stats {
244255
u64 total_period;
245256
u64 total_non_filtered_period;
246257
u64 total_lost;
258+
u64 total_lost_samples;
247259
u64 total_invalid_chains;
248260
u32 nr_events[PERF_RECORD_HEADER_MAX];
249261
u32 nr_non_filtered_samples;
@@ -342,6 +354,7 @@ union perf_event {
342354
struct comm_event comm;
343355
struct fork_event fork;
344356
struct lost_event lost;
357+
struct lost_samples_event lost_samples;
345358
struct read_event read;
346359
struct throttle_event throttle;
347360
struct sample_event sample;
@@ -390,6 +403,10 @@ int perf_event__process_lost(struct perf_tool *tool,
390403
union perf_event *event,
391404
struct perf_sample *sample,
392405
struct machine *machine);
406+
int perf_event__process_lost_samples(struct perf_tool *tool,
407+
union perf_event *event,
408+
struct perf_sample *sample,
409+
struct machine *machine);
393410
int perf_event__process_aux(struct perf_tool *tool,
394411
union perf_event *event,
395412
struct perf_sample *sample,

tools/perf/util/machine.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,14 @@ int machine__process_lost_event(struct machine *machine __maybe_unused,
482482
return 0;
483483
}
484484

485+
int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
486+
union perf_event *event, struct perf_sample *sample)
487+
{
488+
dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n",
489+
sample->id, event->lost_samples.lost);
490+
return 0;
491+
}
492+
485493
static struct dso*
486494
machine__module_dso(struct machine *machine, struct kmod_path *m,
487495
const char *filename)
@@ -1419,6 +1427,8 @@ int machine__process_event(struct machine *machine, union perf_event *event,
14191427
ret = machine__process_aux_event(machine, event); break;
14201428
case PERF_RECORD_ITRACE_START:
14211429
ret = machine__process_itrace_start_event(machine, event);
1430+
case PERF_RECORD_LOST_SAMPLES:
1431+
ret = machine__process_lost_samples_event(machine, event, sample); break;
14221432
break;
14231433
default:
14241434
ret = -1;

tools/perf/util/machine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ int machine__process_fork_event(struct machine *machine, union perf_event *event
8181
struct perf_sample *sample);
8282
int machine__process_lost_event(struct machine *machine, union perf_event *event,
8383
struct perf_sample *sample);
84+
int machine__process_lost_samples_event(struct machine *machine, union perf_event *event,
85+
struct perf_sample *sample);
8486
int machine__process_aux_event(struct machine *machine,
8587
union perf_event *event);
8688
int machine__process_itrace_start_event(struct machine *machine,

tools/perf/util/session.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ void perf_tool__fill_defaults(struct perf_tool *tool)
325325
tool->exit = process_event_stub;
326326
if (tool->lost == NULL)
327327
tool->lost = perf_event__process_lost;
328+
if (tool->lost_samples == NULL)
329+
tool->lost_samples = perf_event__process_lost_samples;
328330
if (tool->aux == NULL)
329331
tool->aux = perf_event__process_aux;
330332
if (tool->itrace_start == NULL)
@@ -606,6 +608,7 @@ static perf_event__swap_op perf_event__swap_ops[] = {
606608
[PERF_RECORD_SAMPLE] = perf_event__all64_swap,
607609
[PERF_RECORD_AUX] = perf_event__aux_swap,
608610
[PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap,
611+
[PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap,
609612
[PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
610613
[PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
611614
[PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
@@ -1049,6 +1052,10 @@ static int machines__deliver_event(struct machines *machines,
10491052
if (tool->lost == perf_event__process_lost)
10501053
evlist->stats.total_lost += event->lost.lost;
10511054
return tool->lost(tool, event, sample, machine);
1055+
case PERF_RECORD_LOST_SAMPLES:
1056+
if (tool->lost_samples == perf_event__process_lost_samples)
1057+
evlist->stats.total_lost_samples += event->lost_samples.lost;
1058+
return tool->lost_samples(tool, event, sample, machine);
10521059
case PERF_RECORD_READ:
10531060
return tool->read(tool, event, sample, evsel, machine);
10541061
case PERF_RECORD_THROTTLE:
@@ -1286,6 +1293,18 @@ static void perf_session__warn_about_errors(const struct perf_session *session)
12861293
stats->nr_events[PERF_RECORD_LOST]);
12871294
}
12881295

1296+
if (session->tool->lost_samples == perf_event__process_lost_samples) {
1297+
double drop_rate;
1298+
1299+
drop_rate = (double)stats->total_lost_samples /
1300+
(double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
1301+
if (drop_rate > 0.05) {
1302+
ui__warning("Processed %lu samples and lost %3.2f%% samples!\n\n",
1303+
stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
1304+
drop_rate * 100.0);
1305+
}
1306+
}
1307+
12891308
if (stats->nr_unknown_events != 0) {
12901309
ui__warning("Found %u unknown events!\n\n"
12911310
"Is this an older tool processing a perf.data "

tools/perf/util/tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct perf_tool {
4343
fork,
4444
exit,
4545
lost,
46+
lost_samples,
4647
aux,
4748
itrace_start,
4849
throttle,

0 commit comments

Comments
 (0)