Skip to content

Commit 25bc9cb

Browse files
Implement the tracing macros using a backend for esp32. (#29543)
* Implement the tracing macros using a backend for esp32. - This PR is aimed at addressing the review comments on PR #29318. - This is a draft implementation of the backend. * Move esp32 tracing configs around: better location, integrate in build examples, carry over configurations * More options removals * Remove fixme text * Restrict tracing to light app, fix include paths in main, add ignore for authkey since that should not be checked in * Move dependencies around even more - esp32 now seems to compile with tracing enabled * Ensure tracing is actually enabled when insights is on * Restyle * Made esp32_trace backend in sync with existing version * Made the common include on the tracing config for all esp32 examples * Fixed the CI failure --------- Co-authored-by: Andrei Litvin <andy314@gmail.com>
1 parent e8b7436 commit 25bc9cb

File tree

15 files changed

+159
-72
lines changed

15 files changed

+159
-72
lines changed

config/esp32/BUILD.gn

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ import("//build_overrides/build.gni")
1919
import("//build_overrides/chip.gni")
2020

2121
import("${chip_root}/build/chip/tests.gni")
22+
import("${chip_root}/src/tracing/tracing_args.gni")
2223

2324
declare_args() {
2425
chip_build_pw_rpc_lib = false
2526
}
2627

2728
group("esp32") {
28-
deps = [ "${chip_root}/src/lib" ]
29+
deps = [
30+
"${chip_root}/src/lib",
31+
matter_trace_config,
32+
]
2933

3034
if (chip_build_pw_rpc_lib) {
3135
deps += [ "//lib/pw_rpc" ]

config/esp32/components/chip/CMakeLists.txt

+11-6
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,18 @@ if (CONFIG_SEC_CERT_DAC_PROVIDER)
261261
endif()
262262

263263
if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE)
264-
chip_gn_arg_append("matter_enable_esp_insights_trace" "true")
264+
chip_gn_arg_bool("matter_enable_tracing_support" "true")
265+
chip_gn_arg_append("matter_trace_config" "\"${CHIP_ROOT}/src/tracing/esp32_trace:esp32_trace_tracing\"")
265266
endif()
266267

267268
if (CONFIG_USE_ESP32_ECDSA_PERIPHERAL)
268269
chip_gn_arg_append("chip_use_esp32_ecdsa_peripheral" "true")
269270
endif()
270271

272+
if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE)
273+
target_include_directories(${COMPONENT_LIB} INTERFACE "${CHIP_ROOT}/src/tracing/esp32_trace/include")
274+
endif()
275+
271276
set(args_gn_input "${CMAKE_CURRENT_BINARY_DIR}/args.gn.in")
272277
file(GENERATE OUTPUT "${args_gn_input}" CONTENT "${chip_gn_args}")
273278

@@ -318,7 +323,11 @@ set(GN_ROOT_TARGET ${CHIP_ROOT}/config/esp32)
318323
set(chip_libraries "${CMAKE_CURRENT_BINARY_DIR}/lib/libCHIP.a")
319324

320325
if(CONFIG_ENABLE_PW_RPC)
321-
list(APPEND chip_libraries "${CMAKE_CURRENT_BINARY_DIR}/lib/libPwRpc.a")
326+
list(APPEND chip_libraries "${CMAKE_CURRENT_BINARY_DIR}/lib/libPwRpc.a")
327+
endif()
328+
329+
if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE)
330+
list(APPEND chip_libraries "${CMAKE_CURRENT_BINARY_DIR}/lib/libEsp32TracingBackend.a")
322331
endif()
323332

324333
# When using the pregenerated files, there is a edge case where an error appears for
@@ -371,10 +380,6 @@ target_include_directories(${COMPONENT_LIB} INTERFACE
371380
"${CHIP_ROOT}/config/esp32/${CONFIG_CHIP_EXTERNAL_PLATFORM_DIR}/../../"
372381
)
373382

374-
if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE)
375-
target_include_directories(${COMPONENT_LIB} INTERFACE "${CHIP_ROOT}/src/tracing/esp32_trace/include")
376-
endif()
377-
378383
idf_component_get_property(mbedtls_lib mbedtls COMPONENT_LIB)
379384

380385
idf_build_get_property(idf_target IDF_TARGET)
+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/build/
22
/sdkconfig
33
/sdkconfig.old
4+
main/insights_auth_key.txt

examples/lighting-app/esp32/main/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ if(${IDF_TARGET} STREQUAL "esp32")
7373
list(APPEND PRIV_REQUIRES_LIST spidriver screen-framework)
7474
endif()
7575

76+
7677
if (CONFIG_ENABLE_PW_RPC)
7778
# Append additional directories for RPC build
7879
set(PRIV_INCLUDE_DIRS_LIST "${PRIV_INCLUDE_DIRS_LIST}"

examples/lighting-app/esp32/main/main.cpp

+19-16
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
#include "DeviceCallbacks.h"
1919

2020
#include "AppTask.h"
21+
#include "esp_log.h"
2122
#include <common/CHIPDeviceManager.h>
2223
#include <common/Esp32AppServer.h>
2324
#include <common/Esp32ThreadInit.h>
24-
25-
#include "esp_log.h"
2625
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
2726
#include "spi_flash_mmap.h"
2827
#else
@@ -61,6 +60,8 @@
6160

6261
#if CONFIG_ENABLE_ESP_INSIGHTS_TRACE
6362
#include <esp_insights.h>
63+
#include <tracing/esp32_trace/esp32_tracing.h>
64+
#include <tracing/registry.h>
6465
#endif
6566

6667
using namespace ::chip;
@@ -113,6 +114,22 @@ static void InitServer(intptr_t context)
113114

114115
DeviceCallbacksDelegate::Instance().SetAppDelegate(&sAppDeviceCallbacksDelegate);
115116
Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config
117+
#if CONFIG_ENABLE_ESP_INSIGHTS_TRACE
118+
esp_insights_config_t config = {
119+
.log_type = ESP_DIAG_LOG_TYPE_ERROR | ESP_DIAG_LOG_TYPE_WARNING | ESP_DIAG_LOG_TYPE_EVENT,
120+
.auth_key = insights_auth_key_start,
121+
};
122+
123+
esp_err_t ret = esp_insights_init(&config);
124+
125+
if (ret != ESP_OK)
126+
{
127+
ESP_LOGE(TAG, "Failed to initialize ESP Insights, err:0x%x", ret);
128+
}
129+
130+
static Tracing::Insights::ESP32Backend backend;
131+
Tracing::Register(backend);
132+
#endif
116133
}
117134

118135
extern "C" void app_main()
@@ -134,20 +151,6 @@ extern "C" void app_main()
134151
chip::rpc::Init();
135152
#endif
136153

137-
#if CONFIG_ENABLE_ESP_INSIGHTS_TRACE
138-
esp_insights_config_t config = {
139-
.log_type = ESP_DIAG_LOG_TYPE_ERROR | ESP_DIAG_LOG_TYPE_WARNING | ESP_DIAG_LOG_TYPE_EVENT,
140-
.auth_key = insights_auth_key_start,
141-
};
142-
143-
esp_err_t ret = esp_insights_init(&config);
144-
145-
if (ret != ESP_OK)
146-
{
147-
ESP_LOGE(TAG, "Failed to initialize ESP Insights, err:0x%x", ret);
148-
}
149-
#endif
150-
151154
ESP_LOGI(TAG, "==================================================");
152155
ESP_LOGI(TAG, "chip-esp32-light-example starting");
153156
ESP_LOGI(TAG, "==================================================");

scripts/build/build/targets.py

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ def BuildEsp32Target():
206206

207207
target.AppendModifier('rpc', enable_rpcs=True)
208208
target.AppendModifier('ipv6only', enable_ipv4=False)
209+
target.AppendModifier('tracing', enable_insights_trace=True).OnlyIfRe("light")
209210

210211
return target
211212

scripts/build/builders/esp32.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,15 @@ def __init__(self,
147147
board: Esp32Board = Esp32Board.M5Stack,
148148
app: Esp32App = Esp32App.ALL_CLUSTERS,
149149
enable_rpcs: bool = False,
150-
enable_ipv4: bool = True
150+
enable_ipv4: bool = True,
151+
enable_insights_trace: bool = False
151152
):
152153
super(Esp32Builder, self).__init__(root, runner)
153154
self.board = board
154155
self.app = app
155156
self.enable_rpcs = enable_rpcs
156157
self.enable_ipv4 = enable_ipv4
158+
self.enable_insights_trace = enable_insights_trace
157159

