18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-05 16:55:32 +01:00
Files
maszyna/tests/unit/test_worker_pool.cpp
T
jerrrrycho 2e967bf343 perf(vulkan): the threads recording the command buffers sleep right after each job
worker_pool takes how long its threads look out for the next job
before they sleep (spin_time, 50 us, by default). The pool of the
vulkan renderer gets zero: it has one job a frame, which comes
milliseconds after the last, so looking out for it never caught one
and only kept up to cores - 3 threads spinning beside the simulation's
pool in every frame. Its threads now go to sleep at once, as they did
before the pool looked out for jobs, and are woken by the job as
before. The simulation's pool, whose jobs of a frame come close after
one another, keeps its 50 us in every mode.

A unit test runs back-to-back jobs and jobs that need a woken thread on
a pool with zero look-out time.
2026-12-04 19:47:49 +01:00

172 lines
5.0 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
// the pool of threads the vehicle physics splits its work over (worker_pool): each index of a job done once, whichever
// threads take part, jobs close after one another and after the threads went to sleep, and an exception passed on
#include "stdafx.h"
#include "utilities/workerpool.h"
#include "utilities/Globals.h"
#include "doctest.h"
#include <algorithm>
#include <atomic>
#include <numeric>
#include <stdexcept>
#include <thread>
#include <vector>
namespace
{
// Global.performance_multithreading on for the test, as it was after it
struct multithreading_on
{
bool const was{Global.performance_multithreading};
multithreading_on() { Global.performance_multithreading = true; }
~multithreading_on() { Global.performance_multithreading = was; }
multithreading_on(multithreading_on const &) = delete;
multithreading_on &operator=(multithreading_on const &) = delete;
};
// runs a job on the pool and tells whether each of its indices was done exactly once
bool each_once(worker_pool &Pool, std::size_t const Count, std::size_t const Threads)
{
std::vector<std::atomic<int>> calls(Count);
Pool.run(Count, Threads, [&calls](std::size_t const Index) { calls[Index].fetch_add(1, std::memory_order_relaxed); });
return std::ranges::all_of(calls, [](std::atomic<int> const &Calls) { return Calls.load() == 1; });
}
} // namespace
TEST_CASE("worker pool: each index of a job is done once, by any number of threads")
{
multithreading_on const multithreading;
worker_pool pool;
auto const cores{worker_pool::capacity()};
for (std::size_t const threads : {std::size_t{1}, std::size_t{2}, std::size_t{3}, cores, cores + 5})
{
for (std::size_t const count : {std::size_t{0}, std::size_t{1}, std::size_t{2}, std::size_t{7}, std::size_t{64}, std::size_t{1000}})
{
CAPTURE(threads);
CAPTURE(count);
CHECK(each_once(pool, count, threads));
}
}
CHECK(pool.size() <= cores);
}
TEST_CASE("worker pool: jobs right after one another, and after the threads went to sleep")
{
multithreading_on const multithreading;
worker_pool pool;
auto const threads{worker_pool::capacity()};
// back to back, as the jobs of a frame come: the threads still look out for the next one
auto failures{0};
for (int job = 0; job < 2000; ++job)
{
if (false == each_once(pool, 1 + job % 97, 2 + job % threads))
{
++failures;
}
}
CHECK(failures == 0);
// with pauses longer than the threads look out for a job: they sleep, and are woken
for (int job = 0; job < 20; ++job)
{
std::this_thread::sleep_for(worker_pool::spin_time * 4);
CHECK(each_once(pool, 500, threads));
}
}
TEST_CASE("worker pool: a pool whose threads sleep right after each job, as the one of the vulkan renderer")
{
multithreading_on const multithreading;
worker_pool pool{std::chrono::microseconds::zero()};
auto const threads{worker_pool::capacity()};
// back to back: each job wakes the threads it gets a seat for
auto failures{0};
for (int job = 0; job < 2000; ++job)
{
if (false == each_once(pool, 1 + job % 97, 2 + job % threads))
{
++failures;
}
}
CHECK(failures == 0);
if (threads < 2)
{
return;
}
// a job of two calls which wait for each other ends only if a sleeping thread of the pool is woken for one of them
for (int job = 0; job < 20; ++job)
{
std::atomic<int> arrived{0};
pool.run(2, 2, [&arrived](std::size_t) {
arrived.fetch_add(1);
while (arrived.load() < 2)
{
std::this_thread::yield();
}
});
CHECK(arrived.load() == 2);
}
}
TEST_CASE("worker pool: the threads of the pool take part in the work")
{
multithreading_on const multithreading;
if (worker_pool::capacity() < 2)
{
return;
}
worker_pool pool;
// a job of two calls which wait for each other: it ends only if a thread of the pool takes one of them
std::atomic<int> arrived{0};
pool.run(2, 2, [&arrived](std::size_t) {
arrived.fetch_add(1);
while (arrived.load() < 2)
{
std::this_thread::yield();
}
});
CHECK(arrived.load() == 2);
// and again once it went to sleep
std::this_thread::sleep_for(worker_pool::spin_time * 4);
arrived = 0;
pool.run(2, 2, [&arrived](std::size_t) {
arrived.fetch_add(1);
while (arrived.load() < 2)
{
std::this_thread::yield();
}
});
CHECK(arrived.load() == 2);
}
TEST_CASE("worker pool: an exception of the work is passed on once the other calls are done, and the pool works on")
{
multithreading_on const multithreading;
worker_pool pool;
auto const threads{worker_pool::capacity()};
std::atomic<int> calls{0};
CHECK_THROWS_AS(pool.run(100, threads,
[&](std::size_t const Index) {
calls.fetch_add(1);
if (Index == 10)
{
throw std::runtime_error("work failed");
}
}),
std::runtime_error);
CHECK(calls.load() <= 100);
CHECK(each_once(pool, 300, threads));
}