Skip to content

Commit

Permalink
Added custom_tracing_functionality for esp32 platform and integrated …
Browse files Browse the repository at this point in the history
…the tracing with esp_insights.

Added a esp32_trace directory to provide esp32 specific basic implementation of the scoped tracing macros.
Integrated esp-insights on a config option in the all-clusters-app.
This is an initial implemenation of the matter_insights framework .
  • Loading branch information
shripad621git committed Jul 27, 2023
1 parent 426be96 commit c9f80a2
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 3 deletions.
9 changes: 9 additions & 0 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ if (CONFIG_SEC_CERT_DAC_PROVIDER)
chip_gn_arg_append("chip_use_secure_cert_dac_provider" "true")
endif()

if (CONFIG_ENABLE_INSIGHTS_CUSTOM_TRACE)
chip_gn_arg_append("chip_enable_insights_custom_trace" "true")
endif()

if (CONFIG_USE_ESP32_ECDSA_PERIPHERAL)
chip_gn_arg_append("chip_use_esp32_ecdsa_peripheral" "true")
endif()
Expand Down Expand Up @@ -414,6 +418,11 @@ if (CONFIG_SEC_CERT_DAC_PROVIDER)
list(APPEND chip_libraries $<TARGET_FILE:${esp32_secure_cert_mgr_lib}>)
endif()

if (CONFIG_ENABLE_INSIGHTS_CUSTOM_TRACE)
idf_component_get_property(esp_insights_lib espressif__esp_insights COMPONENT_LIB)
list(APPEND chip_libraries $<TARGET_FILE:${esp_insights_lib}>)
endif()

idf_component_get_property(lwip_lib lwip COMPONENT_LIB)
list(APPEND chip_libraries $<TARGET_FILE:${lwip_lib}>)

Expand Down
7 changes: 7 additions & 0 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,13 @@ menu "CHIP Device Layer"
then this option gets enabled.
Also, please disable ESP_SECURE_CERT_DS_PERIPHERAL from the menuconfig when this option is disabled

config ENABLE_INSIGHTS_CUSTOM_TRACE
bool "Enable ESP32 Custom Tracing Functionality"
depends on ESP_INSIGHTS_ENABLED
default n
help
Enables ESP32 Custom Tracing functionality with esp-insights.

endmenu


Expand Down
3 changes: 3 additions & 0 deletions config/esp32/components/chip/idf_component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ dependencies:
version: "2.0.3"
rules:
- if: "idf_version >=4.4"

espressif/esp_insights:
version: "1.0.0"
21 changes: 21 additions & 0 deletions examples/all-clusters-app/esp32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ and refer
[building and commissioning](../../../docs/guides/esp32/build_app_and_commission.md)
guides to get started.

#Note: Enabling ESP-Insights in the example:

1.Before building the app, enable the options: ESP_INSIGHTS_ENABLED and
CONFIG_ENABLE_INSIGHTS_CUSTOM_TRACE through menuconfig.

2.Create a file named insights_auth_key.txt in the main directory of the
example.

