Skip to content

Commit 2aabbe9

Browse files
authored
perfetto_cmd: minor code cleanups after adding --notify-fd flag (#3625)
Add missing comment and use a more appropriate name for BgProcessStatus enum now that it's used without a background process.
1 parent 094e105 commit 2aabbe9

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

src/perfetto_cmd/perfetto_cmd.cc

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ std::optional<int> PerfettoCmd::ParseCmdlineAndMaybeDaemonize(int argc,
856856
// below.
857857
}
858858

859-
void PerfettoCmd::Notify(BgProcessStatus status) {
859+
void PerfettoCmd::NotifyFd(WaitStatus status) {
860860
#if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
861861
if (!notify_fd_) {
862862
return;
@@ -869,7 +869,7 @@ void PerfettoCmd::Notify(BgProcessStatus status) {
869869
#endif //! PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
870870
}
871871

872-
void PerfettoCmd::NotifyBgProcessPipe(BgProcessStatus status) {
872+
void PerfettoCmd::NotifyBgProcessPipe(WaitStatus status) {
873873
#if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
874874
if (!background_wait_pipe_.wr) {
875875
return;
@@ -882,43 +882,44 @@ void PerfettoCmd::NotifyBgProcessPipe(BgProcessStatus status) {
882882
#endif //! PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
883883
}
884884

885-
PerfettoCmd::BgProcessStatus PerfettoCmd::WaitOnBgProcessPipe() {
885+
PerfettoCmd::WaitStatus PerfettoCmd::WaitOnBgProcessPipe() {
886886
#if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
887887
base::ScopedPlatformHandle fd = std::move(background_wait_pipe_.rd);
888888
PERFETTO_CHECK(fd);
889889

890-
BgProcessStatus msg;
890+
WaitStatus msg;
891891
static_assert(sizeof msg == 1, "Enum bigger than one byte");
892892
std::array<pollfd, 1> pollfds = {pollfd{fd.get(), POLLIN, 0}};
893893

894894
int ret = PERFETTO_EINTR(poll(&pollfds[0], pollfds.size(), 30000 /*ms*/));
895895
PERFETTO_CHECK(ret >= 0);
896896
if (ret == 0) {
897897
fprintf(stderr, "Timeout waiting for all data sources to start\n");
898-
return kBackgroundTimeout;
898+
return kWaitTimeout;
899899
}
900900
ssize_t read_ret = PERFETTO_EINTR(read(fd.get(), &msg, 1));
901901
PERFETTO_CHECK(read_ret >= 0);
902902
if (read_ret == 0) {
903903
fprintf(stderr, "Background process didn't report anything\n");
904-
return kBackgroundOtherError;
904+
return kWaitOtherError;
905905
}
906906

907-
if (msg != kBackgroundOk) {
908-
fprintf(stderr, "Background process failed, BgProcessStatus=%d\n",
907+
if (msg != kWaitOk) {
908+
fprintf(stderr, "Background process failed, WaitStatus=%d\n",
909909
static_cast<int>(msg));
910910
return msg;
911911
}
912912
#endif //! PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
913913

914-
return kBackgroundOk;
914+
return kWaitOk;
915915
}
916916

917917
int PerfettoCmd::ConnectToServiceRunAndMaybeNotify() {
918918
int exit_code = ConnectToServiceAndRun();
919919

920-
Notify(exit_code == 0 ? kBackgroundOk : kBackgroundOtherError);
921-
NotifyBgProcessPipe(exit_code == 0 ? kBackgroundOk : kBackgroundOtherError);
920+
WaitStatus wait_status = exit_code == 0 ? kWaitOk : kWaitOtherError;
921+
NotifyFd(wait_status);
922+
NotifyBgProcessPipe(wait_status);
922923

923924
return exit_code;
924925
}
@@ -1520,8 +1521,8 @@ ID UID STATE BUF (#) KB DUR (s) #DS STARTED NAME
15201521
void PerfettoCmd::OnObservableEvents(
15211522
const ObservableEvents& observable_events) {
15221523
if (observable_events.all_data_sources_started()) {
1523-
Notify(kBackgroundOk);
1524-
NotifyBgProcessPipe(kBackgroundOk);
1524+
NotifyFd(kWaitOk);
1525+
NotifyBgProcessPipe(kWaitOk);
15251526
}
15261527
if (observable_events.has_clone_trigger_hit()) {
15271528
int64_t tsid = observable_events.clone_trigger_hit().tracing_session_id();

src/perfetto_cmd/perfetto_cmd.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,28 @@ class PerfettoCmd : public Consumer {
109109

110110
void ReadbackTraceDataAndQuit(const std::string& error);
111111

112-
enum BgProcessStatus : char {
113-
kBackgroundOk = 0,
114-
kBackgroundOtherError = 1,
115-
kBackgroundTimeout = 2,
112+
enum WaitStatus : char {
113+
kWaitOk = 0,
114+
kWaitOtherError = 1,
115+
kWaitTimeout = 2,
116116
};
117117

118-
void Notify(BgProcessStatus status);
118+
// Used to implement the --notify-fd flag.
119+
//
120+
// Signals client by writing to FD (if there is one) that data sources has
121+
// started (or failed to start).
122+
//
123+
// Only the first time this function is called is significant. Further calls
124+
// will have no effect.
125+
void NotifyFd(WaitStatus status);
119126

120127
// Used to implement the --background-wait flag.
121128
//
122129
// Waits (up to 30s) for the child process to signal (success or an error).
123130
//
124131
// Returns the status received from the child process or kTimeout, in case of
125132
// timeout.
126-
BgProcessStatus WaitOnBgProcessPipe();
133+
WaitStatus WaitOnBgProcessPipe();
127134

128135
// Used to implement the --background-wait flag.
129136
//
@@ -132,7 +139,7 @@ class PerfettoCmd : public Consumer {
132139
//
133140
// Only the first time this function is called is significant. Further calls
134141
// will have no effect.
135-
void NotifyBgProcessPipe(BgProcessStatus status);
142+
void NotifyBgProcessPipe(WaitStatus status);
136143

137144
void OnCloneSnapshotTriggerReceived(TracingSessionID,
138145
const SnapshotTriggerInfo& trigger);

0 commit comments

Comments
 (0)