Browse files

[GH #935] honor user-rlimits for returned sysmem_amount (untested)

We need to check for RLIMIT_DATA and then RLIMIT_AS,
first hard, then soft limits.
  • Loading branch information...
1 parent b23f23e commit 2c4e32bebe880f8017d4c0cb0a561b8e049c10ad @rurban rurban committed Feb 17, 2013
Showing with 22 additions and 3 deletions.
  1. +1 −1 config/auto/headers.pm
  2. +21 −2 src/platform/generic/sysmem.c
View
2 config/auto/headers.pm
@@ -97,7 +97,7 @@ sub _list_extra_headers {
# the header.
my @extra_headers = qw(malloc.h fcntl.h setjmp.h pthread.h signal.h
sys/types.h sys/socket.h netinet/in.h arpa/inet.h
- sys/stat.h sysexit.h limits.h sys/sysctl.h libcpuid.h);
+ sys/stat.h sysexit.h limits.h sys/resource.h sys/sysctl.h libcpuid.h);
# more extra_headers needed on mingw/msys; *BSD fails if they are present
if ( $conf->data->get('OSNAME_provisional') eq "msys" ) {
View
23 src/platform/generic/sysmem.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010-2012, Parrot Foundation.
+ * Copyright (C) 2010-2013, Parrot Foundation.
*/
/*
@@ -31,6 +31,9 @@ Get system memory information.
#if defined(PARROT_HAS_HEADER_SYSSYSCTL)
# include <sys/sysctl.h>
#endif
+#if defined(PARROT_HAS_HEADER_SYSRESOURCE)
+# include <sys/resource.h>
+#endif
/* HEADERIZER HFILE: none */
@@ -40,7 +43,7 @@ Get system memory information.
Get information about available physical memory.
-Returns available physical memory in bytes.
+Returns available physical memory in bytes, with respect to resource limits.
=cut
@@ -59,6 +62,9 @@ size_t
Parrot_sysmem_amount(PARROT_INTERP)
{
size_t memsize = 0;
+#if defined(PARROT_HAS_HEADER_SYSRESOURCE)
+ struct rlimit rlim;
+#endif
#if defined(_SC_PAGESIZE) && defined(_SC_AVPHYS_PAGES)
UNUSED(interp);
@@ -99,6 +105,19 @@ Parrot_sysmem_amount(PARROT_INTERP)
memsize = 512 * 1024 * 1024;
#endif
+#if defined(PARROT_HAS_HEADER_SYSRESOURCE)
+ if (getrlimit(RLIMIT_DATA, &rlim) == 0) {
+ if ((rlim.rlim_max != RLIM_INFINITY) && (rlim.rlim_max < memsize)) memsize = rlim.rlim_max;
+ else
+ if ((rlim.rlim_cur != RLIM_INFINITY) && (rlim.rlim_cur < memsize)) memsize = rlim.rlim_cur;
+ }
+ if (getrlimit(RLIMIT_AS, &rlim) == 0) {
+ if ((rlim.rlim_max != RLIM_INFINITY) && (rlim.rlim_max < memsize)) memsize = rlim.rlim_max;
+ else
+ if ((rlim.rlim_cur != RLIM_INFINITY) && (rlim.rlim_cur < memsize)) memsize = rlim.rlim_cur;
+ }
+#endif
+
return memsize;
}

0 comments on commit 2c4e32b

Please sign in to comment.