Add AutoProfiler-like API to Profiler

- migrate all startTask/completeTask pairs to the new API

PiperOrigin-RevId: 200038703
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BuildSummaryStatsModule.java b/src/main/java/com/google/devtools/build/lib/runtime/BuildSummaryStatsModule.java
index 4d2cdce..86f1147 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BuildSummaryStatsModule.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BuildSummaryStatsModule.java
@@ -30,6 +30,7 @@
 import com.google.devtools.build.lib.exec.ExecutorBuilder;
 import com.google.devtools.build.lib.profiler.Profiler;
 import com.google.devtools.build.lib.profiler.ProfilerTask;
+import com.google.devtools.build.lib.profiler.SilentCloseable;
 import com.google.devtools.build.lib.util.Pair;
 import com.google.protobuf.ByteString;
 import java.time.Duration;
@@ -101,27 +102,29 @@
           String.format("%f", event.getResult().getElapsedSeconds()))));
 
       if (criticalPathComputer != null) {
-        Profiler.instance().startTask(ProfilerTask.CRITICAL_PATH, "Critical path");
-        AggregatedCriticalPath<SimpleCriticalPathComponent> criticalPath =
-            criticalPathComputer.aggregate();
-        items.add(criticalPath.toStringSummary());
-        statistics.add(Pair.of("critical path", ByteString.copyFromUtf8(criticalPath.toString())));
-        logger.info(criticalPath.toString());
-        logger.info(
-            "Slowest actions:\n  "
-                + Joiner.on("\n  ").join(criticalPathComputer.getSlowestComponents()));
-        // We reverse the critical path because the profiler expect events ordered by the time
-        // when the actions were executed while critical path computation is stored in the reverse
-        // way.
-        for (SimpleCriticalPathComponent stat : criticalPath.components().reverse()) {
-          Profiler.instance()
-              .logSimpleTaskDuration(
-                  stat.getStartNanos(),
-                  Duration.ofNanos(stat.getElapsedTimeNanos()),
-                  ProfilerTask.CRITICAL_PATH_COMPONENT,
-                  stat.prettyPrintAction());
+        try (SilentCloseable c =
+            Profiler.instance().profile(ProfilerTask.CRITICAL_PATH, "Critical path")) {
+          AggregatedCriticalPath<SimpleCriticalPathComponent> criticalPath =
+              criticalPathComputer.aggregate();
+          items.add(criticalPath.toStringSummary());
+          statistics.add(
+              Pair.of("critical path", ByteString.copyFromUtf8(criticalPath.toString())));
+          logger.info(criticalPath.toString());
+          logger.info(
+              "Slowest actions:\n  "
+                  + Joiner.on("\n  ").join(criticalPathComputer.getSlowestComponents()));
+          // We reverse the critical path because the profiler expect events ordered by the time
+          // when the actions were executed while critical path computation is stored in the reverse
+          // way.
+          for (SimpleCriticalPathComponent stat : criticalPath.components().reverse()) {
+            Profiler.instance()
+                .logSimpleTaskDuration(
+                    stat.getStartNanos(),
+                    Duration.ofNanos(stat.getElapsedTimeNanos()),
+                    ProfilerTask.CRITICAL_PATH_COMPONENT,
+                    stat.prettyPrintAction());
+          }
         }
-        Profiler.instance().completeTask(ProfilerTask.CRITICAL_PATH);
       }
 
       reporter.handle(Event.info(Joiner.on(", ").join(items)));