Skip to content

Commit 45d3e7f

Browse files
committed
feat: add PLUGIN_VM_MEMORY property to query Wasm VM memory usage
Change-Id: I787f124bb7b1b35d7b9fecfa8fe6261fd52f413f
1 parent d65cb3e commit 45d3e7f

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

source/extensions/common/wasm/context.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,17 @@ WasmResult serializeValue(Filters::Common::Expr::CelValue value, std::string* re
456456
return WasmResult::SerializationFailure;
457457
}
458458

459+
#if defined(HIGRESS)
460+
#define PROPERTY_TOKENS(_f) \
461+
_f(NODE) _f(LISTENER_DIRECTION) _f(LISTENER_METADATA) _f(CLUSTER_NAME) _f(CLUSTER_METADATA) \
462+
_f(ROUTE_NAME) _f(ROUTE_METADATA) _f(PLUGIN_NAME) _f(UPSTREAM_HOST_METADATA) \
463+
_f(PLUGIN_ROOT_ID) _f(PLUGIN_VM_ID) _f(PLUGIN_VM_MEMORY) _f(CONNECTION_ID)
464+
#else
459465
#define PROPERTY_TOKENS(_f) \
460466
_f(NODE) _f(LISTENER_DIRECTION) _f(LISTENER_METADATA) _f(CLUSTER_NAME) _f(CLUSTER_METADATA) \
461467
_f(ROUTE_NAME) _f(ROUTE_METADATA) _f(PLUGIN_NAME) _f(UPSTREAM_HOST_METADATA) \
462468
_f(PLUGIN_ROOT_ID) _f(PLUGIN_VM_ID) _f(CONNECTION_ID)
469+
#endif
463470

464471
static inline std::string downCase(std::string s) {
465472
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); });
@@ -614,6 +621,13 @@ Context::findValue(absl::string_view name, Protobuf::Arena* arena, bool last) co
614621
return CelValue::CreateStringView(toAbslStringView(root_id()));
615622
case PropertyToken::PLUGIN_VM_ID:
616623
return CelValue::CreateStringView(toAbslStringView(wasm()->vm_id()));
624+
#if defined(HIGRESS)
625+
case PropertyToken::PLUGIN_VM_MEMORY:
626+
if (wasm() && wasm()->wasm_vm()) {
627+
return CelValue::CreateUint64(wasm()->wasm_vm()->getMemorySize());
628+
}
629+
break;
630+
#endif
617631
}
618632
return {};
619633
}

test/extensions/filters/http/wasm/test_data/test_cpp.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,21 @@ FilterHeadersStatus TestContext::onRequestHeaders(uint32_t, bool) {
286286
logError("get route name failed");
287287
}
288288
return FilterHeadersStatus::Continue;
289+
} else if (test == "GetVMMemorySize") {
290+
std::string value;
291+
if (getValue({"plugin_vm_memory"}, &value)) {
292+
// The value is stored as binary uint64_t, convert to string for logging
293+
if (value.size() == sizeof(uint64_t)) {
294+
uint64_t memory_size;
295+
memcpy(&memory_size, value.data(), sizeof(uint64_t));
296+
logInfo("vm memory size is " + std::to_string(memory_size));
297+
} else {
298+
logError("invalid memory size format");
299+
}
300+
} else {
301+
logError("get vm memory size failed");
302+
}
303+
return FilterHeadersStatus::Continue;
289304
} else if (test == "CrashRecover") {
290305
if (!getRequestHeader("crash")->toString().empty()) {
291306
abort();

test/extensions/filters/http/wasm/wasm_filter_test.cc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,21 @@ TEST_P(WasmHttpFilterTest, GetRouteName) {
18501850
EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter().decodeHeaders(request_headers, false));
18511851
filter().onDestroy();
18521852
}
1853+
TEST_P(WasmHttpFilterTest, GetVMMemorySize) {
1854+
auto runtime = std::get<0>(GetParam());
1855+
if (runtime == "null") {
1856+
return;
1857+
}
1858+
if (std::get<1>(GetParam()) != "cpp") {
1859+
return;
1860+
}
1861+
setupTest("", "GetVMMemorySize");
1862+
setupFilter();
1863+
EXPECT_CALL(filter(), log_(spdlog::level::info, testing::StartsWith("vm memory size is ")));
1864+
Http::TestRequestHeaderMapImpl request_headers{};
1865+
EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter().decodeHeaders(request_headers, false));
1866+
filter().onDestroy();
1867+
}
18531868
TEST_P(WasmHttpFilterTest, RecoverFromCrash) {
18541869
auto runtime = std::get<0>(GetParam());
18551870
if (runtime == "null") {
@@ -1980,7 +1995,7 @@ TEST_P(WasmHttpFilterTest, ProactiveRebuild) {
19801995
request_headers = Http::TestRequestHeaderMapImpl{{"rebuild", "true"}};
19811996
EXPECT_CALL(filter(), log_(spdlog::level::info, Eq("Setting rebuild flag")));
19821997
EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter().decodeHeaders(request_headers, false));
1983-
EXPECT_EQ(0U, rebuild_total.value()); // No rebuild yet, just set the flag
1998+
EXPECT_EQ(0U, rebuild_total.value()); // No rebuild yet, just set the flag
19841999
EXPECT_EQ(0U, recover_total.value());
19852000

19862001
// Now trigger the actual rebuild using doRebuild()
@@ -1998,7 +2013,7 @@ TEST_P(WasmHttpFilterTest, ProactiveRebuild) {
19982013
request_headers = Http::TestRequestHeaderMapImpl{{"rebuild", "true"}};
19992014
EXPECT_CALL(filter(), log_(spdlog::level::info, Eq("Setting rebuild flag")));
20002015
EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter().decodeHeaders(request_headers, false));
2001-
EXPECT_EQ(1U, rebuild_total.value()); // Still 1, just set the flag again
2016+
EXPECT_EQ(1U, rebuild_total.value()); // Still 1, just set the flag again
20022017
EXPECT_EQ(0U, recover_total.value());
20032018

20042019
// Trigger second rebuild using doRebuild()

0 commit comments

Comments
 (0)