diff options
Diffstat (limited to 'runtime/shims/dstorage.cpp')
| -rw-r--r-- | runtime/shims/dstorage.cpp | 172 |
1 files changed, 162 insertions, 10 deletions
diff --git a/runtime/shims/dstorage.cpp b/runtime/shims/dstorage.cpp index fe800fe68..103a5c9df 100644 --- a/runtime/shims/dstorage.cpp +++ b/runtime/shims/dstorage.cpp | |||
| @@ -10,6 +10,8 @@ | |||
| 10 | // it has. Completion is therefore in queue order, as the API guarantees. | 10 | // it has. Completion is therefore in queue order, as the API guarantees. |
| 11 | // - Compressed requests stage RFC 1950 inflate, BCPack BC block reconstruction and DirectStorage | 11 | // - Compressed requests stage RFC 1950 inflate, BCPack BC block reconstruction and DirectStorage |
| 12 | // macro-unshuffle before publishing the destination. Physical page numbers resolve in xmem at execution. | 12 | // macro-unshuffle before publishing the destination. Physical page numbers resolve in xmem at execution. |
| 13 | // - A queue half full submits itself, but a read into physical pages and everything enqueued behind it wait for | ||
| 14 | // the title's own Submit: the title may fill the page list only then (hold_pages below). | ||
| 13 | // Nothing here goes online or touches the GPU. | 15 | // Nothing here goes online or touches the GPU. |
| 14 | #include <windows.h> | 16 | #include <windows.h> |
| 15 | #include <d3d12.h> | 17 | #include <d3d12.h> |
| @@ -38,6 +40,7 @@ | |||
| 38 | #include "../src/shader_directory.h" | 40 | #include "../src/shader_directory.h" |
| 39 | #include "../src/dstorage_swizzle.h" | 41 | #include "../src/dstorage_swizzle.h" |
| 40 | #include "../src/dstorage_trace.h" | 42 | #include "../src/dstorage_trace.h" |
| 43 | #include "../src/dstorage_hold.h" | ||
| 41 | #include "dstorage_xs_abi.h" | 44 | #include "dstorage_xs_abi.h" |
| 42 | #include "dstorage_staging.h" | 45 | #include "dstorage_staging.h" |
| 43 | #include "startup_trace.h" | 46 | #include "startup_trace.h" |
| @@ -542,6 +545,47 @@ HRESULT read_file_range(HANDLE handle, uint64_t offset, void* destination, uint3 | |||
| 542 | return S_OK; | 545 | return S_OK; |
| 543 | } | 546 | } |
| 544 | 547 | ||
| 548 | // ---- Reads into physical pages wait for the title's Submit ------------------------------------------------------- | ||
| 549 | // | ||
| 550 | // A request into physical pages names them through a pointer into the title's own page-number table, and a title may | ||
| 551 | // fill that table for a batch only in its submit path, right before Submit. A queue that submits itself at half its | ||
| 552 | // capacity then starts such a read early: it finds page 0 and fails, or finds the number the slot held before and | ||
| 553 | // writes the data into a page that now belongs to something else, with no error at all. So a self-submit stops at | ||
| 554 | // the first read into physical pages: that read and every entry behind it (the queue completes in order) wait for | ||
| 555 | // the title's Submit. A full queue still submits everything, since the title cannot call Submit while it waits in an | ||
| 556 | // enqueue, and Close does as before. The idea is from VIctory (Marvisak/VIctory eddde73, carried in | ||
| 557 | // fxceless/victory-fork 79cfdd9); the code here is written anew for this queue. | ||
| 558 | // | ||
| 559 | // LEONIDA_DSTORAGE_HOLD_PAGES=0 restores the old self-submit of everything. A hold has a time limit, so that a | ||
| 560 | // title path that never calls Submit and counted on the self-submit cannot stall for good: held entries that have | ||
| 561 | // waited LEONIDA_DSTORAGE_HOLD_MS milliseconds (2000 unless set) are submitted, page lists as they are, and counted | ||
| 562 | // as a timed release. 0 is no limit: they wait for Submit, however long. Both are read when a queue is created. | ||
| 563 | constexpr uint32_t kDefaultHoldMs = 2000; | ||
| 564 | |||
| 565 | uint32_t environment_number(const wchar_t* name, uint32_t fallback) { | ||
| 566 | wchar_t value[16]{}; | ||
| 567 | const DWORD n = GetEnvironmentVariableW(name, value, 16); | ||
| 568 | if (n == 0 || n >= 16) return fallback; | ||
| 569 | wchar_t* end = nullptr; | ||
| 570 | const unsigned long parsed = std::wcstoul(value, &end, 10); | ||
| 571 | return end && *end == 0 ? static_cast<uint32_t>(parsed) : fallback; | ||
| 572 | } | ||
| 573 | |||
| 574 | // One line with every counter, at each power of two of the counter that moved: countable in a truncated log | ||
| 575 | // without a line per event. | ||
| 576 | void log_hold(const char* event, uint64_t n, unsigned capacity, size_t held_now) { | ||
| 577 | if (n & (n - 1)) return; | ||
| 578 | const auto& h = leonida::dstorage::hold_stats(); | ||
| 579 | std::lock_guard lock(log_mutex()); | ||
| 580 | std::fprintf(stderr, "[rt] ds-hold event=%s self_submits=%llu held_reads=%llu full_releases=%llu " | ||
| 581 | "timed_releases=%llu page0_entries=%llu capacity=%u held_now=%zu\n", event, | ||
| 582 | static_cast<unsigned long long>(h.self_submits.load()), | ||
| 583 | static_cast<unsigned long long>(h.held_reads.load()), | ||
| 584 | static_cast<unsigned long long>(h.full_releases.load()), | ||
| 585 | static_cast<unsigned long long>(h.timed_releases.load()), | ||
| 586 | static_cast<unsigned long long>(h.page0_entries.load()), capacity, held_now); | ||
| 587 | } | ||
| 588 | |||
| 545 | // ---- Queues ------------------------------------------------------------------------------------------------------- | 589 | // ---- Queues ------------------------------------------------------------------------------------------------------- |
| 546 | 590 | ||
| 547 | // Only sampled when request tracing is enabled. Times include work done by the file/decode worker. | 591 | // Only sampled when request tracing is enabled. Times include work done by the file/decode worker. |
| @@ -576,7 +620,10 @@ struct Entry { | |||
| 576 | 620 | ||
| 577 | class Queue final : public IDStorageQueueX2 { | 621 | class Queue final : public IDStorageQueueX2 { |
| 578 | public: | 622 | public: |
| 579 | explicit Queue(const DSTORAGE_QUEUE_DESC1& desc) : desc_(desc) { | 623 | explicit Queue(const DSTORAGE_QUEUE_DESC1& desc) |
| 624 | : desc_(desc), | ||
| 625 | hold_pages_(environment_number(L"LEONIDA_DSTORAGE_HOLD_PAGES", 1) != 0), | ||
| 626 | hold_release_ms_(environment_number(L"LEONIDA_DSTORAGE_HOLD_MS", kDefaultHoldMs)) { | ||
| 580 | error_event_ = CreateEventW(nullptr, TRUE, FALSE, nullptr); | 627 | error_event_ = CreateEventW(nullptr, TRUE, FALSE, nullptr); |
| 581 | clear_record_locked(); | 628 | clear_record_locked(); |
| 582 | std::lock_guard lock(stats().mutex); | 629 | std::lock_guard lock(stats().mutex); |
| @@ -781,15 +828,64 @@ private: | |||
| 781 | bool auto_submit() const { return !desc_.Options.AutoSubmitDisabled; } | 828 | bool auto_submit() const { return !desc_.Options.AutoSubmitDisabled; } |
| 782 | size_t used_locked() const { return entries_.size() + in_flight_; } | 829 | size_t used_locked() const { return entries_.size() + in_flight_; } |
| 783 | 830 | ||
| 831 | // A read into physical pages: its page list may not be filled before the title's Submit (hold_pages above). | ||
| 832 | static bool into_pages(const Entry& e) { | ||
| 833 | return e.type == Entry::Type::Request && (e.request.Options.AsUINT64 & options::kDestinationPages) != 0; | ||
| 834 | } | ||
| 835 | |||
| 836 | // The queue has reached half its capacity and holds nothing yet. Submits up to the first read into physical | ||
| 837 | // pages, which waits with everything behind it for the title's Submit. | ||
| 838 | void self_submit_locked() { | ||
| 839 | auto& h = leonida::dstorage::hold_stats(); | ||
| 840 | if (!hold_pages_) { | ||
| 841 | log_hold("self-submit", ++h.self_submits, desc_.Capacity, 0); | ||
| 842 | submit_locked(); | ||
| 843 | return; | ||
| 844 | } | ||
| 845 | size_t end = submitted_; | ||
| 846 | while (end < entries_.size() && !into_pages(entries_[end])) ++end; | ||
| 847 | if (end < entries_.size()) { | ||
| 848 | uint64_t reads = 0; | ||
| 849 | for (size_t i = end; i < entries_.size(); ++i) reads += into_pages(entries_[i]); | ||
| 850 | h.held_reads += reads; | ||
| 851 | holding_ = true; | ||
| 852 | hold_warned_ = false; | ||
| 853 | held_since_ = std::chrono::steady_clock::now(); | ||
| 854 | } | ||
| 855 | const size_t held_now = entries_.size() - end; | ||
| 856 | const bool hold = holding_; // submit_locked ends a hold when it submits everything | ||
| 857 | submit_locked(end); | ||
| 858 | holding_ = hold; | ||
| 859 | if (holding_) work_.notify_one(); // the worker starts timing the hold | ||
| 860 | log_hold("self-submit", ++h.self_submits, desc_.Capacity, held_now); | ||
| 861 | } | ||
| 862 | |||
| 784 | void enqueue(Entry&& e) { | 863 | void enqueue(Entry&& e) { |
| 785 | { | 864 | { |
| 786 | std::unique_lock lock(mutex_); | 865 | std::unique_lock lock(mutex_); |
| 787 | // A full queue waits for the worker to free a slot. With auto-submit on, half the queue is always | 866 | // A full queue waits for the worker to free a slot. With auto-submit on, half the queue was submitted |
| 788 | // submitted by then; with it off, only a Submit from another thread frees one (as the reference warns). | 867 | // by then, except what is held for the title's Submit: the title cannot make that call while it waits |
| 789 | space_.wait(lock, [&] { return closing_ || used_locked() < usable(); }); | 868 | // here, so a full queue submits the held entries too, page lists as they are. With auto-submit off, |
| 869 | // only a Submit from another thread frees a slot (as the reference warns). | ||
| 870 | space_.wait(lock, [&] { | ||
| 871 | if (closing_ || used_locked() < usable()) return true; | ||
| 872 | if (holding_) { | ||
| 873 | const size_t held_now = entries_.size() - submitted_; | ||
| 874 | submit_locked(); | ||
| 875 | log_hold("full-queue", ++leonida::dstorage::hold_stats().full_releases, desc_.Capacity, held_now); | ||
| 876 | log_once("full-hold", "a full queue submitted reads into physical pages before the title's " | ||
| 877 | "Submit (their page lists may not be filled yet)"); | ||
| 878 | } | ||
| 879 | return false; | ||
| 880 | }); | ||
| 790 | if (!closing_) { | 881 | if (!closing_) { |
| 791 | entries_.push_back(std::move(e)); | 882 | entries_.push_back(std::move(e)); |
| 792 | if (auto_submit() && entries_.size() - submitted_ >= half()) submit_locked(); | 883 | if (holding_) { |
| 884 | // Behind a held read: it waits with it, whatever it is. No second self-submit until the hold ends. | ||
| 885 | if (into_pages(entries_.back())) ++leonida::dstorage::hold_stats().held_reads; | ||
| 886 | } else if (auto_submit() && entries_.size() - submitted_ >= half()) { | ||
| 887 | self_submit_locked(); | ||
| 888 | } | ||
| 793 | return; | 889 | return; |
| 794 | } | 890 | } |
| 795 | } | 891 | } |
| @@ -801,10 +897,16 @@ private: | |||
| 801 | release_references(e); | 897 | release_references(e); |
| 802 | } | 898 | } |
| 803 | 899 | ||
| 804 | void submit_locked() { | 900 | // Submits the entries before "end". Without one it submits all of them and ends a hold: the title's Submit, a |
| 901 | // full queue, Close, a hold that ran out of time. | ||
| 902 | void submit_locked(size_t end = SIZE_MAX) { | ||
| 903 | if (end >= entries_.size()) { | ||
| 904 | end = entries_.size(); | ||
| 905 | holding_ = false; | ||
| 906 | } | ||
| 805 | uint32_t requests = 0; | 907 | uint32_t requests = 0; |
| 806 | uint64_t source_bytes = 0, destination_bytes = 0; | 908 | uint64_t source_bytes = 0, destination_bytes = 0; |
| 807 | for (size_t i = submitted_; i < entries_.size(); ++i) { | 909 | for (size_t i = submitted_; i < end; ++i) { |
| 808 | Entry& e = entries_[i]; | 910 | Entry& e = entries_[i]; |
| 809 | if (e.file) e.file_counted = e.file->add_pending(); | 911 | if (e.file) e.file_counted = e.file->add_pending(); |
| 810 | if (e.counted) { | 912 | if (e.counted) { |
| @@ -820,12 +922,49 @@ private: | |||
| 820 | p.TotalRequestSourceByteCount += source_bytes; | 922 | p.TotalRequestSourceByteCount += source_bytes; |
| 821 | p.TotalRequestDestinationByteCount += destination_bytes; | 923 | p.TotalRequestDestinationByteCount += destination_bytes; |
| 822 | } | 924 | } |
| 823 | if (entries_.size() > submitted_) { | 925 | if (end > submitted_) { |
| 824 | submitted_ = entries_.size(); | 926 | submitted_ = end; |
| 825 | work_.notify_one(); | 927 | work_.notify_one(); |
| 826 | } | 928 | } |
| 827 | } | 929 | } |
| 828 | 930 | ||
| 931 | // How long the worker sleeps before it looks at a hold again: until the time limit runs out, a quarter of a | ||
| 932 | // second at most (and exactly that without a limit, for the five-second line). | ||
| 933 | std::chrono::milliseconds hold_wait_locked() const { | ||
| 934 | long long ms = 250; | ||
| 935 | if (hold_release_ms_) { | ||
| 936 | const auto waited = std::chrono::duration_cast<std::chrono::milliseconds>( | ||
| 937 | std::chrono::steady_clock::now() - held_since_).count(); | ||
| 938 | ms = std::clamp<long long>(static_cast<long long>(hold_release_ms_) - waited, 1, 250); | ||
| 939 | } | ||
| 940 | return std::chrono::milliseconds(ms); | ||
| 941 | } | ||
| 942 | |||
| 943 | // On the worker while entries are held: a hold with a time limit ends when it runs out (a timed release), and | ||
| 944 | // one without says once that it has lasted five seconds (a title that never calls Submit would show up as this | ||
| 945 | // line and nothing after it). | ||
| 946 | void hold_tick_locked() { | ||
| 947 | if (!holding_) return; | ||
| 948 | const auto waited = std::chrono::duration_cast<std::chrono::milliseconds>( | ||
| 949 | std::chrono::steady_clock::now() - held_since_).count(); | ||
| 950 | const size_t held_now = entries_.size() - submitted_; | ||
| 951 | if (hold_release_ms_ && waited >= static_cast<long long>(hold_release_ms_)) { | ||
| 952 | submit_locked(); | ||
| 953 | log_hold("timed", ++leonida::dstorage::hold_stats().timed_releases, desc_.Capacity, held_now); | ||
| 954 | log_once("timed-hold", "timed release: reads into physical pages held for the title's Submit were " | ||
| 955 | "submitted after LEONIDA_DSTORAGE_HOLD_MS without one (their page lists may " | ||
| 956 | "not be filled; the ds-hold line counts these)"); | ||
| 957 | } else if (waited >= 5000 && !hold_warned_) { | ||
| 958 | hold_warned_ = true; | ||
| 959 | std::lock_guard lock(log_mutex()); | ||
| 960 | std::fprintf(stderr, "[rt] dstorage: %zu entries behind a read into physical pages have waited %lld ms " | ||
| 961 | "for the title's Submit (queue %p, capacity %u; LEONIDA_DSTORAGE_HOLD_MS=<n> " | ||
| 962 | "submits them after n ms, LEONIDA_DSTORAGE_HOLD_PAGES=0 never holds)\n", | ||
| 963 | held_now, static_cast<long long>(waited), static_cast<void*>(this), | ||
| 964 | unsigned(desc_.Capacity)); | ||
| 965 | } | ||
| 966 | } | ||
| 967 | |||
| 829 | struct Prepared { | 968 | struct Prepared { |
| 830 | Entry& entry; | 969 | Entry& entry; |
| 831 | HRESULT result = S_OK; | 970 | HRESULT result = S_OK; |
| @@ -859,7 +998,11 @@ private: | |||
| 859 | bool paired = false; | 998 | bool paired = false; |
| 860 | { | 999 | { |
| 861 | std::unique_lock lock(mutex_); | 1000 | std::unique_lock lock(mutex_); |
| 862 | work_.wait(lock, [&] { return submitted_ > 0 || closed_; }); | 1001 | while (!(submitted_ > 0 || closed_)) { |
| 1002 | if (!holding_) work_.wait(lock); | ||
| 1003 | else if (work_.wait_for(lock, hold_wait_locked()) == std::cv_status::timeout) | ||
| 1004 | hold_tick_locked(); | ||
| 1005 | } | ||
| 863 | if (submitted_ == 0) return; | 1006 | if (submitted_ == 0) return; |
| 864 | head = std::move(entries_.front()); | 1007 | head = std::move(entries_.front()); |
| 865 | entries_.pop_front(); | 1008 | entries_.pop_front(); |
| @@ -1088,6 +1231,10 @@ private: | |||
| 1088 | std::vector<uint64_t> numbers(1 + (buffer.size() - 1 + offset) / 65536); | 1231 | std::vector<uint64_t> numbers(1 + (buffer.size() - 1 + offset) / 65536); |
| 1089 | if (!guarded_move(numbers.data(), pages, numbers.size() * sizeof(numbers[0]))) | 1232 | if (!guarded_move(numbers.data(), pages, numbers.size() * sizeof(numbers[0]))) |
| 1090 | return E_DSTORAGE_ACCESS_VIOLATION; | 1233 | return E_DSTORAGE_ACCESS_VIOLATION; |
| 1234 | // Page 0 is never a title page: a destination list that names it was not filled when its request ran, and | ||
| 1235 | // the request fails below. Counted, since each one is data the title asked for and did not get. | ||
| 1236 | if (write && std::find(numbers.begin(), numbers.end(), uint64_t{0}) != numbers.end()) | ||
| 1237 | log_hold("page0", ++leonida::dstorage::hold_stats().page0_entries, 0, 0); | ||
| 1091 | if (rt::xmem::transfer_physical(numbers, offset, buffer, write)) return S_OK; | 1238 | if (rt::xmem::transfer_physical(numbers, offset, buffer, write)) return S_OK; |
| 1092 | return HRESULT_FROM_WIN32(GetLastError()); | 1239 | return HRESULT_FROM_WIN32(GetLastError()); |
| 1093 | } | 1240 | } |
| @@ -1305,6 +1452,11 @@ private: | |||
| 1305 | std::condition_variable work_, space_, idle_; | 1452 | std::condition_variable work_, space_, idle_; |
| 1306 | std::deque<Entry> entries_; | 1453 | std::deque<Entry> entries_; |
| 1307 | size_t submitted_ = 0; // the first submitted_ entries are submitted | 1454 | size_t submitted_ = 0; // the first submitted_ entries are submitted |
| 1455 | const bool hold_pages_; // LEONIDA_DSTORAGE_HOLD_PAGES | ||
| 1456 | const uint32_t hold_release_ms_; // LEONIDA_DSTORAGE_HOLD_MS (kDefaultHoldMs unless set); 0: no time limit | ||
| 1457 | bool holding_ = false; // under mutex_: a self-submit left entries for the title's Submit | ||
| 1458 | bool hold_warned_ = false; // under mutex_: this hold's five-second line was written | ||
| 1459 | std::chrono::steady_clock::time_point held_since_{}; // under mutex_ | ||
| 1308 | size_t in_flight_ = 0; | 1460 | size_t in_flight_ = 0; |
| 1309 | bool closing_ = false; | 1461 | bool closing_ = false; |
| 1310 | bool closed_ = false; | 1462 | bool closed_ = false; |