Skip to content

Commit b1cf6f6

Browse files
ahunter6acmel
authored andcommitted
perf tools: Add a function to calculate sample event size
Add perf_event__sample_event_size() which can be used when synthesizing sample events to determine how big the resulting event will be, and therefore how much memory to allocate. Signed-off-by: Adrian Hunter <[email protected]> Cc: David Ahern <[email protected]> Cc: Frederic Weisbecker <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Mike Galbraith <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent d03f217 commit b1cf6f6

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

tools/perf/util/event.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ int perf_event__preprocess_sample(const union perf_event *self,
229229

230230
const char *perf_event__name(unsigned int id);
231231

232+
size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
233+
u64 sample_regs_user, u64 read_format);
232234
int perf_event__synthesize_sample(union perf_event *event, u64 type,
233235
u64 sample_regs_user, u64 read_format,
234236
const struct perf_sample *sample,

tools/perf/util/evsel.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,98 @@ int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
14651465
return 0;
14661466
}
14671467

1468+
size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1469+
u64 sample_regs_user, u64 read_format)
1470+
{
1471+
size_t sz, result = sizeof(struct sample_event);
1472+
1473+
if (type & PERF_SAMPLE_IDENTIFIER)
1474+
result += sizeof(u64);
1475+
1476+
if (type & PERF_SAMPLE_IP)
1477+
result += sizeof(u64);
1478+
1479+
if (type & PERF_SAMPLE_TID)
1480+
result += sizeof(u64);
1481+
1482+
if (type & PERF_SAMPLE_TIME)
1483+
result += sizeof(u64);
1484+
1485+
if (type & PERF_SAMPLE_ADDR)
1486+
result += sizeof(u64);
1487+
1488+
if (type & PERF_SAMPLE_ID)
1489+
result += sizeof(u64);
1490+
1491+
if (type & PERF_SAMPLE_STREAM_ID)
1492+
result += sizeof(u64);
1493+
1494+
if (type & PERF_SAMPLE_CPU)
1495+
result += sizeof(u64);
1496+
1497+
if (type & PERF_SAMPLE_PERIOD)
1498+
result += sizeof(u64);
1499+
1500+
if (type & PERF_SAMPLE_READ) {
1501+
result += sizeof(u64);
1502+
if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1503+
result += sizeof(u64);
1504+
if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1505+
result += sizeof(u64);
1506+
/* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1507+
if (read_format & PERF_FORMAT_GROUP) {
1508+
sz = sample->read.group.nr *
1509+
sizeof(struct sample_read_value);
1510+
result += sz;
1511+
} else {
1512+
result += sizeof(u64);
1513+
}
1514+
}
1515+
1516+
if (type & PERF_SAMPLE_CALLCHAIN) {
1517+
sz = (sample->callchain->nr + 1) * sizeof(u64);
1518+
result += sz;
1519+
}
1520+
1521+
if (type & PERF_SAMPLE_RAW) {
1522+
result += sizeof(u32);
1523+
result += sample->raw_size;
1524+
}
1525+
1526+
if (type & PERF_SAMPLE_BRANCH_STACK) {
1527+
sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1528+
sz += sizeof(u64);
1529+
result += sz;
1530+
}
1531+
1532+
if (type & PERF_SAMPLE_REGS_USER) {
1533+
if (sample->user_regs.abi) {
1534+
result += sizeof(u64);
1535+
sz = hweight_long(sample_regs_user) * sizeof(u64);
1536+
result += sz;
1537+
} else {
1538+
result += sizeof(u64);
1539+
}
1540+
}
1541+
1542+
if (type & PERF_SAMPLE_STACK_USER) {
1543+
sz = sample->user_stack.size;
1544+
result += sizeof(u64);
1545+
if (sz) {
1546+
result += sz;
1547+
result += sizeof(u64);
1548+
}
1549+
}
1550+
1551+
if (type & PERF_SAMPLE_WEIGHT)
1552+
result += sizeof(u64);
1553+
1554+
if (type & PERF_SAMPLE_DATA_SRC)
1555+
result += sizeof(u64);
1556+
1557+
return result;
1558+
}
1559+
14681560
int perf_event__synthesize_sample(union perf_event *event, u64 type,
14691561
u64 sample_regs_user, u64 read_format,
14701562
const struct perf_sample *sample,

0 commit comments

Comments
 (0)