Merge pull request #1570 from scop/perf/unnecessary-regexps

Avoid some uses of regexps
pull/1573/head
shirou 1 year ago committed by GitHub
commit 08afc01d17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,15 +5,12 @@ package cpu
import (
"context"
"regexp"
"strconv"
"strings"
"github.com/shirou/gopsutil/v3/internal/common"
)
var whiteSpaces = regexp.MustCompile(`\s+`)
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
if percpu {
return []TimesStat{}, common.ErrNotImplementedError
@ -28,8 +25,8 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
}
ret := TimesStat{CPU: "cpu-total"}
h := whiteSpaces.Split(lines[len(lines)-3], -1) // headers
v := whiteSpaces.Split(lines[len(lines)-2], -1) // values
h := strings.Fields(lines[len(lines)-3]) // headers
v := strings.Fields(lines[len(lines)-2]) // values
for i, header := range h {
if t, err := strconv.ParseFloat(v[i], 64); err == nil {
switch header {
@ -58,14 +55,14 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
ret := InfoStat{}
for _, line := range strings.Split(string(out), "\n") {
if strings.HasPrefix(line, "Number Of Processors:") {
p := whiteSpaces.Split(line, 4)
p := strings.Fields(line)
if len(p) > 3 {
if t, err := strconv.ParseUint(p[3], 10, 64); err == nil {
ret.Cores = int32(t)
}
}
} else if strings.HasPrefix(line, "Processor Clock Speed:") {
p := whiteSpaces.Split(line, 5)
p := strings.Fields(line)
if len(p) > 4 {
if t, err := strconv.ParseFloat(p[3], 64); err == nil {
switch strings.ToUpper(p[4]) {

@ -12,7 +12,6 @@ import (
"golang.org/x/sys/unix"
)
var whiteSpaces = regexp.MustCompile(`\s+`)
var startBlank = regexp.MustCompile(`^\s+`)
var ignoreFSType = map[string]bool{"procfs": true}
@ -60,7 +59,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
if startBlank.MatchString(line) {
line = "localhost" + line
}
p := whiteSpaces.Split(lines[idx], 6)
p := strings.Fields(lines[idx])
if len(p) < 5 || ignoreFSType[p[colidx["vfs"]]] {
continue
}

@ -5,15 +5,12 @@ package mem
import (
"context"
"regexp"
"strconv"
"strings"
"github.com/shirou/gopsutil/v3/internal/common"
)
var whiteSpaces = regexp.MustCompile(`\s+`)
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
vmem, swap, err := callSVMon(ctx)
if err != nil {
@ -49,7 +46,7 @@ func callSVMon(ctx context.Context) (*VirtualMemoryStat, *SwapMemoryStat, error)
swap := &SwapMemoryStat{}
for _, line := range strings.Split(string(out), "\n") {
if strings.HasPrefix(line, "memory") {
p := whiteSpaces.Split(line, 7)
p := strings.Fields(line)
if len(p) > 2 {
if t, err := strconv.ParseUint(p[1], 10, 64); err == nil {
vmem.Total = t * pagesize
@ -65,7 +62,7 @@ func callSVMon(ctx context.Context) (*VirtualMemoryStat, *SwapMemoryStat, error)
}
}
} else if strings.HasPrefix(line, "pg space") {
p := whiteSpaces.Split(line, 4)
p := strings.Fields(line)
if len(p) > 3 {
if t, err := strconv.ParseUint(p[2], 10, 64); err == nil {
swap.Total = t * pagesize

Loading…
Cancel
Save