^C will exit the program before the defer in your main can run.

I recommend using my profiling package, github.com/pkg/profile

https://dave.cheney.net/2014/10/22/simple-profiling-package-moved-updated

On Wednesday, 8 February 2017 08:55:33 UTC+11, Jason E. Aten wrote:
>
> How does one reliably obtain a cpu profile written to file?  I tried 
> following the instructions in runtime/pprof.go, but the profile file is 
> most always empty... only on some magical moments can I discover a profile 
> file being written to disk.
>
> I also tried calling f.Sync() on the file hanlde f, from main (below) 
> every minute, but that didn't help. 
>
> Supposedly the profiler is sampling at 100Hz, but I can't seem to get any 
> samples.
>
> repro code:
> $ cat addack.go
> package main
>
> import (
>     "fmt"
>     "log"
>     //"net/http"
>     _ "net/http/pprof"
>     "os"
>     "runtime/pprof"
> )
>
> const ProgramName = "profileme"
>
> func main() {
>
>     f, err := os.Create(fmt.Sprintf("./addack-cpu-profile.%v", 
> os.Getpid()))
>     if err != nil {
>         log.Fatal(err)
>     }
>     pprof.StartCPUProfile(f)
>     defer pprof.StopCPUProfile()
>
>     massiveAddack()
> }
>
> func massiveAddack() {
>     m := 0
>     for {
>         m = addSub(m)
>         fmt.Printf("m=%v\n", m)
>     }
> }
>
> func addSub(n int) int {
>     for i := 0; i < 1000000000; i++ {
>         n += i * i
>     }
>     return n
> }
>
>  $ go run addack.go 
> m=3338615082255021824
> m=6677230164510043648
> m=-8430898826944486144
> m=-5092283744689464320
> m=-1753668662434442496
> m=1584946419820579328
> ^Csignal: interrupt
> $ 
> $ ls -al|grep addack-cpu-profile.31523 
> -rw-rw-r--    1 jason  staff           0 Feb  7 15:50 
> addack-cpu-profile.31523  ## hmm.... profile file is always empty
>  $ 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to