Add stress test for memory allocation and free on D3D12

It may not be the root cause of CTS memory leak, but we found some
issues in D3D12 memory allocation for large buffers (> 4M).

The memory allocation is not freed correctly when we call buffer
destroy. When we create a large buffer, it always allocate committed
resources from the system memory and append it to LRU cache, and will
try to free the expired memory in LRU cache when the left memory size
is not enough for the current allocation. When we call buffer destroy,
we remove its heap from LRU cache, release its allocation, but will
add the allocation reference to a delete queue and wait to be freed
together in the next device.tick. At this time, the destroyed memory is
not really released to the system memory. If no tick is triggered during
the buffer allocation and deallocation, these memories will never be
reused again and there will be a memory leak. We need to free these
memories when we detect there is not enough memory, do not wait for
device.tick to do that.

Add a stress case to track the issue, and need to discuss how to fix it.

Bug: dawn:957
Change-Id: Id4bcc97496d372ad7c27c31675327ca12e133217
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/55740
Commit-Queue: Hao Li <hao.x.li@intel.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/tests/BUILD.gn b/src/tests/BUILD.gn
index d7046df..5410642 100644
--- a/src/tests/BUILD.gn
+++ b/src/tests/BUILD.gn
@@ -320,6 +320,7 @@
     "end2end/FirstIndexOffsetTests.cpp",
     "end2end/GpuMemorySynchronizationTests.cpp",
     "end2end/IndexFormatTests.cpp",
+    "end2end/MemoryAllocationStressTests.cpp",
     "end2end/MultisampledRenderingTests.cpp",
     "end2end/MultisampledSamplingTests.cpp",
     "end2end/NonzeroBufferCreationTests.cpp",
diff --git a/src/tests/end2end/MemoryAllocationStressTests.cpp b/src/tests/end2end/MemoryAllocationStressTests.cpp
new file mode 100644
index 0000000..c72a5b8
--- /dev/null
+++ b/src/tests/end2end/MemoryAllocationStressTests.cpp
@@ -0,0 +1,45 @@
+// Copyright 2021 The Dawn Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "tests/DawnTest.h"
+
+class MemoryAllocationStressTests : public DawnTest {};
+
+// Test memory allocation is freed correctly when creating and destroying large buffers.
+// It will consume a total of 100G of memory, 1G each time. Expect not to trigger out of memory on
+// devices with gpu memory less than 100G.
+TEST_P(MemoryAllocationStressTests, LargeBuffer) {
+    // TODO(crbug.com/dawn/957): Memory leak on D3D12, the memory of destroyed buffer cannot be
+    // released.
+    DAWN_TEST_UNSUPPORTED_IF(IsD3D12());
+
+    // TODO(crbug.com/dawn/957): Check whether it can be reproduced on each backend.
+    DAWN_TEST_UNSUPPORTED_IF(IsMetal() || IsOpenGL() || IsOpenGLES() || IsVulkan());
+
+    uint32_t count = 100;
+    for (uint32_t i = 0; i < count; i++) {
+        wgpu::BufferDescriptor descriptor;
+        descriptor.size = 1024 * 1024 * 1024;  // 1G
+        descriptor.usage = wgpu::BufferUsage::Storage;
+        wgpu::Buffer buffer = device.CreateBuffer(&descriptor);
+        buffer.Destroy();
+    }
+}
+
+DAWN_INSTANTIATE_TEST(MemoryAllocationStressTests,
+                      D3D12Backend(),
+                      MetalBackend(),
+                      OpenGLBackend(),
+                      OpenGLESBackend(),
+                      VulkanBackend());