Comparing Canister Cycles vs Performance Counter
From Internet Computer Wiki
Revision as of 10:59, 24 May 2022 by Andriy.berestovskyy (talk | contribs)
This example clarifies the charges in terms of Canister Cycles (i.e. ic0.canister_cycle_balance()
) and WASM Instructions (i.e. ic0.performance_counter()
).
Setup
- There is a User and two Canisters: Canisters A and Canister B
- The User sends an 1KiB Ingress Message to Canister A
- The Canister A calls Canister B with an 2KiB Update Call
- The Canister B replies to Canister A with an 300-byte response
- The Canister A replies to User with an 500-byte response
Example Cycles Flow
Naming conventions:
1K == 1'000
1KiB == 1'024 bytes
1M == 1'000'000
1MiB == 1'048'576 bytes
1B == 1'000'000'000
1T == 1'000'000'000'000
What is Happening | Canister A Cycles Balance |
Canister A Perf Counter |
Canister B Cycles Balance |
Canister B Perf Counter |
---|---|---|---|---|
1. Initial state | 1T |
N/A 1) |
1T |
N/A 1)
|
2. Canister A receives 1KiB Ingress Message | -1,2M -1KiB*2K ≈999'996M |
N/A 1) |
1T |
N/A 1)
|
3. Canister A starts execution 2) | -5B/10*4≈997'996M |
0 (5B limit) |
1T |
N/A 1)
|
4. Canister A executes 100M WASM instructions | 997'996M |
+100M=100M (5B limit) |
1T |
N/A 1)
|
5. Canister A sends 2KiB call to Canister B 3) | -260K -2KiB*1K -5B/10*4 -2MiB*1K ≈993'897M |
+3*20 +1KiB ≈100M (5B limit) |
1T |
N/A 1)
|
5. Canister A finishes execution 4) | +(5B-100M)*4/10≈995'857M |
N/A 1) |
1T |
N/A 1)
|
6. Canister B receives a call from Canister A | 995'857M |
N/A 1) |
-590K≈999'999M |
N/A 1)
|
7. Canister B starts execution 2) | 995'857M |
N/A 1) |
-5B/10*4≈997'999M |
0 (5B limit) |
8. Canister B executes 100M WASM instructions | 995'857M |
N/A 1) |
997'999M |
+100M=100M (5B limit) |
9. Canister B sends 300-byte response to Canister A | 995'857M |
N/A 1) |
997'999M |
+2*20 +300 ≈100M (5B limit) |
10. Canister B finishes execution 4) | 995'857M |
N/A 1) |
+(5B-100M)*4/10≈999'959M |
N/A 1)
|
11. Canister A receives a response from Canister B 5) | +(2MiB-300)*1K≈997'953M |
N/A 1) |
999'959M |
N/A 1)
|
12. Canister A starts reply callback execution 6) | 997'953M |
0 (5B limit) |
999'959M |
N/A 1)
|
13. Canister A executes 100M WASM instructions | 997'953M |
+100M=100M (5B limit) |
999'959M |
N/A 1)
|
14. Canister A sends 500-byte reply to User | 997'953M |
+2*20 +500 ≈100M (5B limit) |
999'959M |
N/A 1)
|
15. Canister A finishes execution 4) | +(5B-100M)*4/10≈999'913M |
N/A 1) |
999'959M |
N/A 1)
|
Total: | 87M Cycles for ~200M WASM Instructions Executed + messaging |
41M Cycles for ~100M WASM Instructions Executed + messaging |
Notes:
- The Performance Counter is available only during execution, so it's marked as
N/A
when the execution has not started yet. - At the moment, there is a 5B instructions limit per normal message execution, so the Canister get charged for the maximum amount of Instructions it can potentially execute.
- The Canister get charged:
- 260'000
- for the inter-canister call
- 2KiB * 1'000
- for byte transmission
- 5B/10*4
- for 5B instructions limit to process the response
- 2MiB * 1K
- for the maximum response size
- The Canister gets a refund for the unused Instructions.
- The Canister gets a refund for the unused response bytes.
- The Canister has already been charged for the reply callback.