Skip to content

Commit b29521d

Browse files
committed
shrink net module footprint
1 parent 3b9388a commit b29521d

5 files changed

Lines changed: 152 additions & 50 deletions

File tree

src/ZeroKernelConfig.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,36 @@
301301
#define ZEROKERNEL_ENABLE_CAPABILITIES ZEROKERNEL_DEFAULT_ENABLE_CAPABILITIES
302302
#endif
303303

304+
#ifndef ZEROKERNEL_ENABLE_NET_EXTENDED_METRICS
305+
#if defined(ZEROKERNEL_PROFILE_EXTENDED) || defined(ZEROKERNEL_PROFILE_DIAGNOSTIC)
306+
#define ZEROKERNEL_ENABLE_NET_EXTENDED_METRICS 1
307+
#else
308+
#define ZEROKERNEL_ENABLE_NET_EXTENDED_METRICS 0
309+
#endif
310+
#endif
311+
312+
#ifndef ZEROKERNEL_HTTP_PUMP_QUEUE_CAPACITY
313+
#if defined(ZEROKERNEL_PROFILE_TINY) || defined(ZEROKERNEL_PROFILE_MINIMAL_RUNTIME) || \
314+
defined(ZEROKERNEL_PROFILE_POWER_SAVE)
315+
#define ZEROKERNEL_HTTP_PUMP_QUEUE_CAPACITY 2
316+
#elif defined(ZEROKERNEL_PROFILE_EXTENDED) || defined(ZEROKERNEL_PROFILE_DIAGNOSTIC)
317+
#define ZEROKERNEL_HTTP_PUMP_QUEUE_CAPACITY 4
318+
#else
319+
#define ZEROKERNEL_HTTP_PUMP_QUEUE_CAPACITY 3
320+
#endif
321+
#endif
322+
323+
#ifndef ZEROKERNEL_MQTT_PUMP_QUEUE_CAPACITY
324+
#if defined(ZEROKERNEL_PROFILE_TINY) || defined(ZEROKERNEL_PROFILE_MINIMAL_RUNTIME) || \
325+
defined(ZEROKERNEL_PROFILE_POWER_SAVE)
326+
#define ZEROKERNEL_MQTT_PUMP_QUEUE_CAPACITY 2
327+
#elif defined(ZEROKERNEL_PROFILE_EXTENDED) || defined(ZEROKERNEL_PROFILE_DIAGNOSTIC)
328+
#define ZEROKERNEL_MQTT_PUMP_QUEUE_CAPACITY 6
329+
#else
330+
#define ZEROKERNEL_MQTT_PUMP_QUEUE_CAPACITY 4
331+
#endif
332+
#endif
333+
304334
#define ZEROKERNEL_QUEUE_DROP_NEWEST 0
305335
#define ZEROKERNEL_QUEUE_DROP_OLDEST 1
306336

src/modules/net/ZeroHttpPump.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class ZeroHttpPump {
6363
emitCompletionEvents(true) {}
6464
};
6565

66-
static const uint8_t kQueueCapacity = 4;
66+
static const uint8_t kQueueCapacity = ZEROKERNEL_HTTP_PUMP_QUEUE_CAPACITY;
6767

6868
ZeroHttpPump()
6969
: kernel_(NULL),

src/modules/net/ZeroMqttPump.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ZeroMqttPump {
4848
stateTopicKey(0) {}
4949
};
5050

51-
static const uint8_t kQueueCapacity = 6;
51+
static const uint8_t kQueueCapacity = ZEROKERNEL_MQTT_PUMP_QUEUE_CAPACITY;
5252

5353
ZeroMqttPump()
5454
: kernel_(NULL),

src/modules/net/ZeroTransportMetrics.h

Lines changed: 99 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,97 +33,152 @@ class ZeroTransportMetrics {
3333
}
3434

