Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / system / SystemStats.h
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 declares the CHIP API to collect statistics
22  *  on the state of CHIP, Inet and System resources
23  */
24
25 #pragma once
26
27 // Include standard C library limit macros
28 #ifndef __STDC_LIMIT_MACROS
29 #define __STDC_LIMIT_MACROS
30 #endif
31
32 // Include configuration headers
33 #include <core/CHIPConfig.h>
34 #include <inet/InetConfig.h>
35
36 // Include dependent headers
37 #include <support/DLLUtil.h>
38
39 #if CHIP_SYSTEM_CONFIG_USE_LWIP
40 #include <lwip/mem.h>
41 #include <lwip/opt.h>
42 #include <lwip/pbuf.h>
43 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
44
45 #include <stdint.h>
46
47 namespace chip {
48 namespace System {
49 namespace Stats {
50
51 enum
52 {
53 #if CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_PBUF_FROM_CUSTOM_POOLS
54 #define LWIP_PBUF_MEMPOOL(name, num, payload, desc) kSystemLayer_Num##name,
55 #include "lwippools.h"
56 #undef LWIP_PBUF_MEMPOOL
57 #else
58     kSystemLayer_NumPacketBufs,
59 #endif
60     kSystemLayer_NumTimers,
61 #if INET_CONFIG_NUM_RAW_ENDPOINTS
62     kInetLayer_NumRawEps,
63 #endif
64 #if INET_CONFIG_NUM_TCP_ENDPOINTS
65     kInetLayer_NumTCPEps,
66 #endif
67 #if INET_CONFIG_NUM_UDP_ENDPOINTS
68     kInetLayer_NumUDPEps,
69 #endif
70 #if INET_CONFIG_NUM_DNS_RESOLVERS
71     kInetLayer_NumDNSResolvers,
72 #endif
73     kExchangeMgr_NumContexts,
74     kExchangeMgr_NumUMHandlers,
75     kExchangeMgr_NumBindings,
76     kMessageLayer_NumConnections,
77     kNumEntries
78 };
79
80 typedef int8_t count_t;
81 #define PRI_CHIP_SYS_STATS_COUNT PRId8
82 #define CHIP_SYS_STATS_COUNT_MAX INT8_MAX
83
84 extern count_t ResourcesInUse[kNumEntries];
85 extern count_t HighWatermarks[kNumEntries];
86
87 class Snapshot
88 {
89 public:
90     count_t mResourcesInUse[kNumEntries];
91     count_t mHighWatermarks[kNumEntries];
92 };
93
94 bool Difference(Snapshot & result, Snapshot & after, Snapshot & before);
95 void UpdateSnapshot(Snapshot & aSnapshot);
96 count_t * GetResourcesInUse();
97 count_t * GetHighWatermarks();
98
99 #if CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_STATS && MEMP_STATS
100 void UpdateLwipPbufCounts(void);
101 #endif
102
103 typedef const char * Label;
104 const Label * GetStrings();
105
106 } // namespace Stats
107 } // namespace System
108 } // namespace chip
109
110 #if CHIP_SYSTEM_CONFIG_PROVIDE_STATISTICS
111
112 #define SYSTEM_STATS_INCREMENT(entry)                                                                                              \
113     do                                                                                                                             \
114     {                                                                                                                              \
115         chip::System::Stats::count_t new_value = ++(chip::System::Stats::GetResourcesInUse()[entry]);                              \
116         if (chip::System::Stats::GetHighWatermarks()[entry] < new_value)                                                           \
117         {                                                                                                                          \
118             chip::System::Stats::GetHighWatermarks()[entry] = new_value;                                                           \
119         }                                                                                                                          \
120     } while (0);
121
122 #define SYSTEM_STATS_DECREMENT(entry)                                                                                              \
123     do                                                                                                                             \
124     {                                                                                                                              \
125         chip::System::Stats::GetResourcesInUse()[entry]--;                                                                         \
126     } while (0);
127
128 #define SYSTEM_STATS_DECREMENT_BY_N(entry, count)                                                                                  \
129     do                                                                                                                             \
130     {                                                                                                                              \
131         chip::System::Stats::GetResourcesInUse()[entry] -= (count);                                                                \
132     } while (0);
133
134 #define SYSTEM_STATS_SET(entry, count)                                                                                             \
135     do                                                                                                                             \
136     {                                                                                                                              \
137         chip::System::Stats::count_t new_value = chip::System::Stats::GetResourcesInUse()[entry] = (count);                        \
138         if (chip::System::Stats::GetHighWatermarks()[entry] < new_value)                                                           \
139         {                                                                                                                          \
140             chip::System::Stats::GetHighWatermarks()[entry] = new_value;                                                           \
141         }                                                                                                                          \
142     } while (0);
143
144 #define SYSTEM_STATS_RESET(entry)                                                                                                  \
145     do                                                                                                                             \
146     {                                                                                                                              \
147         chip::System::Stats::GetResourcesInUse()[entry] = 0;                                                                       \
148     } while (0);
149
150 #if CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_STATS && MEMP_STATS
151 #define SYSTEM_STATS_UPDATE_LWIP_PBUF_COUNTS()                                                                                     \
152     do                                                                                                                             \
153     {                                                                                                                              \
154         chip::System::Stats::UpdateLwipPbufCounts();                                                                               \
155     } while (0);
156 #else // CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_STATS && MEMP_STATS
157 #define SYSTEM_STATS_UPDATE_LWIP_PBUF_COUNTS()
158 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_STATS && MEMP_STATS
159
160 #else // CHIP_SYSTEM_CONFIG_PROVIDE_STATISTICS
161
162 #define SYSTEM_STATS_INCREMENT(entry)
163
164 #define SYSTEM_STATS_DECREMENT(entry)
165
166 #define SYSTEM_STATS_DECREMENT_BY_N(entry, count)
167
168 #define SYSTEM_STATS_RESET(entry)
169
170 #define SYSTEM_STATS_UPDATE_LWIP_PBUF_COUNTS()
171
172 #endif // CHIP_SYSTEM_CONFIG_PROVIDE_STATISTICS