diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc index 924dd2c5803bfe..9a3396cbd5a549 100644 --- a/src/main/cpp/blaze.cc +++ b/src/main/cpp/blaze.cc @@ -714,13 +714,13 @@ static int StartServer(const WorkspaceLayout *workspace_layout, // This function passes the commands array to the blaze process. // This array should start with a command ("build", "info", etc.). static void StartStandalone(const WorkspaceLayout *workspace_layout, - BlazeServer *server) { + BlazeServer *server, uint64_t start_time) { if (server->Connected()) { server->KillRunningServer(); } // Wall clock time since process startup. - globals->startup_time = GetMillisecondsSinceProcessStart(); + globals->startup_time = GetMillisecondsMonotonic() - start_time; BAZEL_LOG(INFO) << "Starting " << globals->options->product_name << " in batch mode."; @@ -1266,7 +1266,8 @@ static void CancelServer() { blaze_server->Cancel(); } // Performs all I/O for a single client request to the server, and // shuts down the client (by exit or signal). static ATTRIBUTE_NORETURN void SendServerRequest( - const WorkspaceLayout *workspace_layout, BlazeServer *server) { + const WorkspaceLayout *workspace_layout, BlazeServer *server, + uint64_t start_time) { while (true) { if (!server->Connected()) { StartServerAndConnect(workspace_layout, server); @@ -1304,7 +1305,7 @@ static ATTRIBUTE_NORETURN void SendServerRequest( BAZEL_LOG(INFO) << "Connected (server pid=" << globals->server_pid << ")."; // Wall clock time since process startup. - globals->startup_time = GetMillisecondsSinceProcessStart(); + globals->startup_time = GetMillisecondsMonotonic() - start_time; SignalHandler::Get().Install(globals, CancelServer); SignalHandler::Get().PropagateSignalOrExit(server->Communicate()); @@ -1511,7 +1512,7 @@ int GetExitCodeForAbruptExit(const GlobalVariables &globals) { } int Main(int argc, const char *argv[], WorkspaceLayout *workspace_layout, - OptionProcessor *option_processor) { + OptionProcessor *option_processor, uint64_t start_time) { // Logging must be set first to assure no log statements are missed. std::unique_ptr default_handler( new blaze_util::BazelLogHandler()); @@ -1588,9 +1589,9 @@ int Main(int argc, const char *argv[], WorkspaceLayout *workspace_layout, if (globals->options->batch) { SetScheduling(globals->options->batch_cpu_scheduling, globals->options->io_nice_level); - StartStandalone(workspace_layout, blaze_server); + StartStandalone(workspace_layout, blaze_server, start_time); } else { - SendServerRequest(workspace_layout, blaze_server); + SendServerRequest(workspace_layout, blaze_server, start_time); } return 0; } diff --git a/src/main/cpp/blaze.h b/src/main/cpp/blaze.h index d5c467d464cd72..77bf54851d98d0 100644 --- a/src/main/cpp/blaze.h +++ b/src/main/cpp/blaze.h @@ -21,7 +21,7 @@ namespace blaze { int Main(int argc, const char* argv[], WorkspaceLayout* workspace_layout, - OptionProcessor* option_processor); + OptionProcessor* option_processor, uint64_t start_time); } // namespace blaze diff --git a/src/main/cpp/blaze_util_freebsd.cc b/src/main/cpp/blaze_util_freebsd.cc index 7cca0a0973a9cc..ed7ec49045d0ab 100644 --- a/src/main/cpp/blaze_util_freebsd.cc +++ b/src/main/cpp/blaze_util_freebsd.cc @@ -100,12 +100,6 @@ uint64_t GetMillisecondsMonotonic() { return ts.tv_sec * 1000LL + (ts.tv_nsec / 1000000LL); } -uint64_t GetMillisecondsSinceProcessStart() { - struct timespec ts = {}; - clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); - return ts.tv_sec * 1000LL + (ts.tv_nsec / 1000000LL); -} - void SetScheduling(bool batch_cpu_scheduling, int io_nice_level) { // Stubbed out so we can compile for FreeBSD. } diff --git a/src/main/cpp/blaze_util_linux.cc b/src/main/cpp/blaze_util_linux.cc index dee5463f7243c3..cf1b0ded1606ce 100644 --- a/src/main/cpp/blaze_util_linux.cc +++ b/src/main/cpp/blaze_util_linux.cc @@ -102,12 +102,6 @@ uint64_t GetMillisecondsMonotonic() { return ts.tv_sec * 1000LL + (ts.tv_nsec / 1000000LL); } -uint64_t GetMillisecondsSinceProcessStart() { - struct timespec ts = {}; - clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); - return ts.tv_sec * 1000LL + (ts.tv_nsec / 1000000LL); -} - void SetScheduling(bool batch_cpu_scheduling, int io_nice_level) { if (batch_cpu_scheduling) { sched_param param = {}; diff --git a/src/main/cpp/blaze_util_platform.h b/src/main/cpp/blaze_util_platform.h index ec8063f79dd168..2e13acff952adc 100644 --- a/src/main/cpp/blaze_util_platform.h +++ b/src/main/cpp/blaze_util_platform.h @@ -113,9 +113,6 @@ void WarnFilesystemType(const std::string& output_base); // a value less than a previous result. uint64_t GetMillisecondsMonotonic(); -// Returns elapsed milliseconds since the process started. -uint64_t GetMillisecondsSinceProcessStart(); - // Set cpu and IO scheduling properties. Note that this can take ~50ms // on Linux, so it should only be called when necessary. void SetScheduling(bool batch_cpu_scheduling, int io_nice_level); diff --git a/src/main/cpp/blaze_util_windows.cc b/src/main/cpp/blaze_util_windows.cc index d5c3e178451e25..3f3f8bfbb935e1 100644 --- a/src/main/cpp/blaze_util_windows.cc +++ b/src/main/cpp/blaze_util_windows.cc @@ -277,7 +277,6 @@ SignalHandler SignalHandler::INSTANCE; class WindowsClock { public: uint64_t GetMilliseconds() const; - uint64_t GetProcessMilliseconds() const; static const WindowsClock INSTANCE; @@ -289,9 +288,6 @@ class WindowsClock { // queried upon application initialization, and the result can be cached." const LARGE_INTEGER kFrequency; - // Time (in milliseconds) at process start. - const LARGE_INTEGER kStart; - WindowsClock(); static LARGE_INTEGER GetFrequency(); @@ -432,10 +428,6 @@ uint64_t GetMillisecondsMonotonic() { return WindowsClock::INSTANCE.GetMilliseconds(); } -uint64_t GetMillisecondsSinceProcessStart() { - return WindowsClock::INSTANCE.GetProcessMilliseconds(); -} - void SetScheduling(bool batch_cpu_scheduling, int io_nice_level) { // TODO(bazel-team): There should be a similar function on Windows. } @@ -1115,17 +1107,12 @@ LARGE_INTEGER WindowsClock::GetMillisecondsAsLargeInt( const WindowsClock WindowsClock::INSTANCE; WindowsClock::WindowsClock() - : kFrequency(GetFrequency()), - kStart(GetMillisecondsAsLargeInt(kFrequency)) {} + : kFrequency(GetFrequency()) {} uint64_t WindowsClock::GetMilliseconds() const { return GetMillisecondsAsLargeInt(kFrequency).QuadPart; } -uint64_t WindowsClock::GetProcessMilliseconds() const { - return GetMilliseconds() - kStart.QuadPart; -} - uint64_t AcquireLock(const string& output_base, bool batch_mode, bool block, BlazeLock* blaze_lock) { string lockfile = blaze_util::JoinPath(output_base, "lock"); diff --git a/src/main/cpp/main.cc b/src/main/cpp/main.cc index 25a1dbf79b424a..e7102ec0d67f38 100644 --- a/src/main/cpp/main.cc +++ b/src/main/cpp/main.cc @@ -16,16 +16,19 @@ #include "src/main/cpp/bazel_startup_options.h" #include "src/main/cpp/blaze.h" +#include "src/main/cpp/blaze_util_platform.h" #include "src/main/cpp/option_processor.h" #include "src/main/cpp/startup_options.h" #include "src/main/cpp/workspace_layout.h" int main(int argc, const char *argv[]) { + uint64_t start_time = blaze::GetMillisecondsMonotonic(); std::unique_ptr workspace_layout( new blaze::WorkspaceLayout()); std::unique_ptr startup_options( new blaze::BazelStartupOptions(workspace_layout.get())); return blaze::Main(argc, argv, workspace_layout.get(), new blaze::OptionProcessor(workspace_layout.get(), - std::move(startup_options))); + std::move(startup_options)), + start_time); }