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

Commit

Permalink
sunos: look up free memory with sysconf(_SC_AVPHYS_PAGES)
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Oct 12, 2011
1 parent 7b01ad1 commit e0a4e72
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 21 deletions.
24 changes: 3 additions & 21 deletions src/unix/sunos.c
Expand Up @@ -65,35 +65,17 @@ 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_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);
}


void uv_loadavg(double avg[3]) {
(void) getloadavg(avg, 3);
}
Expand Down
3 changes: 3 additions & 0 deletions test/test-get-memory.c
Expand Up @@ -26,8 +26,11 @@ TEST_IMPL(get_memory) {
double free_mem = uv_get_free_memory();
double total_mem = uv_get_total_memory();

printf("free_mem=%.0f, total_mem=%.0f\n", free_mem, total_mem);

ASSERT(free_mem > 0);
ASSERT(total_mem > 0);
ASSERT(total_mem > free_mem);

return 0;
}

0 comments on commit e0a4e72

Please sign in to comment.