diff options
Diffstat (limited to 'runtime/test/xmem_test.cpp')
| -rw-r--r-- | runtime/test/xmem_test.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index 1ad4080f7..bd38cab39 100644 --- a/runtime/test/xmem_test.cpp +++ b/runtime/test/xmem_test.cpp | |||
| @@ -10,8 +10,12 @@ | |||
| 10 | #include <cstdint> | 10 | #include <cstdint> |
| 11 | #include <cstdio> | 11 | #include <cstdio> |
| 12 | #include <cstring> | 12 | #include <cstring> |
| 13 | #include <fstream> | ||
| 14 | #include <sstream> | ||
| 15 | #include <string> | ||
| 13 | 16 | ||
| 14 | #include "shims.h" | 17 | #include "shims.h" |
| 18 | #include "startup_trace.h" | ||
| 15 | 19 | ||
| 16 | namespace { | 20 | namespace { |
| 17 | 21 | ||
| @@ -79,10 +83,26 @@ void test_working_set(XMemGetWorkingSetStatisticsFn get_stats) { | |||
| 79 | const BOOL ok = get_stats(kMeasuredKind, stats); | 83 | const BOOL ok = get_stats(kMeasuredKind, stats); |
| 80 | check(ok != FALSE, "XMemGetWorkingSetStatistics(kind, 64-byte out) returns TRUE"); | 84 | check(ok != FALSE, "XMemGetWorkingSetStatistics(kind, 64-byte out) returns TRUE"); |
| 81 | 85 | ||
| 86 | uint64_t total_memory = 0; | ||
| 87 | std::memcpy(&total_memory, stats + 0, sizeof(total_memory)); | ||
| 88 | check(ok != FALSE && total_memory != 0, "XMemGetWorkingSetStatistics writes a non-zero total memory at +0"); | ||
| 89 | |||
| 90 | MEMORYSTATUSEX ms{sizeof(ms)}; | ||
| 91 | if (GlobalMemoryStatusEx(&ms) && ms.ullTotalPhys != 0) { | ||
| 92 | check(ok != FALSE && total_memory == ms.ullTotalPhys, | ||
| 93 | "XMemGetWorkingSetStatistics total memory (+0) matches GlobalMemoryStatusEx ullTotalPhys"); | ||
| 94 | } else { | ||
| 95 | check(ok != FALSE && total_memory == 12ULL * 1024 * 1024 * 1024, | ||
| 96 | "XMemGetWorkingSetStatistics total memory (+0) falls back to 12 GiB title limit"); | ||
| 97 | } | ||
| 98 | |||
| 82 | uint64_t working_set = 0; | 99 | uint64_t working_set = 0; |
| 83 | std::memcpy(&working_set, stats + 8, sizeof(working_set)); | 100 | std::memcpy(&working_set, stats + 8, sizeof(working_set)); |
| 84 | check(ok != FALSE && working_set != 0, "XMemGetWorkingSetStatistics writes a non-zero size at +8"); | 101 | check(ok != FALSE && working_set != 0, "XMemGetWorkingSetStatistics writes a non-zero size at +8"); |
| 85 | 102 | ||
| 103 | check(ok != FALSE && total_memory >= working_set, | ||
| 104 | "XMemGetWorkingSetStatistics total memory (+0) >= working set (+8)"); | ||
| 105 | |||
| 86 | bool tail_untouched = true; | 106 | bool tail_untouched = true; |
| 87 | for (size_t i = 16; i < sizeof(stats); ++i) { | 107 | for (size_t i = 16; i < sizeof(stats); ++i) { |
| 88 | if (stats[i] != 0xa5) { | 108 | if (stats[i] != 0xa5) { |
| @@ -204,6 +224,48 @@ void test_virtual_alloc(XMemVirtualAllocFn virtual_alloc, XMemGetWriteWatchFn ge | |||
| 204 | "nonzero XMem allocation attributes are rejected before host allocation"); | 224 | "nonzero XMem allocation attributes are rejected before host allocation"); |
| 205 | } | 225 | } |
| 206 | 226 | ||
| 227 | // gta6rt --trace: one "xmem_attributes" event per distinct raw attribute word and API, none with the trace off. | ||
| 228 | void test_attribute_trace(XMemAllocFn alloc, XMemFreeFn free_mem) { | ||
| 229 | wchar_t temp[MAX_PATH]; | ||
| 230 | GetTempPathW(MAX_PATH, temp); | ||
| 231 | const std::wstring path = | ||
| 232 | std::wstring(temp) + L"xmem_trace_test_" + std::to_wstring(GetCurrentProcessId()) + L".jsonl"; | ||
| 233 | DeleteFileW(path.c_str()); | ||
| 234 | |||
| 235 | void* untraced = alloc(64, 0x1111); // trace not open yet: nothing to write, nothing to crash | ||
| 236 | check(untraced != nullptr, "XMemAlloc with the trace off still allocates"); | ||
| 237 | free_mem(untraced, 0x1111); | ||
| 238 | |||
| 239 | check(rt::startup_trace::open(path.c_str()), "open the startup trace file"); | ||
| 240 | void* a = alloc(4096, 0x5e50000); | ||
| 241 | void* b = alloc(8192, 0x5e50000); // same word again: no second event | ||
| 242 | void* c = alloc(16, 0x99); | ||
| 243 | free_mem(a, 0x5e50000); | ||
| 244 | free_mem(b, 0x5e50000); // same word again: no second event | ||
| 245 | free_mem(c, 0x99); | ||
| 246 | free_mem(nullptr, 0x77); // a free-only word is recorded too | ||
| 247 | rt::startup_trace::close(); | ||
| 248 | |||
| 249 | std::ifstream in(path); | ||
| 250 | std::stringstream text; | ||
| 251 | text << in.rdbuf(); | ||
| 252 | const std::string log = text.str(); | ||
| 253 | auto count = [&](const std::string& needle) { | ||
| 254 | int n = 0; | ||
| 255 | for (size_t at = log.find(needle); at != std::string::npos; at = log.find(needle, at + 1)) ++n; | ||
| 256 | return n; | ||
| 257 | }; | ||
| 258 | check(count("\"kind\":\"xmem_attributes\"") == 5, "five events: two XMemAlloc words and three XMemFree words"); | ||
| 259 | check(count("\"api\":\"XMemAlloc\",\"attributes\":\"0x5e50000\",\"size\":4096") == 1, | ||
| 260 | "XMemAlloc 0x5e50000 is recorded once, with the size of the first call"); | ||
| 261 | check(count("\"api\":\"XMemAlloc\",\"attributes\":\"0x99\",\"size\":16") == 1, "XMemAlloc 0x99 is recorded once"); | ||
| 262 | check(count("\"api\":\"XMemFree\",\"attributes\":\"0x5e50000\"") == 1, "XMemFree 0x5e50000 is recorded once"); | ||
| 263 | check(count("\"api\":\"XMemFree\",\"attributes\":\"0x77\"") == 1, "XMemFree 0x77 is recorded"); | ||
| 264 | check(count("\"attributes\":\"0x1111\"") == 0, "nothing was recorded while the trace was off"); | ||
| 265 | check(count("\"caller\":\"") == 5, "every event names its caller"); | ||
| 266 | DeleteFileW(path.c_str()); | ||
| 267 | } | ||
| 268 | |||
| 207 | using HostVirtualAllocFn = void*(WINAPI*)(void*, SIZE_T, DWORD, DWORD); | 269 | using HostVirtualAllocFn = void*(WINAPI*)(void*, SIZE_T, DWORD, DWORD); |
| 208 | 270 | ||
| 209 | // RT-217: a title's kernel32!VirtualAlloc carries the console's MEM_GRAPHICS / MEM_TITLE type bits, which the host | 271 | // RT-217: a title's kernel32!VirtualAlloc carries the console's MEM_GRAPHICS / MEM_TITLE type bits, which the host |
| @@ -277,6 +339,7 @@ int main() { | |||
| 277 | if (virtual_alloc && get_write_watch && reset_write_watch) { | 339 | if (virtual_alloc && get_write_watch && reset_write_watch) { |
| 278 | test_virtual_alloc(virtual_alloc, get_write_watch, reset_write_watch); | 340 | test_virtual_alloc(virtual_alloc, get_write_watch, reset_write_watch); |
| 279 | } | 341 | } |
| 342 | test_attribute_trace(alloc, free_mem); | ||
| 280 | 343 | ||
| 281 | test_title_virtual_alloc(); | 344 | test_title_virtual_alloc(); |
| 282 | 345 | ||