macOS: `interprocess_semaphore` waiters either busy-spin a core or wake up ~2.5 ms late
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 48/100
Rechercherichtung
Start by reading spin_semaphore and spin_wait in the interprocess synchronization implementation, then check how platform-specific code is selected for macOS. The issue includes a cross-process reproducer and measured latency and CPU usage. Done means shared-memory waits on supported macOS versions block and wake with lower CPU use and latency, while the fallback continues to work on other versions.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
On macOS, BOOST_INTERPROCESS_POSIX_UNNAMED_SEMAPHORES is disabled (sem_init returns ENOSYS), so interprocess_semaphore uses spin_semaphore, and waits go through spin_wait. With CLK_TCK = 100, the "tick" is 10 ms. A waiter therefore calls sched_yield() in a loop for the first 10 ms, using a full core, and then alternates yields with 5 ms nanosleeps, so a post that arrives later is seen ~2.5 ms late on average and up to ~10 ms late. (Checked on macOS 27.2: sem_init(pshared=1) fails with ENOSYS; tracing one 50 ms wait shows ~40k sched_yield calls in the first 10.0 ms, then nanosleep(5 ms) calls.)
Reproducer: two processes, a semaphore in MAP_SHARED memory, and the parent posts every N ms (source below). Boost 1.92, built with clang++ -std=c++17 -O2 -I<boost> sem_latency.cpp -o sem_latency and run as ./sem_latency <interval ms>:
| post interval | macOS 27.2 (M-series): wake latency avg / waiter CPU | Linux 6.8 (x86_64) |
|---|---|---|
| 1 ms | 1.6 µs / 98% | 121 µs / 3% |
| 5 ms | 1.3 µs / 99% | 113 µs / 1% |
| 20 ms | 2.7 ms / 37% | 122 µs / 0% |
| 50 ms | 2.8 ms / 17% | 135 µs / 0% |
sem_latency.cpp
// Cross-process wake-up latency and waiter CPU of boost::interprocess::interprocess_semaphore.
// Usage: sem_latency <post interval ms>
// The parent posts a semaphore in shared memory every <interval> ms; the child waits on it.
#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <new>
#include <thread>
#include <vector>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <unistd.h>
namespace bi = boost::interprocess;
using clk = std::chrono::steady_clock;
constexpr int N = 200;
struct Shared {
bi::interprocess_semaphore sem{0};
bi::interprocess_semaphore done{0};
std::atomic<long long> post_ns{0};
double lat_us[N];
double cpu_s;
};
static long long now_ns() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(clk::now().time_since_epoch()).count();
}
static double cpu_s() {
struct rusage r;
getrusage(RUSAGE_SELF, &r);
return r.ru_utime.tv_sec + r.ru_stime.tv_sec + (r.ru_utime.tv_usec + r.ru_stime.tv_usec) / 1e6;
}
int main(int argc, char** argv) {
const int interval_ms = argc > 1 ? std::atoi(argv[1]) : 20;
void* mem = mmap(nullptr, sizeof(Shared), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
Shared* s = new (mem) Shared;
pid_t child = fork();
if (child == 0) {
double c0 = cpu_s();
for (int i = 0; i < N; ++i) {
s->sem.wait();
s->lat_us[i] = (now_ns() - s->post_ns.load()) / 1000.0;
s->done.post();
}
s->cpu_s = cpu_s() - c0;
_exit(0);
}
auto t0 = clk::now();
for (int i = 0; i < N; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms));
s->post_ns.store(now_ns());
s->sem.post();
s->done.wait();
}
double wall = std::chrono::duration<double>(clk::now() - t0).count();
waitpid(child, nullptr, 0);
std::vector<double> v(s->lat_us, s->lat_us + N);
std::sort(v.begin(), v.end());
double sum = 0;
for (double x : v) sum += x;
std::printf("post every %3d ms: wake latency avg %7.1f us, median %7.1f us, max %7.1f us | waiter CPU %3.0f%%\n",
interval_ms, sum / N, v[N / 2], v.back(), 100.0 * s->cpu_s / wall);
return 0;
}
Possible fix: macOS 14.4+ has a public futex-like API for shared memory, os_sync_wait_on_address() / os_sync_wake_by_address_any() with OS_SYNC_WAIT_ON_ADDRESS_SHARED (<os/os_sync_wait_on_address.h>). spin_semaphore could keep its 32-bit counter and block on it with this API when available, falling back to spin_wait otherwise.
- Vorherrschende Sprache
- C++
- Sterne
- 186
- Forks
- 131
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Entwicklungsumgebung
Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus boostorg/interprocess
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 68/100
boostorg/interprocess#196 · 1 Reaktion ·
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 35/100
boostorg/interprocess#279 ·
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 35/100
boostorg/interprocess#274 · 1 Kommentar ·
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 28/100
boostorg/interprocess#268 · 4 Kommentare ·
-
Semaphore use in signalsOffen
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 35/100
boostorg/interprocess#262 · 2 Kommentare ·
Alle Issues in boostorg/interprocess
Ähnliche Issues
-
agent:WSL code-review linux pri:medium refactoring
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
Maintainer antworten meist innerhalb von 1 Tag
-
kind/enhancement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 82/100
psi29a/d2decomped#691 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
CachyOS/packageinstaller#144 ·
Maintainer antworten meist innerhalb von 1 Tag
-
(S1 - Need confirmation)
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
CleverRaven/Cataclysm-DDA#89715 ·
Maintainer antworten meist innerhalb von 1 Tag