package linux_generic import ( "strings" "git.mchus.pro/mchus/logpile/internal/models" ) // lscpuInfo is the system-wide CPU summary printed by `lscpu`. type lscpuInfo struct { model string sockets int coresPerSocket int threadsPerCore int mhz int maxMHz int } func parseLscpu(file sourceFile, inv *inventory) { info := &inv.lscpu for _, line := range strings.Split(file.content, "\n") { key, value, ok := splitKeyValue(line) if !ok || value == "" { continue } switch key { case "Model name": setIfEmpty(&info.model, value) case "Socket(s)": setIntIfZero(&info.sockets, atoi(value)) case "Core(s) per socket": setIntIfZero(&info.coresPerSocket, atoi(value)) case "Thread(s) per core": setIntIfZero(&info.threadsPerCore, atoi(value)) case "CPU MHz": setIntIfZero(&info.mhz, int(leadingNumber(value))) case "CPU max MHz": setIntIfZero(&info.maxMHz, int(leadingNumber(value))) } } } func setIntIfZero(dst *int, value int) { if *dst == 0 { *dst = value } } // buildCPUs prefers per-socket dmidecode records and fills their gaps from // lscpu; with lscpu alone it emits one record per reported socket. func (inv *inventory) buildCPUs() []models.CPU { info := inv.lscpu threads := info.coresPerSocket * info.threadsPerCore if len(inv.cpus) > 0 { cpus := make([]models.CPU, len(inv.cpus)) copy(cpus, inv.cpus) for i := range cpus { setIfEmpty(&cpus[i].Model, info.model) setIntIfZero(&cpus[i].Cores, info.coresPerSocket) setIntIfZero(&cpus[i].Threads, threads) setIntIfZero(&cpus[i].MaxFreqMHz, info.maxMHz) } return cpus } if info.model == "" { return nil } sockets := info.sockets if sockets <= 0 { sockets = 1 } cpus := make([]models.CPU, 0, sockets) for socket := 0; socket < sockets; socket++ { cpus = append(cpus, models.CPU{ Socket: socket, Model: info.model, Cores: info.coresPerSocket, Threads: threads, FrequencyMHz: info.mhz, MaxFreqMHz: info.maxMHz, }) } return cpus }