{"id":761,"date":"2024-08-27T18:11:36","date_gmt":"2024-08-27T16:11:36","guid":{"rendered":"https:\/\/cammonte.com\/?page_id=761"},"modified":"2024-08-27T18:11:36","modified_gmt":"2024-08-27T16:11:36","slug":"cpu-profiling","status":"publish","type":"page","link":"https:\/\/cammonte.com\/index.php\/cpu-profiling\/","title":{"rendered":"CPU Profiling"},"content":{"rendered":"\n<p>Analysing and measuring the performance of a CPU while it is executing a program<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Understand how efficiently a program uses the CPU<\/li>\n\n\n\n<li>Identify performance bottlenecks<\/li>\n\n\n\n<li>Optimise the program to run faster and more efficiently<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Metrics<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>CPU usage<\/strong>: percentage of CPU capacity being used by the program, low usage might mean the program is waiting for other resources (I\/O, &#8230;)<\/li>\n\n\n\n<li><strong>Execution time<\/strong>: total time taken by the CPU to execute a program or a specific function within the program<\/li>\n\n\n\n<li><strong>Call graph<\/strong>: visual representation of function calls made during the execution<\/li>\n\n\n\n<li><strong>CPU cycles<\/strong>: number of clock cycles the CPU spends executing a particular section of code, high cycle counts = inefficiencies (complex calculations or frequent memory accesses)<\/li>\n\n\n\n<li><strong>Instruction count<\/strong>: number of instructions executed by the CPU, aim to reduce it<\/li>\n\n\n\n<li><strong>Cache misses<\/strong>: when the CPU cannot find the required data in its cache and must fetch it from slower main memory<\/li>\n\n\n\n<li><strong>Thread performance<\/strong>: how well the CPU manages and executes multiple threads<\/li>\n\n\n\n<li><strong>Context switching<\/strong>: overhead when the CPU switches from executing one process or thread to another<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Steps<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Determine which part of the program you want to profile: entire application, specific function, section of code.<\/li>\n\n\n\n<li>Use a profiling tool to run the program while collecting performance data<\/li>\n\n\n\n<li>Examine the collected data to identify performance bottlenecks: functions that consume a lot of CPU time, have high instruction counts, or cause frequent cache misses<\/li>\n\n\n\n<li>Make changes to the code to improve performance: rewriting inefficient algorithms, optimising loops, reducing function calls, better managing memory<\/li>\n\n\n\n<li>Re-profile and Iterate<\/li>\n<\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">Tools<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/ftp.gnu.org\/old-gnu\/Manuals\/gprof-2.9.1\/html_mono\/gprof.html\">gprof<\/a><\/strong>: Unix-like systems<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Visual Studio Profiler<\/strong>: integrated into Microsoft Visual Studio<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/perf.wiki.kernel.org\/index.php\/Tutorial\">perf<\/a><\/strong>: Linux tool for profiling CPU performance<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Intel VTune Profiler<\/strong><\/li>\n<\/ul>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">TODO: DETAIL USAGE OF gprof, perf and VSProfiler<\/mark><\/strong><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Analysing and measuring the performance of a CPU while it is executing a program Metrics Steps Tools TODO: DETAIL USAGE OF gprof, perf and VSProfiler<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"ub_ctt_via":"","site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"class_list":["post-761","page","type-page","status-publish","hentry"],"featured_image_src":null,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cammonte.com\/index.php\/wp-json\/wp\/v2\/pages\/761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cammonte.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cammonte.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cammonte.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cammonte.com\/index.php\/wp-json\/wp\/v2\/comments?post=761"}],"version-history":[{"count":10,"href":"https:\/\/cammonte.com\/index.php\/wp-json\/wp\/v2\/pages\/761\/revisions"}],"predecessor-version":[{"id":772,"href":"https:\/\/cammonte.com\/index.php\/wp-json\/wp\/v2\/pages\/761\/revisions\/772"}],"wp:attachment":[{"href":"https:\/\/cammonte.com\/index.php\/wp-json\/wp\/v2\/media?parent=761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}