Skip to content

Commit

Permalink
Allow switching to/from traced and traced_relay
Browse files Browse the repository at this point in the history
Allow switching to/from traced and traced_relay via the persist.traced.enable system property. Setting the property to
0 disables everything, setting it to 1 enables traced and setting it to
2 enables traced_relay. Enabling traced_relay is mutually exclusive
with traced. Therefore when traced_relay is enabled, traced is stopped and when traced_relay is disabled, traced is started.

Also, one can pass the relay socket to traced_relay via the
traced_relay.relay_port system property. This allows us to
dynamically pass the relay socket in a multi-machine virtualized
environment.

To switch to traced_relay, run:
    adb root
    adb shell setprop traced_relay.relay_port <vsock-address>
    adb shell setprop persist.traced.enable 2

To switch from traced_relay to traced, run:
    adb shell setprop persist.traced.enable 1

Test: build in Android tree, switch back and forth to traced_relay
Bug: 393210308
Change-Id: I5e7a38c94d8c49974a74518074367e1d7ad3002a
  • Loading branch information
Jahdiel Alvarez committed Feb 4, 2025
1 parent 22c1eb4 commit 71d2c08
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 12 deletions.
4 changes: 2 additions & 2 deletions include/perfetto/tracing/default_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ PERFETTO_EXPORT_COMPONENT std::vector<std::string> TokenizeProducerSockets(
const char* producer_socket_names);
PERFETTO_EXPORT_COMPONENT const char* GetProducerSocket();

// Optionally returns the relay socket name (nullable). The relay socket is used
// Optionally returns the relay socket name. The relay socket is used
// for forwarding the IPC messages between the local producers and the remote
// tracing service.
PERFETTO_EXPORT_COMPONENT const char* GetRelaySocket();
PERFETTO_EXPORT_COMPONENT std::string GetRelaySocket();

} // namespace perfetto

Expand Down
30 changes: 28 additions & 2 deletions perfetto.rc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ service traced /system/bin/traced
group nobody
task_profiles ProcessCapacityHigh

# traced_relay relays its local producer's data to a central traced service
# running in a different VM or in the host. traced and traced_relay are mutually
# exclusive, therefore make sure they aren't running at the same time.
#
# Note:
# * The relay socket name is specified by the |traced_relay.relay_port|
# system property.
service traced_relay /system/bin/traced_relay
class late_start
disabled
socket traced_producer stream 0666 root root
user nobody
group nobody
task_profiles ProcessCapacityHigh

service traced_probes /system/bin/traced_probes
class late_start
disabled
Expand Down Expand Up @@ -48,6 +63,15 @@ on property:debug.atrace.user_initiated=1
on property:persist.traced.enable=1 && property:debug.atrace.user_initiated=""
start traced_probes

# Possible values of persist.traced.enable:
# - 0=disabled
# - 1=enabled
# - 2=relay_mode
on property:persist.traced.enable=0
stop traced
stop traced_relay
stop traced_probes

on property:persist.traced.enable=1
# Trace files need to be:
# - Written by either uid:shell or uid:statsd.
Expand All @@ -66,12 +90,14 @@ on property:persist.traced.enable=1
# too many other domains can write into that. See b/170404111.
mkdir /data/misc/perfetto-configs 0775 root shell

stop traced_relay
start traced
start traced_probes

on property:persist.traced.enable=0
on property:persist.traced.enable=2
stop traced
stop traced_probes
start traced_relay
start traced_probes

# Reset the Perfetto guard rail state on boot:
on post-fs-data
Expand Down
4 changes: 2 additions & 2 deletions src/traced_relay/relay_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ RelayService::RelayService(base::TaskRunner* task_runner)
: task_runner_(task_runner), machine_id_hint_(GetMachineIdHint()) {}

void RelayService::Start(const char* listening_socket_name,
const char* client_socket_name) {
std::string client_socket_name) {
auto sock_family = base::GetSockFamily(listening_socket_name);
listening_socket_ =
base::UnixSocket::Listen(listening_socket_name, this, task_runner_,
Expand All @@ -215,7 +215,7 @@ void RelayService::Start(const char* listening_socket_name,
}

void RelayService::Start(base::ScopedSocketHandle server_socket_handle,
const char* client_socket_name) {
std::string client_socket_name) {
// Called when the service is started by Android init, where
// |server_socket_handle| is a unix socket.
listening_socket_ = base::UnixSocket::Listen(
Expand Down
4 changes: 2 additions & 2 deletions src/traced_relay/relay_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ class RelayService : public base::UnixSocket::EventListener {

// Starts the service relay that forwards messages between the
// |server_socket_name| and |client_socket_name| ports.
void Start(const char* server_socket_name, const char* client_socket_name);
void Start(const char* server_socket_name, std::string client_socket_name);

// Starts the service relay that forwards messages between the
// |server_socket_handle| and |client_socket_name| ports. Called when the
// service is started by Android init.
void Start(base::ScopedSocketHandle server_socket_handle,
const char* client_socket_name);
std::string client_socket_name);

static std::string GetMachineIdHint(
bool use_pseudo_boot_id_for_testing = false);
Expand Down
4 changes: 2 additions & 2 deletions src/traced_relay/relay_service_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static int RelayServiceMain(int argc, char** argv) {
}
}

if (!GetRelaySocket()) {
if (GetRelaySocket().empty()) {
PrintUsage(argv[0]);
return 1;
}
Expand Down Expand Up @@ -158,7 +158,7 @@ static int RelayServiceMain(int argc, char** argv) {
watchdog->Start();

PERFETTO_ILOG("Started traced_relay, listening on %s, forwarding to %s",
GetProducerSocket(), GetRelaySocket());
GetProducerSocket(), GetRelaySocket().c_str());

task_runner.Run();
return 0;
Expand Down
14 changes: 12 additions & 2 deletions src/tracing/ipc/default_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "perfetto/base/build_config.h"
#include "perfetto/base/logging.h"
#include "perfetto/ext/base/android_utils.h"
#include "perfetto/ext/base/string_utils.h"
#include "perfetto/ext/base/utils.h"
#include "perfetto/ext/ipc/basic_types.h"
Expand Down Expand Up @@ -86,9 +87,18 @@ const char* GetProducerSocket() {
return name;
}

const char* GetRelaySocket() {
std::string GetRelaySocket() {
// The relay socket is optional and is connected only when the env var is set.
return getenv("PERFETTO_RELAY_SOCK_NAME");
// In Android, if the env var isn't set then we check the
// |traced_relay.relay_port| system property.
const char* name = getenv("PERFETTO_RELAY_SOCK_NAME");
if (name != nullptr)
return std::string(name);
#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
return base::GetAndroidProp("traced_relay.relay_port");
#else
return std::string();
#endif
}

std::vector<std::string> TokenizeProducerSockets(
Expand Down

0 comments on commit 71d2c08

Please sign in to comment.