-
Notifications
You must be signed in to change notification settings - Fork 103
/
cache.rs
740 lines (667 loc) · 22.8 KB
/
cache.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
use std::collections::HashSet;
use std::convert::TryInto;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::str::from_utf8;
use cosmwasm_vm::{features_from_csv, Cache, CacheOptions, Checksum, Size};
use crate::api::GoApi;
use crate::args::{CACHE_ARG, CHECKSUM_ARG, DATA_DIR_ARG, FEATURES_ARG, WASM_ARG};
use crate::error::{handle_c_error_binary, handle_c_error_default, handle_c_error_ptr, Error};
use crate::memory::{ByteSliceView, UnmanagedVector};
use crate::querier::GoQuerier;
use crate::storage::GoStorage;
#[repr(C)]
pub struct cache_t {}
pub fn to_cache(ptr: *mut cache_t) -> Option<&'static mut Cache<GoApi, GoStorage, GoQuerier>> {
if ptr.is_null() {
None
} else {
let c = unsafe { &mut *(ptr as *mut Cache<GoApi, GoStorage, GoQuerier>) };
Some(c)
}
}
#[no_mangle]
pub extern "C" fn init_cache(
data_dir: ByteSliceView,
supported_features: ByteSliceView,
cache_size: u32, // in MiB
instance_memory_limit: u32, // in MiB
error_msg: Option<&mut UnmanagedVector>,
) -> *mut cache_t {
let r = catch_unwind(|| {
do_init_cache(
data_dir,
supported_features,
cache_size,
instance_memory_limit,
)
})
.unwrap_or_else(|_| Err(Error::panic()));
handle_c_error_ptr(r, error_msg) as *mut cache_t
}
fn do_init_cache(
data_dir: ByteSliceView,
supported_features: ByteSliceView,
cache_size: u32, // in MiB
instance_memory_limit: u32, // in MiB
) -> Result<*mut Cache<GoApi, GoStorage, GoQuerier>, Error> {
let dir = data_dir
.read()
.ok_or_else(|| Error::unset_arg(DATA_DIR_ARG))?;
let dir_str = String::from_utf8(dir.to_vec())?;
// parse the supported features
let features_bin = supported_features
.read()
.ok_or_else(|| Error::unset_arg(FEATURES_ARG))?;
let features_str = from_utf8(features_bin)?;
let features = features_from_csv(features_str);
let memory_cache_size = Size::mebi(
cache_size
.try_into()
.expect("Cannot convert u32 to usize. What kind of system is this?"),
);
let instance_memory_limit = Size::mebi(
instance_memory_limit
.try_into()
.expect("Cannot convert u32 to usize. What kind of system is this?"),
);
let options = CacheOptions {
base_dir: dir_str.into(),
supported_features: features,
memory_cache_size,
instance_memory_limit,
};
let cache = unsafe { Cache::new(options) }?;
let out = Box::new(cache);
Ok(Box::into_raw(out))
}
#[no_mangle]
pub extern "C" fn save_wasm(
cache: *mut cache_t,
wasm: ByteSliceView,
error_msg: Option<&mut UnmanagedVector>,
) -> UnmanagedVector {
let r = match to_cache(cache) {
Some(c) => catch_unwind(AssertUnwindSafe(move || do_save_wasm(c, wasm)))
.unwrap_or_else(|_| Err(Error::panic())),
None => Err(Error::unset_arg(CACHE_ARG)),
};
let checksum = handle_c_error_binary(r, error_msg);
UnmanagedVector::new(Some(checksum))
}
fn do_save_wasm(
cache: &mut Cache<GoApi, GoStorage, GoQuerier>,
wasm: ByteSliceView,
) -> Result<Checksum, Error> {
let wasm = wasm.read().ok_or_else(|| Error::unset_arg(WASM_ARG))?;
let checksum = cache.save_wasm(wasm)?;
Ok(checksum)
}
#[no_mangle]
pub extern "C" fn load_wasm(
cache: *mut cache_t,
checksum: ByteSliceView,
error_msg: Option<&mut UnmanagedVector>,
) -> UnmanagedVector {
let r = match to_cache(cache) {
Some(c) => catch_unwind(AssertUnwindSafe(move || do_load_wasm(c, checksum)))
.unwrap_or_else(|_| Err(Error::panic())),
None => Err(Error::unset_arg(CACHE_ARG)),
};
let data = handle_c_error_binary(r, error_msg);
UnmanagedVector::new(Some(data))
}
fn do_load_wasm(
cache: &mut Cache<GoApi, GoStorage, GoQuerier>,
checksum: ByteSliceView,
) -> Result<Vec<u8>, Error> {
let checksum: Checksum = checksum
.read()
.ok_or_else(|| Error::unset_arg(CHECKSUM_ARG))?
.try_into()?;
let wasm = cache.load_wasm(&checksum)?;
Ok(wasm)
}
#[no_mangle]
pub extern "C" fn pin(
cache: *mut cache_t,
checksum: ByteSliceView,
error_msg: Option<&mut UnmanagedVector>,
) {
let r = match to_cache(cache) {
Some(c) => catch_unwind(AssertUnwindSafe(move || do_pin(c, checksum)))
.unwrap_or_else(|_| Err(Error::panic())),
None => Err(Error::unset_arg(CACHE_ARG)),
};
handle_c_error_default(r, error_msg)
}
fn do_pin(
cache: &mut Cache<GoApi, GoStorage, GoQuerier>,
checksum: ByteSliceView,
) -> Result<(), Error> {
let checksum: Checksum = checksum
.read()
.ok_or_else(|| Error::unset_arg(CHECKSUM_ARG))?
.try_into()?;
cache.pin(&checksum)?;
Ok(())
}
#[no_mangle]
pub extern "C" fn unpin(
cache: *mut cache_t,
checksum: ByteSliceView,
error_msg: Option<&mut UnmanagedVector>,
) {
let r = match to_cache(cache) {
Some(c) => catch_unwind(AssertUnwindSafe(move || do_unpin(c, checksum)))
.unwrap_or_else(|_| Err(Error::panic())),
None => Err(Error::unset_arg(CACHE_ARG)),
};
handle_c_error_default(r, error_msg)
}
fn do_unpin(
cache: &mut Cache<GoApi, GoStorage, GoQuerier>,
checksum: ByteSliceView,
) -> Result<(), Error> {
let checksum: Checksum = checksum
.read()
.ok_or_else(|| Error::unset_arg(CHECKSUM_ARG))?
.try_into()?;
cache.unpin(&checksum)?;
Ok(())
}
/// The result type of the FFI function analyze_code.
///
/// Please note that the unmanaged vector in `required_features`
/// has to be destroyed exactly once. When calling `analyze_code`
/// from Go this is done via `C.destroy_unmanaged_vector`.
#[repr(C)]
#[derive(Copy, Clone, Default, Debug, PartialEq)]
pub struct AnalysisReport {
pub has_ibc_entry_points: bool,
/// An UTF-8 encoded comma separated list of reqired features.
/// This is never None/nil.
pub required_features: UnmanagedVector,
}
impl From<cosmwasm_vm::AnalysisReport> for AnalysisReport {
fn from(report: cosmwasm_vm::AnalysisReport) -> Self {
let cosmwasm_vm::AnalysisReport {
has_ibc_entry_points,
required_features,
} = report;
let required_features_utf8 = set_to_csv(required_features).into_bytes();
AnalysisReport {
has_ibc_entry_points,
required_features: UnmanagedVector::new(Some(required_features_utf8)),
}
}
}
fn set_to_csv(set: HashSet<String>) -> String {
let mut list: Vec<String> = set.into_iter().collect();
list.sort_unstable();
list.join(",")
}
#[no_mangle]
pub extern "C" fn analyze_code(
cache: *mut cache_t,
checksum: ByteSliceView,
error_msg: Option<&mut UnmanagedVector>,
) -> AnalysisReport {
let r = match to_cache(cache) {
Some(c) => catch_unwind(AssertUnwindSafe(move || do_analyze_code(c, checksum)))
.unwrap_or_else(|_| Err(Error::panic())),
None => Err(Error::unset_arg(CACHE_ARG)),
};
handle_c_error_default(r, error_msg)
}
fn do_analyze_code(
cache: &mut Cache<GoApi, GoStorage, GoQuerier>,
checksum: ByteSliceView,
) -> Result<AnalysisReport, Error> {
let checksum: Checksum = checksum
.read()
.ok_or_else(|| Error::unset_arg(CHECKSUM_ARG))?
.try_into()?;
let report = cache.analyze(&checksum)?;
Ok(report.into())
}
#[repr(C)]
#[derive(Copy, Clone, Default, Debug, PartialEq)]
pub struct Metrics {
pub hits_pinned_memory_cache: u32,
pub hits_memory_cache: u32,
pub hits_fs_cache: u32,
pub misses: u32,
pub elements_pinned_memory_cache: u64,
pub elements_memory_cache: u64,
pub size_pinned_memory_cache: u64,
pub size_memory_cache: u64,
}
impl From<cosmwasm_vm::Metrics> for Metrics {
fn from(report: cosmwasm_vm::Metrics) -> Self {
let cosmwasm_vm::Metrics {
stats:
cosmwasm_vm::Stats {
hits_pinned_memory_cache,
hits_memory_cache,
hits_fs_cache,
misses,
},
elements_pinned_memory_cache,
elements_memory_cache,
size_pinned_memory_cache,
size_memory_cache,
} = report;
Metrics {
hits_pinned_memory_cache,
hits_memory_cache,
hits_fs_cache,
misses,
elements_pinned_memory_cache: elements_pinned_memory_cache
.try_into()
.expect("usize is larger than 64 bit? Really?"),
elements_memory_cache: elements_memory_cache
.try_into()
.expect("usize is larger than 64 bit? Really?"),
size_pinned_memory_cache: size_pinned_memory_cache
.try_into()
.expect("usize is larger than 64 bit? Really?"),
size_memory_cache: size_memory_cache
.try_into()
.expect("usize is larger than 64 bit? Really?"),
}
}
}
#[no_mangle]
pub extern "C" fn get_metrics(
cache: *mut cache_t,
error_msg: Option<&mut UnmanagedVector>,
) -> Metrics {
let r = match to_cache(cache) {
Some(c) => catch_unwind(AssertUnwindSafe(move || do_get_metrics(c)))
.unwrap_or_else(|_| Err(Error::panic())),
None => Err(Error::unset_arg(CACHE_ARG)),
};
handle_c_error_default(r, error_msg)
}
#[allow(clippy::unnecessary_wraps)] // Keep unused Result for consistent boilerplate for all fn do_*
fn do_get_metrics(cache: &mut Cache<GoApi, GoStorage, GoQuerier>) -> Result<Metrics, Error> {
Ok(cache.metrics().into())
}
/// frees a cache reference
///
/// # Safety
///
/// This must be called exactly once for any `*cache_t` returned by `init_cache`
/// and cannot be called on any other pointer.
#[no_mangle]
pub extern "C" fn release_cache(cache: *mut cache_t) {
if !cache.is_null() {
// this will free cache when it goes out of scope
let _ = unsafe { Box::from_raw(cache as *mut Cache<GoApi, GoStorage, GoQuerier>) };
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::iter::FromIterator;
use tempfile::TempDir;
static HACKATOM: &[u8] = include_bytes!("../../api/testdata/hackatom.wasm");
static IBC_REFLECT: &[u8] = include_bytes!("../../api/testdata/ibc_reflect.wasm");
#[test]
fn init_cache_and_release_cache_work() {
let dir: String = TempDir::new().unwrap().path().to_str().unwrap().to_owned();
let features = b"staking";
let mut error_msg = UnmanagedVector::default();
let cache_ptr = init_cache(
ByteSliceView::new(dir.as_bytes()),
ByteSliceView::new(features),
512,
32,
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
release_cache(cache_ptr);
}
#[test]
fn init_cache_writes_error() {
let dir: String = String::from("broken\0dir"); // null bytes are valid UTF8 but not allowed in FS paths
let features = b"staking";
let mut error_msg = UnmanagedVector::default();
let cache_ptr = init_cache(
ByteSliceView::new(dir.as_bytes()),
ByteSliceView::new(features),
512,
32,
Some(&mut error_msg),
);
assert!(cache_ptr.is_null());
assert_eq!(error_msg.is_some(), true);
let msg = String::from_utf8(error_msg.consume().unwrap()).unwrap();
assert_eq!(msg, "Error calling the VM: Cache error: Error creating directory broken\u{0}dir/state: data provided contains a nul byte");
}
#[test]
fn save_wasm_works() {
let dir: String = TempDir::new().unwrap().path().to_str().unwrap().to_owned();
let features = b"staking";
let mut error_msg = UnmanagedVector::default();
let cache_ptr = init_cache(
ByteSliceView::new(dir.as_bytes()),
ByteSliceView::new(features),
512,
32,
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
let mut error_msg = UnmanagedVector::default();
save_wasm(
cache_ptr,
ByteSliceView::new(HACKATOM),
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
release_cache(cache_ptr);
}
#[test]
fn load_wasm_works() {
let dir: String = TempDir::new().unwrap().path().to_str().unwrap().to_owned();
let features = b"staking";
let mut error_msg = UnmanagedVector::default();
let cache_ptr = init_cache(
ByteSliceView::new(dir.as_bytes()),
ByteSliceView::new(features),
512,
32,
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
let mut error_msg = UnmanagedVector::default();
let checksum = save_wasm(
cache_ptr,
ByteSliceView::new(HACKATOM),
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
let checksum = checksum.consume().unwrap_or_default();
let mut error_msg = UnmanagedVector::default();
let wasm = load_wasm(
cache_ptr,
ByteSliceView::new(&checksum),
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
let wasm = wasm.consume().unwrap_or_default();
assert_eq!(wasm, HACKATOM);
release_cache(cache_ptr);
}
#[test]
fn pin_works() {
let dir: String = TempDir::new().unwrap().path().to_str().unwrap().to_owned();
let features = b"staking";
let mut error_msg = UnmanagedVector::default();
let cache_ptr = init_cache(
ByteSliceView::new(dir.as_bytes()),
ByteSliceView::new(features),
512,
32,
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
let mut error_msg = UnmanagedVector::default();
let checksum = save_wasm(
cache_ptr,
ByteSliceView::new(HACKATOM),
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
let checksum = checksum.consume().unwrap_or_default();
let mut error_msg = UnmanagedVector::default();
pin(
cache_ptr,
ByteSliceView::new(&checksum),
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
// pinning again has no effect
let mut error_msg = UnmanagedVector::default();
pin(
cache_ptr,
ByteSliceView::new(&checksum),
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
release_cache(cache_ptr);
}
#[test]
fn unpin_works() {
let dir: String = TempDir::new().unwrap().path().to_str().unwrap().to_owned();
let features = b"staking";
let mut error_msg = UnmanagedVector::default();
let cache_ptr = init_cache(
ByteSliceView::new(dir.as_bytes()),
ByteSliceView::new(features),
512,
32,
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
let mut error_msg = UnmanagedVector::default();
let checksum = save_wasm(
cache_ptr,
ByteSliceView::new(HACKATOM),
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
let checksum = checksum.consume().unwrap_or_default();
let mut error_msg = UnmanagedVector::default();
pin(
cache_ptr,
ByteSliceView::new(&checksum),
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
let mut error_msg = UnmanagedVector::default();
unpin(
cache_ptr,
ByteSliceView::new(&checksum),
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
// Unpinning again has no effect
let mut error_msg = UnmanagedVector::default();
unpin(
cache_ptr,
ByteSliceView::new(&checksum),
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
release_cache(cache_ptr);
}
#[test]
fn analyze_code_works() {
let dir: String = TempDir::new().unwrap().path().to_str().unwrap().to_owned();
let features = b"staking,stargate,iterator";
let mut error_msg = UnmanagedVector::default();
let cache_ptr = init_cache(
ByteSliceView::new(dir.as_bytes()),
ByteSliceView::new(features),
512,
32,
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
let mut error_msg = UnmanagedVector::default();
let checksum_hackatom = save_wasm(
cache_ptr,
ByteSliceView::new(HACKATOM),
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
let checksum_hackatom = checksum_hackatom.consume().unwrap_or_default();
let mut error_msg = UnmanagedVector::default();
let checksum_ibc_reflect = save_wasm(
cache_ptr,
ByteSliceView::new(IBC_REFLECT),
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
let checksum_ibc_reflect = checksum_ibc_reflect.consume().unwrap_or_default();
let mut error_msg = UnmanagedVector::default();
let hackatom_report = analyze_code(
cache_ptr,
ByteSliceView::new(&checksum_hackatom),
Some(&mut error_msg),
);
let _ = error_msg.consume();
assert_eq!(hackatom_report.has_ibc_entry_points, false);
assert_eq!(hackatom_report.required_features.consume().unwrap(), b"");
let mut error_msg = UnmanagedVector::default();
let ibc_reflect_report = analyze_code(
cache_ptr,
ByteSliceView::new(&checksum_ibc_reflect),
Some(&mut error_msg),
);
let _ = error_msg.consume();
assert_eq!(ibc_reflect_report.has_ibc_entry_points, true);
assert_eq!(
ibc_reflect_report.required_features.consume().unwrap(),
b"iterator,staking,stargate"
);
release_cache(cache_ptr);
}
#[test]
fn set_to_csv_works() {
assert_eq!(set_to_csv(HashSet::new()), "");
assert_eq!(
set_to_csv(HashSet::from_iter(vec!["foo".to_string()])),
"foo",
);
assert_eq!(
set_to_csv(HashSet::from_iter(vec![
"foo".to_string(),
"bar".to_string(),
"baz".to_string(),
])),
"bar,baz,foo",
);
assert_eq!(
set_to_csv(HashSet::from_iter(vec![
"a".to_string(),
"aa".to_string(),
"b".to_string(),
"c".to_string(),
"A".to_string(),
"AA".to_string(),
"B".to_string(),
"C".to_string(),
])),
"A,AA,B,C,a,aa,b,c",
);
}
#[test]
fn get_metrics_works() {
let dir: String = TempDir::new().unwrap().path().to_str().unwrap().to_owned();
let features = b"staking";
// Init cache
let mut error_msg = UnmanagedVector::default();
let cache_ptr = init_cache(
ByteSliceView::new(dir.as_bytes()),
ByteSliceView::new(features),
512,
32,
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
// Get metrics 1
let mut error_msg = UnmanagedVector::default();
let metrics = get_metrics(cache_ptr, Some(&mut error_msg));
let _ = error_msg.consume();
assert_eq!(metrics, Metrics::default());
// Save wasm
let mut error_msg = UnmanagedVector::default();
let checksum_hackatom = save_wasm(
cache_ptr,
ByteSliceView::new(HACKATOM),
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
let checksum = checksum_hackatom.consume().unwrap_or_default();
// Get metrics 2
let mut error_msg = UnmanagedVector::default();
let metrics = get_metrics(cache_ptr, Some(&mut error_msg));
let _ = error_msg.consume();
assert_eq!(metrics, Metrics::default());
// Pin
let mut error_msg = UnmanagedVector::default();
pin(
cache_ptr,
ByteSliceView::new(&checksum),
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
// Get metrics 3
let mut error_msg = UnmanagedVector::default();
let metrics = get_metrics(cache_ptr, Some(&mut error_msg));
let _ = error_msg.consume();
assert_eq!(
metrics,
Metrics {
hits_pinned_memory_cache: 0,
hits_memory_cache: 0,
hits_fs_cache: 1,
misses: 0,
elements_pinned_memory_cache: 1,
elements_memory_cache: 0,
size_pinned_memory_cache: 5576140,
size_memory_cache: 0,
}
);
// Unpin
let mut error_msg = UnmanagedVector::default();
unpin(
cache_ptr,
ByteSliceView::new(&checksum),
Some(&mut error_msg),
);
assert_eq!(error_msg.is_none(), true);
let _ = error_msg.consume();
// Get metrics 4
let mut error_msg = UnmanagedVector::default();
let metrics = get_metrics(cache_ptr, Some(&mut error_msg));
let _ = error_msg.consume();
assert_eq!(
metrics,
Metrics {
hits_pinned_memory_cache: 0,
hits_memory_cache: 0,
hits_fs_cache: 1,
misses: 0,
elements_pinned_memory_cache: 0,
elements_memory_cache: 0,
size_pinned_memory_cache: 0,
size_memory_cache: 0,
}
);
release_cache(cache_ptr);
}
}