func profileWriter(w io.Writer) { startTime := time.Now() // This will buffer the entire profile into buf and then // translate it into a profile.Profile structure. This will // create two copies of all the data in the profile in memory. // TODO(matloob): Convert each chunk of the proto output and // stream it out instead of converting the entire profile. var buf bytes.Buffer for { data := runtime.CPUProfile() if data == nil { break } buf.Write(data) } profile, err := protopprof.TranslateCPUProfile(buf.Bytes(), startTime) if err != nil { // The runtime should never produce an invalid or truncated profile. // It drops records that can't fit into its log buffers. panic(fmt.Errorf("could not translate binary profile to proto format: %v", err)) } profile.Write(w) cpu.done <- true }
func profileWriter(w io.Writer) { var buf bytes.Buffer for { data := runtime.CPUProfile() buf.Write(data) if data == nil { break } } p, err := protopprof.TranslateCPUProfile(buf.Bytes(), cpu.startTime) if err != nil { panic(err) } p.RemapAll() protopprof.CleanupDuplicateLocations(p) protopprof.Symbolize(p) p.Write(w) cpu.done <- true }