@@ -142,6 +142,49 @@ func parseCPUThermalThrottle(cpuPath string) (*CPUThermalThrottle, error) {
142142 return & t , nil
143143}
144144
145+ func binSearch (elem uint16 , elemSlice * []uint16 ) bool {
146+ if len (* elemSlice ) == 0 {
147+ return false
148+ }
149+
150+ if len (* elemSlice ) == 1 {
151+ return elem == (* elemSlice )[0 ]
152+ }
153+
154+ start := 0
155+ end := len (* elemSlice ) - 1
156+
157+ var mid int
158+
159+ for start <= end {
160+ mid = (start + end ) / 2
161+ if (* elemSlice )[mid ] == elem {
162+ return true
163+ } else if (* elemSlice )[mid ] > elem {
164+ end = mid - 1
165+ } else if (* elemSlice )[mid ] < elem {
166+ start = mid + 1
167+ }
168+ }
169+
170+ return false
171+ }
172+
173+ func filterOfflineCPUs (offlineCpus * []uint16 , cpus * []string ) error {
174+ for i , cpu := range * cpus {
175+ cpuName := strings .TrimPrefix (filepath .Base (cpu ), "cpu" )
176+ cpuNameUint16 , err := strconv .Atoi (cpuName )
177+ if err != nil {
178+ return err
179+ }
180+ if binSearch (uint16 (cpuNameUint16 ), offlineCpus ) {
181+ * cpus = append ((* cpus )[:i ], (* cpus )[i + 1 :]... )
182+ }
183+ }
184+
185+ return nil
186+ }
187+
145188// SystemCpufreq returns CPU frequency metrics for all CPUs.
146189func (fs FS ) SystemCpufreq () ([]SystemCPUCpufreqStats , error ) {
147190 var g errgroup.Group
@@ -151,6 +194,25 @@ func (fs FS) SystemCpufreq() ([]SystemCPUCpufreqStats, error) {
151194 return nil , err
152195 }
153196
197+ line , err := util .ReadFileNoStat (fs .sys .Path ("devices/system/cpu/offline" ))
198+ if err != nil {
199+ return nil , err
200+ }
201+
202+ if strings .TrimSpace (string (line )) != "" {
203+ offlineCPUs , err := parseCPURange (line )
204+ if err != nil {
205+ return nil , err
206+ }
207+
208+ if len (offlineCPUs ) > 0 {
209+ err = filterOfflineCPUs (& offlineCPUs , & cpus )
210+ if err != nil {
211+ return nil , err
212+ }
213+ }
214+ }
215+
154216 systemCpufreq := make ([]SystemCPUCpufreqStats , len (cpus ))
155217 for i , cpu := range cpus {
156218 cpuName := strings .TrimPrefix (filepath .Base (cpu ), "cpu" )
@@ -251,12 +313,12 @@ func (fs FS) IsolatedCPUs() ([]uint16, error) {
251313 return nil , err
252314 }
253315
254- return parseIsolatedCPUs (isolcpus )
316+ return parseCPURange (isolcpus )
255317}
256318
257- func parseIsolatedCPUs (data []byte ) ([]uint16 , error ) {
319+ func parseCPURange (data []byte ) ([]uint16 , error ) {
258320
259- var isolcpusInt = []uint16 {}
321+ var cpusInt = []uint16 {}
260322
261323 for _ , cpu := range strings .Split (strings .TrimRight (string (data ), "\n " ), "," ) {
262324 if cpu == "" {
@@ -277,7 +339,7 @@ func parseIsolatedCPUs(data []byte) ([]uint16, error) {
277339 }
278340
279341 for i := startRange ; i <= endRange ; i ++ {
280- isolcpusInt = append (isolcpusInt , uint16 (i ))
342+ cpusInt = append (cpusInt , uint16 (i ))
281343 }
282344 continue
283345 }
@@ -286,7 +348,7 @@ func parseIsolatedCPUs(data []byte) ([]uint16, error) {
286348 if err != nil {
287349 return nil , err
288350 }
289- isolcpusInt = append (isolcpusInt , uint16 (cpuN ))
351+ cpusInt = append (cpusInt , uint16 (cpuN ))
290352 }
291- return isolcpusInt , nil
353+ return cpusInt , nil
292354}
0 commit comments