Skip to content

Commit dbc37d4

Browse files
author
Pete Stevenson
authored
Write pprof from stirling profiler. (#1303)
Summary: We update the `stirling_profiler` binary to directly write a ppprof protobuf file. Type of change: /kind feature Test Plan: Ran the binary locally. Opened the resulting pprof pb file using `go tool pprof`. Observed expected flamegraph. --------- Signed-off-by: Pete Stevenson <jps@pixielabs.ai>
1 parent 5d2b24e commit dbc37d4

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

src/stirling/binaries/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pl_cc_binary(
9696
name = "stirling_profiler",
9797
srcs = ["stirling_profiler.cc"],
9898
deps = [
99+
"//src/shared/pprof:cc_library",
99100
"//src/stirling/source_connectors/perf_profiler:cc_library",
100101
],
101102
)

src/stirling/binaries/stirling_profiler.cc

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,48 @@
1616
* SPDX-License-Identifier: Apache-2.0
1717
*/
1818

19+
#include <sys/sysinfo.h>
20+
1921
#include <csignal>
2022
#include <iostream>
2123
#include <thread>
2224

2325
#include "src/common/base/base.h"
26+
#include "src/shared/pprof/pprof.h"
2427
#include "src/shared/upid/upid.h"
2528
#include "src/stirling/core/unit_connector.h"
2629
#include "src/stirling/source_connectors/perf_profiler/perf_profile_connector.h"
27-
#include "src/stirling/source_connectors/perf_profiler/stack_traces_table.h"
2830

2931
using ::px::Status;
3032

3133
DEFINE_uint32(time, 30, "Number of seconds to run the profiler.");
34+
DEFINE_string(pprof_pb_file, "profile.pb", "File path for pprof protobuf output.");
35+
DECLARE_uint32(stirling_profiler_stack_trace_sample_period_ms);
3236

3337
namespace px {
3438
namespace stirling {
3539

3640
class Profiler : public UnitConnector<PerfProfileConnector> {
3741
public:
38-
Status PrintData() {
39-
// Build the stack traces histogram.
42+
Status WritePProf() {
43+
// Build stack traces histogram.
4044
PX_RETURN_IF_ERROR(BuildHistogram());
4145

42-
// Print the stack traces histogram.
43-
// TODO(jps): replace this with a pprof proto file writer.
44-
// 15x: libc.so;main;foo;bar
45-
// 12x: libc.so;main;foo;qux
46-
for (const auto& [str, count] : histo_) {
47-
LOG(INFO) << count << "x: " << str;
46+
// Create the pprof profile.
47+
const uint32_t num_cpus = get_nprocs_conf();
48+
const uint32_t period_ms = FLAGS_stirling_profiler_stack_trace_sample_period_ms;
49+
const auto pprof_pb = px::shared::CreatePProfProfile(num_cpus, period_ms, histo_);
50+
51+
// Write the pprof profile to disk.
52+
std::fstream outfile(FLAGS_pprof_pb_file, std::ios::out | std::ios::trunc | std::ios::binary);
53+
if (!outfile.is_open()) {
54+
char const* const err_msg = "Failed to open output file: $0.";
55+
return error::Internal(absl::Substitute(err_msg, FLAGS_pprof_pb_file));
56+
}
57+
58+
if (!pprof_pb.SerializeToOstream(&outfile)) {
59+
char const* const err_msg = "Failed to write pprof protobuf to file: $0.";
60+
return error::Internal(absl::Substitute(err_msg, FLAGS_pprof_pb_file));
4861
}
4962
return Status::OK();
5063
}
@@ -101,8 +114,8 @@ Status RunProfiler() {
101114
// Stop collecting data and do a final read out of eBPF perf buffer & maps.
102115
PX_RETURN_IF_ERROR(g_profiler->Stop());
103116

104-
// Print the info. We will replace this with a pprof proto file write out.
105-
PX_RETURN_IF_ERROR(g_profiler->PrintData());
117+
// Write a pprof proto file.
118+
PX_RETURN_IF_ERROR(g_profiler->WritePProf());
106119

107120
// Phew. We are outta here.
108121
return Status::OK();

0 commit comments

Comments
 (0)