Fix collection of stale soong build metrics

Bug: b/256037411
Test: run mixed build twice and ensure soong_build_metric.pb file is absent on second run.
Change-Id: Ia11d850e1ad318e6383d8b392f20420259bc2f4d
This commit is contained in:
usta
2022-10-27 23:01:02 -04:00
parent 9b019134c2
commit 7f56eaa748
2 changed files with 18 additions and 11 deletions

View File

@@ -532,17 +532,22 @@ func runSoong(ctx Context, config Config) {
targets = append(targets, config.SoongNinjaFile())
}
ninja("bootstrap", "bootstrap.ninja", targets...)
if shouldCollectBuildSoongMetrics(config) {
soongBuildMetrics := loadSoongBuildMetrics(ctx, config)
if soongBuildMetrics != nil {
logSoongBuildMetrics(ctx, soongBuildMetrics)
if ctx.Metrics != nil {
ctx.Metrics.SetSoongBuildMetrics(soongBuildMetrics)
}
soongBuildMetricsFile := filepath.Join(config.LogsDir(), "soong_build_metrics.pb")
if err := os.Remove(soongBuildMetricsFile); err != nil && !os.IsNotExist(err) {
ctx.Verbosef("Failed to remove %s", soongBuildMetricsFile)
}
defer func() {
soongBuildMetrics := loadSoongBuildMetrics(ctx, soongBuildMetricsFile)
if soongBuildMetrics != nil {
logSoongBuildMetrics(ctx, soongBuildMetrics)
if ctx.Metrics != nil {
ctx.Metrics.SetSoongBuildMetrics(soongBuildMetrics)
}
}
}()
}
ninja("bootstrap", "bootstrap.ninja", targets...)
distGzipFile(ctx, config, config.SoongNinjaFile(), "soong")
distFile(ctx, config, config.SoongVarsFile(), "soong")
@@ -581,8 +586,7 @@ func shouldCollectBuildSoongMetrics(config Config) bool {
return config.SoongBuildInvocationNeeded()
}
func loadSoongBuildMetrics(ctx Context, config Config) *soong_metrics_proto.SoongBuildMetrics {
soongBuildMetricsFile := filepath.Join(config.LogsDir(), "soong_build_metrics.pb")
func loadSoongBuildMetrics(ctx Context, soongBuildMetricsFile string) *soong_metrics_proto.SoongBuildMetrics {
buf, err := os.ReadFile(soongBuildMetricsFile)
if errors.Is(err, fs.ErrNotExist) {
// Soong may not have run during this invocation

View File

@@ -18,6 +18,7 @@ package build
// another.
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
@@ -56,7 +57,9 @@ func pruneMetricsFiles(paths []string) []string {
}
if fi.IsDir() {
if l, err := ioutil.ReadDir(p); err == nil {
if l, err := ioutil.ReadDir(p); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Failed to find files under %s\n", p)
} else {
files := make([]string, 0, len(l))
for _, fi := range l {
files = append(files, filepath.Join(p, fi.Name()))