1 Star 0 Fork 0

lqinggang/psiphon-tunnel-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
profiles.go 3.46 KB
一键复制 编辑 原始数据 按行查看 历史
Rod Hynes 提交于 2018-10-22 11:22 . Additional Go runtime profiling support
/*
* Copyright (c) 2018, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package common
import (
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"time"
)
// WriteRuntimeProfiles writes Go runtime profile information to a set of
// files in the specified output directory. The profiles include "heap",
// "goroutine", and other selected profiles from:
// https://golang.org/pkg/runtime/pprof/#Profile.
//
// The SampleDurationSeconds inputs determine how long to wait and sample
// profiles that require active sampling. When set to 0, these profiles are
// skipped.
func WriteRuntimeProfiles(
logger Logger,
outputDirectory string,
blockSampleDurationSeconds int,
cpuSampleDurationSeconds int) {
openProfileFile := func(profileName string) *os.File {
fileName := filepath.Join(outputDirectory, profileName+".profile")
file, err := os.OpenFile(
fileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
logger.WithContextFields(
LogFields{
"error": err,
"fileName": fileName}).Error("open profile file failed")
return nil
}
return file
}
writeProfile := func(profileName string) {
file := openProfileFile(profileName)
if file == nil {
return
}
err := pprof.Lookup(profileName).WriteTo(file, 1)
file.Close()
if err != nil {
logger.WithContextFields(
LogFields{
"error": err,
"profileName": profileName}).Error("write profile failed")
}
}
// TODO: capture https://golang.org/pkg/runtime/debug/#WriteHeapDump?
// May not be useful in its current state, as per:
// https://groups.google.com/forum/#!topic/golang-dev/cYAkuU45Qyw
// Write goroutine, heap, and threadcreate profiles
// https://golang.org/pkg/runtime/pprof/#Profile
writeProfile("goroutine")
writeProfile("heap")
writeProfile("threadcreate")
// Write CPU profile (after sampling)
// https://golang.org/pkg/runtime/pprof/#StartCPUProfile
if cpuSampleDurationSeconds > 0 {
file := openProfileFile("cpu")
if file != nil {
logger.WithContext().Info("start cpu profiling")
err := pprof.StartCPUProfile(file)
if err != nil {
logger.WithContextFields(
LogFields{"error": err}).Error("StartCPUProfile failed")
} else {
time.Sleep(time.Duration(cpuSampleDurationSeconds) * time.Second)
pprof.StopCPUProfile()
logger.WithContext().Info("end cpu profiling")
}
file.Close()
}
}
// Write block profile (after sampling)
// https://golang.org/pkg/runtime/pprof/#Profile
if blockSampleDurationSeconds > 0 {
logger.WithContext().Info("start block/mutex profiling")
runtime.SetBlockProfileRate(1)
runtime.SetMutexProfileFraction(1)
time.Sleep(time.Duration(blockSampleDurationSeconds) * time.Second)
runtime.SetBlockProfileRate(0)
runtime.SetMutexProfileFraction(0)
logger.WithContext().Info("end block/mutex profiling")
writeProfile("block")
writeProfile("mutex")
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/lqinggang/psiphon-tunnel-core.git
git@gitee.com:lqinggang/psiphon-tunnel-core.git
lqinggang
psiphon-tunnel-core
psiphon-tunnel-core
v1.0.10

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385