Skip to content

Commit

Permalink
OS-specific build files
Browse files Browse the repository at this point in the history
Signed-off-by: Shlomi Noach <[email protected]>
  • Loading branch information
shlomi-noach committed Nov 12, 2024
1 parent b3f3199 commit 8adc7eb
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 30 deletions.
30 changes: 0 additions & 30 deletions go/osutil/os.go → go/osutil/loadavg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,10 @@ package osutil

import (
"fmt"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
)

// LoadAvg returns the past 1 minute system load average. This works on linux and darwin systems.
// On other systems, it returns 0 with no error.
func LoadAvg() (float64, error) {
switch runtime.GOOS {
case "linux":
content, err := os.ReadFile("/proc/loadavg")
if err != nil {
return 0, err
}
return parseLoadAvg(string(content))
case "darwin":
cmd := exec.Command("sysctl", "-n", "vm.loadavg")
// Sample output: `{ 2.83 3.01 3.36 }`
output, err := cmd.CombinedOutput()
if err != nil {
return 0, err
}
if len(output) < 1 {
return 0, fmt.Errorf("unexpected sysctl output: %q", output)
}
output = output[1:] // Remove the leading `{ `
return parseLoadAvg(string(output))
default:
return 0, nil
}
}

// parseLoadAvg parses the load average from the content of /proc/loadavg or sysctl output.
// Input such as "1.00 0.99 0.98 1/1 1", "2.83 3.01 3.36"
func parseLoadAvg(content string) (float64, error) {
Expand Down
40 changes: 40 additions & 0 deletions go/osutil/loadavg_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//go:build darwin

/*
Copyright 2024 The Vitess 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.
*/

package osutil

import (
"fmt"
"os/exec"
)

// LoadAvg returns the past 1 minute system load average. This works on linux and darwin systems.
// On other systems, it returns 0 with no error.
func LoadAvg() (float64, error) {
cmd := exec.Command("sysctl", "-n", "vm.loadavg")
// Sample output: `{ 2.83 3.01 3.36 }`
output, err := cmd.CombinedOutput()
if err != nil {
return 0, err
}
if len(output) < 1 {
return 0, fmt.Errorf("unexpected sysctl output: %q", output)
}
output = output[1:] // Remove the leading `{ `
return parseLoadAvg(string(output))
}
33 changes: 33 additions & 0 deletions go/osutil/loadavg_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build linux

/*
Copyright 2024 The Vitess 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.
*/

package osutil

import (
"os"
)

// LoadAvg returns the past 1 minute system load average. This works on linux and darwin systems.
// On other systems, it returns 0 with no error.
func LoadAvg() (float64, error) {
content, err := os.ReadFile("/proc/loadavg")
if err != nil {
return 0, err
}
return parseLoadAvg(string(content))
}
25 changes: 25 additions & 0 deletions go/osutil/loadavg_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build !linux && !darwin

/*
Copyright 2024 The Vitess 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.
*/

package osutil

// LoadAvg returns the past 1 minute system load average. This works on linux and darwin systems.
// On other systems, it returns 0 with no error.
func LoadAvg() (float64, error) {
return 0, nil
}
File renamed without changes.

0 comments on commit 8adc7eb

Please sign in to comment.