Skip to content

Commit c5b539c

Browse files
author
Pete Stevenson
authored
[record & replay] Move callback cookie into perf buffer spec. (#1656)
Summary: To support record & replay, we move the callback cookie into the perf buffer spec. Previously, we plumbed it through as a separate argument. Moving the callback cookie into the perf buffer spec. will allow us to insert a different callback and save the original callback, i.e. to interpose a recorder for recording of perf buffer events. Relevant Issues: #1163 Type of change: /kind feature Test Plan: Fully covered by existing tests. --------- Signed-off-by: Pete Stevenson <jps@pixielabs.ai>
1 parent a1633f6 commit c5b539c

File tree

8 files changed

+34
-32
lines changed

8 files changed

+34
-32
lines changed

src/stirling/bpf_tools/bcc_wrapper.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ void BCCWrapper::DetachTracepoints() {
346346
tracepoints_.clear();
347347
}
348348

349-
Status BCCWrapper::OpenPerfBuffer(const PerfBufferSpec& perf_buffer, void* cb_cookie) {
349+
Status BCCWrapper::OpenPerfBuffer(const PerfBufferSpec& perf_buffer) {
350350
const int kPageSizeBytes = system::Config::GetInstance().PageSizeBytes();
351351
int num_pages = IntRoundUpDivide(perf_buffer.size_bytes, kPageSizeBytes);
352352

@@ -358,15 +358,15 @@ Status BCCWrapper::OpenPerfBuffer(const PerfBufferSpec& perf_buffer, void* cb_co
358358
perf_buffer.ToString(), num_pages, num_pages * kPageSizeBytes);
359359
PX_RETURN_IF_ERROR(bpf_.open_perf_buffer(std::string(perf_buffer.name),
360360
perf_buffer.probe_output_fn, perf_buffer.probe_loss_fn,
361-
cb_cookie, num_pages));
361+
perf_buffer.cb_cookie, num_pages));
362362
perf_buffers_.push_back(perf_buffer);
363363
++num_open_perf_buffers_;
364364
return Status::OK();
365365
}
366366

