Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement getloadavg on Windows. Fixes #604 and #1484 #1485

Merged
merged 4 commits into from
Apr 11, 2019

Conversation

ammaraskar
Copy link
Contributor

@ammaraskar ammaraskar commented Apr 11, 2019

(ref. #604 and #1484)

I added some guards for Vista+ since I realized PdhAddEnglishCounterW is not available on XP.

There's a choice between calling init_loadavg_counter on import or on the first getloadavg call. I'm inclined to say the latter is better since then we don't introduce overhead when it isn't needed.

Not sure how to test properly this properly on the windows CI since there's nothing built in we can test against. There might be some external programs we can use but I'd like to get your take on it.

@ammaraskar ammaraskar force-pushed the master branch 2 times, most recently from 9fb91f3 to 0f16f77 Compare April 11, 2019 03:00
Copy link
Owner

@giampaolo giampaolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent code. I'll merge when you'll give me the OK.

psutil/tests/test_linux.py Outdated Show resolved Hide resolved
psutil/tests/test_memory_leaks.py Outdated Show resolved Hide resolved
psutil/tests/test_system.py Show resolved Hide resolved
psutil/arch/windows/wmi.c Show resolved Hide resolved
docs/index.rst Outdated Show resolved Hide resolved
HISTORY.rst Outdated Show resolved Hide resolved
docs/index.rst Outdated Show resolved Hide resolved
psutil/_psutil_windows.c Outdated Show resolved Hide resolved
psutil/_pswindows.py Show resolved Hide resolved
psutil/arch/windows/wmi.c Show resolved Hide resolved
@giampaolo
Copy link
Owner

There's a choice between calling init_loadavg_counter on import or on the first getloadavg call. I'm inclined to say the latter is better since then we don't introduce overhead when it isn't needed.

I agree

Not sure how to test properly this properly on the windows CI since there's nothing built in we can test against. There might be some external programs we can use but I'd like to get your take on it.

Probably there will be something in wmic but I wouldn't bother (too boring =)).

setup.py Show resolved Hide resolved
psutil/tests/__init__.py Outdated Show resolved Hide resolved
@ammaraskar ammaraskar force-pushed the master branch 3 times, most recently from 3bb90fb to 3e82e54 Compare April 11, 2019 05:00
@ammaraskar ammaraskar force-pushed the master branch 4 times, most recently from 08d4ecb to 231ab22 Compare April 11, 2019 06:32
@ammaraskar
Copy link
Contributor Author

I've gone ahead and incorporated the feedback from the code review along with updating install.sh to install a more recent version of python2 (the old one was incompatible with the latest openssl) in order to fix the CI.

@giampaolo giampaolo merged commit 921870d into giampaolo:master Apr 11, 2019
@giampaolo
Copy link
Owner

Nice, thanks Ammar.

@vstinner
Copy link

Cool stuff 👍

@vstinner
Copy link

Python also got its toy: https://github.com/python/cpython/pull/8357/files

@giampaolo
Copy link
Owner

Cool stuff indeed. I had been floating around this for a while back in the days (the original proposal is old). There was/is a bunch of info on internet mentioning the bits with which it's theoretically possible to do this ("System Processor Queue Length") but I couldn't find any real implementation (e.g. knowing how to calculate LOADAVG_FACTOR_1F magic numbers is non trivial and requires some actual understanding of how this works).

nlevitt added a commit to nlevitt/psutil that referenced this pull request May 6, 2019
* origin/master:
  Fix giampaolo#1276: [AIX] use getargs to get process cmdline (giampaolo#1500) (patch by @wiggin15)
  Fix Process.ionice example using wrong keyword arg (giampaolo#1504)
  fix history syntax
  remove catching IOError; let the test fail and adjust it later
  Fix cpu freq (giampaolo#1496)
  pre release
  fix giampaolo#1493: [Linux] cpu_freq(): handle the case where /sys/devices/system/cpu/cpufreq/ exists but is empty.
  Revert "Fix cpu_freq (giampaolo#1493)" (giampaolo#1495)
  Fix cpu_freq (giampaolo#1493)
  Update cpu_freq to return 0 for max/min if not available (giampaolo#1487)
  give CREDITS to @agnewee for giampaolo#1491
  SunOS / net_if_addrs(): free() ifap struct on error (giampaolo#1491)
  fix giampaolo#1486: add wraps() decorator around wrap_exceptions
  refactor/move some utilities into _common.py
  update doc
  update HISTORY
  Implement getloadavg on Windows. Fixes giampaolo#604 and giampaolo#1484 (giampaolo#1485) (patch by Ammar Askar)
  give credits to @amanusk for giampaolo#1472
pbo-linaro pushed a commit to pbo-linaro/qemu-ci that referenced this pull request Dec 2, 2024
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é <berrange@redhat.com>
pbo-linaro pushed a commit to pbo-linaro/qemu-ci that referenced this pull request Dec 2, 2024
https://lore.kernel.org/qemu-devel/20241202121927.864335-1-berrange@redhat.com

---

From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= <berrange@redhat.com>,
 Paolo Bonzini <pbonzini@redhat.com>,
 Konstantin Kostiuk <kkostiuk@redhat.com>,
 =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= <philmd@linaro.org>,
 Michael Roth <michael.roth@amd.com>,
 =?UTF-8?q?Jo=C3=A3o=20Vila=C3=A7a?= <jvilaca@redhat.com>,
 Eric Blake <eblake@redhat.com>, Markus Armbruster <armbru@redhat.com>,
 =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
Subject: [PATCH] qga: implement a 'guest-get-load' command
Date: Mon,  2 Dec 2024 12:19:27 +0000
Message-ID: <20241202121927.864335-1-berrange@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12
Received-SPF: pass client-ip=170.10.133.124; envelope-from=berrange@redhat.com;
 helo=us-smtp-delivery-124.mimecast.com
X-Spam_score_int: -50
X-Spam_score: -5.1
X-Spam_bar: -----
X-Spam_report: (-5.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.996,
 DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
 RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001,
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001,
 SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-BeenThere: qemu-devel@nongnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: <qemu-devel.nongnu.org>
List-Unsubscribe: <https://lists.nongnu.org/mailman/options/qemu-devel>,
 <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>
List-Archive: <https://lists.nongnu.org/archive/html/qemu-devel>
List-Post: <mailto:qemu-devel@nongnu.org>
List-Help: <mailto:qemu-devel-request@nongnu.org?subject=help>
List-Subscribe: <https://lists.nongnu.org/mailman/listinfo/qemu-devel>,
 <mailto:qemu-devel-request@nongnu.org?subject=subscribe>
Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org
Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org

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é <berrange@redhat.com>
---
 meson.build          |  1 +
 qga/commands-posix.c | 20 ++++++++++++++++++++
 qga/qapi-schema.json | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+)

diff --git a/meson.build b/meson.build
index a290dbfa33..9c65e56fff 100644
--- a/meson.build
+++ b/meson.build
@@ -2619,6 +2619,7 @@ config_host_data.set('CONFIG_SETNS', cc.has_function('setns') and cc.has_functio
 config_host_data.set('CONFIG_SYNCFS', cc.has_function('syncfs'))
 config_host_data.set('CONFIG_SYNC_FILE_RANGE', cc.has_function('sync_file_range'))
 config_host_data.set('CONFIG_TIMERFD', cc.has_function('timerfd_create'))
+config_host_data.set('CONFIG_GETLOADAVG', cc.has_function('getloadavg'))
 config_host_data.set('HAVE_COPY_FILE_RANGE', cc.has_function('copy_file_range'))
 config_host_data.set('HAVE_GETIFADDRS', cc.has_function('getifaddrs'))
 config_host_data.set('HAVE_GLIB_WITH_SLICE_ALLOCATOR', glib_has_gslice)
diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index 636307bedf..6e3c15f539 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -1368,3 +1368,23 @@ char *qga_get_host_name(Error **errp)

     return g_steal_pointer(&hostname);
 }
+
+#ifdef CONFIG_GETLOADAVG
+GuestLoadAverage *qmp_guest_get_load(Error **errp)
+{
+    double loadavg[3];
+    GuestLoadAverage *ret = NULL;
+
+    if (getloadavg(loadavg, G_N_ELEMENTS(loadavg)) < 0) {
+        error_setg_errno(errp, errno,
+                         "cannot query load average");
+        return NULL;
+    }
+
+    ret = g_new0(GuestLoadAverage, 1);
+    ret->load1m = loadavg[0];
+    ret->load5m = loadavg[1];
+    ret->load15m = loadavg[2];
+    return ret;
+}
+#endif
diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json
index 0537bb7886..995594aaf4 100644
--- a/qga/qapi-schema.json
+++ b/qga/qapi-schema.json
@@ -1843,6 +1843,43 @@
   'if': 'CONFIG_LINUX'
 }

+
+##
+# @GuestLoadAverage:
+#
+# Statistics about process load information
+#
+# @Load1m: 1-minute load avage
+#
+# @load5m: 5-minute load avage
+#
+# @load15m: 15-minute load avage
+#
+# Since: 10.0
+##
+{ 'struct': 'GuestLoadAverage',
+  'data': {
+      'load1m': 'number',
+      'load5m': 'number',
+      'load15m': 'number'
+  },
+  'if': 'CONFIG_GETLOADAVG'
+}
+
+##
+# @guest-get-load:
+#
+# Retrieve CPU process load information
+#
+# Returns: load information
+#
+# Since: 10.0
+##
+{ 'command': 'guest-get-load',
+  'returns': 'GuestLoadAverage',
+  'if': 'CONFIG_GETLOADAVG'
+}
+
 ##
 # @GuestNetworkRoute:
 #
--
2.46.0

Signed-off-by: GitHub Actions Bot <bot@github.com>
kostyanf14 pushed a commit to kostyanf14/qemu that referenced this pull request Jan 6, 2025
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é <berrange@redhat.com>
Reviewed-by: Konstantin Kostiuk <kkostiuk@redhat.com>
Message-ID: <20241202121927.864335-1-berrange@redhat.com>
Signed-off-by: Konstantin Kostiuk <kkostiuk@redhat.com>
pbo-linaro pushed a commit to pbo-linaro/qemu-ci that referenced this pull request Jan 6, 2025
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é <berrange@redhat.com>
Reviewed-by: Konstantin Kostiuk <kkostiuk@redhat.com>
Message-ID: <20241202121927.864335-1-berrange@redhat.com>
Signed-off-by: Konstantin Kostiuk <kkostiuk@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants