Monthly Archives: January 2007

Kill-a-hole: Reorganizing struct layouts

Say a project has this struct:

struct cheese {
char name[52];
char a;
int b;
char c;
int d;
short e;

And we want to see how the layout looks like more precisely:

[acme@filo examples]$ pahole swiss_cheese cheese
/* <11b> /home/acme/git/pahole/examples/swiss_cheese.c:3 */
struct cheese {
char name[52]; /* 0 52 */
char a; /* 52 1 */

/* XXX 3 bytes hole, try to pack */

int b; /* 56 4 */
char c; /* 60 1 */

/* XXX 3 bytes hole, try to pack */

/* — cacheline 1 boundary (64 bytes) — */
int d; /* 64 4 */
short int e; /* 68 2 */
}; /* size: 72, cachelines: 2 */
/* sum members: 64, holes: 2, sum holes: 6 */
/* padding: 2 */
/* last cacheline: 8 bytes */
[acme@filo examples]$

Heck, what a swiss cheese! Surely we can do better, huh? Lets ask pahole for a little help:

[acme@filo examples]$ pahole -kV swiss_cheese cheese
/* moving c(size=1) to after a(offset=52, size=1, hole=3) */
/* moving e(size=2) to after c(offset=53, size=1, hole=2) */

/* <11b> /home/acme/git/pahole/examples/swiss_cheese.c:3 */
struct cheese {
char name[52]; /* 0 52 */
char a; /* 52 1 */
char c; /* 53 1 */
short int e; /* 54 2 */
int b; /* 56 4 */
int d; /* 60 4 */
/* — cacheline 1 boundary (64 bytes) — */
}; /* size: 64, cachelines: 1 */
/* saved 8 bytes and 1 cacheline! */
[acme@filo examples]$

Much better now, no?

Ok, lets try something more interesting, like some Linux kernel structs, the output is big, so here are some links for some structs that spent some jiffies on pahole’s spa:

Type demotion of bigger than needed bitfields will help getting more saved 🙂

Unfolding structs

Check the git cset comment! New rpms are available at the usual place with this new feature, for the curious, here is struct task_struct unfolded.

ctracer made easy

Make sure you have the kernel-debuginfo package installed!

rpm -ivh
mkdir foo
cd foo
ln -s /usr/lib/ctracer/Makefile .
ln -s /usr/lib/ctracer/ctracer_jprobe.c .
make CLASS=sock
insmod ctracer.ko
# do some networking activity or just move the mouse 🙂
dmesg # or tail /var/log/messages
rmmod ctracer

Now to work on kahole, kill a hole, a tool that will reorganize structs to kill holes and that will provide the bits needed for struct “views”, i.e. to specify a list of fields in a struct that are of interest for collection at each probe point and that will also be used to create a userspace utility that will read the relay channel (/sys/kernel/ctracer0) and generate what ostra-cg, the callgraph tool from the OSTRA days needs to generate things such as this session collecting struct task_struct methods calls in the Linux kernel, ah, ostra-cg also produces this methods statistics and from this page you get plottings such as this one for sched_
fork calls
, workload is forgotten right now, but as soon as I get my new test machine we’ll have more interesting graphics 🙂

libdwarves (mostly) explained

In response to a comment I’ve added comments to the ctracer tool source code in an attempt to help people interested in helping improving these tools, check it out!

How many functions receive as a parameter a pointer to a struct?

Implemented a new command line option in pahole: –nr_members, look here for the result using a kernel built for qemu.

Support for more C++ tags

Added support for DW_TAG_reference and DW_AT_specification, still have to work on DW_TAG_namespace and rework the list of tags to make classes within classes to properly be shown in the tools, nowadays its just a flat namespace, which has not been a problem for the tools written so far, but for pdwtags, the dwarf I’m working on now, that will do a complete dump of all tags it is a must.

What are the dwarves?

Answering Avi Alkalay’s request for a description of what are the dwarves I managed to write the rpm spec file description tag, should help in clarifying the usefulness of these tools 🙂

RPM packages for the dwarves

Got cmake in shape for installing the tools, resulting rpms packages are available here, please test and tell me your impressions!

The rpms were built on a Mandriva Cooker machine, but were already tested by friends on at least a Fedora Core 6 machine, working perfectly.

Lots of improvements done on the dwarves

Mostly on class nesting, look at this commit to see the goodies, hint, look for anonymous unions and structs 🙂

Fuck for all to see

Want to get your paycheck boosted? Go to a beach, find some celebrity going down the drain, fuck her, make sure that it will get recorded and sent to some place where it’ll get into the screens of curious people, share the profits, yes, it is that easy.

Its not easy to be a brazilian, judges could help our people feel more proud after all the messes we’ve been seeing from people supposed to be keeping law, order and decency, go, go, set the standards, but please, don’t make it to be so lame as they are right now.

Update: some friends even thought that my machine was invaded for such a rude language, sorry, but to provide some context on the outrage that is motivating many such blog posts look no further than here.