Skip to content

Commit f1f121e

Browse files
committed
Add support for xqd_compute_runtime_get_heap_mib
See fastly/Viceroy#572
1 parent 5c41442 commit f1f121e

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

framing.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ func validateAndApplyFramingMode(headers http.Header, mode FramingHeadersMode, l
7171
return FramingHeadersModeAutomatic
7272
}
7373

74-
// Manual mode with valid headers (or no framing headers at all)
74+
if !hasContentLength && !hasTransferEncoding {
75+
if logger != nil {
76+
logger("missing Content-Length and Transfer-Encoding headers, falling back to automatic framing")
77+
}
78+
filterFramingHeaders(headers)
79+
return FramingHeadersModeAutomatic
80+
}
81+
82+
// Manual mode with valid framing headers
7583
return FramingHeadersModeManuallyFromHeaders
7684
}

framing_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,12 @@ func TestValidateAndApplyFramingMode(t *testing.T) {
274274
expectTransferEncoding: false,
275275
},
276276
{
277-
name: "manual mode - no framing headers is valid",
277+
name: "manual mode - no framing headers falls back to automatic",
278278
headers: http.Header{
279279
"Content-Type": []string{"text/plain"},
280280
},
281281
mode: FramingHeadersModeManuallyFromHeaders,
282-
expectedMode: FramingHeadersModeManuallyFromHeaders,
282+
expectedMode: FramingHeadersModeAutomatic,
283283
expectContentLength: false,
284284
expectTransferEncoding: false,
285285
},

wasmcontext.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,9 @@ func (i *Instance) link(store *wasmtime.Store, linker *wasmtime.Linker) {
768768
_ = linker.FuncWrap("fastly_compute_runtime", "get_vcpu_ms", safeWrap1(i, "get_vcpu_ms", func(vcpu_time_ms_out int32) int32 {
769769
return i.xqd_compute_runtime_get_vcpu_ms(vcpu_time_ms_out)
770770
}))
771+
_ = linker.FuncWrap("fastly_compute_runtime", "get_heap_mib", safeWrap1(i, "get_heap_mib", func(heap_mib_out int32) int32 {
772+
return i.xqd_compute_runtime_get_heap_mib(heap_mib_out)
773+
}))
771774
_ = linker.FuncWrap("fastly_compute_runtime", "get_sandbox_id", safeWrap3(i, "get_sandbox_id", func(sandbox_id_out int32, sandbox_id_max_len int32, nwritten_out int32) int32 {
772775
return i.xqd_compute_runtime_get_sandbox_id(sandbox_id_out, sandbox_id_max_len, nwritten_out)
773776
}))
@@ -1150,6 +1153,7 @@ func (i *Instance) linklegacy(store *wasmtime.Store, linker *wasmtime.Linker) {
11501153

11511154
// xqd_compute_runtime.go
11521155
_ = linker.FuncWrap("env", "xqd_compute_runtime_get_vcpu_ms", safeWrap1(i, "xqd_compute_runtime_get_vcpu_ms", func(a int32) int32 { return i.xqd_compute_runtime_get_vcpu_ms(a) }))
1156+
_ = linker.FuncWrap("env", "xqd_compute_runtime_get_heap_mib", safeWrap1(i, "xqd_compute_runtime_get_heap_mib", func(a int32) int32 { return i.xqd_compute_runtime_get_heap_mib(a) }))
11531157

11541158
// xqd_async_io.go
11551159
_ = linker.FuncWrap("env", "xqd_async_io_select", safeWrap4(i, "xqd_async_io_select", func(a int32, b int32, c int32, d int32) int32 { return i.xqd_async_io_select(a, b, c, d) }))

xqd_compute_runtime.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,30 @@ func (i *Instance) xqd_compute_runtime_get_hostname(hostname_out int32, hostname
134134
return XqdStatusOK
135135
}
136136

137+
// xqd_compute_runtime_get_heap_mib returns the current heap usage in mebibytes (2^20 bytes),
138+
// rounded up to the nearest MiB.
139+
//
140+
// Signature: (heap_mib_out: *mut u32) -> fastly_status
141+
func (i *Instance) xqd_compute_runtime_get_heap_mib(heap_mib_out int32) int32 {
142+
i.abilog.Printf("compute_runtime_get_heap_mib: heap_mib_out=%d", heap_mib_out)
143+
144+
if i.memory == nil {
145+
return XqdError
146+
}
147+
148+
const mebibyte uint64 = 1024 * 1024
149+
heapBytes := uint64(i.memory.Len())
150+
heapMib := (heapBytes + mebibyte - 1) / mebibyte
151+
if heapMib > uint64(^uint32(0)) {
152+
heapMib = uint64(^uint32(0))
153+
}
154+
155+
i.abilog.Printf("compute_runtime_get_heap_mib: returning %d MiB (%d bytes)", heapMib, heapBytes)
156+
i.memory.WriteUint32(heap_mib_out, uint32(heapMib))
157+
158+
return XqdStatusOK
159+
}
160+
137161
// xqd_compute_runtime_get_vcpu_ms returns the amount of active CPU time (in milliseconds)
138162
// that has been consumed by the WebAssembly guest during request processing.
139163
//

0 commit comments

Comments
 (0)