Example #1
0
func startProfiler(filename *string) error {
	if filename == nil || *filename == "" {
		return nil
	}

	cpuProfileFile, err := os.Create(*filename)
	if err != nil {
		return err
	}
	runtime.SetCPUProfileRate(500)
	stopped := make(chan bool)

	go waitForSignals(stopped)

	go func() {
		for {
			select {
			default:
				data := runtime.CPUProfile()
				if data == nil {
					cpuProfileFile.Close()
					stopped <- true
					break
				}
				cpuProfileFile.Write(data)
			}
		}
	}()
	return nil
}
Example #2
0
File: pprof.go Project: oshimaya/go
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
}
Example #3
0
func startProfiler() error {
	if profileFilename == nil || *profileFilename == "" {
		return nil
	}

	startCHeapProfiler(*profileFilename)
	runtime.MemProfileRate = 1024

	cpuProfileFile, err := os.Create(fmt.Sprintf("%s.cpu", *profileFilename))
	if err != nil {
		return err
	}
	runtime.SetCPUProfileRate(500)
	stopped := make(chan bool)

	go waitForSignals(*profileFilename, stopped)

	go func() {
		for {
			select {
			default:
				data := runtime.CPUProfile()
				if data == nil {
					cpuProfileFile.Close()
					stopped <- true
					break
				}
				cpuProfileFile.Write(data)
			}
		}
	}()
	return nil
}
Example #4
0
func profileWriter(w io.Writer) {
	for {
		data := runtime.CPUProfile()
		if data == nil {
			break
		}
		w.Write(data)
	}
	cpu.done <- true
}
Example #5
0
File: main.go Project: timtadh/sfp
func profileWriter(w io.Writer) {
	for {
		data := runtime.CPUProfile()
		if data == nil {
			break
		}
		errors.Logf("DEBUG", "profileWriter got data %v", len(data))
		w.Write(data)
	}
	profileDone <- true
}
Example #6
0
func main() {
	select {
	case <-time.After(time.Second):
		fmt.Println(time.Minute.Seconds(), time.Second)
	}
	select {
	case <-time.After(time.Minute):
		fmt.Println(time.Minute.Seconds(), time.Second)
	}
	var kvlist []int
	kvlist = make([]int, 0, 10)
	kvlist = append(kvlist, 1)
	fmt.Println(kvlist)
	m := make(map[int]int, 10)
	m[1] += 1
	fmt.Println("aaaa:", string(runtime.CPUProfile()), m, cap(kvlist))
	for i := 0; ; i++ {
		pc, file, line, ok := runtime.Caller(i)
		if ok == false {
			break
		}
		fmt.Println(pc, file, line, ok)
		//runtime.Breakpoint()
	}
	fmt.Println(runtime.Version())
	buf := make([]byte, 1025)
	len := runtime.Stack(buf, true)
	fmt.Println(string(runtime.CPUProfile()))
	fmt.Println(len, string(buf[:len]))
	//debug.PrintStack()
	fmt.Println(debug.SetMaxThreads(10))
	i := 100
	fmt.Println(reflect.TypeOf((*int32)(nil)).Elem())
	fmt.Println(reflect.ValueOf(&i).Kind())
	fmt.Println(reflect.TypeOf(&i).Kind())
	RoundPos(3, func(r, x, y int32) {
		println(r, x, y)
	})
}
Example #7
0
File: pprof.go Project: kraj/gcc
func profileWriter(w io.Writer) {
	for {
		data := runtime.CPUProfile()
		if data == nil {
			break
		}
		w.Write(data)
	}

	// We are emitting the legacy profiling format, which permits
	// a memory map following the CPU samples. The memory map is
	// simply a copy of the GNU/Linux /proc/self/maps file. The
	// profiler uses the memory map to map PC values in shared
	// libraries to a shared library in the filesystem, in order
	// to report the correct function and, if the shared library
	// has debug info, file/line. This is particularly useful for
	// PIE (position independent executables) as on ELF systems a
	// PIE is simply an executable shared library.
	//
	// Because the profiling format expects the memory map in
	// GNU/Linux format, we only do this on GNU/Linux for now. To
	// add support for profiling PIE on other ELF-based systems,
	// it may be necessary to map the system-specific mapping
	// information to the GNU/Linux format. For a reasonably
	// portable C++ version, see the FillProcSelfMaps function in
	// https://github.com/gperftools/gperftools/blob/master/src/base/sysinfo.cc
	//
	// The code that parses this mapping for the pprof tool is
	// ParseMemoryMap in cmd/internal/pprof/legacy_profile.go, but
	// don't change that code, as similar code exists in other
	// (non-Go) pprof readers. Change this code so that that code works.
	//
	// We ignore errors reading or copying the memory map; the
	// profile is likely usable without it, and we have no good way
	// to report errors.
	if runtime.GOOS == "linux" {
		f, err := os.Open("/proc/self/maps")
		if err == nil {
			io.WriteString(w, "\nMAPPED_LIBRARIES:\n")
			io.Copy(w, f)
			f.Close()
		}
	}

	cpu.done <- true
}
Example #8
0
File: pprof.go Project: achanda/go
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
}
Example #9
0
func startProfiler(stoppable Stoppable) error {
	if profileFilename == nil || *profileFilename == "" {
		log.Info("Not starting profiling since the profile prefix is not set")
		return nil
	}

	log.Info("Starting profiling with prefix %s", *profileFilename)

	startCHeapProfiler(*profileFilename)
	// startCCpuProfiler(*profileFilename)
	runtime.MemProfileRate = 1024

	cpuProfileFile, err := os.Create(fmt.Sprintf("%s.cpu", *profileFilename))
	if err != nil {
		return err
	}
	runtime.SetCPUProfileRate(500)
	stopped := make(chan bool)

	go waitForSignals(stoppable, *profileFilename, stopped)

	go func() {
		for {
			select {
			default:
				data := runtime.CPUProfile()
				if data == nil {
					cpuProfileFile.Close()
					stopped <- true
					break
				}
				cpuProfileFile.Write(data)
			}
		}
	}()
	return nil
}