Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
os: implement memory bindings
Browse files Browse the repository at this point in the history
* us_get_free_memory
* us_get_total_memory
  • Loading branch information
indutny authored and bnoordhuis committed Oct 4, 2011
1 parent 6221904 commit 33cb877
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config-unix.mk
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions include/uv.h
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/unix/cygwin.c
Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions src/unix/darwin.c
Expand Up @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions src/unix/freebsd.c
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/unix/linux.c
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions src/unix/netbsd.c
Expand Up @@ -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,
Expand Down
30 changes: 30 additions & 0 deletions src/unix/sunos.c
Expand Up @@ -28,6 +28,7 @@

#include <sys/time.h>
#include <unistd.h>
#include <kstat.h>


uint64_t uv_hrtime() {
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions src/win/util.c
Expand Up @@ -94,3 +94,27 @@ int uv_exepath(char* buffer, size_t* size) {

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 changes: 34 additions & 0 deletions test/test-get-memory.c
@@ -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;
}
3 changes: 3 additions & 0 deletions test/test-list.h
Expand Up @@ -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)
Expand Down Expand Up @@ -185,6 +186,8 @@ TASK_LIST_START

TEST_ENTRY (get_currentexe)

TEST_ENTRY (get_memory)

TEST_ENTRY (hrtime)

TEST_ENTRY (getaddrinfo_basic)
Expand Down
1 change: 1 addition & 0 deletions uv.gyp
Expand Up @@ -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',
Expand Down

0 comments on commit 33cb877

Please sign in to comment.