os: implement loadavg (not working on cygwin/win)

This commit is contained in:
Fedor Indutny 2011-10-02 00:48:42 +07:00 committed by Ben Noordhuis
parent 33cb8775bc
commit a35591bbfc
11 changed files with 107 additions and 0 deletions

View File

@ -1044,6 +1044,9 @@ struct uv_fs_event_s {
};
/* Gets load avg */
void uv_loadavg(double avg[3]);
/*
* If filename is a directory then we will watch for all events in that
* directory. If filename is a file - we will only get events from that

View File

@ -36,6 +36,11 @@ uint64_t uv_hrtime() {
return (ts.tv_sec * NANOSEC + ts.tv_nsec);
}
void uv_loadavg(double avg[3]) {
/* Unsupported as of cygwin 1.7.7 */
avg[0] = avg[1] = avg[2] = 0;
}
int uv_exepath(char* buffer, size_t* size) {
uint32_t usize;

View File

@ -30,6 +30,9 @@
#include <mach/mach_time.h>
#include <mach-o/dyld.h> /* _NSGetExecutablePath */
#include <sys/resource.h>
#include <sys/sysctl.h>
uint64_t uv_hrtime() {
uint64_t time;
@ -92,6 +95,17 @@ double uv_get_total_memory(void) {
return (double) info;
}
void uv_loadavg(double avg[3]) {
struct loadavg info;
size_t size = sizeof(info);
int which[] = {CTL_VM, VM_LOADAVG};
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) return;
avg[0] = (double) info.ldavg[0] / info.fscale;
avg[1] = (double) info.ldavg[1] / info.fscale;
avg[2] = (double) info.ldavg[2] / info.fscale;
}
int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,

View File

@ -25,6 +25,7 @@
#include <errno.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <time.h>
@ -92,6 +93,17 @@ double uv_get_total_memory(void) {
return (double) info;
}
void uv_loadavg(double avg[3]) {
struct loadavg info;
size_t size = sizeof(info);
int which[] = {CTL_VM, VM_LOADAVG};
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) return;
avg[0] = (double) info.ldavg[0] / info.fscale;
avg[1] = (double) info.ldavg[1] / info.fscale;
avg[2] = (double) info.ldavg[2] / info.fscale;
}
int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,

View File

@ -28,6 +28,7 @@
#include <errno.h>
#include <sys/inotify.h>
#include <sys/sysinfo.h>
#include <unistd.h>
#include <time.h>
@ -52,6 +53,16 @@ uint64_t uv_hrtime() {
return (ts.tv_sec * NANOSEC + ts.tv_nsec);
}
void uv_loadavg(double avg[3]) {
struct sysinfo info;
if (sysinfo(&info) < 0) return;
avg[0] = (double) info.loads[0] / 65536.0;
avg[1] = (double) info.loads[1] / 65536.0;
avg[2] = (double) info.loads[2] / 65536.0;
}
int uv_exepath(char* buffer, size_t* size) {
if (!buffer || !size) {

View File

@ -24,6 +24,7 @@
#include <string.h>
#include <errno.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/sysctl.h>
@ -40,6 +41,17 @@ uint64_t uv_hrtime(void) {
return (ts.tv_sec * NANOSEC + ts.tv_nsec);
}
void uv_loadavg(double avg[3]) {
struct loadavg info;
size_t size = sizeof(info);
int which[] = {CTL_VM, VM_LOADAVG};
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) return;
avg[0] = (double) info.ldavg[0] / info.fscale;
avg[1] = (double) info.ldavg[1] / info.fscale;
avg[2] = (double) info.ldavg[2] / info.fscale;
}
int uv_exepath(char* buffer, size_t* size) {
uint32_t usize;

View File

@ -27,6 +27,7 @@
#include <errno.h>
#include <sys/time.h>
#include <sys/loadavg.h>
#include <unistd.h>
#include <kstat.h>
@ -93,6 +94,10 @@ double uv_get_total_memory(void) {
return (double) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
}
void uv_loadavg(double avg[3]) {
(void) getloadavg(avg, 3);
}
int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,

View File

@ -95,6 +95,11 @@ done:
return retVal;
}
void uv_loadavg(double avg[3]) {
/* Can't be implemented */
avg[0] = avg[1] = avg[2] = 0;
}
double uv_get_free_memory(void) {
MEMORYSTATUSEX memory_status;
memory_status.dwLength = sizeof(memory_status);

36
test/test-get-loadavg.c Normal file
View File

@ -0,0 +1,36 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "task.h"
TEST_IMPL(get_loadavg) {
double avg[3];
uv_loadavg(avg);
ASSERT(avg != NULL);
ASSERT(avg[0] >= 0);
ASSERT(avg[1] >= 0);
ASSERT(avg[2] >= 0);
return 0;
}

View File

@ -61,6 +61,7 @@ TEST_DECLARE (idle_starvation)
TEST_DECLARE (loop_handles)
TEST_DECLARE (ref)
TEST_DECLARE (idle_ref)
TEST_DECLARE (get_loadavg)
TEST_DECLARE (async_ref)
TEST_DECLARE (prepare_ref)
TEST_DECLARE (check_ref)
@ -188,6 +189,8 @@ TASK_LIST_START
TEST_ENTRY (get_memory)
TEST_ENTRY (get_loadavg)
TEST_ENTRY (hrtime)
TEST_ENTRY (getaddrinfo_basic)

1
uv.gyp
View File

@ -241,6 +241,7 @@
'test/run-tests.c',
'test/runner.c',
'test/runner.h',
'test/test-get-loadavg.c',
'test/task.h',
'test/test-async.c',
'test/test-callback-stack.c',