mirror of
				https://github.com/ClusterCockpit/cc-metric-collector.git
				synced 2025-11-04 02:35:07 +01:00 
			
		
		
		
	Reuse ccTopology functionality
This commit is contained in:
		@@ -11,22 +11,13 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
 | 
						cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
 | 
				
			||||||
	lp "github.com/ClusterCockpit/cc-metric-collector/pkg/ccMetric"
 | 
						lp "github.com/ClusterCockpit/cc-metric-collector/pkg/ccMetric"
 | 
				
			||||||
 | 
						"github.com/ClusterCockpit/cc-metric-collector/pkg/ccTopology"
 | 
				
			||||||
	"golang.org/x/sys/unix"
 | 
						"golang.org/x/sys/unix"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type CPUFreqCollectorTopology struct {
 | 
					type CPUFreqCollectorTopology struct {
 | 
				
			||||||
	processor               string // logical processor number (continuous, starting at 0)
 | 
						scalingCurFreqFile string
 | 
				
			||||||
	coreID                  string // socket local core ID
 | 
						tagSet             map[string]string
 | 
				
			||||||
	coreID_int              int64
 | 
					 | 
				
			||||||
	physicalPackageID       string // socket / package ID
 | 
					 | 
				
			||||||
	physicalPackageID_int   int64
 | 
					 | 
				
			||||||
	numPhysicalPackages     string // number of  sockets / packages
 | 
					 | 
				
			||||||
	numPhysicalPackages_int int64
 | 
					 | 
				
			||||||
	isHT                    bool
 | 
					 | 
				
			||||||
	numNonHT                string // number of non hyper-threading processors
 | 
					 | 
				
			||||||
	numNonHT_int            int64
 | 
					 | 
				
			||||||
	scalingCurFreqFile      string
 | 
					 | 
				
			||||||
	tagSet                  map[string]string
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// CPUFreqCollector
 | 
					// CPUFreqCollector
 | 
				
			||||||
@@ -64,112 +55,38 @@ func (m *CPUFreqCollector) Init(config json.RawMessage) error {
 | 
				
			|||||||
		"unit":   "Hz",
 | 
							"unit":   "Hz",
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Loop for all CPU directories
 | 
						m.topology = make([]CPUFreqCollectorTopology, 0)
 | 
				
			||||||
	baseDir := "/sys/devices/system/cpu"
 | 
						for _, c := range ccTopology.CpuData() {
 | 
				
			||||||
	globPattern := filepath.Join(baseDir, "cpu[0-9]*")
 | 
					 | 
				
			||||||
	cpuDirs, err := filepath.Glob(globPattern)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return fmt.Errorf("unable to glob files with pattern '%s': %v", globPattern, err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if cpuDirs == nil {
 | 
					 | 
				
			||||||
		return fmt.Errorf("unable to find any files with pattern '%s'", globPattern)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Initialize CPU topology
 | 
							// Skip hyper threading CPUs
 | 
				
			||||||
	m.topology = make([]CPUFreqCollectorTopology, len(cpuDirs))
 | 
							if c.CpuID != c.CoreCPUsList[0] {
 | 
				
			||||||
	for _, cpuDir := range cpuDirs {
 | 
								continue
 | 
				
			||||||
		processor := strings.TrimPrefix(cpuDir, "/sys/devices/system/cpu/cpu")
 | 
					 | 
				
			||||||
		processor_int, err := strconv.ParseInt(processor, 10, 64)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return fmt.Errorf("unable to convert cpuID '%s' to int64: %v", processor, err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// Read package ID
 | 
					 | 
				
			||||||
		physicalPackageIDFile := filepath.Join(cpuDir, "topology", "physical_package_id")
 | 
					 | 
				
			||||||
		line, err := os.ReadFile(physicalPackageIDFile)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return fmt.Errorf("unable to read physical package ID from file '%s': %v", physicalPackageIDFile, err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		physicalPackageID := strings.TrimSpace(string(line))
 | 
					 | 
				
			||||||
		physicalPackageID_int, err := strconv.ParseInt(physicalPackageID, 10, 64)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return fmt.Errorf("unable to convert packageID '%s' to int64: %v", physicalPackageID, err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// Read core ID
 | 
					 | 
				
			||||||
		coreIDFile := filepath.Join(cpuDir, "topology", "core_id")
 | 
					 | 
				
			||||||
		line, err = os.ReadFile(coreIDFile)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return fmt.Errorf("unable to read core ID from file '%s': %v", coreIDFile, err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		coreID := strings.TrimSpace(string(line))
 | 
					 | 
				
			||||||
		coreID_int, err := strconv.ParseInt(coreID, 10, 64)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return fmt.Errorf("unable to convert coreID '%s' to int64: %v", coreID, err)
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Check access to current frequency file
 | 
							// Check access to current frequency file
 | 
				
			||||||
		scalingCurFreqFile := filepath.Join(cpuDir, "cpufreq", "scaling_cur_freq")
 | 
							scalingCurFreqFile := filepath.Join("/sys/devices/system/cpu", fmt.Sprintf("cpu%d", c.CpuID), "cpufreq/scaling_cur_freq")
 | 
				
			||||||
		err = unix.Access(scalingCurFreqFile, unix.R_OK)
 | 
							err := unix.Access(scalingCurFreqFile, unix.R_OK)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return fmt.Errorf("unable to access file '%s': %v", scalingCurFreqFile, err)
 | 
								return fmt.Errorf("unable to access file '%s': %v", scalingCurFreqFile, err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		t := &m.topology[processor_int]
 | 
							m.topology = append(m.topology,
 | 
				
			||||||
		t.processor = processor
 | 
								CPUFreqCollectorTopology{
 | 
				
			||||||
		t.physicalPackageID = physicalPackageID
 | 
									tagSet: map[string]string{
 | 
				
			||||||
		t.physicalPackageID_int = physicalPackageID_int
 | 
										"type":       "hwthread",
 | 
				
			||||||
		t.coreID = coreID
 | 
										"type-id":    fmt.Sprint(c.CpuID),
 | 
				
			||||||
		t.coreID_int = coreID_int
 | 
										"package_id": fmt.Sprint(c.Socket),
 | 
				
			||||||
		t.scalingCurFreqFile = scalingCurFreqFile
 | 
									},
 | 
				
			||||||
	}
 | 
									scalingCurFreqFile: scalingCurFreqFile,
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
	// is processor a hyper-thread?
 | 
							)
 | 
				
			||||||
	coreSeenBefore := make(map[string]bool)
 | 
					 | 
				
			||||||
	for i := range m.topology {
 | 
					 | 
				
			||||||
		t := &m.topology[i]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		globalID := t.physicalPackageID + ":" + t.coreID
 | 
					 | 
				
			||||||
		t.isHT = coreSeenBefore[globalID]
 | 
					 | 
				
			||||||
		coreSeenBefore[globalID] = true
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// number of non hyper-thread cores and packages / sockets
 | 
					 | 
				
			||||||
	var numNonHT_int int64 = 0
 | 
					 | 
				
			||||||
	PhysicalPackageIDs := make(map[int64]struct{})
 | 
					 | 
				
			||||||
	for i := range m.topology {
 | 
					 | 
				
			||||||
		t := &m.topology[i]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if !t.isHT {
 | 
					 | 
				
			||||||
			numNonHT_int++
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		PhysicalPackageIDs[t.physicalPackageID_int] = struct{}{}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	numPhysicalPackageID_int := int64(len(PhysicalPackageIDs))
 | 
					 | 
				
			||||||
	numPhysicalPackageID := fmt.Sprint(numPhysicalPackageID_int)
 | 
					 | 
				
			||||||
	numNonHT := fmt.Sprint(numNonHT_int)
 | 
					 | 
				
			||||||
	for i := range m.topology {
 | 
					 | 
				
			||||||
		t := &m.topology[i]
 | 
					 | 
				
			||||||
		t.numPhysicalPackages = numPhysicalPackageID
 | 
					 | 
				
			||||||
		t.numPhysicalPackages_int = numPhysicalPackageID_int
 | 
					 | 
				
			||||||
		t.numNonHT = numNonHT
 | 
					 | 
				
			||||||
		t.numNonHT_int = numNonHT_int
 | 
					 | 
				
			||||||
		t.tagSet = map[string]string{
 | 
					 | 
				
			||||||
			"type":       "hwthread",
 | 
					 | 
				
			||||||
			"type-id":    t.processor,
 | 
					 | 
				
			||||||
			"package_id": t.physicalPackageID,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Initialized
 | 
						// Initialized
 | 
				
			||||||
	cclog.ComponentDebug(
 | 
						cclog.ComponentDebug(
 | 
				
			||||||
		m.name,
 | 
							m.name,
 | 
				
			||||||
		"initialized",
 | 
							"initialized",
 | 
				
			||||||
		numPhysicalPackageID_int, "physical packages,",
 | 
							len(m.topology), "non-hyper-threading CPUs")
 | 
				
			||||||
		len(cpuDirs), "CPUs,",
 | 
					 | 
				
			||||||
		numNonHT, "non-hyper-threading CPUs")
 | 
					 | 
				
			||||||
	m.init = true
 | 
						m.init = true
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -184,11 +101,6 @@ func (m *CPUFreqCollector) Read(interval time.Duration, output chan lp.CCMetric)
 | 
				
			|||||||
	for i := range m.topology {
 | 
						for i := range m.topology {
 | 
				
			||||||
		t := &m.topology[i]
 | 
							t := &m.topology[i]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// skip hyper-threads
 | 
					 | 
				
			||||||
		if t.isHT {
 | 
					 | 
				
			||||||
			continue
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// Read current frequency
 | 
							// Read current frequency
 | 
				
			||||||
		line, err := os.ReadFile(t.scalingCurFreqFile)
 | 
							line, err := os.ReadFile(t.scalingCurFreqFile)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user