{"id":4418,"date":"2018-11-19T16:39:54","date_gmt":"2018-11-20T00:39:54","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=4418"},"modified":"2018-11-19T17:03:58","modified_gmt":"2018-11-20T01:03:58","slug":"profiling-rtx-based-projects-with-the-instrumenting-profiler","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/keil\/rtx\/instrumentation\/","title":{"rendered":"Profiling RTX-based Projects with the Instrumenting Profiler"},"content":{"rendered":"<p>This tutorial shows how to profile the RTX-based projects built\u00a0with the Keil compiler using the\u00a0VisualGDB&#8217;s instrumenting profiler. Before you begin, install VisualGDB 5.4\u00a0Preview 10 or later and create a basic RTX-based project by following <a href=\"https:\/\/visualgdb.com\/tutorials\/arm\/keil\/rtx\/\">this tutorial<\/a>.<\/p>\n<p>The instrumenting profiler works by patching\u00a0some, or all of the\u00a0functions in your program so that they report their run times to VisualGDB.\u00a0This provides more accurate and detailed results compared to the sampling profiler, but also results in higher run-time overhead, as the run time of each instrumented function\u00a0needs to be measured separately.<\/p>\n<ol>\n<li>Open the project in Visual Studio and select Analyze-&gt;Analyze Performance with VisualGDB: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/01-analyze.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4420\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/01-analyze.png\" alt=\"01-analyze\" width=\"1097\" height=\"784\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/01-analyze.png 1097w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/01-analyze-300x214.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/01-analyze-1024x732.png 1024w\" sizes=\"(max-width: 1097px) 100vw, 1097px\" \/><\/a><\/li>\n<li>If you have not\u00a0profiled this project before, VisualGDB will suggest automatically referencing the Embedded Profiler framework. Proceed by\u00a0pressing &#8220;Yes&#8221;:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/02-addref.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4421\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/02-addref.png\" alt=\"02-addref\" width=\"1097\" height=\"784\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/02-addref.png 1097w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/02-addref-300x214.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/02-addref-1024x732.png 1024w\" sizes=\"(max-width: 1097px) 100vw, 1097px\" \/><\/a><\/li>\n<li>Open VisualGDB Project Properties and go to the Embedded Frameworks page. As configuring the sampling profiler requires extra steps, we will disable it in this tutorial by checking &#8220;Exclude sampling profiler code&#8221;. Also set RTOS support to &#8220;Keil RTX&#8221;:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/04-profiler.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4423\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/04-profiler.png\" alt=\"04-profiler\" width=\"890\" height=\"652\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/04-profiler.png 890w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/04-profiler-300x220.png 300w\" sizes=\"(max-width: 890px) 100vw, 890px\" \/><\/a><\/li>\n<li>Open the main file of your project, include the <strong>&lt;SysprogsProfiler.h&gt;<\/strong> file and call <strong>InitializeInstrumentingProfiler()<\/strong> from the <strong>main()<\/strong> function after you configure the system clock:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/06-built1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4419\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/06-built1.png\" alt=\"06-built\" width=\"1097\" height=\"784\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/06-built1.png 1097w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/06-built1-300x214.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/06-built1-1024x732.png 1024w\" sizes=\"(max-width: 1097px) 100vw, 1097px\" \/><\/a><\/li>\n<li>Depending on the version of RTX you are using, you may encounter a &#8220;missing<strong> thread_switch_helper()<\/strong>&#8221; error. If this happens, follow the instructions in <a href=\"https:\/\/visualgdb.com\/tutorials\/arm\/keil\/rtx\/tracing\/\">this tutorial<\/a> to patch the\u00a0RTX\u00a0interrupt\u00a0handler so that VisualGDB will be able to profile\u00a0low-level RTX functions.<\/li>\n<li>Click <strong>Analyze-&gt;Analyze Performance with VisualGDB<\/strong> again, then click &#8220;Enable relocation record generation&#8221;:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/07-reloc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4424\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/07-reloc.png\" alt=\"07-reloc\" width=\"1097\" height=\"784\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/07-reloc.png 1097w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/07-reloc-300x214.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/07-reloc-1024x732.png 1024w\" sizes=\"(max-width: 1097px) 100vw, 1097px\" \/><\/a><\/li>\n<li>Click\u00a0&#8220;Analyze Performance with VisualGDB&#8221; again. Now you will be able to select the functions you would like to profile (VisualGDB will automatically exclude the functions that cannot be profiled). Click &#8220;OK&#8221; to begin profiling:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/08-instrument.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4425\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/08-instrument.png\" alt=\"08-instrument\" width=\"790\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/08-instrument.png 790w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/08-instrument-300x245.png 300w\" sizes=\"(max-width: 790px) 100vw, 790px\" \/><\/a><\/li>\n<li>If you are using the default RTX configuration, the initial profiling session will result in an error:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/09-error.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4426\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/09-error.png\" alt=\"09-error\" width=\"1097\" height=\"784\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/09-error.png 1097w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/09-error-300x214.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/09-error-1024x732.png 1024w\" sizes=\"(max-width: 1097px) 100vw, 1097px\" \/><\/a><\/li>\n<li>Stopping the program in debugger will quickly reveal that RTX has detected a stack underflow:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/10-stack.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4427\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/10-stack.png\" alt=\"10-stack\" width=\"1097\" height=\"784\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/10-stack.png 1097w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/10-stack-300x214.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/10-stack-1024x732.png 1024w\" sizes=\"(max-width: 1097px) 100vw, 1097px\" \/><\/a><\/li>\n<li>This can be easily fixed by increasing the RTX dynamic memory size and then\u00a0increasing the sizes of each thread you would like to profile (VisualGDB automatically excludes the idle thread). Add &#8220;OS_DYNAMIC_MEM_SIZE=16384&#8221; to the preprocessor macros and\u00a0set the &#8220;.stack_size = 4096&#8221; attribute in the <strong>thread1_attr<\/strong> and <strong>thread2_attr<\/strong> structures:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/11-memsize.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4428\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/11-memsize.png\" alt=\"11-memsize\" width=\"890\" height=\"652\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/11-memsize.png 890w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/11-memsize-300x220.png 300w\" sizes=\"(max-width: 890px) 100vw, 890px\" \/><\/a><\/li>\n<li>Build the project and start another\u00a0profiling session. VisualGDB will now show\u00a0the real-time call tree for each of the threads, including the number of calls and the time spent in each of the called functions:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/12-profilerdata.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4429\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/12-profilerdata.png\" alt=\"12-profilerdata\" width=\"1097\" height=\"784\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/12-profilerdata.png 1097w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/12-profilerdata-300x214.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/12-profilerdata-1024x732.png 1024w\" sizes=\"(max-width: 1097px) 100vw, 1097px\" \/><\/a><\/li>\n<li>You can\u00a0analyze the stack overhead of the VisualGDB&#8217;s profiler by setting a breakpoint in the WriteRawFastSemihostingData() function and using the\u00a0debug stack view in Embedded Memory Explorer:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/13-stackoverhead.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4430\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/13-stackoverhead.png\" alt=\"13-stackoverhead\" width=\"1097\" height=\"784\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/13-stackoverhead.png 1097w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/13-stackoverhead-300x214.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2018\/11\/13-stackoverhead-1024x732.png 1024w\" sizes=\"(max-width: 1097px) 100vw, 1097px\" \/><\/a>Note that\u00a0this will only show the overhead (size required by the\u00a0profiler\u00a0framework functions), as the gdb debugger won&#8217;t be able to unwind the stack past the <strong>ReturnHook()<\/strong> function and show the actual stack.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to profile the RTX-based projects built\u00a0with the Keil compiler using the\u00a0VisualGDB&#8217;s instrumenting profiler. Before you begin,<\/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\/4418"}],"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=4418"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4418\/revisions"}],"predecessor-version":[{"id":4431,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4418\/revisions\/4431"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=4418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=4418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=4418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}