Skip to content

Commit

Permalink
Actually use wall time for startup_time (as documented).
Browse files Browse the repository at this point in the history
For cold builds, the CPU and wall time differ by more than 1s.

RELNOTES: None
PiperOrigin-RevId: 215368509
  • Loading branch information
meisterT authored and Copybara-Service committed Oct 2, 2018
1 parent e888150 commit f59fad7
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 38 deletions.
15 changes: 8 additions & 7 deletions src/main/cpp/blaze.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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<blaze_util::BazelLogHandler> default_handler(
new blaze_util::BazelLogHandler());
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/blaze.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 0 additions & 6 deletions src/main/cpp/blaze_util_freebsd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand Down
6 changes: 0 additions & 6 deletions src/main/cpp/blaze_util_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
3 changes: 0 additions & 3 deletions src/main/cpp/blaze_util_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 1 addition & 14 deletions src/main/cpp/blaze_util_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ SignalHandler SignalHandler::INSTANCE;
class WindowsClock {
public:
uint64_t GetMilliseconds() const;
uint64_t GetProcessMilliseconds() const;

static const WindowsClock INSTANCE;

Expand All @@ -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();
Expand Down Expand Up @@ -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.
}
Expand Down Expand Up @@ -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");
Expand Down
5 changes: 4 additions & 1 deletion src/main/cpp/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<blaze::WorkspaceLayout> workspace_layout(
new blaze::WorkspaceLayout());
std::unique_ptr<blaze::StartupOptions> 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);
}

0 comments on commit f59fad7

Please sign in to comment.