# Programmatically measure cycles consumption

**URL:** <https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450>\
**Category:** Developers\
**Created:** [April 26, 2022, 3:13pm UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450 "2022-04-26T15:13:49Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![berestovskyy](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/berestovskyy/32/3989_2.png) [@berestovskyy](https://forum.dfinity.org/u/berestovskyy)\
**Post date:** [May 23, 2022, 12:34pm UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/21 "2022-05-23T12:34:16Z")

</div>

> [@gabe](#):
>
> Interesting! Is that a link to internal docs? I don’t seem to be able to access it but would love to read up on it.

Ah sorry, here is the [link to the spec on smartcontracts.org](https://smartcontracts.org/docs/current/references/ic-interface-spec/#system-api-performance-counter)

---

<div class="post-metadata">

**Author:** ![gabe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabe/32/4612_2.png) [@gabe](https://forum.dfinity.org/u/gabe)\
**Post date:** [May 23, 2022, 2:39pm UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/22 "2022-05-23T14:39:36Z")

</div>

Thank you, I am very excited for this.

If I interpret the specification right, `ic0.performance_counter` counts all in-canister Wasm instructions from the entry point invocation, including chained functions. Does it also count instructions executed in other canisters from inter-canister calls?

---

<div class="post-metadata">

**Author:** ![inviscidpixels](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/inviscidpixels/32/9021_2.png) [@inviscidpixels](https://forum.dfinity.org/u/inviscidpixels)\
**Post date:** [May 23, 2022, 3:10pm UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/23 "2022-05-23T15:10:34Z")

</div>

Very useful distinction to understand how actor messaging on the IC works.

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [May 24, 2022, 12:37am UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/24 "2022-05-24T00:37:33Z")

</div>

@chenyan @berestovskyy Would you happen to know? I’m curious about this as well.

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [May 24, 2022, 12:58am UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/25 "2022-05-24T00:58:12Z")

</div>

Actually, this might be the answer: [Calculate an estimate of cycles consumption: a universal example - #6 by domwoe](https://forum.dfinity.org/t/calculate-an-estimate-of-cycles-consumption-a-universal-example/12526/6)

---

<div class="post-metadata">

**Author:** ![berestovskyy](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/berestovskyy/32/3989_2.png) [@berestovskyy](https://forum.dfinity.org/u/berestovskyy)\
**Post date:** [May 24, 2022, 4:02pm UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/26 "2022-05-24T16:02:51Z")

</div>

> [@gabe](#):
>
> If I interpret the specification right, `ic0.performance_counter` counts all in-canister Wasm instructions from the entry point invocation, including chained functions. Does it also count instructions executed in other canisters from inter-canister calls?

hey @gabe,  
The perf counter is in WASM instructions, and it starts from zero at the beginning of each execution. It’s a bit tricky with the actor model, as every `await` is a new execution.

I just created an example of the Cycles balance and Perf Counter during the inter-canister calls on Wiki, please have a look: [Comparing Canister Cycles vs Performance Counter - Internet Computer Wiki](https://wiki.internetcomputer.org/wiki/Comparing_Canister_Cycles_vs_Performance_Counter)

@jzxchiang At the moment the perf counter starts from zero for each new execution. We could preserve the counter and continue it after the `await`, but it gets tricky when we have a few calls in flight…

So for now please use the perf counter only for the “normal” WASM code, not across inter-canister calls.

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [May 24, 2022, 11:26pm UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/27 "2022-05-24T23:26:52Z")

</div>

This wiki is a really great resource, thanks.

---

<div class="post-metadata">

**Author:** ![gabe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabe/32/4612_2.png) [@gabe](https://forum.dfinity.org/u/gabe)\
**Post date:** [May 25, 2022, 9:47am UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/28 "2022-05-25T09:47:58Z")

</div>

This is great! Thank you for sharing.

For others reading this thread, I can recommend [this thread](https://forum.dfinity.org/t/calculate-an-estimate-of-cycles-consumption-a-universal-example/12526) as well.

---

<div class="post-metadata">

**Author:** ![berestovskyy](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/berestovskyy/32/3989_2.png) [@berestovskyy](https://forum.dfinity.org/u/berestovskyy)\
**Post date:** [May 25, 2022, 10:23am UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/29 "2022-05-25T10:23:58Z")

</div>

it’s very cool @gabe, very detailed page 👍 Looking forward to see a blog post!

And sure, if you have more questions, we are here to help!

---

<div class="post-metadata">

**Author:** ![gabe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabe/32/4612_2.png) [@gabe](https://forum.dfinity.org/u/gabe)\
**Post date:** [May 26, 2022, 3:08pm UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/30 "2022-05-26T15:08:20Z")

</div>

Thank you! I’m glad people appreciate it. 🙂

I have a question regarding the number of instructions I am getting from measurements of a particular entry point. I am using the profiling mode in Playground for the measurements and measured the instruction count 30 times.

Here is a plot of the data:  
 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/d/d0bf20d5a8784a8b232bfd4ca4766abde124eb91.png)

Here are some statistics:

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/6/62934a0cf9809f9ff8ec094e055088aefb275ca9.png)

And here is the raw data:

```auto
{
"x":[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],
"y":[4909516, 446697, 446697, 446697, 4913081, 446697, 446697, 446697, 4916251, 446697, 446697, 446697, 4919456, 446697, 446697, 446697, 4922661, 446697, 446697, 446697, 4925866, 446697, 446697, 446697, 4925866, 446697, 446697, 446697, 4925866, 446697]
}

```

Each bottom of the pattern is exactly 446,697 instructions.

Below is some simplified pseudo-ish code of what the entry point does:

```auto
let messages: TrieMap<MessageId, Message>;

let userToReceivedIds: TrieMap<Principal, List<MessageId>>;
let userToSentIds: TrieMap<Principal, List<MessageId>>;

  // Stores the message and adds the ID to the sender's sent IDs and each recipient's received IDs.
  func addMessage(message: Message) {
    messages.put(message.header.id, message);

    // Add message ID to sender' sent messages.
    let currentIdList = userToSentIds.get(message.header.from);
    let newIdList = List.push(message.header.id, currentIdList);
    userToSentIdsMap.put(message.header.from, newIdList);

    // Add message ID to each recipients received messages.
    let currentIdList = userToReceivedIds.get(message.header.to);
    let newIdList = List.push(message.header.id, currentIdList);
    userToReceivedIdsMap.put(message.header.to, newIdList);
  };

```

So basically there are two `get` from two `TrieMap`s, two `push` to two `List`s, and three `put` to three `TrieMap`s.

Do you know what could be causing this repeating triangle/sawtooth pattern? I am very curious to know since the difference between the bottoms (446,697) and the peaks (max 4,925,866) is huge.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [May 26, 2022, 4:10pm UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/31 "2022-05-26T16:10:57Z")

</div>

Do you force GC when compiling the canister? I suspect the difference is due to whether GC is triggered.

---

<div class="post-metadata">

**Author:** ![gabe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabe/32/4612_2.png) [@gabe](https://forum.dfinity.org/u/gabe)\
**Post date:** [May 26, 2022, 6:54pm UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/32 "2022-05-26T18:54:56Z")

</div>

Sounds like a very valid guess. To be honest, I am not really sure. I use the default canister settings.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [May 26, 2022, 7:36pm UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/33 "2022-05-26T19:36:02Z")

</div>

Yep, the default flag only triggers GC when there are enough changes from last GC. In the playground, there is a checkbox “Force garbage collection (only if you want to test GC)” to enable GC for every message.

---

<div class="post-metadata">

**Author:** ![gabe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabe/32/4612_2.png) [@gabe](https://forum.dfinity.org/u/gabe)\
**Post date:** [May 27, 2022, 7:58am UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/34 "2022-05-27T07:58:17Z")

</div>

That must be it then, thank you!

> [@chenyan](#):
>
> triggers GC when there are enough changes from last GC

What is considered enough changes?

Also, as a bonus question: is the stable memory in canisters deployed in the Playground capped to a size of less than 4 GB? If so, can I bypass it e.g by sending more cycles to it?

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [May 28, 2022, 4:57am UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/35 "2022-05-28T04:57:46Z")

</div>

Here is the current parameter: [motoko/Changelog.md at master · dfinity/motoko · GitHub](https://github.com/dfinity/motoko/blob/master/Changelog.md#066-2021-07-30)

The playground caps stable memory at 1G. If you want to experiment with stable memory, is it easier to deploy locally?

---

<div class="post-metadata">

**Author:** ![gabe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabe/32/4612_2.png) [@gabe](https://forum.dfinity.org/u/gabe)\
**Post date:** [May 28, 2022, 5:56am UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/36 "2022-05-28T05:56:20Z")

</div>

Perfect, thank you!

> [@chenyan](#):
>
> The playground caps stable memory at 1G. If you want to experiment with stable memory, is it easier to deploy locally?

Definitely much easier. The thing is that I am using the profiler in Playground to count instructions, and have not had time to fiddle with that in a local environment yet.

---

<div class="post-metadata">

**Author:** ![gabe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabe/32/4612_2.png) [@gabe](https://forum.dfinity.org/u/gabe)\
**Post date:** [June 27, 2022, 3:43pm UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/37 "2022-06-27T15:43:25Z")

</div>

I am measuring wasm instruction count with forced GC (copying) and get the following linear pattern:  
 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/3/38f41e0fe8d55a7d8fa59478302174b51eea47bf.png)

In each measurement, I push an element to a list and put it in a TrieMap. Each element is of the same size and the canister’s state is not reset between measurements.

Do you know what could be causing the linear pattern in instruction count?

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [June 27, 2022, 11:43pm UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/38 "2022-06-27T23:43:15Z")

</div>

Could be related to forcing GC. The container size is increasing each time, causing the GC to copy more data after each insertion.

---

<div class="post-metadata">

**Author:** ![gabe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gabe/32/4612_2.png) [@gabe](https://forum.dfinity.org/u/gabe)\
**Post date:** [June 28, 2022, 8:44am UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/39 "2022-06-28T08:44:28Z")

</div>

Good point. The amount of useful data that the GC has to copy grows linearly.

I was speculating around the fact that I am using a Trie structure. AFAIK, the Motoko Trie implementation produces a new copy of the trie for each put. I assume that the memory of the obsolete Trie is freed by the GC, but I am not sure if this would result in a higher number of instructions. Do you have any thoughts on this?

---

<div class="post-metadata">

**Author:** ![roman-kashitsyn](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/roman-kashitsyn/32/3802_2.png) [@roman-kashitsyn](https://forum.dfinity.org/u/roman-kashitsyn)\
**Post date:** [June 28, 2022, 9:11am UTC](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450/40 "2022-06-28T09:11:17Z")

</div>

> [@gabe](#):
>
> Do you know what could be causing the linear pattern in instruction count?

That’s a common pattern with all copying garbage collectors: GC time grows linearly with the size of the working set. Pusher engineers wrote a nice article about problems they experienced with the GHC GC: [Low latency, large working set, and GHC’s garbage collector: pick two of three | Pusher blog](https://making.pusher.com/latency-working-set-ghc-gc-pick-two/)

> [@gabe](#):
>
> AFAIK, the Motoko Trie implementation produces a new copy of the trie for each put. I assume that the memory of the obsolete Trie is freed by the GC, but I am not sure if this would result in a higher number of instructions.

Insertions into a Trie don’t require copying the entire data structure, only a few nodes that changed. In addition, with a copying GC, it’s not important how much garbage you have on the heap, only how much live memory you have.

[Previous page](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450.md?page=1)

[Next page](https://forum.dfinity.org/t/programmatically-measure-cycles-consumption/12450.md?page=3)
