package_parallel_read.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <Windows.h>
  2. #include <algorithm>
  3. #include <array>
  4. #include <vector>
  5. #include "parallel.h"
  6. namespace sunrise::middleware::content::packages::reader::parallel {
  7. namespace {
  8. /** Cores left to the game, which is drawing frames while the batch reads. */
  9. constexpr std::size_t kReservedCores = 2;
  10. /** One reader's share of a batch. Nothing here is shared with another reader. */
  11. struct Share {
  12. const Source* source{};
  13. std::span<const std::uint32_t> tags;
  14. Visitor visitor{};
  15. void* context{};
  16. std::size_t worker{};
  17. Scratch* scratch{};
  18. std::vector<std::byte>* bytes{};
  19. };
  20. /**
  21. * Reader storage, sized once and kept until the pass releases it.
  22. * These are megabytes each, so they are allocated rather than declared. Only the reader that
  23. * owns the index touches one.
  24. */
  25. std::vector<Scratch> g_scratch{};
  26. std::vector<std::vector<std::byte>> g_bytes{};
  27. /** Where one reader's kept blobs sit inside its own store. */
  28. struct KeptRow {
  29. std::uint32_t tag{};
  30. std::size_t offset{};
  31. std::size_t size{};
  32. };
  33. /** One reader's kept blobs, written only by that reader while a batch runs. */
  34. struct Keep {
  35. std::vector<std::byte> store;
  36. std::vector<KeptRow> rows;
  37. };
  38. std::vector<Keep> g_keep{};
  39. /** @param share One reader's share. Reads every tag of it and reports each blob. */
  40. void run_share(Share& share) noexcept {
  41. for (const std::uint32_t tag : share.tags) {
  42. if (read_tag(*share.source, *share.scratch, tag, *share.bytes)) {
  43. share.visitor(share.context, share.worker, tag, *share.bytes);
  44. }
  45. }
  46. }
  47. /** @param parameter One reader's share. @return Always zero; it reports through its visitor. */
  48. DWORD WINAPI thread_main(LPVOID parameter) noexcept {
  49. run_share(*static_cast<Share*>(parameter));
  50. return 0;
  51. }
  52. /**
  53. * Keeps one blob for the caller to parse after the batch.
  54. * It runs on the reader's thread and touches only that reader's store, so nothing locks.
  55. * @param worker Reader index.
  56. * @param tag Tag that read.
  57. * @param blob Entry bytes.
  58. */
  59. void keep_blob(void*,
  60. std::size_t worker,
  61. std::uint32_t tag,
  62. std::span<const std::byte> blob) noexcept {
  63. if (worker >= g_keep.size()) {
  64. return;
  65. }
  66. Keep& keep = g_keep[worker];
  67. const std::size_t offset = keep.store.size();
  68. keep.store.insert(keep.store.end(), blob.begin(), blob.end());
  69. keep.rows.push_back(KeptRow{tag, offset, blob.size()});
  70. }
  71. /** @param workers Readers wanted. @return True when storage for that many is ready. */
  72. [[nodiscard]] bool prepare(std::size_t workers) noexcept {
  73. if (g_scratch.size() >= workers) {
  74. return true;
  75. }
  76. // Growing moves the scratches, so it only ever happens before any file is open.
  77. g_scratch.resize(workers);
  78. g_bytes.resize(workers);
  79. return g_scratch.size() >= workers && g_bytes.size() >= workers;
  80. }
  81. } // namespace
  82. /** @return Reader count, at least one. */
  83. std::size_t worker_count() noexcept {
  84. SYSTEM_INFO info{};
  85. GetSystemInfo(&info);
  86. const auto cores = static_cast<std::size_t>(info.dwNumberOfProcessors);
  87. const std::size_t spare = cores > kReservedCores ? cores - kReservedCores : 1;
  88. return (std::min)(spare, kMaxWorkers);
  89. }
  90. /** Reads a run of tags across several readers and returns when every one has finished. */
  91. bool read_tags(const Source& source,
  92. std::span<const std::uint32_t> tags,
  93. Visitor visitor,
  94. void* context) noexcept {
  95. if (visitor == nullptr) {
  96. return false;
  97. }
  98. if (tags.empty()) {
  99. return true;
  100. }
  101. const std::size_t workers = (std::min)(worker_count(), tags.size());
  102. if (!prepare(workers)) {
  103. return false;
  104. }
  105. // Each reader takes one run of tags, not every nth. Tags next to each other share a block,
  106. // so a reader that keeps hitting its own block cache reads far less.
  107. const std::size_t share = (tags.size() + workers - 1U) / workers;
  108. std::array<Share, kMaxWorkers> shares{};
  109. std::array<HANDLE, kMaxWorkers> threads{};
  110. std::size_t spawned = 0;
  111. for (std::size_t worker = 0; worker < workers; ++worker) {
  112. const std::size_t begin = (std::min)(worker * share, tags.size());
  113. const std::size_t end = (std::min)(begin + share, tags.size());
  114. shares[worker] = Share{&source,
  115. tags.subspan(begin, end - begin),
  116. visitor,
  117. context,
  118. worker,
  119. &g_scratch[worker],
  120. &g_bytes[worker]};
  121. }
  122. // The calling thread reads the first share, so one fewer thread is spawned for the same work.
  123. std::array<bool, kMaxWorkers> started{};
  124. for (std::size_t worker = 1; worker < workers; ++worker) {
  125. const HANDLE thread = CreateThread(nullptr, 0, &thread_main, &shares[worker], 0, nullptr);
  126. if (thread == nullptr) {
  127. continue;
  128. }
  129. threads[spawned++] = thread;
  130. started[worker] = true;
  131. }
  132. run_share(shares[0]);
  133. // A reader that would not start is read here instead, on this thread and with this thread's
  134. // storage, so the batch is never left partly read. A share that did start is not touched
  135. // again until its thread has joined.
  136. for (std::size_t worker = 1; worker < workers; ++worker) {
  137. if (!started[worker]) {
  138. shares[worker].worker = 0;
  139. shares[worker].scratch = &g_scratch[0];
  140. shares[worker].bytes = &g_bytes[0];
  141. run_share(shares[worker]);
  142. }
  143. }
  144. if (spawned != 0) {
  145. (void)WaitForMultipleObjects(static_cast<DWORD>(spawned), threads.data(), TRUE, INFINITE);
  146. for (std::size_t index = 0; index < spawned; ++index) {
  147. (void)CloseHandle(threads[index]);
  148. }
  149. }
  150. return true;
  151. }
  152. /** Reads a run of tags across several readers and keeps every blob that read. */
  153. bool read_kept(const Source& source,
  154. std::span<const std::uint32_t> tags,
  155. std::vector<Held>& kept) noexcept {
  156. kept.clear();
  157. const std::size_t workers = worker_count();
  158. if (g_keep.size() < workers) {
  159. g_keep.resize(workers);
  160. }
  161. if (g_keep.size() < workers) {
  162. return false;
  163. }
  164. for (Keep& keep : g_keep) {
  165. keep.store.clear();
  166. keep.rows.clear();
  167. }
  168. if (!read_tags(source, tags, &keep_blob, nullptr)) {
  169. return false;
  170. }
  171. // Each reader took one run of tags in order, so reading the readers in order returns the
  172. // blobs in the order the caller asked for them.
  173. for (const Keep& keep : g_keep) {
  174. for (const KeptRow& row : keep.rows) {
  175. kept.push_back(Held{row.tag, {keep.store.data() + row.offset, row.size}});
  176. }
  177. }
  178. return true;
  179. }
  180. /** Closes every reader's files and frees the batch storage. */
  181. void release() noexcept {
  182. for (Scratch& scratch : g_scratch) {
  183. close_files(scratch);
  184. }
  185. g_scratch.clear();
  186. g_scratch.shrink_to_fit();
  187. g_bytes.clear();
  188. g_bytes.shrink_to_fit();
  189. g_keep.clear();
  190. g_keep.shrink_to_fit();
  191. }
  192. } // namespace sunrise::middleware::content::packages::reader::parallel