-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
__motoko_runtime_information()
as Privileged Query for Runtime Stat…
…istics. (#4635) # Privileged System-Level Query for Runtime Statistics Exposing a privileged system-level query function `__motoko_runtime_information()` that reports the current runtime statistics of the canister, such as the compiler version, runtime system version, the GC in use, the heap size, the total number of allocated objects, the total amount of reclaimed memory and more. This is useful because several statistics of the reported information cannot be inspected on the IC replica dashboard as they are internal to the Motoko runtime system. This query is **only authorized to the canister controllers and self-calls of the canister**. ### Function Signature ``` __motoko_runtime_information : () -> { compilerVersion : Text; rtsVersion : Text; garbageCollector : Text; memorySize : Nat; heapSize : Nat; totalAllocation : Nat; reclaimed : Nat; maxLiveSize : Nat; stableMemorySize : Nat; logicalStableMemorySize : Nat; maxStackSize : Nat; callbackTableCount : Nat; callbackTableSize : Nat; } ```
- Loading branch information
1 parent
c7e5ac9
commit 4a3ff85
Showing
16 changed files
with
285 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
ingress Completed: Reply: 0x4449444c016c01b3c4b1f204680100010a00000000000000000101 | ||
ingress Completed: Reply: 0x4449444c0000 | ||
debug.print: (50_227, +29_863_068, 708_174_952) | ||
debug.print: (50_070, +32_992_212, 766_613_680) | ||
debug.print: (50_070, +32_992_212, 766_613_520) | ||
ingress Completed: Reply: 0x4449444c0000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
ingress Completed: Reply: 0x4449444c016c01b3c4b1f204680100010a00000000000000000101 | ||
ingress Completed: Reply: 0x4449444c0000 | ||
debug.print: (50_227, +29_863_068, 769_000_085) | ||
debug.print: (50_070, +32_992_212, 830_427_376) | ||
debug.print: (50_070, +32_992_212, 830_427_128) | ||
ingress Completed: Reply: 0x4449444c0000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
ingress Completed: Reply: 0x4449444c016c01b3c4b1f204680100010a00000000000000000101 | ||
ingress Completed: Reply: 0x4449444c0000 | ||
debug.print: Self information okay | ||
debug.print: Controllee information okay | ||
debug.print: IC0503: Canister rwlgt-iiaaa-aaaaa-aaaaa-cai trapped explicitly: Unauthorized call of __motoko_runtime_information | ||
ingress Completed: Reply: 0x4449444c0000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
ingress Completed: Reply: 0x4449444c016c01b3c4b1f204680100010a00000000000000000101 | ||
ingress Completed: Reply: 0x4449444c0000 | ||
debug.print: Ignore Diff: (ignored) | ||
ingress Completed: Reply: 0x4449444c0000 | ||
ingress Completed: Reply: 0x4449444c0000 | ||
debug.print: Ignore Diff: (ignored) | ||
ingress Completed: Reply: 0x4449444c0000 | ||
ingress Completed: Reply: 0x4449444c0000 | ||
debug.print: Ignore Diff: (ignored) | ||
ingress Completed: Reply: 0x4449444c0000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Prim "mo:⛔"; | ||
import Lib "runtime-info-acl/C"; | ||
import Info "runtime-info/info" | ||
// Test `__motoko_runtime_information` can only be invoked from self or controller. | ||
actor Self { | ||
public func go() : async () { | ||
// Test call from Self | ||
do { | ||
try { | ||
ignore await Info.introspect(Self).__motoko_runtime_information(); | ||
Prim.debugPrint("Self information okay"); | ||
} | ||
catch _e { | ||
assert false; | ||
} | ||
}; | ||
|
||
let c = await (Lib.C(Info.introspect(Self))); | ||
|
||
// Test call from controller (Self is a controller of c). | ||
do { | ||
try { | ||
ignore await Info.introspect(c).__motoko_runtime_information(); | ||
Prim.debugPrint("Controllee information okay"); | ||
} | ||
catch _e { | ||
assert false; | ||
} | ||
}; | ||
|
||
// Test callback from non-controller (c is not a controller of Self). | ||
do { | ||
try { | ||
await c.callback(); | ||
assert false; | ||
} | ||
catch e { | ||
Prim.debugPrint(Prim.errorMessage(e)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
//CALL ingress go "DIDL\x00\x00" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
actor class C( | ||
internal : actor { | ||
__motoko_runtime_information : () -> async {}; | ||
} | ||
) { | ||
|
||
// attempt to call runtime info for the actor passed as reference | ||
public shared func callback() : async () { | ||
ignore await internal.__motoko_runtime_information(); // should fail unless controller or selfaxs | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Prim "mo:prim"; | ||
import Info "runtime-info/info"; | ||
actor Self { | ||
var array : [var Nat] = [var]; | ||
let region = Prim.regionNew(); | ||
ignore Prim.regionGrow(region, 1); | ||
|
||
public func increaseHeap() : async () { | ||
array := Prim.Array_init<Nat>(array.size() + 1024 * 1024, 0); | ||
}; | ||
|
||
func validGC(strategy : Text) : Bool { | ||
for (name in ["copying", "compacting", "generational", "incremental"].vals()) { | ||
if (strategy == name # " force") { | ||
return true; | ||
}; | ||
}; | ||
return false; | ||
}; | ||
|
||
public func checkInformation() : async () { | ||
let information = await Info.introspect(Self).__motoko_runtime_information(); | ||
Prim.debugPrint("Ignore Diff: " # debug_show (information)); | ||
// Runtime information differs between GCs and sanity check options. | ||
assert (validGC(information.garbageCollector)); | ||
assert (information.heapSize > array.size() * 4); // or 8 with 64-bit | ||
assert (information.heapSize < information.memorySize); | ||
assert (information.maxStackSize > 1024); | ||
assert (information.maxStackSize % 1024 == 0); | ||
assert (information.totalAllocation > 0); | ||
assert (information.stableMemorySize > 0); | ||
assert (information.logicalStableMemorySize == information.stableMemorySize); | ||
assert (information.callbackTableCount <= information.callbackTableSize); | ||
}; | ||
}; | ||
|
||
// Not calling query __motoko_runtime_information "DIDL\x00\x00" as it differs | ||
// between the compiler options and changes with every compiler/RTS adjustment. | ||
|
||
//SKIP run | ||
//SKIP run-low | ||
//SKIP run-ir | ||
//CALL ingress checkInformation "DIDL\x00\x00" | ||
//CALL ingress increaseHeap "DIDL\x00\x00" | ||
//CALL ingress checkInformation "DIDL\x00\x00" | ||
//CALL ingress increaseHeap "DIDL\x00\x00" | ||
//CALL ingress checkInformation "DIDL\x00\x00" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Prim "mo:prim"; | ||
|
||
module { | ||
public type RuntimeInformation = { | ||
compilerVersion : Text; | ||
rtsVersion : Text; | ||
garbageCollector : Text; | ||
sanityChecks : Bool; | ||
memorySize : Nat; | ||
heapSize : Nat; | ||
totalAllocation : Nat; | ||
reclaimed : Nat; | ||
maxLiveSize : Nat; | ||
stableMemorySize : Nat; | ||
logicalStableMemorySize : Nat; | ||
maxStackSize : Nat; | ||
callbackTableCount : Nat; | ||
callbackTableSize : Nat; | ||
}; | ||
|
||
public type ActorIntrospection = actor { | ||
__motoko_runtime_information : () -> async RuntimeInformation; | ||
}; | ||
|
||
public func introspect(a : actor {}) : ActorIntrospection { | ||
(actor (debug_show (Prim.principalOfActor(a))) : ActorIntrospection); | ||
}; | ||
}; |