|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2024 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <string.h> |
| 20 | +#include <tracing/esp32_diagnostic_trace/Counter.h> |
| 21 | + |
| 22 | +using namespace chip; |
| 23 | + |
| 24 | +namespace Insights { |
| 25 | + |
| 26 | +// This is a one time allocation for counters. It is not supposed to be freed. |
| 27 | +ESPDiagnosticCounter * ESPDiagnosticCounter::mHead = nullptr; |
| 28 | + |
| 29 | +ESPDiagnosticCounter * ESPDiagnosticCounter::GetInstance(const char * label) |
| 30 | +{ |
| 31 | + ESPDiagnosticCounter * current = mHead; |
| 32 | + |
| 33 | + while (current != nullptr) |
| 34 | + { |
| 35 | + if (strcmp(current->label, label) == 0) |
| 36 | + { |
| 37 | + current->instanceCount++; |
| 38 | + return current; |
| 39 | + } |
| 40 | + current = current->mNext; |
| 41 | + } |
| 42 | + |
| 43 | + // Allocate a new instance if counter is not present in the list. |
| 44 | + void * ptr = Platform::MemoryAlloc(sizeof(ESPDiagnosticCounter)); |
| 45 | + VerifyOrDie(ptr != nullptr); |
| 46 | + |
| 47 | + ESPDiagnosticCounter * newInstance = new (ptr) ESPDiagnosticCounter(label); |
| 48 | + newInstance->mNext = mHead; |
| 49 | + mHead = newInstance; |
| 50 | + |
| 51 | + return newInstance; |
| 52 | +} |
| 53 | + |
| 54 | +int32_t ESPDiagnosticCounter::GetInstanceCount() const |
| 55 | +{ |
| 56 | + return instanceCount; |
| 57 | +} |
| 58 | + |
| 59 | +void ESPDiagnosticCounter::ReportMetrics() |
| 60 | +{ |
| 61 | + CHIP_ERROR err = CHIP_NO_ERROR; |
| 62 | + Counter counter(label, instanceCount, esp_log_timestamp()); |
| 63 | + DiagnosticStorageImpl & diagnosticStorage = DiagnosticStorageImpl::GetInstance(); |
| 64 | + err = diagnosticStorage.Store(counter); |
| 65 | + VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Failed to store Counter diagnostic data")); |
| 66 | +} |
| 67 | + |
| 68 | +} // namespace Insights |
0 commit comments