Move commandStartTime from BlazeRuntime to CommandEnvironment.

--
MOS_MIGRATED_REVID=105840775
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java b/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java
index cc3c804..bf018f1 100644
--- a/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java
@@ -440,7 +440,7 @@
       interrupted = true;
       throw e;
     } finally {
-      runtime.recordLastExecutionTime();
+      env.recordLastExecutionTime();
       if (request.isRunningInEmacs()) {
         request.getOutErr().printErrLn("blaze: Leaving directory `" + getExecRoot() + "/'");
       }
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java
index 784b0e1..100a075 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java
@@ -212,9 +212,6 @@
     // anything before this!
     long execStartTimeNanos = runtime.getClock().nanoTime();
 
-    // Record the command's starting time for use by the commands themselves.
-    runtime.recordCommandStartTime(firstContactTime);
-
     // Record the command's starting time again, for use by
     // TimestampGranularityMonitor.waitForTimestampGranularity().
     // This should be done as close as possible to the start of
@@ -222,6 +219,8 @@
     // rather than in runtime.beforeCommand().
     runtime.getTimestampGranularityMonitor().setCommandStartTime();
     CommandEnvironment env = runtime.initCommand();
+    // Record the command's starting time for use by the commands themselves.
+    env.recordCommandStartTime(firstContactTime);
 
     if (args.isEmpty()) { // Default to help command if no arguments specified.
       args = HELP_COMMAND;
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java
index a3074e5..ebfd4bf 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java
@@ -156,10 +156,10 @@
   private static final Logger LOG = Logger.getLogger(BlazeRuntime.class.getName());
 
   private final BlazeDirectories directories;
-  private long commandStartTime;
 
+  /** The execution time range of the previous build command in this server, if any. */
   @Nullable
-  private Range<Long> lastExecutionStartFinish = null;
+  private Range<Long> lastExecutionRange = null;
 
   private final SkyframeExecutor skyframeExecutor;
 
@@ -374,9 +374,9 @@
     }
   }
 
-  public void recordLastExecutionTime() {
+  void recordLastExecutionTime(long commandStartTime) {
     long currentTimeMillis = clock.currentTimeMillis();
-    lastExecutionStartFinish = currentTimeMillis >= commandStartTime
+    lastExecutionRange = currentTimeMillis >= commandStartTime
         ? Range.closed(commandStartTime, currentTimeMillis)
         : null;
   }
@@ -386,15 +386,7 @@
    */
   @Nullable
   public Range<Long> getLastExecutionTimeRange() {
-    return lastExecutionStartFinish;
-  }
-
-  public void recordCommandStartTime(long commandStartTime) {
-    this.commandStartTime = commandStartTime;
-  }
-
-  public long getCommandStartTime() {
-    return commandStartTime;
+    return lastExecutionRange;
   }
 
   public String getWorkspaceName() {
@@ -561,16 +553,11 @@
    * Removes in-memory caches.
    */
   public void clearCaches() throws IOException {
-    clearSkyframeRelevantCaches();
+    skyframeExecutor.resetEvaluator();
     actionCache = null;
     FileSystemUtils.deleteTree(getCacheDirectory());
   }
 
-  /** Removes skyframe cache and other caches that must be kept synchronized with skyframe. */
-  private void clearSkyframeRelevantCaches() {
-    skyframeExecutor.resetEvaluator();
-  }
-
   /**
    * Returns the TimestampGranularityMonitor. The same monitor object is used
    * across multiple Blaze commands, but it doesn't hold any persistent state
@@ -609,8 +596,6 @@
   void beforeCommand(Command command, CommandEnvironment env, OptionsParser optionsParser,
       CommonCommandOptions options, long execStartTimeNanos)
       throws AbruptExitException {
-    commandStartTime -= options.startupTime;
-
     env.getEventBus().post(new GotOptionsEvent(startupOptionsProvider,
         optionsParser));
     env.throwPendingException();
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java b/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java
index 732c942..9eda313 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java
@@ -17,6 +17,7 @@
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Range;
 import com.google.common.eventbus.EventBus;
 import com.google.devtools.build.lib.actions.PackageRootResolver;
 import com.google.devtools.build.lib.actions.cache.ActionCache;
@@ -52,6 +53,8 @@
 import java.util.UUID;
 import java.util.concurrent.atomic.AtomicReference;
 
+import javax.annotation.Nullable;
+
 /**
  * Encapsulates the state needed for a single command. The environment is dropped when the current
  * command is done and all corresponding objects are garbage collected.
@@ -68,6 +71,7 @@
   private final LoadingPhaseRunner loadingPhaseRunner;
   private final BuildView view;
 
+  private long commandStartTime;
   private String outputFileSystem;
   private Path workingDirectory;
 
@@ -266,8 +270,16 @@
         getWorkingDirectory(), defaultsPackageContents, commandId);
   }
 
+  public void recordLastExecutionTime() {
+    runtime.recordLastExecutionTime(getCommandStartTime());
+  }
+
+  public void recordCommandStartTime(long commandStartTime) {
+    this.commandStartTime = commandStartTime;
+  }
+
   public long getCommandStartTime() {
-    return runtime.getCommandStartTime();
+    return commandStartTime;
   }
 
   void setOutputFileSystem(String outputFileSystem) {
@@ -292,6 +304,8 @@
   void beforeCommand(Command command, OptionsParser optionsParser,
       CommonCommandOptions options, long execStartTimeNanos)
       throws AbruptExitException {
+    commandStartTime -= options.startupTime;
+
     runtime.beforeCommand(command, this, optionsParser, options, execStartTimeNanos);
   }
 }