parent
1e23a94207
commit
c34f568056
@ -1,55 +0,0 @@ |
|||||||
package main |
|
||||||
|
|
||||||
import ( |
|
||||||
"flag" |
|
||||||
"fmt" |
|
||||||
"time" |
|
||||||
|
|
||||||
"github.com/shirou/gopsutil/process" |
|
||||||
"github.com/simple-rules/harmony-benchmark/log" |
|
||||||
) |
|
||||||
|
|
||||||
type profilerSetting struct { |
|
||||||
pid int32 |
|
||||||
shardID string |
|
||||||
} |
|
||||||
|
|
||||||
var ( |
|
||||||
setting profilerSetting |
|
||||||
) |
|
||||||
|
|
||||||
func logPerf() { |
|
||||||
p, _ := process.NewProcess(setting.pid) |
|
||||||
for { |
|
||||||
// log mem usage
|
|
||||||
info, _ := p.MemoryInfo() |
|
||||||
memMap, _ := p.MemoryMaps(false) |
|
||||||
log.Info("Mem Report", "info", info, "map", memMap, "shardID", setting.shardID) |
|
||||||
|
|
||||||
// log cpu usage
|
|
||||||
percent, _ := p.CPUPercent() |
|
||||||
times, _ := p.Times() |
|
||||||
log.Info("CPU Report", "percent", percent, "times", times, "shardID", setting.shardID) |
|
||||||
|
|
||||||
time.Sleep(3 * time.Second) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func main() { |
|
||||||
pid := flag.Int("pid", 0, "process id of the node") |
|
||||||
shardID := flag.String("shard_id", "0", "the shard id of this node") |
|
||||||
logFolder := flag.String("log_folder", "latest", "the folder collecting the logs of this execution") |
|
||||||
flag.Parse() |
|
||||||
|
|
||||||
setting.pid = int32(*pid) |
|
||||||
setting.shardID = *shardID |
|
||||||
logFileName := fmt.Sprintf("./%v/profiler-%v.log", *logFolder, *shardID) |
|
||||||
h := log.MultiHandler( |
|
||||||
log.StdoutHandler, |
|
||||||
log.Must.FileHandler(logFileName, log.JSONFormat()), // Log to file
|
|
||||||
// log.Must.NetHandler("tcp", ":3000", log.JSONFormat()) // Log to remote
|
|
||||||
) |
|
||||||
log.Root().SetHandler(h) |
|
||||||
|
|
||||||
logPerf() |
|
||||||
} |
|
@ -0,0 +1,48 @@ |
|||||||
|
package profiler |
||||||
|
|
||||||
|
import ( |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/shirou/gopsutil/process" |
||||||
|
"github.com/simple-rules/harmony-benchmark/log" |
||||||
|
) |
||||||
|
|
||||||
|
type Profiler struct { |
||||||
|
logger log.Logger |
||||||
|
PID int32 |
||||||
|
ShardID string |
||||||
|
proc *process.Process |
||||||
|
} |
||||||
|
|
||||||
|
func NewProfiler(logger log.Logger, pid int, shardID string) *Profiler { |
||||||
|
profiler := Profiler{logger, int32(pid), shardID, nil} |
||||||
|
return &profiler |
||||||
|
} |
||||||
|
|
||||||
|
func (profiler *Profiler) LogMemory() { |
||||||
|
for { |
||||||
|
// log mem usage
|
||||||
|
info, _ := profiler.proc.MemoryInfo() |
||||||
|
memMap, _ := profiler.proc.MemoryMaps(false) |
||||||
|
profiler.logger.Info("Mem Report", "info", info, "map", memMap, "shardID", profiler.ShardID) |
||||||
|
|
||||||
|
time.Sleep(3 * time.Second) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func (profiler *Profiler) LogCPU() { |
||||||
|
for { |
||||||
|
// log cpu usage
|
||||||
|
percent, _ := profiler.proc.CPUPercent() |
||||||
|
times, _ := profiler.proc.Times() |
||||||
|
profiler.logger.Info("CPU Report", "percent", percent, "times", times, "shardID", profiler.ShardID) |
||||||
|
|
||||||
|
time.Sleep(3 * time.Second) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func (profiler *Profiler) Start() { |
||||||
|
profiler.proc, _ = process.NewProcess(profiler.PID) |
||||||
|
go profiler.LogCPU() |
||||||
|
go profiler.LogMemory() |
||||||
|
} |
Loading…
Reference in new issue