Fix some problems with soong metrics loading

If we didn't need to run soong_build during the current run, we still
try to load the soong metrics. But in the case of `dist`, that's in a
directory that is not guaranteed to persist between runs.

Make loading the soong metrics optional if the file does not exist. Also
fixes a variable shadowing issue that meant we never passed it into
ctx.Metrics.

Test: treehugger
Change-Id: Ic836282f4d13e91daa0e7241ad7c488de3293d8b
(cherry picked from commit de3604453f)
Merged-In: Ic836282f4d13e91daa0e7241ad7c488de3293d8b
This commit is contained in:
Dan Willemsen
2022-04-20 21:21:55 -07:00
committed by Cherrypicker Worker
parent 224fbdb596
commit 71b2daf9da

View File

@@ -15,7 +15,9 @@
package build
import (
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
@@ -491,10 +493,14 @@ func runSoong(ctx Context, config Config) {
ninja("bootstrap", "bootstrap.ninja", targets...)
var soongBuildMetrics *soong_metrics_proto.SoongBuildMetrics
if shouldCollectBuildSoongMetrics(config) {
soongBuildMetrics := loadSoongBuildMetrics(ctx, config)
logSoongBuildMetrics(ctx, soongBuildMetrics)
if soongBuildMetrics != nil {
logSoongBuildMetrics(ctx, soongBuildMetrics)
if ctx.Metrics != nil {
ctx.Metrics.SetSoongBuildMetrics(soongBuildMetrics)
}
}
}
distGzipFile(ctx, config, config.SoongNinjaFile(), "soong")
@@ -504,9 +510,6 @@ func runSoong(ctx Context, config Config) {
distGzipFile(ctx, config, config.SoongMakeVarsMk(), "soong")
}
if shouldCollectBuildSoongMetrics(config) && ctx.Metrics != nil {
ctx.Metrics.SetSoongBuildMetrics(soongBuildMetrics)
}
if config.JsonModuleGraph() {
distGzipFile(ctx, config, config.ModuleGraphFile(), "soong")
}
@@ -538,8 +541,12 @@ func shouldCollectBuildSoongMetrics(config Config) bool {
func loadSoongBuildMetrics(ctx Context, config Config) *soong_metrics_proto.SoongBuildMetrics {
soongBuildMetricsFile := filepath.Join(config.LogsDir(), "soong_build_metrics.pb")
buf, err := ioutil.ReadFile(soongBuildMetricsFile)
if err != nil {
buf, err := os.ReadFile(soongBuildMetricsFile)
if errors.Is(err, fs.ErrNotExist) {
// Soong may not have run during this invocation
ctx.Verbosef("Failed to read metrics file, %s: %s", soongBuildMetricsFile, err)
return nil
} else if err != nil {
ctx.Fatalf("Failed to load %s: %s", soongBuildMetricsFile, err)
}
soongBuildMetrics := &soong_metrics_proto.SoongBuildMetrics{}