Skip to content

Commit

Permalink
Linux: simplify sleep before next frame snapshot
Browse files Browse the repository at this point in the history
Before this commit kmsgrab, x11grab, cuda used a "busy" wait that did
not in fact keep the thread busy since it called `sleep_for()`.
The whole waiting period is now slept for at once.

Wlgrab used a true busy wait for a third of the waiting time. This
entailed a rather high CPU usage. The watiting method has been aligned
to the simplified method of the other Linux capture backends.

(Benchmark data can be found in the discussion of PR #2333.)
  • Loading branch information
gschintgen committed Apr 4, 2024
1 parent b8b44cb commit 0b77071
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 33 deletions.
6 changes: 1 addition & 5 deletions src/platform/linux/cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,11 +803,7 @@ namespace cuda {
while (true) {
auto now = std::chrono::steady_clock::now();
if (next_frame > now) {
std::this_thread::sleep_for((next_frame - now) / 3 * 2);
}
while (next_frame > now) {
std::this_thread::sleep_for(1ns);
now = std::chrono::steady_clock::now();
std::this_thread::sleep_for(next_frame - now);
}
next_frame += delay;
if (next_frame < now) { // some major slowdown happened; we couldn't keep up
Expand Down
12 changes: 2 additions & 10 deletions src/platform/linux/kmsgrab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1197,11 +1197,7 @@ namespace platf {
auto now = std::chrono::steady_clock::now();

if (next_frame > now) {
std::this_thread::sleep_for((next_frame - now) / 3 * 2);
}
while (next_frame > now) {
std::this_thread::sleep_for(1ns);
now = std::chrono::steady_clock::now();
std::this_thread::sleep_for(next_frame - now);
}
next_frame += delay;
if (next_frame < now) { // some major slowdown happened; we couldn't keep up
Expand Down Expand Up @@ -1421,11 +1417,7 @@ namespace platf {
auto now = std::chrono::steady_clock::now();

if (next_frame > now) {
std::this_thread::sleep_for((next_frame - now) / 3 * 2);
}
while (next_frame > now) {
std::this_thread::sleep_for(1ns);
now = std::chrono::steady_clock::now();
std::this_thread::sleep_for(next_frame - now);
}

if (config::sunshine.min_log_level <= 1) {
Expand Down
16 changes: 8 additions & 8 deletions src/platform/linux/wlgrab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ namespace wl {
auto now = std::chrono::steady_clock::now();

if (next_frame > now) {
std::this_thread::sleep_for((next_frame - now) / 3 * 2);
std::this_thread::sleep_for(next_frame - now);
}
while (next_frame > now) {
now = std::chrono::steady_clock::now();
next_frame += delay;
if (next_frame < now) { // some major slowdown happened; we couldn't keep up
next_frame = now + delay;
}
next_frame = now + delay;

std::shared_ptr<platf::img_t> img_out;
auto status = snapshot(pull_free_image_cb, img_out, 1000ms, *cursor);
Expand Down Expand Up @@ -263,12 +263,12 @@ namespace wl {
auto now = std::chrono::steady_clock::now();

if (next_frame > now) {
std::this_thread::sleep_for((next_frame - now) / 3 * 2);
std::this_thread::sleep_for(next_frame - now);
}
while (next_frame > now) {
now = std::chrono::steady_clock::now();
next_frame += delay;
if (next_frame < now) { // some major slowdown happened; we couldn't keep up
next_frame = now + delay;
}
next_frame = now + delay;

std::shared_ptr<platf::img_t> img_out;
auto status = snapshot(pull_free_image_cb, img_out, 1000ms, *cursor);
Expand Down
12 changes: 2 additions & 10 deletions src/platform/linux/x11grab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,11 +485,7 @@ namespace platf {
auto now = std::chrono::steady_clock::now();

if (next_frame > now) {
std::this_thread::sleep_for((next_frame - now) / 3 * 2);
}
while (next_frame > now) {
std::this_thread::sleep_for(1ns);
now = std::chrono::steady_clock::now();
std::this_thread::sleep_for(next_frame - now);
}
next_frame += delay;
if (next_frame < now) { // some major slowdown happened; we couldn't keep up
Expand Down Expand Up @@ -629,11 +625,7 @@ namespace platf {
auto now = std::chrono::steady_clock::now();

if (next_frame > now) {
std::this_thread::sleep_for((next_frame - now) / 3 * 2);
}
while (next_frame > now) {
std::this_thread::sleep_for(1ns);
now = std::chrono::steady_clock::now();
std::this_thread::sleep_for(next_frame - now);
}
next_frame += delay;
if (next_frame < now) { // some major slowdown happened; we couldn't keep up
Expand Down

0 comments on commit 0b77071

Please sign in to comment.