internal CacheDescriptor(CacheLevel level, CacheAssociativity associativity, Int16 lineSize, Int32 size, ProcessorCacheType type) { m_level = level; m_associativity = associativity; m_lineSize = lineSize; m_size = size; m_type = type; }
private void PrepareIntelCaches(ref uint eax, ref uint ebx, ref uint ecx, ref uint edx, ulong mask) { uint cacheType = 0xFFFF; uint leaf = 0; while (cacheType != 0) { if (Opcode.CpuidTx(CPUID_4, leaf, out eax, out ebx, out ecx, out edx, mask)) { cacheType = eax & 0x1f; } else { cacheType = 0; } if (cacheType == 0) { continue; } var level = (eax >> 5) & 0x07; var waysOfAssociativity = ((ebx >> 22) & 0x3FF) + 1; var linePartitions = ((ebx >> 12) & 0x3FF) + 1; var lineSize = (ushort)((ebx & 0xFFF) + 1); var sets = ecx + 1; var size = (waysOfAssociativity * linePartitions * lineSize * sets) / 1024; var fullAssociative = Convert.ToBoolean((eax >> 9) & 1); CacheAssociativity cacheAssociativity = CacheAssociativity.Disabled; if (fullAssociative) { cacheAssociativity = CacheAssociativity.Fully; } else if (waysOfAssociativity > 0) { cacheAssociativity = (CacheAssociativity)waysOfAssociativity; } var cacheItem = new CacheItem() { Level = (CacheLevels)level, LineSize = lineSize, SizeKbytes = size, CacheType = (CacheType)cacheType, Associativity = cacheAssociativity }; if (caches.ContainsKey(cacheItem.Level)) { var cachesOfLevel = caches[cacheItem.Level].ToList(); cachesOfLevel.Add(cacheItem); caches[cacheItem.Level] = cachesOfLevel.ToArray(); } else { caches[cacheItem.Level] = new CacheItem[] { cacheItem }; } leaf++; } }
public static unsafe LogicalProcessorInformation[] GetLogicalProcessorInformation() { // Get the required buffer size Int32 length = 0; NativeMethods.GetLogicalProcessorInformation(null, ref length); Contract.Assume(length > 0); // Allocate the buffer & query the data Byte[] bytes = new Byte[length]; fixed(Byte *pb = &bytes[0]) { Boolean ok = NativeMethods.GetLogicalProcessorInformation(pb, ref length); if (!ok) { throw new Win32Exception(); } } List <LogicalProcessorInformation> list = new List <LogicalProcessorInformation>(); for (Int32 elementIndex = 0; elementIndex < length; elementIndex += (2 * IntPtr.Size) + 16) { Int32 index = elementIndex; Int64 processorMask; if (IntPtr.Size == 4) { processorMask = (Int64)BitConverter.ToInt32(bytes, index); } else { processorMask = BitConverter.ToInt64(bytes, index); } index += IntPtr.Size; LogicalProcessorRelationship lpr; if (IntPtr.Size == 4) { lpr = (LogicalProcessorRelationship)BitConverter.ToInt32(bytes, index); } else { lpr = (LogicalProcessorRelationship)BitConverter.ToInt64(bytes, index); } index += IntPtr.Size; LogicalProcessorInformation lpi = null; switch (lpr) { case LogicalProcessorRelationship.Cache: CacheLevel level = (CacheLevel)bytes[index++]; CacheAssociativity associativity = (CacheAssociativity)bytes[index++]; Int16 lineSize = BitConverter.ToInt16(bytes, index); index += 2; Int32 size = BitConverter.ToInt32(bytes, index); index += 4; ProcessorCacheType type = (ProcessorCacheType)BitConverter.ToInt32(bytes, index); lpi = new LogicalProcessorInformation(processorMask, lpr, new CacheDescriptor(level, associativity, lineSize, size, type)); break; case LogicalProcessorRelationship.NumaNode: lpi = new LogicalProcessorInformation(processorMask, lpr, BitConverter.ToInt32(bytes, index)); break; case LogicalProcessorRelationship.ProcessorCore: lpi = new LogicalProcessorInformation(processorMask, lpr, bytes[index]); break; case LogicalProcessorRelationship.ProcessorPackage: lpi = new LogicalProcessorInformation(processorMask, lpr); break; } list.Add(lpi); } return(list.ToArray()); }