LoginSignup
1
2

More than 5 years have passed since last update.

libvirtでドメインのCPU使用率を計算する

Last updated at Posted at 2015-09-11

How do I calculate %CPU in my own libvirt programs?

そういえばQiitaは$\LaTeX$記法が使えるらしい。

libvirtにはドメインのCPU使用率を直接取得するメソッドが無いのだが、上記の記事によるとCPUの使用時間から使用率を計算できるようだ。以下はlibvirt-pythonの話。

ドキュメントが整備されつつある。
http://libvirt.org/docs/libvirt-appdev-guide-python/en-US/html/index.html

まずはlibvirtdに接続する。

import libvirt

con = libvirt.openReadOnly()
dom0 = con.listAllDomains()[0]

dom0がドメイン。

dom0.info()でドメインが起動してからCPUを何秒使ったかをナノ秒単位で取得できる($cpuTime$)。データ構造はvirDomainInfo。5番目の値。

cpuTime = dom0.info()[4]

これを$t$秒ごとに取得したとすると、ドメインのCPU使用率は

\frac{cpuTime_{now} - cpuTime_{now-t}}{t \times nrCores \times 10^9}\times 100

$nrCores$はシステムの物理コア数を示し、con.getInfo()で取得できる(virNodeInfo)。

コアごとのCPU使用率を知りたい場合には、dom0.vcpus()を使うとコアごとのCPU使用時間を取得できる(virVcpuInfo)。この場合、$nrCores$は1、またはハイパースレッドが効いている場合には2である。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2