memory allocation diagnostics tool

Marnix Coppens (
Mon, 15 Dec 1997 09:52:07 +0100

Allright, for those of you who are interested in how Linux is
using all your physical memory, and why it complains it can't get a free page,
I've written a small stand-alone module for kernels 2.0.xx that will
will register two files /proc/memmap and /proc/mempages. I have attached
the introducing comment which explains in more detail what these files do.

* memmap.c, a module to examine the state of the physical memory.
* This will register two files /proc/memmap and /proc/mempages.
* /proc/memmap shows one character per physical page as follows:
* 'R' for reserved, 'b' for buffers, 'c' for cached,
* 'u' for used and 'f' for free.
* The number of these should match the output of 'free' or /proc/meminfo,
* with this difference that 'u' doesn't include 'b' or 'c'.
* The number of reserved bytes matches the amount of memory used by the
* kernel as reported at boot time. The output is 32 characters at a line,
* so that every line describes 128 KB of physical memory.
* /proc/mempages shows a more detailed description of every physical page
* that is non-free (and non-reserved).
* The output can be one of the following:
* 02ba5000 00001000 1 c 00000000 0 03:02 14284
* 02ba6000 00000000 1 u
* 02ba7000 0000f000 1 c 02cb8b18 2 03:02 54908
* 02ba8000 00003000 1 b
* where the fields are (see also include/linux/mm.h):
* 1) physical address.
* 2) page offset.
* 3) page reference count.
* 4) state (as above, but without 'f' or 'R').
* 5) pointer to memory mapping (vm_area_struct); if non-zero,
* this is also a physical address and the corresponding page
* will show up as 'u'.
* 6) inode reference count.
* 7,8) device and inode number. With the use of dentries in recent
* kernels (2.1.xx), this will become the inode's filename.

Of course, a picture says more than a thousand words, so uudecode, gunzip,
gcc, insmod, more, grep and wc will tell you what it's all about :-).

Writing a small program or scipt to count the number of pages for every
type should be a small exercise for any 19 year old CS student. The original
intent was to find the longest stretch of free pages still available, but
Shift-ScrollLock will tell you that as well.

Staring at these two files has already shown me a few things about Linux
memory management. For instance, page allocation starts from the top of
physical memory and works its way down, so people who add extra memory will
find it gets tested real soon. Also, you can now track how much of a shared
library actually resides in main memory. On my system (no swap, plenty of
memory), libc turns out to be paged in for more than 90 %
(that's more than 500 K!!).

Anyway, try it and enjoy it. Credit where it's due: the algorithm for
separating the output in lines was 'adapted' from that of Bruno Haible
in fs/proc/array.c for proc/pid/maps (BTW anyone know why this is a pipe
iso a regular file, it's annoying at times..).

begin 777 memmap.c.gz
M'XL("#1>E#0""VUE;6UA<"YC -U8VV[;1A!]IKYBDB(1*9$V)3N)4MD"TD1)
MC?I2.'93H"T(BEQ:1,A=@A=?VOK?.S-+4I0E TF1IQBV3.[.?<Z<77MWT(.!
MD8HT];.=P 8?4A56B8!2@;CUTUCBXU) 4?JE !7Q2[:\*^+ 3P#U5'ZW0R8N
MEG$!-W&20"ZNXJ(4.90W"J(X$07L9KD*=K47\&6X6LC\*U&0 ;+Q5F5W>7RU
M>B'*A:A+($!6Z0*=ZT(5@J*KDA!2OPR67#M5E5E5DD"?%/N %MI\8ADIFTS=
MQ"6)8TG#F.(1,J ^^"6'$2I1R'X)L0R2*A0<.YK!B+?$T:0(B[L26\&1X&^*
M]!%D86TO*V,ER93VM(8",L518+!228<: ";UDMZ:0EEK>06^A(5@?-7HUSB*
MY14CR7#'"_^%Z[H ^.&.^ E&$/"KJU]=</=^=,<PVA]/]ENMEZV6VVA5[>:K
M@ACO[M.NYA$3(>4OU+6P<=Y*(+1CN7FB",?G?8LU7K ]%4MF$]6@%*<^P_*#
M>9UZF(?O%65>!:4UA3CB3OXM<F7K\<%ORI"L 7WY&^GP+%-E I7C>Z9D2,8Y
M9)G'".M8HK4 WT /7@'F>&>T<WMKV6W\94NS"Q&H5/>4[?<+)EOII\Q&N[W>
M+LT.AI5FN$[A^U1^&3I^0G"NB9Z*3\V&JR )X#F+ #G; Q.%F<"G$BE<8GC
M_C5M9;"4/<Y<5]U#-(>FAG.-F@'_LJ%>I!;#@#YM=@ #/+!LLJ/Q9_7^Z1GL
MV0BF/0/7\4$E52I1*+R=-IL#A7KX5ILEM!L#^K1A(&1(3QBE@0-ELETX -?J
M&48NRBJ7X,R/3G][<TPBV'_OZ/2M=_D12W!V>7I!B]HE''*\SBSR,E7 <S W
M"PT>[:WY=V:,I:Y:4*OI2TY7JR-4U4)\.] BE-O*^*]H_+P^/=F3I7VP]CEI
MDWI[$=$F6B<L%352?"9K";(T'.I2X@>/$K9Q./P+-0C=7!)N[.'#Z6P;A @(
MLCNO5%%A\JCPE61#G/M'P!ANF.*M%7PH]_M>X[I&S"->2(!4:L-Z %&Y"^U#
M,#4ZG ;#UG:R0FAK@#53]V[^8.KJX:0(T!KA'+W?TT'@?,,OXO0M],57LV]$
M8'4AO'*3DXRN<8.,H_ &K:WQ'AZ*-?E]3X3V*(W!\^<UWQRT=-,AMB</V.6?
M]5'#3&6)QZYH* RKAVZ*+,>:1J;&]=-G[B2Y!="_GNV%\-3F6=:VJ74R)Q2/
MQMUU?57LKN@HFE'>9#>>>8Q SSRQ7)=43=U]W.G2&SPYU*WES"C\X7K\6%*&
MQ-.@D\"S IXE59V&85:RB*\DDA5>E:XLT([0@T?'="VT6N0TZM7/>/&C2YBY
MVL85R^[8P">K>T)LR10YM^6\U6I]T&@0,R /*)4V6<U2E++3<H6QHLD9K,Z4
MR UZB?C@HW?T_GS^ ?X%?#J__'!FP\@&M_Y^D% ;%N]]L7?]?XW&_T3[Y\6O
M9!R8.OI<J=*&;BTMB^YUQ.3\E^__L<+UL#KSWD2&DTX1]X)$^++*-K)@ Y5L
>O*Q9[X2XDZB;ALV_1$7_MW.E=-_[#QBVYK2 %0



Marnix Coppens

Marnix Coppens

Reality is that which                   | Artificial Intelligence
when you stop believing                 | stands no chance against
in it doesn't go away. (Philip K. Dick) | Natural Stupidity.