Esempio n. 1
0
        // Modeled after https://github.com/docker/cli/blob/v19.03.9/cli/command/container/stats_helpers.go#L166
        Option <double> GetCpuLinux(DockerStats module)
        {
            Option <double> currentTotal  = module.CpuStats.AndThen(s => s.CpuUsage).AndThen(s => s.TotalUsage);
            Option <double> previousTotal = module.Name.AndThen(this.previousModuleCpu.GetOption);
            Option <double> moduleDelta   = currentTotal.AndThen(curr => previousTotal.Map(prev => curr - prev));

            Option <double> currentSystem  = module.CpuStats.AndThen(s => s.SystemCpuUsage);
            Option <double> previousSystem = module.Name.AndThen(this.previousSystemCpu.GetOption);
            Option <double> systemDelta    = currentSystem.AndThen(curr => previousSystem.Map(prev => curr - prev));

            // set previous to new current
            module.Name.ForEach(name =>
            {
                currentTotal.ForEach(curr => this.previousModuleCpu[name]  = curr);
                currentSystem.ForEach(curr => this.previousSystemCpu[name] = curr);
            });

            return(moduleDelta.AndThen(moduleDif => systemDelta.AndThen(systemDif =>
            {
                if (moduleDif >= 0 && systemDif > 0)
                {
                    double result = 100 * moduleDif / systemDif;

                    // Occasionally on startup results in a very large number (billions of percent). Ignore this point.
                    if (result < 100)
                    {
                        return Option.Some(result);
                    }
                }

                return Option.None <double>();
            })));
        }
Esempio n. 2
0
 Option <double> GetCpuUsage(DockerStats module)
 {
     if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
     {
         return(this.GetCpuLinux(module));
     }
     else
     {
         return(this.GetCpuWindows(module));
     }
 }
Esempio n. 3
0
        Option <double> GetCpuUsage(DockerStats module)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                if (this.previousModuleCpu.TryGetValue(module.Name, out ulong prevModule) && this.previousSystemCpu.TryGetValue(module.Name, out ulong prevSystem))
                {
                    double moduleDiff = module.CpuStats.CpuUsage.TotalUsage - prevModule;
                    double systemDiff = module.CpuStats.SystemCpuUsage - prevSystem;
                    if (systemDiff > 0)
                    {
                        double result = 100 * moduleDiff / systemDiff;

                        // Occasionally on startup results in a very large number (billions of percent). Ignore this point.
                        if (result < 100)
                        {
                            return(Option.Some(result));
                        }
                    }
                }

                this.previousModuleCpu[module.Name] = module.CpuStats.CpuUsage.TotalUsage;
                this.previousSystemCpu[module.Name] = module.CpuStats.SystemCpuUsage;
            }
            else
            {
                if (this.previousModuleCpu.TryGetValue(module.Name, out ulong prevModule) && this.previousReadTime.TryGetValue(module.Name, out DateTime prevTime))
                {
                    double totalIntervals = (module.Read - prevTime).TotalMilliseconds * 10; // Get number of 100ns intervals during read
                    ulong  intervalsUsed  = module.CpuStats.CpuUsage.TotalUsage - prevModule;

                    if (totalIntervals > 0)
                    {
                        return(Option.Some(100 * intervalsUsed / totalIntervals));
                    }
                }

                this.previousModuleCpu[module.Name] = module.CpuStats.CpuUsage.TotalUsage;
                this.previousReadTime[module.Name]  = module.Read;
            }

            return(Option.None <double>());
        }
        double GetCpuUsage(DockerStats module)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                double result = 0;
                if (this.previousModuleCpu.TryGetValue(module.Name, out ulong prevModule) && this.previousSystemCpu.TryGetValue(module.Name, out ulong prevSystem))
                {
                    double moduleDiff = module.CpuStats.CpuUsage.TotalUsage - prevModule;
                    double systemDiff = module.CpuStats.SystemCpuUsage - prevSystem;
                    result = moduleDiff / systemDiff;
                }

                this.previousModuleCpu[module.Name] = module.CpuStats.CpuUsage.TotalUsage;
                this.previousSystemCpu[module.Name] = module.CpuStats.SystemCpuUsage;

                return(result);
            }
            else
            {
                double result = 0;
                if (this.previousModuleCpu.TryGetValue(module.Name, out ulong prevModule) && this.previousReadTime.TryGetValue(module.Name, out DateTime prevTime))
                {
                    double totalIntervals = (module.Read - prevTime).TotalMilliseconds * 10; // Get number of 100ns intervals during read
                    ulong  intervalsUsed  = module.CpuStats.CpuUsage.TotalUsage - prevModule;

                    if (totalIntervals > 0)
                    {
                        result = intervalsUsed / totalIntervals;
                    }
                }

                this.previousModuleCpu[module.Name] = module.CpuStats.CpuUsage.TotalUsage;
                this.previousReadTime[module.Name]  = module.Read;

                return(result);
            }
        }
Esempio n. 5
0
        // Modeled after https://github.com/docker/cli/blob/v19.03.9/cli/command/container/stats_helpers.go#L185
        Option <double> GetCpuWindows(DockerStats module)
        {
            Option <DateTime> previousRead        = module.Name.AndThen(this.previousReadTime.GetOption);
            Option <TimeSpan> timeBetweenReadings = module.Read.AndThen(read => previousRead.Map(preRead => read - preRead));

            // Get 100ns intervals
            Option <long> intervalsPerCpu   = timeBetweenReadings.Map(tbr => (long)tbr.TotalMilliseconds * 10000);
            Option <long> possibleIntervals = intervalsPerCpu.AndThen(cpuInt => module.NumProcesses.Map(numProc => cpuInt * numProc));

            Option <double> currentTotal  = module.CpuStats.AndThen(s => s.CpuUsage).AndThen(s => s.TotalUsage);
            Option <double> previousTotal = module.Name.AndThen(this.previousModuleCpu.GetOption);
            Option <double> intervalsUsed = currentTotal.AndThen(curr => previousTotal.Map(prev => curr - prev));

            // set previous to new current
            module.Name.ForEach(name =>
            {
                currentTotal.ForEach(curr => this.previousModuleCpu[name] = curr);
                module.Read.ForEach(curr => this.previousReadTime[name]   = curr);
            });

            return(intervalsUsed.AndThen(used => possibleIntervals.AndThen(possible =>
            {
                if (possible > 0)
                {
                    double result = 100 * used / possible;

                    // Occasionally on startup results in a very large number (billions of percent). Ignore this point.
                    if (result < 100)
                    {
                        return Option.Some(result);
                    }
                }

                return Option.None <double>();
            })));
        }
Esempio n. 6
0
        Option <double> GetCpuUsage(DockerStats module, string name)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                // Get values if exist
                double totalUsage = 0, systemUsage = 0;
                if (!module.CpuStats.Exists(cpuStats => cpuStats.CpuUsage.Exists(cpuUsage => cpuUsage.TotalUsage.Exists(tu =>
                {
                    totalUsage = tu;
                    return(true);
                })) && cpuStats.SystemCpuUsage.Exists(su =>
                {
                    systemUsage = su;
                    return(true);
                })))
                {
                    // One of the values is missing, skip.
                    return(Option.None <double>());
                }

                // Calculate
                if (this.previousModuleCpu.TryGetValue(name, out double prevModule) && this.previousSystemCpu.TryGetValue(name, out double prevSystem))
                {
                    double moduleDiff = totalUsage - prevModule;
                    double systemDiff = systemUsage - prevSystem;
                    if (systemDiff > 0)
                    {
                        double result = 100 * moduleDiff / systemDiff;

                        // Occasionally on startup results in a very large number (billions of percent). Ignore this point.
                        if (result < 100)
                        {
                            return(Option.Some(result));
                        }
                    }
                }

                this.previousModuleCpu[name] = totalUsage;
                this.previousSystemCpu[name] = systemUsage;
            }
            else
            {
                // Get values if exist
                double   totalUsage = 0;
                DateTime readTime   = DateTime.MinValue;
                if (!(module.CpuStats.Exists(cpuStats => cpuStats.CpuUsage.Exists(cpuUsage => cpuUsage.TotalUsage.Exists(tu =>
                {
                    totalUsage = tu;
                    return(true);
                }))) && module.Read.Exists(read =>
                {
                    readTime = read;
                    return(true);
                })))
                {
                    // One of the values is missing, skip.
                    return(Option.None <double>());
                }

                // Calculate
                if (this.previousModuleCpu.TryGetValue(name, out double prevModule) && this.previousReadTime.TryGetValue(name, out DateTime prevTime))
                {
                    double totalIntervals = (readTime - prevTime).TotalMilliseconds * 10; // Get number of 100ns intervals during read
                    double intervalsUsed  = totalUsage - prevModule;

                    if (totalIntervals > 0)
                    {
                        return(Option.Some(100 * intervalsUsed / totalIntervals));
                    }
                }

                this.previousModuleCpu[name] = totalUsage;
                this.previousReadTime[name]  = readTime;
            }

            return(Option.None <double>());
        }