367-
Status BCCWrapper::OpenPerfBuffers(const ArrayView<PerfBufferSpec>& perf_buffers, void* cb_cookie) {
367+
Status BCCWrapper::OpenPerfBuffers(const ArrayView<PerfBufferSpec>& perf_buffers) {
368368
for (const PerfBufferSpec& p : perf_buffers) {
369-
PX_RETURN_IF_ERROR(OpenPerfBuffer(p, cb_cookie));
369+
PX_RETURN_IF_ERROR(OpenPerfBuffer(p));
370370
}
371371
return Status::OK();
372372
}

src/stirling/bpf_tools/bcc_wrapper.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,10 @@ class BCCWrapper {
142142
/**
143143
* Open a perf buffer for reading events.
144144
* @param perf_buff Specifications of the perf buffer (name, callback function, etc.).
145-
* @param cb_cookie A pointer that is sent to the callback function when triggered by
146145
* PollPerfBuffer().
147146
* @return Error if perf buffer cannot be opened (e.g. perf buffer does not exist).
148147
*/
149-
Status OpenPerfBuffer(const PerfBufferSpec& perf_buffer, void* cb_cookie = nullptr);
148+
Status OpenPerfBuffer(const PerfBufferSpec& perf_buffer);
150149

151150
/**
152151
* Attach a perf event, which runs a probe every time a perf counter reaches a threshold
@@ -192,10 +191,9 @@ class BCCWrapper {
192191
/**
193192
* Convenience function that opens multiple perf buffers.
194193
* @param probes Vector of perf buffer descriptors.
195-
* @param cb_cookie Raw pointer returned on callback, typically used for tracking context.
196194
* @return Error of first failure (remaining perf buffer opens are not attempted).
197195
*/
198-
Status OpenPerfBuffers(const ArrayView<PerfBufferSpec>& perf_buffers, void* cb_cookie);
196+
Status OpenPerfBuffers(const ArrayView<PerfBufferSpec>& perf_buffers);
199197

200198
/**
201199
* Convenience function that opens multiple perf events.

src/stirling/bpf_tools/probe_specs/probe_specs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ struct PerfBufferSpec {
170170
// Function that will be called if there are lost/clobbered perf events.
171171
perf_reader_lost_cb probe_loss_fn;
172172

173+
// Used to invoke callback.
174+
void* cb_cookie;
175+
173176
// Size of perf buffer. Will be rounded up to and allocated in a power of 2 number of pages.
174177
int size_bytes = 1024 * 1024;
175178

src/stirling/source_connectors/dynamic_tracer/dynamic_trace_connector.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,10 @@ Status DynamicTraceConnector::InitImpl() {
184184
.name = bcc_program_.perf_buffer_specs.front().name,
185185
.probe_output_fn = &GenericHandleEvent,
186186
.probe_loss_fn = &GenericHandleEventLoss,
187+
.cb_cookie = this,
187188
};
188189

189-
PX_RETURN_IF_ERROR(OpenPerfBuffer(spec, this));
190+
PX_RETURN_IF_ERROR(OpenPerfBuffer(spec));
190191

191192
return Status::OK();
192193
}

src/stirling/source_connectors/perf_profiler/perf_profile_connector.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ Status PerfProfileConnector::InitImpl() {
138138
{"sample_call_stack", static_cast<uint64_t>(stack_trace_sampling_period_.count())});
139139

140140
const auto perf_buffer_specs = MakeArray<bpf_tools::PerfBufferSpec>(
141-
{{std::string(kHistogramAName), HandleHistoEvent, HandleHistoLoss, perf_buffer_size},
142-
{std::string(kHistogramBName), HandleHistoEvent, HandleHistoLoss, perf_buffer_size}});
141+
{{std::string(kHistogramAName), HandleHistoEvent, HandleHistoLoss, this, perf_buffer_size},
142+
{std::string(kHistogramBName), HandleHistoEvent, HandleHistoLoss, this, perf_buffer_size}});
143143

144144
PX_RETURN_IF_ERROR(InitBPFProgram(profiler_bcc_script, defines));
145145
PX_RETURN_IF_ERROR(AttachSamplingProbes(probe_specs));
146-
PX_RETURN_IF_ERROR(OpenPerfBuffers(perf_buffer_specs, this));
146+
PX_RETURN_IF_ERROR(OpenPerfBuffers(perf_buffer_specs));
147147

148148
stack_traces_a_ = WrappedBCCStackTable::Create(this, "stack_traces_a");
149149
stack_traces_b_ = WrappedBCCStackTable::Create(this, "stack_traces_b");

src/stirling/source_connectors/proc_exit/proc_exit_connector.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ Status ProcExitConnector::InitImpl() {
9494
}
9595

9696
const auto perf_buffer_specs = MakeArray<bpf_tools::PerfBufferSpec>({
97-
{"proc_exit_events", HandleProcExitEvent, HandleProcExitEventLoss, kPerfBufferPerCPUSizeBytes,
98-
bpf_tools::PerfBufferSizeCategory::kControl},
97+
{"proc_exit_events", HandleProcExitEvent, HandleProcExitEventLoss, this,
98+
kPerfBufferPerCPUSizeBytes, bpf_tools::PerfBufferSizeCategory::kControl},
9999
});
100100

101101
PX_RETURN_IF_ERROR(AttachTracepoints(kTracepointSpecs));
102-
PX_RETURN_IF_ERROR(OpenPerfBuffers(perf_buffer_specs, this));
102+
PX_RETURN_IF_ERROR(OpenPerfBuffers(perf_buffer_specs));
103103

104104
return Status::OK();
105105
}

src/stirling/source_connectors/socket_tracer/socket_trace_connector.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -394,22 +394,22 @@ auto SocketTraceConnector::InitPerfBufferSpecs() {
394394

395395
auto specs = MakeArray<bpf_tools::PerfBufferSpec>({
396396
// For data events. The order must be consistent with output tables.
397-
{"socket_data_events", HandleDataEvent, HandleDataEventLoss, kTargetDataBufferSize,
397+
{"socket_data_events", HandleDataEvent, HandleDataEventLoss, this, kTargetDataBufferSize,
398398
PerfBufferSizeCategory::kData},
399399
// For non-data events. Must not mix with the above perf buffers for data events.
400-
{"socket_control_events", HandleControlEvent, HandleControlEventLoss,
400+
{"socket_control_events", HandleControlEvent, HandleControlEventLoss, this,
401401
kTargetControlBufferSize, PerfBufferSizeCategory::kControl},
402-
{"conn_stats_events", HandleConnStatsEvent, HandleConnStatsEventLoss,
402+
{"conn_stats_events", HandleConnStatsEvent, HandleConnStatsEventLoss, this,
403403
kTargetControlBufferSize, PerfBufferSizeCategory::kControl},
404-
{"mmap_events", HandleMMapEvent, HandleMMapEventLoss, kTargetControlBufferSize / 10,
404+
{"mmap_events", HandleMMapEvent, HandleMMapEventLoss, this, kTargetControlBufferSize / 10,
405405
PerfBufferSizeCategory::kControl},
406-
{"go_grpc_events", HandleHTTP2Event, HandleHTTP2EventLoss, kTargetDataBufferSize,
406+
{"go_grpc_events", HandleHTTP2Event, HandleHTTP2EventLoss, this, kTargetDataBufferSize,
407407
PerfBufferSizeCategory::kData},
408-
{"grpc_c_events", HandleGrpcCEvent, HandleGrpcCDataLoss, kTargetDataBufferSize,
408+
{"grpc_c_events", HandleGrpcCEvent, HandleGrpcCDataLoss, this, kTargetDataBufferSize,
409409
PerfBufferSizeCategory::kData},
410-
{"grpc_c_header_events", HandleGrpcCHeaderEvent, HandleGrpcCHeaderDataLoss,
410+
{"grpc_c_header_events", HandleGrpcCHeaderEvent, HandleGrpcCHeaderDataLoss, this,
411411
kTargetDataBufferSize, PerfBufferSizeCategory::kData},
412-
{"grpc_c_close_events", HandleGrpcCCloseEvent, HandleGrpcCCloseDataLoss,
412+
{"grpc_c_close_events", HandleGrpcCCloseEvent, HandleGrpcCCloseDataLoss, this,
413413
kTargetDataBufferSize, PerfBufferSizeCategory::kData},
414414
});
415415
ResizePerfBufferSpecs(&specs, category_maximums);
@@ -438,9 +438,9 @@ Status SocketTraceConnector::InitBPF() {
438438
LOG(INFO) << absl::Substitute("Number of kprobes deployed = $0", kProbeSpecs.size());
439439
LOG(INFO) << "Probes successfully deployed.";
440440

441-
const auto kPerfBufferSpecs = InitPerfBufferSpecs();
442-
PX_RETURN_IF_ERROR(OpenPerfBuffers(kPerfBufferSpecs, this));
443-
LOG(INFO) << absl::Substitute("Number of perf buffers opened = $0", kPerfBufferSpecs.size());
441+
const auto perf_buffer_specs = InitPerfBufferSpecs();
442+
PX_RETURN_IF_ERROR(OpenPerfBuffers(perf_buffer_specs));
443+
LOG(INFO) << absl::Substitute("Number of perf buffers opened = $0", perf_buffer_specs.size());
444444

445445
// Set trace role to BPF probes.
446446
for (const auto& p : magic_enum::enum_values<traffic_protocol_t>()) {

src/stirling/source_connectors/tcp_stats/tcp_stats_connector.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ void HandleTcpEventLoss(void* /*cb_cookie*/, uint64_t /*lost*/) {
6060
// TODO(RagalahariP): Add stats counter.
6161
}
6262

63-
const auto kPerfBufferSpecs = MakeArray<bpf_tools::PerfBufferSpec>({
64-
{"tcp_events", HandleTcpEvent, HandleTcpEventLoss, kPerfBufferPerCPUSizeBytes,
65-
bpf_tools::PerfBufferSizeCategory::kData},
66-
});
67-
6863
Status TCPStatsConnector::InitImpl() {
64+
const auto perf_buffer_specs = MakeArray<bpf_tools::PerfBufferSpec>({
65+
{"tcp_events", HandleTcpEvent, HandleTcpEventLoss, this, kPerfBufferPerCPUSizeBytes,
66+
bpf_tools::PerfBufferSizeCategory::kData},
67+
});
68+
6969
sampling_freq_mgr_.set_period(kSamplingPeriod);
7070
push_freq_mgr_.set_period(kPushPeriod);
7171
PX_RETURN_IF_ERROR(InitBPFProgram(tcpstats_bcc_script));
7272
PX_RETURN_IF_ERROR(AttachKProbes(kProbeSpecs));
73-
PX_RETURN_IF_ERROR(OpenPerfBuffers(kPerfBufferSpecs, this));
73+
PX_RETURN_IF_ERROR(OpenPerfBuffers(perf_buffer_specs));
7474
LOG(INFO) << absl::Substitute("Successfully deployed $0 kprobes.", kProbeSpecs.size());
7575
return Status::OK();
7676
}

0 commit comments

Comments
 (0)