blob: 3c364193714eeb18f26de247d34f033e4fb4548f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package stats
import (
"fmt"
"math"
"metachan/types"
"runtime"
"time"
)
var startTime = time.Now()
func GetMemoryStats() types.MemoryStats {
var m runtime.MemStats
runtime.ReadMemStats(&m)
used := m.Alloc / 1024 // Total allocated memory in MiB
total := m.Sys / 1024 // Total system memory obtained in MiB
free := (m.Sys - m.Alloc) / 1024
usage := math.Round(float64(m.Alloc)/float64(m.Sys)*100*100) / 100 // Memory usage percentage
return types.MemoryStats{
Used: fmt.Sprintf("%d MiB", used),
Total: fmt.Sprintf("%d MiB", total),
Free: fmt.Sprintf("%d MiB", free),
Usage: fmt.Sprintf("%.2f%%", usage),
}
}
func GetUptime() string {
var timeString string
uptime := time.Since(startTime)
// return in format "2d 3h 4m 5s"
days := int(uptime.Hours() / 24)
hours := int(uptime.Hours()) % 24
minutes := int(uptime.Minutes()) % 60
seconds := int(uptime.Seconds()) % 60
if days > 0 {
timeString += fmt.Sprintf("%dd ", days)
}
if hours > 0 {
timeString += fmt.Sprintf("%dh ", hours)
}
if minutes > 0 {
timeString += fmt.Sprintf("%dm ", minutes)
}
if seconds > 0 {
timeString += fmt.Sprintf("%ds ", seconds)
}
if timeString == "" {
timeString = "0s"
} else {
timeString = timeString[:len(timeString)-1] // Remove trailing space
}
return timeString
}
|