-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathruntime.go
82 lines (74 loc) · 2.17 KB
/
runtime.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
"runtime"
)
type WebInfo struct {
Webroot string `json:"webroot"`
Writable bool `json:"writable"`
StopOnIdle bool `json:"stop-on-idle"`
VersionCheck bool `json:"daily-version-check"`
}
type BuildInfo struct {
GitVersion string `json:"gitversion"`
Buildtime string `json:"buildtime"`
GoVersion string `json:"goversion"`
LatestReleaseUrl string `json:"latest-release-url"`
}
type MemoryInfo struct {
MemoryAlloc string `json:"alloc"`
MemoryTotalAlloc string `json:"total-alloc"`
MemorySys string `json:"sys"`
MemoryLookups uint64 `json:"lookups"`
MemoryMallocs uint64 `json:"mallocs"`
MemoryFrees uint64 `json:"frees"`
}
type RuntimeResponse struct {
Home string `json:"home"`
Version string `json:"version"`
Platform string `json:"platform"`
Web WebInfo `json:"web"`
Build BuildInfo `json:"build"`
Memory MemoryInfo `json:"memory"`
}
func GetRuntimeInfo(dir string, webOpts webOptions) RuntimeResponse {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
return RuntimeResponse{
Home: dir,
Version: getVersion(gitversion),
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
Web: WebInfo{
Webroot: webOpts.webroot,
Writable: !webOpts.readOnly,
StopOnIdle: webOpts.heartbeat,
VersionCheck: webOpts.check,
},
Build: BuildInfo{
GitVersion: gitversion,
Buildtime: buildtime,
GoVersion: runtime.Version(),
LatestReleaseUrl: latestReleaseUrl,
},
Memory: MemoryInfo{
MemoryAlloc: bytesToHumanReadable(memStats.Alloc),
MemoryTotalAlloc: bytesToHumanReadable(memStats.TotalAlloc),
MemorySys: bytesToHumanReadable(memStats.Sys),
MemoryLookups: memStats.Lookups,
MemoryMallocs: memStats.Mallocs,
MemoryFrees: memStats.Frees,
},
}
}
func bytesToHumanReadable(bytes uint64) string {
const unit = 1024
if bytes < unit {
return fmt.Sprintf("%d B", bytes)
}
div, exp := int64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
}