@cryptotaxi247 / kubo / commits / d56917d86

fix(main): profile at top-level since work may be async

Brian Tiger Chow committed Jan 12, 2015 at 20:12 UTC d56917d86014d1a724d1f0fd8482e21889037857
1 file changed +22 -10
cmd/ipfs/main.go
+22 -10
@@ -67,6 +67,13 @@ func main() {
67 fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
68 }
69
70 + stopFunc, err := profileIfEnabled()
71 + if err != nil {
72 + printErr(err)
73 + os.Exit(1)
74 + }
75 + defer stopFunc() // to be executed as late as possible
76 +
77 // this is a local helper to print out help text.
78 // there's some considerations that this makes easier.
79 printHelp := func(long bool, w io.Writer) {
@@ -153,16 +160,6 @@ func (i *cmdInvocation) Run(ctx context.Context) (output io.Reader, err error) {
160 u.SetDebugLogging()
161 }
162
156 - // if debugging, let's profile.
157 - // TODO maybe change this to its own option... profiling makes it slower.
158 - if u.Debug {
159 - stopProfilingFunc, err := startProfiling()
160 - if err != nil {
161 - return nil, err
162 - }
163 - defer stopProfilingFunc() // to be executed as late as possible
164 - }
165 -
163 res, err := callCommand(ctx, i.req, Root, i.cmd)
164 if err != nil {
165 return nil, err
@@ -511,3 +508,18 @@ func allInterruptSignals() chan os.Signal {
508 syscall.SIGTERM)
509 return sigc
510 }
511 +
512 +func profileIfEnabled() (func(), error) {
513 + // FIXME this is a temporary hack so profiling of asynchronous operations
514 + // works as intended.
515 + if u.GetenvBool("DEBUG") || os.Getenv("IPFS_LOGGING") == "debug" {
516 + u.Debug = true
517 + u.SetDebugLogging()
518 + stopProfilingFunc, err := startProfiling() // TODO maybe change this to its own option... profiling makes it slower.
519 + if err != nil {
520 + return nil, err
521 + }
522 + return stopProfilingFunc, nil
523 + }
524 + return func() {}, nil
525 +}