pkg / profile

Simple profiling for Go
BSD 2-Clause "Simplified" License
2k stars 122 forks source link

0 Byte files when attempting to generate CPU and Memory profiles #48

Closed manasbhatnagar closed 5 years ago

manasbhatnagar commented 5 years ago

I have been trying to collect CPU and Memory profiles for a program I wrote. I have imported "github.com/pkg/profile" and added the appropriate line for either CPU or Memory profile.

import (
    ...
    "github.com/pkg/profile"
    ...
)

...

func main() {
    // memory profile
    defer profile.Start(profile.MemProfile, profile.ProfilePath("./profiles/")).Stop()
    // OR cpu profile
    defer profile.Start(profile.CPUProfile, profile.ProfilePath("./profiles/")).Stop()
}

When I run the program, I see messages like the following on the command line:

2019/10/11 12:33:24 profile: memory profiling enabled (rate 4096), profiles/mem.pprof

However, the cpu.pprof or mem.pprof files are always 0 bytes. This used to work before for the same program, I do not understand why this no longer works.

ls -alhrt profiles
total 8.0K
-rw-rw-r-- 1 manas manas    0 Oct 11 12:33 mem.pprof
drwxrwxr-x 9 manas manas 4.0K Oct 11 12:33 ..
drwxrwxr-x 2 manas manas 4.0K Oct 11 12:33 .
davecheney commented 5 years ago

Do you see a second line saying the profile is stopped?

On 12 Oct 2019, at 06:57, manasbhatnagar notifications@github.com wrote:

 I have been trying to collect CPU and Memory profiles for a program I wrote. I have imported "github.com/pkg/profile" and added the appropriate line for either CPU or Memory profile.

import ( ... "github.com/pkg/profile" ... )

...

func main() { // memory profile defer profile.Start(profile.MemProfile, profile.ProfilePath("./profiles/")).Stop() // OR cpu profile defer profile.Start(profile.CPUProfile, profile.ProfilePath("./profiles/")).Stop() } When I run the program, I see messages like the following on the command line:

2019/10/11 12:33:24 profile: memory profiling enabled (rate 4096), profiles/mem.pprof However, the cpu.pprof or mem.pprof files are always 0 bytes. This used to work before for the same program, I do not understand why this no longer works.

ls -alhrt profiles total 8.0K -rw-rw-r-- 1 manas manas 0 Oct 11 12:33 mem.pprof drwxrwxr-x 9 manas manas 4.0K Oct 11 12:33 .. drwxrwxr-x 2 manas manas 4.0K Oct 11 12:33 . — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

manasbhatnagar commented 5 years ago

No, the line saying that the profile is disabled at the end of program execution is not seen.

e.g.

2019/10/11 13:37:59 profile: memory profiling disabled, profiles/mem.pprof

I had a syscall.Exit(0) line in my code which may have been preventing the profiles from being written to the disk. Removing this line and re-running produces the profile files as expected.

davecheney commented 5 years ago

Yes, that'll do it. Don't call syscall.Exit as is prevents defers running.

stigok commented 4 years ago

FWIW: Empty profiles also is the case if the program exits through log.Fatalf. Calling Stop() before solves it :)