screeps/game/
cpu.rs

1//! Information about, and functions to manage, your code's resource utilization
2//!
3//! [Screeps documentation](http://docs.screeps.com/api/#Game.cpu)
4use wasm_bindgen::prelude::*;
5
6#[cfg(feature = "mmo")]
7use crate::{constants::ErrorCode, prelude::*};
8#[cfg(feature = "mmo")]
9use js_sys::{JsString, Object};
10
11#[wasm_bindgen]
12extern "C" {
13    #[wasm_bindgen(js_name = "cpu")]
14    type Cpu;
15
16    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, getter, js_name = limit)]
17    fn limit() -> u32;
18
19    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, getter, js_name = tickLimit)]
20    fn tick_limit() -> f64;
21
22    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, getter, js_name = bucket)]
23    fn bucket() -> i32;
24
25    #[cfg(feature = "mmo")]
26    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, js_name = shardLimits)]
27    fn shard_limits() -> Object;
28
29    #[cfg(feature = "mmo")]
30    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, getter, js_name = unlocked)]
31    fn unlocked() -> bool;
32
33    #[cfg(feature = "mmo")]
34    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, getter, js_name = unlockedTime)]
35    fn unlocked_time() -> Option<u64>;
36
37    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, js_name = getHeapStatistics)]
38    fn get_heap_statistics() -> HeapStatistics;
39
40    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, js_name = getUsed)]
41    fn get_used() -> f64;
42
43    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, js_name = halt)]
44    fn halt();
45
46    #[cfg(feature = "mmo")]
47    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, js_name = setShardLimits)]
48    fn set_shard_limits(limits: &Object) -> i8;
49
50    #[cfg(feature = "mmo")]
51    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, js_name = unlock)]
52    fn unlock() -> i8;
53
54    #[cfg(feature = "mmo")]
55    #[wasm_bindgen(js_namespace = ["Game"], js_class = "cpu", static_method_of = Cpu, js_name = generatePixel)]
56    fn generate_pixel() -> i8;
57}
58
59/// Your assigned CPU for the current shard.
60pub fn limit() -> u32 {
61    Cpu::limit()
62}
63
64/// The amount of CPU available for execution in a given tick.
65///
66/// Consists of your per-tick CPU [`limit`] plus your accrued [`bucket`], up to
67/// a maximum of 500 ([`CPU_TICK_LIMIT_MAX`]); [`f64::INFINITY`] on sim.
68///
69/// [`CPU_TICK_LIMIT_MAX`]: crate::constants::extra::CPU_TICK_LIMIT_MAX
70pub fn tick_limit() -> f64 {
71    Cpu::tick_limit()
72}
73
74/// The amount of CPU that has accumulated in your bucket.
75pub fn bucket() -> i32 {
76    Cpu::bucket()
77}
78
79/// Your assigned CPU limits for each shard in an [`Object`], with shard
80/// names in [`JsString`] form as keys and numbers as values. This is the
81/// same format accepted by [`set_shard_limits`].
82#[cfg(feature = "mmo")]
83pub fn shard_limits() -> JsHashMap<JsString, u32> {
84    Cpu::shard_limits().into()
85}
86
87/// Whether your account is unlocked to have full CPU.
88#[cfg(feature = "mmo")]
89pub fn unlocked() -> bool {
90    Cpu::unlocked()
91}
92
93/// If your account has been unlocked for a limited time, contains the time
94/// it's unlocked until in milliseconds since epoch.
95#[cfg(feature = "mmo")]
96pub fn unlocked_time() -> Option<u64> {
97    Cpu::unlocked_time()
98}
99
100/// Get information about your script's memory heap usage.
101///
102/// [Screeps documentation](https://docs.screeps.com/api/#Game.cpu.getHeapStatistics)
103pub fn get_heap_statistics() -> HeapStatistics {
104    Cpu::get_heap_statistics()
105}
106
107/// Get the amount of CPU time used for execution so far this tick.
108///
109/// [Screeps documentation](https://docs.screeps.com/api/#Game.cpu.getUsed)
110pub fn get_used() -> f64 {
111    Cpu::get_used()
112}
113
114/// Stop execution of your script immediately and requests the destruction of
115/// your code's environment, which will start fresh on the following tick.
116///
117/// Note that this will cause your code to not complete API calls called earlier
118/// in the current tick; no log messages will be sent to the console, email
119/// messages sent via `game::notify` are not sent, and game actions taken should
120/// not complete.
121///
122/// [Screeps documentation](https://docs.screeps.com/api/#Game.cpu.halt)
123pub fn halt() {
124    Cpu::halt()
125}
126
127/// Set the allocation of your CPU among the server shards.
128///
129/// Limits should be in an [`Object`], with shard names in [`JsString`] form as
130/// keys and numbers as values. This is the same format returned by
131/// [`shard_limits`]. Total amount of CPU should remain equal to the sum of the
132/// values of [`shard_limits`]. This method can be used only once per 12 hours
133/// ([`CPU_SET_SHARD_LIMITS_COOLDOWN`]).
134///
135/// [Screeps documentation](https://docs.screeps.com/api/#Game.cpu.setShardLimits)
136///
137/// [`CPU_SET_SHARD_LIMITS_COOLDOWN`]: crate::constants::CPU_SET_SHARD_LIMITS_COOLDOWN
138#[cfg(feature = "mmo")]
139pub fn set_shard_limits(limits: &Object) -> Result<(), ErrorCode> {
140    ErrorCode::result_from_i8(Cpu::set_shard_limits(limits))
141}
142
143/// Consume a [`CpuUnlock`] to unlock your full CPU for 24 hours.
144///
145/// [Screeps documentation](https://docs.screeps.com/api/#Game.cpu.unlock)
146///
147/// [`CpuUnlock`]: crate::constants::IntershardResourceType::CpuUnlock
148#[cfg(feature = "mmo")]
149pub fn unlock() -> Result<(), ErrorCode> {
150    ErrorCode::result_from_i8(Cpu::unlock())
151}
152
153/// Generate a [`Pixel`], consuming [`PIXEL_CPU_COST`] CPU from your bucket.
154///
155/// [Screeps documentation](https://docs.screeps.com/api/#Game.cpu.generatePixel)
156///
157/// [`Pixel`]: crate::constants::IntershardResourceType::Pixel
158/// [`PIXEL_CPU_COST`]: crate::constants::PIXEL_CPU_COST
159#[cfg(feature = "mmo")]
160pub fn generate_pixel() -> Result<(), ErrorCode> {
161    ErrorCode::result_from_i8(Cpu::generate_pixel())
162}
163
164#[wasm_bindgen]
165extern "C" {
166    /// Object with info about the memory heap of your virtual machine.
167    ///
168    /// [Screeps documentation](https://docs.screeps.com/api/#Game.cpu.getHeapStatistics)
169    #[wasm_bindgen]
170    pub type HeapStatistics;
171
172    /// The total heap consumed.
173    #[wasm_bindgen(method, getter)]
174    pub fn total_heap_size(this: &HeapStatistics) -> u32;
175
176    /// The total heap consumed by executable code.
177    #[wasm_bindgen(method, getter)]
178    pub fn total_heap_size_executable(this: &HeapStatistics) -> u32;
179
180    /// The total amount of heap committed to memory.
181    #[wasm_bindgen(method, getter)]
182    pub fn total_physical_size(this: &HeapStatistics) -> u32;
183
184    /// Amount of heap available for allocation.
185    #[wasm_bindgen(method, getter)]
186    pub fn total_available_size(this: &HeapStatistics) -> u32;
187
188    /// Total heap consumed by application data.
189    #[wasm_bindgen(method, getter)]
190    pub fn used_heap_size(this: &HeapStatistics) -> u32;
191
192    /// The allowed limit for total heap memory.
193    #[wasm_bindgen(method, getter)]
194    pub fn heap_size_limit(this: &HeapStatistics) -> u32;
195
196    /// Total amount of memory obtained by malloc.
197    #[wasm_bindgen(method, getter)]
198    pub fn malloced_memory(this: &HeapStatistics) -> u32;
199
200    /// Maximum amount of memory obtained by malloc.
201    #[wasm_bindgen(method, getter)]
202    pub fn peak_malloced_memory(this: &HeapStatistics) -> u32;
203
204    /// Whether the virtual machine overwrites memory as it deallocates -
205    /// usually 0.
206    #[wasm_bindgen(method, getter)]
207    pub fn does_zap_garbage(this: &HeapStatistics) -> u32;
208
209    /// External allocations that are outside of the v8 heap but still count
210    /// against the memory limit.
211    #[wasm_bindgen(method, getter)]
212    pub fn externally_allocated_size(this: &HeapStatistics) -> u32;
213}