Skip to content

Commit b34b239

Browse files
committed
code clean
1 parent 56b86d2 commit b34b239

3 files changed

Lines changed: 29 additions & 15 deletions

File tree

cmd/topic/topic.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ func genSummary(stat *linuxproc.Stat) []string {
2929
tc := pkg.GetTaskCount()
3030
cpuCount := pkg.GetCpuCount(stat)
3131
userCpu, systemCpu, idleCpu := pkg.GetCpuUsage(cpuCount)
32-
totalMem, freeMem, usedMem, cacheMem := pkg.GetTotalMemInMiB()
33-
avail := freeMem + cacheMem
32+
memInfo := pkg.GetTotalMemInMiB()
33+
avail := memInfo.Free + memInfo.Cache
3434
return []string{
3535
fmt.Sprintf("topic - %v up %s, %d users, load average: %s", currentTime, upTime, pkg.GetUsers(), loadMonitor.GetLoad()),
3636
fmt.Sprintf("Tasks: [%3d](mod:bold) total, [%3d](mod:bold) running, [%3d](mod:bold) sleeping, [%3d](mod:bold) stopped, [%3d](mod:bold) zombie",
3737
tc.Total, tc.Running, tc.Sleeping, tc.Stopped, tc.Zombie),
3838
fmt.Sprintf("%%Cpu(%sc): [%2.1f](mod:bold) us, [%2.1f](mod:bold) sy, [0.0](mod:bold) ni, [%2.1f](mod:bold) id, [0.0](mod:bold) wa, [0.0 hi,](mod:bold) [0.0](mod:bold) si, [0.0](mod:bold) st",
3939
pkg.CpuCountToString(cpuCount), userCpu, systemCpu, idleCpu),
4040
fmt.Sprintf("MiB Mem : [%7.1f](mod:bold) total, [%7.1f](mod:bold) free, [%7.1f](mod:bold) used, [%7.1f](mod:bold) buff/cache",
41-
totalMem, freeMem, usedMem, cacheMem),
41+
memInfo.Total, memInfo.Free, memInfo.Used, memInfo.Cache),
4242
fmt.Sprintf("MiB Swap: [0](mod:bold) total, [0](mod:bold) free, [0](mod:bold) used. [%7.1f](mod:bold) avail Mem", avail),
4343
}
4444
}

pkg/memory.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import (
66
v1 "github.com/containerd/cgroups/stats/v1"
77
)
88

9-
func GetMemInfoInByte() (total, free, used, cache float64) {
9+
type MemInfo struct {
10+
Total, Free, Used, Cache float64
11+
}
12+
13+
func GetMemInfoInByte() (memInfo MemInfo) {
1014
stats := v1.Metrics{}
1115
mem := cgroups.NewMemory("/sys/fs/cgroup/", cgroups.IgnoreModules("memsw"))
1216
err := mem.Stat("", &stats)
@@ -20,22 +24,31 @@ func GetMemInfoInByte() (total, free, used, cache float64) {
2024
}
2125

2226
// convert Byte to MiB
27+
var total float64
2328
if stats.Memory.Usage.Limit/1024 > hostMemInfo.MemTotal {
2429
total = float64(hostMemInfo.MemTotal) * 1024
2530
} else {
2631
total = float64(stats.Memory.Usage.Limit)
2732
}
28-
used = float64(stats.Memory.Usage.Usage)
29-
cache = float64(stats.Memory.Cache)
30-
free = total - used
31-
return
33+
used := float64(stats.Memory.Usage.Usage)
34+
cache := float64(stats.Memory.Cache)
35+
free := total - used
36+
return MemInfo{Total: total, Free: free, Used: used, Cache: cache}
3237
}
3338

34-
func GetTotalMemInMiB() (total, free, used, cache float64) {
35-
total, free, used, cache = GetMemInfoInByte()
36-
return total / 1048576, free / 1048576, used / 1048576, cache / 1048576
39+
func GetTotalMemInMiB() (memInfo MemInfo) {
40+
memInfo = GetMemInfoInByte()
41+
memInfo.Total = memInfo.Total / 1048576
42+
memInfo.Free = memInfo.Free / 1048576
43+
memInfo.Used = memInfo.Used / 1048576
44+
memInfo.Cache = memInfo.Cache / 1048576
45+
return memInfo
3746
}
38-
func GetTotalMemInKiB() (total, free, used, cache float64) {
39-
total, free, used, cache = GetMemInfoInByte()
40-
return total / 1024, free / 1024, used / 1024, cache / 1024
47+
func GetTotalMemInKiB() (memInfo MemInfo) {
48+
memInfo = GetMemInfoInByte()
49+
memInfo.Total = memInfo.Total / 1024
50+
memInfo.Free = memInfo.Free / 1024
51+
memInfo.Used = memInfo.Used / 1024
52+
memInfo.Cache = memInfo.Cache / 1024
53+
return memInfo
4154
}

pkg/tasks.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ func (t *TaskMonitor) GetTaskInfos() (infos []string) {
103103
return
104104
}
105105

106-
total, _, _, _ := GetTotalMemInKiB()
106+
memInfo := GetTotalMemInKiB()
107+
total := memInfo.Total
107108

108109
taskInfos := make([]string, 0)
109110
for _, v := range files {

0 commit comments

Comments
 (0)