[Piglit] [PATCH 1/6] Add piglit_set_rlimit utility function

Ian Romanick idr at freedesktop.org
Wed Jul 27 17:50:44 PDT 2011


From: Ian Romanick <ian.d.romanick at intel.com>

This function sets the maximum amount of memory that a process can
have in its address space.  This is useful for tests that use
"infinite" memory on failing implementations.  Setting this low can
prevent the test from exhausting system memory and adversely affecting
other tests.
---
 CMakeLists.txt           |    3 +++
 tests/util/config.h.in   |    3 +++
 tests/util/piglit-util.c |   38 ++++++++++++++++++++++++++++++++++++++
 tests/util/piglit-util.h |    2 ++
 4 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1015165..847be0c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -68,8 +68,11 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${piglit_BINARY_DIR}/bin)
 
 check_function_exists(strchrnul HAVE_STRCHRNUL)
 check_function_exists(fopen_s   HAVE_FOPEN_S)
+check_function_exists(setrlimit HAVE_SETRLIMIT)
 
+check_include_file(sys/time.h  HAVE_SYS_TIME_H)
 check_include_file(sys/types.h HAVE_SYS_TYPES_H)
+check_include_file(sys/resource.h  HAVE_SYS_RESOURCE_H)
 check_include_file(sys/stat.h  HAVE_SYS_STAT_H)
 check_include_file(unistd.h    HAVE_UNISTD_H)
 check_include_file(fcntl.h     HAVE_FCNTL_H)
diff --git a/tests/util/config.h.in b/tests/util/config.h.in
index a012cc3..b664adc 100644
--- a/tests/util/config.h.in
+++ b/tests/util/config.h.in
@@ -1,7 +1,10 @@
 #cmakedefine HAVE_STRCHRNUL
 #cmakedefine HAVE_FOPEN_S
+#cmakedefine HAVE_SETRLIMIT
 
 #cmakedefine HAVE_FCNTL_H
 #cmakedefine HAVE_SYS_STAT_H
 #cmakedefine HAVE_SYS_TYPES_H
+#cmakedefine HAVE_SYS_TIME_H
+#cmakedefine HAVE_SYS_RESOURCE_H
 #cmakedefine HAVE_UNISTD_H
diff --git a/tests/util/piglit-util.c b/tests/util/piglit-util.c
index 78722d9..22941fd 100644
--- a/tests/util/piglit-util.c
+++ b/tests/util/piglit-util.c
@@ -33,6 +33,14 @@
 #include <errno.h>
 #include <sys/stat.h>
 
+#include "config.h"
+#if defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_SETRLIMIT)
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <errno.h>
+#define USE_SETRLIMIT
+#endif
+
 #include "piglit-util.h"
 
 void piglit_glutInit(int argc, char **argv)
@@ -473,3 +481,33 @@ char *strchrnul(const char *s, int c)
 	return (t == NULL) ? ((char *) s + strlen(s)) : t;
 }
 #endif
+
+
+void
+piglit_set_rlimit(unsigned long lim)
+{
+#if defined(USE_SETRLIMIT)
+	struct rlimit rl;
+	if (getrlimit(RLIMIT_AS, &rl) != -1) {
+		printf("Address space limit = %lu, max = %lu\n",
+		       (unsigned long) rl.rlim_cur,
+		       (unsigned long) rl.rlim_max);
+
+		if (rl.rlim_max > lim) {
+			printf("Resetting limit to %lu.\n", lim);
+
+			rl.rlim_cur = lim;
+			rl.rlim_max = lim;
+			if (setrlimit(RLIMIT_AS, &rl) == -1) {
+				printf("Could not set rlimit "
+				       "due to: %s (%d)\n",
+				       strerror(errno), errno);
+			}
+		}
+	}
+
+	printf("\n");
+#else
+	printf("Cannot reset rlimit on this platform.\n\n");
+#endif
+}
diff --git a/tests/util/piglit-util.h b/tests/util/piglit-util.h
index e877c71..e7f7477 100644
--- a/tests/util/piglit-util.h
+++ b/tests/util/piglit-util.h
@@ -291,5 +291,7 @@ extern void piglit_require_vertex_shader(void);
 char *strchrnul(const char *s, int c);
 #endif
 
+extern void piglit_set_rlimit(unsigned long lim);
+
 static const GLint PIGLIT_ATTRIB_POS = 0;
 static const GLint PIGLIT_ATTRIB_TEX = 1;
-- 
1.7.4.4



More information about the Piglit mailing list