mkernel 0.0.2
Micro-kernel framework, everything as a module
oom.c File Reference
#include "oom.h"
#include "debug/assert.h"
#include <sys/resource.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <alloca.h>
#include <unistd.h>
Include dependency graph for oom.c:

Go to the source code of this file.

Macros

#define __asm__   asm
 
#define __sync_synchronize   void
 
#define _GNU_SOURCE
 
#define RAMBLOCKS_MAX   1000
 Maximum number of fragmented blocks to allocate. More...
 

Functions

size_t oomtest_config (const size_t hardlimit)
 Sets the oomtest helpers hard rlimit and enables the oomtest helper features. More...
 
unsigned char oomtest_enabled ()
 

Variables

size_t(* oomtest_fill )(const size_t minHeap, const size_t minStack) =oomtest_fill_preinit
 Starts an almost OOM single and simple test. More...
 
void(* oomtest_free )() =oomtest_free_preinit
 Ends a single simple OOM test. More...
 
size_t(* oomtest_enable )(const size_t softlimit) =oomtest_enable_preinit
 Starts a new oomtest environment or reconfigure the soft limit. More...
 
size_t(* oomtest_disable )() =oomtest_disable_preinit
 Stops the current oomtest. More...
 

Detailed Description

Author
François Cerbelle (Fanfan), franc.nosp@m.ois@.nosp@m.cerbe.nosp@m.lle..nosp@m.net

Definition in file oom.c.

Macro Definition Documentation

◆ __asm__

#define __asm__   asm

Definition at line 21 of file oom.c.

◆ __sync_synchronize

#define __sync_synchronize   void

Definition at line 24 of file oom.c.

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 38 of file oom.c.

◆ RAMBLOCKS_MAX

#define RAMBLOCKS_MAX   1000

Maximum number of fragmented blocks to allocate.

I never reached more than 350 on my systems. The value needs to be less than UINT_MAX

Definition at line 47 of file oom.c.

Function Documentation

◆ oomtest_config()

size_t oomtest_config ( const size_t  hardlimit)

Sets the oomtest helpers hard rlimit and enables the oomtest helper features.

This function needs to be invoqued BEFORE any other helper from the framework. It will configure an hard RAM limit for the current process and each of his children. Then, it will enable the other oomtest helper functions.

It refuses to set a limit over the actually installed physical RAM to limit pushing RAM pages to the swap. If the requested value is zero, it will default to the actually installed physical RAM.

If anything goes wrong or did not behaves as expected, it abort the process to avoid RAM bombing and swapping.

Despite it was designed to be invoked only once, from the main parent process, it can be invoked several times as long as the hardlimit parameter is always less than the previous call, otherwise it will fail and abort the process.

Parameters
[in]hardlimitthe size in bytes to limit the process to.
Returns
Returns the actual configured size. It should be the same value as the hardlimit parameter, otherwise something went wrong, it was not detected and the process was not aborted (which is a bug to report)
See also
oomtest_enable
oomtest_disable
oomtest_fill
oomtest_free

Definition at line 367 of file oom.c.

◆ oomtest_enabled()

unsigned char oomtest_enabled ( )

Definition at line 422 of file oom.c.

Variable Documentation

◆ oomtest_disable

size_t(* oomtest_disable) () ( ) =oomtest_disable_preinit

Stops the current oomtest.

This function can be invoked any time after the oomtest_config initialized the environment. If invocated before, it will abort the current process to help detecting mistakes in the test code.

This function disables the fill/free oomtest functions and free the allocated RAM before reverting the soft limit to the hard limit value.

It is designed to be invoked t the end of a test case.

Despite it could be invoked twice or without a prior call to oomtest_enable, it is illegal and not allowed to help detecting mistakes in the test code.

Returns
Returns the applied soft limit, which should be the same as the hard limit.
See also
oomtest_enable
oomtest_disable
oomtest_fill
oomtest_free

Definition at line 138 of file oom.c.

◆ oomtest_enable

size_t(* oomtest_enable) (const size_t softlimit) ( const size_t  softlimit) =oomtest_enable_preinit

Starts a new oomtest environment or reconfigure the soft limit.

This function can only be invoked after at least a first call to oomtest_config to initialize the oomtest helpers environment. If invocated before, it will abort the current process to help detecting mistakes in the test code.

It will (soft)limit the current process and his children to the provided value in bytes. Then, it will enable the fill/free oomtest helper functions which are disabled otherwise.

It is designed to be invoked at the begining of a test case to apply for all tests in this testcase.

In case of any unexpected behavior, it should abort the current process.

Parameters
[in]softlimitSoft limit to set in bytes. It has to be less than the hardlimit otherwise it will fail and abort. If set to 0, it will apply the same value as the hardlimit.
Returns
Returns the actually set value as a softlimit, either the requested value or the hardlimit in case of 0 requested.
See also
oomtest_enable
oomtest_disable
oomtest_fill
oomtest_free

Definition at line 120 of file oom.c.

◆ oomtest_fill

size_t(* oomtest_fill) (const size_t minHeap, const size_t minStack) ( const size_t  minHeap,
const size_t  minStack 
) =oomtest_fill_preinit

Starts an almost OOM single and simple test.

This function is only enabled after a call to oomtest_enable and will have no effect otherwise. Its goal is to completely fill the RAM until the very last bytes, to keep only between minHeap and maxHeap bytes available in the heap and minStack bytes in the stack.

If oomtest_enable was not invoked beforehand, this function will simply return without any RAM consumption.

It should be called immediately before the test and should be reverted with oomtest_free immediately after. It can create so much pressure on the available memory that a simple printf could fail.

It is designed to fail if invoked twice as this is very probably a mistake in the test code. Should you need to change the RAM filling values, first call oomtest_free and reapply oomtest_fill. This ensure a really wanted behavior and not a mistake in your test code.

Parameters
[in]minHeap
[in]minStack
Returns
Returns the allocated size to fill the memory
See also
oomtest_enable
oomtest_disable
oomtest_fill
oomtest_free

Definition at line 84 of file oom.c.

◆ oomtest_free

void(* oomtest_free) () ( ) =oomtest_free_preinit

Ends a single simple OOM test.

If this function is invoked between an oomtest_enable and an oomtest_disable invocations, it deallocates the RAM allocated by the oomtest_fill function. It should be called immediately after the single and simple test because the RAM pressure can even make a printf to fail.

If called without a prior oomtest_enable invocation, this function simply returns without any action.

This function is designed to fail and abort the process if invocated whereas there is no current RAM allocated, when called twice, for example. This helps to avoid mistakes in the test scenario.

Definition at line 100 of file oom.c.