1 Star 0 Fork 0

csingo/cHelper

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
System.go 2.20 KB
一键复制 编辑 原始数据 按行查看 历史
joe 提交于 2024-12-11 10:58 +08:00 . update
package cHelper
import (
"context"
"fmt"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"time"
)
type MemoryStatus struct {
Pid int
Used int
G string
M string
K string
Err error
}
func PrintMemory(ctx context.Context, interval int64, handle func(ctx context.Context, mem *MemoryStatus)) {
msg := make(chan *MemoryStatus)
ticker := time.NewTicker(time.Duration(interval) * time.Second)
go func(ctx context.Context, handle func(ctx context.Context, mem *MemoryStatus), msg chan *MemoryStatus) {
for { //nolint:gosimple
select {
case mem := <-msg:
handle(ctx, mem)
}
}
}(ctx, handle, msg)
go func(ticker *time.Ticker, msg chan *MemoryStatus) {
for { //nolint:gosimple
select {
case <-ticker.C:
switch runtime.GOOS {
case "linux":
pid := os.Getpid()
processFile := fmt.Sprintf("/proc/%d/status", pid)
detail, err := os.ReadFile(processFile)
var res int
if err != nil {
msg <- &MemoryStatus{
Pid: pid,
Err: err,
}
continue
}
reg := regexp.MustCompile(`VmRSS[^\d]*([0-9]*)`)
mem := reg.FindAllStringSubmatch(string(detail), -1)
res = ToInt(mem[0][1])
msg <- &MemoryStatus{
Pid: pid,
Used: res,
G: fmt.Sprintf("%.2f GB", float64(res)/float64(1024)/float64(1024)),
M: fmt.Sprintf("%d MB", res/1024),
K: fmt.Sprintf("%d KB", res),
Err: err,
}
case "windows":
pid := os.Getpid()
command := fmt.Sprintf("wmic process where processid=%d get WorkingSetSize", pid)
commands := strings.Split(command, " ")
cmd := exec.Command(commands[0], commands[1:]...)
var res int
detail, err := cmd.Output()
if err != nil {
msg <- &MemoryStatus{
Pid: pid,
Err: err,
}
continue
}
reg := regexp.MustCompile(`([0-9]+)`)
mem := reg.FindAllString(string(detail), -1)
res = ToInt(string(mem[0]))
msg <- &MemoryStatus{
Pid: pid,
Used: res,
G: fmt.Sprintf("%.2f GB", float64(res)/float64(1024)/float64(1024)/float64(1024)),
M: fmt.Sprintf("%d MB", res/1024/1024),
K: fmt.Sprintf("%d KB", res/1024),
Err: err,
}
}
}
}
}(ticker, msg)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/csingo/cHelper.git
git@gitee.com:csingo/cHelper.git
csingo
cHelper
cHelper
v0.4.32

搜索帮助