mirror of
				https://github.com/ClusterCockpit/cc-metric-collector.git
				synced 2025-10-31 09:05:05 +01:00 
			
		
		
		
	Avoid type conversion by using Atoi
Avoid copying structs by using pointer access Increase readability with CamelCase variable names
This commit is contained in:
		| @@ -30,13 +30,13 @@ func fileToInt(path string) int { | |||||||
| 		cclogger.ComponentError("ccTopology", "Reading", path, ":", err.Error()) | 		cclogger.ComponentError("ccTopology", "Reading", path, ":", err.Error()) | ||||||
| 		return -1 | 		return -1 | ||||||
| 	} | 	} | ||||||
| 	sbuffer := strings.TrimSpace(string(buffer)) | 	stringBuffer := strings.TrimSpace(string(buffer)) | ||||||
| 	id, err := strconv.ParseInt(sbuffer, 10, 32) | 	id, err := strconv.Atoi(stringBuffer) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		cclogger.ComponentError("ccTopology", "Parsing", path, ":", sbuffer, err.Error()) | 		cclogger.ComponentError("ccTopology", "Parsing", path, ":", stringBuffer, err.Error()) | ||||||
| 		return -1 | 		return -1 | ||||||
| 	} | 	} | ||||||
| 	return int(id) | 	return id | ||||||
| } | } | ||||||
|  |  | ||||||
| // SocketList gets the list of CPU socket IDs | // SocketList gets the list of CPU socket IDs | ||||||
| @@ -55,13 +55,13 @@ func SocketList() []int { | |||||||
| 		line := scanner.Text() | 		line := scanner.Text() | ||||||
| 		if strings.HasPrefix(line, "physical id") { | 		if strings.HasPrefix(line, "physical id") { | ||||||
| 			lv := strings.Fields(line) | 			lv := strings.Fields(line) | ||||||
| 			id, err := strconv.ParseInt(lv[3], 10, 32) | 			id, err := strconv.Atoi(lv[3]) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				log.Print(err) | 				log.Print(err) | ||||||
| 				return nil | 				return nil | ||||||
| 			} | 			} | ||||||
| 			if found := slices.Contains(packs, int(id)); !found { | 			if found := slices.Contains(packs, id); !found { | ||||||
| 				packs = append(packs, int(id)) | 				packs = append(packs, id) | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -83,13 +83,13 @@ func HwthreadList() []int { | |||||||
| 		line := scanner.Text() | 		line := scanner.Text() | ||||||
| 		if strings.HasPrefix(line, "processor") { | 		if strings.HasPrefix(line, "processor") { | ||||||
| 			lv := strings.Fields(line) | 			lv := strings.Fields(line) | ||||||
| 			id, err := strconv.ParseInt(lv[2], 10, 32) | 			id, err := strconv.Atoi(lv[2]) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				log.Print(err) | 				log.Print(err) | ||||||
| 				return nil | 				return nil | ||||||
| 			} | 			} | ||||||
| 			if found := slices.Contains(cpuList, int(id)); !found { | 			if found := slices.Contains(cpuList, id); !found { | ||||||
| 				cpuList = append(cpuList, int(id)) | 				cpuList = append(cpuList, id) | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -117,13 +117,13 @@ func CoreList() []int { | |||||||
| 		line := scanner.Text() | 		line := scanner.Text() | ||||||
| 		if strings.HasPrefix(line, "core id") { | 		if strings.HasPrefix(line, "core id") { | ||||||
| 			lv := strings.Fields(line) | 			lv := strings.Fields(line) | ||||||
| 			id, err := strconv.ParseInt(lv[3], 10, 32) | 			id, err := strconv.Atoi(lv[3]) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				log.Print(err) | 				log.Print(err) | ||||||
| 				return nil | 				return nil | ||||||
| 			} | 			} | ||||||
| 			if found := slices.Contains(coreList, int(id)); !found { | 			if found := slices.Contains(coreList, id); !found { | ||||||
| 				coreList = append(coreList, int(id)) | 				coreList = append(coreList, id) | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -176,8 +176,8 @@ func DieList() []int { | |||||||
| 			"topology/die_id") | 			"topology/die_id") | ||||||
| 		dieID := fileToInt(diePath) | 		dieID := fileToInt(diePath) | ||||||
| 		if dieID > 0 { | 		if dieID > 0 { | ||||||
| 			if found := slices.Contains(dieList, int(dieID)); !found { | 			if found := slices.Contains(dieList, dieID); !found { | ||||||
| 				dieList = append(dieList, int(dieID)) | 				dieList = append(dieList, dieID) | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -242,11 +242,11 @@ func CpuData() []HwthreadEntry { | |||||||
| 			threadList := make([]int, 0) | 			threadList := make([]int, 0) | ||||||
| 			stringBuffer := strings.TrimSpace(string(buffer)) | 			stringBuffer := strings.TrimSpace(string(buffer)) | ||||||
| 			for _, x := range strings.Split(stringBuffer, ",") { | 			for _, x := range strings.Split(stringBuffer, ",") { | ||||||
| 				id, err := strconv.ParseInt(x, 10, 32) | 				id, err := strconv.Atoi(x) | ||||||
| 				if err != nil { | 				if err != nil { | ||||||
| 					cclogger.ComponentError("CCTopology", "CpuData:getSMT", err.Error()) | 					cclogger.ComponentError("CCTopology", "CpuData:getSMT", err.Error()) | ||||||
| 				} | 				} | ||||||
| 				threadList = append(threadList, int(id)) | 				threadList = append(threadList, id) | ||||||
| 			} | 			} | ||||||
| 			if i := slices.Index(threadList, cpuID); i != -1 { | 			if i := slices.Index(threadList, cpuID); i != -1 { | ||||||
| 				return i | 				return i | ||||||
| @@ -265,7 +265,6 @@ func CpuData() []HwthreadEntry { | |||||||
| 			} | 			} | ||||||
| 			for _, file := range files { | 			for _, file := range files { | ||||||
| 				matches := regex.FindStringSubmatch(file) | 				matches := regex.FindStringSubmatch(file) | ||||||
| 				fmt.Println(len(matches)) |  | ||||||
| 				if len(matches) == 2 { | 				if len(matches) == 2 { | ||||||
| 					id, err := strconv.Atoi(matches[1]) | 					id, err := strconv.Atoi(matches[1]) | ||||||
| 					if err == nil { | 					if err == nil { | ||||||
| @@ -326,7 +325,7 @@ type CpuInformation struct { | |||||||
| 	NumNumaDomains int | 	NumNumaDomains int | ||||||
| } | } | ||||||
|  |  | ||||||
| // Get basic information about the CPU | // CpuInformation reports basic information about the CPU | ||||||
| func CpuInfo() CpuInformation { | func CpuInfo() CpuInformation { | ||||||
|  |  | ||||||
| 	smtList := make([]int, 0) | 	smtList := make([]int, 0) | ||||||
| @@ -334,8 +333,9 @@ func CpuInfo() CpuInformation { | |||||||
| 	dieList := make([]int, 0) | 	dieList := make([]int, 0) | ||||||
| 	socketList := make([]int, 0) | 	socketList := make([]int, 0) | ||||||
| 	coreList := make([]int, 0) | 	coreList := make([]int, 0) | ||||||
| 	cdata := CpuData() | 	cpuData := CpuData() | ||||||
| 	for _, d := range cdata { | 	for i := range cpuData { | ||||||
|  | 		d := &cpuData[i] | ||||||
| 		if ok := slices.Contains(smtList, d.SMT); !ok { | 		if ok := slices.Contains(smtList, d.SMT); !ok { | ||||||
| 			smtList = append(smtList, d.SMT) | 			smtList = append(smtList, d.SMT) | ||||||
| 		} | 		} | ||||||
| @@ -358,98 +358,110 @@ func CpuInfo() CpuInformation { | |||||||
| 		NumDies:        len(dieList), | 		NumDies:        len(dieList), | ||||||
| 		NumCores:       len(coreList), | 		NumCores:       len(coreList), | ||||||
| 		NumSockets:     len(socketList), | 		NumSockets:     len(socketList), | ||||||
| 		NumHWthreads:   len(cdata), | 		NumHWthreads:   len(cpuData), | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| // Get the CPU socket ID for a given hardware thread ID | // GetHwthreadSocket gets the CPU socket ID for a given hardware thread ID | ||||||
| func GetHwthreadSocket(cpuid int) int { | // In case hardware thread ID is not found -1 is returned | ||||||
| 	cdata := CpuData() | func GetHwthreadSocket(cpuID int) int { | ||||||
| 	for _, d := range cdata { | 	cpuData := CpuData() | ||||||
| 		if d.CpuID == cpuid { | 	for i := range cpuData { | ||||||
|  | 		d := &cpuData[i] | ||||||
|  | 		if d.CpuID == cpuID { | ||||||
| 			return d.Socket | 			return d.Socket | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return -1 | 	return -1 | ||||||
| } | } | ||||||
|  |  | ||||||
| // Get the NUMA node ID for a given hardware thread ID | // GetHwthreadNumaDomain gets the NUMA domain ID for a given hardware thread ID | ||||||
| func GetHwthreadNumaDomain(cpuid int) int { | // In case hardware thread ID is not found -1 is returned | ||||||
| 	cdata := CpuData() | func GetHwthreadNumaDomain(cpuID int) int { | ||||||
| 	for _, d := range cdata { | 	cpuData := CpuData() | ||||||
| 		if d.CpuID == cpuid { | 	for i := range cpuData { | ||||||
|  | 		d := &cpuData[i] | ||||||
|  | 		if d.CpuID == cpuID { | ||||||
| 			return d.NumaDomain | 			return d.NumaDomain | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return -1 | 	return -1 | ||||||
| } | } | ||||||
|  |  | ||||||
| // Get the CPU die ID for a given hardware thread ID | // GetHwthreadDie gets the CPU die ID for a given hardware thread ID | ||||||
| func GetHwthreadDie(cpuid int) int { | // In case hardware thread ID is not found -1 is returned | ||||||
| 	cdata := CpuData() | func GetHwthreadDie(cpuID int) int { | ||||||
| 	for _, d := range cdata { | 	cpuData := CpuData() | ||||||
| 		if d.CpuID == cpuid { | 	for i := range cpuData { | ||||||
|  | 		d := &cpuData[i] | ||||||
|  | 		if d.CpuID == cpuID { | ||||||
| 			return d.Die | 			return d.Die | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return -1 | 	return -1 | ||||||
| } | } | ||||||
|  |  | ||||||
| // Get the CPU core ID for a given hardware thread ID | // GetHwthreadCore gets the CPU core ID for a given hardware thread ID | ||||||
| func GetHwthreadCore(cpuid int) int { | // In case hardware thread ID is not found -1 is returned | ||||||
| 	cdata := CpuData() | func GetHwthreadCore(cpuID int) int { | ||||||
| 	for _, d := range cdata { | 	cpuData := CpuData() | ||||||
| 		if d.CpuID == cpuid { | 	for i := range cpuData { | ||||||
|  | 		d := &cpuData[i] | ||||||
|  | 		if d.CpuID == cpuID { | ||||||
| 			return d.Core | 			return d.Core | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return -1 | 	return -1 | ||||||
| } | } | ||||||
|  |  | ||||||
| // Get the all hardware thread ID associated with a CPU socket | // GetSocketHwthreads gets all hardware thread IDs associated with a CPU socket | ||||||
| func GetSocketHwthreads(socket int) []int { | func GetSocketHwthreads(socket int) []int { | ||||||
| 	all := CpuData() | 	cpuData := CpuData() | ||||||
| 	cpulist := make([]int, 0) | 	cpuList := make([]int, 0) | ||||||
| 	for _, d := range all { | 	for i := range cpuData { | ||||||
|  | 		d := &cpuData[i] | ||||||
| 		if d.Socket == socket { | 		if d.Socket == socket { | ||||||
| 			cpulist = append(cpulist, d.CpuID) | 			cpuList = append(cpuList, d.CpuID) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return cpulist | 	return cpuList | ||||||
| } | } | ||||||
|  |  | ||||||
| // Get the all hardware thread ID associated with a NUMA node | // GetNumaDomainHwthreads gets the all hardware thread IDs associated with a NUMA domain | ||||||
| func GetNumaDomainHwthreads(domain int) []int { | func GetNumaDomainHwthreads(numaDomain int) []int { | ||||||
| 	all := CpuData() | 	cpuData := CpuData() | ||||||
| 	cpulist := make([]int, 0) | 	cpuList := make([]int, 0) | ||||||
| 	for _, d := range all { | 	for i := range cpuData { | ||||||
| 		if d.NumaDomain == domain { | 		d := &cpuData[i] | ||||||
| 			cpulist = append(cpulist, d.CpuID) | 		if d.NumaDomain == numaDomain { | ||||||
|  | 			cpuList = append(cpuList, d.CpuID) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return cpulist | 	return cpuList | ||||||
| } | } | ||||||
|  |  | ||||||
| // Get the all hardware thread ID associated with a CPU die | // GetDieHwthreads gets all hardware thread IDs associated with a CPU die | ||||||
| func GetDieHwthreads(die int) []int { | func GetDieHwthreads(die int) []int { | ||||||
| 	all := CpuData() | 	cpuData := CpuData() | ||||||
| 	cpulist := make([]int, 0) | 	cpuList := make([]int, 0) | ||||||
| 	for _, d := range all { | 	for i := range cpuData { | ||||||
|  | 		d := &cpuData[i] | ||||||
| 		if d.Die == die { | 		if d.Die == die { | ||||||
| 			cpulist = append(cpulist, d.CpuID) | 			cpuList = append(cpuList, d.CpuID) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return cpulist | 	return cpuList | ||||||
| } | } | ||||||
|  |  | ||||||
| // Get the all hardware thread ID associated with a CPU core | // GetCoreHwthreads get all hardware thread IDs associated with a CPU core | ||||||
| func GetCoreHwthreads(core int) []int { | func GetCoreHwthreads(core int) []int { | ||||||
| 	all := CpuData() | 	cpuData := CpuData() | ||||||
| 	cpulist := make([]int, 0) | 	cpuList := make([]int, 0) | ||||||
| 	for _, d := range all { | 	for i := range cpuData { | ||||||
|  | 		d := &cpuData[i] | ||||||
| 		if d.Core == core { | 		if d.Core == core { | ||||||
| 			cpulist = append(cpulist, d.CpuID) | 			cpuList = append(cpuList, d.CpuID) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return cpulist | 	return cpuList | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user