<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Finding the heap low water mark when using nRFConnect/Zephyr</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/67495/finding-the-heap-low-water-mark-when-using-nrfconnect-zephyr</link><description>I need to monitor how much heap was ever left available, as a minimum, after I have run my CI test suite. This is the true minimum heap ever remaining, so taking account of calls to malloc() as well as to Zephyr&amp;#39;s memory management APIs, i.e. the true</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 20 Nov 2020 09:22:53 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/67495/finding-the-heap-low-water-mark-when-using-nrfconnect-zephyr" /><item><title>RE: Finding the heap low water mark when using nRFConnect/Zephyr</title><link>https://devzone.nordicsemi.com/thread/281050?ContentTypeID=1</link><pubDate>Fri, 20 Nov 2020 09:22:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:17fb1671-c4b2-49c2-890e-650ddeb609f8</guid><dc:creator>Simon</dc:creator><description>&lt;p&gt;Thanks for sharing your findings. Appreciated.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Finding the heap low water mark when using nRFConnect/Zephyr</title><link>https://devzone.nordicsemi.com/thread/280917?ContentTypeID=1</link><pubDate>Thu, 19 Nov 2020 13:54:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:50e50c12-4925-4250-ac34-e74f888db33c</guid><dc:creator>RobMeades</dc:creator><description>&lt;p&gt;FYI, since&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code&gt;newlib&lt;/code&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;is being used by Zephyr under the hood (at least on NRF53) it is&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;almost&lt;/strong&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;possible to do this by calling the&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code&gt;newlib&lt;/code&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code&gt;mallinfo()&lt;/code&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;function. The reason this is only &amp;quot;almost&amp;quot; is that&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code&gt;mallinfo()&lt;/code&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;is not the whole story:&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code&gt;newlib&lt;/code&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;calls&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code&gt;sbrk()&lt;/code&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;as required to give it heap memory from the ultimate heap, so the real number is&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code&gt;mallinfo&lt;/code&gt;&amp;#39;s&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code&gt;fordblks&lt;/code&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;plus&lt;/strong&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;what&amp;#39;s left in&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code&gt;sbrk()&lt;/code&gt;: without this the reported heap can end up going UP from it&amp;#39;s original value, a &amp;quot;negative leak&amp;quot;, which would cause any strict heap checking to fail.&lt;/p&gt;
&lt;p&gt;Unfortunately, the variable that tracks the memory left inside&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code&gt;sbrk()&lt;/code&gt;,&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code&gt;heap_sz&lt;/code&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;in&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code&gt;lib/libc/newlib/libc-hooks.c&lt;/code&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;is&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code&gt;static&lt;/code&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;so one can&amp;#39;t get at it to do the free heap calculation properly.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve noted this on &lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="https://github.com/zephyrproject-rtos/zephyr/issues/23076"&gt;https://github.com/zephyrproject-rtos/zephyr/issues/23076&lt;/a&gt;&amp;nbsp;and suggested that the simplest change would be to remove the&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code&gt;static&lt;/code&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;from that variable, then people can solve this problem themselves.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Finding the heap low water mark when using nRFConnect/Zephyr</title><link>https://devzone.nordicsemi.com/thread/278640?ContentTypeID=1</link><pubDate>Thu, 05 Nov 2020 14:41:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1288cc7c-baa8-4548-9048-78adf76bb5bf</guid><dc:creator>RobMeades</dc:creator><description>&lt;p&gt;Will do.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Finding the heap low water mark when using nRFConnect/Zephyr</title><link>https://devzone.nordicsemi.com/thread/278637?ContentTypeID=1</link><pubDate>Thu, 05 Nov 2020 14:35:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a039786b-479b-4325-bec7-e0528bddb02a</guid><dc:creator>Simon</dc:creator><description>&lt;p&gt;Could you try to ask about this in the GitHub issue?&amp;nbsp;If you don&amp;#39;t get any answers, please ping me here.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Finding the heap low water mark when using nRFConnect/Zephyr</title><link>https://devzone.nordicsemi.com/thread/278567?ContentTypeID=1</link><pubDate>Thu, 05 Nov 2020 10:54:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bc2b5a02-c8e3-4b35-af6a-b5ff05aecd8b</guid><dc:creator>RobMeades</dc:creator><description>&lt;p&gt;Agree that largest contiguous&amp;nbsp;region is probably a better measure, also saw your colleague &amp;quot;bjda&amp;quot; post that code on the Github thread, thanks.&lt;br /&gt;&lt;br /&gt;The remaining question, then, is how Zephyr uses the heap.&amp;nbsp; We are, of course, going to be calling &lt;strong&gt;malloc()&lt;/strong&gt;/&lt;strong&gt;free()&lt;/strong&gt; as any C code would, does Zephyr wrap those functions somehow and do entirely it&amp;#39;s own thing with the heap memory, or does it work in parallel with them and sit atop &lt;strong&gt;malloc()&lt;/strong&gt;/f&lt;strong&gt;ree()&lt;/strong&gt; itself?&amp;nbsp; If the former then your colleague&amp;#39;s solution is good, if the latter it won&amp;#39;t help because it&amp;#39;s only half the story.&lt;/p&gt;
&lt;p&gt;Or do you suggest I take this conversation up with your colleague on the Github issue thread?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Finding the heap low water mark when using nRFConnect/Zephyr</title><link>https://devzone.nordicsemi.com/thread/278200?ContentTypeID=1</link><pubDate>Tue, 03 Nov 2020 08:31:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:97ffb179-76f8-4cc8-b47c-e43e0672004e</guid><dc:creator>Simon</dc:creator><description>&lt;p&gt;A colleague put some work into this, check it out here:&amp;nbsp;&lt;a title="https://github.com/zephyrproject-rtos/zephyr/pull/29735" href="https://github.com/zephyrproject-rtos/zephyr/pull/29735" rel="noopener noreferrer" target="_blank"&gt;https://github.com/zephyrproject-rtos/zephyr/pull/29735&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The approach I mentioned earlier, by filling the heap and looking at it, may be misleading.&amp;nbsp;The minimum largest contiguous region is probably the best &amp;quot;low water mark&amp;quot;, since it will react to fragmentation of the heap.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Finding the heap low water mark when using nRFConnect/Zephyr</title><link>https://devzone.nordicsemi.com/thread/278052?ContentTypeID=1</link><pubDate>Mon, 02 Nov 2020 12:24:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7d509424-12fe-4021-8c20-86eaf13175d3</guid><dc:creator>Simon</dc:creator><description>&lt;p&gt;I&amp;#39;ll put off some time Wednesday to look into this.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Finding the heap low water mark when using nRFConnect/Zephyr</title><link>https://devzone.nordicsemi.com/thread/278035?ContentTypeID=1</link><pubDate>Mon, 02 Nov 2020 11:20:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2c7d97b9-a63e-42c0-9757-8a2abe1191e8</guid><dc:creator>RobMeades</dc:creator><description>&lt;p&gt;We have stack sorted (we call&amp;nbsp;k_thread_stack_space_get()), it&amp;#39;s heap that we can&amp;#39;t measure.&lt;/p&gt;
&lt;p&gt;I had thought that it would be possible to do a maximum heap extent measurement by simply monitoring newlib&amp;#39;s calls to sbrk(), since newlib will not ask for more space until it needs it.&amp;nbsp; But it might be that newlib calls for more space&amp;nbsp;irrespective of whether there might be a free&amp;#39;d block it could use, deliberating putting off the freeing process as an expensive operation.&lt;/p&gt;
&lt;p&gt;So yes, if you were able to devise a mechanism to monitor heap I would be very interested.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Finding the heap low water mark when using nRFConnect/Zephyr</title><link>https://devzone.nordicsemi.com/thread/278020?ContentTypeID=1</link><pubDate>Mon, 02 Nov 2020 10:37:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:afeae98e-d431-45b5-86f4-88d866e9f62a</guid><dc:creator>Simon</dc:creator><description>&lt;p&gt;I got some answers internally. It does not seem like there is something built-in for &amp;quot;heap painting&amp;quot;. However, for stack there are, check out the config &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.4.0/kconfig/CONFIG_INIT_STACKS.html#cmdoption-arg-CONFIG_INIT_STACKS"&gt;CONFIG_INIT_STACKS&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In order to achieve this, you would need to initialize the area on init, and clear/reset every allocated/free&amp;#39;d blob. Do you know how to go about this? If not I can look into it and try to make it work.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Finding the heap low water mark when using nRFConnect/Zephyr</title><link>https://devzone.nordicsemi.com/thread/277314?ContentTypeID=1</link><pubDate>Wed, 28 Oct 2020 09:41:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c720b564-c936-4b0e-a5c1-590d6a2f5c93</guid><dc:creator>RobMeades</dc:creator><description>&lt;p&gt;FYI, just been pointed at this:&amp;nbsp;&lt;a href="https://github.com/zephyrproject-rtos/zephyr/issues/23076"&gt;https://github.com/zephyrproject-rtos/zephyr/issues/23076&lt;/a&gt;. Seems that heap monitoring is not yet implemented in Zephyr.&amp;nbsp; But even if it was it doesn&amp;#39;t answer the whole question because I guess Zephyr would likely sit on top of &lt;strong&gt;malloc()&lt;/strong&gt; rather than below.&amp;nbsp; How do I monitor the true heap usage on NRF53 using nRFConnect/.Zephyr?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Finding the heap low water mark when using nRFConnect/Zephyr</title><link>https://devzone.nordicsemi.com/thread/277064?ContentTypeID=1</link><pubDate>Tue, 27 Oct 2020 09:21:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5905ba22-cf96-40f6-91ec-ab7b26b85111</guid><dc:creator>RobMeades</dc:creator><description>&lt;p&gt;No, no response from Zephyr to my questions either on their Slack channel or via their mailing list.&amp;nbsp; Very odd: like being asked to buy a car without a fuel gauge.&amp;nbsp; Can&amp;#39;t be normal can it?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Finding the heap low water mark when using nRFConnect/Zephyr</title><link>https://devzone.nordicsemi.com/thread/276739?ContentTypeID=1</link><pubDate>Sat, 24 Oct 2020 16:11:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:58cecf46-7e8f-4be3-830d-6629e60dd7c3</guid><dc:creator>RobMeades</dc:creator><description>&lt;p&gt;OK, have asked on the Zephyr Slack but I expect they&amp;#39;ll bounce me back here for a &amp;quot;platform implementation&amp;quot; issue.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Finding the heap low water mark when using nRFConnect/Zephyr</title><link>https://devzone.nordicsemi.com/thread/276564?ContentTypeID=1</link><pubDate>Fri, 23 Oct 2020 08:12:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b5832953-974b-4199-b174-0395c244de44</guid><dc:creator>Simon</dc:creator><description>&lt;p&gt;I&amp;#39;m not sure about this. I&amp;#39;ll ask internally about it. You could also try to ask in any of the resources listed &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.3.0/zephyr/introduction/index.html#community-support"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>