-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement GNU Make 4.4+ jobserver fifo / semaphore client support
The principle of such a job server is rather simple: Before starting a new job (edge in ninja-speak), a token must be acquired from an external entity. On posix systems, that entity is simply a fifo filled with N characters. On win32 systems it is a semaphore initialized to N. Once a job is finished, the token must be returned to the external entity. This functionality is desired when ninja is used as part of a bigger build, such as builds with Yocto/OpenEmbedded, Buildroot and Android. Here, multiple compile jobs are executed in parallel to maximize cpu utilization, but if each compile job uses all available cores, the system is over loaded.
- Loading branch information
Showing
9 changed files
with
318 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2024 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <fcntl.h> | ||
#include <cassert> | ||
#include <cstring> | ||
#include <unistd.h> | ||
|
||
#include "jobserver.h" | ||
#include "util.h" | ||
|
||
Jobserver::Jobserver() : token_count_(0), fd_(-1) | ||
{} | ||
|
||
void Jobserver::Init() { | ||
assert(fd_ < 0); | ||
|
||
if (!ParseJobserverAuth("fifo")) { | ||
return; | ||
} | ||
|
||
const char *jobserver = jobserver_name_.c_str(); | ||
|
||
fd_ = open(jobserver, O_NONBLOCK | O_CLOEXEC | O_RDWR); | ||
if (fd_ < 0) { | ||
Fatal("failed to open jobserver: %s: %s", jobserver, strerror(errno)); | ||
} | ||
|
||
Info("Using jobserver: %s", jobserver); | ||
} | ||
|
||
Jobserver::~Jobserver() { | ||
assert(token_count_ == 0); | ||
|
||
if (fd_ >= 0) { | ||
close(fd_); | ||
} | ||
} | ||
|
||
bool Jobserver::Enabled() const { | ||
return fd_ >= 0; | ||
} | ||
|
||
bool Jobserver::AcquireToken() { | ||
char token; | ||
int res = read(fd_, &token, 1); | ||
if (res < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { | ||
Fatal("failed to read jobserver token: %s", strerror(errno)); | ||
} | ||
|
||
return res > 0; | ||
} | ||
|
||
void Jobserver::ReleaseToken() { | ||
char token = '+'; | ||
int res = write(fd_, &token, 1); | ||
if (res != 1) { | ||
Fatal("failed to write token: %s: %s", jobserver_name_.c_str(), | ||
strerror(errno)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2024 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <windows.h> | ||
#include <cassert> | ||
|
||
#include "jobserver.h" | ||
#include "util.h" | ||
|
||
Jobserver::Jobserver() : token_count_(0), sem_(NULL) | ||
{} | ||
|
||
void Jobserver::Init() { | ||
assert(sem_ == NULL); | ||
|
||
if (!ParseJobserverAuth("sem")) { | ||
return; | ||
} | ||
|
||
const char *name = jobserver_name_.c_str(); | ||
|
||
sem_ = OpenSemaphore(SEMAPHORE_ALL_ACCESS, false, name); | ||
if (sem_ == NULL) { | ||
Win32Fatal("OpenSemaphore"); | ||
} | ||
|
||
Info("using jobserver: %s", name); | ||
} | ||
|
||
Jobserver::~Jobserver() { | ||
assert(token_count_ == 0); | ||
|
||
if (sem_ != NULL) { | ||
CloseHandle(sem_); | ||
} | ||
} | ||
|
||
bool Jobserver::Enabled() const { | ||
return sem_ != NULL; | ||
} | ||
|
||
bool Jobserver::AcquireToken() { | ||
return WaitForSingleObject(sem_, 0) == WAIT_OBJECT_0; | ||
} | ||
|
||
void Jobserver::ReleaseToken() { | ||
if (ReleaseSemaphore(sem_, 1, NULL)) { | ||
Win32Fatal("ReleaseSemaphore"); | ||
} | ||
} |
Oops, something went wrong.