# User pages accounting

User Beancounters
Definition
/proc/user_beancounters
/proc/bc/
General information
Units of measurement
VSwap
Parameters description
Primary parameters
numproc, numtcpsock, numothersock, vmguarpages
Secondary parameters
kmemsize, tcpsndbuf, tcprcvbuf, othersockbuf, dgramrcvbuf, oomguarpages, privvmpages
Auxiliary parameters
lockedpages, shmpages, physpages, numfile, numflock, numpty, numsiginfo, dcachesize, numiptent, swappages
Internals
User pages accounting
On-demand accounting
UBC consistency
Consistency formulae
System-wide configuration
vzubc(8)
Configuration examples
Basic
Derived
Intermediate configurations
Tables
List of parameters
Parameter properties
Consistency
Config examples

## Introduction

User pages is the second important resource (after kmemsize) which must be accounted. Unlike kernel memory, which is either used or not, the set of user pages may have different classifications. Pages may be backed by file, locked, unused, i.e. requested with mmap/brk but not yet touched and so on. Thus user pages accounting is trickier than the one for kernel pages.

## Ways of accounting

There are different approaches to user pages control:

Account all the mappings on mmap/brk and reject as soon as the sum of VMA's lengths reaches the barrier.
This approach is very bad as applications always map more than they really use, very often MUCH more.
Account only the really used memory and reject as soon as RSS reaches the limit.
This approach is not good either as the only place where pages appear in user space is page fault handler and the only way to reject is killing the task. Comparing to previous scenarion this is much worse as application won't even be able to terminate gracefully.
Account a part of memory on mmap/brk and reject there, and account the rest of the memory in page fault handlers without any rejects.
This type of accounting is used in UBC.
Account physical memory and behave like a standalone kernel - reclaim user memory when run out of it.
This type of memory control is to be introduced later as an addition to current scheme. UBC provides all the needed statistics for this (physical memory, swap pages etc.)

## UBC user pages accounting

### Terms

The following terms are used by UBC:

• shmem mapping — a mapping of file belonging to tmpfs. UBC accounts these pages separately and the description below doesn't take such pages into account;
• private mapping — this includes the following types of mappings:
• writable anonymous mappings
• writable private file mappings
Both types are not backed by disk file and thus may not be just freed. These mappings are charged with possible reject right when they are made — in sys_mmap()/sys_brk().
• unused pages — this is the number of pages which belong to private mapping, but are not yet touched.

### Math model

The following notations are used:

• ${\displaystyle UB_{privvm}}$ is the total number of privvmpages accounted on UB. In other words, this is the value seen in /proc/user_beancounters in privvmpages.held;
• ${\displaystyle UB_{unused}}$ is the total number of unused pages. This parameter is shown in /proc/user_beancounters_debug file;
• ${\displaystyle Frac(page,UB)}$ is some value that represents the part of the page charged to beancounter in case when page is mapped;
• ${\displaystyle RSS}$ is the amount of physical memory used by processes.

Page fraction — ${\displaystyle Frac(page,UB)}$ — normally should be ${\displaystyle {\frac {1}{N}}}$, where ${\displaystyle N}$ is the number of UBs the pages are shared between, but this is bad since adding a new UB to page shared set would require recalculation of the whole current set. In UB ${\displaystyle Frac(page,UB)={\frac {1}{2^{UB_{shift}}}}}$, where ${\displaystyle UB_{shift}}$ is some parameter which is calculated so that

${\displaystyle \sum _{UB:page\in UB}Frac(page,UB)=1,\forall page}$.

The notation ${\displaystyle page\in UB}$ means “there exists an mm_struct, where the page ${\displaystyle page}$ is mapped to and this mm_struct belongs to ${\displaystyle UB}$ beancounter”. This type of calculation allows making an O(1) algorithm of fractions accounting.

Privvmpages accounts the sum of unused pages and the “normalized” number of RSS pages:

${\displaystyle UB_{privvm}=UB_{unused}+\sum _{page\in UB}Frac(page,UB)}$

If you sum the privvmpages of all beancounters in the system you'll get an upper estimation of current physical memory usage by processes:

${\displaystyle \sum _{UB}UB_{privvm}=\,}$

${\displaystyle \sum _{UB}UB_{unused}+\sum _{UB}{\sum _{page\in UB}Frac(page,UB)}=}$

${\displaystyle \sum _{UB}UB_{unused}+\sum _{page}\sum _{UB:page\in UB}Frac(page,UB)=}$

${\displaystyle \sum _{page:page\ is\ mapped}1+\sum _{UB}UB_{unused}=}$

${\displaystyle RSS+\sum _{UB}UB_{unused}\,}$

To get real physical memory usage physpages resource is used [1].

${\displaystyle UB_{physpages}=\sum _{page\in UB}Frac(page,UB)}$

Thus

${\displaystyle \sum _{UB}UB_{physpages}=RSS\,}$

1. Actually physpages also include tmpfs resident pages