private static SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] ManagedGetLogicalProcessorInformation() { uint ReturnLength = 0; GetLogicalProcessorInformation(IntPtr.Zero, ref ReturnLength); if (Marshal.GetLastWin32Error() != ERROR_INSUFFICIENT_BUFFER) { return(null); } IntPtr Ptr = Marshal.AllocHGlobal((int)ReturnLength); try { if (GetLogicalProcessorInformation(Ptr, ref ReturnLength)) { int size = Marshal.SizeOf <SYSTEM_LOGICAL_PROCESSOR_INFORMATION>(); int len = (int)ReturnLength / size; SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] Buffer = new SYSTEM_LOGICAL_PROCESSOR_INFORMATION[len]; IntPtr Item = Ptr; for (int i = 0; i < len; i++) { Buffer[i] = Marshal.PtrToStructure <SYSTEM_LOGICAL_PROCESSOR_INFORMATION>(Item); Item += size; } return(Buffer); } } finally { Marshal.FreeHGlobal(Ptr); } return(null); }
public static SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] GetLogicalProcessorInformationMarshal() { uint ReturnLength = 0; GetLogicalProcessorInformation(IntPtr.Zero, ref ReturnLength); if (Marshal.GetLastWin32Error() != ERROR_INSUFFICIENT_BUFFER) { throw new Exception("GetLogicalProcessorInformationがERROR_INSUFFICIENT_BUFFERを返さなかった"); } IntPtr Ptr = Marshal.AllocHGlobal((int)ReturnLength); try { if (GetLogicalProcessorInformation(Ptr, ref ReturnLength) == false) { throw new Exception("GetLogicalProcessorInformationに失敗"); } int size = Marshal.SizeOf(typeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION)); int len = (int)ReturnLength / size; SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] Buffer = new SYSTEM_LOGICAL_PROCESSOR_INFORMATION[len]; IntPtr Item = Ptr; for (int i = 0; i < len; i++) { Buffer[i] = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION)Marshal.PtrToStructure(Item, typeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION)); Item += size; } return(Buffer); } finally { Marshal.FreeHGlobal(Ptr); } }
public static SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] GetLogicalProcessorInformation() { uint ReturnLength = 0; GetLogicalProcessorInformation(IntPtr.Zero, ref ReturnLength); if (Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER) { IntPtr Ptr = Marshal.AllocHGlobal((int)ReturnLength); try { if (GetLogicalProcessorInformation(Ptr, ref ReturnLength)) { int size = Marshal.SizeOf(typeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION)); int len = (int)ReturnLength / size; var buffer = new SYSTEM_LOGICAL_PROCESSOR_INFORMATION[len]; IntPtr Item = Ptr; for (int i = 0; i < len; i++) { buffer[i] = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION)Marshal.PtrToStructure(Item, typeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION)); Item += size; } return(buffer); } } finally { Marshal.FreeHGlobal(Ptr); } } return(null); }
private static SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] ManagedGetLogicalProcessorInformation() { var ReturnLength = 0u; GetLogicalProcessorInformation(IntPtr.Zero, ref ReturnLength); if (Marshal.GetLastWin32Error() != ERROR_INSUFFICIENT_BUFFER) { return(null); } var pointer = Marshal.AllocHGlobal((int)ReturnLength); try { if (GetLogicalProcessorInformation(pointer, ref ReturnLength)) { var size = Marshal.SizeOf <SYSTEM_LOGICAL_PROCESSOR_INFORMATION>(); var length = (int)ReturnLength / size; var buffer = new SYSTEM_LOGICAL_PROCESSOR_INFORMATION[length]; var itemPointer = pointer; for (int i = 0; i < length; i++) { buffer[i] = Marshal.PtrToStructure <SYSTEM_LOGICAL_PROCESSOR_INFORMATION>(itemPointer); itemPointer += size; } return(buffer); } } finally { Marshal.FreeHGlobal(pointer); } return(null); }
public static int GetNumaNodes() { int numaNodes = 0; uint returnLength = 0; GetLogicalProcessorInformation(IntPtr.Zero, ref returnLength); IntPtr Ptr = Marshal.AllocHGlobal((int)returnLength); try { if (GetLogicalProcessorInformation(Ptr, ref returnLength)) { int size = Marshal.SizeOf(typeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION)); int len = (int)returnLength / size; IntPtr item = Ptr; for (int i = 0; i < len; i++) { SYSTEM_LOGICAL_PROCESSOR_INFORMATION record = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION)Marshal.PtrToStructure( item, typeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION)); if (record.Relationship == LOGICAL_PROCESSOR_RELATIONSHIP.RelationNumaNode) { numaNodes += 1; } item += size; } return(numaNodes); } } catch (Exception ex) { Debug.WriteLine("Failed to find NUMA nodes: " + ex); } finally { Marshal.FreeHGlobal(Ptr); } return(0); }