|
2 | 2 | // Licensed under the MIT license. |
3 | 3 |
|
4 | 4 | using System.Diagnostics; |
| 5 | +using System.Runtime.InteropServices; |
5 | 6 | using System.Threading; |
6 | 7 | using System.Threading.Tasks; |
7 | 8 | using NUnit.Framework; |
@@ -110,5 +111,153 @@ void Report(string name, System.Func<int, double> run) |
110 | 111 | Report("mimalloc, UNTRACKED", th => { SectorAlignedBufferPool.NativeAllocator = new UntrackedMimallocAllocator(); return RunShared(th); }); |
111 | 112 | SectorAlignedBufferPool.NativeAllocator = null; |
112 | 113 | } |
| 114 | + |
| 115 | + [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] |
| 116 | + static nint RunRaw(int variant, long n) |
| 117 | + { |
| 118 | + nint sink = 0; |
| 119 | + switch (variant) |
| 120 | + { |
| 121 | + case 0: for (long i = 0; i < n; i++) { var p = Mimalloc.Malloc((nuint)BufferSize); sink ^= p; Mimalloc.Free(p); } break; |
| 122 | + case 1: for (long i = 0; i < n; i++) { var p = Mimalloc.MallocFast((nuint)BufferSize); sink ^= p; Mimalloc.FreeFast(p); } break; |
| 123 | + case 2: for (long i = 0; i < n; i++) { var p = Mimalloc.MallocAligned((nuint)BufferSize, (nuint)SectorSize); sink ^= p; Mimalloc.Free(p); } break; |
| 124 | + case 3: for (long i = 0; i < n; i++) { var p = Mimalloc.MallocAlignedFast((nuint)BufferSize, (nuint)SectorSize); sink ^= p; Mimalloc.FreeFast(p); } break; |
| 125 | + } |
| 126 | + return sink; |
| 127 | + } |
| 128 | + |
| 129 | + static double RawNs(int variant, long n) |
| 130 | + { |
| 131 | + _ = RunRaw(variant, n / 10); // warm up |
| 132 | + var sw = Stopwatch.StartNew(); |
| 133 | + var sink = RunRaw(variant, n); |
| 134 | + sw.Stop(); |
| 135 | + System.GC.KeepAlive(sink); |
| 136 | + return sw.Elapsed.TotalSeconds / n * 1e9; |
| 137 | + } |
| 138 | + |
| 139 | + [Test] |
| 140 | + public void SingleThreadBreakdown() |
| 141 | + { |
| 142 | + if (!Mimalloc.TryInitialize()) |
| 143 | + Assert.Ignore("mimalloc native library not available for this RID"); |
| 144 | + |
| 145 | + // Alignment probe: is plain mi_malloc(4096) already sector/page aligned (would let us skip the |
| 146 | + // aligned slow path)? |
| 147 | + const int nchk = 100_000; |
| 148 | + var ptrs = new nint[nchk]; |
| 149 | + int a512 = 0, a4096 = 0; |
| 150 | + for (var i = 0; i < nchk; i++) { var p = Mimalloc.Malloc((nuint)BufferSize); ptrs[i] = p; if ((p & (SectorSize - 1)) == 0) a512++; if ((p & 4095) == 0) a4096++; } |
| 151 | + for (var i = 0; i < nchk; i++) Mimalloc.Free(ptrs[i]); |
| 152 | + |
| 153 | + const long n = 20_000_000; |
| 154 | + double managedReuseNs = 1000.0 / RunShared(1); // Mops/s -> ns/op |
| 155 | + |
| 156 | + double rawMalloc = RawNs(0, n); |
| 157 | + double rawMallocFast = RawNs(1, n); |
| 158 | + double rawAligned = RawNs(2, n); |
| 159 | + double rawAlignedFast = RawNs(3, n); |
| 160 | + |
| 161 | + SectorAlignedBufferPool.NativeAllocator = new UntrackedMimallocAllocator(); |
| 162 | + double poolUntrackedNs = 1000.0 / RunShared(1); |
| 163 | + SectorAlignedBufferPool.NativeAllocator = new MimallocPooledAllocator(); |
| 164 | + double poolTrackedNs = 1000.0 / RunShared(1); |
| 165 | + SectorAlignedBufferPool.NativeAllocator = null; |
| 166 | + |
| 167 | + TestContext.Progress.WriteLine($"plain mi_malloc(4096) alignment: 512-aligned {a512}/{nchk}, 4096-aligned {a4096}/{nchk}"); |
| 168 | + TestContext.Progress.WriteLine(""); |
| 169 | + TestContext.Progress.WriteLine($"{"single-thread cost (ns/op)",-40} | {"ns/op",8}"); |
| 170 | + TestContext.Progress.WriteLine(new string('-', 52)); |
| 171 | + TestContext.Progress.WriteLine($"{"managed pool reuse (floor)",-40} | {managedReuseNs,8:F1}"); |
| 172 | + TestContext.Progress.WriteLine($"{"raw mi_malloc + mi_free (normal xition)",-40} | {rawMalloc,8:F1}"); |
| 173 | + TestContext.Progress.WriteLine($"{"raw mi_malloc + mi_free (SuppressGC)",-40} | {rawMallocFast,8:F1}"); |
| 174 | + TestContext.Progress.WriteLine($"{"raw mi_malloc_ALIGNED + free (normal)",-40} | {rawAligned,8:F1}"); |
| 175 | + TestContext.Progress.WriteLine($"{"raw mi_malloc_ALIGNED + free (SuppressGC)",-40} | {rawAlignedFast,8:F1}"); |
| 176 | + TestContext.Progress.WriteLine($"{"pool, mimalloc UNTRACKED (aligned,normal)",-40} | {poolUntrackedNs,8:F1}"); |
| 177 | + TestContext.Progress.WriteLine($"{"pool, mimalloc tracked (current)",-40} | {poolTrackedNs,8:F1}"); |
| 178 | + TestContext.Progress.WriteLine(""); |
| 179 | + TestContext.Progress.WriteLine($"GC-transition cost (2 calls/op): normal-vs-fast plain = {rawMalloc - rawMallocFast,6:F1} ns/op"); |
| 180 | + TestContext.Progress.WriteLine($"GC-transition cost (2 calls/op): normal-vs-fast aligned = {rawAligned - rawAlignedFast,6:F1} ns/op"); |
| 181 | + TestContext.Progress.WriteLine($"alignment slow-path cost (normal): aligned - plain = {rawAligned - rawMalloc,6:F1} ns/op"); |
| 182 | + TestContext.Progress.WriteLine($"alignment slow-path cost (fast): aligned - plain = {rawAlignedFast - rawMallocFast,6:F1} ns/op"); |
| 183 | + TestContext.Progress.WriteLine($"pool + wrapper overhead: poolUntracked - rawAligned = {poolUntrackedNs - rawAligned,6:F1} ns/op"); |
| 184 | + TestContext.Progress.WriteLine($"tracker overhead: poolTracked - poolUntracked = {poolTrackedNs - poolUntrackedNs,6:F1} ns/op"); |
| 185 | + } |
| 186 | + |
| 187 | + // ---- Physical-memory / page-fault verification ---- |
| 188 | + |
| 189 | + [DllImport("libc", SetLastError = true)] |
| 190 | + static extern int getrusage(int who, byte[] usage); |
| 191 | + |
| 192 | + // struct rusage on Linux/x86-64: ru_minflt is the 9th long (offset 64), ru_majflt the 10th (offset 72). |
| 193 | + static (long minor, long major) Faults() |
| 194 | + { |
| 195 | + var b = new byte[144]; |
| 196 | + if (getrusage(0, b) != 0) |
| 197 | + return (0, 0); |
| 198 | + return (System.BitConverter.ToInt64(b, 64), System.BitConverter.ToInt64(b, 72)); |
| 199 | + } |
| 200 | + |
| 201 | + static void PoolLoop(SectorAlignedBufferPool pool, long n, bool clearOnReturn, int touch) |
| 202 | + { |
| 203 | + for (long i = 0; i < n; i++) |
| 204 | + { |
| 205 | + var page = pool.Get(BufferSize, clearOnReturn); |
| 206 | + if (touch == 2) |
| 207 | + { |
| 208 | + page.aligned_pointer[0] = 1; |
| 209 | + page.aligned_pointer[BufferSize - 1] = 1; |
| 210 | + } |
| 211 | + else if (touch < 0) |
| 212 | + { |
| 213 | + new System.Span<byte>(page.aligned_pointer, BufferSize).Fill(1); // simulate real IO writing the whole buffer |
| 214 | + } |
| 215 | + page.Return(); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + [Test] |
| 220 | + public void PhysicalMemoryCheck() |
| 221 | + { |
| 222 | + if (!Mimalloc.TryInitialize()) |
| 223 | + Assert.Ignore("mimalloc native library not available for this RID"); |
| 224 | + |
| 225 | + const long n = 5_000_000; |
| 226 | + |
| 227 | + (double ns, long minflt, long majflt) Measure(INativePinnedAllocator alloc, bool clearOnReturn, int touch) |
| 228 | + { |
| 229 | + SectorAlignedBufferPool.NativeAllocator = alloc; |
| 230 | + var pool = new SectorAlignedBufferPool(1, SectorSize); |
| 231 | + PoolLoop(pool, n / 20, clearOnReturn, touch); // warm up (fault in the reused block) |
| 232 | + var (min0, maj0) = Faults(); |
| 233 | + var sw = Stopwatch.StartNew(); |
| 234 | + PoolLoop(pool, n, clearOnReturn, touch); |
| 235 | + sw.Stop(); |
| 236 | + var (min1, maj1) = Faults(); |
| 237 | + pool.Free(); |
| 238 | + SectorAlignedBufferPool.NativeAllocator = null; |
| 239 | + return (sw.Elapsed.TotalSeconds / n * 1e9, min1 - min0, maj1 - maj0); |
| 240 | + } |
| 241 | + |
| 242 | + INativePinnedAllocator Managed() => null; |
| 243 | + INativePinnedAllocator Native() => new MimallocPooledAllocator(); |
| 244 | + |
| 245 | + TestContext.Progress.WriteLine($"N = {n:N0} ops/scenario. minflt/majflt = page faults during the measured loop (NOT warmup)."); |
| 246 | + TestContext.Progress.WriteLine($"{"scenario",-46} | {"ns/op",7} | {"minflt",8} | {"majflt",7} | flt/op"); |
| 247 | + TestContext.Progress.WriteLine(new string('-', 90)); |
| 248 | + |
| 249 | + void Row(string name, INativePinnedAllocator alloc, bool clr, int touch) |
| 250 | + { |
| 251 | + var r = Measure(alloc, clr, touch); |
| 252 | + TestContext.Progress.WriteLine($"{name,-46} | {r.ns,7:F1} | {r.minflt,8:N0} | {r.majflt,7:N0} | {(double)r.minflt / n,6:F4}"); |
| 253 | + } |
| 254 | + |
| 255 | + Row("managed pool, clr=false, touch 2 bytes", Managed(), false, 2); |
| 256 | + Row("mimalloc pool, clr=false, touch 2 bytes", Native(), false, 2); |
| 257 | + Row("managed pool, clr=false, touch FULL 4KB", Managed(), false, -1); |
| 258 | + Row("mimalloc pool, clr=false, touch FULL 4KB", Native(), false, -1); |
| 259 | + Row("managed pool, clr=TRUE (zeroed), touch 2", Managed(), true, 2); |
| 260 | + Row("mimalloc pool, clr=TRUE (mi_zalloc), touch 2", Native(), true, 2); |
| 261 | + } |
113 | 262 | } |
114 | 263 | } |
0 commit comments