Example #1
0
        /// <summary>
        ///     Gathers lots of information about the running processor.
        ///     Currently does NOT support multi-processor setups (e.g. two Intel Xeon CPUs).
        ///     For detailed information about the information provided please see the readme.
        /// </summary>
        /// <param name="skipClockspeedTest">
        ///     Default true. If false it will run a quick speed test of all cores to determine
        ///     maximum frequency.
        /// </param>
        /// <returns></returns>
        public static MachineInformation GatherInformation(bool skipClockspeedTest = true)
        {
            if (information != null && lastSkipClockspeedTest == skipClockspeedTest)
            {
                return(information);
            }

            if (RuntimeInformation.ProcessArchitecture == Architecture.X86 ||
                RuntimeInformation.ProcessArchitecture == Architecture.X64)
            {
                Opcode.Open();

                AppDomain.CurrentDomain.DomainUnload += (sender, args) => { Opcode.Close(); };
            }

            lastSkipClockspeedTest = skipClockspeedTest;
            information            = new MachineInformation();

            GetCommonCpuInformation();

            GatherCommonPerCoreInformation();

            foreach (var informationProvider in InformationProviders)
            {
                try
                {
                    if (informationProvider.Available(information))
                    {
                        informationProvider.GatherInformation(ref information);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                }
            }

            foreach (var cpuCore in information.Cpu.Cores)
            {
                cpuCore.NormalClockSpeed = information.Cpu.NormalClockSpeed;
                cpuCore.MaxClockSpeed    = information.Cpu.MaxClockSpeed;
            }

            foreach (var informationProvider in InformationProviders)
            {
                try
                {
                    if (informationProvider.Available(information))
                    {
                        informationProvider.PostProviderUpdateInformation(ref information);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                }
            }

            if (!skipClockspeedTest && (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ||
                                        RuntimeInformation.IsOSPlatform(OSPlatform.Linux)))
            {
                GetCoreSpeeds();
            }

            // Fix some things that may be propagated from lower-level information providers

            if (information.Cpu != null)
            {
                if (information.Cpu.Name != null)
                {
                    information.Cpu.Name = information.Cpu.Name.Trim();
                }

                if (information.Cpu.Caption != null)
                {
                    information.Cpu.Caption = information.Cpu.Caption.Trim();
                }
            }

            return(information);
        }
Example #2
0
        /// <summary>
        ///     Gathers lots of information about the running processor.
        ///     Currently does NOT support multi-processor setups (e.g. two Intel Xeon CPUs).
        ///     For detailed information about the information provided please see the readme.
        /// </summary>
        /// <param name="skipClockspeedTest">
        ///     Default true. If false it will run a quick speed test of all cores to determine
        ///     maximum frequency.
        /// </param>
        /// <param name="invalidateCache">
        ///     Default false. Whether to re-gather all the informations.
        /// </param>
        /// <param name="logger"></param>
        /// <returns></returns>
        public static MachineInformation GatherInformation(bool skipClockspeedTest             = true, bool invalidateCache = false,
                                                           ILogger <MachineInformation> logger = null)
        {
            if (logger == null)
            {
                logger = new NullLogger <MachineInformation>();
            }

            Logger = logger;

            if (information != null && lastSkipClockspeedTest == skipClockspeedTest && !invalidateCache)
            {
                Logger.LogInformation("Returning cached information");
                return(information);
            }

            if (RuntimeInformation.ProcessArchitecture == Architecture.X86 ||
                RuntimeInformation.ProcessArchitecture == Architecture.X64)
            {
                Logger.LogInformation("Loading OpCodes for CPUID");
                Opcode.Open();

                AppDomain.CurrentDomain.DomainUnload += (sender, args) => { Opcode.Close(); };
            }
            else
            {
                Logger.LogInformation("No CPUID available on non-x86 CPUs");
            }

            lastSkipClockspeedTest = skipClockspeedTest;
            information            = new MachineInformation();

            foreach (var informationProvider in InformationProviders)
            {
                Logger.LogInformation("Collecting information from {Provider}",
                                      informationProvider.GetType().Name);

                try
                {
                    if (informationProvider.Available(information))
                    {
                        informationProvider.GatherInformation(ref information);
                    }
                }
                catch (Exception e)
                {
                    Logger.LogError(e, "Exception when collecting information from {Provider}",
                                    informationProvider.GetType().Name);
                }
            }

            foreach (var cpuCore in information.Cpu.Cores)
            {
                if (cpuCore.NormalClockSpeed == 0)
                {
                    cpuCore.NormalClockSpeed = information.Cpu.NormalClockSpeed;
                }

                if (cpuCore.MaxClockSpeed == 0)
                {
                    cpuCore.MaxClockSpeed = information.Cpu.MaxClockSpeed;
                }
            }

            foreach (var informationProvider in InformationProviders)
            {
                Logger.LogInformation("Running post update on {Provider}", informationProvider.GetType().Name);

                try
                {
                    if (informationProvider.Available(information))
                    {
                        informationProvider.PostProviderUpdateInformation(ref information);
                    }
                }
                catch (Exception e)
                {
                    Logger.LogError(e, "Exception when running post-update from {Provider}",
                                    informationProvider.GetType().Name);
                }
            }

            if (!skipClockspeedTest && (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ||
                                        RuntimeInformation.IsOSPlatform(OSPlatform.Linux)))
            {
                Logger.LogWarning("Running clockspeed tests");
                GetCoreSpeeds();
            }

            // Fix some things that may be propagated from lower-level information providers

            if (information.Cpu != null)
            {
                if (information.Cpu.Name != null)
                {
                    information.Cpu.Name = information.Cpu.Name.Trim();
                }

                if (information.Cpu.Caption != null)
                {
                    information.Cpu.Caption = information.Cpu.Caption.Trim();
                }
            }

            return(information);
        }
        /// <summary>
        ///     Gathers lots of information about the running processor.
        ///     Currently does NOT support multi-processor setups (e.g. two Intel Xeon CPUs).
        ///     For detailed information about the information provided please see the readme.
        /// </summary>
        /// <param name="skipClockspeedTest">
        ///     Default true. If false it will run a quick speed test of all cores to determine
        ///     maximum frequency.
        /// </param>
        /// <param name="invalidateCache">
        ///     Default false. Whether to re-gather all the informations.
        /// </param>
        /// <param name="logger"></param>
        /// <param name="options">NULL means default all</param>
        /// <returns></returns>
        public static MachineInformation GatherInformation(bool skipClockspeedTest = true, bool invalidateCache     = false,
                                                           ILogger logger          = null, GatheringOptions options = null)
        {
            Logger = logger ?? new NullLogger <MachineInformation>();

            if (machineInformation != null && lastSkipClockspeedTest == skipClockspeedTest && !invalidateCache)
            {
                Logger.LogInformation("Returning cached information");
                return(machineInformation);
            }

            Options = options ?? new GatheringOptions();
            var mapper = CreateMapper();

            if (RuntimeInformation.ProcessArchitecture == Architecture.X86 ||
                RuntimeInformation.ProcessArchitecture == Architecture.X64)
            {
                Logger.LogInformation("Loading OpCodes for CPUID");
                Opcode.Open();

                AppDomain.CurrentDomain.DomainUnload += (sender, args) => { Opcode.Close(); };
            }
            else
            {
                Logger.LogInformation("No CPUID available on non-x86 CPUs");
            }

            lastSkipClockspeedTest = skipClockspeedTest;
            machineInformation     = new MachineInformation();
            var informationProviders = new List <InformationProvider>();

            foreach (var provider in InformationProviders)
            {
                if (provider.Available(machineInformation))
                {
                    informationProviders.Add(provider);
                }
                else
                {
                    Logger.LogWarning("{Provider} is not available", provider.GetType().Name);
                }
            }

            var info = new MachineInformation();

            foreach (var informationProvider in informationProviders)
            {
                Logger.LogInformation("Collecting General System information from {Provider}",
                                      informationProvider.GetType().Name);

                try
                {
                    informationProvider.GatherGeneralSystemInformation(ref info);
                }
                catch (Exception e)
                {
                    Logger.LogError(e, "Exception when collecting information from {Provider}",
                                    informationProvider.GetType().Name);
                }
            }

            var informations = new Dictionary <string, MachineInformation>();

            foreach (var informationProvider in informationProviders)
            {
                Logger.LogInformation("Collecting information from {Provider}",
                                      informationProvider.GetType().Name);

                // Copy general system info from before onto a MachineInformation for each provider
                var information = new MachineInformation();
                information = mapper.Map(info, information);

                foreach (var fieldInfo in typeof(GatheringOptions).GetProperties(BindingFlags.Instance |
                                                                                 BindingFlags.Public))
                {
                    try
                    {
                        switch (fieldInfo.Name)
                        {
                        case nameof(GatheringOptions.GatherCpuInformation):
                        {
                            informationProvider.GatherCpuInformation(ref information);
                            break;
                        }

                        case nameof(GatheringOptions.GatherCpuSpeedInformation):
                        {
                            informationProvider.GatherCpuSpeedInformation(ref information);
                            break;
                        }

                        case nameof(GatheringOptions.GatherCpuCacheTopologyInformation):
                        {
                            informationProvider.GatherCpuCacheTopologyInformation(ref information);
                            break;
                        }

                        case nameof(GatheringOptions.GatherCpuFeatureFlagInformation):
                        {
                            informationProvider.GatherCpuFeatureFlagInformation(ref information);
                            break;
                        }

                        case nameof(GatheringOptions.GatherMainboardInformation):
                        {
                            informationProvider.GatherMainboardInformation(ref information);
                            break;
                        }

                        case nameof(GatheringOptions.GatherRamInformation):
                        {
                            informationProvider.GatherRamInformation(ref information);
                            break;
                        }

                        case nameof(GatheringOptions.GatherDiskInformation):
                        {
                            informationProvider.GatherDiskInformation(ref information);
                            break;
                        }

                        case nameof(GatheringOptions.GatherGpuInformation):
                        {
                            informationProvider.GatherGpuInformation(ref information);
                            break;
                        }

                        case nameof(GatheringOptions.GatherUsbInformation):
                        {
                            informationProvider.GatherUsbInformation(ref information);
                            break;
                        }

                        case nameof(GatheringOptions.GatherMonitorInformation):
                        {
                            informationProvider.GatherMonitorInformation(ref information);
                            break;
                        }
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.LogError(e, "Exception when collecting information from {Provider}",
                                        informationProvider.GetType().Name);
                    }
                }

                informations.Add(informationProvider.GetType().Name, information);
            }

            if (!skipClockspeedTest && (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ||
                                        RuntimeInformation.IsOSPlatform(OSPlatform.Linux)))
            {
                Logger.LogWarning("Running clockspeed tests");
                GetCoreSpeeds();
            }

            foreach (var informationProvider in informationProviders)
            {
                Logger.LogInformation("Running post update on {Provider}", informationProvider.GetType().Name);

                var information = informations[informationProvider.GetType().Name];
                try
                {
                    informationProvider.PostProviderUpdateInformation(ref information);
                }
                catch (Exception e)
                {
                    Logger.LogError(e, "Exception when running post-update from {Provider}",
                                    informationProvider.GetType().Name);
                }
            }


            var dest = new MachineInformation();

            foreach (var kvp in informations)
            {
                dest = mapper.Map(kvp.Value, dest);
            }

            machineInformation = dest;

            PostOperations();

            return(machineInformation);
        }