forked from lox/apt-proxy
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
382 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package system | ||
|
||
import ( | ||
"math" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func DiskAvailable() (int64, error) { | ||
var stat unix.Statfs_t | ||
wd, err := os.Getwd() | ||
|
||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
unix.Statfs(wd, &stat) | ||
return int64(stat.Bavail * uint64(stat.Bsize)), nil | ||
} | ||
|
||
// https://stackoverflow.com/questions/32482673/how-to-get-directory-total-size | ||
func DirSize(path string) (int64, error) { | ||
var size int64 | ||
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if !info.IsDir() { | ||
size += info.Size() | ||
} | ||
return err | ||
}) | ||
return size, err | ||
} | ||
|
||
// https://hakk.dev/docs/golang-convert-file-size-human-readable/ | ||
func fileSizeRound(val float64, roundOn float64, places int) float64 { | ||
var round float64 | ||
pow := math.Pow(10, float64(places)) | ||
digit := pow * val | ||
_, div := math.Modf(digit) | ||
if div >= roundOn { | ||
round = math.Ceil(digit) | ||
} else { | ||
round = math.Floor(digit) | ||
} | ||
return round / pow | ||
} | ||
|
||
func HumanFileSize(input int64) string { | ||
size := float64(input) | ||
|
||
var units = []string{ | ||
0: "B", | ||
1: "KB", | ||
2: "MB", | ||
3: "GB", | ||
4: "TB", | ||
} | ||
|
||
level := math.Log(size) / math.Log(1024) | ||
fileSize := fileSizeRound(math.Pow(1024, level-math.Floor(level)), .5, 2) | ||
unit := units[int(math.Floor(level))] | ||
|
||
return strconv.FormatFloat(fileSize, 'f', -1, 64) + " " + string(unit) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package system | ||
|
||
// https://github.com/soulteary/hosts-blackhole/blob/main/pkg/system/gc.go | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"runtime/debug" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func GetMemoryUsageAndGoroutine() (int64, string) { | ||
var m runtime.MemStats | ||
runtime.ReadMemStats(&m) | ||
return int64(m.Alloc), strconv.Itoa(runtime.NumGoroutine()) | ||
} | ||
|
||
func Stats(logPrint bool) string { | ||
var m runtime.MemStats | ||
runtime.ReadMemStats(&m) | ||
|
||
stats := []string{ | ||
"Runtime Information:", | ||
fmt.Sprintf(" MEM Alloc = %10v MB", toMB(m.Alloc)), | ||
fmt.Sprintf(" MEM HeapAlloc = %10v MB", toMB(m.HeapAlloc)), | ||
fmt.Sprintf(" MEM Sys = %10v MB", toMB(m.Sys)), | ||
fmt.Sprintf(" MEM NumGC = %10v", m.NumGC), | ||
fmt.Sprintf(" RUN NumCPU = %10d", runtime.NumCPU()), | ||
fmt.Sprintf(" RUN NumGoroutine = %10d", runtime.NumGoroutine()), | ||
} | ||
|
||
if logPrint { | ||
for _, info := range stats { | ||
fmt.Println(info) | ||
} | ||
} | ||
return strings.Join(stats, "\n") | ||
} | ||
|
||
func ManualGC() { | ||
runtime.GC() | ||
debug.FreeOSMemory() | ||
Stats(true) | ||
} | ||
|
||
func toMB(b uint64) uint64 { | ||
const bytesInKB = 1024 | ||
return b / bytesInKB / bytesInKB | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.