private void CollectThreadPoolMetrics(CurrentProcessMetrics metrics)
        {
            ThreadPool.GetMinThreads(out var minWorkerThreads, out var minIocpThreads);
            ThreadPool.GetMaxThreads(out var maxWorkerThreads, out var maxIocpThreads);
            ThreadPool.GetAvailableThreads(out var availableWorkerThreads, out var availableIocpThreads);

            metrics.ThreadPoolMinWorkers = minWorkerThreads;
            metrics.ThreadPoolMinIo      = minIocpThreads;

            metrics.ThreadPoolBusyWorkers = maxWorkerThreads - availableWorkerThreads;
            metrics.ThreadPoolBusyIo      = maxIocpThreads - availableIocpThreads;

            metrics.ThreadPoolTotalCount  = ThreadPoolTotalCountProvider();
            metrics.ThreadPoolQueueLength = ThreadPoolQueueLengthProvider();

            if (metrics.ThreadPoolMinWorkers > 0)
            {
                metrics.ThreadPoolWorkersUtilizedFraction = ((double)metrics.ThreadPoolBusyWorkers / metrics.ThreadPoolMinWorkers).Clamp(0, 1);
            }

            if (metrics.ThreadPoolMinIo > 0)
            {
                metrics.ThreadPoolIoUtilizedFraction = ((double)metrics.ThreadPoolBusyIo / metrics.ThreadPoolMinIo).Clamp(0, 1);
            }
        }
 private void CollectMiscMetrics(CurrentProcessMetrics metrics)
 {
     metrics.LockContentionCount = (int)lockContentionCount.Collect();
     metrics.ExceptionsCount     = (int)exceptionsCount.Collect();
     metrics.ActiveTimersCount   = ActiveTimersCountProvider();
     metrics.UptimeSeconds       = UptimeMeter.Elapsed.TotalSeconds;
 }
예제 #3
0
        public void Collect(CurrentProcessMetrics metrics)
        {
            var systemStat    = ReadSystemStat();
            var processStat   = ReadProcessStat();
            var processStatus = ReadProcessStatus();

            if (processStatus.FileDescriptorsCount.HasValue)
            {
                metrics.HandlesCount = processStatus.FileDescriptorsCount.Value;
            }

            if (processStatus.VirtualMemoryResident.HasValue)
            {
                metrics.MemoryResident = processStatus.VirtualMemoryResident.Value;
            }

            if (processStatus.VirtualMemoryData.HasValue)
            {
                metrics.MemoryPrivate = processStatus.VirtualMemoryData.Value;
            }

            if (systemStat.Filled && processStat.Filled)
            {
                var systemTime  = systemStat.SystemTime.Value + systemStat.UserTime.Value + systemStat.IdleTime.Value;
                var processTime = processStat.SystemTime.Value + processStat.UserTime.Value;

                cpuCollector.Collect(metrics, systemTime, processTime, systemStat.CpuCount);
            }
        }
예제 #4
0
 public void Collect(CurrentProcessMetrics metrics)
 {
     if (!RuntimeDetector.IsDotNet50AndNewer)
     {
         return;
     }
     metrics.DnsLookupsCount       = lookupsCounter.CollectAndReset();
     metrics.FailedDnsLookupsCount = failedLookupsCounter.CollectAndReset();
 }
        public void Collect(CurrentProcessMetrics metrics)
        {
            if (!RuntimeDetector.IsDotNet50AndNewer)
            {
                return;
            }
            metrics.OutgoingTcpConnectionsCount = outgoingTcpConnectionsCounter.CollectAndReset();
            metrics.IncomingTcpConnectionsCount = incomingTcpConnectionsCounter.CollectAndReset();
            metrics.FailedTcpConnectionsCount   = failedTcpConnectionsCounter.CollectAndReset();

            metrics.OutgoingDatagramsCount = outgoingDatagramsCounter.Collect();
            metrics.IncomingDatagramsCount = incomingDatagramsCounter.Collect();
        }
        private void CollectGCMetrics(CurrentProcessMetrics metrics)
        {
            metrics.GcAllocatedBytes  = allocatedBytes.Collect();
            metrics.GcGen0Collections = (int)gen0Collections.Collect();
            metrics.GcGen1Collections = (int)gen1Collections.Collect();
            metrics.GcGen2Collections = (int)gen2Collections.Collect();

            metrics.GcHeapSize    = GC.GetTotalMemory(false);
            metrics.GcGen0Size    = (long)GcGenerationSizeProvider(0);
            metrics.GcGen1Size    = (long)GcGenerationSizeProvider(1);
            metrics.GcGen2Size    = (long)GcGenerationSizeProvider(2);
            metrics.GcLOHSize     = (long)GcGenerationSizeProvider(3);
            metrics.GcTimePercent = GcTimeLastPercentProvider();
        }
        private static void CollectHandlesCount(CurrentProcessMetrics metrics)
        {
            try
            {
                if (!GetProcessHandleCount(CurrentProcessHandle, out var handleCount))
                {
                    WinMetricsCollectorHelper.ThrowOnError();
                }

                metrics.HandlesCount = (int)handleCount;
            }
            catch (Exception error)
            {
                InternalErrorLogger.Warn(error);
            }
        }
        private static unsafe void CollectMemoryMetrics(CurrentProcessMetrics metrics)
        {
            try
            {
                if (!GetProcessMemoryInfo(CurrentProcessHandle, out var memoryCounters, sizeof(PROCESS_MEMORY_COUNTERS_EX)))
                {
                    WinMetricsCollectorHelper.ThrowOnError();
                }

                metrics.MemoryResident = (long)memoryCounters.WorkingSetSize;
                metrics.MemoryPrivate  = (long)memoryCounters.PrivateUsage;
            }
            catch (Exception error)
            {
                InternalErrorLogger.Warn(error);
            }
        }
        private void CollectLimitsMetrics(CurrentProcessMetrics metrics)
        {
            metrics.NullableCpuLimitCores = settings.CpuCoresLimitProvider?.Invoke();
            metrics.CpuLimitCores         = metrics.NullableCpuLimitCores ?? Environment.ProcessorCount;

            if (metrics.CpuLimitCores > 0)
            {
                metrics.CpuUtilizedFraction = (metrics.CpuUtilizedCores / metrics.CpuLimitCores).Clamp(0, 1);
            }

            metrics.NullableMemoryLimit = settings.MemoryBytesLimitProvider?.Invoke();
            metrics.MemoryLimit         = metrics.NullableMemoryLimit ?? totalHostMemory.Value;

            if (metrics.MemoryLimit > 0)
            {
                metrics.MemoryUtilizedFraction = ((double)metrics.MemoryResident / metrics.MemoryLimit).Clamp(0, 1);
            }
        }
        public CurrentProcessMetrics Collect()
        {
            var metrics = new CurrentProcessMetrics();

            CollectThreadPoolMetrics(metrics);

            CollectGCMetrics(metrics);

            CollectMiscMetrics(metrics);

            CollectNativeMetrics(metrics);

            CollectLimitsMetrics(metrics);

            CollectSocketMetrics(metrics);

            CollectDnsMetrics(metrics);

            return(metrics);
        }
예제 #11
0
        public void Collect(CurrentProcessMetrics metrics, ulong systemTime, ulong processTime, int?systemCores = null)
        {
            var systemTimeDiff  = (double)systemTime - previousSystemTime;
            var processTimeDiff = (double)processTime - previousProcessTime;

            if (previousSystemTime == 0 || systemTimeDiff <= 0)
            {
                metrics.CpuUtilizedCores    = 0d;
                metrics.CpuUtilizedFraction = 0d;
            }
            else
            {
                var cores = systemCores ?? DefaultCoresCount;

                metrics.CpuUtilizedCores    = (cores * processTimeDiff / systemTimeDiff).Clamp(0, cores);
                metrics.CpuUtilizedFraction = (processTimeDiff / systemTimeDiff).Clamp(0, 1);
            }

            previousSystemTime  = systemTime;
            previousProcessTime = processTime;
        }
        private void CollectCpuUtilization(CurrentProcessMetrics metrics)
        {
            try
            {
                if (!WinMetricsCollectorHelper.GetSystemTimes(out _, out var systemKernel, out var systemUser))
                {
                    WinMetricsCollectorHelper.ThrowOnError();
                }

                if (!GetProcessTimes(CurrentProcessHandle, out _, out _, out var processKernel, out var processUser))
                {
                    WinMetricsCollectorHelper.ThrowOnError();
                }

                var systemTime  = systemKernel.ToUInt64() + systemUser.ToUInt64();
                var processTime = processKernel.ToUInt64() + processUser.ToUInt64();

                cpuCollector.Collect(metrics, systemTime, processTime);
            }
            catch (Exception error)
            {
                InternalErrorLogger.Warn(error);
            }
        }
 public void Collect(CurrentProcessMetrics metrics)
 {
     CollectMemoryMetrics(metrics);
     CollectHandlesCount(metrics);
     CollectCpuUtilization(metrics);
 }
 private void CollectDnsMetrics(CurrentProcessMetrics metrics) =>
 dnsObserver.Collect(metrics);
 private void CollectSocketMetrics(CurrentProcessMetrics metrics) =>
 socketMonitor.Collect(metrics);
 private void CollectNativeMetrics(CurrentProcessMetrics metrics)
 => nativeCollector?.Invoke(metrics);