You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/11/29 10:13:25 UTC

[GitHub] sven-lange-last edited a comment on issue #3246: ActionLimitsTests memory test failing

sven-lange-last edited a comment on issue #3246: ActionLimitsTests memory test failing
URL: https://github.com/apache/incubator-openwhisk/issues/3246#issuecomment-442779102
 
 
   I guess the problem lies somewhere in the details of physical / virtual memory, container (cgroup) rss / swap memory limits and overall system memory.
   
   The exception stack starting with `RangeError: Array buffer allocation failed` says that the Node.js could not allocate the requested amount of _virtual_ memory. See https://github.com/nodejs/node/blob/v6.15.0/lib/buffer.js for the underlying Node.js version 6 code.
   
   To explain why this error happens, let's run the `free -hw` command on the global system and within a memory limited standard action container (256 MB):
   
   ```
   # free -hw
                 total        used        free      shared     buffers       cache   available
   Mem:            15G        2.3G        1.5G        101M        832M         11G         12G
   
   # docker exec -it 05b670eafb46 free -hw
                 total        used        free      shared     buffers       cache   available
   Mem:            15G        2.6G        1.5G        101M        832M         10G         12G
   ```
   
   There is barely no difference between the global system and the container. Let's ignore the small differences in output for now. Even though there is a memory limit defined on the container, it seems to have 15G RAM. Containers (i.e. namespaces) do not influence the results of memory management kernel calls because there is no memory namespace [1]. The container "thinks" it has lots of memory even if there is a limit on the physical memory.
   
   If you request an amount of (virtual) memory - regardless of whether you are in a container or not -, you will receive a positive result if the kernel thinks it will be able to contain the requested memory. It won't reserve any physical memory pages for your request. [2]
   
   When receiving a `RangeError: Array buffer allocation failed`, the kernel returned a failure because the overall system (or VM) must have been short on memory. So the kernel decided that it cannot contain a contiguous block of 256 MiB of virtual memory.
   
   As already mentioned, when you receive the desired amount of virtual memory, it's simply table entries but not backed by physical memory.
   
   If you start filling the memory, the memory management system will receive page faults whenever you touch a virtual memory page for the first time and provide a physical page backing the virtual page. This is where the action container limits apply: The memory subsystem has an accounting how much physical memory is owned by the container. Once the actual physical memory reaches the defined limit, the memory subsystem tries to reduce physical memory pressure of the container (cgroup). The final action is to invoke the out-of-memory (OOM) killer which stops the process with the highest score. The process consuming most physical memory usually has the highest score.
   
   BTW: if your action code starts a child process which consumes more physical memory than your main process, the OOM killer will stop the child process. At the same time, the main action process holding the communication socket to the invoker will continue to run normally. You won't see an OOM error of the action.
   
   Another interesting detail: some operating systems and VMs perform memory de-deplication [3]. If you allocate a large buffer and fill all virtual memory pages with the same values, it may end up with a single physical page representing all required virtual memory pages. That's why our current action [4] may fail to provoke the desired OOM kill in some environments.
   
   Based on these details, I suggest to follow this strategy:
   * Set the action's memory limit to the lowest possible value. At the moment, we set it to `128.megabytes` which is actually the limit. We should use `MemoryLimit.minMemory`.
   * The action should allocate exactly the limit. I guess this should already be sufficient.
   * The action should determine the system's page size and fill one randomly chosen byte on each page of the buffer with a random value. On the one hand, that's probably faster than writing each byte. On the other hand, this prevents memory deduplication or compression.
   
   Nevertheless, if the system has too few memory, this strategy won't help either.
   
   [1] http://man7.org/linux/man-pages/man7/namespaces.7.html
   [2] https://linux.die.net/man/3/malloc
   [3] https://en.wikipedia.org/wiki/Kernel_same-page_merging
   [4] https://github.com/apache/incubator-openwhisk/blob/master/tests/dat/actions/memoryWithGC.js

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services