Line | Branch | Exec | Source |
---|---|---|---|
1 | #include "pool.h" | ||
2 | #include "logging/logging.h" | ||
3 | #include <iostream> | ||
4 | |||
5 | namespace lython { | ||
6 | |||
7 | #if BUILD_WEBASSEMBLY | ||
8 | #else | ||
9 | |||
10 | void worker_loop(ThreadPool* pool, std::size_t n); | ||
11 | |||
12 | ThreadPool::ThreadPool(std::size_t thread_count) { | ||
13 |
1/1✓ Branch 1 taken 6 times.
|
6 | tasks.reserve(128); |
14 |
1/1✓ Branch 1 taken 6 times.
|
6 | stats.reserve(thread_count); |
15 |
1/1✓ Branch 1 taken 6 times.
|
6 | threads.reserve(thread_count); |
16 | |||
17 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for (std::size_t i = 0; i < thread_count; ++i) { |
18 |
1/1✓ Branch 1 taken 12 times.
|
12 | insert_worker(); |
19 | } | ||
20 | 6 | } | |
21 | |||
22 | std::optional<ThreadPool::Task_t> ThreadPool::pop() { | ||
23 |
1/1✓ Branch 1 taken 21 times.
|
21 | std::lock_guard lock(mux); |
24 | 21 | std::optional<Task_t> task; | |
25 | |||
26 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 17 times.
|
21 | if (tasks.size() > 0) { |
27 |
1/1✓ Branch 4 taken 4 times.
|
4 | task = *(tasks.end() - 1); |
28 | 4 | tasks.pop_back(); | |
29 | } | ||
30 | |||
31 | 42 | return task; | |
32 | 21 | } | |
33 | |||
34 | void ThreadPool::insert_worker() { | ||
35 | 14 | std::size_t n = threads.size(); | |
36 |
1/1✓ Branch 1 taken 14 times.
|
14 | stats.emplace_back(); |
37 |
1/1✓ Branch 1 taken 14 times.
|
14 | threads.emplace_back(worker_loop, this, n); |
38 | 14 | } | |
39 | |||
40 | void ThreadPool::shutdown(bool wait) { | ||
41 |
2/2✓ Branch 4 taken 14 times.
✓ Branch 5 taken 10 times.
|
24 | for (auto& state: stats) { |
42 | 14 | state.running = false; | |
43 | } | ||
44 | |||
45 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (wait) { |
46 |
2/2✓ Branch 5 taken 14 times.
✓ Branch 6 taken 10 times.
|
24 | for (auto& thread: threads) { |
47 |
1/1✓ Branch 1 taken 14 times.
|
14 | thread.join(); |
48 | } | ||
49 | } | ||
50 | |||
51 |
1/1✓ Branch 5 taken 10 times.
|
10 | threads.erase(std::begin(threads), std::end(threads)); |
52 |
1/1✓ Branch 5 taken 10 times.
|
10 | stats.erase(std::begin(stats), std::end(stats)); |
53 | 10 | } | |
54 | |||
55 | std::size_t ThreadPool::size() const { return threads.size(); } | ||
56 | |||
57 | std::ostream& ThreadPool::print(std::ostream& out) const { | ||
58 | 1 | auto end = StopWatch<>::Clock::now(); | |
59 | 1 | int total_tasks = 0; | |
60 | |||
61 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
2 | out << fmt::format("| {:4} | {:6} | {:4} | {} |\n", "#id", "busy%", "task", "sleep"); |
62 |
1/1✓ Branch 1 taken 1 times.
|
1 | out << "|------+--------+------+-------|\n"; |
63 | |||
64 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | for (std::size_t i = 0; i < size(); ++i) { |
65 | 2 | Stat_t const& stat = stats[i]; | |
66 |
1/1✓ Branch 1 taken 2 times.
|
2 | auto total = float(StopWatch<>::diff(stat.start, end)); |
67 | 2 | auto busy = stat.work_time * 100 / total; | |
68 | |||
69 | 2 | total_tasks += stat.task; | |
70 | |||
71 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
4 | out << fmt::format("| {:4} | {:6.2f} | {:4} | {:5} |\n", i, busy, stat.task, stat.sleeping); |
72 | } | ||
73 | |||
74 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
2 | out << fmt::format(" Total Tasks: {}\n", total_tasks); |
75 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
2 | out << fmt::format("Remaining Tasks: {}\n", tasks.size()); |
76 | 1 | return out; | |
77 | } | ||
78 | |||
79 | void worker_loop(ThreadPool* pool, std::size_t n) { | ||
80 | 14 | pool->stats[n].start = StopWatch<>::Clock::now(); | |
81 | |||
82 |
2/2✓ Branch 1 taken 21 times.
✓ Branch 2 taken 14 times.
|
35 | while (pool->stats[n].running) { |
83 |
1/1✓ Branch 1 taken 21 times.
|
21 | auto maybe_task = pool->pop(); |
84 | |||
85 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 17 times.
|
21 | if (maybe_task.has_value()) { |
86 | 4 | pool->stats[n].sleeping = false; | |
87 | 4 | StopWatch<> chrono; | |
88 | |||
89 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
|
4 | std::function<void()> task = maybe_task.value(); |
90 |
1/1✓ Branch 1 taken 4 times.
|
4 | task(); |
91 | |||
92 |
1/1✓ Branch 1 taken 4 times.
|
4 | pool->stats[n].work_time += float(chrono.stop()); |
93 | 4 | pool->stats[n].task += 1; | |
94 | 4 | pool->stats[n].sleeping = true; | |
95 | 4 | } else { | |
96 | 17 | std::this_thread::yield(); | |
97 | } | ||
98 | 21 | } | |
99 | 14 | } | |
100 | |||
101 | #endif | ||
102 | } // namespace lython | ||
103 |