// (Triforce, 11-26) A report word the sets hold already is not noted again: the words memory_cpu_owned was given before, // answered from a table at the note. // // The replay writes every counter report into guest memory on the CPU and notes its two words (vk_memory.cpp // memory_cpu_owned), so that a mirror's write-back leaves them out (the one-frame dark flashes of 11-07). The same report // words come every frame, and each was pushed onto cpu_words_new again, then sorted, made unique and searched for in both // sets at the next part's end with write-backs (cpu_words_merge), only to be found there and dropped: 1.6% of the // Presentation Thread at Lake Hylia and 2.5% at the Market on v0.8.3 (the sort, the two binary searches a word). // // The table says of a word "it is in cpu_words, cpu_words_recent or cpu_words_new": such a word is not pushed again, and // the sets end up the same, since the merge would have dropped it. What makes that exact: // - an entry holds the WHOLE address and the sets' generation, and both are compared. Two words that share an entry // push each other out, and the one pushed out is noted again as before (the merge drops it): a shared entry costs a // push, never a word. A hash alone would call a word known that the sets do not hold, the write-back would copy the // mirror's stale word over it, and the dark flashes would be back; // - a word is marked only when it is pushed, and words leave the sets in one place only, memory_cpu_owned's clear of // both past 65,536 words: forget() is called there, in the same statement, and makes every entry stale at once; // - all of it under cpu_words_mu, like the sets. // The clear comes at the same note as before: it needs more than 65,536 different words held, and what the sizes add up // to is never less than that with or without the repeats. Which of the two sets a word is in can differ for a while // (the merges come at other times); the two together, and so the cut runs, do not (tests/test_cpuwordsseen.py runs the // notes and the cuts with the table off, on and checking, against a std::set). // // The switch: RX_VK_CPU_WORDS_SEEN=0 notes every word again, as before; =check notes every word as before and looks each // word the table calls known up in the sets (a word held nowhere is a mismatch, counted in the mirror syncs' status // line); any other value is on, as unset, and logged when not 1. The lab knob nowordsseen (RX_VK_LAB_AB) notes every // word again in its phases; the table stays right through them, since a word noted past it is only one more the table // does not know. Without mirrors (wb_notes_off: Intel's integrated GPUs, RX_VK_MIRROR=0) or with RX_VK_WB_REPORTS set // no report word is noted: the switch is not read or logged and the four counts stay 0. #pragma once #include #include #include #include #include #include #include namespace cpu_words_seen { enum Mode { OFF = 0, ON = 1, CHECK_BOTH = 2 }; /// RX_VK_CPU_WORDS_SEEN's value: not set, "" or any other value is on, "0" is off, "check" checks inline int mode(const char* env) { if (!env || !*env) return ON; if (!strcmp(env, "check")) return CHECK_BOTH; return env[0] == '0' && !env[1] ? OFF : ON; } /// The mode of one note: the lab knob nowordsseen turns the table off in its phases inline int mode_now(int env, bool knob) { return knob ? OFF : env; } // (the sets hold up to 65,536 words: twice that in entries, 2 MB of zeroes until a word is marked, so that few words // share one. A frame's report words touch a cache line of it each: a few hundred KB for a few thousand words) constexpr int kSlotBits = 17; constexpr size_t kSlots = size_t(1) << kSlotBits; struct Table { struct Entry { uint64_t word, stamp; // (the word's whole address; the generation it was marked in, plus one: 0 is no word) }; Entry e[kSlots]; uint64_t forgets = 0; // (the sets' generation: how often they were cleared) // (for the status line, read without the lock: written only under cpu_words_mu, so a load and a store do) std::atomic st_noted{0}, st_known{0}, st_forgets{0}, st_mismatch{0}; static void count(std::atomic& c) { c.store(c.load(std::memory_order_relaxed) + 1, std::memory_order_relaxed); } /// The entry of a word (words are 8 bytes apart) static size_t slot(uint64_t word) { return (size_t)(((word >> 3) * 0x9E3779B97F4A7C15ull) >> (64 - kSlotBits)); } /// The word was marked since the last forget() and nothing has taken its entry bool known(uint64_t word) const { const Entry& x = e[slot(word)]; return x.stamp == forgets + 1 && x.word == word; } void mark(uint64_t word) { e[slot(word)] = {word, forgets + 1}; } /// The sets were cleared: no word is known any more void forget() { forgets++, count(st_forgets); } }; /// (check) The word is in one of the sets, or noted since the last merge inline bool held(uint64_t word, const std::vector& big, const std::vector& recent, const std::vector& noted) { return std::binary_search(big.begin(), big.end(), word) || std::binary_search(recent.begin(), recent.end(), word) || std::find(noted.begin(), noted.end(), word) != noted.end(); } /// One word of a note: whether to push it onto the words noted since the last merge (holding the sets' lock). /// Off: always, the table untouched. On: unless the table knows it; a word pushed is marked. Checking: always, as /// before, and a word the table knows must be held (big and recent: the two sorted sets; noted: since the last merge) inline bool push(Table& t, int mode, uint64_t word, const std::vector& big, const std::vector& recent, const std::vector& noted) { if (mode == OFF) return true; Table::count(t.st_noted); if (!t.known(word)) return t.mark(word), true; Table::count(t.st_known); if (mode == ON) return false; if (!held(word, big, recent, noted)) Table::count(t.st_mismatch); return true; } } // namespace cpu_words_seen