Skip to content

Commit

Permalink
Handling arm properly: with build tags
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej "Iwan" Iwanowski <maciej.iwanowski@critical.today>
  • Loading branch information
iwankgb committed Dec 3, 2020
1 parent 879ab86 commit 4152655
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 63 deletions.
40 changes: 2 additions & 38 deletions machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@
package machine

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"

// s390/s390x changes
"runtime"
"strconv"
"strings"

info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/utils"
Expand Down Expand Up @@ -236,39 +233,6 @@ func parseCapacity(b []byte, r *regexp.Regexp) (uint64, error) {
return m * 1024, err
}

// Looks for sysfs cpu path containing given CPU property, e.g. core_id or physical_package_id
// and returns number of unique values of given property, exemplary usage: getting number of CPU physical cores
func getUniqueCPUPropertyCount(cpuBusPath string, propertyName string) int {
pathPattern := cpuBusPath + "cpu*[0-9]"
sysCPUPaths, err := filepath.Glob(pathPattern)
if err != nil {
klog.Errorf("Cannot find files matching pattern (pathPattern: %s), number of unique %s set to 0", pathPattern, propertyName)
return 0
}
uniques := make(map[string]bool)
for _, sysCPUPath := range sysCPUPaths {
onlinePath := filepath.Join(sysCPUPath, "online")
onlineVal, err := ioutil.ReadFile(onlinePath)
if err != nil {
klog.Warningf("Cannot determine CPU %s online state, skipping", sysCPUPath)
continue
}
onlineVal = bytes.TrimSpace(onlineVal)
if len(onlineVal) == 0 || onlineVal[0] != 49 {
klog.Warningf("CPU %s is offline, skipping", sysCPUPath)
continue
}
propertyPath := filepath.Join(sysCPUPath, sysFsCPUTopology, propertyName)
propertyVal, err := ioutil.ReadFile(propertyPath)
if err != nil {
klog.Errorf("Cannot open %s, number of unique %s set to 0", propertyPath, propertyName)
return 0
}
uniques[string(propertyVal)] = true
}
return len(uniques)
}

// getUniqueMatchesCount returns number of unique matches in given argument using provided regular expression
func getUniqueMatchesCount(s string, r *regexp.Regexp) int {
matches := r.FindAllString(s, -1)
Expand Down
44 changes: 44 additions & 0 deletions machine/machine_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2020 Google Inc. All Rights Reserved.
//
// 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.

package machine

import (
"io/ioutil"
"path/filepath"

"k8s.io/klog/v2"
)

// Looks for sysfs cpu path containing given CPU property, e.g. core_id or physical_package_id
// and returns number of unique values of given property, exemplary usage: getting number of CPU physical cores
func getUniqueCPUPropertyCount(cpuBusPath string, propertyName string) int {
pathPattern := cpuBusPath + "cpu*[0-9]"
sysCPUPaths, err := filepath.Glob(pathPattern)
if err != nil {
klog.Errorf("Cannot find files matching pattern (pathPattern: %s), number of unique %s set to 0", pathPattern, propertyName)
return 0
}
uniques := make(map[string]bool)
for _, sysCPUPath := range sysCPUPaths {
propertyPath := filepath.Join(sysCPUPath, sysFsCPUTopology, propertyName)
propertyVal, err := ioutil.ReadFile(propertyPath)
if err != nil {
klog.Errorf("Cannot open %s, number of unique %s set to 0", propertyPath, propertyName)
return 0
}
uniques[string(propertyVal)] = true
}
return len(uniques)
}
58 changes: 58 additions & 0 deletions machine/machine_notarm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// +build !arm64

// Copyright 2020 Google Inc. All Rights Reserved.
//
// 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.

package machine

import (
"bytes"
"io/ioutil"
"path/filepath"

"k8s.io/klog/v2"
)

// Looks for sysfs cpu path containing given CPU property, e.g. core_id or physical_package_id
// and returns number of unique values of given property, exemplary usage: getting number of CPU physical cores
func getUniqueCPUPropertyCount(cpuBusPath string, propertyName string) int {
pathPattern := cpuBusPath + "cpu*[0-9]"
sysCPUPaths, err := filepath.Glob(pathPattern)
if err != nil {
klog.Errorf("Cannot find files matching pattern (pathPattern: %s), number of unique %s set to 0", pathPattern, propertyName)
return 0
}
uniques := make(map[string]bool)
for _, sysCPUPath := range sysCPUPaths {
onlinePath := filepath.Join(sysCPUPath, "online")
onlineVal, err := ioutil.ReadFile(onlinePath)
if err != nil {
klog.Warningf("Cannot determine CPU %s online state, skipping", sysCPUPath)
continue
}
onlineVal = bytes.TrimSpace(onlineVal)
if len(onlineVal) == 0 || onlineVal[0] != 49 {
klog.Warningf("CPU %s is offline, skipping", sysCPUPath)
continue
}
propertyPath := filepath.Join(sysCPUPath, sysFsCPUTopology, propertyName)
propertyVal, err := ioutil.ReadFile(propertyPath)
if err != nil {
klog.Errorf("Cannot open %s, number of unique %s set to 0", propertyPath, propertyName)
return 0
}
uniques[string(propertyVal)] = true
}
return len(uniques)
}
26 changes: 1 addition & 25 deletions utils/sysfs/sysfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@
package sysfs

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"

"k8s.io/klog/v2"
)

const (
Expand Down Expand Up @@ -336,24 +331,5 @@ func (fs *realSysFs) GetSystemUUID() (string, error) {
}

func (fs *realSysFs) IsCPUOnline(dir string) bool {
cpuPath := fmt.Sprintf("%s/online", dir)
content, err := ioutil.ReadFile(cpuPath)
if err != nil {
pathErr, ok := err.(*os.PathError)
if ok {
if errors.Is(pathErr.Unwrap(), os.ErrNotExist) && isZeroCPU(dir) {
return true
}
}
klog.Warningf("unable to read %s: %s", cpuPath, err.Error())
return false
}
trimmed := bytes.TrimSpace(content)
return len(trimmed) == 1 && trimmed[0] == 49
}

func isZeroCPU(dir string) bool {
regex := regexp.MustCompile("cpu([0-9]*)")
matches := regex.FindStringSubmatch(dir)
return len(matches) == 2 && matches[1] == "0"
return isCPUOnline(dir)
}
19 changes: 19 additions & 0 deletions utils/sysfs/sysfs_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2020 Google Inc. All Rights Reserved.
//
// 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.

package sysfs

func isCPUOnline(_ string) bool {
return true
}
51 changes: 51 additions & 0 deletions utils/sysfs/sysfs_notarm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// +build !arm64

// Copyright 2020 Google Inc. All Rights Reserved.
//
// 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.

package sysfs

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"regexp"

"k8s.io/klog/v2"
)

func isCPUOnline(dir string) bool {
cpuPath := fmt.Sprintf("%s/online", dir)
content, err := ioutil.ReadFile(cpuPath)
if err != nil {
pathErr, ok := err.(*os.PathError)
if ok {
if errors.Is(pathErr.Unwrap(), os.ErrNotExist) && isZeroCPU(dir) {
return true
}
}
klog.Warningf("unable to read %s: %s", cpuPath, err.Error())
return false
}
trimmed := bytes.TrimSpace(content)
return len(trimmed) == 1 && trimmed[0] == 49
}

func isZeroCPU(dir string) bool {
regex := regexp.MustCompile("cpu([0-9]*)")
matches := regex.FindStringSubmatch(dir)
return len(matches) == 2 && matches[1] == "0"
}

0 comments on commit 4152655

Please sign in to comment.