Skip to content

Commit

Permalink
Add a perfetto backend for tracing and enable it for chip-tool & ligh…
Browse files Browse the repository at this point in the history
…t-app on linux (#27354)

* Create varargs macros for tracing, including tests

* Add matter trace scope macros

* Add missing semicolon

* Add labels to trace end

* Restyled by whitespace

* Restyled by clang-format

* Restyled by gn

* Add perfetto v35.0 as a submodule for linux and android

* Start adding a perfetto backend

* Enable perfetto and switch chip-tool C++ standard to 17 as we never compile it for embedded using other compilers

* make library compile and link

* GN format

* Add a track event register, since this is what seems to be needed according to SDK guides

* Add open/close support

* Use a buildconfig header for tracing configuration

* Fix missing include

* Restyled by gn

* Add a file path to perfetto command line argument tracing

* Add a file path to perfetto command line argument tracing

* Fix start/stop to setup tracing

* Some minor reshuffling ... tracing not yet outputted

* Tracing to file works now

* Better tracing for dnssd stuff

* better tracing

* Better tracing logic - only enable perfetto tracing for chiptool

* Minor update

* more tracing ... shows active commissioning delegate calls are SLOOOW

* Remove old MATTER_TRACE_EVENT_SCOPE

* Remove trace.h include

* Remove src/trace dependency

* Minor change to kick re-builds

* Restyled by clang-format

* Restyled by gn

* Restyled by prettier-markdown

* Use a config variable to enable choice of perfetto enabling in tracing

* Restyle

* Fix logic for tracing enabling to use current_os  instead of target os

* Disable tracing by default, since building on linux or not is not sufficient ... many platforms build on linux

* remove chip_build_pw_trace_lib flag

* Switch linux default C++ standard to 17, to generally support perfetto everywhere

* Restyle

* Disable include check based since it is ifdef-guarded

* Make perfetto and tokenization words in our wordlist

* enable tracing on all linux apps by default and make perfetto the default linux backend

* Restyle

* Make only chip-tool use tracing for now - tracing backends require explicit configuration

* Restyle

* Use public header as a source set for macros_impl.h

* Remove multiplexed trace begin/end/instant from perfetto_tracing since review comment was that this is a footgun

* Multiplexed tracing is not thread safe. Assert lock held in all tracing locations

* Revert "Multiplexed tracing is not thread safe. Assert lock held in all tracing locations"

This reverts commit fd01203.

* Add thread safety documentation

* Allow shadow errors for perfetto compilation

* Remove old tracing calls from jni code

* Make esp32 compile with pw_rpc

* Set perfetto to only apply to linux and android for now

* Set perfetto to linux only after all ... once we have android build rules we can update

* Undo cmakelist reformat

* Undo pragmas warning disabled, use just void for now to make both gcc and clang happy

* Restyle

* make only chip-tool use C++17, since it is the only tracing/perfetto-enabled build

* More update of defaults to make perfetto compile only for chip-tool for now

* more changes to make the c++ version checks more generic

* Restyle

* more documentation

* Restyle

* Make file name be quoted

* Fix typo

* Enable trace support in generic linux examples, and explicitly enable it in the lighting app

* Perfetto has its own scope method. use that instead of the custom implemented class for multiplexing

* Use a buildconfig header for tracing command line arguments

* Restyle

* Restyle

* Add nogncheck for ifdef-guarded file

* Remove include path check for ifdef-guarded include

* Split out perfetto initialization and file output logic, to allow inclusion separation

* Ensure perfetto file output is closed at exit

* Ensure TrackEvent registration is done

* Another split, to ensure event storage is registered properly

* Restyled by clang-format

* Restyled by gn

* Restyled by prettier-markdown

* Fix conditional for perfetto tracing

* Make tracing for android default-enabled

* Restyled by clang-format

* Restyled by gn

* add perfetto repo for android

* Debug odd load path failure for java codegen

* Remove bootstrap cache for testing smoketest

* Revert "Debug odd load path failure for java codegen"

This reverts commit 13ec3eb.

* Revert "Remove bootstrap cache for testing smoketest"

This reverts commit 3cfb7e4.

* Force bootstrap cache refresh, because buggy cache

* Switch to if instead of ifdef

* Restyled by gn

* Remove usage of vectors, turn more ifdefs into ifs

* Fix removal of registered backends

* Place the in list check in the register to avoid some code uglyness/duplication

* Move the inlist check back to the registry, to have even smaller code changes

* Fix logic errors regarding defined

* Fix conditional for ifndef

* Move trace setup to a class

* Correct another ifdef

* Fix a few more if/ifdef mismatches

* Undo unintentional repo change

* Restyled by clang-format

* update dependency logic to use explicit targets for each of the none/multiplexed/perfetto

* Add missing files

* Fix paths and comments

* Restyle

* Remove invalid deps on perfetto from none and multiplexed

* Bump up build time for python setup payload test because large build

---------

Co-authored-by: Restyled.io <commits@restyled.io>
Co-authored-by: Andrei Litvin <andreilitvin@google.com>
  • Loading branch information
3 people authored and pull[bot] committed May 8, 2024
1 parent f4ab989 commit 65fc18e
Show file tree
Hide file tree
Showing 79 changed files with 1,219 additions and 405 deletions.
2 changes: 2 additions & 0 deletions .github/.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,7 @@ peerNodeId
peerSessionId
pem
percentageLiftValue
perfetto
periodicQueryTimeout
pexpect
pickString
Expand Down Expand Up @@ -1360,6 +1361,7 @@ TODO
toJson
tokenized
tokenizer
tokenization
toolchain
toolchains
topologies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ jobs:
scripts/run_in_build_env.sh '(cd src/controller/python/test/unit_tests/ && python3 -m unittest -v)'
- name: Run Python Setup Payload Generator Test
timeout-minutes: 10
timeout-minutes: 20
run: |
scripts/run_in_build_env.sh 'scripts/examples/gn_build_example.sh examples/chip-tool out/'
scripts/run_in_build_env.sh 'pip3 install -r src/setup_payload/python/requirements.txt'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/examples-linux-standalone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ jobs:
"./scripts/build/build_examples.py \
--target linux-x64-address-resolve-tool-platform-mdns-ipv6only \
build"
- name: Build example Standalone Echo Client
- name: Build example Standalone chip tool
timeout-minutes: 10
run: |
./scripts/run_in_build_env.sh \
Expand Down
5 changes: 5 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,8 @@
url = https://github.com/asriot/ASR595X_Freertos.git
branch = matter
platforms = asr
[submodule "perfetto"]
path = third_party/perfetto/repo
url = https://github.com/google/perfetto.git
branch = master
platforms = linux,android
6 changes: 5 additions & 1 deletion build/config/compiler/compiler.gni
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ declare_args() {
c_standard = "gnu11"

# C++ standard level (value for -std flag).
cpp_standard = "gnu++14"
if (current_os == "android") {
cpp_standard = "gnu++17"
} else {
cpp_standard = "gnu++14"
}

# enable libfuzzer
is_libfuzzer = false
Expand Down
2 changes: 0 additions & 2 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,9 @@ if(CONFIG_ENABLE_PW_RPC)
string(APPEND chip_gn_args "import(\"//build_overrides/pigweed.gni\")\n")
chip_gn_arg_append("remove_default_configs" "[\"//third_party/connectedhomeip/third_party/pigweed/repo/pw_build:toolchain_cpp_standard\"]")
chip_gn_arg_append("chip_build_pw_rpc_lib" "true")
chip_gn_arg_append("chip_build_pw_trace_lib" "true")
chip_gn_arg_append("pw_log_BACKEND" "\"//third_party/connectedhomeip/third_party/pigweed/repo/pw_log_basic\"")
chip_gn_arg_append("pw_assert_BACKEND" "\"//third_party/connectedhomeip/third_party/pigweed/repo/pw_assert_log:check_backend\"")
chip_gn_arg_append("pw_sys_io_BACKEND" "\"//third_party/connectedhomeip/examples/platform/esp32/pw_sys_io:pw_sys_io_esp32\"")
chip_gn_arg_append("pw_trace_BACKEND" "\"//third_party/connectedhomeip/third_party/pigweed/repo/pw_trace_tokenized\"")
chip_gn_arg_append("dir_pw_third_party_nanopb" "\"//third_party/connectedhomeip/third_party/nanopb/repo\"")
chip_gn_arg_append("pw_build_LINK_DEPS" "[\"\$dir_pw_assert:impl\", \"\$dir_pw_log:impl\"]")
chip_gn_arg_append("pw_rpc_CONFIG" "\"//third_party/connectedhomeip/third_party/pigweed/repo/pw_rpc:disable_global_mutex\"")
Expand Down
1 change: 0 additions & 1 deletion examples/chef/linux/with_pw_rpc.gni
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ pw_build_LINK_DEPS = [
]

chip_enable_pw_rpc = true
chip_build_pw_trace_lib = true
chip_use_pw_logging = true
1 change: 0 additions & 1 deletion examples/chef/silabs/with_pw_rpc.gni
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ silabs_sdk_target = get_label_info(":sdk", "label_no_toolchain")

chip_enable_pw_rpc = true
chip_enable_openthread = true
chip_build_pw_trace_lib = true

cpp_standard = "gnu++17"

Expand Down
3 changes: 3 additions & 0 deletions examples/chip-tool/args.gni
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ chip_project_config_include_dirs = [ "${chip_root}/examples/chip-tool/include" ]
chip_project_config_include_dirs += [ "${chip_root}/config/standalone" ]

matter_enable_tracing_support = true

# Perfetto requires C++17
cpp_standard = "gnu++17"
5 changes: 2 additions & 3 deletions examples/chip-tool/commands/common/CHIPCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "CHIPCommand.h"

#include <TracingCommandLineArgument.h>
#include <controller/CHIPDeviceControllerFactory.h>
#include <core/CHIPBuildConfig.h>
#include <credentials/attestation_verifier/FileAttestationTrustStore.h>
Expand Down Expand Up @@ -247,7 +246,7 @@ void CHIPCommand::StartTracing()
{
for (const auto & destination : mTraceTo.Value())
{
chip::CommandLineApp::EnableTracingFor(destination.c_str());
mTracingSetup.EnableTracingFor(destination.c_str());
}
}

Expand Down Expand Up @@ -277,7 +276,7 @@ void CHIPCommand::StartTracing()

void CHIPCommand::StopTracing()
{
chip::CommandLineApp::StopTracing();
mTracingSetup.StopTracing();

#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED
chip::trace::DeInitTrace();
Expand Down
1 change: 1 addition & 0 deletions examples/chip-tool/commands/common/CHIPCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,6 @@ class CHIPCommand : public Command
chip::Optional<bool> mTraceDecode;
#endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED

chip::CommandLineApp::TracingSetup mTracingSetup;
chip::Optional<std::vector<std::string>> mTraceTo;
};
32 changes: 32 additions & 0 deletions examples/common/tracing/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,30 @@

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")
import("${build_root}/config/compiler/compiler.gni")
import("${chip_root}/build/chip/buildconfig_header.gni")
import("${chip_root}/src/lib/lib.gni")

declare_args() {
# TODO: cpp_standard check is not ideal, it should be >= 17,
# however for now this is what we use in compilations
matter_commandline_enable_perfetto_tracing =
current_os == "linux" && cpp_standard == "gnu++17"
}

config("default_config") {
include_dirs = [ "." ]
}

buildconfig_header("tracing_features") {
header = "enabled_features.h"
header_dir = "tracing"

defines = [
"ENABLE_PERFETTO_TRACING=${matter_commandline_enable_perfetto_tracing}",
]
}

source_set("commandline") {
sources = [
"TracingCommandLineArgument.cpp",
Expand All @@ -32,8 +50,22 @@ source_set("commandline") {
"${chip_root}/src/tracing/log_json",
]

public_deps = [ ":tracing_features" ]

public_configs = [ ":default_config" ]

if (matter_commandline_enable_perfetto_tracing) {
public_deps += [
"${chip_root}/src/tracing/perfetto",
"${chip_root}/src/tracing/perfetto:file_output",
]

deps += [
"${chip_root}/src/tracing/perfetto:event_storage",
"${chip_root}/src/tracing/perfetto:simple_initialization",
]
}

cflags = [ "-Wconversion" ]
}

Expand Down
62 changes: 49 additions & 13 deletions examples/common/tracing/TracingCommandLineArgument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,37 @@
#include <tracing/log_json/log_json_tracing.h>
#include <tracing/registry.h>

#if ENABLE_PERFETTO_TRACING
#include <tracing/perfetto/event_storage.h> // nogncheck
#include <tracing/perfetto/simple_initialize.h> // nogncheck
#endif

#include <memory>
#include <string>
#include <vector>

namespace chip {
namespace CommandLineApp {

namespace {
using ::chip::Tracing::ScopedRegistration;
using ::chip::Tracing::LogJson::LogJsonBackend;
#if ENABLE_PERFETTO_TRACING

// currently supported backends
LogJsonBackend log_json_backend;
bool StartsWith(CharSpan argument, const char * prefix)
{
const size_t prefix_len = strlen(prefix);
if (argument.size() < prefix_len)
{
return false;
}

// ScopedRegistration ensures register/unregister is met, as long
// as the vector is cleared (and we do so when stopping tracing).
std::vector<std::unique_ptr<ScopedRegistration>> tracing_backends;
argument.reduce_size(prefix_len);
return argument.data_equal(CharSpan(prefix, prefix_len));
}

#endif

} // namespace

void EnableTracingFor(const char * cliArg)
void TracingSetup::EnableTracingFor(const char * cliArg)
{
chip::StringSplitter splitter(cliArg, ',');
chip::CharSpan value;
Expand All @@ -51,21 +61,47 @@ void EnableTracingFor(const char * cliArg)
{
if (value.data_equal(CharSpan::fromCharString("log")))
{
if (!log_json_backend.IsInList())
chip::Tracing::Register(mLogJsonBackend);
}
#if ENABLE_PERFETTO_TRACING
else if (value.data_equal(CharSpan::fromCharString("perfetto")))
{
chip::Tracing::Perfetto::Initialize(perfetto::kSystemBackend);
chip::Tracing::Perfetto::RegisterEventTrackingStorage();
chip::Tracing::Register(mPerfettoBackend);
}
else if (StartsWith(value, "perfetto:"))
{
std::string fileName(value.data() + 9, value.size() - 9);

chip::Tracing::Perfetto::Initialize(perfetto::kInProcessBackend);
chip::Tracing::Perfetto::RegisterEventTrackingStorage();

CHIP_ERROR err = mPerfettoFileOutput.Open(fileName.c_str());
if (err != CHIP_NO_ERROR)
{
tracing_backends.push_back(std::make_unique<ScopedRegistration>(log_json_backend));
ChipLogError(AppServer, "Failed to open perfetto trace output: %" CHIP_ERROR_FORMAT, err.Format());
}
chip::Tracing::Register(mPerfettoBackend);
}
#endif // ENABLE_PERFETTO_TRACING
else
{
ChipLogError(AppServer, "Unknown trace destination: '%s'", std::string(value.data(), value.size()).c_str());
}
}
}

void StopTracing()
void TracingSetup::StopTracing()
{
tracing_backends.clear();
#if ENABLE_PERFETTO_TRACING
chip::Tracing::Perfetto::FlushEventTrackingStorage();
mPerfettoFileOutput.Close();
chip::Tracing::Unregister(mPerfettoBackend);

#endif

chip::Tracing::Unregister(mLogJsonBackend);
}

} // namespace CommandLineApp
Expand Down
52 changes: 40 additions & 12 deletions examples/common/tracing/TracingCommandLineArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,53 @@
*/
#pragma once

#include "tracing/enabled_features.h"

#include <tracing/log_json/log_json_tracing.h>

#if ENABLE_PERFETTO_TRACING
#include <tracing/perfetto/file_output.h> // nogncheck
#include <tracing/perfetto/perfetto_tracing.h> // nogncheck
#endif

/// A string with supported command line tracing targets
/// to be pretty-printed in help strings if needed
#if ENABLE_PERFETTO_TRACING
#define SUPPORTED_COMMAND_LINE_TRACING_TARGETS "log, perfetto, perfetto:<path>"
#else
#define SUPPORTED_COMMAND_LINE_TRACING_TARGETS "log"
#endif

namespace chip {
namespace CommandLineApp {

/// Enable tracing based on the given command line argument
/// like "log" or "log,perfetto" or similar
///
/// Single arguments as well as comma separated ones are accepted.
///
/// Calling this method multiple times is ok and will enable each of
/// the given tracing modules if not already enabled.
void EnableTracingFor(const char * cliArg);

/// If EnableTracingFor is called, this MUST be called as well
/// to unregister tracing backends
void StopTracing();
class TracingSetup
{
public:
TracingSetup() = default;
~TracingSetup() { StopTracing(); }

/// Enable tracing based on the given command line argument
/// like "log" or "log,perfetto" or similar
///
/// Single arguments as well as comma separated ones are accepted.
///
/// Calling this method multiple times is ok and will enable each of
/// the given tracing modules if not already enabled.
void EnableTracingFor(const char * cliArg);

/// If EnableTracingFor is called, this MUST be called as well
/// to unregister tracing backends
void StopTracing();

private:
::chip::Tracing::LogJson::LogJsonBackend mLogJsonBackend;

#if ENABLE_PERFETTO_TRACING
chip::Tracing::Perfetto::FileTraceOutput mPerfettoFileOutput;
chip::Tracing::Perfetto::PerfettoBackend mPerfettoBackend;
#endif
};

} // namespace CommandLineApp
} // namespace chip
1 change: 0 additions & 1 deletion examples/lighting-app/bouffalolab/bl602/with_pw_rpc.gni
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import("${chip_root}/config/bouffalolab/common/lib/pw_rpc/pw_rpc.gni")
import("${chip_root}/examples/platform/bouffalolab/bl602/args.gni")

chip_enable_pw_rpc = true
chip_build_pw_trace_lib = false

cpp_standard = "gnu++17"

Expand Down
1 change: 0 additions & 1 deletion examples/lighting-app/bouffalolab/bl702/with_pw_rpc.gni
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import("${chip_root}/config/bouffalolab/common/lib/pw_rpc/pw_rpc.gni")
import("${chip_root}/examples/platform/bouffalolab/bl702/args.gni")

chip_enable_pw_rpc = true
chip_build_pw_trace_lib = false

cpp_standard = "gnu++17"

Expand Down
5 changes: 5 additions & 0 deletions examples/lighting-app/linux/args.gni
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ chip_system_project_config_include = "<SystemProjectConfig.h>"
chip_project_config_include_dirs =
[ "${chip_root}/examples/lighting-app/linux/include" ]
chip_project_config_include_dirs += [ "${chip_root}/config/standalone" ]

matter_enable_tracing_support = true

# Perfetto requires C++17
cpp_standard = "gnu++17"
1 change: 0 additions & 1 deletion examples/lighting-app/linux/with_pw_rpc.gni
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ pw_build_LINK_DEPS = [
]

chip_enable_pw_rpc = true
chip_build_pw_trace_lib = true
chip_use_pw_logging = true
1 change: 0 additions & 1 deletion examples/lighting-app/silabs/with_pw_rpc.gni
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ silabs_sdk_target = get_label_info(":sdk", "label_no_toolchain")
app_data_model = "${chip_root}/examples/lighting-app/lighting-common"
chip_enable_pw_rpc = true
chip_enable_openthread = true
chip_build_pw_trace_lib = true

cpp_standard = "gnu++17"

Expand Down
Loading

0 comments on commit 65fc18e

Please sign in to comment.