private double?GetAvailablePhysicalBytesMacOS()
        {
            double?      availableAvailablePhysicalBytes = null;
            RamUsageInfo ramUsageInfo = new RamUsageInfo();

            unsafe
            {
                if (GetRamUsageInfo(&ramUsageInfo) == MACOS_INTEROP_SUCCESS)
                {
                    // The 'free' portion of GetRamUsageInfo() indicates how many physical RAM is still available,
                    // we have to look at VM statistics to calculate the actual available RAM though, akin to ActivityMonitor on macOS.
                    availableAvailablePhysicalBytes =
                        OperatingSystemHelper.GetPhysicalMemorySize().Bytes - (ramUsageInfo.Active + ramUsageInfo.Speculative +
                                                                               ramUsageInfo.Wired + ramUsageInfo.Compressed - ramUsageInfo.Purgable);
                }
            }

            return(availableAvailablePhysicalBytes);
        }
Exemple #2
0
        private void CollectOnce()
        {
            lock (m_collectLock)
            {
                // This must be reacquired for every collection and may not be cached because some of the fields like memory
                // usage are only set in the Process() constructor
                Process currentProcess = Process.GetCurrentProcess();

                // Compute the performance data
                double?machineCpu             = 0.0;
                double?processCpu             = GetProcessCpu(currentProcess);
                double processThreads         = currentProcess.Threads.Count;
                double processPrivateBytes    = currentProcess.PrivateMemorySize64;
                double processWorkingSetBytes = currentProcess.WorkingSet64;
                double processHeldBytes       = m_collectHeldBytesFromGC ? GC.GetTotalMemory(forceFullCollection: true) : 0;

                double?            machineAvailablePhysicalBytes = null;
                double?            machineTotalPhysicalBytes     = null;
                double?            commitTotalBytes = null;
                double?            commitLimitBytes = null;
                DISK_PERFORMANCE[] diskStats        = null;

                if (!OperatingSystemHelper.IsUnixOS)
                {
                    machineCpu = GetMachineCpu();

                    MEMORYSTATUSEX memoryStatusEx = new MEMORYSTATUSEX();
                    if (GlobalMemoryStatusEx(memoryStatusEx))
                    {
                        machineAvailablePhysicalBytes = memoryStatusEx.ullAvailPhys;
                        machineTotalPhysicalBytes     = memoryStatusEx.ullTotalPhys;
                    }

                    PERFORMANCE_INFORMATION performanceInfo = PERFORMANCE_INFORMATION.CreatePerfInfo();
                    if (GetPerformanceInfo(out performanceInfo, performanceInfo.cb))
                    {
                        commitTotalBytes = performanceInfo.CommitTotal.ToInt64() * performanceInfo.PageSize.ToInt64();
                        commitLimitBytes = performanceInfo.CommitLimit.ToInt64() * performanceInfo.PageSize.ToInt64();
                    }

                    diskStats = GetDiskCounters();
                }
                else
                {
                    machineCpu = GetMachineCpuMacOS();
                    ulong totalPhysicalBytes = OperatingSystemHelper.GetPhysicalMemorySize().Bytes;
                    machineTotalPhysicalBytes     = totalPhysicalBytes;
                    machineAvailablePhysicalBytes = GetAvailablePhysicalBytesMacOS(totalPhysicalBytes);
                }

                // stop network monitor measurement and gather data
                m_networkMonitor?.StopMeasurement();

                DateTime temp     = DateTime.UtcNow;
                TimeSpan duration = temp - m_networkTimeLastCollectedAt;
                m_networkTimeLastCollectedAt = temp;

                double?machineKbitsPerSecSent     = null;
                double?machineKbitsPerSecReceived = null;

                if (m_networkMonitor != null)
                {
                    machineKbitsPerSecSent     = Math.Round(1000 * BytesToKbits(m_networkMonitor.TotalSentBytes) / Math.Max(duration.TotalMilliseconds, 1.0), 3);
                    machineKbitsPerSecReceived = Math.Round(1000 * BytesToKbits(m_networkMonitor.TotalReceivedBytes) / Math.Max(duration.TotalMilliseconds, 1.0), 3);
                }

                // Update the aggregators
                lock (m_aggregators)
                {
                    foreach (var aggregator in m_aggregators)
                    {
                        aggregator.RegisterSample(
                            processCpu: processCpu,
                            processPrivateBytes: processPrivateBytes,
                            processWorkingSetBytes: processWorkingSetBytes,
                            threads: processThreads,
                            machineCpu: machineCpu,
                            machineTotalPhysicalBytes: machineTotalPhysicalBytes,
                            machineAvailablePhysicalBytes: machineAvailablePhysicalBytes,
                            commitTotalBytes: commitTotalBytes,
                            commitLimitBytes: commitLimitBytes,
                            machineBandwidth: m_networkMonitor?.Bandwidth,
                            machineKbitsPerSecSent: machineKbitsPerSecSent,
                            machineKbitsPerSecReceived: machineKbitsPerSecReceived,
                            diskPerformance: diskStats,
                            gcHeldBytes: processHeldBytes);
                    }
                }

                // restart network monitor to start new measurement
                m_networkMonitor?.StartMeasurement();
            }
        }