Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / system / SystemStats.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2016-2017 Nest Labs, Inc.
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 /**
20  * @file
21  *  This file implements the CHIP API to collect statistics
22  *  on the state of CHIP, Inet and System resources
23  */
24
25 // Include common private header
26 #include "SystemLayerPrivate.h"
27
28 // Include local headers
29 #include <system/SystemTimer.h>
30
31 // Include module header
32 #include <system/SystemStats.h>
33
34 #include <support/SafeInt.h>
35
36 #include <string.h>
37
38 namespace chip {
39 namespace System {
40 namespace Stats {
41
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
47 #else
48     "SystemLayer_NumPacketBufs",
49 #endif
50     "SystemLayer_NumTimersInUse",
51 #if INET_CONFIG_NUM_RAW_ENDPOINTS
52     "InetLayer_NumRawEpsInUse",
53 #endif
54 #if INET_CONFIG_NUM_TCP_ENDPOINTS
55     "InetLayer_NumTCPEpsInUse",
56 #endif
57 #if INET_CONFIG_NUM_UDP_ENDPOINTS
58     "InetLayer_NumUDPEpsInUse",
59 #endif
60 #if INET_CONFIG_NUM_DNS_RESOLVERS
61     "InetLayer_NumDNSResolversInUse",
62 #endif
63     "ExchangeMgr_NumContextsInUse",   "ExchangeMgr_NumUMHandlersInUse",
64     "ExchangeMgr_NumBindings",        "MessageLayer_NumConnectionsInUse",
65 };
66
67 count_t sResourcesInUse[kNumEntries];
68 count_t sHighWatermarks[kNumEntries];
69
70 const Label * GetStrings()
71 {
72     return sStatsStrings;
73 }
74
75 count_t * GetResourcesInUse()
76 {
77     return sResourcesInUse;
78 }
79
80 count_t * GetHighWatermarks()
81 {
82     return sHighWatermarks;
83 }
84
85 void UpdateSnapshot(Snapshot & aSnapshot)
86 {
87     memcpy(&aSnapshot.mResourcesInUse, &sResourcesInUse, sizeof(aSnapshot.mResourcesInUse));
88     memcpy(&aSnapshot.mHighWatermarks, &sHighWatermarks, sizeof(aSnapshot.mHighWatermarks));
89
90     chip::System::Timer::GetStatistics(aSnapshot.mResourcesInUse[kSystemLayer_NumTimers],
91                                        aSnapshot.mHighWatermarks[kSystemLayer_NumTimers]);
92
93     SYSTEM_STATS_UPDATE_LWIP_PBUF_COUNTS();
94 }
95
96 bool Difference(Snapshot & result, Snapshot & after, Snapshot & before)
97 {
98     int i;
99     bool leak = false;
100
101     for (i = 0; i < kNumEntries; i++)
102     {
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]);
106
107         if (result.mResourcesInUse[i] > 0)
108         {
109             leak = true;
110         }
111     }
112
113     return leak;
114 }
115
116 #if CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_STATS && MEMP_STATS
117 void UpdateLwipPbufCounts(void)
118 {
119 #if LWIP_PBUF_FROM_CUSTOM_POOLS
120     size_t lwip_pool_idx = PBUF_CUSTOM_POOL_IDX_END;
121     size_t system_idx    = 0;
122
123     while (lwip_pool_idx <= PBUF_CUSTOM_POOL_IDX_START)
124     {
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);
127         lwip_pool_idx++;
128         system_idx++;
129     }
130
131 #else // LWIP_PBUF_FROM_CUSTOM_POOLS
132
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);
135
136 #endif // LWIP_PBUF_FROM_CUSTOM_POOLS
137 }
138 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_STATS && MEMP_STATS
139
140 } // namespace Stats
141 } // namespace System
142 } // namespace chip