{"id":4432,"date":"2018-11-19T17:02:46","date_gmt":"2018-11-20T01:02:46","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=4432"},"modified":"2018-11-19T17:02:46","modified_gmt":"2018-11-20T01:02:46","slug":"profiling-rtx-based-projects-with-the-sampling-profiler","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/keil\/rtx\/sampling\/","title":{"rendered":"Profiling RTX-based Projects with the Sampling Profiler"},"content":{"rendered":"<p>This tutorial shows how to use the VisualGDB&#8217;s sampling profiler to analyze the performance of\u00a0the projects build with the Keil compiler using the RTX operating system.<\/p>\n<p>The sampling profiler works by periodically capturing the contents of the processor&#8217;s stack and reporting it to VisualGDB. This produces less accurate results than the instrumenting profiler, however results in much less overhead, as it does not need to record each of the functions individually.<\/p>\n<p>Before you begin, create a basic RTX-based &#8220;Blinking LED&#8221; project by following <a href=\"https:\/\/visualgdb.com\/tutorials\/arm\/keil\/rtx\/\">this tutorial<\/a>.<\/p>\n<ol>\n<li>Open your project in Visual Studio and click Analyze -&gt; Analyze Performance with VisualGDB:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/01-addref.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4433\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/01-addref.png\" alt=\"01-addref\" width=\"1097\" height=\"784\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/01-addref.png 1097w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/01-addref-300x214.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/01-addref-1024x732.png 1024w\" sizes=\"(max-width: 1097px) 100vw, 1097px\" \/><\/a>Click &#8220;Yes&#8221; when VisualGDB prompts to add a reference to the profiler framework.<\/li>\n<li>Open VisualGDB Project Properties and go to the Embedded\u00a0Frameworks page. In order to avoid sending the entire stack to VisualGDB at each sample, the sampling profiler\u00a0uses a pair of auxiliary functions to determine whether\u00a0a certain value looks\u00a0like a valid code or stack pointer. For GCC-based projects, this can be determined automatically based on the symbols generated by the GNU linker, however the\u00a0projects built with the Keil compiler need to handle this explicitly. Check the &#8220;Provide custom address validation functions&#8221; checkbox to override the default GCC-based checks. Also set RTOS support to Keil RTX:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/02-sampling.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4434\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/02-sampling.png\" alt=\"02-sampling\" width=\"890\" height=\"652\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/02-sampling.png 890w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/02-sampling-300x220.png 300w\" sizes=\"(max-width: 890px) 100vw, 890px\" \/><\/a><\/li>\n<li>Depending on your project configuration, you may get errors\u00a0due to the missing STM32 components when trying to build the profiler code:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/03-dmahandle.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4435\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/03-dmahandle.png\" alt=\"03-dmahandle\" width=\"1097\" height=\"784\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/03-dmahandle.png 1097w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/03-dmahandle-300x214.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/03-dmahandle-1024x732.png 1024w\" sizes=\"(max-width: 1097px) 100vw, 1097px\" \/><\/a><\/li>\n<li>If this happens,\u00a0go to the Keil Components page of VisualGDB Project Properties and add the missing components to your project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/04-dma.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4436\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/04-dma.png\" alt=\"04-dma\" width=\"890\" height=\"652\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/04-dma.png 890w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/04-dma-300x220.png 300w\" sizes=\"(max-width: 890px) 100vw, 890px\" \/><\/a><\/li>\n<li>If you try building the project now, it will fail due to missing\u00a0<strong>_estack<\/strong> symbol and the\u00a0address validation functions:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/05-estack.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4437\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/05-estack.png\" alt=\"05-estack\" width=\"1097\" height=\"784\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/05-estack.png 1097w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/05-estack-300x214.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/05-estack-1024x732.png 1024w\" sizes=\"(max-width: 1097px) 100vw, 1097px\" \/><\/a><\/li>\n<li>Locate the end of the stack memory from your device specification (or the VisualGDB&#8217;s Memory Explorer) window and define the SYSPROGS_PROFILER_END_OF_RAM macro to point there:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/06-stackdef.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4438\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/06-stackdef.png\" alt=\"06-stackdef\" width=\"890\" height=\"652\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/06-stackdef.png 890w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/06-stackdef-300x220.png 300w\" sizes=\"(max-width: 890px) 100vw, 890px\" \/><\/a><\/li>\n<li>Then add the address checking functions to your main file. For the STM32F407VG device shown in this tutorial, the functions will look as follows:\n<pre class=\"\">int IsValidCodeAddress(void *pAddr)\r\n{\r\n    return (pAddr &gt;= (void *)0x08000000 &amp;&amp; pAddr &lt;= (void *)0x080fffff);\r\n}\r\n\r\nint IsValidStackAddress(void **pStackSlot)\r\n{\r\n    return pStackSlot &gt;= (void *)0x20000000 &amp;&amp; pStackSlot &lt; (void *)0x2001ffff;\r\n}<\/pre>\n<p>Now you should be able to build the project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/07-isvalid.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4439\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/07-isvalid.png\" alt=\"07-isvalid\" width=\"1097\" height=\"784\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/07-isvalid.png 1097w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/07-isvalid-300x214.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/07-isvalid-1024x732.png 1024w\" sizes=\"(max-width: 1097px) 100vw, 1097px\" \/><\/a><\/li>\n<li>Select Analyze -&gt; Analyze Performance with VisualGDB again and\u00a0choose the sampling profiler. Set the sampling rate at 100 times per second:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/08-sample.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4440\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/08-sample.png\" alt=\"08-sample\" width=\"736\" height=\"593\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/08-sample.png 736w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/08-sample-300x242.png 300w\" sizes=\"(max-width: 736px) 100vw, 736px\" \/><\/a><\/li>\n<li>Press OK to begin profiling. VisualGDB will\u00a0capture the\u00a0snapshots of the CPU&#8217;s stack 100 times per second, will unwind them and display them in the Live Profiling window:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/09-idle.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4441\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/09-idle.png\" alt=\"09-idle\" width=\"1097\" height=\"784\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/09-idle.png 1097w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/09-idle-300x214.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/09-idle-1024x732.png 1024w\" sizes=\"(max-width: 1097px) 100vw, 1097px\" \/><\/a>As most of the time both threads are sleeping, the periodic sampling of\u00a0the stack will\u00a0almost exclusively\u00a0show the idle thread. To get more detailed\u00a0performance information, use the instrumenting profiler as shown in <a href=\"https:\/\/visualgdb.com\/tutorials\/arm\/keil\/rtx\/instrumentation\/\">this tutorial<\/a>.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use the VisualGDB&#8217;s sampling profiler to analyze the performance of\u00a0the projects build with the Keil<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[53,147,109,175],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4432"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=4432"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4432\/revisions"}],"predecessor-version":[{"id":4443,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4432\/revisions\/4443"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=4432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=4432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=4432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}