Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Win32: Improve last commit.

  • Loading branch information...
commit 01e89c21f21e1b869d57661b8662b177163f6b47 1 parent 96390f2
@tnodir authored
Showing with 19 additions and 15 deletions.
  1. +19 −15 src/win32/getloadavg.c
View
34 src/win32/getloadavg.c
@@ -4,13 +4,11 @@
#define filetime_int64(ft) ((int64_t) ((LARGE_INTEGER *) &(ft))->QuadPart)
-struct win32_times {
- FILETIME idle;
- FILETIME kernel;
- FILETIME user;
-};
-
-static struct win32_times g_OldTimes;
+static struct {
+ int64_t idle;
+ int64_t kernel;
+ int64_t user;
+} g_OldTimes;
static int
@@ -20,17 +18,23 @@ getloadavg (double *loadavg)
EnterCriticalSection(&g_CritSect);
{
- struct win32_times times;
+ FILETIME idleTime, kernelTime, userTime;
+
+ if (GetSystemTimes(&idleTime, &kernelTime, &userTime)) {
+ const int64_t idle = filetime_int64(idleTime);
+ const int64_t kernel = filetime_int64(kernelTime);
+ const int64_t user = filetime_int64(userTime);
+
+ const int64_t idle_delta = idle - g_OldTimes.idle;
+ const int64_t kernel_delta = kernel - g_OldTimes.kernel;
+ const int64_t user_delta = user - g_OldTimes.user;
- if (GetSystemTimes(&times.idle, &times.kernel, &times.user)) {
- const int64_t usr = filetime_int64(times.user) - filetime_int64(g_OldTimes.user);
- const int64_t kerl = filetime_int64(times.kernel) - filetime_int64(g_OldTimes.kernel);
- const int64_t idl = filetime_int64(times.idle) - filetime_int64(g_OldTimes.idle);
- const int64_t sys = kerl + usr;
+ *loadavg = 1.0 - (double) idle_delta / (kernel_delta + user_delta);
- *loadavg = 1.0 - (double) idl / sys;
+ g_OldTimes.idle = idle;
+ g_OldTimes.kernel = kernel;
+ g_OldTimes.user = user;
- g_OldTimes = times;
res = 0;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.