158160
if not app.IsCompatible(board):
159161
raise Exception(
@@ -191,6 +193,15 @@ def generate(self):
191193
self._Execute(
192194
['bash', '-c', 'echo -e "\\nCONFIG_DISABLE_IPV4=y\\n" >>%s' % shlex.quote(defaults_out)])
193195

196+
if self.enable_insights_trace:
197+
insights_flag = 'y'
198+
else:
199+
insights_flag = 'n'
200+
201+
# pre-requisite
202+
self._Execute(
203+
['bash', '-c', 'echo -e "\\nCONFIG_ESP_INSIGHTS_ENABLED=%s\\nCONFIG_ENABLE_ESP_INSIGHTS_TRACE=%s\\n" >>%s' % (insights_flag, insights_flag, shlex.quote(defaults_out))])
204+
194205
cmake_flags = []
195206

196207
if self.options.pregen_dir:

scripts/build/testdata/all_targets_linux_x64.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ti-cc13x2x7_26x2x7-{lighting,lock,pump,pump-controller}[-mtd]
77
ti-cc13x4_26x4-{all-clusters,lighting,lock,pump,pump-controller}[-mtd][-ftd]
88
cyw30739-cyw930739m2evb_01-{light,lock,ota-requestor,switch}
99
efr32-{brd4161a,brd4187c,brd4186c,brd4163a,brd4164a,brd4166a,brd4170a,brd4186a,brd4187a,brd4304a}-{window-covering,switch,unit-test,light,lock,thermostat,pump}[-rpc][-with-ota-requestor][-icd][-low-power][-shell][-no_logging][-openthread_mtd][-enable_heap_monitoring][-no_openthread_cli][-show_qr_code][-wifi][-rs911x][-wf200][-wifi_ipv4][-additional_data_advertising][-use_ot_lib][-use_ot_coap_lib][-no-version]
10-
esp32-{m5stack,c3devkit,devkitc,qemu}-{all-clusters,all-clusters-minimal,ota-provider,ota-requestor,shell,light,lock,bridge,temperature-measurement,ota-requestor,tests}[-rpc][-ipv6only]
10+
esp32-{m5stack,c3devkit,devkitc,qemu}-{all-clusters,all-clusters-minimal,ota-provider,ota-requestor,shell,light,lock,bridge,temperature-measurement,ota-requestor,tests}[-rpc][-ipv6only][-tracing]
1111
genio-lighting-app
1212
linux-fake-tests[-mbedtls][-boringssl][-asan][-tsan][-ubsan][-libfuzzer][-ossfuzz][-coverage][-dmalloc][-clang]
1313
linux-{x64,arm64}-{rpc-console,all-clusters,all-clusters-minimal,chip-tool,thermostat,java-matter-controller,minmdns,light,lock,shell,ota-provider,ota-requestor,simulated-app1,simulated-app2,python-bindings,tv-app,tv-casting-app,bridge,tests,chip-cert,address-resolve-tool,contact-sensor,dishwasher,refrigerator,rvc}[-nodeps][-platform-mdns][-minmdns-verbose][-libnl][-same-event-loop][-no-interactive][-ipv6only][-no-ble][-no-wifi][-no-thread][-mbedtls][-boringssl][-asan][-tsan][-ubsan][-libfuzzer][-ossfuzz][-coverage][-dmalloc][-clang][-test][-rpc][-with-ui]

scripts/build/testdata/dry_run_esp32-devkitc-light-rpc.txt

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ cp examples/lighting-app/esp32/sdkconfig_rpc.defaults {out}/esp32-devkitc-light-
88

99
rm -f examples/lighting-app/esp32/sdkconfig
1010

11+
bash -c 'echo -e "\nCONFIG_ESP_INSIGHTS_ENABLED=n\nCONFIG_ENABLE_ESP_INSIGHTS_TRACE=n\n" >>{out}/esp32-devkitc-light-rpc/sdkconfig.defaults'
12+
1113
bash -c 'source $IDF_PATH/export.sh; source scripts/activate.sh;
1214
export SDKCONFIG_DEFAULTS={out}/esp32-devkitc-light-rpc/sdkconfig.defaults
1315
idf.py -C examples/lighting-app/esp32 -B {out}/esp32-devkitc-light-rpc reconfigure'

scripts/build/testdata/dry_run_esp32-m5stack-all-clusters-minimal-rpc-ipv6only.txt

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ rm -f examples/all-clusters-minimal-app/esp32/sdkconfig
1010

1111
bash -c 'echo -e "\nCONFIG_DISABLE_IPV4=y\n" >>{out}/esp32-m5stack-all-clusters-minimal-rpc-ipv6only/sdkconfig.defaults'
1212

13+
bash -c 'echo -e "\nCONFIG_ESP_INSIGHTS_ENABLED=n\nCONFIG_ENABLE_ESP_INSIGHTS_TRACE=n\n" >>{out}/esp32-m5stack-all-clusters-minimal-rpc-ipv6only/sdkconfig.defaults'
14+
1315
bash -c 'source $IDF_PATH/export.sh; source scripts/activate.sh;
1416
export SDKCONFIG_DEFAULTS={out}/esp32-m5stack-all-clusters-minimal-rpc-ipv6only/sdkconfig.defaults
1517
idf.py -C examples/all-clusters-minimal-app/esp32 -B {out}/esp32-m5stack-all-clusters-minimal-rpc-ipv6only reconfigure'

src/tracing/esp32_trace/BUILD.gn

+13-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,19 @@ config("tracing") {
2020
include_dirs = [ "include" ]
2121
}
2222

23-
source_set("esp32_trace") {
23+
static_library("backend") {
24+
output_name = "libEsp32TracingBackend"
25+
output_dir = "${root_out_dir}/lib"
26+
27+
sources = [
28+
"esp32_tracing.cpp",
29+
"esp32_tracing.h",
30+
]
31+
public_deps = [ "${chip_root}/src/tracing" ]
32+
}
33+
34+
source_set("esp32_trace_tracing") {
2435
public = [ "include/matter/tracing/macros_impl.h" ]
25-
sources = [ "include/matter/tracing/macros_impl.cpp" ]
2636
public_configs = [ ":tracing" ]
37+
deps = [ ":backend" ]
2738
}

src/tracing/esp32_trace/include/matter/tracing/macros_impl.cpp src/tracing/esp32_trace/esp32_tracing.cpp

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/*
2+
*
23
* Copyright (c) 2023 Project CHIP Authors
4+
* All rights reserved.
35
*
46
* Licensed under the Apache License, Version 2.0 (the "License");
57
* you may not use this file except in compliance with the License.
@@ -14,9 +16,15 @@
1416
* limitations under the License.
1517
*/
1618

17-
#include "macros_impl.h"
19+
#include "esp32_tracing.h"
1820
#include <esp_heap_caps.h>
1921
#include <esp_insights.h>
22+
#include <esp_log.h>
23+
#include <memory>
24+
#include <tracing/backend.h>
25+
26+
namespace chip {
27+
namespace Tracing {
2028
namespace Insights {
2129

2230
#define LOG_HEAP_INFO(label, group, entry_exit) \
@@ -28,17 +36,31 @@ namespace Insights {
2836
heap_caps_get_free_size(MALLOC_CAP_8BIT)); \
2937
} while (0)
3038

31-
ESP32Backend::ESP32Backend(const char * str, ...)
39+
void ESP32Backend::LogMessageReceived(MessageReceivedInfo & info) {}
40+
41+
void ESP32Backend::LogMessageSend(MessageSendInfo & info) {}
42+
43+
void ESP32Backend::LogNodeLookup(NodeLookupInfo & info) {}
44+
45+
void ESP32Backend::LogNodeDiscovered(NodeDiscoveredInfo & info) {}
46+
47+
void ESP32Backend::LogNodeDiscoveryFailed(NodeDiscoveryFailedInfo & info) {}
48+
49+
void ESP32Backend::TraceBegin(const char * label, const char * group)
50+
{
51+
LOG_HEAP_INFO(label, group, "Entry");
52+
}
53+
54+
void ESP32Backend::TraceEnd(const char * label, const char * group)
3255
{
33-
va_list args;
34-
va_start(args, str);
35-
mlabel = str;
36-
mgroup = va_arg(args, const char *);
37-
LOG_HEAP_INFO(mlabel, mgroup, "Entry");
56+
LOG_HEAP_INFO(label, group, "Exit");
3857
}
3958

40-
ESP32Backend::~ESP32Backend()
59+
void ESP32Backend::TraceInstant(const char * label, const char * group)
4160
{
42-
LOG_HEAP_INFO(mlabel, mgroup, "Exit");
61+
ESP_DIAG_EVENT("MTR_TRC", "Instant : %s -%s", label, group);
4362
}
4463
} // namespace Insights
64+
} // namespace Tracing
65+
} // namespace chip
66+
// namespace chip
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <tracing/backend.h>
2+
3+
#include <memory>
4+
5+
namespace chip {
6+
namespace Tracing {
7+
namespace Insights {
8+
9+
/// A Backend that outputs data to chip logging.
10+
///
11+
/// Structured data is formatted as json strings.
12+
class ESP32Backend : public ::chip::Tracing::Backend
13+
{
14+
public:
15+
ESP32Backend() = default;
16+
17+
void TraceBegin(const char * label, const char * group) override;
18+
19+
void TraceEnd(const char * label, const char * group) override;
20+
21+
/// Trace a zero-sized event
22+
void TraceInstant(const char * label, const char * group) override;
23+
24+
void LogMessageSend(MessageSendInfo &) override;
25+
void LogMessageReceived(MessageReceivedInfo &) override;
26+
27+
void LogNodeLookup(NodeLookupInfo &) override;
28+
void LogNodeDiscovered(NodeDiscoveredInfo &) override;
29+
void LogNodeDiscoveryFailed(NodeDiscoveryFailedInfo &) override;
30+
};
31+
32+
} // namespace Insights
33+
} // namespace Tracing
34+
} // namespace chip
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
*
23
* Copyright (c) 2023 Project CHIP Authors
34
* All rights reserved.
45
*
@@ -21,30 +22,30 @@
2122
#error "Tracing macros seem to be double defined"
2223
#endif
2324

25+
#include <tracing/registry.h>
26+
27+
// This gets forwarded to the multiplexed instance
28+
#define MATTER_TRACE_BEGIN(label, group) ::chip::Tracing::Internal::Begin(label, group)
29+
#define MATTER_TRACE_END(label, group) ::chip::Tracing::Internal::End(label, group)
30+
#define MATTER_TRACE_INSTANT(label, group) ::chip::Tracing::Internal::Instant(label, group)
31+
32+
namespace chip {
33+
namespace Tracing {
2434
namespace Insights {
25-
class ESP32Backend
35+
class Scoped
2636
{
2737
public:
28-
ESP32Backend(const char * str, ...);
29-
~ESP32Backend();
38+
inline Scoped(const char * label, const char * group) : mLabel(label), mGroup(group) { MATTER_TRACE_BEGIN(label, group); }
39+
inline ~Scoped() { MATTER_TRACE_END(mLabel, mGroup); }
3040

3141
private:
32-
const char * mlabel;
33-
const char * mgroup;
42+
const char * mLabel;
43+
const char * mGroup;
3444
};
3545
} // namespace Insights
46+
} // namespace Tracing
47+
} // namespace chip
48+
#define _CONCAT_IMPL(a, b) a##b
49+
#define _MACRO_CONCAT(a, b) _CONCAT_IMPL(a, b)
3650

37-
#define MATTER_TRACE_SCOPE(...) \
38-
do \
39-
{ \
40-
Insights::ESP32Backend backend(__VA_ARGS__); \
41-
} while (0)
42-
43-
#define _MATTER_TRACE_DISABLE(...) \
44-
do \
45-
{ \
46-
} while (false)
47-
48-
#define MATTER_TRACE_BEGIN(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
49-
#define MATTER_TRACE_END(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
50-
#define MATTER_TRACE_INSTANT(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
51+
#define MATTER_TRACE_SCOPE(label, group) ::chip::Tracing::Insights::Scoped _MACRO_CONCAT(_trace_scope, __COUNTER__)(label, group)

0 commit comments

Comments
 (0)