internal static bool GetMaxProcessorSpeeds(out int[] result)
        {
            IntPtr zero = IntPtr.Zero;
            bool   flag = false;

            result = new int[Environment.ProcessorCount];
            try
            {
                flag = CPUPowerWrapper.AllocateAndGetProcessorInfo(out zero);
                if (flag)
                {
                    int num = 0;
                    for (int i = 0; i < Environment.ProcessorCount; i++)
                    {
                        IntPtr ptr = CPUPowerWrapper.AddOffset(zero, num);
                        PROCESSOR_POWER_INFORMATION processor_POWER_INFORMATION = (PROCESSOR_POWER_INFORMATION)Marshal.PtrToStructure(ptr, typeof(PROCESSOR_POWER_INFORMATION));
                        result[i] = (int)processor_POWER_INFORMATION.MaxMhz;
                        num      += CPUPowerWrapper.PpiSize;
                    }
                }
            }
            finally
            {
                Marshal.FreeCoTaskMem(zero);
            }
            return(flag);
        }
        internal static bool GetCurrentPowerInfoForProcessor(int currentProcessor, out PROCESSOR_POWER_INFORMATION ppi)
        {
            IntPtr zero = IntPtr.Zero;
            bool   flag = false;

            try
            {
                flag = CPUPowerWrapper.AllocateAndGetProcessorInfo(out zero);
                if (flag)
                {
                    int    offset = currentProcessor * CPUPowerWrapper.PpiSize;
                    IntPtr ptr    = CPUPowerWrapper.AddOffset(zero, offset);
                    ppi = (PROCESSOR_POWER_INFORMATION)Marshal.PtrToStructure(ptr, typeof(PROCESSOR_POWER_INFORMATION));
                }
                else
                {
                    ppi = default(PROCESSOR_POWER_INFORMATION);
                }
            }
            finally
            {
                Marshal.FreeCoTaskMem(zero);
            }
            return(flag);
        }
Example #3
0
        private static bool GetIsCpuThrottlingSupported()
        {
            bool isProcessorThrottlingEnabled = CPUPowerWrapper.IsProcessorThrottlingEnabled;

            if (!isProcessorThrottlingEnabled && !CPUPowerWrapper.GetMaxProcessorSpeeds(out MyStopwatch.nonThrottlingCpuMHz))
            {
                throw new InvalidOperationException("CPUPowerWrapper.GetMaxProcessorSpeeds() unexpectedly returned false.");
            }
            return(isProcessorThrottlingEnabled);
        }
Example #4
0
        private static void GetCurrentAndMaxMHz(int cpuId, out int current, out int max)
        {
            if (cpuId < 0 || cpuId > Environment.ProcessorCount)
            {
                throw new ArgumentException("Needs to be between 0 and Environment.ProcessorCount-1.", "cpuId");
            }
            current = 0;
            max     = 0;
            if (!MyStopwatch.isCpuThrottlingSupported)
            {
                current = MyStopwatch.nonThrottlingCpuMHz[cpuId];
                max     = MyStopwatch.nonThrottlingCpuMHz[cpuId];
                return;
            }
            PROCESSOR_POWER_INFORMATION processor_POWER_INFORMATION;

            if (CPUPowerWrapper.GetCurrentPowerInfoForProcessor(cpuId, out processor_POWER_INFORMATION))
            {
                current = (int)processor_POWER_INFORMATION.CurrentMhz;
                max     = (int)processor_POWER_INFORMATION.MaxMhz;
                return;
            }
            throw new InvalidOperationException("CPUPowerWrapper.GetCurrentPowerInfoForProcessor() unexpectedly returned false.");
        }