skeleton for testing purposes

This commit is contained in:
lin committed 2026-10-05 14:17:03 +02:00
1 parent 8076b30e3f
commit 71e4f1294e
11 files changed
+273

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
package system_check
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
var numberRegex *regexp.Regexp = regexp.MustCompile(`\d+`)
func getLinuxRamInfo() (MemoryInformation, error) {
memoryFile, err := os.Open("/proc/meminfo")
if err != nil {
return MemoryInformation{}, fmt.Errorf("Could not find /proc/meminfo. Are you sure you are on linux?")
}
fileReader := bufio.NewScanner(memoryFile)
var TotalMemory uint64
var AvailMemory uint64
for fileReader.Scan() {
if fileReader.Err() != nil {
return MemoryInformation{}, fmt.Errorf("There was an error while reading /proc/meninfo. Error: %v", fileReader.Err())
}
line := fileReader.Text()
if strings.Contains(line, "MemTotal") {
TotalMemory, err = strconv.ParseUint(numberRegex.FindString(line), 10, 64)
if err != nil {
return MemoryInformation{}, fmt.Errorf("Could not parse total memory from /proc/meminfo")
}
} else if strings.Contains(line, "MemAvailable") {
AvailMemory, err = strconv.ParseUint(numberRegex.FindString(line), 10, 64)
if err != nil {
return MemoryInformation{}, fmt.Errorf("Could not parse available memory from /proc/meminfo")
}
}
}
return MemoryInformation{
SystemMemory: TotalMemory,
AvailableMemory: AvailMemory,
}, nil
}
+32
View File
@@ -0,0 +1,32 @@
package system_check
import (
"fmt"
"runtime"
)
type MemoryInformation struct {
/*
* The total amount of memory the system has available
*/
SystemMemory uint64
/*
* How much memory is currently unused
*/
AvailableMemory uint64
}
func (memInfo MemoryInformation) GetUsedMemory() uint64 {
return memInfo.SystemMemory - memInfo.AvailableMemory
}
func GetRamInformation() (MemoryInformation, error) {
switch runtime.GOOS {
case "windows":
return getWinRamInfo(getWindowsTools().ProcGlobalMemoryStatusEx), nil
case "linux":
return getLinuxRamInfo()
default:
return MemoryInformation{}, fmt.Errorf("Unsupported OS: \"%s\"", runtime.GOOS)
}
}
+28
View File
@@ -0,0 +1,28 @@
package system_check
import (
"sync"
"golang.org/x/sys/windows"
)
type WindowsSystemCheckTools struct {
/*
* Win32 API
*/
Kernel32 *windows.LazyDLL
/*
* Windows memory information
*/
ProcGlobalMemoryStatusEx *windows.LazyProc
}
func getWindowsTools() WindowsSystemCheckTools {
kernel32 := sync.OnceValue(func() *windows.LazyDLL { return windows.NewLazySystemDLL("kernel32.dll") })
procGlobalMemoryStatusEx := sync.OnceValue(func() *windows.LazyProc { return kernel32().NewProc("GlobalMemoryStatusEx") })
return WindowsSystemCheckTools{
Kernel32: kernel32(),
ProcGlobalMemoryStatusEx: procGlobalMemoryStatusEx(),
}
}
+44
View File
@@ -0,0 +1,44 @@
package system_check
import (
"log"
"unsafe"
"golang.org/x/sys/windows"
)
type memoryStatusEx struct {
/*
* Struct for storing memory status information.
*
* It gets populated via syscall.
*/
dwLength uint32
dwMemoryLoad uint32
ullTotalPhys uint64
ullAvailPhys uint64
ullTotalPageFile uint64
ullAvailPageFile uint64
ullTotalVirtual uint64
ullAvailVirtual uint64
ullAvailExtendedVirtual uint64
}
func getWinRamInfo(procGlobalMemoryStatusEx *windows.LazyProc) MemoryInformation {
var mem memoryStatusEx
// * Windows requires the dwLength field to be set to the struct's size
// * before execution so it knows which OS struct version is being used.
mem.dwLength = uint32(unsafe.Sizeof(mem))
ret, _, err := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&mem))) // exec syscall
if ret == 0 { // winapi syscalls return 0 on failure
log.Fatalf("GlobalMemoryStatusEx failed: %v", err)
}
return MemoryInformation{
SystemMemory: mem.ullTotalPhys,
AvailableMemory: mem.ullAvailPhys,
}
}