os: implement memory bindings

* us_get_free_memory
* us_get_total_memory
This commit is contained in:
Fedor Indutny 2011-10-01 15:45:42 +07:00 committed by Ben Noordhuis
parent 6221904013
commit 33cb8775bc
12 changed files with 188 additions and 1 deletions

View File

@ -44,7 +44,7 @@ ifeq (SunOS,$(uname_S))
EV_CONFIG=config_sunos.h
EIO_CONFIG=config_sunos.h
CPPFLAGS += -Isrc/ares/config_sunos -D__EXTENSIONS__ -D_XOPEN_SOURCE=500
LINKFLAGS+=-lsocket -lnsl
LINKFLAGS+=-lsocket -lnsl -lkstat
OBJS += src/unix/sunos.o
endif

View File

@ -1065,6 +1065,10 @@ int uv_ip6_name(struct sockaddr_in6* src, char* dst, size_t size);
/* Gets the executable path */
int uv_exepath(char* buffer, size_t* size);
/* Memory info */
double uv_get_free_memory(void);
double uv_get_total_memory(void);
/*
* Returns the current high-resolution real time. This is expressed in
* nanoseconds. It is relative to an arbitrary time in the past. It is not

View File

@ -53,6 +53,13 @@ int uv_exepath(char* buffer, size_t* size) {
return 0;
}
double uv_get_free_memory(void) {
return (double) sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES);
}
double uv_get_total_memory(void) {
return (double) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
}
int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,

View File

@ -68,6 +68,30 @@ int uv_exepath(char* buffer, size_t* size) {
return 0;
}
double uv_get_free_memory(void) {
vm_statistics_data_t info;
mach_msg_type_number_t count = sizeof(info) / sizeof(integer_t);
if (host_statistics(mach_host_self(), HOST_VM_INFO,
(host_info_t)&info, &count) != KERN_SUCCESS) {
return -1;
}
return (double) info.free_count * sysconf(_SC_PAGESIZE);
}
double uv_get_total_memory(void) {
uint64_t info;
int which[] = {CTL_HW, HW_MEMSIZE};
size_t size = sizeof(info);
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
return -1;
}
return (double) info;
}
int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,

View File

@ -67,6 +67,31 @@ int uv_exepath(char* buffer, size_t* size) {
return 0;
}
double uv_get_free_memory(void) {
vm_statistics_data_t info;
mach_msg_type_number_t count = sizeof(info) / sizeof(integer_t);
if (host_statistics(mach_host_self(), HOST_VM_INFO,
(host_info_t)&info, &count) != KERN_SUCCESS) {
return -1;
}
return (double) info.free_count * sysconf(_SC_PAGESIZE);
}
double uv_get_total_memory(void) {
unsigned long info;
int which[] = {CTL_HW, HW_PHYSMEM};
size_t size = sizeof(info);
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
return -1;
}
return (double) info;
}
int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,

View File

@ -64,6 +64,13 @@ int uv_exepath(char* buffer, size_t* size) {
return 0;
}
double uv_get_free_memory(void) {
return (double) sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES);
}
double uv_get_total_memory(void) {
return (double) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
}
static int new_inotify_fd(void) {
#if defined(IN_NONBLOCK) && defined(IN_CLOEXEC)

View File

@ -70,6 +70,34 @@ int uv_exepath(char* buffer, size_t* size) {
return 0;
}
double uv_get_free_memory(void) {
struct uvmexp info;
size_t size = sizeof(info);
int which[] = {CTL_VM, VM_UVMEXP};
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
return -1;
}
return (double) info.free * psysconf(_SC_PAGESIZE);
}
double uv_get_total_memory(void) {
#if defined(HW_PHYSMEM64)
uint64_t info;
int which[] = {CTL_HW, HW_PHYSMEM64};
#else
unsigned int info;
int which[] = {CTL_HW, HW_PHYSMEM};
#endif
size_t size = sizeof(info);
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
return -1;
}
return (double) info;
}
int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,

View File

@ -28,6 +28,7 @@
#include <sys/time.h>
#include <unistd.h>
#include <kstat.h>
uint64_t uv_hrtime() {
@ -63,6 +64,35 @@ int uv_exepath(char* buffer, size_t* size) {
return (0);
}
double uv_get_free_memory(void) {
kstat_ctl_t *kc;
kstat_t *ksp;
kstat_named_t *knp;
ulong_t freemem;
if ((kc = kstat_open()) == NULL) return -1;
ksp = kstat_lookup(kc, (char *)"unix", 0, (char *)"system_pages");
if(kstat_read(kc, ksp, NULL) == -1){
return -1;
}
else {
knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"freemem");
freemem = knp->value.ul;
}
kstat_close(kc);
return (double) freemem * sysconf(_SC_PAGESIZE);
}
double uv_get_total_memory(void) {
return (double) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
}
int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,

View File

@ -94,3 +94,27 @@ done:
return retVal;
}
double uv_get_free_memory(void) {
MEMORYSTATUSEX memory_status;
memory_status.dwLength = sizeof(memory_status);
if(!GlobalMemoryStatusEx(&memory_status))
{
return -1;
}
return (double)memory_status.ullAvailPhys;
}
double uv_get_total_memory(void) {
MEMORYSTATUSEX memory_status;
memory_status.dwLength = sizeof(memory_status);
if(!GlobalMemoryStatusEx(&memory_status))
{
return -1;
}
return (double)memory_status.ullTotalPhys;
}

34
test/test-get-memory.c Normal file
View File

@ -0,0 +1,34 @@
/* 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_memory) {
double free_mem = uv_get_free_memory();
double total_mem = uv_get_total_memory();
ASSERT(free_mem > 0);
ASSERT(total_mem > 0);
ASSERT(total_mem > free_mem);
return 0;
}

View File

@ -67,6 +67,7 @@ TEST_DECLARE (check_ref)
TEST_DECLARE (unref_in_prepare_cb)
TEST_DECLARE (async)
TEST_DECLARE (get_currentexe)
TEST_DECLARE (get_memory)
TEST_DECLARE (hrtime)
TEST_DECLARE (getaddrinfo_basic)
TEST_DECLARE (getaddrinfo_concurrent)
@ -185,6 +186,8 @@ TASK_LIST_START
TEST_ENTRY (get_currentexe)
TEST_ENTRY (get_memory)
TEST_ENTRY (hrtime)
TEST_ENTRY (getaddrinfo_basic)

1
uv.gyp
View File

@ -250,6 +250,7 @@
'test/test-fs.c',
'test/test-fs-event.c',
'test/test-get-currentexe.c',
'test/test-get-memory.c',
'test/test-getaddrinfo.c',
'test/test-gethostbyname.c',
'test/test-getsockname.c',