Determine max. memory consumption of process

In a recent project, we needed to measure the increase in memory consumption for an application process. How to obtain “the right values” for this depends on the actual scenario and, apparently, is not straight forward in all cases.

Let me first describe the scenario a little more: We want to obtain measurements for both fully serial and MPI parallel applications. These applications are run in (1) an unchanged (vanilla), (2) an instrumented version (version 1) and (3) a version, which uses LD_PRELOAD to sneak-in another library that overloads MPI functions to do additional work (version 2).

More precisely what we want:

  • A way to obtain reliable measurements for the different configurations, as we are interested in the additional amount of memory we need in version 1 and version 2, when compared to vanilla.
  • The max memory consumption at runtime, not regarding potential /swap memory.
  • We are only interested running on a Linux operating system

Eventually, we used the rusage feature. The returned struct offers different fields related to memory. We found that for our use case, the correct value was to use the maximum resident set size (max RSS). This proved to be reliable and reasonable compared to manual calculations of the memory we assumed we require. An example code is given below.

#include <sys/resource.h>
#include <stdio.h>
#include "mpi.h"

/* Needs to be called at the end of the process */
int MPI_Finalize() {
  int rank;
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  // We assume only MPI root should output memory consumption
  if (rank == 0) {
    struct rusage r;
    getrusage(RUSAGE_SELF, &r);
    printf("MAX RSS: %ld\n", r.ru_maxrss);
  }
  // ... Call to PMPI for actual MPI_Finalize
}

Keyboard layout and key mappings

When I switched to i3wm as my main work-horse window manager, I decided to use the “Alt Gr” or “Alt_R” key as the modifier key used for the i3 command shortcuts. The main reason for that comes from my habits to mainly work on workspaces 1 – 5. In order to decrease the stress on my left hand, the modifier should be one key that is controlled by the right hand.

First of all, I had to teach the US layout that the both Alt keys were actually different modifier keys. For this purpose I simply use the xmodmap command:

xmodmap -e 'remove Mod1 = Alt_R'
xmodmap -e 'add Mod3 = Alt_R'

In addition of selecting the “Mod3” key in the i3 config of course. This was working fine as long as I used th US keyboard layout.

Since working at a German university requires some interaction in German from time to time, I had to also have a German keyboard layout available. A short search in the web lead to the suggestion so use the simple setxkbmap command with the desired keyboard layout as follows:

setxkbmap de # For the German keyboard layout
setxbkmap us # For the US keyboard layout

So far so good. I change to the German keyboard layout and received an error message on the console saying that it could not remove the binding of the “Alt_R” symbol as no such mapping exists. I did not pay too much attention to that until I realized that I was unable to control my window manager anymore.

After a little while I finally found out that the “Alt Gr” key is mapped to something called “ISO_Level_3_shift_modifier” in certain keyboard mappings, e.g. the German keyboard mapping. After that I did invest quite some time in trying to figure out how I can resolve that problem and get a fully working German keyboard layout.

I then came across this great article , which explains (for me sufficiently detailed) how the key mapping is done. I was then able to adapt my simple commands to change between keyboard layouts to somewhat more lengthy one:

setxkbmap us; xmodmap -e 'remove Mod1 = Alt_R'; xmodmap -e 'add Mod3 = Alt_R' # For the US layout
setxkbmap de; xmodmap -e 'keycode 108 = Alt_R'; xmodmap -e 'add Mod3 = Alt_R'; xmodmap -e 'keycode 133 = ISO_Level3_Shift' # For the German layout

The additional command for the German keyboard maps the “super” or “Windows” key to the former “Alt Gr” functionality – that is the “ISO_Level_3_shift_modifier”. This is necessary, as on a German keyboard, for example, the ‘@’ sign is placed as a third modifier symbol. Thus, without this functionality available it would be a little less convenient.