Files
new-api/common/pprof.go
nosqli fb44c8bf03 添加完整项目文件
包含Go API项目的所有源代码、配置文件、Docker配置、文档和前端资源

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-21 23:33:10 +08:00

45 lines
1011 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package common
import (
"fmt"
"github.com/shirou/gopsutil/cpu"
"os"
"runtime/pprof"
"time"
)
// Monitor 定时监控cpu使用率超过阈值输出pprof文件
func Monitor() {
for {
percent, err := cpu.Percent(time.Second, false)
if err != nil {
panic(err)
}
if percent[0] > 80 {
fmt.Println("cpu usage too high")
// write pprof file
if _, err := os.Stat("./pprof"); os.IsNotExist(err) {
err := os.Mkdir("./pprof", os.ModePerm)
if err != nil {
SysLog("创建pprof文件夹失败 " + err.Error())
continue
}
}
f, err := os.Create("./pprof/" + fmt.Sprintf("cpu-%s.pprof", time.Now().Format("20060102150405")))
if err != nil {
SysLog("创建pprof文件失败 " + err.Error())
continue
}
err = pprof.StartCPUProfile(f)
if err != nil {
SysLog("启动pprof失败 " + err.Error())
continue
}
time.Sleep(10 * time.Second) // profile for 30 seconds
pprof.StopCPUProfile()
f.Close()
}
time.Sleep(30 * time.Second)
}
}