-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50c6f1f
commit 0cd1d4a
Showing
2 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"runtime" | ||
"runtime/pprof" | ||
) | ||
|
||
const ( | ||
PProfHeapFile = "PPROF_HEAP_FILE" | ||
PProfCPUFile = "PPROF_CPU_FILE" | ||
) | ||
|
||
func instrumentPprof() func() { | ||
heapFile := createProfileFile(os.Getenv(PProfHeapFile)) | ||
cpuFile := createProfileFile(os.Getenv(PProfCPUFile)) | ||
if cpuFile != nil { | ||
if err := pprof.StartCPUProfile(cpuFile); err != nil { | ||
log.Fatal("could not start CPU profile: ", err) | ||
} | ||
} | ||
|
||
return func() { | ||
if cpuFile != nil { | ||
pprof.StopCPUProfile() | ||
if err := cpuFile.Close(); err != nil { | ||
fmt.Printf("failed to close CPU profile: %v\n", err) | ||
} | ||
} | ||
if heapFile != nil { | ||
// get up-to-date statistics | ||
runtime.GC() // nolint:revive | ||
if err := pprof.WriteHeapProfile(heapFile); err != nil { | ||
log.Fatal("could not write memory profile: ", err) | ||
} | ||
if err := heapFile.Close(); err != nil { | ||
fmt.Printf("failed to close heap profile: %v\n", err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func createProfileFile(path string) *os.File { | ||
if path == "" { | ||
return nil | ||
} | ||
|
||
f, err := os.Create(path) | ||
if err != nil { | ||
log.Fatalf("failed to create %q file for profile: %v", path, err) | ||
} | ||
|
||
return f | ||
} |