Skip to content

Commit

Permalink
simplify loadavg reading, only available on linux
Browse files Browse the repository at this point in the history
Signed-off-by: Shlomi Noach <[email protected]>
  • Loading branch information
shlomi-noach committed Jul 29, 2024
1 parent a23b15f commit 66e0e83
Showing 1 changed file with 12 additions and 33 deletions.
45 changes: 12 additions & 33 deletions go/vt/vttablet/tabletserver/throttle/base/self_metric_loadavg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,26 @@ limitations under the License.
package base

import (
"bufio"
"context"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"sync/atomic"

"vitess.io/vitess/go/vt/vttablet/tabletserver/connpool"
)

var (
loadavgOnlyAvailableOnLinuxMetric = &ThrottleMetric{
Scope: SelfScope,
Err: fmt.Errorf("loadavg metric is only available on Linux"),
}
)

var _ SelfMetric = registerSelfMetric(&LoadAvgSelfMetric{})

type LoadAvgSelfMetric struct {
hostCpuCoreCount atomic.Int32
}

func (m *LoadAvgSelfMetric) Name() MetricName {
Expand All @@ -51,38 +56,12 @@ func (m *LoadAvgSelfMetric) RequiresConn() bool {
}

func (m *LoadAvgSelfMetric) Read(ctx context.Context, throttler ThrottlerMetricsPublisher, conn *connpool.Conn) *ThrottleMetric {
if runtime.GOOS != "linux" {
return loadavgOnlyAvailableOnLinuxMetric
}
metric := &ThrottleMetric{
Scope: SelfScope,
}

coreCount := m.hostCpuCoreCount.Load()
if coreCount == 0 {
// Count cores. This number is not going to change in the lifetime of this tablet,
// hence it makes sense to read it once then cache it.

// We choose to read /proc/cpuinfo over executing "nproc" or similar commands.
var coreCount int32
f, err := os.Open("/proc/cpuinfo")
if err != nil {
return metric.WithError(err)
}
defer f.Close()

scanner := bufio.NewScanner(f)
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "processor") {
coreCount++
}
}

if err := scanner.Err(); err != nil {
return metric.WithError(err)
}
m.hostCpuCoreCount.Store(coreCount)
}
if coreCount == 0 {
return metric.WithError(fmt.Errorf("could not determine number of cores"))
}
{
content, err := os.ReadFile("/proc/loadavg")
if err != nil {
Expand All @@ -96,7 +75,7 @@ func (m *LoadAvgSelfMetric) Read(ctx context.Context, throttler ThrottlerMetrics
if err != nil {
return metric.WithError(err)
}
metric.Value = loadAvg / float64(m.hostCpuCoreCount.Load())
metric.Value = loadAvg / float64(runtime.NumCPU())
}
return metric
}

0 comments on commit 66e0e83

Please sign in to comment.