Coverage Report

Created: 2025-05-04 06:22

/src/openweave-core/src/system/SystemStats.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *
3
 *    Copyright (c) 2016-2017 Nest Labs, Inc.
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
/**
20
 * @file
21
 *  This file implements the Weave API to collect statistics
22
 *  on the state of Weave, Inet and System resources
23
 */
24
25
// Include module header
26
#include <SystemLayer/SystemStats.h>
27
28
// Include common private header
29
#include "SystemLayerPrivate.h"
30
31
// Include local headers
32
#include <SystemLayer/SystemTimer.h>
33
34
#include <string.h>
35
36
namespace nl {
37
namespace Weave {
38
namespace System {
39
namespace Stats {
40
41
static const Label sStatsStrings[nl::Weave::System::Stats::kNumEntries] =
42
{
43
#if WEAVE_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_TUN_ENDPOINTS
61
    "InetLayer_NumTunEpsInUse",
62
#endif
63
#if INET_CONFIG_NUM_DNS_RESOLVERS
64
    "InetLayer_NumDNSResolversInUse",
65
#endif
66
    "ExchangeMgr_NumContextsInUse",
67
    "ExchangeMgr_NumUMHandlersInUse",
68
    "ExchangeMgr_NumBindings",
69
    "MessageLayer_NumConnectionsInUse",
70
#if WEAVE_CONFIG_ENABLE_SERVICE_DIRECTORY
71
    "ServiceMgr_NumRequestsInUse",
72
#endif
73
74
#if WDM_ENABLE_SUBSCRIPTION_PUBLISHER
75
    "WDM_NumTraits",
76
#endif
77
#if WDM_ENABLE_SUBSCRIPTION_CLIENT
78
    "WDM_NumSubscriptionClients",
79
#endif
80
#if WDM_ENABLE_SUBSCRIPTION_PUBLISHER
81
    "WDM_NumSubscriptionHandlers",
82
#endif
83
#if WDM_PUBLISHER_ENABLE_CUSTOM_COMMAND_HANDLER
84
    "WDM_NumCommands",
85
#endif
86
87
#if WEAVE_CONFIG_LEGACY_WDM
88
    "WDMLegacy_NumViewInUse",
89
#if WEAVE_CONFIG_WDM_ALLOW_CLIENT_SUBSCRIPTION
90
    "WDMLegacy_NumSubscribeInUse",
91
    "WDMLegacy_NumCancelInUse",
92
#endif // WEAVE_CONFIG_WDM_ALLOW_CLIENT_SUBSCRIPTION
93
    "WDMLegacy_NumUpdateInUse",
94
    "WDMLegacy_NumBindingsInUse",
95
    "WDMLegacy_NumTransactions",
96
#endif // WEAVE_CONFIG_LEGACY_WDM
97
98
};
99
100
count_t sResourcesInUse[kNumEntries];
101
count_t sHighWatermarks[kNumEntries];
102
103
const Label *GetStrings(void)
104
0
{
105
0
    return sStatsStrings;
106
0
}
107
108
count_t *GetResourcesInUse(void)
109
51.9k
{
110
51.9k
    return sResourcesInUse;
111
51.9k
}
112
113
count_t *GetHighWatermarks(void)
114
25.9k
{
115
25.9k
    return sHighWatermarks;
116
25.9k
}
117
118
void UpdateSnapshot(Snapshot &aSnapshot)
119
0
{
120
0
    memcpy(&aSnapshot.mResourcesInUse, &sResourcesInUse, sizeof(aSnapshot.mResourcesInUse));
121
0
    memcpy(&aSnapshot.mHighWatermarks, &sHighWatermarks, sizeof(aSnapshot.mHighWatermarks));
122
123
0
    nl::Weave::System::Timer::GetStatistics(aSnapshot.mResourcesInUse[kSystemLayer_NumTimers],
124
0
                                            aSnapshot.mHighWatermarks[kSystemLayer_NumTimers]);
125
126
0
    SYSTEM_STATS_UPDATE_LWIP_PBUF_COUNTS();
127
0
}
128
129
bool Difference(Snapshot &result, Snapshot &after, Snapshot &before)
130
0
{
131
0
    int i;
132
0
    bool leak = false;
133
134
0
    for (i = 0; i < kNumEntries; i++)
135
0
    {
136
0
        result.mResourcesInUse[i] = after.mResourcesInUse[i] - before.mResourcesInUse[i];
137
0
        result.mHighWatermarks[i] = after.mHighWatermarks[i] - before.mHighWatermarks[i];
138
139
0
        if (result.mResourcesInUse[i] > 0)
140
0
        {
141
0
            leak = true;
142
0
        }
143
0
    }
144
145
0
    return leak;
146
0
}
147
148
#if WEAVE_SYSTEM_CONFIG_USE_LWIP && LWIP_STATS && MEMP_STATS
149
void UpdateLwipPbufCounts(void)
150
{
151
#if LWIP_PBUF_FROM_CUSTOM_POOLS
152
    size_t lwip_pool_idx = PBUF_CUSTOM_POOL_IDX_END;
153
    size_t system_idx = 0;
154
155
    while (lwip_pool_idx <= PBUF_CUSTOM_POOL_IDX_START)
156
    {
157
        nl::Weave::System::Stats::GetResourcesInUse()[system_idx] = MEMP_STATS_GET(used, lwip_pool_idx);
158
        nl::Weave::System::Stats::GetHighWatermarks()[system_idx] = MEMP_STATS_GET(max, lwip_pool_idx);
159
        lwip_pool_idx++;
160
        system_idx++;
161
    }
162
163
#else // LWIP_PBUF_FROM_CUSTOM_POOLS
164
165
    nl::Weave::System::Stats::GetResourcesInUse()[kSystemLayer_NumPacketBufs] = MEMP_STATS_GET(used, MEMP_PBUF_POOL);
166
    nl::Weave::System::Stats::GetHighWatermarks()[kSystemLayer_NumPacketBufs] = MEMP_STATS_GET(max, MEMP_PBUF_POOL);
167
168
#endif // LWIP_PBUF_FROM_CUSTOM_POOLS
169
}
170
#endif // WEAVE_SYSTEM_CONFIG_USE_LWIP && LWIP_STATS && MEMP_STATS
171
172
173
} // namespace Stats
174
} // namespace System
175
} // namespace Weave
176
} // namespace nl