3535
void reset() {
36-
snapshot_.connectAttempts = 0;
37-
snapshot_.connectSuccesses = 0;
38-
snapshot_.connectFailures = 0;
39-
snapshot_.sendAttempts = 0;
40-
snapshot_.sendSuccesses = 0;
41-
snapshot_.sendFailures = 0;
42-
snapshot_.loopCalls = 0;
43-
snapshot_.queueDrops = 0;
44-
snapshot_.backoffSchedules = 0;
45-
snapshot_.consecutiveFailures = 0;
46-
snapshot_.lastConnectLatencyMs = 0;
47-
snapshot_.worstConnectLatencyMs = 0;
48-
snapshot_.lastSendLatencyMs = 0;
49-
snapshot_.worstSendLatencyMs = 0;
50-
snapshot_.lastQueueDwellMs = 0;
51-
snapshot_.worstQueueDwellMs = 0;
52-
snapshot_.maxQueueDepth = 0;
36+
connectAttempts_ = 0;
37+
connectSuccesses_ = 0;
38+
connectFailures_ = 0;
39+
sendAttempts_ = 0;
40+
sendSuccesses_ = 0;
41+
sendFailures_ = 0;
42+
loopCalls_ = 0;
43+
queueDrops_ = 0;
44+
backoffSchedules_ = 0;
45+
consecutiveFailures_ = 0;
46+
maxQueueDepth_ = 0;
47+
48+
#if ZEROKERNEL_ENABLE_NET_EXTENDED_METRICS
49+
lastConnectLatencyMs_ = 0;
50+
worstConnectLatencyMs_ = 0;
51+
lastSendLatencyMs_ = 0;
52+
worstSendLatencyMs_ = 0;
53+
lastQueueDwellMs_ = 0;
54+
worstQueueDwellMs_ = 0;
55+
#endif
5356
}
5457

5558
void recordConnectAttempt() {
56-
++snapshot_.connectAttempts;
59+
++connectAttempts_;
5760
}
5861

