Skip to content

Commit

Permalink
Use the workspace name in the process title of the Bazel server.
Browse files Browse the repository at this point in the history
This makes way more sense than using the name of the *parent* directory of the workspace.

Closes bazelbuild#4253 - thanks @akira-baruah for suggesting this change and making it happen!

PiperOrigin-RevId: 184147456
  • Loading branch information
philwo authored and Copybara-Service committed Feb 1, 2018
1 parent 875068a commit a7831cc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/main/cpp/blaze.cc
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,14 @@ string GetEmbeddedBinariesRoot(const string &install_base) {
}

// Returns the JVM command argument array.
static vector<string> GetArgumentArray() {
static vector<string> GetArgumentArray(
const WorkspaceLayout *workspace_layout) {
vector<string> result;

// e.g. A Blaze server process running in ~/src/build_root (where there's a
// ~/src/build_root/WORKSPACE file) will appear in ps(1) as "blaze(src)".
string workspace =
blaze_util::Basename(blaze_util::Dirname(globals->workspace));
workspace_layout->GetPrettyWorkspaceName(globals->workspace);
string product = globals->options->product_name;
blaze_util::ToLower(&product);
result.push_back(product + "(" + workspace + ")");
Expand Down Expand Up @@ -673,7 +674,7 @@ static void VerifyJavaVersionAndSetJvm() {
// Starts the Blaze server.
static int StartServer(const WorkspaceLayout *workspace_layout,
BlazeServerStartup **server_startup) {
vector<string> jvm_args_vector = GetArgumentArray();
vector<string> jvm_args_vector = GetArgumentArray(workspace_layout);
string argument_string = GetArgumentString(jvm_args_vector);
string server_dir =
blaze_util::JoinPath(globals->options->output_base, "server");
Expand Down Expand Up @@ -734,7 +735,7 @@ static void StartStandalone(const WorkspaceLayout *workspace_layout,
globals->options->product_name.c_str(),
globals->options->product_name.c_str(), product.c_str());
}
vector<string> jvm_args_vector = GetArgumentArray();
vector<string> jvm_args_vector = GetArgumentArray(workspace_layout);
if (!command.empty()) {
jvm_args_vector.push_back(command);
AddLoggingArgs(&jvm_args_vector);
Expand Down Expand Up @@ -1134,7 +1135,8 @@ static bool ServerNeedsToBeKilled(const vector<string> &args1,
}

// Kills the running Blaze server, if any, if the startup options do not match.
static void KillRunningServerIfDifferentStartupOptions(BlazeServer *server) {
static void KillRunningServerIfDifferentStartupOptions(
const WorkspaceLayout *workspace_layout, BlazeServer *server) {
if (!server->Connected()) {
return;
}
Expand All @@ -1153,7 +1155,7 @@ static void KillRunningServerIfDifferentStartupOptions(BlazeServer *server) {
// These strings contain null-separated command line arguments. If they are
// the same, the server can stay alive, otherwise, it needs shuffle off this
// mortal coil.
if (ServerNeedsToBeKilled(arguments, GetArgumentArray())) {
if (ServerNeedsToBeKilled(arguments, GetArgumentArray(workspace_layout))) {
globals->restart_reason = NEW_OPTIONS;
PrintWarning(
"Running %s server needs to be killed, because the "
Expand Down Expand Up @@ -1493,7 +1495,7 @@ int Main(int argc, const char *argv[], WorkspaceLayout *workspace_layout,

blaze_server->Connect();
EnsureCorrectRunningVersion(blaze_server);
KillRunningServerIfDifferentStartupOptions(blaze_server);
KillRunningServerIfDifferentStartupOptions(workspace_layout, blaze_server);

if (globals->options->batch) {
SetScheduling(globals->options->batch_cpu_scheduling,
Expand Down
7 changes: 7 additions & 0 deletions src/main/cpp/workspace_layout.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ string WorkspaceLayout::GetWorkspace(const string &cwd) const {
return "";
}

string WorkspaceLayout::GetPrettyWorkspaceName(
const std::string& workspace) const {
// e.g. A Bazel server process running in ~/src/myproject (where there's a
// ~/src/myproject/WORKSPACE file) will appear in ps(1) as "bazel(myproject)".
return blaze_util::Basename(workspace);
}

static string FindDepotBlazerc(const blaze::WorkspaceLayout* workspace_layout,
const string& workspace) {
// Package semantics are ignored here, but that's acceptable because
Expand Down
5 changes: 5 additions & 0 deletions src/main/cpp/workspace_layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class WorkspaceLayout {
// relative or absolute.
virtual std::string GetWorkspace(const std::string& cwd) const;

// Given a result returned from GetWorkspace, returns a pretty workspace name
// than can e.g. be used in the process title of the Bazel server.
virtual std::string GetPrettyWorkspaceName(
const std::string& workspace) const;

// Returns if workspace is a valid build workspace.
virtual bool InWorkspace(const std::string& workspace) const;

Expand Down
11 changes: 11 additions & 0 deletions src/test/shell/bazel/client_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ EOF
expect_log "^output_path:.*/execroot/blerp/bazel-out\$"
expect_log "^execution_root:.*/execroot/blerp\$"
}

# This test is for Bazel only and not for Google's internal version (Blaze),
# because Bazel uses a different way to compute the workspace name.
function test_server_process_name_has_workspace_name() {
mkdir foobarspace
cd foobarspace
touch WORKSPACE BUILD
ps -o cmd= "$(bazel info server_pid)" &>"$TEST_log"
expect_log "^bazel(foobarspace)"
bazel shutdown
}

0 comments on commit a7831cc

Please sign in to comment.