-
-
Notifications
You must be signed in to change notification settings - Fork 962
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f18f585
commit 9d191c1
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters