天天看點

如何實時監測程序排程累計的runtime

構想

程序的排程資料可通過proc檔案系統檢視,/prod/${pid}/sched中的參數,對性能優化來說很有參考意義,比如1号程序的資料如下:

systemd (1, #threads: 1)
-------------------------------------------------------------------
se.exec_start                                :     269493519.475163  #最近被排程到開始執行時間,ns
se.vruntime                                  :           939.800291  #虛拟運作時間
se.sum_exec_runtime                          :          4193.962960  #程序實際累積運作實體時間, ms
se.nr_migrations                             :                 1303
nr_switches                                  :                12433
nr_voluntary_switches                        :                11709
nr_involuntary_switches                      :                  724
se.load.weight                               :              1048576
se.avg.load_sum                              :                  116
se.avg.runnable_sum                          :               118784
se.avg.util_sum                              :               118784
se.avg.load_avg                              :                    0
se.avg.runnable_avg                          :                    0
se.avg.util_avg                              :                    0
se.avg.last_update_time                      :      269493417332736
se.avg.util_est.ewma                         :                   12
se.avg.util_est.enqueued                     :                    1
policy                                       :                    0
prio                                         :                  120
clock-delta                                  :                    9
mm->numa_scan_seq                            :                    0
numa_pages_migrated                          :                    0
numa_preferred_nid                           :                   -1
total_numa_faults                            :                    0
current_node=0, numa_group_id=0
numa_faults node=0 task_private=0 task_shared=0 group_private=0 group_shared=0
           

複制

我現在想寫個腳本,可以實時顯示指定程序累積運作實體時間(sum_exec_runtime)。

代碼

通過python代碼實作:

#!/usr/bin/env python3

import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

vcpu_thread1 = sys.argv[1]
vcpu_thread2 = sys.argv[2]
keywords = "sum_exec_runtime"
time_interval = 1000

xtime = 0
xdata = []
ydata1, ydata2 = [], []

#擷取指定pid的sum_exec_runtime值
def read_sum_exec_runtime(pid,keyword):
    runtime = 0
    with open('/proc/'+str(pid)+'/sched') as procf:
        for line in procf.readlines():
            if keyword in line:
                runtime = float((line.split(':')[1]).strip())
    #print("pid:", pid, "runtime:", "%.2f" % runtime)
    return runtime

#繪圖重新整理函數
def animate(i):
    global xtime
    xtime += time_interval/1000
    xdata.append(xtime)
    ydata1.append(read_sum_exec_runtime(vcpu_thread1, keywords))
    ydata2.append(read_sum_exec_runtime(vcpu_thread2, keywords))
    plt.cla()
    plt.title(keywords)
    plt.xlabel("time(s)")
    plt.ylabel("runtime(ms)")
    plt.plot(xdata, ydata1, marker='x', label=vcpu_thread1)
    plt.plot(xdata, ydata2, marker='o', label=vcpu_thread2)
    plt.legend()

anim = FuncAnimation(plt.figure(), animate, frames=None, interval=time_interval)
#plt.show()
anim.save('runtime.gif', writer='imagemagick', fps=60)
           

複制

測試

啟動一個qemu虛拟機,兩個vcpu(兩個VCPU線程id分别是241255,241266),然後跑上面的程式:

./runtime.py 241255 241266

結果如下圖所示(隻測量了100s左右,在qemu虛拟機裡跑了兩個純耗CPU時間的任務),可以看到vcpu線程的runtime直線上升。

如何實時監測程式排程累計的runtime

在虛拟機裡隻跑一個cpu消耗性任務,其曲線如下:

如何實時監測程式排程累計的runtime