Patch to avoid gprof profiling overwrites

Started by korryd@enterprisedb.comover 19 years ago13 messagespatches
Jump to latest
#1korryd@enterprisedb.com
korryd@enterprisedb.com

It's difficult to profile a backend server process (using gprof)
because each process overwrites any earlier profile as it exits.

It is especially tricky to nab a useful profile if you happen to have
autovacuum enabled.

This patch reduces the problem by forcing the backend to 'cd' to a new
directory ($PGDATA/gprof/pid) just before calling exit(), but only if
the backend was compiled with -DLINUX_PROFILE.

I've tested this with Linux, but not with other host architectures.

-- Korry

--
Korry Douglas korryd@enterprisedb.com
EnterpriseDB http://www.enterprisedb.com

#2korryd@enterprisedb.com
korryd@enterprisedb.com
In reply to: korryd@enterprisedb.com (#1)
Re: Patch to avoid gprof profiling overwrites

And the patch is where?

You caught me; guess I'd better make something up fast, huh?

Here it is, thanks.

-- Korry

---------------------------------------------------------------------------

korryd@enterprisedb.com wrote:

It's difficult to profile a backend server process (using gprof)
because each process overwrites any earlier profile as it exits.

It is especially tricky to nab a useful profile if you happen to have
autovacuum enabled.

This patch reduces the problem by forcing the backend to 'cd' to a new
directory ($PGDATA/gprof/pid) just before calling exit(), but only if
the backend was compiled with -DLINUX_PROFILE.

I've tested this with Linux, but not with other host architectures.

-- Korry

--
Korry Douglas korryd@enterprisedb.com
EnterpriseDB http://www.enterprisedb.com

--
Korry Douglas korryd@enterprisedb.com
EnterpriseDB http://www.enterprisedb.com

Attachments:

gprof.patchtext/x-patch; charset=UTF-8; name=gprof.patchDownload+30-0
#3Mark Kirkwood
mark.kirkwood@catalyst.net.nz
In reply to: korryd@enterprisedb.com (#2)
Re: [pgsql-patches] Patch to avoid gprof profiling overwrites

korryd@enterprisedb.com wrote:

And the patch is where?

You caught me; guess I'd better make something up fast, huh?

Here it is, thanks.

-- Korry

---------------------------------------------------------------------------

korryd@enterprisedb.com <mailto:korryd@enterprisedb.com> wrote:

It's difficult to profile a backend server process (using gprof)
because each process overwrites any earlier profile as it exits.

It is especially tricky to nab a useful profile if you happen to have
autovacuum enabled.

This patch reduces the problem by forcing the backend to 'cd' to a new
directory ($PGDATA/gprof/pid) just before calling exit(), but only if
the backend was compiled with -DLINUX_PROFILE.

Nice addition - tested on FreeBSD 6.2, works great!

The name for the define variable could perhaps be better - feels silly
adding -DLINUX_PROFILE on Freebsd! (maybe just PROFILE or GPROF_PROFILE?).

Cheers

Mark

#4korryd@enterprisedb.com
korryd@enterprisedb.com
In reply to: Mark Kirkwood (#3)
Re: [pgsql-patches] Patch to avoid gprof profilingoverwrites

Nice addition - tested on FreeBSD 6.2, works great!

Cool - thanks for the quick feedback.

The name for the define variable could perhaps be better - feels silly
adding -DLINUX_PROFILE on Freebsd! (maybe just PROFILE or GPROF_PROFILE?).

That wasn't my choice, there is other code elsewhere that depends on
that symbol, I just added a little bit more.

-- Korry

#5Mark Kirkwood
mark.kirkwood@catalyst.net.nz
In reply to: korryd@enterprisedb.com (#4)
Re: [pgsql-patches] Patch to avoid gprof profilingoverwrites

korryd@enterprisedb.com wrote:

The name for the define variable could perhaps be better - feels silly
adding -DLINUX_PROFILE on Freebsd! (maybe just PROFILE or GPROF_PROFILE?).

That wasn't my choice, there is other code elsewhere that depends on
that symbol, I just added a little bit more.

Right - but LINUX_PROFILE was added to correct Linux specific oddities
with the time counter accumulation, whereas your patch is not Linux
specific at all. So I think a more representative symbol is required.

Cheers

Mark

#6Mark Kirkwood
mark.kirkwood@catalyst.net.nz
In reply to: Mark Kirkwood (#5)
Re: [pgsql-patches] Patch to avoid gprof profilingoverwrites

Mark Kirkwood wrote:

korryd@enterprisedb.com wrote:

The name for the define variable could perhaps be better - feels
silly adding -DLINUX_PROFILE on Freebsd! (maybe just PROFILE or
GPROF_PROFILE?).

That wasn't my choice, there is other code elsewhere that depends on
that symbol, I just added a little bit more.

Right - but LINUX_PROFILE was added to correct Linux specific oddities
with the time counter accumulation, whereas your patch is not Linux
specific at all. So I think a more representative symbol is required.

In fact - thinking about this a bit more, probably a construction like:

#if defined(LINUX_PROFILE) || defined(PROFILE)

or similar would work - because I think those of us not on Linux do
*not* want to define LINUX_PROFILE, as our timer accumulation for forked
process works fine as it is...but we don't want to make Linux guys have
to define LINUX_PROFILE *and* PROFILE...

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Mark Kirkwood (#5)
Re: [pgsql-patches] Patch to avoid gprof profilingoverwrites

Mark Kirkwood <markir@paradise.net.nz> writes:

Right - but LINUX_PROFILE was added to correct Linux specific oddities
with the time counter accumulation, whereas your patch is not Linux
specific at all. So I think a more representative symbol is required.

Yeah, that was my problem with the patch too. The issue that it's
addressing isn't Linux-specific in the least.

Is there a way to detect via #if that profiling is enabled? I wouldn't
expect a really portable answer, but maybe there's something that works
for gcc?

regards, tom lane

#8Neil Conway
neilc@samurai.com
In reply to: Tom Lane (#7)
Re: [pgsql-patches] Patch to avoid gprof profilingoverwrites

On Thu, 2007-02-01 at 00:59 -0500, Tom Lane wrote:

Is there a way to detect via #if that profiling is enabled? I wouldn't
expect a really portable answer, but maybe there's something that works
for gcc?

What about a "--enable-gprof" (or "--enable-profiling"?) configure flag?
This could add the appropriate compiler flags to CFLAGS, enable
LINUX_PROFILE if on Linux, and enable the "gprof/pid" mkdir().

-Neil

#9Peter Eisentraut
peter_e@gmx.net
In reply to: Neil Conway (#8)
Re: [pgsql-patches] Patch to avoid gprof profilingoverwrites

Neil Conway wrote:

What about a "--enable-gprof" (or "--enable-profiling"?) configure
flag? This could add the appropriate compiler flags to CFLAGS, enable
LINUX_PROFILE if on Linux, and enable the "gprof/pid" mkdir().

That would really only work for GCC, wouldn't it?

--
Peter Eisentraut
http://developer.postgresql.org/~petere/

#10korryd@enterprisedb.com
korryd@enterprisedb.com
In reply to: Tom Lane (#7)
Re: [pgsql-patches] Patch to avoid gprofprofilingoverwrites

Right - but LINUX_PROFILE was added to correct Linux specific oddities
with the time counter accumulation, whereas your patch is not Linux
specific at all. So I think a more representative symbol is required.

Yeah, that was my problem with the patch too. The issue that it's
addressing isn't Linux-specific in the least.

Is there a way to detect via #if that profiling is enabled? I wouldn't
expect a really portable answer, but maybe there's something that works
for gcc?

You're right - I hadn't really looked beyond the problem that I was
trying to solve.

The technique should work for gprof on other platforms, but, as you
point out, LINUX_PROFILE is unique to Linux (no, I hadn't noticed that,
but it seems pretty obvious in retrospect).

I like Neil's idea of a using --enable-profiling configure flag. Every
time I need to profile, I have to go search the archives for 'gprof' -
it would be nice to see --enable-profiling when I configure --help.

-- Korry

#11Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#9)
Re: [pgsql-patches] Patch to avoid gprof profilingoverwrites

Peter Eisentraut <peter_e@gmx.net> writes:

Neil Conway wrote:

What about a "--enable-gprof" (or "--enable-profiling"?) configure
flag? This could add the appropriate compiler flags to CFLAGS, enable
LINUX_PROFILE if on Linux, and enable the "gprof/pid" mkdir().

That would really only work for GCC, wouldn't it?

Well, yeah, but that's what many of us use anyway. I would envision it
as adding $(PROFILE) to CFLAGS, and then there would be one place
to adjust "-pg" to something else for another compiler --- perhaps the
template files could be given a chance to change PROFILE to something
else.

regards, tom lane

#12korryd@enterprisedb.com
korryd@enterprisedb.com
In reply to: Tom Lane (#11)
Re: [pgsql-patches] Patch to avoid gprofprofilingoverwrites

What about a "--enable-gprof" (or "--enable-profiling"?) configure
flag? This could add the appropriate compiler flags to CFLAGS, enable
LINUX_PROFILE if on Linux, and enable the "gprof/pid" mkdir().

That would really only work for GCC, wouldn't it?

Well, yeah, but that's what many of us use anyway. I would envision it
as adding $(PROFILE) to CFLAGS, and then there would be one place
to adjust "-pg" to something else for another compiler --- perhaps the
template files could be given a chance to change PROFILE to something
else.

I don't feel competent to muck around with configure.in (sorry, I'm not
tying to shirk the work, I've just never had any success in writing
configure/automake/autoconf stuff - I have the "leaping goats" book, but
I need a small meaningful example to start with).

Can someone else volunteer to make this change? And then forward the
patch to me so I can learn something useful about how to change
configure.in without breaking it?

-- Korry

#13Bruce Momjian
bruce@momjian.us
In reply to: korryd@enterprisedb.com (#12)
Re: [pgsql-patches] Patch to avoid gprofprofilingoverwrites

korryd@enterprisedb.com wrote:

What about a "--enable-gprof" (or "--enable-profiling"?) configure
flag? This could add the appropriate compiler flags to CFLAGS, enable
LINUX_PROFILE if on Linux, and enable the "gprof/pid" mkdir().

That would really only work for GCC, wouldn't it?

Well, yeah, but that's what many of us use anyway. I would envision it
as adding $(PROFILE) to CFLAGS, and then there would be one place
to adjust "-pg" to something else for another compiler --- perhaps the
template files could be given a chance to change PROFILE to something
else.

I don't feel competent to muck around with configure.in (sorry, I'm not
tying to shirk the work, I've just never had any success in writing
configure/automake/autoconf stuff - I have the "leaping goats" book, but
I need a small meaningful example to start with).

Can someone else volunteer to make this change? And then forward the
patch to me so I can learn something useful about how to change
configure.in without breaking it?

I can work on this.

--
Bruce Momjian bruce@momjian.us
EnterpriseDB http://www.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +