Skip to content

Commit 5288d9d

Browse files
berrangekostyanf14
authored andcommitted
qga: implement a 'guest-get-load' command
Provide a way to report the process load average, via a new 'guest-get-load' command. This is only implemented for POSIX platforms providing 'getloadavg'. Example illustrated with qmp-shell: (QEMU) guest-get-load { "return": { "load15m": 1.546875, "load1m": 1.669921875, "load5m": 1.9306640625 } } Windows has no native equivalent API, but it would be possible to simulate it as illustrated here (BSD-3-Clause): giampaolo/psutil#1485 This is left as an exercise for future contributors. Signed-off-by: Daniel P. Berrangé <[email protected]> Reviewed-by: Konstantin Kostiuk <[email protected]> Message-ID: <[email protected]> Signed-off-by: Konstantin Kostiuk <[email protected]>
1 parent 9ee90cf commit 5288d9d

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,6 +2646,7 @@ config_host_data.set('CONFIG_SETNS', cc.has_function('setns') and cc.has_functio
26462646
config_host_data.set('CONFIG_SYNCFS', cc.has_function('syncfs'))
26472647
config_host_data.set('CONFIG_SYNC_FILE_RANGE', cc.has_function('sync_file_range'))
26482648
config_host_data.set('CONFIG_TIMERFD', cc.has_function('timerfd_create'))
2649+
config_host_data.set('CONFIG_GETLOADAVG', cc.has_function('getloadavg'))
26492650
config_host_data.set('HAVE_COPY_FILE_RANGE', cc.has_function('copy_file_range'))
26502651
config_host_data.set('HAVE_GETIFADDRS', cc.has_function('getifaddrs'))
26512652
config_host_data.set('HAVE_GLIB_WITH_SLICE_ALLOCATOR', glib_has_gslice)

qga/commands-posix.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,3 +1368,23 @@ char *qga_get_host_name(Error **errp)
13681368

13691369
return g_steal_pointer(&hostname);
13701370
}
1371+
1372+
#ifdef CONFIG_GETLOADAVG
1373+
GuestLoadAverage *qmp_guest_get_load(Error **errp)
1374+
{
1375+
double loadavg[3];
1376+
GuestLoadAverage *ret = NULL;
1377+
1378+
if (getloadavg(loadavg, G_N_ELEMENTS(loadavg)) < 0) {
1379+
error_setg_errno(errp, errno,
1380+
"cannot query load average");
1381+
return NULL;
1382+
}
1383+
1384+
ret = g_new0(GuestLoadAverage, 1);
1385+
ret->load1m = loadavg[0];
1386+
ret->load5m = loadavg[1];
1387+
ret->load15m = loadavg[2];
1388+
return ret;
1389+
}
1390+
#endif

qga/qapi-schema.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,43 @@
18431843
'if': 'CONFIG_LINUX'
18441844
}
18451845

1846+
1847+
##
1848+
# @GuestLoadAverage:
1849+
#
1850+
# Statistics about process load information
1851+
#
1852+
# @load1m: 1-minute load avage
1853+
#
1854+
# @load5m: 5-minute load avage
1855+
#
1856+
# @load15m: 15-minute load avage
1857+
#
1858+
# Since: 10.0
1859+
##
1860+
{ 'struct': 'GuestLoadAverage',
1861+
'data': {
1862+
'load1m': 'number',
1863+
'load5m': 'number',
1864+
'load15m': 'number'
1865+
},
1866+
'if': 'CONFIG_GETLOADAVG'
1867+
}
1868+
1869+
##
1870+
# @guest-get-load:
1871+
#
1872+
# Retrieve CPU process load information
1873+
#
1874+
# Returns: load information
1875+
#
1876+
# Since: 10.0
1877+
##
1878+
{ 'command': 'guest-get-load',
1879+
'returns': 'GuestLoadAverage',
1880+
'if': 'CONFIG_GETLOADAVG'
1881+
}
1882+
18461883
##
18471884
# @GuestNetworkRoute:
18481885
#

0 commit comments

Comments
 (0)