Testing for linux cpuinfo
This commit is contained in:
1 parent
cbc9e4a0b4
commit
ea5e79a46a
8 files changed
+172
-20
No files matched your search
@@ -0,0 +1,60 @@
|
||||
//go:build linux
|
||||
|
||||
package memory
|
||||
|
||||
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?")
|
||||
}
|
||||
defer memoryFile.Close()
|
||||
|
||||
fileReader := bufio.NewScanner(memoryFile)
|
||||
|
||||
var TotalMemory uint64
|
||||
var AvailMemory uint64
|
||||
|
||||
for fileReader.Scan() {
|
||||
line := fileReader.Text()
|
||||
|
||||
if strings.HasPrefix(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.HasPrefix(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")
|
||||
}
|
||||
}
|
||||
|
||||
if TotalMemory > 0 && AvailMemory > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if fileReader.Err() != nil {
|
||||
return MemoryInformation{}, fmt.Errorf("There was an error while reading /proc/meninfo. Error: %v", fileReader.Err())
|
||||
}
|
||||
|
||||
return MemoryInformation{
|
||||
SystemMemory: TotalMemory * 1024,
|
||||
AvailableMemory: AvailMemory * 1024,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func GetRamInformation() (MemoryInformation, error) {
|
||||
return getLinuxRamInfo()
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package memory
|
||||
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
//go:build windows
|
||||
|
||||
package memory
|
||||
|
||||
import (
|
||||
"log"
|
||||
"unsafe"
|
||||
|
||||
"rpi5-health/internal/windows_system"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
type DWORD uint32
|
||||
type DWORDLONG uint64
|
||||
|
||||
type memoryStatusEx struct {
|
||||
/*
|
||||
* Struct for storing memory status information.
|
||||
*
|
||||
* It gets populated via syscall.
|
||||
*/
|
||||
dwLength DWORD
|
||||
dwMemoryLoad DWORD
|
||||
ullTotalPhys DWORDLONG
|
||||
ullAvailPhys DWORDLONG
|
||||
ullTotalPageFile DWORDLONG
|
||||
ullAvailPageFile DWORDLONG
|
||||
ullTotalVirtual DWORDLONG
|
||||
ullAvailVirtual DWORDLONG
|
||||
ullAvailExtendedVirtual DWORDLONG
|
||||
}
|
||||
|
||||
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 = DWORD(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: uint64(mem.ullTotalPhys),
|
||||
AvailableMemory: uint64(mem.ullAvailPhys),
|
||||
}
|
||||
}
|
||||
|
||||
func GetRamInformation() (MemoryInformation, error) {
|
||||
return getWinRamInfo(windows_system.GetWindowsTools().ProcGlobalMemoryStatusEx), nil
|
||||
}
|
||||
Reference in new issue
Block a user