pahole - Finding holes in kernel structs

Garance A Drosehn gad at FreeBSD.org
Thu Feb 12 18:41:29 PST 2009


At 2:08 PM +0000 2/12/09, Andrew Brampton wrote:
>I found this useful tool called pahole[1]. It basically finds holes
>within structs, so for example on my 64bit machine this struct:
>
>struct test {
>    int foo;
>    const char *bar;
>    int blah;
>}
>
>Would have a hole between foo and bar of 4 bytes because both for and
>bar have been aligned on a 8 byte boundary, and the struct would also
>have 4 bytes of padding on the end. However, if I simply moved blah
>between foo and bar then the struct has shrunk by 8 bytes, which could
>be a good thing. This could also help keep structs within single cache
>lines, and just generally keep memory usage to a minimum when the
>struct is used many times (for example in an array).
>
>So I ran the tool pahole over a 7.1 FreeBSD Kernel, and found that
>many of the struct had holes, and some of which could be rearranged to
>fill the gap. I've made the list available here[2]. So my questions
>are two fold:
>
>1) Is it worth my time trying to rearrange structs? If so do you
>think many of my patches would be accepted?

In most cases, re-arranging things is probably not worth it.
That's just my opinion, though.

>2) Is there a way to find out the most heavily used structs? There
>are ~3600 structs, and ~2000 holes, it might be a waste of my time
>fixing the structs which are only used once.

I used a similar tool (which I wrote myself) when making some changes
to 'struct kinfo_proc' some time ago.  The first issue which comes up
is that a hole on one hardware platform may not be a hole on other
platforms.

I think it is perhaps useful to shuffle around fields in a struct if
you're doing some *other* change at the same time.  A tool like this
pahole program seems like a good idea at those times.  And I do think
it can be useful to document the holes which exist, especially if you
take the time to figure out what is happening on different platforms
which have different alignment requirements.

-- 
Garance Alistair Drosehn     =               drosehn at rpi.edu
Senior Systems Programmer               or   gad at FreeBSD.org
Rensselaer Polytechnic Institute;             Troy, NY;  USA


More information about the freebsd-hackers mailing list