Test the OOM situation

Maxim Dounin mdounin at mdounin.ru
Tue May 18 23:26:34 MSD 2010


Hello!

On Tue, May 18, 2010 at 08:25:46PM +0400, Peter Leonov wrote:

> Hello devels,
> 
> There is a module that requires a heavy testing in environment with
> low memory. Especialy it could be good to test how it goes if no
> more memory is available in the system.
> 
> So the questions are: is there a way to test such a module without a
> virtual machines? and are there some tools which might be of help?

You may tune memory limit for a given process instead of using 
virtual machine.  Though this won't really help with testing nginx 
modules as pool allocator will protect most of allocations from 
seeing errors (while they still can happen with sligtly different 
config).

Injecting random errors into allocation routines proven to be much 
more usable.  Just for completeness: attached trivial patch which 
I used while searching for allocation error handling bugs in nginx 
itself.

Maxim Dounin
-------------- next part --------------
diff --git a/src/core/ngx_palloc.c b/src/core/ngx_palloc.c
--- a/src/core/ngx_palloc.c
+++ b/src/core/ngx_palloc.c
@@ -117,6 +117,14 @@ ngx_palloc(ngx_pool_t *pool, size_t size
     u_char      *m;
     ngx_pool_t  *p;
 
+#if 1 /* XXX */
+    if (ngx_random() % 1000 == 1) {
+        ngx_log_error(NGX_LOG_EMERG, pool->log, 0,
+                      "shit happens, failing allocation of %uz bytes", size);
+        return NULL;
+    }
+#endif
+
     if (size <= pool->max) {
 
         p = pool->current;
diff --git a/src/os/unix/ngx_alloc.c b/src/os/unix/ngx_alloc.c
--- a/src/os/unix/ngx_alloc.c
+++ b/src/os/unix/ngx_alloc.c
@@ -19,6 +19,14 @@ ngx_alloc(size_t size, ngx_log_t *log)
     void  *p;
 
     p = malloc(size);
+
+#if 1 /* XXX */
+    if (p != NULL && ngx_random() % 100 == 1) {
+        free(p);
+        p = NULL;
+    }
+#endif
+
     if (p == NULL) {
         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
                       "malloc() %uz bytes failed", size);


More information about the nginx-devel mailing list