Skip to main navigation

# Blogs & News

Power and Performance

# PSSTOP: Chasing memory mirages in Linux*

23 Mar, 2017

by Victor Rodriguez Bahena

Every day, the global Internet population grows rapidly. Just in 2015, the Internet passed the 3 billion user milestone [1]. These users consume more than 1 billion websites that exist on the World Wide Web today[2]. Each one of these websites equates to a process fighting for memory in a far away server living in a cold data center. More memory generally means the server is better at handling demanding applications. Yet, even with several gigabytes of RAM available, a web server can still become starved for memory and cause websites to go offline [3] .

A small part of the server's available memory is used by the operating system and its applications. The largest chunk of memory is reserved for all the applications and their users. It is not uncommon for PHP applications to require tens of megabytes of memory for each user connection. And finally, some memory is needed for database storage. Knowing how much memory is being consumed and what processes are consuming it becomes crucial for today's data centers' operating systems.

The Clear Linux* Project uses the information coming from the proportional set size (PSS) to solve this problem. Before discussing the detailed implementation, let's review the ways used to measure the memory consumption of a single process.

The methods used decades ago to measure memory consumption of a single process on an operating system were fairly simple, but this is not the case with modern operating systems. Now multiple processes share virtual memory to allow software developers to utilize the powerful computer systems we have today. This also makes measuring the memory consumption of a single process much more difficult.

To improve both the utilization of the CPU and the speed of a computer’s response, current operating systems must keep several programs in memory, creating a need for memory management. As described in [4], “Memory management is responsible for:

- Allocating and deallocating memory space as needed.

- Keeping track of which parts of memory are currently being used and by what processes”.

Linux* is equipped with virtual memory management and has ways to provide metrics that show how much memory is being used by a process. The Linux tools most commonly used are the VSZ (Virtual Memory Size) and RSS (Resident Set Size).

The VSZ describes the entire virtual memory size of the process regardless of whether or not pages are being loaded in physical memory. This metric is not a realistic indicator to measure memory consumption, since it includes pages that are not actually consumed [5].

RSS is an indicator that shows the memory consumption when the process is running by itself without sharing anything with other processes. Since all current programs have shared libraries, RSS overestimates the amount of memory consumed by the process.

Proportional set size (PSS), provides a better perspective of the memory in use. The proportional set size of a process indicates the pages a process has in memory, but each page is divided by the number of processes sharing it. Imagine a process with 1000 pages all to itself and 1000 pages shared with one other process. The PSS of the process is 1500 [6]. PSS gives a more realistic indicator of memory consumption compared to RSS and VSZ. It works especially well when considering the memory consumption of an entire system and not each process individually.

The proportional set size is given by the kernel on the /proc/<PID>/smaps files as:

`Pss: <size> kB`

One value is provided per currently mapped memory regions of the process under analysis. The /proc/PID/smaps file contains the list of the currently mapped memory regions [7] , for example:

`[heap] = the heap of the program
[stack] = the stack of the main process
[vdso] = the "virtual dynamic shared object", the kernel system call handler`

For each of mapped region, there is a series of lines, for example:

`08048000-080bc000 r-xp 00000000 03:02 13130 /bin/bash
Size: 1084 kB
Rss: 892 kB
Pss: 374 kB
Shared_Clean: 892 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 0 kB
Referenced: 892 kB
Anonymous: 0 kB
AnonHugePages: 0 kB
ShmemPmdMapped: 0 kB
Shared_Hugetlb: 0 kB
Private_Hugetlb: 0 kB
Swap: 0 kB
SwapPss: 0 kB
KernelPageSize: 4 kB
MMUPageSize: 4 kB
Locked: 0 kB`

Since there is extra effort required to add together all the PSS from all the mapped memory regions, Clear Linux provides a simple tool called PSSTOP, which presents the detailed information provided by PSS in an easy-to-digest format. PSSTOP tracks the amount of memory from the process's proportional share of the PSS mapping and the proportional swap share of the SwapPSS mapping. PSSTOP is an easy tool with few lines of code that everyone can use.

Another example is this simple code to show how accurate PSSTOP is when compared to other tools used to measure the memory.

`#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
int main( int argc, char *argv[] ) {
char *str,*p;
unsigned int secs = 30;
unsigned int timer;
int block = 100;
if( argc == 2 ) {
block = strtol(argv[1], &p, 10);
}
printf("PID = %d\n",getpid());
for(int i = 0 ; i < block; i++){
str = (char *) malloc(1024);
}
strcpy(str, "simple_test");
printf("Reserving %d Kb of memory\n", block);
printf("Value of String = %s\n", str);
printf("Address = %u\n", str);
timer = time(0) + secs;
printf("Waiting for %d seconds\n", secs);
while (time(0) < timer);
free(str);
return(0);
}`

This code requests memory through malloc in chunks of 1 KB. Figure 1 shows how PSSTOP accurately measures the increments of memory as the block variable changes. The information given by other tools using VSZ and RSS, on the other hand, is not a realistic metric according to the behavior of the code.

Figure 1

As illustrated in Figure 1, conventional methods to measure the amount of memory consumed by the processes running in our systems are not accurate. PSSTOP provides a more accurate measurement of memory consumption. The technology that makes it possible has been in our Linux kernels for more than five years. In a world with billions of processes running in the current data centers, it is necessary to embrace new tools for a new world.

References:

[1] Brown, K. (n.d.). Internet Society. Retrieved January 02, 2017, from Global Internet Report 2016: https://www.internetsociety.org/globalinternetreport/2016/wp-content/uploads/2016/11/ISOC_GIR_2016-v1.pdf

[2] Real Time Statistics Project. (n.d.). Retrieved February 02, 2017, from Total number of Websites: http://www.internetlivestats.com/total-number-of-websites/

[3] Combell. (n.d.). Retrieved January 02, 2017, from How much memory does a webserver need?: https://support.combell.com/en/how-much-memory-does-a-webserver-need/313

[4] Abraham-Silberschat. (2008). Operating System Concepts. In Abraham-Silberschat. Wiley Publishing .

[5] Corbet, J. (2007, April 18). LWN.net . Retrieved from How much memory are applications really using: https://lwn.net/Articles/230975/

[6] Emilics. (n.d.). Retrieved January 02, 2017, from Memory Consumption in Linux: https://emilics.com/notebook/enblog/p871.html

[7] kernel.org. (n.d.). Retrieved January 02, 2017, from The proc filesystem: https://www.kernel.org/doc/Documentation/filesystems/proc.txt

*Other names and brands may be claimed as the property of others.