Click or drag to resize

LogicalProcessorInfoProvider Constructor

Initializes a new instance of the LogicalProcessorInfoProvider class.

Namespace:  Xcalibur.WMI.Hardware.Processor
Assembly:  Xcalibur.WMI (in Xcalibur.WMI.dll) Version: 3.0.0.0 (3.0.1.0)
Syntax
public LogicalProcessorInfoProvider(
	ManagementScope ms
)

Parameters

ms
Type: System.ManagementManagementScope
The management scope.
Examples
C#
  1using System;
  2using System.Collections.Generic;
  3using System.Management;
  4using System.Threading;
  5using Xcalibur.Models.Hardware.Processor;
  6using Xcalibur.WMI.Hardware.Processor;
  7
  8// Program
  9internal class Program
 10{
 11    // Main
 12    private static void Main()
 13    {
 14        // Make a connection to the local machine
 15        const string computer = ".";
 16        var managementScope = new ManagementScope($@"\\{computer}\root\cimv2", new ConnectionOptions
 17        {
 18            Impersonation = ImpersonationLevel.Impersonate,
 19            Authentication = AuthenticationLevel.Connect
 20
 21        });
 22        managementScope.Connect();
 23
 24        // Logical Processor Info Provider
 25        var provider = new LogicalProcessorInfoProvider(managementScope);
 26
 27        // Fetch first set of data
 28        provider.Refresh();
 29
 30        // Store first result set so it's not overwritten
 31        var storedItems = new List<ILogicalProcessorInfo>(provider.Results);
 32
 33        // Wait 2 seconds
 34        Thread.Sleep(2000);
 35
 36        // Fetch second set of data
 37        provider.Refresh();
 38
 39        // Get logical processor count
 40        var logicalProcessorCount = provider.Results.Length - 1;
 41
 42        // Print
 43        // Assess each connection based on their time difference against the 
 44        // PERF_100NSEC_TIMER algorithm.
 45        for (var i = 0; i < logicalProcessorCount; i++)
 46        {
 47            var info = provider.Results[i];
 48            var stored = storedItems[i];
 49
 50            Console.WriteLine($"Processor #: \t\t{info.Name}");
 51
 52            // Processor Time
 53            var processorTime = CalculatePerf100NsecTimer(
 54                info.PercentProcessorTime, info.TimestampSys100Ns,
 55                stored.PercentProcessorTime, stored.TimestampSys100Ns, logicalProcessorCount);
 56            Console.WriteLine($"Processor Time: \t{processorTime:n2}%");
 57
 58            // Privileged Time
 59            var kernelTime = CalculatePerf100NsecTimer(
 60                info.PercentPrivilegedTime, info.TimestampSys100Ns,
 61                stored.PercentPrivilegedTime, stored.TimestampSys100Ns, logicalProcessorCount);
 62            Console.WriteLine($"Privileged Time: \t{kernelTime:n2}%");
 63
 64            // User Time
 65            var userTime = CalculatePerf100NsecTimer(
 66                info.PercentUserTime, info.TimestampSys100Ns,
 67                stored.PercentUserTime, stored.TimestampSys100Ns, logicalProcessorCount);
 68            Console.WriteLine($"User Time: \t\t{userTime:n2}%");
 69
 70            // Interrupt Time
 71            var interruptTime = CalculatePerf100NsecTimer(
 72                info.PercentInterruptTime, info.TimestampSys100Ns,
 73                stored.PercentInterruptTime, stored.TimestampSys100Ns, logicalProcessorCount);
 74            Console.WriteLine($"Interrupt Time: \t{interruptTime:n2}%");
 75
 76            // Idle Time
 77            var idleTime = CalculatePerf100NsecTimer(
 78                info.PercentIdleTime, info.TimestampSys100Ns,
 79                stored.PercentIdleTime, stored.TimestampSys100Ns, logicalProcessorCount);
 80            Console.WriteLine($"Idle Time: \t\t{idleTime:n2}%");
 81
 82            // Spacer
 83            Console.WriteLine("");
 84        }
 85
 86        // Read
 87        Console.Read();
 88    }
 89
 90    /// <summary>
 91    /// PERF_100NSEC_TIMER algorithm.
 92    /// <para/>PERF_100NSEC_TIMER on MSDN
 93    /// </summary>
 94    /// <param name="newValue">The current value.</param>
 95    /// <param name="newDate">The current date.</param>
 96    /// <param name="previousValue">The previous value.</param>
 97    /// <param name="previousDate">The previous date.</param>
 98    /// <param name="processorCount">The processor count.</param>
 99    /// <returns></returns>
100    public static double CalculatePerf100NsecTimer(
101        long newValue, ulong newDate, long previousValue, ulong previousDate, int processorCount)
102    {
103        double usage = 0;
104        try
105        {
106            if (previousDate > 0)
107            {
108                double dataDifference = newValue - previousValue;
109                double timeDifference = newDate - previousDate;
110                var dUsage = (dataDifference * 100) / timeDifference;
111                usage = dUsage / processorCount;
112
113                // Enforce positive values
114                usage = usage >= 0 ? usage : 0;
115            }
116
117            else
118            {
119                usage = 0;
120            }
121        }
122        catch
123        {
124            // ignored
125        }
126
127        // Return
128        return usage;
129    }
130}
131
132/* Results: 
133 * 
134 * Processor #:            0
135 * Processor Time:         8.93%
136 * Privileged Time:        1.55%
137 * User Time:              1.94%
138 * Interrupt Time:         0.19%
139 * Idle Time:              8.34%
140 * 
141 * Processor #:            1
142 * Processor Time:         9.61%
143 * Privileged Time:        0.78%
144 * User Time:              2.04%
145 * Interrupt Time:         0.10%
146 * Idle Time:              9.43%
147 * 
148 * Processor #:            2
149 * Processor Time:         9.81%
150 * Privileged Time:        0.10%
151 * User Time:              2.52%
152 * Interrupt Time:         0.00%
153 * Idle Time:              8.73%
154 * 
155 * Processor #:            3
156 * Processor Time:         7.77%
157 * Privileged Time:        1.36%
158 * User Time:              3.30%
159 * Interrupt Time:         0.00%
160 * Idle Time:              7.88%
161 * 
162 * Processor #:            4
163 * Processor Time:         8.55%
164 * Privileged Time:        0.87%
165 * User Time:              3.01%
166 * Interrupt Time:         0.00%
167 * Idle Time:              8.33%
168 * 
169 * Processor #:            5
170 * Processor Time:         10.49%
171 * Privileged Time:        0.58%
172 * User Time:              1.36%
173 * Interrupt Time:         0.10%
174 * Idle Time:              9.63%
175 * 
176 * Processor #:            6
177 * Processor Time:         8.93%
178 * Privileged Time:        1.07%
179 * User Time:              2.43%
180 * Interrupt Time:         0.00%
181 * Idle Time:              8.46%
182 * 
183 * Processor #:            7
184 * Processor Time:         9.42%
185 * Privileged Time:        1.07%
186 * User Time:              1.94%
187 * Interrupt Time:         0.10%
188 * Idle Time:              8.69%
189 * 
190 */
See Also