public static (long Rss, long Swap) GetMemoryUsageFromProcStatus()
        {
            var path = $"/proc/{ProcessId}/status";

            try
            {
                using (var bufferedReader = new KernelVirtualFileSystemUtils.BufferedPosixKeyValueOutputValueReader(path))
                {
                    bufferedReader.ReadFileIntoBuffer();
                    var vmrss  = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(VmRss);
                    var vmswap = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(VmSwap);

                    // value is in KB, we need to return bytes
                    return(vmrss * 1024, vmswap * 1024);
                }
            }
            catch (Exception ex)
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info($"Failed to read value from {path}", ex);
                }
                return(-1, -1);
            }
        }
Beispiel #2
0
        public static (long Committed_AS, long CommitLimit) GetCommitAsAndCommitLimitFromProcMemInfo()
        {
            // MemFree is really different then MemAvailable (while free is usually lower then the real free,
            // and available is only estimated free which sometimes higher then the real free memory)
            var path = "/proc/meminfo";

            try
            {
                using (var bufferedReader = new KernelVirtualFileSystemUtils.BufferedPosixKeyValueOutputValueReader(path))
                {
                    bufferedReader.ReadFileIntoBuffer();
                    var committedAs = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(Committed_AS);
                    var commitLimit = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(CommitLimit);
                    return(Committed_AS : committedAs, CommitLimit : commitLimit);
                }
            }
            catch (Exception ex)
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info($"Failed to read value from {path}", ex);
                }
                return(-1, -1);
            }
        }
Beispiel #3
0
        internal static bool GetFromProcMemInfo(SmapsReader smapsReader, ref ProcMemInfoResults procMemInfoResults)
        {
            const string path = "/proc/meminfo";

            // this is different then sysinfo freeram+buffered (and the closest to the real free memory)
            // MemFree is really different then MemAvailable (while free is usually lower then the real free,
            // and available is only estimated free which sometimes higher then the real free memory)
            // for some distros we have only MemFree
            try
            {
                using (var bufferedReader = new KernelVirtualFileSystemUtils.BufferedPosixKeyValueOutputValueReader(path))
                {
                    bufferedReader.ReadFileIntoBuffer();
                    var memAvailableInKb = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(MemAvailable);
                    var memFreeInKb      = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(MemFree);
                    var totalMemInKb     = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(MemTotal);
                    var swapTotalInKb    = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(SwapTotal);
                    var commitedInKb     = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(Committed_AS);

                    var totalClean        = new Size(memAvailableInKb, SizeUnit.Kilobytes);
                    var totalDirty        = new Size(0, SizeUnit.Bytes);
                    var sharedCleanMemory = new Size(0, SizeUnit.Bytes);
                    if (smapsReader != null)
                    {
                        var result = smapsReader.CalculateMemUsageFromSmaps <SmapsReaderNoAllocResults>();
                        totalClean.Add(result.SharedClean, SizeUnit.Bytes);
                        totalClean.Add(result.PrivateClean, SizeUnit.Bytes);
                        sharedCleanMemory.Set(result.SharedClean, SizeUnit.Bytes);
                        totalDirty.Add(result.TotalDirty, SizeUnit.Bytes);
                    }

                    procMemInfoResults.MemAvailable = new Size(Math.Max(memAvailableInKb, memFreeInKb), SizeUnit.Kilobytes);
                    procMemInfoResults.TotalMemory  = new Size(totalMemInKb, SizeUnit.Kilobytes);
                    procMemInfoResults.Commited     = new Size(commitedInKb, SizeUnit.Kilobytes);

                    // on Linux, we use the swap + ram as the commit limit, because the actual limit
                    // is dependent on many different factors
                    procMemInfoResults.CommitLimit = new Size(totalMemInKb + swapTotalInKb, SizeUnit.Kilobytes);
                    procMemInfoResults.AvailableWithoutTotalCleanMemory = totalClean;
                    procMemInfoResults.SharedCleanMemory = sharedCleanMemory;
                    procMemInfoResults.TotalDirty        = totalDirty;
                }
            }
            catch (Exception ex)
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info($"Failed to read value from {path}", ex);
                }

                return(false);
            }

            return(true);
        }
Beispiel #4
0
        public static (Size MemAvailable, Size TotalMemory, Size Commited, Size CommitLimit) GetFromProcMemInfo()
        {
            const string path = "/proc/meminfo";

            // this is different then sysinfo freeram+buffered (and the closest to the real free memory)
            // MemFree is really different then MemAvailable (while free is usually lower then the real free,
            // and available is only estimated free which sometimes higher then the real free memory)
            // for some distros we have only MemFree
            try
            {
                using (var bufferedReader = new KernelVirtualFileSystemUtils.BufferedPosixKeyValueOutputValueReader(path))
                {
                    bufferedReader.ReadFileIntoBuffer();
                    var memAvailableInKb = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(MemAvailable);
                    var memFreeInKb      = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(MemFree);
                    var totalMemInKb     = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(MemTotal);
                    var swapTotalInKb    = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(SwapTotal);
                    var commitedInKb     = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(Committed_AS);
                    return(
                        MemAvailable : new Size(Math.Max(memAvailableInKb, memFreeInKb), SizeUnit.Kilobytes),
                        TotalMemory : new Size(totalMemInKb, SizeUnit.Kilobytes),
                        Commited : new Size(commitedInKb, SizeUnit.Kilobytes),

                        // on Linux, we use the swap + ram as the commit limit, because the actual limit
                        // is dependent on many different factors
                        CommitLimit : new Size(totalMemInKb + swapTotalInKb, SizeUnit.Kilobytes)
                        );
                }
            }
            catch (Exception ex)
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info($"Failed to read value from {path}", ex);
                }

                return(new Size(), new Size(), new Size(), new Size());
            }
        }
Beispiel #5
0
        public static (long MemAvailable, long MemFree) GetAvailableAndFreeMemoryFromProcMemInfo()
        {
            var path = "/proc/meminfo";

            // this is different then sysinfo freeram+buffered (and the closest to the real free memory)
            try
            {
                using (var bufferedReader = new KernelVirtualFileSystemUtils.BufferedPosixKeyValueOutputValueReader(path))
                {
                    bufferedReader.ReadFileIntoBuffer();
                    var memAvailable = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(MemAvailable);
                    var memFree      = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(MemFree);
                    return(MemAvailable : memAvailable, MemFree : memFree);
                }
            }
            catch (Exception ex)
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info($"Failed to read value from {path}", ex);
                }
                return(-1, -1);
            }
        }
        private static bool GetFromProcMemInfo(SmapsReader smapsReader, ref ProcMemInfoResults procMemInfoResults)
        {
            const string path = "/proc/meminfo";

            // this is different then sysinfo freeram+buffered (and the closest to the real free memory)
            // MemFree is really different then MemAvailable (while free is usually lower then the real free,
            // and available is only estimated free which sometimes higher then the real free memory)
            // for some distros we have only MemFree
            try
            {
                using (var bufferedReader = new KernelVirtualFileSystemUtils.BufferedPosixKeyValueOutputValueReader(path))
                {
                    bufferedReader.ReadFileIntoBuffer();
                    var memAvailableInKb = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(MemAvailable);
                    var memFreeInKb      = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(MemFree);
                    var totalMemInKb     = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(MemTotal);
                    var swapTotalInKb    = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(SwapTotal);
                    var commitedInKb     = bufferedReader.ExtractNumericValueFromKeyValuePairsFormattedFile(Committed_AS);

                    var totalClean        = new Size(0, SizeUnit.Kilobytes);
                    var totalDirty        = new Size(0, SizeUnit.Bytes);
                    var sharedCleanMemory = new Size(0, SizeUnit.Bytes);
                    var swapWorkingSet    = new Size(0, SizeUnit.Bytes);
                    if (smapsReader != null)
                    {
                        var result = smapsReader.CalculateMemUsageFromSmaps <SmapsReaderNoAllocResults>();
                        totalClean.Add(result.SharedClean, SizeUnit.Bytes);
                        totalClean.Add(result.PrivateClean, SizeUnit.Bytes);
                        sharedCleanMemory.Set(result.SharedClean, SizeUnit.Bytes);
                        totalDirty.Add(result.TotalDirty, SizeUnit.Bytes);
                        swapWorkingSet.Add(result.Swap, SizeUnit.Bytes);
                    }

                    procMemInfoResults.AvailableMemory = new Size(memFreeInKb, SizeUnit.Kilobytes);
                    procMemInfoResults.TotalMemory     = new Size(totalMemInKb, SizeUnit.Kilobytes);
                    procMemInfoResults.Commited        = new Size(commitedInKb, SizeUnit.Kilobytes);

                    // on Linux, we use the swap + ram as the commit limit, because the actual limit
                    // is dependent on many different factors
                    procMemInfoResults.CommitLimit = new Size(totalMemInKb + swapTotalInKb, SizeUnit.Kilobytes);

                    // AvailableMemoryForProcessing: AvailableMemory actually does add reclaimable memory (divided by 2), so if AvailableMemory is equal or lower then the _real_ available memory
                    // If it is lower the the real value because of RavenDB's Clean memory - then we use 'totalClean' as reference
                    // Otherwise - either it is correct value, or it is lower because of (clean or dirty memory of) another process
                    var availableMemoryForProcessing = new Size(Math.Max(memAvailableInKb, memFreeInKb), SizeUnit.Kilobytes);
                    procMemInfoResults.AvailableMemoryForProcessing = Size.Max(availableMemoryForProcessing, totalClean);

                    procMemInfoResults.SharedCleanMemory = sharedCleanMemory;
                    procMemInfoResults.TotalDirty        = totalDirty;
                    procMemInfoResults.TotalSwap         = new Size(swapTotalInKb, SizeUnit.Kilobytes);
                    procMemInfoResults.WorkingSetSwap    = swapWorkingSet;
                }
            }
            catch (Exception ex)
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info($"Failed to read value from {path}", ex);
                }

                return(false);
            }

            return(true);
        }