|
|
Subscribe / Log in / New account

ELC2009: Visualizing memory usage with smem

By Jake Edge
April 29, 2009

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]

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.6M
This 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]

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.



to post comments

ELC2009: Visualizing memory usage with smem

Posted Apr 30, 2009 3:24 UTC (Thu) by dberkholz (guest, #23346) [Link] (6 responses)

There are so many top variants out there. Why do people keep writing new ones instead of adding features to the existing ones?

ELC2009: Visualizing memory usage with smem

Posted Apr 30, 2009 4:29 UTC (Thu) by pabs (subscriber, #43278) [Link] (1 responses)

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, so now is the time to get over it and worry about something else.

ELC2009: Visualizing memory usage with smem

Posted May 1, 2009 17:04 UTC (Fri) by giraffedata (guest, #1954) [Link]

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,

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.

ELC2009: Visualizing memory usage with smem

Posted Apr 30, 2009 8:57 UTC (Thu) by marcH (subscriber, #57642) [Link] (1 responses)

> There are so many top variants out there. Why do people keep writing new ones instead of adding features to the existing ones?

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?

ELC2009: Visualizing memory usage with smem

Posted May 1, 2009 16:53 UTC (Fri) by giraffedata (guest, #1954) [Link]

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.

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.

ELC2009: Visualizing memory usage with smem

Posted Apr 30, 2009 10:24 UTC (Thu) by jldugger (guest, #57576) [Link]

The top manpage is already long enough, thank you.

ELC2009: Visualizing memory usage with smem

Posted May 2, 2009 2:26 UTC (Sat) by GreyWizard (guest, #1026) [Link]

This is a fantastically unproductive line of reasoning. Fitting the features of smem into top might be desirable, but requiring this as a precondition of releasing the smem work at all would be insane. Nothing prevents a motivated individual from taking on the integration work afterward.

In other words, people keep writing new ones because this is the most effective way to make progress.

ELC2009: Visualizing memory usage with smem

Posted Apr 30, 2009 6:21 UTC (Thu) by aleXXX (subscriber, #2742) [Link] (4 responses)

Why is konsole *that* big in the first screenshot ? Does it have a huge
history collected ?

Alex

ELC2009: Visualizing memory usage with smem

Posted Apr 30, 2009 13:05 UTC (Thu) by robertknight (guest, #42536) [Link] (2 responses)

Probably. Memory usage will depend mostly on the number of open tabs (or windows) and the size of the scrollback in each.

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.

ELC2009: Visualizing memory usage with smem

Posted Apr 30, 2009 13:11 UTC (Thu) by robertknight (guest, #42536) [Link]

I left of the end of my previous response.

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.

ELC2009: Visualizing memory usage with smem

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.

ELC2009: Visualizing memory usage with smem

Posted Apr 30, 2009 14:00 UTC (Thu) by jake (editor, #205) [Link]

> Why is konsole *that* big in the first screenshot ? Does it have a huge
> history collected ?

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

ELC2009: Visualizing memory usage with smem

Posted Apr 30, 2009 11:18 UTC (Thu) by michich (guest, #17902) [Link] (2 responses)

I made an RPM package: http://michich.fedorapeople.org/smem/
As soon as Matt clarifies what the license is, I'm going to submit it to Fedora.

ELC2009: Visualizing memory usage with smem

Posted Apr 30, 2009 17:55 UTC (Thu) by mattdm (subscriber, #18) [Link]

ELC2009: Visualizing memory usage with smem

Posted Apr 30, 2009 18:02 UTC (Thu) by mattdm (subscriber, #18) [Link]

I had the same thought, by the way, and hadn't seen a review request in progress. So: https://bugzilla.redhat.com/show_bug.cgi?id=498490

I'm not particularly attached, though (and over-busy already) so feel free to take over.

ELC2009: Visualizing memory usage with smem

Posted May 1, 2009 6:18 UTC (Fri) by sandmann (subscriber, #473) [Link]

An interesting tool would be one that would display a representation of the physical RAM in the machine, and what that RAM is being used for and by how many processes. This is much more useful than trying to blame individual processes, because it provides an answer to the question "if I do optimization x, how much actual RAM will it yield?"

Use Gnote instead

Posted May 1, 2009 17:11 UTC (Fri) by rahulsundaram (subscriber, #21946) [Link] (1 responses)

tomboy is "useful, but I'm not sure it's 6.9M of useful"

---

So, use Gnote instead - port of Tomboy to C++

# yum info gnote

Loaded plugins: dellsysidplugin2, presto, refresh-packagekit

Installed Packages
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

Posted May 4, 2009 13:11 UTC (Mon) by jlokier (guest, #52227) [Link]

Comments on the recent Gnote article imply that Gnote, although lighter to start, uses similar memory to Tomboy when it's running - probably due to the Gtk+ libraries rather than the application.

ELC2009: Visualizing memory usage with smem

Posted May 3, 2009 22:01 UTC (Sun) by wcooley (guest, #1233) [Link]

This looks like great work; I've often been troubled by the inability to reliably calculate memory usage from ps, top, etc. Now if we can get support for these additional stats in sar/sysstat and Net-SNMP, then we'll be able to collect long-term process data.

What's a modern kernel?

Posted May 7, 2009 13:10 UTC (Thu) by deleteme (guest, #49633) [Link]

Hmm isn't 2.6.27 pretty new; It's not in Debian stable, nor in Ubuntu LTS. Maybe I'm just old and slow nowdays..


Copyright © 2009, Eklektix, Inc.
This article may be redistributed under the terms of the Creative Commons CC BY-SA 4.0 license
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds