当前位置: 首页>>代码示例>>Golang>>正文


Golang pprof.WriteHeapProfile函数代码示例

本文整理汇总了Golang中runtime/pprof.WriteHeapProfile函数的典型用法代码示例。如果您正苦于以下问题:Golang WriteHeapProfile函数的具体用法?Golang WriteHeapProfile怎么用?Golang WriteHeapProfile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了WriteHeapProfile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: startProfile

func startProfile() {
	if cpuprofile != "" {
		f, err := os.Create(cpuprofile)
		if err != nil {
			log.Fatalf("%v", err)
		}
		if err := pprof.StartCPUProfile(f); err != nil {
			log.Fatalf("%v", err)
		}
		AtExit(pprof.StopCPUProfile)
	}
	if memprofile != "" {
		if memprofilerate != 0 {
			runtime.MemProfileRate = int(memprofilerate)
		}
		f, err := os.Create(memprofile)
		if err != nil {
			log.Fatalf("%v", err)
		}
		AtExit(func() {
			runtime.GC() // profile all outstanding allocations
			if err := pprof.WriteHeapProfile(f); err != nil {
				log.Fatalf("%v", err)
			}
		})
	}
}
开发者ID:sreis,项目名称:go,代码行数:27,代码来源:util.go

示例2: runBenchmarkOnce

// runBenchmarkOnce runs f once and collects all performance metrics and profiles.
func runBenchmarkOnce(f func(uint64), N uint64) Result {
	latencyInit(N)
	runtime.GC()
	mstats0 := new(runtime.MemStats)
	runtime.ReadMemStats(mstats0)
	ss := InitSysStats(N)
	res := MakeResult()
	res.N = N
	res.Files["memprof0"] = tempFilename("memprof")
	memprof0, err := os.Create(res.Files["memprof0"])
	if err != nil {
		log.Fatalf("Failed to create profile file '%v': %v", res.Files["memprof0"], err)
	}
	pprof.WriteHeapProfile(memprof0)
	memprof0.Close()

	res.Files["cpuprof"] = tempFilename("cpuprof")
	cpuprof, err := os.Create(res.Files["cpuprof"])
	if err != nil {
		log.Fatalf("Failed to create profile file '%v': %v", res.Files["cpuprof"], err)
	}
	defer cpuprof.Close()
	pprof.StartCPUProfile(cpuprof)
	t0 := time.Now()
	f(N)
	res.Duration = time.Since(t0)
	res.RunTime = uint64(time.Since(t0)) / N
	res.Metrics["time"] = res.RunTime
	pprof.StopCPUProfile()

	latencyCollect(&res)
	ss.Collect(&res)

	res.Files["memprof"] = tempFilename("memprof")
	memprof, err := os.Create(res.Files["memprof"])
	if err != nil {
		log.Fatalf("Failed to create profile file '%v': %v", res.Files["memprof"], err)
	}
	pprof.WriteHeapProfile(memprof)
	memprof.Close()

	mstats1 := new(runtime.MemStats)
	runtime.ReadMemStats(mstats1)
	res.Metrics["allocated"] = (mstats1.TotalAlloc - mstats0.TotalAlloc) / N
	res.Metrics["allocs"] = (mstats1.Mallocs - mstats0.Mallocs) / N
	res.Metrics["sys-total"] = mstats1.Sys
	res.Metrics["sys-heap"] = mstats1.HeapSys
	res.Metrics["sys-stack"] = mstats1.StackSys
	res.Metrics["gc-pause-total"] = (mstats1.PauseTotalNs - mstats0.PauseTotalNs) / N
	collectGo12MemStats(&res, mstats0, mstats1)
	numGC := uint64(mstats1.NumGC - mstats0.NumGC)
	if numGC == 0 {
		res.Metrics["gc-pause-one"] = 0
	} else {
		res.Metrics["gc-pause-one"] = (mstats1.PauseTotalNs - mstats0.PauseTotalNs) / numGC
	}
	return res
}
开发者ID:dvyukov,项目名称:goperfd,代码行数:59,代码来源:driver.go

示例3: main

func main() {
	inputPath := flag.String("input", "-", "Input filename, '-' for stdin")
	cpuprofile := flag.String("cpuprofile", "", "Write CPU profile to file")
	memprofile := flag.String("memprofile", "", "Write memory profile to file")
	flag.Parse()

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Println(err.Error())
			os.Exit(1)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
		defer f.Close()
	}
	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			log.Println(err.Error())
			os.Exit(1)
		}
		go func() {
			for {
				time.Sleep(5 * time.Second)
				pprof.WriteHeapProfile(f)
			}
		}()
		defer pprof.WriteHeapProfile(f)
		defer f.Close()
	}

	sigIntChan := make(chan os.Signal, 1)
	signal.Notify(sigIntChan, syscall.SIGINT)
	go func() {
		<-sigIntChan
		os.Exit(1)
	}()

	// Initialize input reading
	log.Println("Reading from", *inputPath)
	var inputFile *os.File = os.Stdin
	if *inputPath != "-" {
		var err error
		inputFile, err = os.Open(*inputPath)
		if err != nil {
			log.Println(err.Error())
			os.Exit(1)
		}
		defer inputFile.Close()
	}
	input := bufio.NewReader(inputFile)
	// TODO
	output := os.Stdout

	process(Config, input, output)
}
开发者ID:ostrovok-team,项目名称:pgdump-obfuscator,代码行数:57,代码来源:main.go

示例4: main

func main() {
	go func() {
		if *memprofile != "" {
			written := false
			for {
				if written {
					os.Remove(*memprofile)
				}

				f, err := os.Create(*memprofile)
				if err != nil {
					log.Fatal(err)
				}
				pprof.WriteHeapProfile(f)
				time.Sleep(1 * time.Millisecond)
				f.Close()
			}
		}
	}()

	args, err := GetLegacyArguments()

	if err != nil {
		fmt.Println(err.Error())
		return
	}

	if args.Help {
		flag.Usage()
		return
	}

	legacy, err := args.GetLegacy()
	if err != nil {
		log.Println(err)
		return
	}

	legacy.SetupLogging()
	legacy.Run()

	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.WriteHeapProfile(f)
		f.Close()
		return
	}

	legacy.ShutdownLogging()
}
开发者ID:iamthemovie,项目名称:legacy,代码行数:53,代码来源:legacy.go

示例5: initConfig

// initConfig is run by cobra after initialising the flags
func initConfig() {
	// Log file output
	if *logFile != "" {
		f, err := os.OpenFile(*logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
		if err != nil {
			log.Fatalf("Failed to open log file: %v", err)
		}
		_, err = f.Seek(0, os.SEEK_END)
		if err != nil {
			fs.ErrorLog(nil, "Failed to seek log file to end: %v", err)
		}
		log.SetOutput(f)
		fs.DebugLogger.SetOutput(f)
		redirectStderr(f)
	}

	// Load the rest of the config now we have started the logger
	fs.LoadConfig()

	// Write the args for debug purposes
	fs.Debug("rclone", "Version %q starting with parameters %q", fs.Version, os.Args)

	// Setup CPU profiling if desired
	if *cpuProfile != "" {
		fs.Log(nil, "Creating CPU profile %q\n", *cpuProfile)
		f, err := os.Create(*cpuProfile)
		if err != nil {
			fs.Stats.Error()
			log.Fatal(err)
		}
		err = pprof.StartCPUProfile(f)
		if err != nil {
			fs.Stats.Error()
			log.Fatal(err)
		}
		defer pprof.StopCPUProfile()
	}

	// Setup memory profiling if desired
	if *memProfile != "" {
		defer func() {
			fs.Log(nil, "Saving Memory profile %q\n", *memProfile)
			f, err := os.Create(*memProfile)
			if err != nil {
				fs.Stats.Error()
				log.Fatal(err)
			}
			err = pprof.WriteHeapProfile(f)
			if err != nil {
				fs.Stats.Error()
				log.Fatal(err)
			}
			err = f.Close()
			if err != nil {
				fs.Stats.Error()
				log.Fatal(err)
			}
		}()
	}
}
开发者ID:marcopaganini,项目名称:rclone,代码行数:61,代码来源:cmd.go

示例6: Profile

// Profile starts CPU and memory profiling and returns a function that will stop that.
//
//
// Writes "cpu" + filename and "mem" + filename.
//
// Usage:
//
//   defer Profile("logfast.prof")()
//
func Profile(filename string) func() {
	cpu, err := os.Create("cpu" + filename)
	if err != nil {
		panic(err)
	}

	mem, err := os.Create("mem" + filename)
	if err != nil {
		panic(err)
	}

	then := time.Now()
	log.Printf("Profile starting %s\n", filename)
	pprof.StartCPUProfile(cpu)
	pprof.WriteHeapProfile(mem)

	return func() {
		elapsed := time.Now().Sub(then)
		log.Printf("Profile stopping %s (elapsed %v)\n", filename, elapsed)
		pprof.StopCPUProfile()
		if err := mem.Close(); err != nil {
			log.Printf("error closing mem profile (%v)", err)
		}
	}
}
开发者ID:Comcast,项目名称:rulio,代码行数:34,代码来源:util.go

示例7: saveHeapProfiles

func saveHeapProfiles() {
	runtime.MemProfileRate = 1
	var memstats, prevMemstats runtime.MemStats

	t0 := time.Now()
	for t := range time.NewTicker(250 * time.Millisecond).C {
		startms := int(t.Sub(t0).Seconds() * 1000)
		runtime.ReadMemStats(&memstats)
		if memstats.HeapInuse > prevMemstats.HeapInuse {
			fd, err := os.Create(fmt.Sprintf("heap-%05d-%07d.pprof", syscall.Getpid(), startms))
			if err != nil {
				panic(err)
			}
			err = pprof.WriteHeapProfile(fd)
			if err != nil {
				panic(err)
			}
			err = fd.Close()
			if err != nil {
				panic(err)
			}
			prevMemstats = memstats
		}
	}
}
开发者ID:baa-archieve,项目名称:syncthing,代码行数:25,代码来源:heapprof.go

示例8: main

func main() {
	flag.Parse()
	log.SetFlags(log.Lshortfile | log.Ltime)
	fmt.Printf(
		`lldb:      version exp
Keys:       16 bytes each
Values:     100 bytes each (50 bytes after compression)
Entries:    1000000
RawSize:    110.6 MB (estimated)
FileSize:   62.9 MB (estimated)
------------------------------------------------
`)
	if *memprofile != "" {
		runtime.MemProfileRate = 1
	}
	fillseq()
	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.WriteHeapProfile(f)
		f.Close()
		return
	}

}
开发者ID:rfistman,项目名称:camlistore,代码行数:27,代码来源:main.go

示例9: main

func main() {
	flag.Parse()
	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			fmt.Println("Error: ", err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}
	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			fmt.Println("Error: ", err)
		}
		pprof.WriteHeapProfile(f)
	}
	var wg sync.WaitGroup
	cidrs, err := parseCidrs("./hosts.json")
	if err != nil {
		log.Fatal(err)
	}
	servers := make([]Server, 0, 10)
	for k := range cidrs.Cidrs {
		servers = append(servers, Server{Cidr: cidrs.Cidrs[k]})
	}
	fmt.Println(len(servers))
	for k := range servers {
		wg.Add(1)
		go servers[k].checkIP(&wg)
	}
	wg.Wait()
}
开发者ID:ASPecherkin,项目名称:Experiments,代码行数:33,代码来源:massAccess.go

示例10: main

func main() {
	f, err := os.Create("cpuprofile.prof")
	if err != nil {
		log.Fatal(err)
	}
	pprof.StartCPUProfile(f)
	defer pprof.StopCPUProfile()

	fmt.Printf("GoHM Software Version [%s]\n", TLibCommon.NV_VERSION)
	if len(os.Args) <= 2 {
		fmt.Printf("Usage: \n")
		fmt.Printf("	HM Encoder: gohm.exe -c encoder.cfg [trace.txt]\n")
		fmt.Printf("	HM Decoder: gohm.exe -d test.bin test.yuv [n trace.txt]\n")
	} else {
		if os.Args[1] == "-c" {
			Encoder()
		} else if os.Args[1] == "-d" {
			Decoder()
		} else {
			fmt.Printf("Unknown argment %s\n", os.Args[1])
		}
	}

	m, err := os.Create("memprofile.prof")
	if err != nil {
		log.Fatal(err)
	}
	pprof.WriteHeapProfile(m)
	m.Close()
}
开发者ID:hanjinze,项目名称:GoHM,代码行数:30,代码来源:GoHMCodec.go

示例11: main

func main() {
	argParse()
	runtime.GOMAXPROCS(runtime.NumCPU())

	// start cpu profile.
	fname := "client.pprof"
	fd, err := os.Create(fname)
	if err != nil {
		log.Fatalf("unable to create %q: %v\n", fname, err)
	}
	defer fd.Close()
	pprof.StartCPUProfile(fd)
	defer pprof.StopCPUProfile()

	doTransport()

	// take memory profile.
	fname = "client.mprof"
	fd, err = os.Create(fname)
	if err != nil {
		log.Fatal(err)
	}
	defer fd.Close()
	pprof.WriteHeapProfile(fd)
}
开发者ID:houcy,项目名称:gofast,代码行数:25,代码来源:main.go

示例12: cleanup

func cleanup() {
	Debug("cleanup")

	// write memory profile
	if *flag_memprof != "" {
		f, err := os.Create(*flag_memprof)
		if err != nil {
			Log(err)
		}
		Log("Writing memory profile to", *flag_memprof)
		pprof.WriteHeapProfile(f)
		f.Close()
	}

	// write cpu profile
	if *flag_cpuprof != "" {
		Log("Flushing CPU profile", *flag_cpuprof)
		pprof.StopCPUProfile()
	}

	// print timers
	if *flag_debug {
		PrintTimers()
	}

	cuda.DeviceReset()
	// kill subprocess?
}
开发者ID:LStoleriu,项目名称:hotspin,代码行数:28,代码来源:main.go

示例13: Start

// Start starts listeners
func (s *Service) Start() error {
	s.Dispatcher.Run()

	f, err := os.Create("cpu.prof")
	if err != nil {
		log.Fatal("could not create CPU profile: ", err)
	}
	if err := pprof.StartCPUProfile(f); err != nil {
		log.Fatal("could not start CPU profile: ", err)
	}
	defer pprof.StopCPUProfile()

	f2, err := os.Create("mem.prof")
	if err != nil {
		log.Fatal("could not create memory profile: ", err)
	}
	runtime.GC() // get up-to-date statistics
	if err := pprof.WriteHeapProfile(f2); err != nil {
		log.Fatal("could not write memory profile: ", err)
	}
	f2.Close()

	server := &http.Server{
		Addr:           fmt.Sprintf("%s:%d", s.config.Interface, s.config.Port),
		Handler:        &myHandler{},
		ReadTimeout:    2 * time.Second,
		WriteTimeout:   2 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	mux = make(map[string]func(http.ResponseWriter, *http.Request))
	mux["/v1/events"] = s.eventConsumerHandler

	return server.ListenAndServe()
}
开发者ID:efimovalex,项目名称:EventKitAPI,代码行数:35,代码来源:app.go

示例14: doprofile

func doprofile(fn string) {
	var err error
	var fc, fh, ft *os.File
	for i := 1; i > 0; i++ {
		fc, err = os.Create(fn + "-cpu-" + strconv.Itoa(i) + ".prof")
		if err != nil {
			log.Fatal(err)
		}

		pprof.StartCPUProfile(fc)
		time.Sleep(300 * time.Second)
		pprof.StopCPUProfile()
		fc.Close()

		fh, err = os.Create(fn + "-heap-" + strconv.Itoa(i) + ".prof")
		if err != nil {
			log.Fatal(err)
		}
		pprof.WriteHeapProfile(fh)
		fh.Close()

		ft, err = os.Create(fn + "-threadcreate-" + strconv.Itoa(i) + ".prof")
		if err != nil {
			log.Fatal(err)
		}
		pprof.Lookup("threadcreate").WriteTo(ft, 0)
		ft.Close()
		log.Println("Created CPU, heap and threadcreate profile of 300 seconds")
	}
}
开发者ID:vys,项目名称:go-netperf,代码行数:30,代码来源:main.go

示例15: after

// after runs after all testing.
func after() {
	if *cpuProfile != "" {
		pprof.StopCPUProfile() // flushes profile to disk
	}
	if *memProfile != "" {
		f, err := os.Create(*memProfile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "testing: %s", err)
			return
		}
		if err = pprof.WriteHeapProfile(f); err != nil {
			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s", *memProfile, err)
		}
		f.Close()
	}
	if *blockProfile != "" && *blockProfileRate >= 0 {
		f, err := os.Create(*blockProfile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "testing: %s", err)
			return
		}
		if err = pprof.Lookup("block").WriteTo(f, 0); err != nil {
			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s", *blockProfile, err)
		}
		f.Close()
	}
}
开发者ID:redcatmiss,项目名称:gcc,代码行数:28,代码来源:testing.go


注:本文中的runtime/pprof.WriteHeapProfile函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。