Skip to content
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

Avoid repeated regexp compilations #1569

Merged
merged 1 commit into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cpu/cpu_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func Times(percpu bool) ([]TimesStat, error) {
return TimesWithContext(context.Background(), percpu)
}

var kstatSplit = regexp.MustCompile(`[:\s]+`)

func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
kstatSysOut, err := invoke.CommandWithContext(ctx, "kstat", "-p", "cpu_stat:*:*:/^idle$|^user$|^kernel$|^iowait$|^swap$/")
if err != nil {
Expand All @@ -47,9 +49,8 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
kern := make(map[float64]float64)
iowt := make(map[float64]float64)
// swap := make(map[float64]float64)
re := regexp.MustCompile(`[:\s]+`)
for _, line := range strings.Split(string(kstatSysOut), "\n") {
fields := re.Split(line, -1)
fields := kstatSplit.Split(line, -1)
if fields[0] != "cpu_stat" {
continue
}
Expand Down
5 changes: 3 additions & 2 deletions disk/disk_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
return ret, err
}

var kstatSplit = regexp.MustCompile(`[:\s]+`)

func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) {
var issolaris bool
if runtime.GOOS == "illumos" {
Expand All @@ -107,15 +109,14 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
writesarr := make(map[string]uint64)
rtimearr := make(map[string]uint64)
wtimearr := make(map[string]uint64)
re := regexp.MustCompile(`[:\s]+`)

// in case the name is "/dev/sda1", then convert to "sda1"
for i, name := range names {
names[i] = filepath.Base(name)
}

for _, line := range lines {
fields := re.Split(line, -1)
fields := kstatSplit.Split(line, -1)
if len(fields) == 0 {
continue
}
Expand Down
13 changes: 10 additions & 3 deletions host/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,15 @@ func getSlackwareVersion(contents []string) string {
return c
}

var redhatishReleaseMatch = regexp.MustCompile(`release (\w[\d.]*)`)

func getRedhatishVersion(contents []string) string {
c := strings.ToLower(strings.Join(contents, ""))

if strings.Contains(c, "rawhide") {
return "rawhide"
}
if matches := regexp.MustCompile(`release (\w[\d.]*)`).FindStringSubmatch(c); matches != nil {
if matches := redhatishReleaseMatch.FindStringSubmatch(c); matches != nil {
return matches[1]
}
return ""
Expand All @@ -362,12 +364,17 @@ func getRedhatishPlatform(contents []string) string {
return f[0]
}

var (
suseVersionMatch = regexp.MustCompile(`VERSION = ([\d.]+)`)
susePatchLevelMatch = regexp.MustCompile(`PATCHLEVEL = (\d+)`)
)

func getSuseVersion(contents []string) string {
version := ""
for _, line := range contents {
if matches := regexp.MustCompile(`VERSION = ([\d.]+)`).FindStringSubmatch(line); matches != nil {
if matches := suseVersionMatch.FindStringSubmatch(line); matches != nil {
version = matches[1]
} else if matches := regexp.MustCompile(`PATCHLEVEL = ([\d]+)`).FindStringSubmatch(line); matches != nil {
} else if matches = susePatchLevelMatch.FindStringSubmatch(line); matches != nil {
version = version + "." + matches[1]
}
}
Expand Down
5 changes: 3 additions & 2 deletions net/net_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func IOCounters(pernic bool) ([]IOCountersStat, error) {
return IOCountersWithContext(context.Background(), pernic)
}

var kstatSplit = regexp.MustCompile(`[:\s]+`)

func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
// collect all the net class's links with below statistics
filterstr := "/^(?!vnic)/::phys:/^rbytes64$|^ipackets64$|^idrops64$|^ierrors$|^obytes64$|^opackets64$|^odrops64$|^oerrors$/"
Expand All @@ -47,9 +49,8 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
odrops64arr := make(map[string]uint64)
oerrorsarr := make(map[string]uint64)

re := regexp.MustCompile(`[:\s]+`)
for _, line := range lines {
fields := re.Split(line, -1)
fields := kstatSplit.Split(line, -1)
interfaceName := fields[0]
instance := fields[1]
switch fields[3] {
Expand Down
Loading