5962
void recordConnectResult(bool success, unsigned long latencyMs) {
60-
snapshot_.lastConnectLatencyMs = latencyMs;
61-
if (latencyMs > snapshot_.worstConnectLatencyMs) {
62-
snapshot_.worstConnectLatencyMs = latencyMs;
63+
#if ZEROKERNEL_ENABLE_NET_EXTENDED_METRICS
64+
lastConnectLatencyMs_ = latencyMs;
65+
if (latencyMs > worstConnectLatencyMs_) {
66+
worstConnectLatencyMs_ = latencyMs;
6367
}
68+
#endif
6469

6570
if (success) {
66-
++snapshot_.connectSuccesses;
67-
snapshot_.consecutiveFailures = 0;
71+
++connectSuccesses_;
72+
consecutiveFailures_ = 0;
6873
return;
6974
}
7075

71-
++snapshot_.connectFailures;
72-
++snapshot_.consecutiveFailures;
76+
++connectFailures_;
77+
++consecutiveFailures_;
7378
}
7479

7580
void recordSendQueued(unsigned long queueDepth) {
76-
if (queueDepth > snapshot_.maxQueueDepth) {
77-
snapshot_.maxQueueDepth = queueDepth;
81+
if (queueDepth > maxQueueDepth_) {
82+
maxQueueDepth_ = queueDepth;
7883
}
7984
}
8085

8186
void recordSendAttempt() {
82-
++snapshot_.sendAttempts;
87+
++sendAttempts_;
8388
}
8489

8590
void recordSendResult(bool success,
8691
unsigned long latencyMs,
8792
unsigned long queueDwellMs) {
88-
snapshot_.lastSendLatencyMs = latencyMs;
89-
snapshot_.lastQueueDwellMs = queueDwellMs;
93+
#if ZEROKERNEL_ENABLE_NET_EXTENDED_METRICS
94+
lastSendLatencyMs_ = latencyMs;
95+
lastQueueDwellMs_ = queueDwellMs;
9096

91-
if (latencyMs > snapshot_.worstSendLatencyMs) {
92-
snapshot_.worstSendLatencyMs = latencyMs;
97+
if (latencyMs > worstSendLatencyMs_) {
98+
worstSendLatencyMs_ = latencyMs;
9399
}
94100

95-
if (queueDwellMs > snapshot_.worstQueueDwellMs) {
96-
snapshot_.worstQueueDwellMs = queueDwellMs;
101+
if (queueDwellMs > worstQueueDwellMs_) {
102+
worstQueueDwellMs_ = queueDwellMs;
97103
}
104+
#endif
98105

99106
if (success) {
100-
++snapshot_.sendSuccesses;
101-
snapshot_.consecutiveFailures = 0;
107+
++sendSuccesses_;
108+
consecutiveFailures_ = 0;
102109
return;
103110
}
104111

105-
++snapshot_.sendFailures;
106-
++snapshot_.consecutiveFailures;
112+
++sendFailures_;
113+
++consecutiveFailures_;
107114
}
108115

109116
void recordLoopCall() {
110-
++snapshot_.loopCalls;
117+
++loopCalls_;
111118
}
112119

113120
void recordQueueDrop() {
114-
++snapshot_.queueDrops;
121+
++queueDrops_;
115122
}
116123

117124
void recordBackoffSchedule() {
118-
++snapshot_.backoffSchedules;
125+
++backoffSchedules_;
119126
}
120127

121-
const Snapshot& snapshot() const {
122-
return snapshot_;
128+
Snapshot snapshot() const {
129+
Snapshot snapshot;
130+
snapshot.connectAttempts = connectAttempts_;
131+
snapshot.connectSuccesses = connectSuccesses_;
132+
snapshot.connectFailures = connectFailures_;
133+
snapshot.sendAttempts = sendAttempts_;
134+
snapshot.sendSuccesses = sendSuccesses_;
135+
snapshot.sendFailures = sendFailures_;
136+
snapshot.loopCalls = loopCalls_;
137+
snapshot.queueDrops = queueDrops_;
138+
snapshot.backoffSchedules = backoffSchedules_;
139+
snapshot.consecutiveFailures = consecutiveFailures_;
140+
snapshot.maxQueueDepth = maxQueueDepth_;
141+
142+
#if ZEROKERNEL_ENABLE_NET_EXTENDED_METRICS
143+
snapshot.lastConnectLatencyMs = lastConnectLatencyMs_;
144+
snapshot.worstConnectLatencyMs = worstConnectLatencyMs_;
145+
snapshot.lastSendLatencyMs = lastSendLatencyMs_;
146+
snapshot.worstSendLatencyMs = worstSendLatencyMs_;
147+
snapshot.lastQueueDwellMs = lastQueueDwellMs_;
148+
snapshot.worstQueueDwellMs = worstQueueDwellMs_;
149+
#else
150+
snapshot.lastConnectLatencyMs = 0;
151+
snapshot.worstConnectLatencyMs = 0;
152+
snapshot.lastSendLatencyMs = 0;
153+
snapshot.worstSendLatencyMs = 0;
154+
snapshot.lastQueueDwellMs = 0;
155+
snapshot.worstQueueDwellMs = 0;
156+
#endif
157+
158+
return snapshot;
123159
}
124160

125161
private:
126-
Snapshot snapshot_;
162+
unsigned long connectAttempts_;
163+
unsigned long connectSuccesses_;
164+
unsigned long connectFailures_;
165+
unsigned long sendAttempts_;
166+
unsigned long sendSuccesses_;
167+
unsigned long sendFailures_;
168+
unsigned long loopCalls_;
169+
unsigned long queueDrops_;
170+
unsigned long backoffSchedules_;
171+
unsigned long consecutiveFailures_;
172+
unsigned long maxQueueDepth_;
173+
174+
#if ZEROKERNEL_ENABLE_NET_EXTENDED_METRICS
175+
unsigned long lastConnectLatencyMs_;
176+
unsigned long worstConnectLatencyMs_;
177+
unsigned long lastSendLatencyMs_;
178+
unsigned long worstSendLatencyMs_;
179+
unsigned long lastQueueDwellMs_;
180+
unsigned long worstQueueDwellMs_;
181+
#endif
127182
};
128183

129184
} // namespace net

tests/desktop/KernelTests.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,11 @@ int testTransportMetricsModule() {
883883
expectTrue(snapshot.queueDrops == 1, "transport metrics count queue drops");
884884
expectTrue(snapshot.backoffSchedules == 1, "transport metrics count backoff schedules");
885885
expectTrue(snapshot.loopCalls == 1, "transport metrics count loop calls");
886+
#if ZEROKERNEL_ENABLE_NET_EXTENDED_METRICS
886887
expectTrue(snapshot.worstQueueDwellMs == 11, "transport metrics track queue dwell");
888+
#else
889+
expectTrue(snapshot.worstQueueDwellMs == 0, "transport metrics disable queue dwell in lean mode");
890+
#endif
887891
expectTrue(snapshot.maxQueueDepth == 2, "transport metrics track queue depth");
888892
return g_failures;
889893
}
@@ -979,12 +983,19 @@ int testHttpPumpModule() {
979983
expectTrue(g_httpCompletionEvents == 1, "http pump publishes completion event");
980984
expectTrue(g_httpLastCompletion, "http pump publishes successful completion");
981985

982-
for (int index = 0; index < 5; ++index) {
986+
const int httpEnqueueCount = 5;
987+
for (int index = 0; index < httpEnqueueCount; ++index) {
983988
expectTrue(pump.enqueue(request), "http pump accepts bounded queue item");
984989
}
985990
expectTrue(pump.queuedCount() == zerokernel::modules::net::ZeroHttpPump::kQueueCapacity,
986991
"http pump queue remains bounded");
987-
expectTrue(pump.metrics().snapshot().queueDrops == 1, "http pump drops oldest on overflow");
992+
const unsigned long expectedHttpDrops =
993+
httpEnqueueCount > zerokernel::modules::net::ZeroHttpPump::kQueueCapacity
994+
? static_cast<unsigned long>(httpEnqueueCount -
995+
zerokernel::modules::net::ZeroHttpPump::kQueueCapacity)
996+
: 0UL;
997+
expectTrue(pump.metrics().snapshot().queueDrops == expectedHttpDrops,
998+
"http pump drops oldest on overflow");
988999

9891000
expectTrue(isolatedKernel.unsubscribeTypedFast(completionKey, onHttpCompletionEvent),
9901001
"unsubscribe http completion");
@@ -1052,7 +1063,8 @@ int testMqttPumpModule() {
10521063
expectTrue(pump.queuedCount() == 0, "mqtt pump drains queue after retry success");
10531064
expectTrue(g_mqttLoopCalls >= 2, "mqtt pump calls loop while connected");
10541065

1055-
for (int index = 0; index < 7; ++index) {
1066+
const int mqttEnqueueCount = 7;
1067+
for (int index = 0; index < mqttEnqueueCount; ++index) {
10561068
expectTrue(
10571069
pump.enqueue(zerokernel::Kernel::makeTopicKey("mqtt.out"),
10581070
zerokernel::Kernel::EventValue::fromLong(index)),
@@ -1065,7 +1077,12 @@ int testMqttPumpModule() {
10651077
"mqtt pump queue remains bounded");
10661078
expectTrue(mqttMetrics.sendFailures == 1, "mqtt pump records send failure");
10671079
expectTrue(mqttMetrics.sendSuccesses == 1, "mqtt pump records send success");
1068-
expectTrue(mqttMetrics.queueDrops == 1, "mqtt pump records queue drop");
1080+
const unsigned long expectedMqttDrops =
1081+
mqttEnqueueCount > zerokernel::modules::net::ZeroMqttPump::kQueueCapacity
1082+
? static_cast<unsigned long>(mqttEnqueueCount -
1083+
zerokernel::modules::net::ZeroMqttPump::kQueueCapacity)
1084+
: 0UL;
1085+
expectTrue(mqttMetrics.queueDrops == expectedMqttDrops, "mqtt pump records queue drop");
10691086
expectTrue(mqttMetrics.backoffSchedules == 1, "mqtt pump records publish backoff");
10701087

10711088
expectTrue(isolatedKernel.unsubscribeTypedFast(stateKey, onMqttStateEvent),

0 commit comments

Comments
 (0)