public static unsafe void VisitProcesses(ResizeableBuffer resizeableBuffer, SystemProcInfoVisitor visit) { var size = 100 * 1024; while (true) { var buffer = resizeableBuffer.Get(ref size, false); fixed(byte *ptr = buffer) { var status = NtDll.NtQuerySystemInformation( SYSTEM_INFORMATION_CLASS.SystemProcessInformation, (IntPtr)ptr, buffer.Length, out size); if (status == NtStatus.STATUS_INFO_LENGTH_MISMATCH) { continue; } if (!status.IsSuccessful()) { throw new InvalidOperationException($"Can't query SystemProcessInformation. NtStatus: 0x{status:X}"); } VisitProcesses(ptr, buffer, visit); return; } } }
public void Should_not_recreate_large_buffers_with_opt_in(int size1, int size2) { var buffer = new ResizeableBuffer(); var arr1 = buffer.Get(size1); var arr2 = buffer.Get(size2, false); arr2.Should().BeSameAs(arr1); }
public void Should_recreate_large_buffers(int size1, int size2) { var buffer = new ResizeableBuffer(); var arr1 = buffer.Get(size1); var arr2 = buffer.Get(size2); arr2.Should().NotBeSameAs(arr1); }
public void Should_return_array_with_enough_size(int size, int step) { var buffer = new ResizeableBuffer(); for (var i = 0; i <= size; i++) { buffer.Get(i).Length.Should().BeGreaterOrEqualTo(i); } }
private static unsafe void GetTcpTableWithPid(AddressFamily addressFamily, ResizeableBuffer resizeableBuffer) { var size = EstimateBufferSize(addressFamily, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL); var buffer = resizeableBuffer.Get(ref size); while (true) { fixed(byte *b = buffer) { var result = IPHlpApi.GetExtendedTcpTable(b, ref size, false, addressFamily, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0); if (result == (int)ErrorCodes.ERROR_INSUFFICIENT_BUFFER) { buffer = resizeableBuffer.Get(ref size); continue; } if (result != 0) { Win32ExceptionUtility.Throw(result); } var itemSize = addressFamily == AddressFamily.AF_INET ? sizeof(MIB_TCPROW_OWNER_PID) : sizeof(MIB_TCP6ROW_OWNER_PID); var itemCount = *(int *)b; if (itemCount * itemSize + 4 > buffer.Length) { throw new InvalidOperationException($"Buffer overflow check failed. ItemCount: {itemCount}, ItemSize: {itemSize}, BufferSize: {buffer.Length}"); } return; } } }
private NativeThreadsMeterCache() { sync = new object(); cachedBuffer = new ResizeableBuffer(); threadsCountCache = new TimeCache <Dictionary <int, int> >(BuildCache, () => TimeSpan.FromSeconds(1)); }
public ProcessHelper() { buffer = new ResizeableBuffer(); sync = new object(); }
public void Should_not_waste_memory_with_large_arrays(int size) { var buffer = new ResizeableBuffer(); buffer.Get(size).Length.Should().BeLessThan(2 * size); }