mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-28 11:31:16 +00:00
util: Extend virProcessGetStatInfo() for sysTime and userTime
The virProcessGetStatInfo() helper parses /proc stat file for given PID and/or TID and reports cumulative cpuTime which is just a sum of user and sys times. But in near future, we'll need those times separately, so make the function return them too (if caller desires). Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
parent
b59285e089
commit
cdc22d9a21
@ -1075,6 +1075,7 @@ chDomainHelperGetVcpus(virDomainObj *vm,
|
|||||||
vcpuinfo->number = i;
|
vcpuinfo->number = i;
|
||||||
vcpuinfo->state = VIR_VCPU_RUNNING;
|
vcpuinfo->state = VIR_VCPU_RUNNING;
|
||||||
if (virProcessGetStatInfo(&vcpuinfo->cpuTime,
|
if (virProcessGetStatInfo(&vcpuinfo->cpuTime,
|
||||||
|
NULL, NULL,
|
||||||
&vcpuinfo->cpu, NULL,
|
&vcpuinfo->cpu, NULL,
|
||||||
vm->pid, vcpupid) < 0) {
|
vm->pid, vcpupid) < 0) {
|
||||||
virReportSystemError(errno, "%s",
|
virReportSystemError(errno, "%s",
|
||||||
|
@ -1360,6 +1360,7 @@ qemuDomainHelperGetVcpus(virDomainObj *vm,
|
|||||||
vcpuinfo->state = VIR_VCPU_RUNNING;
|
vcpuinfo->state = VIR_VCPU_RUNNING;
|
||||||
|
|
||||||
if (virProcessGetStatInfo(&vcpuinfo->cpuTime,
|
if (virProcessGetStatInfo(&vcpuinfo->cpuTime,
|
||||||
|
NULL, NULL,
|
||||||
&vcpuinfo->cpu, NULL,
|
&vcpuinfo->cpu, NULL,
|
||||||
vm->pid, vcpupid) < 0) {
|
vm->pid, vcpupid) < 0) {
|
||||||
virReportSystemError(errno, "%s",
|
virReportSystemError(errno, "%s",
|
||||||
@ -2516,6 +2517,7 @@ qemuDomainGetInfo(virDomainPtr dom,
|
|||||||
|
|
||||||
if (virDomainObjIsActive(vm)) {
|
if (virDomainObjIsActive(vm)) {
|
||||||
if (virProcessGetStatInfo(&(info->cpuTime), NULL, NULL,
|
if (virProcessGetStatInfo(&(info->cpuTime), NULL, NULL,
|
||||||
|
NULL, NULL,
|
||||||
vm->pid, 0) < 0) {
|
vm->pid, 0) < 0) {
|
||||||
virReportError(VIR_ERR_OPERATION_FAILED, "%s",
|
virReportError(VIR_ERR_OPERATION_FAILED, "%s",
|
||||||
_("cannot read cputime for domain"));
|
_("cannot read cputime for domain"));
|
||||||
@ -10676,7 +10678,7 @@ qemuDomainMemoryStatsInternal(virDomainObj *vm,
|
|||||||
ret = 0;
|
ret = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (virProcessGetStatInfo(NULL, NULL, &rss, vm->pid, 0) < 0) {
|
if (virProcessGetStatInfo(NULL, NULL, NULL, NULL, &rss, vm->pid, 0) < 0) {
|
||||||
virReportError(VIR_ERR_OPERATION_FAILED, "%s",
|
virReportError(VIR_ERR_OPERATION_FAILED, "%s",
|
||||||
_("cannot get RSS for domain"));
|
_("cannot get RSS for domain"));
|
||||||
} else {
|
} else {
|
||||||
|
@ -1737,32 +1737,37 @@ virProcessGetStat(pid_t pid,
|
|||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
int
|
int
|
||||||
virProcessGetStatInfo(unsigned long long *cpuTime,
|
virProcessGetStatInfo(unsigned long long *cpuTime,
|
||||||
|
unsigned long long *userTime,
|
||||||
|
unsigned long long *sysTime,
|
||||||
int *lastCpu,
|
int *lastCpu,
|
||||||
long *vm_rss,
|
long *vm_rss,
|
||||||
pid_t pid,
|
pid_t pid,
|
||||||
pid_t tid)
|
pid_t tid)
|
||||||
{
|
{
|
||||||
g_auto(GStrv) proc_stat = virProcessGetStat(pid, tid);
|
g_auto(GStrv) proc_stat = virProcessGetStat(pid, tid);
|
||||||
unsigned long long usertime = 0, systime = 0;
|
unsigned long long utime = 0;
|
||||||
|
unsigned long long stime = 0;
|
||||||
|
const unsigned long long jiff2nsec = 1000ull * 1000ull * 1000ull /
|
||||||
|
(unsigned long long) sysconf(_SC_CLK_TCK);
|
||||||
long rss = 0;
|
long rss = 0;
|
||||||
int cpu = 0;
|
int cpu = 0;
|
||||||
|
|
||||||
if (!proc_stat ||
|
if (!proc_stat ||
|
||||||
virStrToLong_ullp(proc_stat[VIR_PROCESS_STAT_UTIME], NULL, 10, &usertime) < 0 ||
|
virStrToLong_ullp(proc_stat[VIR_PROCESS_STAT_UTIME], NULL, 10, &utime) < 0 ||
|
||||||
virStrToLong_ullp(proc_stat[VIR_PROCESS_STAT_STIME], NULL, 10, &systime) < 0 ||
|
virStrToLong_ullp(proc_stat[VIR_PROCESS_STAT_STIME], NULL, 10, &stime) < 0 ||
|
||||||
virStrToLong_l(proc_stat[VIR_PROCESS_STAT_RSS], NULL, 10, &rss) < 0 ||
|
virStrToLong_l(proc_stat[VIR_PROCESS_STAT_RSS], NULL, 10, &rss) < 0 ||
|
||||||
virStrToLong_i(proc_stat[VIR_PROCESS_STAT_PROCESSOR], NULL, 10, &cpu) < 0) {
|
virStrToLong_i(proc_stat[VIR_PROCESS_STAT_PROCESSOR], NULL, 10, &cpu) < 0) {
|
||||||
VIR_WARN("cannot parse process status data");
|
VIR_WARN("cannot parse process status data");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We got jiffies
|
utime *= jiff2nsec;
|
||||||
* We want nanoseconds
|
stime *= jiff2nsec;
|
||||||
* _SC_CLK_TCK is jiffies per second
|
|
||||||
* So calculate thus....
|
|
||||||
*/
|
|
||||||
if (cpuTime)
|
if (cpuTime)
|
||||||
*cpuTime = 1000ull * 1000ull * 1000ull * (usertime + systime)
|
*cpuTime = utime + stime;
|
||||||
/ (unsigned long long) sysconf(_SC_CLK_TCK);
|
if (userTime)
|
||||||
|
*userTime = utime;
|
||||||
|
if (sysTime)
|
||||||
|
*sysTime = stime;
|
||||||
if (lastCpu)
|
if (lastCpu)
|
||||||
*lastCpu = cpu;
|
*lastCpu = cpu;
|
||||||
|
|
||||||
@ -1771,7 +1776,7 @@ virProcessGetStatInfo(unsigned long long *cpuTime,
|
|||||||
|
|
||||||
|
|
||||||
VIR_DEBUG("Got status for %d/%d user=%llu sys=%llu cpu=%d rss=%ld",
|
VIR_DEBUG("Got status for %d/%d user=%llu sys=%llu cpu=%d rss=%ld",
|
||||||
(int) pid, tid, usertime, systime, cpu, rss);
|
(int) pid, tid, utime, stime, cpu, rss);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -1844,6 +1849,8 @@ virProcessGetSchedInfo(unsigned long long *cpuWait,
|
|||||||
#else
|
#else
|
||||||
int
|
int
|
||||||
virProcessGetStatInfo(unsigned long long *cpuTime,
|
virProcessGetStatInfo(unsigned long long *cpuTime,
|
||||||
|
unsigned long long *userTime,
|
||||||
|
unsigned long long *sysTime,
|
||||||
int *lastCpu,
|
int *lastCpu,
|
||||||
long *vm_rss,
|
long *vm_rss,
|
||||||
pid_t pid G_GNUC_UNUSED,
|
pid_t pid G_GNUC_UNUSED,
|
||||||
@ -1853,6 +1860,10 @@ virProcessGetStatInfo(unsigned long long *cpuTime,
|
|||||||
* platforms, so just report neutral values */
|
* platforms, so just report neutral values */
|
||||||
if (cpuTime)
|
if (cpuTime)
|
||||||
*cpuTime = 0;
|
*cpuTime = 0;
|
||||||
|
if (userTime)
|
||||||
|
*userTime = 0;
|
||||||
|
if (sysTime)
|
||||||
|
*sysTime = 0;
|
||||||
if (lastCpu)
|
if (lastCpu)
|
||||||
*lastCpu = 0;
|
*lastCpu = 0;
|
||||||
if (vm_rss)
|
if (vm_rss)
|
||||||
|
@ -195,6 +195,8 @@ typedef enum {
|
|||||||
int virProcessNamespaceAvailable(unsigned int ns);
|
int virProcessNamespaceAvailable(unsigned int ns);
|
||||||
|
|
||||||
int virProcessGetStatInfo(unsigned long long *cpuTime,
|
int virProcessGetStatInfo(unsigned long long *cpuTime,
|
||||||
|
unsigned long long *userTime,
|
||||||
|
unsigned long long *sysTime,
|
||||||
int *lastCpu,
|
int *lastCpu,
|
||||||
long *vm_rss,
|
long *vm_rss,
|
||||||
pid_t pid,
|
pid_t pid,
|
||||||
|
Loading…
Reference in New Issue
Block a user