-
Notifications
You must be signed in to change notification settings - Fork 356
multiplatform support for cpuinfo #257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
discordianfish
merged 3 commits into
prometheus:master
from
pgier:cpuinfo-multi-platform
May 5, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,11 +11,15 @@ | |
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // +build linux | ||
|
|
||
| package procfs | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "errors" | ||
| "regexp" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
|
|
@@ -52,6 +56,11 @@ type CPUInfo struct { | |
| PowerManagement string | ||
| } | ||
|
|
||
| var ( | ||
| cpuinfoClockRegexp = regexp.MustCompile(`([\d.]+)`) | ||
| cpuinfoS390XProcessorRegexp = regexp.MustCompile(`^processor\s+(\d+):.*`) | ||
| ) | ||
|
|
||
| // CPUInfo returns information about current system CPUs. | ||
| // See https://www.kernel.org/doc/Documentation/filesystems/proc.txt | ||
| func (fs FS) CPUInfo() ([]CPUInfo, error) { | ||
|
|
@@ -62,14 +71,26 @@ func (fs FS) CPUInfo() ([]CPUInfo, error) { | |
| return parseCPUInfo(data) | ||
| } | ||
|
|
||
| // parseCPUInfo parses data from /proc/cpuinfo | ||
| func parseCPUInfo(info []byte) ([]CPUInfo, error) { | ||
| cpuinfo := []CPUInfo{} | ||
| i := -1 | ||
| func parseCPUInfoX86(info []byte) ([]CPUInfo, error) { | ||
| scanner := bufio.NewScanner(bytes.NewReader(info)) | ||
|
|
||
| // find the first "processor" line | ||
| firstLine := firstNonEmptyLine(scanner) | ||
| if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") { | ||
| return nil, errors.New("invalid cpuinfo file: " + firstLine) | ||
| } | ||
| field := strings.SplitN(firstLine, ": ", 2) | ||
| v, err := strconv.ParseUint(field[1], 0, 32) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| firstcpu := CPUInfo{Processor: uint(v)} | ||
| cpuinfo := []CPUInfo{firstcpu} | ||
| i := 0 | ||
|
|
||
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
| if strings.TrimSpace(line) == "" { | ||
| if !strings.Contains(line, ":") { | ||
| continue | ||
| } | ||
| field := strings.SplitN(line, ": ", 2) | ||
|
|
@@ -82,7 +103,7 @@ func parseCPUInfo(info []byte) ([]CPUInfo, error) { | |
| return nil, err | ||
| } | ||
| cpuinfo[i].Processor = uint(v) | ||
| case "vendor_id": | ||
| case "vendor", "vendor_id": | ||
| cpuinfo[i].VendorID = field[1] | ||
| case "cpu family": | ||
| cpuinfo[i].CPUFamily = field[1] | ||
|
|
@@ -163,5 +184,175 @@ func parseCPUInfo(info []byte) ([]CPUInfo, error) { | |
| } | ||
| } | ||
| return cpuinfo, nil | ||
| } | ||
|
|
||
| func parseCPUInfoARM(info []byte) ([]CPUInfo, error) { | ||
| scanner := bufio.NewScanner(bytes.NewReader(info)) | ||
|
|
||
| firstLine := firstNonEmptyLine(scanner) | ||
| if !strings.HasPrefix(firstLine, "Processor") || !strings.Contains(firstLine, ":") { | ||
| return nil, errors.New("invalid cpuinfo file: " + firstLine) | ||
| } | ||
| field := strings.SplitN(firstLine, ": ", 2) | ||
| commonCPUInfo := CPUInfo{VendorID: field[1]} | ||
|
|
||
| cpuinfo := []CPUInfo{} | ||
| i := -1 | ||
| featuresLine := "" | ||
|
|
||
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
| if !strings.Contains(line, ":") { | ||
| continue | ||
| } | ||
| field := strings.SplitN(line, ": ", 2) | ||
| switch strings.TrimSpace(field[0]) { | ||
| case "processor": | ||
| cpuinfo = append(cpuinfo, commonCPUInfo) // start of the next processor | ||
| i++ | ||
| v, err := strconv.ParseUint(field[1], 0, 32) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| cpuinfo[i].Processor = uint(v) | ||
| case "BogoMIPS": | ||
| v, err := strconv.ParseFloat(field[1], 64) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| cpuinfo[i].BogoMips = v | ||
| case "Features": | ||
| featuresLine = line | ||
| } | ||
| } | ||
| fields := strings.SplitN(featuresLine, ": ", 2) | ||
| for i := range cpuinfo { | ||
| cpuinfo[i].Flags = strings.Fields(fields[1]) | ||
| } | ||
| return cpuinfo, nil | ||
| } | ||
|
|
||
| func parseCPUInfoS390X(info []byte) ([]CPUInfo, error) { | ||
| scanner := bufio.NewScanner(bytes.NewReader(info)) | ||
|
|
||
| firstLine := firstNonEmptyLine(scanner) | ||
| if !strings.HasPrefix(firstLine, "vendor_id") || !strings.Contains(firstLine, ":") { | ||
| return nil, errors.New("invalid cpuinfo file: " + firstLine) | ||
| } | ||
| field := strings.SplitN(firstLine, ": ", 2) | ||
| cpuinfo := []CPUInfo{} | ||
| commonCPUInfo := CPUInfo{VendorID: field[1]} | ||
|
|
||
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
| if !strings.Contains(line, ":") { | ||
| continue | ||
| } | ||
| field := strings.SplitN(line, ": ", 2) | ||
| switch strings.TrimSpace(field[0]) { | ||
| case "bogomips per cpu": | ||
| v, err := strconv.ParseFloat(field[1], 64) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| commonCPUInfo.BogoMips = v | ||
| case "features": | ||
| commonCPUInfo.Flags = strings.Fields(field[1]) | ||
| } | ||
| if strings.HasPrefix(line, "processor") { | ||
| match := cpuinfoS390XProcessorRegexp.FindStringSubmatch(line) | ||
| if len(match) < 2 { | ||
| return nil, errors.New("Invalid line found in cpuinfo: " + line) | ||
| } | ||
| cpu := commonCPUInfo | ||
| v, err := strconv.ParseUint(match[1], 0, 32) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| cpu.Processor = uint(v) | ||
| cpuinfo = append(cpuinfo, cpu) | ||
| } | ||
| if strings.HasPrefix(line, "cpu number") { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| i := 0 | ||
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
| if !strings.Contains(line, ":") { | ||
| continue | ||
| } | ||
| field := strings.SplitN(line, ": ", 2) | ||
| switch strings.TrimSpace(field[0]) { | ||
| case "cpu number": | ||
| i++ | ||
| case "cpu MHz dynamic": | ||
| clock := cpuinfoClockRegexp.FindString(strings.TrimSpace(field[1])) | ||
| v, err := strconv.ParseFloat(clock, 64) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| cpuinfo[i].CPUMHz = v | ||
| } | ||
| } | ||
|
|
||
| return cpuinfo, nil | ||
| } | ||
|
|
||
| func parseCPUInfoPPC(info []byte) ([]CPUInfo, error) { | ||
| scanner := bufio.NewScanner(bytes.NewReader(info)) | ||
|
|
||
| firstLine := firstNonEmptyLine(scanner) | ||
| if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") { | ||
| return nil, errors.New("invalid cpuinfo file: " + firstLine) | ||
| } | ||
| field := strings.SplitN(firstLine, ": ", 2) | ||
| v, err := strconv.ParseUint(field[1], 0, 32) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| firstcpu := CPUInfo{Processor: uint(v)} | ||
| cpuinfo := []CPUInfo{firstcpu} | ||
| i := 0 | ||
|
|
||
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
| if !strings.Contains(line, ":") { | ||
| continue | ||
| } | ||
| field := strings.SplitN(line, ": ", 2) | ||
| switch strings.TrimSpace(field[0]) { | ||
| case "processor": | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't we want to parse the "clock" field in the cpu info for cpu MHz like for x86 ? |
||
| cpuinfo = append(cpuinfo, CPUInfo{}) // start of the next processor | ||
| i++ | ||
| v, err := strconv.ParseUint(field[1], 0, 32) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| cpuinfo[i].Processor = uint(v) | ||
| case "cpu": | ||
| cpuinfo[i].VendorID = field[1] | ||
| case "clock": | ||
| clock := cpuinfoClockRegexp.FindString(strings.TrimSpace(field[1])) | ||
| v, err := strconv.ParseFloat(clock, 64) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| cpuinfo[i].CPUMHz = v | ||
| } | ||
| } | ||
| return cpuinfo, nil | ||
| } | ||
|
|
||
| // firstNonEmptyLine advances the scanner to the first non-empty line | ||
| // and returns the contents of that line | ||
| func firstNonEmptyLine(scanner *bufio.Scanner) string { | ||
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
| if strings.TrimSpace(line) != "" { | ||
| return line | ||
| } | ||
| } | ||
| return "" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Copyright 2020 The Prometheus Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // +build linux | ||
| // +build amd64 | ||
|
|
||
| package procfs | ||
|
|
||
| var parseCPUInfo = parseCPUInfoX86 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Copyright 2020 The Prometheus Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // +build linux | ||
| // +build arm64 | ||
|
|
||
| package procfs | ||
|
|
||
| var parseCPUInfo = parseCPUInfoARM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Copyright 2020 The Prometheus Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // +build linux | ||
| // +build ppc64,ppc64le | ||
|
|
||
| package procfs | ||
|
|
||
| var parseCPUInfo = parseCPUInfoPPC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Copyright 2020 The Prometheus Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // +build linux | ||
| // +build s390x | ||
|
|
||
| package procfs | ||
|
|
||
| var parseCPUInfo = parseCPUInfoS390x |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.