hwinfoshmem

package
v0.0.0-...-bc04fb4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2023 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package hwinfoshmem reads sensor data from HWiNFO, a Windows Monitoring tool, using shared memory.

Example

Example of the structs and their functions provided by the library.

package main

import (
	_ "embed"
	"fmt"
	"github.com/MatthiasKunnen/hwinfo-go/pkg/hwinfoshmem"
	"github.com/MatthiasKunnen/hwinfo-go/pkg/util/text"
	"os"
)

//go:embed testdata/limited_live.bin
var data []byte

// Example of the structs and their functions provided by the library.
func main() {
	// For demo purposes, uses copy of shared memory. See MemoryReader for reading the actual shared memory.
	var memoryReader = hwinfoshmem.NewBytesReader(data)

	hwInfo, err := memoryReader.GetHeader()
	if err != nil {
		fmt.Printf("Failed to get header: %s\n", err)
		return
	}

	if !hwInfo.IsActive() {
		fmt.Println("HWiNFO is not active")
		return
	}

	readings, err := memoryReader.GetReadings(hwInfo)
	if err != nil {
		fmt.Printf("Error getting readings %v\n", err)
		return
	}

	printer := text.NewTablePrinter(os.Stdout, make([]text.Column, 3), "    ")

	printer.Append([]string{"Label", "Value", "Unit"})

	for _, reading := range readings {
		printer.Append([]string{
			reading.UserLabel.String(),
			fmt.Sprintf("%f", reading.Value.ToFloat64()),
			reading.Unit.String(),
		})
	}

	fmt.Println("Limited sensors:")
	err = printer.Write()
	if err != nil {
		fmt.Printf("Failed to print table: %s\n", err)
	}

}
Output:

Limited sensors:
Label                              Value        Unit
CPU (Tctl/Tdie)                    47.250000    °C
CPU Die (average)                  45.087887    °C
CPU CCD1 (Tdie)                    45.125000    °C
CPU CCD2 (Tdie)                    33.375000    °C
Water (EC_TEMP1)                   27.000000    °C
GPU Memory Junction Temperature    48.000000    °C
GPU Hot Spot Temperature           35.000000    °C

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BytesReader

type BytesReader struct {
	Bytes []byte
	*Reader
}

BytesReader allows for extracting the header, sensors, and readings from a copy of HWiNFO's shared memory. A copy can be made using [MemoryReader.Copy].

BytesReader has an initializer function, NewBytesReader.

func NewBytesReader

func NewBytesReader(bytes []byte) *BytesReader

type HwinfoFloat64

type HwinfoFloat64 [8]byte

HwinfoFloat64 represents Go's float64 and C#'s double. The type exists to deal with differences in memory alignment between Go and HWiNFO's shared memory.

func (HwinfoFloat64) ToFloat64

func (b HwinfoFloat64) ToFloat64() float64

type HwinfoHeader

type HwinfoHeader struct {
	// Reports whether HWiNFO is active.
	// "HWiS" when HWiNFO it is Active, "DAED" (sic.) when it is not.
	// Get string using GetStatus or check if it is live using isActive.
	Status [4]byte

	// Structure layout version. 1=Initial; 2=Added UTF-8 strings (HWiNFO v7.33+)
	Version uint32

	// Options:
	//  - 0: Initial layout (HWiNFO ver <= 6.11)
	//  - 1: Added (HWiNFO v6.11-3917)
	Revision uint32

	// The unix time (seconds since 1970-01-01) when the last update to the data occurred.
	// Get int using GetLastUpdate.
	LastUpdate [8]byte

	// Offset of the Sensor section from beginning of HwinfoHeader.
	SensorSectionOffset uint32

	// The size in bytes of every sensor's data. sizeof(HwinfoSensor).
	SensorSize uint32

	// Amount of sensors that are available.
	SensorAmount uint32

	// Offset of the Reading section from beginning of HwinfoHeader.
	ReadingSectionOffset uint32

	// Size of each reading's data in bytes. sizeof(HwinfoReading)
	ReadingSize uint32

	// Number of readings.
	ReadingAmount uint32

	// Time in milliseconds between updates of the data by HWiNFO.
	PollingPeriodInMs uint32
}

HwinfoHeader contains information regarding the rest of the available shared memory.

func (HwinfoHeader) GetLastUpdate

func (info HwinfoHeader) GetLastUpdate() int64

GetLastUpdate returns the time since HWiNFO last updated the shared memory in seconds since Unix Epoch.

func (HwinfoHeader) GetLastUpdateTime

func (info HwinfoHeader) GetLastUpdateTime() time.Time

GetLastUpdateTime returns a Time object representing the time since HWiNFO updated the shared memory.

func (HwinfoHeader) GetStatus

func (info HwinfoHeader) GetStatus() string

GetStatus returns the status of the shared memory. "HWiS" when HWiNFO it is Active, "DAED" (sic.) when it is not. When HWiNFO shared memory is not active, this usually means that the shared memory time limit has expired.

func (HwinfoHeader) IsActive

func (info HwinfoHeader) IsActive() bool

IsActive returns true when HWiNFO is currently updating the shared memory. When HWiNFO shared memory is not active, this usually means that the shared memory time limit has expired.

type HwinfoReading

type HwinfoReading struct {
	// The type of reading.
	Type ReadingType

	// Index of the Sensor this reading belongs to.
	SensorIndex uint32

	// A unique ID of the reading within a particular sensor.
	Id uint32

	// Original Label in English language.
	OriginalLabelAscii HwinfoSensorStringAscii

	// Displayed label which might have been renamed by the user. Use UserLabel instead.
	UserLabelAscii HwinfoSensorStringAscii

	// The unit of the reading. E.g. °C, RPM. Use Unit instead.
	UnitAscii HwinfoUnitStringAscii

	// The value of the reading.
	Value HwinfoFloat64

	// The minimum value of the reading.
	ValueMin HwinfoFloat64

	// The maximum value of the reading.
	ValueMax HwinfoFloat64

	// The average value of the reading.
	ValueAvg HwinfoFloat64

	// Displayed label which might have been renamed by the user.
	UserLabel HwinfoSensorStringUtf8

	// The unit of the reading. E.g. °C, RPM.
	Unit HwinfoUnitStringUtf8
}

type HwinfoSensor

type HwinfoSensor struct {
	// A unique Sensor ID
	SensorId uint32

	// The instance of the sensor (together with SensorId forms a unique ID)
	SensorInstance uint32

	// Original name of sensor in English.
	SensorNameOriginalAscii HwinfoSensorStringAscii

	// Display name of sensor. Might be translated or renamed by user.
	SensorNameAscii HwinfoSensorStringAscii

	// Display name of the sensor. Might be renamed by the user.
	// E.g.
	//   - GIGABYTE B650E AORUS MASTER (ITE IT8689E)
	//   - CPU [#0]: AMD Ryzen 9 7950X
	SensorName HwinfoSensorStringUtf8
}

HwinfoSensor can be seen as a way to group readings.

type HwinfoSensorStringAscii

type HwinfoSensorStringAscii [hwinfoSensorStringLength]byte

HwinfoSensorStringAscii is a fixed length byte array of 8-bit ASCII encoded characters. The specific extended ASCII codepage used depends on the system's locale.

Get the codepage used by your system using this powershell command:

[System.Text.Encoding]::Default

The string it contains is padded by nul bytes.

type HwinfoSensorStringUtf8

type HwinfoSensorStringUtf8 [hwinfoSensorStringLength]byte

HwinfoSensorStringUtf8 is a fixed length byte array of UTF-8 encoded characters. The string it contains is padded by nul bytes. To convert it to a string, use HwinfoSensorStringUtf8.String. It is used in labels for sensor and reading.

func (HwinfoSensorStringUtf8) String

func (s HwinfoSensorStringUtf8) String() string

type HwinfoUnitStringAscii

type HwinfoUnitStringAscii [hwinfoUnitStringLength]byte

HwinfoUnitStringAscii is the same as HwinfoSensorStringAscii but used for unit strings such as °C and MHz.

type HwinfoUnitStringUtf8

type HwinfoUnitStringUtf8 [hwinfoUnitStringLength]byte

HwinfoUnitStringUtf8 is the same as HwinfoSensorStringUtf8 but used for unit strings such as °C and MHz.

func (HwinfoUnitStringUtf8) String

func (s HwinfoUnitStringUtf8) String() string

type Reader

type Reader struct {
	// GetPointer returns a pointer which is the start of the memory/byte array that contains
	// HWiNFO's sensor data. If the function returns an error, it will be passed along.
	GetPointer func() (uintptr, error)
}

Reader is the part that actually converts bytes to the HwinfoSensor, HwinfoReading, and HwinfoHeader structs. It uses the pointer supplied by GetPointer as a start point to convert the bytes into the aforementioned structs.

func (*Reader) GetHeader

func (reader *Reader) GetHeader() (*HwinfoHeader, error)

GetHeader returns the header of the shared memory. Make sure to lock using Lock().

func (*Reader) GetReadings

func (reader *Reader) GetReadings(info *HwinfoHeader) ([]*HwinfoReading, error)

GetReadings returns all HWiNFO readings.

func (*Reader) GetReadingsById

func (reader *Reader) GetReadingsById(info *HwinfoHeader, readingIds []ReadingIdSensorCombo) ([]*HwinfoReading, error)

GetReadingsById returns the readings that match the given sensor index/id combinations.

func (*Reader) GetSensors

func (reader *Reader) GetSensors(info *HwinfoHeader) ([]*HwinfoSensor, error)

GetSensors returns the sensors that are reported by HWiNFO. Make sure that the given HwinfoHeader is current, meaning that the lock was held when calling Reader.GetHeader and stays held while calling this function and processing its results.

type ReadingIdSensorCombo

type ReadingIdSensorCombo struct {
	Id          uint32
	SensorIndex uint32
}

type ReadingType

type ReadingType uint32
const (
	SENSOR_TYPE_NONE ReadingType = iota
	SENSOR_TYPE_TEMP
	SENSOR_TYPE_VOLT
	SENSOR_TYPE_FAN
	SENSOR_TYPE_CURRENT
	SENSOR_TYPE_POWER
	SENSOR_TYPE_CLOCK
	SENSOR_TYPE_USAGE
	SENSOR_TYPE_OTHER
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL