public static CpuUsage?Get(CpuUsageScope scope)
        {
            long kernelMicroseconds;
            long userMicroseconds;
            bool isOk;

            if (scope == CpuUsageScope.Thread)
            {
                isOk = WindowsCpuUsageInterop.GetThreadTimes(out kernelMicroseconds, out userMicroseconds);
            }
            else
            {
                isOk = WindowsCpuUsageInterop.GetProcessTimes(out kernelMicroseconds, out userMicroseconds);
            }

            if (!isOk)
            {
                return(null);
            }

            const long m = 1000000L;

            return(new CpuUsage()
            {
                KernelUsage = new TimeValue()
                {
                    Seconds = kernelMicroseconds / m, MicroSeconds = kernelMicroseconds % m
                },
                UserUsage = new TimeValue()
                {
                    Seconds = userMicroseconds / m, MicroSeconds = userMicroseconds % m
                },
            });
        }
        public static CpuUsage?Get(CpuUsageScope scope)
        {
            var platform = CrossInfo.ThePlatform;

            if (scope == CpuUsageScope.Process)
            {
                if (platform == CrossInfo.Platform.Linux || platform == CrossInfo.Platform.MacOSX)
                {
                    return(LinuxResourceUsageReader.GetByProcess());
                }
                else
                {
                    return(WindowsCpuUsage.Get(CpuUsageScope.Process));
                }
            }

            if (platform == CrossInfo.Platform.Linux)
            {
                return(LinuxResourceUsageReader.GetByThread());
            }

            else if (platform == CrossInfo.Platform.MacOSX)
            {
                return(MacOsThreadInfo.GetByThread());
            }

            else if (platform == CrossInfo.Platform.Windows)
            {
                // throw new NotImplementedException("CPU Usage in the scope of the thread is not yet implemented for Windows");
                return(WindowsCpuUsage.Get(CpuUsageScope.Thread));
            }

            throw new InvalidOperationException($"CPU usage in the scope of {scope} is a kind of an unknown on the {platform}");
        }
        public void IO_Write_Test(CpuUsageScope scope)
        {
            if (!PosixResourceUsage.IsSupported)
            {
                return;
            }
            if (scope == CpuUsageScope.Thread && CrossInfo.ThePlatform != CrossInfo.Platform.Linux)
            {
                return;
            }

            // Arrange: nothing to do

            // Act
            PosixResourceUsage before = PosixResourceUsage.GetByScope(scope).Value;
            var numBytes = 10 * 512 * 1024;

            WriteFile(numBytes);
            PosixResourceUsage after = PosixResourceUsage.GetByScope(scope).Value;
            var delta = PosixResourceUsage.Substruct(after, before);

            Console.WriteLine($"Operation: write {numBytes:n0} bytes. ReadOps = {delta.ReadOps}. WriteOps = {delta.WriteOps}");

            // Assert
            if (CrossInfo.ThePlatform != CrossInfo.Platform.Linux)
            {
                return;
            }
            if (SkipPosixResourcesUsageAsserts || !IsIoReadsWritesSupported())
            {
                return;
            }
            Assert.Greater(delta.WriteOps, 0);
        }
        public void Grow_Usage()
        {
            if (CrossInfo.ThePlatform == CrossInfo.Platform.Windows)
            {
                return;
            }

            CpuLoader.Run(1, 1, true);
            CpuUsageScope scope = CrossInfo.ThePlatform == CrossInfo.Platform.MacOSX
                ? CpuUsageScope.Process
                : CpuUsageScope.Thread;

            Console.WriteLine($"Supported scope: {scope}");
            LinuxResourceUsageReader.GetByScope(scope);
            var prev = LinuxResourceUsageReader.GetByScope(scope);

            for (int i = 0; i < 10; i++)
            {
                CpuLoader.Run(1, 1, true);
                CpuUsage?next = LinuxResourceUsageReader.GetByScope(scope);
                Console.WriteLine($" {i} -> {next}");
                Assert.GreaterOrEqual(next.Value.KernelUsage.TotalMicroSeconds, prev.Value.KernelUsage.TotalMicroSeconds);
                Assert.GreaterOrEqual(next.Value.UserUsage.TotalMicroSeconds, prev.Value.UserUsage.TotalMicroSeconds);
                prev = next;
            }
        }
 public void Smoke_Test(CpuUsageScope scope)
 {
     if (!PosixResourceUsage.IsSupported)
     {
         return;
     }
     Console.WriteLine($"PosixResourceUsage.GetByScope({scope}): {PosixResourceUsage.GetByScope(scope)}");
 }
 public static CpuUsage?SafeGet(CpuUsageScope scope)
 {
     try
     {
         return(Get(scope));
     }
     catch
     {
         return(null);
     }
 }
        public void ContextSwitch_Test(CpuUsageScope scope, int expectedSwitchCount)
        {
            if (!PosixResourceUsage.IsSupported)
            {
                return;
            }
            if (scope == CpuUsageScope.Thread && CrossInfo.ThePlatform != CrossInfo.Platform.Linux)
            {
                return;
            }

            // Act
            PosixResourceUsage before = PosixResourceUsage.GetByScope(scope).Value;

            for (int i = 0; i < expectedSwitchCount; i++)
            {
                // CpuLoader.Run(1, 0, true);
                Stopwatch sw = Stopwatch.StartNew();
                while (sw.ElapsedMilliseconds < 1)
                {
                    ;
                }
                // on some build server sometimes fails (Azure - sometimes fail, AppVeyor - OK)
                Thread.Sleep(3);
            }

            PosixResourceUsage after = PosixResourceUsage.GetByScope(scope).Value;
            // var delta = PosixResourceUsage.Substruct(after, before);
            var delta = after - before;

            Console.WriteLine($"delta.InvoluntaryContextSwitches = {delta.InvoluntaryContextSwitches}");
            Console.WriteLine($"delta.VoluntaryContextSwitches = {delta.VoluntaryContextSwitches}");

            // Assert
            if (CrossInfo.ThePlatform != CrossInfo.Platform.Linux)
            {
                return;
            }
            if (SkipPosixResourcesUsageAsserts)
            {
                return;
            }
            Assert.IsTrue(expectedSwitchCount <= delta.VoluntaryContextSwitches && expectedSwitchCount <= delta.VoluntaryContextSwitches + 7);
//            if (scope == CpuUsageScope.Thread)
//                Assert.IsTrue(expectedSwitchCount == delta.VoluntaryContextSwitches || expectedSwitchCount == delta.VoluntaryContextSwitches - 1);
//            else
//                Assert.GreaterOrEqual(delta.VoluntaryContextSwitches, expectedSwitchCount);
        }
Beispiel #8
0
        void Load(CpuUsageScope scope, int milliseconds)
        {
            CpuUsage?prev = CpuUsage.Get(scope);

            Assert.IsTrue(prev.HasValue, "Prev should has value");
            Stopwatch sw = Stopwatch.StartNew();

            while (sw.ElapsedMilliseconds <= milliseconds)
            {
                EatSomeCpu();
                EatSomeMem();
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            CpuUsage?next         = CpuUsage.Get(scope);
            double   microSeconds = sw.ElapsedTicks * 1000000d / Stopwatch.Frequency;

            Assert.IsTrue(next.HasValue, "Next should has value");


            var delta = CpuUsage.Substruct(next.Value, prev.Value);

            Assert.GreaterOrEqual(next.Value.KernelUsage.TotalMicroSeconds, prev.Value.KernelUsage.TotalMicroSeconds, "Kernel usage should be greater or equal zero");
            Assert.Greater(next.Value.UserUsage.TotalMicroSeconds, prev.Value.UserUsage.TotalMicroSeconds, "User usage should be greater or equal zero");

            string message = string.Format("Duration: {0:f3} of {4}, CPU@{3}: {1:f3} = {2}",
                                           microSeconds / 1000,
                                           (delta.KernelUsage.MicroSeconds + delta.UserUsage.MicroSeconds) / 1000d,
                                           delta,
                                           scope,
                                           milliseconds);

            Console.WriteLine(message);
        }
Beispiel #9
0
        public static CpuUsage?GetByScope(CpuUsageScope scope)
        {
            var s = scope == CpuUsageScope.Process ? LinuxResourceUsageInterop.RUSAGE_SELF : LinuxResourceUsageInterop.RUSAGE_THREAD;

            return(GetLinuxCpuUsageByScope(s));
        }
Beispiel #10
0
 // for intellisense
 public static CpuUsage?Get(CpuUsageScope scope)
 {
     return(CpuUsageReader.Get(scope));
 }
        public static PosixResourceUsage?GetByScope(CpuUsageScope scope)
        {
            int kernelScope = scope == CpuUsageScope.Process ? LinuxResourceUsageInterop.RUSAGE_SELF : LinuxResourceUsageInterop.RUSAGE_THREAD;

            return(LinuxResourceUsageReader.GetResourceUsageByScope(kernelScope));
        }