ELC2009: Visualizing memory usage with smem
One of the more frustrating things to try and figure out on Linux systems is how much memory is actually being used by a process. The ps command offers something of a view into memory usage, but adding up the numbers for various types of memory never yields a sensible result. It is against this backdrop that Matt Mackall presented his smem tool at this year's Embedded Linux Conference.
There is an "accounting problem
" when users try to look at the
memory usage in their systems, according to Mackall. The kernel saves lots
of memory by sharing various pages between processes, but then when it
reports the memory usage, it counts these shared pages multiple times. The
kernel will
also allocate more memory than is actually available, "in the belief
that it won't be used
". This means that users and developers can't
get a good sense of how the memory is used which leads them to "just
throw more memory at the problem
".
In 2007, Mackall attacked the
problem from the kernel side by creating a set of patches that
implemented the pagemap file for each process in /proc.
This binary file "exposes the mapping from virtual to
physical
" memory, which can be used to get a better look at memory
usage.
He also created some user space tools to read the pagemap files
(along with the related /proc/kpagemap for the kernel). As part
of that, he "developed a pair of concepts to give meaningful
measures
" to memory usage.
One of those measures is proportional set size (PSS) which
represents a process's "fair share
" of shared pages. If a
page is shared by five processes, each gets one-fifth of a page added to
its PSS. The other measure is the unique set size (USS) which is the
memory devoted exclusively to the process—how much would be
returned to the system if that process were killed.
He then submitted the pagemap code for inclusion into the mainline. As part of
that process, he got "lots of help
" from various folks, added
a direct PSS calculation, and redesigned the code and its interface. Linus
Torvalds was not very impressed, and called the code "crap
",
but Mackall was able to convince him to include it by listing all of
the people that had assisted as proof that it was a desired feature.
Unfortunately, the changes that were made to pagemap on its way into the
mainline broke all of the
user-space tools he had written and no one else released any tools based on
pagemap.
![[smem bar chart]](https://static.lwn.net/images/smem_bar_sm.png)
So, now, in "take 2
", Mackall is trying to "write a useful
tool and hope it catches on
". The idea behind smem is to
integrate information from multiple sources to provide useful memory usage
information for developers, administrators, and users. In addition to the
expected textual output, Mackall included visualization aids in the form of
pie and bar charts.
With that introduction and history out of the way, Mackall went on to demonstrate the smem program. At its simplest, without any arguments, it produces a list of processes running on the system showing the process id, user, and command, along with four measures of memory used for each. Those measures are the amount of swap, USS, PSS, and resident set size (RSS), with the list being sorted by PSS. But, as Mackall showed, that output can be rearranged, sorted, and filtered by a variety of parameters.
In addition to looking at memory from the perspective of processes,
smem can look at memory usage by mapping or user, and all three
can be used in regular expression filters. As he was showing various
options, Mackall commented on a few
programs running on his laptop, noting that gweather used 5M for "32
square pixels on the screen
", and that tomboy is "useful, but
I'm not sure it's 6.9M of useful
".
Since the target audience was embedded developers—and conference sponsor
CE Linux Forum funded the work—Mackall turned to describing ways to
use smem in embedded environments. The program itself is a Python
application, which is "not that huge, but not small
", so
"[you] don't want to run it on your phone
". What is needed is
a way to capture the data, so that it can be pulled over to another machine
to "slice and dice it
" there.
To that end, smem will read a tar file that has been collected
from the /proc filesystem on the target machine. Mackall has
created a simple script to grab the relevant pieces from /proc and
create a .tgz file.
Mackall also demonstrated a system-wide view of memory that would be useful for embedded developers who are trying to size the memory requirements for their device. By passing arguments that give the amount of installed memory, along with the path to an uncompressed, unstripped kernel image, smem can produce output like:
$ ./smem -R 2G -K ~/linux-2.6/arch/x86/boot/compressed/vmlinux -k -w -t Area Used Cache Noncache firmware/hardware 35.2M 0 35.2M kernel image 6.1M 0 6.1M kernel dynamic memory 1.5G 1.3G 189.6M userspace memory 283.5M 85.8M 197.7M free memory 188.7M 188.7M 0 ---------------------------------------------------------- 5 2.0G 1.6G 428.6MThis shows that with the current workload on this machine, 428M of memory is required. If this workload is known to be fixed, 512M of RAM could reliably be specified for the system.
![[smem pie chart]](https://static.lwn.net/images/smem_pie_sm.png)
All of the smem output can be converted to rudimentary pie and bar charts, which can be saved in a variety of formats (PNG, SVG, JPG, EPS, and more). As Mackall explained, there are still lots of tweaks to be made to the output, but it is basically functional and allows some interaction (zooming in for example).
A better GUI is one of things on the wish list for further smem development. First off, Mackall would like to get some users for the tool that are reporting bugs and hopefully providing patches as well—interested folks are directed at the download page or the project page for additional info. In addition, better capture tools (capturing via TCP for example), adding more sources of data (CPU usage, dirty memory, ...), adding support for better data from the kernel, and improved visualization are all things he would like to see added. It is functional and useful now, but could become something far better down the road.
Posted Apr 30, 2009 3:24 UTC (Thu)
by dberkholz (guest, #23346)
[Link] (6 responses)
Posted Apr 30, 2009 4:29 UTC (Thu)
by pabs (subscriber, #43278)
[Link] (1 responses)
Posted May 1, 2009 17:04 UTC (Fri)
by giraffedata (guest, #1954)
[Link]
And to put a finer point on it, it is a political problem. For many of us, it is far easier, and more satisfying, to develop and distribute an alternative to xyz than to get the people who control xyz to see things our way and cooperate to distribute our work.
Posted Apr 30, 2009 8:57 UTC (Thu)
by marcH (subscriber, #57642)
[Link] (1 responses)
I would be interested to know about pre-existing top variants that give information as simple to understand as the pie chart found in the article. Could you please name (at least) one?
I would also be interested to understand why Nackall had to write some kernel patches if such simple information was already available. Any clue?
Posted May 1, 2009 16:53 UTC (Fri)
by giraffedata (guest, #1954)
[Link]
I think you missed the point. dberkholz isn't saying there is already a top variant that has all the features of smem. He's suggesting it would have been better to add the features to one of the existing ones than to create a separate program.
Posted Apr 30, 2009 10:24 UTC (Thu)
by jldugger (guest, #57576)
[Link]
Posted May 2, 2009 2:26 UTC (Sat)
by GreyWizard (guest, #1026)
[Link]
In other words, people keep writing new ones because this is the most effective way to make progress.
Posted Apr 30, 2009 6:21 UTC (Thu)
by aleXXX (subscriber, #2742)
[Link] (4 responses)
Alex
Posted Apr 30, 2009 13:05 UTC (Thu)
by robertknight (guest, #42536)
[Link] (2 responses)
The storage of history in Konsole is not space efficient and there is a lot that could be done to improve it. Currently it uses 11 bytes per non-blank character (2 for the character itself + 9 for formatting) + (at least) 17 bytes per line. However, given the default settings of a maximum of 1000 lines, with a relatively small average length and normal usage habits of 4-10 tabs open means that memory usage does not grow huge.
One important thing to note is that no music player or web browser is open which makes the picture look quite different from that most users would see - with Firefox being the dominating application by an order of magnitude.
The standard top utility, as is well known, does not give very useful figures. Modern versions of KDE's System Monitor are much better in my view.
Posted Apr 30, 2009 13:11 UTC (Thu)
by robertknight (guest, #42536)
[Link]
The standard top utility, as is well known, does not give very useful figures. Modern versions of KDE's System Monitor are much better in my view and graphical tools like this are a good way of getting people to notice inefficient applications.
Posted Apr 30, 2009 14:24 UTC (Thu)
by pr1268 (guest, #24648)
[Link]
One important thing to note is that no music player or web browser is open which makes the picture look quite different from that most users would see - with Firefox being the dominating application by an order of magnitude. Not necessarily so... The pie chart diagram mentions that smem was run only for processes whose name begins with a 'k'. But, I agree that Firefox would undoubtedly be the biggest slice of pie if it were shown.
Posted Apr 30, 2009 14:00 UTC (Thu)
by jake (editor, #205)
[Link]
huge history, multiple windows, each with multiple tabs ... i also filtered on processes starting with 'k' so that may be part of why it looks a bit funny as well.
jake
Posted Apr 30, 2009 11:18 UTC (Thu)
by michich (guest, #17902)
[Link] (2 responses)
Posted Apr 30, 2009 17:55 UTC (Thu)
by mattdm (subscriber, #18)
[Link]
Posted Apr 30, 2009 18:02 UTC (Thu)
by mattdm (subscriber, #18)
[Link]
I'm not particularly attached, though (and over-busy already) so feel free to take over.
Posted May 1, 2009 6:18 UTC (Fri)
by sandmann (subscriber, #473)
[Link]
Posted May 1, 2009 17:11 UTC (Fri)
by rahulsundaram (subscriber, #21946)
[Link] (1 responses)
---
So, use Gnote instead - port of Tomboy to C++
# yum info gnote
Loaded plugins: dellsysidplugin2, presto, refresh-packagekit
Installed Packages
Posted May 4, 2009 13:11 UTC (Mon)
by jlokier (guest, #52227)
[Link]
Posted May 3, 2009 22:01 UTC (Sun)
by wcooley (guest, #1233)
[Link]
Posted May 7, 2009 13:10 UTC (Thu)
by deleteme (guest, #49633)
[Link]
ELC2009: Visualizing memory usage with smem
ELC2009: Visualizing memory usage with smem
ELC2009: Visualizing memory usage with smem
This is a standard problem with free software developers. Forks, rewrites, embedded code copies, hundreds of different editors/music players/etc. This is a social problem that will probably never go away,
ELC2009: Visualizing memory usage with smem
ELC2009: Visualizing memory usage with smem
I would be interested to know about pre-existing top variants that give information as simple to understand as the pie chart found in the article.
ELC2009: Visualizing memory usage with smem
ELC2009: Visualizing memory usage with smem
ELC2009: Visualizing memory usage with smem
history collected ?
ELC2009: Visualizing memory usage with smem
ELC2009: Visualizing memory usage with smem
ELC2009: Visualizing memory usage with smem
ELC2009: Visualizing memory usage with smem
> history collected ?
ELC2009: Visualizing memory usage with smem
As soon as Matt clarifies what the license is, I'm going to submit it to Fedora.
ELC2009: Visualizing memory usage with smem
ELC2009: Visualizing memory usage with smem
ELC2009: Visualizing memory usage with smem
Use Gnote instead
Name : gnote
Arch : i586
Version : 0.3.0
Release : 1.fc11
Size : 3.3 M
Repo : installed
Summary : Note-taking application
URL : http://live.gnome.org/Gnote
License : GPLv3+
Use Gnote instead
ELC2009: Visualizing memory usage with smem
What's a modern kernel?