3.Follow the steps
present[here](https://github.com/espressif/esp-insights/blob/main/examples/README.md#set-up-esp-insights-account)
to set up an insights_account and the auth key created while setting it up
willbe used in the example.

4.Download the auth key and copy Auth Key to the example

```
cp /path/to/auth/key.txt path/to/connectedhomeip/examples/all-clusters-app/esp32/main/insights_auth_key.txt
```

5.Now build the example.

---

- [Cluster control](#cluster-control)
Expand Down
18 changes: 17 additions & 1 deletion examples/all-clusters-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ set(SRC_DIRS_LIST

set(EXCLUDE_SRCS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/all-clusters-app/all-clusters-common/src/static-supported-modes-manager.cpp")

if (CONFIG_ENABLE_INSIGHTS_CUSTOM_TRACE)
#Append directories for build
set(PRIV_INCLUDE_DIRS_LIST "${PRIV_INCLUDE_DIRS_LIST}"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/tracing/esp32_trace/include")
endif()

if (NOT CONFIG_ENABLE_INSIGHTS_CUSTOM_TRACE)
set(PRIV_INCLUDE_DIRS_LIST "${PRIV_INCLUDE_DIRS_LIST}"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/tracing/none/include")
endif ()

if (CONFIG_ENABLE_PW_RPC)
# Append additional directories for RPC build
set(PRIV_INCLUDE_DIRS_LIST "${PRIV_INCLUDE_DIRS_LIST}"
Expand All @@ -119,7 +130,7 @@ set(SRC_DIRS_LIST "${SRC_DIRS_LIST}"
)
endif (CONFIG_ENABLE_PW_RPC)

set(PRIV_REQUIRES_LIST chip QRCode bt driver app_update nvs_flash spi_flash openthread)
set(PRIV_REQUIRES_LIST chip QRCode bt driver app_update nvs_flash spi_flash openthread espressif__esp_insights)
if(CONFIG_HAVE_DISPLAY)
list(APPEND PRIV_REQUIRES_LIST tft spidrier screen-framework)
endif()
Expand All @@ -146,6 +157,11 @@ target_compile_options(${COMPONENT_LIB} PUBLIC
"-DCHIP_ADDRESS_RESOLVE_IMPL_INCLUDE_HEADER=<lib/address_resolve/AddressResolve_DefaultImpl.h>"
)


if (CONFIG_ENABLE_INSIGHTS_CUSTOM_TRACE)
target_add_binary_data(${COMPONENT_TARGET} "insights_auth_key.txt" TEXT)
endif()

if (CONFIG_ENABLE_PW_RPC)


Expand Down
23 changes: 23 additions & 0 deletions examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
#include <platform/ESP32/ESP32Utils.h>
#include <static-supported-temperature-levels.h>

#if CONFIG_ENABLE_INSIGHTS_CUSTOM_TRACE
#include <esp_insights.h>
#endif

#if CONFIG_HAVE_DISPLAY
#include "DeviceWithDisplay.h"
#endif
Expand Down Expand Up @@ -78,6 +82,11 @@ using namespace ::chip::Credentials;

const char * TAG = "all-clusters-app";

#if CONFIG_ENABLE_INSIGHTS_CUSTOM_TRACE
extern const char insights_auth_key_start[] asm("_binary_insights_auth_key_txt_start");
extern const char insights_auth_key_end[] asm("_binary_insights_auth_key_txt_end");
#endif

static AppDeviceCallbacks EchoCallbacks;
static AppDeviceCallbacksDelegate sAppDeviceCallbacksDelegate;
namespace {
Expand Down Expand Up @@ -161,6 +170,20 @@ extern "C" void app_main()
chip::rpc::Init();
#endif

#if CONFIG_ENABLE_INSIGHTS_CUSTOM_TRACE
esp_insights_config_t config = {
.log_type = ESP_DIAG_LOG_TYPE_ERROR | ESP_DIAG_LOG_TYPE_WARNING | ESP_DIAG_LOG_TYPE_EVENT,
.auth_key = insights_auth_key_start,
};

esp_err_t ret = esp_insights_init(&config);

if (ret != ESP_OK)
{
ESP_LOGE(TAG, "Failed to initialize ESP Insights, err:0x%x", ret);
}
#endif

ESP_LOGI(TAG, "==================================================");
ESP_LOGI(TAG, "chip-esp32-all-cluster-demo starting");
ESP_LOGI(TAG, "==================================================");
Expand Down
27 changes: 27 additions & 0 deletions src/tracing/esp32_trace/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
#Copyright (c) 2023 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

config("tracing") {
include_dirs = [ "include" ]
}

source_set("esp32_trace") {
public = [ "include/matter/tracing/macros_impl.h" ]
sources = [ "include/matter/tracing/macros_impl.cpp" ]
public_configs = [ ":tracing" ]
}
44 changes: 44 additions & 0 deletions src/tracing/esp32_trace/include/matter/tracing/macros_impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "macros_impl.h"
#include <esp_heap_caps.h>
#include <esp_insights.h>
namespace Insights {

ESP32Backend::ESP32Backend(const char * str, ...)
{
va_list args;
va_start(args, str);
var1 = str;
var2 = va_arg(args, const char *);

int start_min_heap = heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT);
int largest_free_block = heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
int start_free_heap = heap_caps_get_free_size(MALLOC_CAP_8BIT);
ESP_DIAG_EVENT("MTR_TRC", "Entry - %s - %s - Start Min Free heap - %5d - Largest Free Block - %5d Start free heap -%5d", var1,
var2, start_min_heap, largest_free_block, start_free_heap);
}

ESP32Backend::~ESP32Backend()
{
int end_minimum_free_heap = heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT);
int end_largest_free_block = heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
int end_free_heap = heap_caps_get_free_size(MALLOC_CAP_8BIT);
ESP_DIAG_EVENT("MTR_TRC", "Exit - %s - %s - End Min Free heap - %5d - End Largest Free Block %5d - End free Heap - %5d", var1,
var2, end_minimum_free_heap, end_largest_free_block, end_free_heap);
}
} // namespace Insights
50 changes: 50 additions & 0 deletions src/tracing/esp32_trace/include/matter/tracing/macros_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

/* Ensure we do not have double tracing macros defined */
#if defined(MATTER_TRACE_BEGIN)
#error "Tracing macros seem to be double defined"
#endif

namespace Insights {
class ESP32Backend
{
public:
ESP32Backend(const char * str, ...);
~ESP32Backend();

private:
const char * var1;
const char * var2;
};
} // namespace Insights

#define MATTER_TRACE_SCOPE(...) \
do \
{ \
Insights::ESP32Backend backend(__VA_ARGS__); \
} while (0)

#define _MATTER_TRACE_DISABLE(...) \
do \
{ \
} while (false)

#define MATTER_TRACE_BEGIN(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
#define MATTER_TRACE_END(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
#define MATTER_TRACE_INSTANT(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
1 change: 1 addition & 0 deletions src/tracing/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
// MATTER_TRACE_END(label, group)
// MATTER_TRACE_INSTANT(label, group)
// MATTER_TRACE_SCOPE(label, group)

#include <matter/tracing/macros_impl.h>
#include <tracing/log_declares.h>
#include <tracing/registry.h>
Expand Down
16 changes: 14 additions & 2 deletions src/tracing/tracing_args.gni
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")
import("${build_root}/config/compiler/compiler.gni")
import("${chip_root}/src/platform/device.gni")

declare_args() {
# Tracing can have a non-zero size impact to binaries. We try to make
Expand All @@ -22,7 +23,9 @@ declare_args() {
#
# Additionally, if tracing is enabled, the main() function has to add
# backends explicitly
matter_enable_tracing_support = current_os == "android"
matter_enable_tracing_support =
current_os == "android" || chip_device_platform == "esp32"
chip_enable_insights_custom_trace = false

# Defines the trace backend. Current matter tracing splits the logic
# into two parts:
Expand All @@ -43,7 +46,16 @@ declare_args() {
# since tracing is very noisy, we generally expect it to be explicitly
# set up.
#
if (current_os == "linux" || current_os == "android") {
}

declare_args() {
if (chip_device_platform == "esp32" && chip_enable_insights_custom_trace) {
matter_trace_config = "${chip_root}/src/tracing/esp32_trace"
} else
# TODO: cpp_standard check is not ideal, it should be >= 17,
# however for now this is what we use in compilations
if ((current_os == "linux" || current_os == "android") &&
cpp_standard == "gnu++17") {
matter_trace_config = "${chip_root}/src/tracing/perfetto:perfetto_tracing"
} else {
matter_trace_config = "${chip_root}/src/tracing/none"
Expand Down

0 comments on commit c9f80a2

Please sign in to comment.