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
use std::collections;
use serde::{Deserialize, Serialize};
use crate::{constants::ReturnCode, macros::*, traits::TryInto};
#[derive(Default, Serialize, Deserialize)]
pub struct HeapStatistics {
pub total_heap_size: u32,
pub total_heap_size_executable: u32,
pub total_physical_size: u32,
pub used_heap_size: u32,
pub heap_size_limit: u32,
pub malloced_memory: u32,
pub peak_malloced_memory: u32,
pub does_zap_garbage: u32,
pub externally_allocated_size: u32,
}
js_serializable!(HeapStatistics);
js_deserializable!(HeapStatistics);
pub fn limit() -> f64 {
js_unwrap!(Game.cpu.limit)
}
pub fn tick_limit() -> f64 {
js_unwrap!(Game.cpu.tickLimit)
}
pub fn bucket() -> f64 {
js_unwrap!(Game.cpu.bucket)
}
pub fn shard_limits() -> collections::HashMap<String, f64> {
js_unwrap!(Game.cpu.shardLimits)
}
pub fn get_heap_statistics() -> HeapStatistics {
use stdweb::Value;
let heap_stats: Value = js_unwrap!(Game.cpu.getHeapStatistics && Game.cpu.getHeapStatistics());
match heap_stats {
Value::Null | Value::Undefined | Value::Bool(false) => HeapStatistics::default(),
other => other.try_into().expect(
"expected Game.cpu.getHeapStatistics() to return an object with a known format",
),
}
}
pub fn get_used() -> f64 {
js_unwrap!(Game.cpu.getUsed())
}
pub fn set_shard_limits(limits: collections::HashMap<String, f64>) -> ReturnCode {
js_unwrap!(Game.cpu.setShardLimits(@{limits}))
}
pub fn halt() {
js! {
Game.cpu.halt();
}
}