3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2016-2017 Nest Labs, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * This file implements the CHIP API to collect statistics
22 * on the state of CHIP, Inet and System resources
25 // Include common private header
26 #include "SystemLayerPrivate.h"
28 // Include local headers
29 #include <system/SystemTimer.h>
31 // Include module header
32 #include <system/SystemStats.h>
34 #include <support/SafeInt.h>
42 static const Label sStatsStrings[chip::System::Stats::kNumEntries] = {
43 #if CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_PBUF_FROM_CUSTOM_POOLS
44 #define LWIP_PBUF_MEMPOOL(name, num, payload, desc) "SystemLayer_Num" desc,
45 #include "lwippools.h"
46 #undef LWIP_PBUF_MEMPOOL
48 "SystemLayer_NumPacketBufs",
50 "SystemLayer_NumTimersInUse",
51 #if INET_CONFIG_NUM_RAW_ENDPOINTS
52 "InetLayer_NumRawEpsInUse",
54 #if INET_CONFIG_NUM_TCP_ENDPOINTS
55 "InetLayer_NumTCPEpsInUse",
57 #if INET_CONFIG_NUM_UDP_ENDPOINTS
58 "InetLayer_NumUDPEpsInUse",
60 #if INET_CONFIG_NUM_DNS_RESOLVERS
61 "InetLayer_NumDNSResolversInUse",
63 "ExchangeMgr_NumContextsInUse", "ExchangeMgr_NumUMHandlersInUse",
64 "ExchangeMgr_NumBindings", "MessageLayer_NumConnectionsInUse",
67 count_t sResourcesInUse[kNumEntries];
68 count_t sHighWatermarks[kNumEntries];
70 const Label * GetStrings()
75 count_t * GetResourcesInUse()
77 return sResourcesInUse;
80 count_t * GetHighWatermarks()
82 return sHighWatermarks;
85 void UpdateSnapshot(Snapshot & aSnapshot)
87 memcpy(&aSnapshot.mResourcesInUse, &sResourcesInUse, sizeof(aSnapshot.mResourcesInUse));
88 memcpy(&aSnapshot.mHighWatermarks, &sHighWatermarks, sizeof(aSnapshot.mHighWatermarks));
90 chip::System::Timer::GetStatistics(aSnapshot.mResourcesInUse[kSystemLayer_NumTimers],
91 aSnapshot.mHighWatermarks[kSystemLayer_NumTimers]);
93 SYSTEM_STATS_UPDATE_LWIP_PBUF_COUNTS();
96 bool Difference(Snapshot & result, Snapshot & after, Snapshot & before)
101 for (i = 0; i < kNumEntries; i++)
103 // TODO: These casts can be bogus. https://github.com/project-chip/connectedhomeip/issues/2949
104 result.mResourcesInUse[i] = static_cast<count_t>(after.mResourcesInUse[i] - before.mResourcesInUse[i]);
105 result.mHighWatermarks[i] = static_cast<count_t>(after.mHighWatermarks[i] - before.mHighWatermarks[i]);
107 if (result.mResourcesInUse[i] > 0)
116 #if CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_STATS && MEMP_STATS
117 void UpdateLwipPbufCounts(void)
119 #if LWIP_PBUF_FROM_CUSTOM_POOLS
120 size_t lwip_pool_idx = PBUF_CUSTOM_POOL_IDX_END;
121 size_t system_idx = 0;
123 while (lwip_pool_idx <= PBUF_CUSTOM_POOL_IDX_START)
125 chip::System::Stats::GetResourcesInUse()[system_idx] = MEMP_STATS_GET(used, lwip_pool_idx);
126 chip::System::Stats::GetHighWatermarks()[system_idx] = MEMP_STATS_GET(max, lwip_pool_idx);
131 #else // LWIP_PBUF_FROM_CUSTOM_POOLS
133 chip::System::Stats::GetResourcesInUse()[kSystemLayer_NumPacketBufs] = MEMP_STATS_GET(used, MEMP_PBUF_POOL);
134 chip::System::Stats::GetHighWatermarks()[kSystemLayer_NumPacketBufs] = MEMP_STATS_GET(max, MEMP_PBUF_POOL);
136 #endif // LWIP_PBUF_FROM_CUSTOM_POOLS
138 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_STATS && MEMP_STATS
141 } // namespace System