forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce66c4d
commit 75676cf
Showing
6 changed files
with
269 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
src/tracing/esp32_trace/include/matter/tracing/insights_sys_stats.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* 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_diag_metrics_add_uint(instance->LabelPointer[i], static_cast<int>(highwatermarks[i])); | ||
} | ||
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_diag_metrics_register(tag, LabelPointer[i], LabelPointer[i], path, ESP_DIAG_DATA_TYPE_UINT); | ||
} | ||
|
||
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 |
75 changes: 75 additions & 0 deletions
75
src/tracing/esp32_trace/include/matter/tracing/insights_sys_stats.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |