/// <summary> /// Calculate CPU usage, e.g.: this process time vs system process time /// return the CPU usage in percentage /// </summary> public static double GetCPUUsage() { double ret = 0.0; //retrieve process time ProcessTimes processTimes = GetProcessTimes(); UInt64 currentProcessTime = processTimes.KernelTime + processTimes.UserTime; //retrieve system time // get number of CPU cores, then, check system time for every CPU core if (numberOfProcessors == 0) { SystemInfo info; GetSystemInfo(out info); Debug.WriteLine(info.NumberOfProcessors); numberOfProcessors = info.NumberOfProcessors; } int size = System.Runtime.InteropServices.Marshal.SizeOf <SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION>(); size = (int)(size * numberOfProcessors); IntPtr systemProcessInfoBuff = Marshal.AllocHGlobal(size); // should be more than adequate uint length = 0; NtStatus result = NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS.SystemProcessorPerformanceInformation, systemProcessInfoBuff, (uint)size, out length); if (result != NtStatus.Success) { Debug.WriteLine($"Failed to obtain processor performance information ({result})"); return(ret); } UInt64 currentSystemTime = 0; var systemProcInfoData = systemProcessInfoBuff; for (int i = 0; i < numberOfProcessors; i++) { SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION processPerInfo = (SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)Marshal.PtrToStructure <SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION>(systemProcInfoData); currentSystemTime += processPerInfo.KernelTime + processPerInfo.UserTime; } //we need to at least measure twice if (previousProcessTime != 0 && previousSystemTIme != 0) { ret = ((double)(currentProcessTime - previousProcessTime) / (double)(currentSystemTime - previousSystemTIme)) * 100.0; } previousProcessTime = currentProcessTime; previousSystemTIme = currentSystemTime; Debug.WriteLine($"CPU usage: {ret}%"); return(ret); }
public void Roundtrip() { var pt = new ProcessTimes(1, 2, 3, 4); XAssert.AreEqual(1, pt.StartTimeUtc.ToFileTimeUtc()); XAssert.AreEqual(2, pt.ExitTimeUtc.ToFileTimeUtc()); XAssert.AreEqual(3, pt.PrivilegedProcessorTime.Ticks); XAssert.AreEqual(4, pt.UserProcessorTime.Ticks); XAssert.AreEqual(7, pt.TotalProcessorTime.Ticks); }
/// <summary> /// Report process time to console and append to log file. /// </summary> /// <param name="options">Parsed CLI options with process file and arguments.</param> /// <param name="pTimes">Measured process times.</param> static void ReportProcessTimes(ParsedOptions options, ProcessTimes pTimes, string measuredProcessName) { string times = pTimes.FormatProcessTimes(); StringBuilder logBuilder = new StringBuilder(); logBuilder.Append(DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss")) .Append('\n') .Append("Measured process: ").Append(measuredProcessName).Append('\n') .Append("Command: ").Append(options.ProcessFile).Append(' ').Append(options.ProcessArguments) .Append('\n') .Append(times); string report = logBuilder.ToString(); AppendToLogFile(report); ColoredPrint(report, ConsoleColor.DarkGreen); }
public void GracefullyHandledIllegalExitTime() { var pt = new ProcessTimes(1, -1, 3, 4); XAssert.AreEqual(DateTime.MaxValue, pt.ExitTimeUtc); }
/**************************************************************************** * HandlePicoBlockData * - acquires data * Output: * - exports the times and voltage readings of Channel A as an array. * Input : * - offset : the offset into the data buffer to start the display's slice. ****************************************************************************/ private void HandlePicoBlockData(int offset, Pico.ChannelSettings[] channelSettings, short handle, ref WaveFormData waveFormData, ref ProcessTimes processTimes, ref System.Windows.Forms.Timer WaveformTimer, int SampleCount, bool ChanelB) { Stopwatch sw = new Stopwatch(); int sampleCount = SampleCount;//*2*2*2*2; // BUFFER_SIZE; short timeUnit = 0; short status = 0; UpdatePicoBufferSize(sampleCount); int[] time = new int[sampleCount]; int[] amp = new int[sampleCount]; PinnedArray <int> pinnedTimes = new PinnedArray <int>(time); PinnedArray <short>[] pinned = new PinnedArray <short> [channelCount]; // Channel buffers for (int i = 0; i < channelCount; i++) { short[] buffer = new short[sampleCount]; pinned[i] = new PinnedArray <short>(buffer); } /* Find the maximum number of samples, the time interval (in nanoseconds), * the most suitable time units (ReportedTimeUnits), and the maximum oversample at the current timebase*/ int timeInterval = 0; do { status = Imports.GetTimebase(handle, timebase, sampleCount, out timeInterval, out timeUnit, oversample, out int maxSamples); if (status != 1) { timebase++; } if (timebase > 250) { break; } }while (status == 0); /* Start the device collecting, then wait for completion*/ sw.Start(); Imports.RunBlock(handle, sampleCount, timebase, oversample, out int timeIndisposed); int timer = 1; bool isMessageDisconnect = false; short ready = 0; while (ready == 0) { ready = Imports.Isready(handle); Thread.Sleep(1); timer++; if (timer > 500 & !isMessageDisconnect) { isMessageDisconnect = true; ready = -1; //MessageBox.Show("Cannot connect to Picoscope: Check the USB Connection and Restart"); //MessageBox.Show("Test"); } // break; } if (ready == -1) { WaveformTimer.Stop(); MessageBox.Show("Cannot connect to Picoscope: Check the USB Connection and Restart"); } sw.Stop(); if (ready > 0) { if (ChanelB) { Imports.GetTimesAndValues(handle, pinnedTimes, null, pinned[0], null, null, out short overflow, timeUnit, sampleCount); } else { Imports.GetTimesAndValues(handle, pinnedTimes, pinned[0], null, null, null, out short overflow, timeUnit, sampleCount); } // handle channel 1 for (int i = 0; i < sampleCount; i++) { amp[i] = adc_to_mv(pinned[0].Target[i], (int)channelSettings[0].range); } } waveFormData.time = time; waveFormData.amp = amp; processTimes.samplingTime = sw.Elapsed; Imports.Stop(handle); }
/**************************************************************************** * GetPlottableData * this function demonstrates how to collect a single block of data * from the unit (start collecting immediately) ****************************************************************************/ public PlottableData GetPlottableData(short handle, Pico.ChannelSettings[] channelSettings, FFTSettings fftSettings, double[] lastfft, ref ProcessTimes processTimes, ref System.Windows.Forms.Timer WaveFormTimer, int SampleCount, bool isChanelB) { Stopwatch sw = new Stopwatch(); //sw.Start(); WaveFormData waveFormData = new WaveFormData(); // Method that communicates with Pico to get blocked data HandlePicoBlockData(0, channelSettings, handle, ref waveFormData, ref processTimes, ref WaveFormTimer, SampleCount, isChanelB); //sw.Stop(); //return if it's chanel B if (isChanelB) { return new PlottableData { fftData = new FFTData(), WaveFormData = waveFormData } } ; // calculate fft data if is not chanel B sw.Restart(); PlottableData plottableData = new PlottableData { fftData = GetFFT(waveFormData.amp, waveFormData.time, fftSettings, lastfft, SampleCount), WaveFormData = waveFormData }; sw.Stop(); processTimes.fftTime = sw.Elapsed; return(plottableData); }