perf record: Ensure space for lost samples
Ian Rogers
reviewerPATCH 1/1 perf record: Ensure space for lost samples
Previous allocation didn't account for sample ID written after the lost samples event. Switch from malloc/free to a stack allocation.
Reported-by Milian Wolff <[email protected]>
Signed-off-by Ian Rogers <[email protected]>
--- tools/lib/perf/include/perf/event.h | 3 +++ tools/perf/builtin-record.c | 37 ++++++++++------------------- 2 files changed, 15 insertions(+), 25 deletions(-)
tools/lib/perf/include/perf/event.h +3 -0
@@ -77,6 +77,9 @@ struct perf_record_lost_samples {__u64 lost; }; +#define MAX_ID_HDR_ENTRIES 6 +#define PERF_RECORD_MAX_LOST_SAMPLE_AND_ID_SIZE \ + (sizeof(struct perf_record_lost_samples) + MAX_ID_HDR_ENTRIES * sizeof(__u64))/* * PERF_FORMAT_ENABLED | PERF_FORMAT_RUNNING | PERF_FORMAT_ID | PERF_FORMAT_LOST */
tools/perf/builtin-record.c +12 -25
@@ -1926,7 +1926,10 @@ static void __record__save_lost_samples(struct record *rec, struct evsel *evsel,static void record__read_lost_samples (struct record *rec) {struct perf_session *session = rec->session; -struct perf_record_lost_samples *lost =NULL ; +union { +struct perf_record_lost_samples lost; +char lost_and_sample_id[PERF_RECORD_MAX_LOST_SAMPLE_AND_ID_SIZE ]; + } lost;struct evsel *evsel;/* there was an error during record__open */
@@ -1951,20 +1954,13 @@ static void record__read_lost_samples(struct record *rec)if (perf_evsel__read (&evsel->core, x, y, &count) <0 ) {pr_debug ("read LOST count failed\n" ); - goto out; +return ; }if (count.lost) { -if (!lost) { - lost =zalloc (sizeof (*lost) + - session->machines.host.id_hdr_size); -if (!lost) { -pr_debug ("Memory allocation failed\n" ); -return ; - } - lost->header.type =PERF_RECORD_LOST_SAMPLES ; - } -__record__save_lost_samples (rec, evsel, lost, +memset (&lost,0 ,sizeof (lost)); + lost.lost.header.type =PERF_RECORD_LOST_SAMPLES ; +__record__save_lost_samples (rec, evsel, &lost.lost, x, y, count.lost,0 ); } }
@@ -1972,21 +1968,12 @@ static void record__read_lost_samples(struct record *rec) lost_count =perf_bpf_filter__lost_count (evsel);if (lost_count) { -if (!lost) { - lost =zalloc (sizeof (*lost) + - session->machines.host.id_hdr_size); -if (!lost) { -pr_debug ("Memory allocation failed\n" ); -return ; - } - lost->header.type =PERF_RECORD_LOST_SAMPLES ; - } -__record__save_lost_samples (rec, evsel, lost,0 ,0 , lost_count, +memset (&lost,0 ,sizeof (lost)); + lost.lost.header.type =PERF_RECORD_LOST_SAMPLES ; +__record__save_lost_samples (rec, evsel, &lost.lost,0 ,0 , lost_count,PERF_RECORD_MISC_LOST_SAMPLES_BPF ); } } -out: -free (lost); }static volatile sig_atomic_t workload_exec_errno;
@@ -3198,7 +3185,7 @@ static int switch_output_setup(struct record *rec)unsigned long val;/* -* If we're using --switch-output-events, then we imply its +* If we're using --switch-output-events, then we imply its * --switch-output=signal, as we'll send a SIGUSR2 from the side band * thread to its parent. */
Signature · 1 line
2.45.2.505.gda0bf45e8d-goog
Namhyung Kim
maintainerQuoted 30 lines · Ian Rogers
Previous allocation didn't account for sample ID written after the lost samples event. Switch from malloc/free to a stack allocation.
Reported-by Milian Wolff <[email protected]>Signed-off-by Ian Rogers <[email protected]>--- tools/lib/perf/include/perf/event.h | 3 +++ tools/perf/builtin-record.c | 37 ++++++++++------------------- 2 files changed, 15 insertions(+), 25 deletions(-)tools/lib/perf/include/perf/event.h +3
-0@@ -77,6 +77,9 @@ struct perf_record_lost_samples {__u64 lost; }; +#define MAX_ID_HDR_ENTRIES 6 +#define PERF_RECORD_MAX_LOST_SAMPLE_AND_ID_SIZE \ + (sizeof(struct perf_record_lost_samples) + MAX_ID_HDR_ENTRIES * sizeof(__u64))/* * PERF_FORMAT_ENABLED | PERF_FORMAT_RUNNING | PERF_FORMAT_ID | PERF_FORMAT_LOST */ tools/perf/builtin-record.c +0
-0
tools/perf/builtin-record.c static void __record__save_lost_samples(struct record *rec, struct evsel *evsel, · line 1932
Quoted 8 lines
tools/perf/builtin-record.c +4
-1@@ -1926,7 +1926,10 @@ static void __record__save_lost_samples(struct record *rec, struct evsel *evsel,static void record__read_lost_samples (struct record *rec) {struct perf_session *session = rec->session; -struct perf_record_lost_samples *lost =NULL ; +union { +struct perf_record_lost_samples lost; +char lost_and_sample_id[PERF_RECORD_MAX_LOST_SAMPLE_AND_ID_SIZE ]; + } lost;
Can we make it struct and keep the sample_id part only?
Other than that, looks good to me.
Thanks,
Namhyung
Quoted 65 lines
tools/perf/builtin-record.c +8
-24struct evsel *evsel;/* there was an error during record__open */ @@ -1951,20 +1954,13 @@ static void record__read_lost_samples(struct record *rec)if (perf_evsel__read (&evsel->core, x, y, &count) <0 ) {pr_debug ("read LOST count failed\n" ); - goto out; +return ; }if (count.lost) { -if (!lost) { - lost =zalloc (sizeof (*lost) + - session->machines.host.id_hdr_size); -if (!lost) { -pr_debug ("Memory allocation failed\n" ); -return ; - } - lost->header.type =PERF_RECORD_LOST_SAMPLES ; - } -__record__save_lost_samples (rec, evsel, lost, +memset (&lost,0 ,sizeof (lost)); + lost.lost.header.type =PERF_RECORD_LOST_SAMPLES ; +__record__save_lost_samples (rec, evsel, &lost.lost, x, y, count.lost,0 ); } }@@ -1972,21 +1968,12 @@ static void record__read_lost_samples(struct record *rec) lost_count =perf_bpf_filter__lost_count (evsel);if (lost_count) { -if (!lost) { - lost =zalloc (sizeof (*lost) + - session->machines.host.id_hdr_size); -if (!lost) { -pr_debug ("Memory allocation failed\n" ); -return ; - } - lost->header.type =PERF_RECORD_LOST_SAMPLES ; - } -__record__save_lost_samples (rec, evsel, lost,0 ,0 , lost_count, +memset (&lost,0 ,sizeof (lost)); + lost.lost.header.type =PERF_RECORD_LOST_SAMPLES ; +__record__save_lost_samples (rec, evsel, &lost.lost,0 ,0 , lost_count,PERF_RECORD_MISC_LOST_SAMPLES_BPF ); } } -out: -free (lost); }static volatile sig_atomic_t workload_exec_errno;@@ -3198,7 +3185,7 @@ static int switch_output_setup(struct record *rec)unsigned long val;/* -* If we're using --switch-output-events, then we imply its +* If we're using --switch-output-events, then we imply its * --switch-output=signal, as we'll send a SIGUSR2 from the side band * thread to its parent. */ Signature · 1 line
2.45.2.505.gda0bf45e8d-goog