github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/client/fingerprint/cpu.go (about)

     1  package fingerprint
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/nomad/client/config"
     8  	"github.com/hashicorp/nomad/helper/stats"
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  )
    11  
    12  // CPUFingerprint is used to fingerprint the CPU
    13  type CPUFingerprint struct {
    14  	StaticFingerprinter
    15  	logger *log.Logger
    16  }
    17  
    18  // NewCPUFingerprint is used to create a CPU fingerprint
    19  func NewCPUFingerprint(logger *log.Logger) Fingerprint {
    20  	f := &CPUFingerprint{logger: logger}
    21  	return f
    22  }
    23  
    24  func (f *CPUFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
    25  	if err := stats.Init(); err != nil {
    26  		return false, fmt.Errorf("Unable to obtain CPU information: %v", err)
    27  	}
    28  
    29  	modelName := stats.CPUModelName()
    30  	if modelName != "" {
    31  		node.Attributes["cpu.modelname"] = modelName
    32  	}
    33  
    34  	mhz := stats.CPUMHzPerCore()
    35  	node.Attributes["cpu.frequency"] = fmt.Sprintf("%.0f", mhz)
    36  	f.logger.Printf("[DEBUG] fingerprint.cpu: frequency: %.0f MHz", mhz)
    37  
    38  	numCores := stats.CPUNumCores()
    39  	node.Attributes["cpu.numcores"] = fmt.Sprintf("%d", numCores)
    40  	f.logger.Printf("[DEBUG] fingerprint.cpu: core count: %d", numCores)
    41  
    42  	tt := stats.TotalTicksAvailable()
    43  	node.Attributes["cpu.totalcompute"] = fmt.Sprintf("%.0f", tt)
    44  
    45  	if node.Resources == nil {
    46  		node.Resources = &structs.Resources{}
    47  	}
    48  
    49  	node.Resources.CPU = int(tt)
    50  
    51  	return true, nil
    52  }