Skip to content

Commit

Permalink
Insights System Stats
Browse files Browse the repository at this point in the history
  • Loading branch information
shripad621git committed Sep 8, 2023
1 parent ce66c4d commit a91a550
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 3 deletions.
4 changes: 4 additions & 0 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ if (CONFIG_ENABLE_ESP_INSIGHTS_TRACE)
chip_gn_arg_append("matter_enable_esp_insights_trace" "true")
endif()

if (CONFIG_ENABLE_ESP_INSIGHTS_SYSTEM_STATS)
chip_gn_arg_append("matter_enable_esp_insights_system_stats" "true")
endif()

if (CONFIG_USE_ESP32_ECDSA_PERIPHERAL)
chip_gn_arg_append("chip_use_esp32_ecdsa_peripheral" "true")
endif()
Expand Down
6 changes: 6 additions & 0 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,12 @@ menu "CHIP Device Layer"
Enabling the above option will enable the esp32 specific tracing functionality and report the
diagnostic information to the insights cloud.

config ENABLE_ESP_INSIGHTS_SYSTEM_STATS
bool "Enable System Stats for insights"
depends on ESP_INSIGHTS_ENABLED
default n
help
This option enables the system statistics to be sent to the insights cloud.
endmenu


Expand Down
8 changes: 8 additions & 0 deletions examples/lighting-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#include <credentials/examples/DeviceAttestationCredsExample.h>
#include <platform/ESP32/ESP32Utils.h>

#if CONFIG_ENABLE_ESP_INSIGHTS_SYSTEM_STATS
#include <matter/tracing/insights_sys_stats.h>
#endif

#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
#include <platform/ESP32/ESP32FactoryDataProvider.h>
#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
Expand Down Expand Up @@ -113,6 +117,10 @@ static void InitServer(intptr_t context)

DeviceCallbacksDelegate::Instance().SetAppDelegate(&sAppDeviceCallbacksDelegate);
Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config

#if CONFIG_ENABLE_ESP_INSIGHTS_SYSTEM_STATS
chip::System::Stats::InsightsSystemMetrics::GetInstance().RegisterAndEnable(chip::System::Clock::Timeout(5000));
#endif
}

extern "C" void app_main()
Expand Down
18 changes: 15 additions & 3 deletions src/tracing/esp32_trace/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,21 @@ import("//build_overrides/chip.gni")
config("tracing") {
include_dirs = [ "include" ]
}

declare_args()
{
matter_enable_esp_insights_system_stats = false
}
source_set("esp32_trace") {
public = [ "include/matter/tracing/macros_impl.h" ]
sources = [ "include/matter/tracing/macros_impl.cpp" ]
public = [ "include/matter/tracing/macros_impl.h", ]
sources = [ "include/matter/tracing/macros_impl.cpp", ]
public_configs = [ ":tracing" ]

if (matter_enable_esp_insights_system_stats)
{
public += ["include/matter/tracing/insights_sys_stats.h",]
sources += ["include/matter/tracing/insights_sys_stats.cpp",]
deps = [ "${chip_root}/src/lib/core",
"${chip_root}/src/system",
"${chip_root}/src/platform"]
}
}
163 changes: 163 additions & 0 deletions src/tracing/esp32_trace/include/matter/tracing/insights_sys_stats.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* 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.
*/

#include "insights_sys_stats.h"
#include <esp_diagnostics_metrics.h>
#include <esp_err.h>
#include <platform/CHIPDeviceLayer.h>
#include <string.h>
#include <system/SystemStats.h>

namespace chip {
namespace System {
namespace Stats {

InsightsSystemMetrics::InsightsSystemMetrics()
{
mRegistered = false;
mTimeout = System::Clock::Milliseconds32(5000);
for (int i = 0; i < kNumEntries; i++)
{
LabelPointer[i] = (char *) malloc(sizeof(kMaxStringLength));
}
}

InsightsSystemMetrics & InsightsSystemMetrics::GetInstance()
{
static InsightsSystemMetrics instance;
return instance;
}

InsightsSystemMetrics::~InsightsSystemMetrics()
{
for (int i = 0; i < chip::System::Stats::kNumEntries; i++)
{
if (LabelPointer[i] != nullptr)
{
free(LabelPointer[i]);
LabelPointer[i] = nullptr;
}
}
}

static void TimerCallback(Layer * systemLayer, void * context)
{
auto instance = static_cast<InsightsSystemMetrics *>(context);
count_t * highwatermarks = GetHighWatermarks();
for (int i = 0; i < System::Stats::kNumEntries; i++)
{
esp_err_t err = esp_diag_metrics_add_uint(instance->LabelPointer[i], static_cast<int>(highwatermarks[i]));
printf("error Add %d", err);
}
DeviceLayer::SystemLayer().StartTimer(instance->GetTimeout(), TimerCallback, instance);
}

CHIP_ERROR InsightsSystemMetrics::UnRegister()
{
for (int i = 0; i < System::Stats::kNumEntries; i++)
{
if (LabelPointer[i] != nullptr)
{
esp_diag_metrics_unregister(LabelPointer[i]);
}
}
for (int i = 0; i < chip::System::Stats::kNumEntries; i++)
{
if (LabelPointer[i] != nullptr)
{
free(LabelPointer[i]);
LabelPointer[i] = nullptr;
}
}
return CHIP_NO_ERROR;
}

CHIP_ERROR InsightsSystemMetrics::CancelTimeout(chip::System::Clock::Timeout aTimeout)
{
if (!mRegistered)
{
return CHIP_ERROR_INCORRECT_STATE;
}

if (aTimeout == System::Clock::kZero)
{
DeviceLayer::SystemLayer().CancelTimer(TimerCallback, this);
for (int i = 0; i < chip::System::Stats::kNumEntries; i++)
{
if (LabelPointer[i] != nullptr)
{
free(LabelPointer[i]);
LabelPointer[i] = nullptr;
}
}
}
else if (aTimeout != mTimeout)
{
DeviceLayer::SystemLayer().CancelTimer(TimerCallback, this);
mTimeout = aTimeout;
DeviceLayer::SystemLayer().StartTimer(mTimeout, TimerCallback, this);
}

return CHIP_NO_ERROR; // Return an appropriate value here
}

CHIP_ERROR InsightsSystemMetrics::RegisterAndEnable(chip::System::Clock::Timeout aTimeout)
{
if (mRegistered)
{
return CHIP_NO_ERROR;
}

if (aTimeout == System::Clock::kZero)
{
return CHIP_ERROR_INVALID_ARGUMENT;
}

const Label * labels = GetStrings();
for (int i = 0; i < System::Stats::kNumEntries; i++)
{
size_t labelLength = strlen(labels[i]);
if (labelLength > kMaxStringLength)
{
labelLength = kMaxStringLength;
}

LabelPointer[i] = strndup(labels[i], labelLength - 1);
if (LabelPointer[i] == NULL)
{
UnRegister();
return CHIP_ERROR_NO_MEMORY;
}

esp_err_t err1 = esp_diag_metrics_register(tag, LabelPointer[i], LabelPointer[i], path, ESP_DIAG_DATA_TYPE_UINT);
printf("Err regsiter %d", err1);
}

SetTimeout(aTimeout);

CHIP_ERROR err = DeviceLayer::SystemLayer().StartTimer(GetTimeout(), TimerCallback, this);
if (err != CHIP_NO_ERROR)
{
return err;
}
mRegistered = true;
return CHIP_NO_ERROR;
}

} // namespace Stats
} // namespace System
} // namespace chip
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.
*/

#include <lib/core/CHIPError.h>
#include <system/SystemClock.h>
#include <system/SystemStats.h>
namespace chip {
namespace System {
namespace Stats {

class InsightsSystemMetrics
{
public:
static InsightsSystemMetrics & GetInstance();

/*
* This api registers the system metrics to the insights and starts the
* timer to enable system stats periodically to the insights.
*/
CHIP_ERROR RegisterAndEnable(chip::System::Clock::Timeout aTimeout);

/*
* This api unregisters the system stats which are registered
* as metrics to the esp-insights.
*/
CHIP_ERROR UnRegister();

/*
* This api cancels the timeout providing the user the flexibility
* to increase or decrease the frequency of sampling the System
* Stats. It cancels the timer if new timeout value is zero.
* If the value of timeout differs from existing value, then
* it cancels the previous timer and starts a new timer.
*/
CHIP_ERROR CancelTimeout(chip::System::Clock::Timeout aTimeout);

/*
* This api sets the mTimeout value to the specified value.
* if the value of this timer is set to 0, then it cancels
* the timer to fetch system metrics.
*/
void SetTimeout(System::Clock::Timeout aTimeout) { mTimeout = System::Clock::Milliseconds32(aTimeout); }
System::Clock::Timeout GetTimeout() { return mTimeout; }

// Destructor to deallocate memory
~InsightsSystemMetrics();

char * LabelPointer[chip::System::Stats::kNumEntries];

private:
InsightsSystemMetrics();
static constexpr int kMaxStringLength = 16;
bool mRegistered;
static constexpr char path[] = "sys.mtr";
static constexpr char tag[] = "MTR";
System::Clock::Timeout mTimeout;
};

} // namespace Stats
} // namespace System
} // namespace chip

0 comments on commit a91a550

Please sign in to comment.