github.com/jaypipes/ghw@v0.21.1/pkg/memory/memory.go (about)

     1  //
     2  // Use and distribution licensed under the Apache license version 2.
     3  //
     4  // See the COPYING file in the root project directory for full text.
     5  //
     6  
     7  package memory
     8  
     9  import (
    10  	"fmt"
    11  	"math"
    12  
    13  	"github.com/jaypipes/ghw/pkg/context"
    14  	"github.com/jaypipes/ghw/pkg/marshal"
    15  	"github.com/jaypipes/ghw/pkg/option"
    16  	"github.com/jaypipes/ghw/pkg/unitutil"
    17  	"github.com/jaypipes/ghw/pkg/util"
    18  )
    19  
    20  // Module describes a single physical memory module for a host system. Pretty
    21  // much all modern systems contain dual in-line memory modules (DIMMs).
    22  //
    23  // See https://en.wikipedia.org/wiki/DIMM
    24  type Module struct {
    25  	Label        string `json:"label"`
    26  	Location     string `json:"location"`
    27  	SerialNumber string `json:"serial_number"`
    28  	SizeBytes    int64  `json:"size_bytes"`
    29  	Vendor       string `json:"vendor"`
    30  }
    31  
    32  // HugePageAmounts describes huge page info
    33  type HugePageAmounts struct {
    34  	Total   int64 `json:"total"`
    35  	Free    int64 `json:"free"`
    36  	Surplus int64 `json:"surplus"`
    37  	// Note: this field will not be populated for Topology call, since data not present in NUMA folder structure
    38  	Reserved int64 `json:"reserved"`
    39  }
    40  
    41  // Area describes a set of physical memory on a host system. Non-NUMA systems
    42  // will almost always have a single memory area containing all memory the
    43  // system can use. NUMA systems will have multiple memory areas, one or more
    44  // for each NUMA node/cell in the system.
    45  type Area struct {
    46  	TotalPhysicalBytes int64 `json:"total_physical_bytes"`
    47  	TotalUsableBytes   int64 `json:"total_usable_bytes"`
    48  	// An array of sizes, in bytes, of memory pages supported in this area
    49  	SupportedPageSizes []uint64 `json:"supported_page_sizes"`
    50  	// Default system huge page size, in bytes
    51  	DefaultHugePageSize uint64 `json:"default_huge_page_size"`
    52  	// Amount of memory, in bytes, consumed by huge pages of all sizes
    53  	TotalHugePageBytes int64 `json:"total_huge_page_bytes"`
    54  	// Huge page info by size
    55  	HugePageAmountsBySize map[uint64]*HugePageAmounts `json:"huge_page_amounts_by_size"`
    56  	Modules               []*Module                   `json:"modules"`
    57  }
    58  
    59  // String returns a short string with a summary of information for this memory
    60  // area
    61  func (a *Area) String() string {
    62  	tpbs := util.UNKNOWN
    63  	if a.TotalPhysicalBytes > 0 {
    64  		tpb := a.TotalPhysicalBytes
    65  		unit, unitStr := unitutil.AmountString(tpb)
    66  		tpb = int64(math.Ceil(float64(a.TotalPhysicalBytes) / float64(unit)))
    67  		tpbs = fmt.Sprintf("%d%s", tpb, unitStr)
    68  	}
    69  	tubs := util.UNKNOWN
    70  	if a.TotalUsableBytes > 0 {
    71  		tub := a.TotalUsableBytes
    72  		unit, unitStr := unitutil.AmountString(tub)
    73  		tub = int64(math.Ceil(float64(a.TotalUsableBytes) / float64(unit)))
    74  		tubs = fmt.Sprintf("%d%s", tub, unitStr)
    75  	}
    76  	return fmt.Sprintf("memory (%s physical, %s usable)", tpbs, tubs)
    77  }
    78  
    79  // Info contains information about the memory on a host system.
    80  type Info struct {
    81  	ctx *context.Context
    82  	Area
    83  }
    84  
    85  // New returns an Info struct that describes the memory on a host system.
    86  func New(opts ...*option.Option) (*Info, error) {
    87  	ctx := context.New(opts...)
    88  	info := &Info{ctx: ctx}
    89  	if err := ctx.Do(info.load); err != nil {
    90  		return nil, err
    91  	}
    92  	return info, nil
    93  }
    94  
    95  // String returns a short string with a summary of memory information
    96  func (i *Info) String() string {
    97  	return i.Area.String()
    98  }
    99  
   100  // simple private struct used to encapsulate memory information in a top-level
   101  // "memory" YAML/JSON map/object key
   102  type memoryPrinter struct {
   103  	Info *Info `json:"memory"`
   104  }
   105  
   106  // YAMLString returns a string with the memory information formatted as YAML
   107  // under a top-level "memory:" key
   108  func (i *Info) YAMLString() string {
   109  	return marshal.SafeYAML(i.ctx, memoryPrinter{i})
   110  }
   111  
   112  // JSONString returns a string with the memory information formatted as JSON
   113  // under a top-level "memory:" key
   114  func (i *Info) JSONString(indent bool) string {
   115  	return marshal.SafeJSON(i.ctx, memoryPrinter{i}, indent)
   116  }