Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 54b01cd3 authored by LaMont Jones's avatar LaMont Jones
Browse files

Add CPU and RAM information to metrics

Bug: b/365537477
Test: manual, TH
Change-Id: Ia76caaff2324f29b1e806915cab77bbb26df4ecb
parent 768b00f9
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ bootstrap_go_package {
        "blueprint-microfactory",
        "soong-elf",
        "soong-finder",
        "soong-finder-fs",
        "soong-remoteexec",
        "soong-shared",
        "soong-ui-build-paths",
+26 −1
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import (
	"syscall"
	"time"

	"android/soong/finder/fs"
	"android/soong/shared"
	"android/soong/ui/metrics"

@@ -107,6 +108,8 @@ type configImpl struct {

	// Autodetected
	totalRAM      uint64
	systemCpuInfo *metrics.CpuInfo
	systemMemInfo *metrics.MemInfo

	brokenDupRules       bool
	brokenUsesNetwork    bool
@@ -237,6 +240,14 @@ func NewConfig(ctx Context, args ...string) Config {
	ret.keepGoing = 1

	ret.totalRAM = detectTotalRAM(ctx)
	ret.systemCpuInfo, err = metrics.NewCpuInfo(fs.OsFs)
	if err != nil {
		ctx.Fatalln("Failed to get cpuinfo:", err)
	}
	ret.systemMemInfo, err = metrics.NewMemInfo(fs.OsFs)
	if err != nil {
		ctx.Fatalln("Failed to get meminfo:", err)
	}
	ret.parseArgs(ctx, args)

	if ret.ninjaWeightListSource == HINT_FROM_SOONG {
@@ -550,9 +561,23 @@ func storeConfigMetrics(ctx Context, config Config) {

	ctx.Metrics.BuildConfig(buildConfig(config))

	cpuInfo := &smpb.SystemCpuInfo{
		VendorId:  proto.String(config.systemCpuInfo.VendorId),
		ModelName: proto.String(config.systemCpuInfo.ModelName),
		CpuCores:  proto.Int32(config.systemCpuInfo.CpuCores),
		Flags:     proto.String(config.systemCpuInfo.Flags),
	}
	memInfo := &smpb.SystemMemInfo{
		MemTotal:     proto.Uint64(config.systemMemInfo.MemTotal),
		MemFree:      proto.Uint64(config.systemMemInfo.MemFree),
		MemAvailable: proto.Uint64(config.systemMemInfo.MemAvailable),
	}

	s := &smpb.SystemResourceInfo{
		TotalPhysicalMemory: proto.Uint64(config.TotalRAM()),
		AvailableCpus:       proto.Int32(int32(runtime.NumCPU())),
		CpuInfo:             cpuInfo,
		MemInfo:             memInfo,
	}
	ctx.Metrics.SystemResourceInfo(s)
}
+15 −0
Original line number Diff line number Diff line
@@ -21,18 +21,33 @@ bootstrap_go_package {
    pkgPath: "android/soong/ui/metrics",
    deps: [
        "golang-protobuf-proto",
        "soong-finder-fs",
        "soong-ui-metrics_upload_proto",
        "soong-ui-metrics_proto",
        "soong-ui-mk_metrics_proto",
        "soong-shared",
    ],
    srcs: [
        "hostinfo.go",
        "metrics.go",
        "event.go",
    ],
    testSrcs: [
        "event_test.go",
    ],
    linux: {
        srcs: [
            "hostinfo_linux.go",
        ],
        testSrcs: [
            "hostinfo_linux_test.go",
        ],
    },
    darwin: {
        srcs: [
            "hostinfo_darwin.go",
        ],
    },
}

bootstrap_go_package {

ui/metrics/hostinfo.go

0 → 100644
+111 −0
Original line number Diff line number Diff line
// Copyright 2024 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 metrics

// The hostinfo* files contain code to extract host information from
// /proc/cpuinfo and /proc/meminfo relevant to machine performance

import (
	"strconv"
	"strings"
)

// CpuInfo holds information regarding the host's CPU cores.
type CpuInfo struct {
	// The vendor id
	VendorId string

	// The model name
	ModelName string

	// The number of CPU cores
	CpuCores int32

	// The CPU flags
	Flags string
}

// MemInfo holds information regarding the host's memory.
// The memory size in each of the field is in bytes.
type MemInfo struct {
	// The total memory.
	MemTotal uint64

	// The amount of free memory.
	MemFree uint64

	// The amount of available memory.
	MemAvailable uint64
}

// fillCpuInfo takes the key and value, converts the value
// to the proper size unit and is stores it in CpuInfo.
func (c *CpuInfo) fillInfo(key, value string) {
	switch key {
	case "vendor_id":
		c.VendorId = value
	case "model name":
		c.ModelName = value
	case "cpu cores":
		v, err := strconv.ParseInt(value, 10, 32)
		if err == nil {
			c.CpuCores = int32(v)
		}
	case "flags":
		c.Flags = value
	default:
		// Ignore unknown keys
	}
}

// fillCpuInfo takes the key and value, converts the value
// to the proper size unit and is stores it in CpuInfo.
func (m *MemInfo) fillInfo(key, value string) {
	v := strToUint64(value)
	switch key {
	case "MemTotal":
		m.MemTotal = v
	case "MemFree":
		m.MemFree = v
	case "MemAvailable":
		m.MemAvailable = v
	default:
		// Ignore unknown keys
	}
}

// strToUint64 takes the string and converts to unsigned 64-bit integer.
// If the string contains a memory unit such as kB and is converted to
// bytes.
func strToUint64(v string) uint64 {
	// v could be "1024 kB" so scan for the empty space and
	// split between the value and the unit.
	var separatorIndex int
	if separatorIndex = strings.IndexAny(v, " "); separatorIndex < 0 {
		separatorIndex = len(v)
	}
	value, err := strconv.ParseUint(v[:separatorIndex], 10, 64)
	if err != nil {
		return 0
	}

	var scale uint64 = 1
	switch strings.TrimSpace(v[separatorIndex:]) {
	case "kB", "KB":
		scale = 1024
	case "mB", "MB":
		scale = 1024 * 1024
	}
	return value * scale
}
+27 −0
Original line number Diff line number Diff line
// Copyright 2024 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 metrics

// This file contain code to extract host information on linux from
// /proc/cpuinfo and /proc/meminfo relevant to machine performance

import ()

func NewCpuInfo(fileSystem fs.FileSystem) (*CpuInfo, error) {
	return &CpuInfo{}, nil
}

func NewMemInfo(fileSystem fs.FileSystem) (*MemInfo, error) {
	return &MemInfo{}, nil
}
Loading