Skip to content

Commit d9078c7

Browse files
committed
Add bindings for the pinned metrics
1 parent eec191a commit d9078c7

File tree

10 files changed

+142
-3
lines changed

10 files changed

+142
-3
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ require (
1212
github.com/davecgh/go-spew v1.1.1 // indirect
1313
github.com/kr/pretty v0.1.0 // indirect
1414
github.com/pmezard/go-difflib v1.0.0 // indirect
15+
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
16+
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
1517
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
1618
gopkg.in/yaml.v3 v3.0.1 // indirect
1719
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
1717
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
1818
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
1919
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
20+
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
21+
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
22+
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
23+
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
2024
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
2125
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
2226
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

internal/api/bindings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ struct AnalysisReport analyze_code(struct cache_t *cache,
441441

442442
struct Metrics get_metrics(struct cache_t *cache, struct UnmanagedVector *error_msg);
443443

444+
struct UnmanagedVector get_pinned_metrics(struct cache_t *cache, struct UnmanagedVector *error_msg);
445+
444446
/**
445447
* frees a cache reference
446448
*

internal/api/lib.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"golang.org/x/sys/unix"
1515

1616
"github.com/CosmWasm/wasmvm/v2/types"
17+
"github.com/vmihailenco/msgpack/v5"
1718
)
1819

1920
// Value types
@@ -190,6 +191,21 @@ func GetMetrics(cache Cache) (*types.Metrics, error) {
190191
}, nil
191192
}
192193

194+
func GetPinnedMetrics(cache Cache) (*types.PinnedMetrics, error) {
195+
errmsg := uninitializedUnmanagedVector()
196+
metrics, err := C.get_pinned_metrics(cache.ptr, &errmsg)
197+
if err != nil {
198+
return nil, errorWithMessage(err, errmsg)
199+
}
200+
201+
var pinnedMetrics types.PinnedMetrics
202+
if err := msgpack.Unmarshal(copyAndDestroyUnmanagedVector(metrics), &pinnedMetrics); err != nil {
203+
return nil, err
204+
}
205+
206+
return &pinnedMetrics, nil
207+
}
208+
193209
func Instantiate(
194210
cache Cache,
195211
checksum []byte,

libwasmvm/Cargo.lock

Lines changed: 26 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libwasmvm/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ backtraces = []
2929
cosmwasm-std = { git = "https://github.com/CosmWasm/cosmwasm.git", rev = "v2.1.0-rc.1", features = ["staking", "stargate", "iterator"] }
3030
cosmwasm-vm = { git = "https://github.com/CosmWasm/cosmwasm.git", rev = "v2.1.0-rc.1", features = ["staking", "stargate", "iterator"] }
3131
errno = "0.3.8"
32+
rmp-serde = "1.1.2"
33+
serde = { version = "1.0.103", features = ["derive"] }
3234
serde_json = "1.0.91"
3335
thiserror = "1.0.38"
3436
hex = "0.4.3"

libwasmvm/bindings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ struct AnalysisReport analyze_code(struct cache_t *cache,
441441

442442
struct Metrics get_metrics(struct cache_t *cache, struct UnmanagedVector *error_msg);
443443

444+
struct UnmanagedVector get_pinned_metrics(struct cache_t *cache, struct UnmanagedVector *error_msg);
445+
444446
/**
445447
* frees a cache reference
446448
*

libwasmvm/src/cache.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::str::from_utf8;
55

66
use cosmwasm_std::Checksum;
77
use cosmwasm_vm::{capabilities_from_csv, Cache, CacheOptions, Size};
8+
use serde::Serialize;
89

910
use crate::api::GoApi;
1011
use crate::args::{AVAILABLE_CAPABILITIES_ARG, CACHE_ARG, CHECKSUM_ARG, DATA_DIR_ARG, WASM_ARG};
@@ -415,6 +416,64 @@ fn do_get_metrics(cache: &mut Cache<GoApi, GoStorage, GoQuerier>) -> Result<Metr
415416
Ok(cache.metrics().into())
416417
}
417418

419+
#[derive(Serialize)]
420+
struct PerModuleMetrics {
421+
hits: u32,
422+
size: usize,
423+
}
424+
425+
impl From<cosmwasm_vm::PerModuleMetrics> for PerModuleMetrics {
426+
fn from(value: cosmwasm_vm::PerModuleMetrics) -> Self {
427+
Self {
428+
hits: value.hits,
429+
size: value.size,
430+
}
431+
}
432+
}
433+
434+
#[derive(Serialize)]
435+
struct PinnedMetrics {
436+
per_module: Vec<(Checksum, PerModuleMetrics)>,
437+
}
438+
439+
impl From<cosmwasm_vm::PinnedMetrics> for PinnedMetrics {
440+
fn from(value: cosmwasm_vm::PinnedMetrics) -> Self {
441+
Self {
442+
per_module: value
443+
.per_module
444+
.iter()
445+
.cloned()
446+
.map(|(checksum, metrics)| (checksum, metrics.into()))
447+
.collect(),
448+
}
449+
}
450+
}
451+
452+
#[no_mangle]
453+
pub extern "C" fn get_pinned_metrics(
454+
cache: *mut cache_t,
455+
error_msg: Option<&mut UnmanagedVector>,
456+
) -> UnmanagedVector {
457+
let r = match to_cache(cache) {
458+
Some(c) => {
459+
catch_unwind(AssertUnwindSafe(move || do_get_pinned_metrics(c))).unwrap_or_else(|err| {
460+
eprintln!("Panic in do_get_pinned_metrics: {err:?}");
461+
Err(Error::panic())
462+
})
463+
}
464+
None => Err(Error::unset_arg(CACHE_ARG)),
465+
};
466+
handle_c_error_default(r, error_msg)
467+
}
468+
469+
fn do_get_pinned_metrics(
470+
cache: &mut Cache<GoApi, GoStorage, GoQuerier>,
471+
) -> Result<UnmanagedVector, Error> {
472+
let pinned_metrics = PinnedMetrics::from(cache.pinned_metrics());
473+
let edgerunner = rmp_serde::to_vec_named(&pinned_metrics)?;
474+
Ok(UnmanagedVector::new(Some(edgerunner)))
475+
}
476+
418477
/// frees a cache reference
419478
///
420479
/// # Safety

libwasmvm/src/error/rust.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ pub enum RustError {
2222
#[cfg(feature = "backtraces")]
2323
backtrace: Backtrace,
2424
},
25+
#[error("Cannot serialize to MessagePack: {msg}")]
26+
MessagePack {
27+
msg: String,
28+
#[cfg(feature = "backtraces")]
29+
backtrace: Backtrace,
30+
},
2531
#[error("Ran out of gas")]
2632
OutOfGas {
2733
#[cfg(feature = "backtraces")]
@@ -68,6 +74,14 @@ impl RustError {
6874
}
6975
}
7076

77+
pub fn message_pack<S: ToString>(msg: S) -> Self {
78+
RustError::MessagePack {
79+
msg: msg.to_string(),
80+
#[cfg(feature = "backtraces")]
81+
backtrace: Backtrace::capture(),
82+
}
83+
}
84+
7185
pub fn panic() -> Self {
7286
RustError::Panic {
7387
#[cfg(feature = "backtraces")]
@@ -121,6 +135,12 @@ impl From<ChecksumError> for RustError {
121135
}
122136
}
123137

138+
impl From<rmp_serde::encode::Error> for RustError {
139+
fn from(source: rmp_serde::encode::Error) -> Self {
140+
RustError::message_pack(source)
141+
}
142+
}
143+
124144
impl From<std::str::Utf8Error> for RustError {
125145
fn from(source: std::str::Utf8Error) -> Self {
126146
RustError::invalid_utf8(source)

types/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ type Metrics struct {
189189
SizeMemoryCache uint64
190190
}
191191

192+
type PerModuleMetrics struct {
193+
Hits uint32
194+
Size uint64
195+
}
196+
197+
type PinnedMetrics struct {
198+
PerModule PerModuleMetrics `json:"per_module"`
199+
}
200+
192201
// Array is a wrapper around a slice that ensures that we get "[]" JSON for nil values.
193202
// When unmarshaling, we get an empty slice for "[]" and "null".
194203
//

0 commit comments

Comments
 (0)