{"id":6341,"date":"2016-10-12T15:59:06","date_gmt":"2016-10-12T07:59:06","guid":{"rendered":"http:\/\/rmohan.com\/?p=6341"},"modified":"2016-10-12T15:59:06","modified_gmt":"2016-10-12T07:59:06","slug":"how-do-i-calculate-cpu-in-my-own-libvirt-programs","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6341","title":{"rendered":"How do I calculate %CPU in my own libvirt programs?"},"content":{"rendered":"<h3 class=\"post-title entry-title\">How do I calculate %CPU in my own libvirt programs?<\/h3>\n<p>&nbsp;<\/p>\n<p>Virt-top FAQ ( http:\/\/people.redhat.com\/~rjones\/virt-top\/faq.html#calccpu )<\/p>\n<p>Simple %CPU usage for a domain is calculated by sampling <code><a href=\"http:\/\/www.libvirt.org\/html\/libvirt-libvirt.html#virDomainGetInfo\">virDomainGetInfo<\/a><\/code>periodically and looking at the <code><a href=\"http:\/\/www.libvirt.org\/html\/libvirt-libvirt.html#virDomainInfo\">virDomainInfo<\/a><\/code> cpuTime field. This 64 bit field counts nanoseconds of CPU time used by the domain since the domain booted.<br \/>\nLet <code>t<\/code> be the number of seconds between samples. (Make sure that <code>t<\/code> is measured as accurately as possible, using something like <code>gettimeofday(2)<\/code> to measure the real sampling interval).<br \/>\nLet <code>cpu_time_diff<\/code> be the change in <code>cpuTime<\/code> over this time, which is the number of nanoseconds of CPU time used by the domain, ie:<\/p>\n<blockquote><p><code>cpu_time_diff = cpuTime<sub>now<\/sub> \u2014 cpuTime<sub>t seconds ago<\/sub><\/code><\/p><\/blockquote>\n<p>Let <code>nr_cores<\/code> be the number of processors (cores) on the system. Use <a href=\"http:\/\/www.libvirt.org\/html\/libvirt-libvirt.html#virNodeGetInfo\">virNodeGetInfo<\/a>to get this.<br \/>\nThen, %CPU used by the domain is:<\/p>\n<blockquote><p><code>%CPU = 100 \u00d7 cpu_time_diff \/ (t \u00d7 nr_cores \u00d7 10<sup>9<\/sup>)<\/code><\/p><\/blockquote>\n<p>Because sampling doesn&#8217;t happen instantaneously, this can be greater than 100%. This is particularly a problem where you have many domains and you have to make a<code>virDomainGetInfo<\/code> call for each one.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How do I calculate %CPU in my own libvirt programs? <\/p>\n<p>&nbsp;<\/p>\n<p>Virt-top FAQ ( http:\/\/people.redhat.com\/~rjones\/virt-top\/faq.html#calccpu )<\/p>\n<p>Simple %CPU usage for a domain is calculated by sampling virDomainGetInfoperiodically and looking at the virDomainInfo cpuTime field. This 64 bit field counts nanoseconds of CPU time used by the domain since the domain booted. Let t be the [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6341"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6341"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6341\/revisions"}],"predecessor-version":[{"id":6342,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6341\/revisions\/6342"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}