Exemple #1
0
        public static DynamicJsonValue LowMemLogInternal()
        {
            var lowMemLog = LowMemoryNotification.Instance.LowMemEventDetailsStack;
            var dja       = new DynamicJsonArray();

            foreach (var item in lowMemLog.OrderByDescending(x =>
            {
                if (x != null)
                {
                    return(x.Time);
                }
                return(DateTime.MinValue);
            }))
            {
                if (item == null || item.Reason == LowMemoryNotification.LowMemReason.None)
                {
                    continue;
                }

                var humanSizes = new DynamicJsonValue
                {
                    ["FreeMem"]             = Size.Humane(item.FreeMem),
                    ["CurrentCommitCharge"] = Size.Humane(item.CurrentCommitCharge),
                    ["TotalUnmanaged"]      = Size.Humane(item.TotalUnmanaged),
                    ["TotalScratchDirty"]   = Size.Humane(item.TotalScratchDirty),
                    ["PhysicalMem"]         = Size.Humane(item.PhysicalMem),
                    ["Threshold"]           = Size.Humane(item.LowMemThreshold)
                };

                var json = new DynamicJsonValue
                {
                    ["Event"]               = item.Reason,
                    ["FreeMem"]             = item.FreeMem,
                    ["CurrentCommitCharge"] = item.CurrentCommitCharge,
                    ["TotalUnmanaged"]      = item.TotalUnmanaged,
                    ["TotalScratchDirty"]   = item.TotalScratchDirty,
                    ["PhysicalMem"]         = item.PhysicalMem,
                    ["TimeOfEvent"]         = item.Time,
                    ["HumanlyReadSizes"]    = humanSizes
                };

                dja.Add(json);
            }

            var djv = new DynamicJsonValue
            {
                ["Low Memory Events"] = dja
            };

            return(djv);
        }
Exemple #2
0
        private static void WriteThreads(bool includeThreads, BlittableJsonTextWriter writer, JsonOperationContext context)
        {
            if (includeThreads == false)
            {
                return;
            }

            var isFirst = true;

            foreach (var stats in NativeMemory.AllThreadStats
                     .Where(x => x.IsThreadAlive())
                     .GroupBy(x => x.Name)
                     .OrderByDescending(x => x.Sum(y => y.TotalAllocated)))
            {
                var unmanagedAllocations = stats.Sum(x => x.TotalAllocated);
                var ids = new DynamicJsonArray(stats.OrderByDescending(x => x.TotalAllocated).Select(x => new DynamicJsonValue
                {
                    ["Id"] = x.UnmanagedThreadId,
                    ["ManagedThreadId"]   = x.ManagedThreadId,
                    ["Allocations"]       = x.TotalAllocated,
                    ["HumaneAllocations"] = Size.Humane(x.TotalAllocated)
                }));
                var groupStats = new DynamicJsonValue
                {
                    ["Name"]              = stats.Key,
                    ["Allocations"]       = unmanagedAllocations,
                    ["HumaneAllocations"] = Size.Humane(unmanagedAllocations)
                };
                if (ids.Count == 1)
                {
                    var threadStats = stats.First();
                    groupStats["Id"] = threadStats.UnmanagedThreadId;
                    groupStats["ManagedThreadId"] = threadStats.ManagedThreadId;
                }
                else
                {
                    groupStats["Ids"] = ids;
                }

                if (isFirst == false)
                {
                    writer.WriteComma();
                }

                isFirst = false;

                context.Write(writer, groupStats);
            }
        }
 static DynamicJsonValue ToJson(GCMemoryInfo info)
 {
     return(new DynamicJsonValue
     {
         [nameof(info.Compacted)] = info.Compacted,
         [nameof(info.Concurrent)] = info.Concurrent,
         [nameof(info.FinalizationPendingCount)] = info.FinalizationPendingCount,
         [nameof(info.FragmentedBytes)] = info.FragmentedBytes,
         ["FragmentedHumane"] = Size.Humane(info.FragmentedBytes),
         [nameof(info.Generation)] = info.Generation,
         [nameof(info.GenerationInfo)] = new DynamicJsonArray(info.GenerationInfo.ToArray().Select(x => new DynamicJsonValue
         {
             [nameof(x.FragmentationAfterBytes)] = x.FragmentationAfterBytes,
             ["FragmentationAfterHumane"] = Size.Humane(x.FragmentationAfterBytes),
             [nameof(x.FragmentationBeforeBytes)] = x.FragmentationBeforeBytes,
             ["FragmentationBeforeHumane"] = Size.Humane(x.FragmentationBeforeBytes),
             [nameof(x.SizeAfterBytes)] = x.SizeAfterBytes,
             ["SizeAfterHumane"] = Size.Humane(x.SizeAfterBytes),
             [nameof(x.SizeBeforeBytes)] = x.SizeBeforeBytes,
             ["SizeBeforeHumane"] = Size.Humane(x.SizeBeforeBytes)
         })),
         [nameof(info.HeapSizeBytes)] = info.HeapSizeBytes,
         ["HeapSizeHumane"] = Size.Humane(info.HeapSizeBytes),
         [nameof(info.HighMemoryLoadThresholdBytes)] = info.HighMemoryLoadThresholdBytes,
         ["HighMemoryLoadThresholdHumane"] = Size.Humane(info.HighMemoryLoadThresholdBytes),
         [nameof(info.Index)] = info.Index,
         [nameof(info.MemoryLoadBytes)] = info.MemoryLoadBytes,
         ["MemoryLoadHumane"] = Size.Humane(info.MemoryLoadBytes),
         [nameof(info.PauseDurations)] = new DynamicJsonArray(info.PauseDurations.ToArray().Cast <object>()),
         [nameof(info.PauseTimePercentage)] = info.PauseTimePercentage,
         [nameof(info.PinnedObjectsCount)] = info.PinnedObjectsCount,
         [nameof(info.PromotedBytes)] = info.PromotedBytes,
         ["PromotedHumane"] = Size.Humane(info.PromotedBytes),
         [nameof(info.TotalAvailableMemoryBytes)] = info.TotalAvailableMemoryBytes,
         ["TotalAvailableMemoryHumane"] = Size.Humane(info.TotalAvailableMemoryBytes),
         [nameof(info.TotalCommittedBytes)] = info.TotalCommittedBytes,
         ["TotalCommittedHumane"] = Size.Humane(info.TotalCommittedBytes)
     });
 }
        private static DynamicJsonValue MemoryStatsInternal()
        {
            using (var currentProcess = Process.GetCurrentProcess())
            {
                var workingSet =
                    PlatformDetails.RunningOnPosix == false || PlatformDetails.RunningOnMacOsx
                        ? currentProcess.WorkingSet64
                        : MemoryInformation.GetRssMemoryUsage(currentProcess.Id);
                var memInfo = MemoryInformation.GetMemoryInfo();

                long totalMapping          = 0;
                var  fileMappingByDir      = new Dictionary <string, Dictionary <string, ConcurrentDictionary <IntPtr, long> > >();
                var  fileMappingSizesByDir = new Dictionary <string, long>();
                foreach (var mapping in NativeMemory.FileMapping)
                {
                    var dir = Path.GetDirectoryName(mapping.Key);

                    if (fileMappingByDir.TryGetValue(dir, out Dictionary <string, ConcurrentDictionary <IntPtr, long> > value) == false)
                    {
                        value = new Dictionary <string, ConcurrentDictionary <IntPtr, long> >();
                        fileMappingByDir[dir] = value;
                    }
                    value[mapping.Key] = mapping.Value;
                    foreach (var singleMapping in mapping.Value)
                    {
                        fileMappingSizesByDir.TryGetValue(dir, out long prevSize);
                        fileMappingSizesByDir[dir] = prevSize + singleMapping.Value;
                        totalMapping += singleMapping.Value;
                    }
                }

                var prefixLength = LongestCommonPrefixLength(new List <string>(fileMappingSizesByDir.Keys));

                var fileMappings = new DynamicJsonArray();
                foreach (var sizes in fileMappingSizesByDir.OrderByDescending(x => x.Value))
                {
                    if (fileMappingByDir.TryGetValue(sizes.Key, out Dictionary <string, ConcurrentDictionary <IntPtr, long> > value))
                    {
                        var details = new DynamicJsonValue();

                        var dir = new DynamicJsonValue
                        {
                            [nameof(MemoryInfoMappingItem.Directory)]                = sizes.Key.Substring(prefixLength),
                            [nameof(MemoryInfoMappingItem.TotalDirectorySize)]       = sizes.Value,
                            [nameof(MemoryInfoMappingItem.HumaneTotalDirectorySize)] = Size.Humane(sizes.Value),
                            [nameof(MemoryInfoMappingItem.Details)] = details
                        };
                        foreach (var file in value.OrderBy(x => x.Key))
                        {
                            long totalMapped = 0;
                            var  dja         = new DynamicJsonArray();
                            var  dic         = new Dictionary <long, long>();
                            foreach (var mapping in file.Value)
                            {
                                totalMapped += mapping.Value;
                                dic.TryGetValue(mapping.Value, out long prev);
                                dic[mapping.Value] = prev + 1;
                            }
                            foreach (var maps in dic)
                            {
                                dja.Add(new DynamicJsonValue
                                {
                                    [nameof(MemoryInfoMappingDetails.Size)]  = maps.Key,
                                    [nameof(MemoryInfoMappingDetails.Count)] = maps.Value
                                });
                            }
                            var fileSize = GetFileSize(file.Key);
                            details[Path.GetFileName(file.Key)] = new DynamicJsonValue
                            {
                                [nameof(MemoryInfoMappingFileInfo.FileSize)]          = fileSize,
                                [nameof(MemoryInfoMappingFileInfo.HumaneFileSize)]    = Size.Humane(fileSize),
                                [nameof(MemoryInfoMappingFileInfo.TotalMapped)]       = totalMapped,
                                [nameof(MemoryInfoMappingFileInfo.HumaneTotalMapped)] = Size.Humane(totalMapped),
                                [nameof(MemoryInfoMappingFileInfo.Mappings)]          = dja
                            };
                        }
                        fileMappings.Add(dir);
                    }
                }

                long totalUnmanagedAllocations = 0;
                var  threads = new DynamicJsonArray();
                foreach (var stats in NativeMemory.ThreadAllocations.Values
                         .Where(x => x.IsThreadAlive())
                         .GroupBy(x => x.Name)
                         .OrderByDescending(x => x.Sum(y => y.TotalAllocated)))
                {
                    var unmanagedAllocations = stats.Sum(x => x.TotalAllocated);
                    totalUnmanagedAllocations += unmanagedAllocations;
                    var ids = new DynamicJsonArray(stats.OrderByDescending(x => x.TotalAllocated).Select(x => new DynamicJsonValue
                    {
                        ["Id"] = x.UnmanagedThreadId,
                        ["ManagedThreadId"]   = x.Id,
                        ["Allocations"]       = x.TotalAllocated,
                        ["HumaneAllocations"] = Size.Humane(x.TotalAllocated)
                    }));
                    var groupStats = new DynamicJsonValue
                    {
                        ["Name"]              = stats.Key,
                        ["Allocations"]       = unmanagedAllocations,
                        ["HumaneAllocations"] = Size.Humane(unmanagedAllocations)
                    };
                    if (ids.Count == 1)
                    {
                        var threadStats = stats.First();
                        groupStats["Id"] = threadStats.UnmanagedThreadId;
                        groupStats["ManagedThreadId"] = threadStats.Id;
                    }
                    else
                    {
                        groupStats["Ids"] = ids;
                    }
                    threads.Add(groupStats);
                }
                var managedMemory = GC.GetTotalMemory(false);
                var djv           = new DynamicJsonValue
                {
                    [nameof(MemoryInfo.WorkingSet)] = workingSet,
                    [nameof(MemoryInfo.TotalUnmanagedAllocations)] = totalUnmanagedAllocations,
                    [nameof(MemoryInfo.ManagedAllocations)]        = managedMemory,
                    [nameof(MemoryInfo.TotalMemoryMapped)]         = totalMapping,
                    [nameof(MemoryInfo.PhysicalMem)]           = Size.Humane(memInfo.TotalPhysicalMemory.GetValue(SizeUnit.Bytes)),
                    [nameof(MemoryInfo.FreeMem)]               = Size.Humane(memInfo.AvailableMemory.GetValue(SizeUnit.Bytes)),
                    [nameof(MemoryInfo.HighMemLastOneMinute)]  = Size.Humane(memInfo.MemoryUsageRecords.High.LastOneMinute.GetValue(SizeUnit.Bytes)),
                    [nameof(MemoryInfo.LowMemLastOneMinute)]   = Size.Humane(memInfo.MemoryUsageRecords.Low.LastOneMinute.GetValue(SizeUnit.Bytes)),
                    [nameof(MemoryInfo.HighMemLastFiveMinute)] = Size.Humane(memInfo.MemoryUsageRecords.High.LastFiveMinutes.GetValue(SizeUnit.Bytes)),
                    [nameof(MemoryInfo.LowMemLastFiveMinute)]  = Size.Humane(memInfo.MemoryUsageRecords.Low.LastFiveMinutes.GetValue(SizeUnit.Bytes)),
                    [nameof(MemoryInfo.HighMemSinceStartup)]   = Size.Humane(memInfo.MemoryUsageRecords.High.SinceStartup.GetValue(SizeUnit.Bytes)),
                    [nameof(MemoryInfo.LowMemSinceStartup)]    = Size.Humane(memInfo.MemoryUsageRecords.Low.SinceStartup.GetValue(SizeUnit.Bytes)),

                    [nameof(MemoryInfo.Humane)] = new DynamicJsonValue
                    {
                        [nameof(MemoryInfoHumane.WorkingSet)] = Size.Humane(workingSet),
                        [nameof(MemoryInfoHumane.TotalUnmanagedAllocations)] = Size.Humane(totalUnmanagedAllocations),
                        [nameof(MemoryInfoHumane.ManagedAllocations)]        = Size.Humane(managedMemory),
                        [nameof(MemoryInfoHumane.TotalMemoryMapped)]         = Size.Humane(totalMapping)
                    },

                    ["Threads"] = threads,

                    [nameof(MemoryInfo.Mappings)] = fileMappings
                };
                return(djv);
            }
        }
Exemple #5
0
        private static DynamicJsonValue MemoryStatsInternal()
        {
            var memInfo            = MemoryInformation.GetMemoryInformationUsingOneTimeSmapsReader();
            var memoryUsageRecords = MemoryInformation.GetMemoryUsageRecords();

            long totalMapping          = 0;
            var  fileMappingByDir      = new Dictionary <string, Dictionary <string, ConcurrentDictionary <IntPtr, long> > >();
            var  fileMappingSizesByDir = new Dictionary <string, long>();

            foreach (var mapping in NativeMemory.FileMapping)
            {
                var dir = Path.GetDirectoryName(mapping.Key);

                if (fileMappingByDir.TryGetValue(dir, out Dictionary <string, ConcurrentDictionary <IntPtr, long> > value) == false)
                {
                    value = new Dictionary <string, ConcurrentDictionary <IntPtr, long> >();
                    fileMappingByDir[dir] = value;
                }

                value[mapping.Key] = mapping.Value.Value.Info;
                foreach (var singleMapping in mapping.Value.Value.Info)
                {
                    fileMappingSizesByDir.TryGetValue(dir, out long prevSize);
                    fileMappingSizesByDir[dir] = prevSize + singleMapping.Value;
                    totalMapping += singleMapping.Value;
                }
            }

            var prefixLength = LongestCommonPrefixLength(new List <string>(fileMappingSizesByDir.Keys));

            var fileMappings = new DynamicJsonArray();

            foreach (var sizes in fileMappingSizesByDir.OrderByDescending(x => x.Value))
            {
                if (fileMappingByDir.TryGetValue(sizes.Key, out Dictionary <string, ConcurrentDictionary <IntPtr, long> > value))
                {
                    var details = new DynamicJsonValue();

                    var dir = new DynamicJsonValue
                    {
                        [nameof(MemoryInfoMappingItem.Directory)]                = sizes.Key.Substring(prefixLength),
                        [nameof(MemoryInfoMappingItem.TotalDirectorySize)]       = sizes.Value,
                        [nameof(MemoryInfoMappingItem.HumaneTotalDirectorySize)] = Size.Humane(sizes.Value),
                        [nameof(MemoryInfoMappingItem.Details)] = details
                    };
                    foreach (var file in value.OrderBy(x => x.Key))
                    {
                        long totalMapped = 0;
                        var  dja         = new DynamicJsonArray();
                        var  dic         = new Dictionary <long, long>();
                        foreach (var mapping in file.Value)
                        {
                            totalMapped += mapping.Value;
                            dic.TryGetValue(mapping.Value, out long prev);
                            dic[mapping.Value] = prev + 1;
                        }

                        foreach (var maps in dic)
                        {
                            dja.Add(new DynamicJsonValue
                            {
                                [nameof(MemoryInfoMappingDetails.Size)]  = maps.Key,
                                [nameof(MemoryInfoMappingDetails.Count)] = maps.Value
                            });
                        }

                        var fileSize = GetFileSize(file.Key);
                        details[Path.GetFileName(file.Key)] = new DynamicJsonValue
                        {
                            [nameof(MemoryInfoMappingFileInfo.FileSize)]          = fileSize,
                            [nameof(MemoryInfoMappingFileInfo.HumaneFileSize)]    = Size.Humane(fileSize),
                            [nameof(MemoryInfoMappingFileInfo.TotalMapped)]       = totalMapped,
                            [nameof(MemoryInfoMappingFileInfo.HumaneTotalMapped)] = Size.Humane(totalMapped),
                            [nameof(MemoryInfoMappingFileInfo.Mappings)]          = dja
                        };
                    }

                    fileMappings.Add(dir);
                }
            }

            long totalUnmanagedAllocations = NativeMemory.TotalAllocatedMemory;
            var  threads = new DynamicJsonArray();

            foreach (var stats in NativeMemory.AllThreadStats
                     .Where(x => x.IsThreadAlive())
                     .GroupBy(x => x.Name)
                     .OrderByDescending(x => x.Sum(y => y.TotalAllocated)))
            {
                var unmanagedAllocations = stats.Sum(x => x.TotalAllocated);
                var ids = new DynamicJsonArray(stats.OrderByDescending(x => x.TotalAllocated).Select(x => new DynamicJsonValue
                {
                    ["Id"] = x.UnmanagedThreadId,
                    ["ManagedThreadId"]   = x.ManagedThreadId,
                    ["Allocations"]       = x.TotalAllocated,
                    ["HumaneAllocations"] = Size.Humane(x.TotalAllocated)
                }));
                var groupStats = new DynamicJsonValue
                {
                    ["Name"]              = stats.Key,
                    ["Allocations"]       = unmanagedAllocations,
                    ["HumaneAllocations"] = Size.Humane(unmanagedAllocations)
                };
                if (ids.Count == 1)
                {
                    var threadStats = stats.First();
                    groupStats["Id"] = threadStats.UnmanagedThreadId;
                    groupStats["ManagedThreadId"] = threadStats.ManagedThreadId;
                }
                else
                {
                    groupStats["Ids"] = ids;
                }

                threads.Add(groupStats);
            }

            long managedMemoryInBytes = AbstractLowMemoryMonitor.GetManagedMemoryInBytes();
            long workingSetInBytes    = memInfo.WorkingSet.GetValue(SizeUnit.Bytes);
            var  dirtyMemoryState     = MemoryInformation.GetDirtyMemoryState();
            var  encryptionBuffers    = EncryptionBuffersPool.Instance.GetStats();

            var djv = new DynamicJsonValue
            {
                [nameof(MemoryInfo.WorkingSet)] = workingSetInBytes,
                [nameof(MemoryInfo.TotalUnmanagedAllocations)] = totalUnmanagedAllocations,
                [nameof(MemoryInfo.ManagedAllocations)]        = managedMemoryInBytes,
                [nameof(MemoryInfo.TotalMemoryMapped)]         = totalMapping,
                [nameof(MemoryInfo.TotalScratchDirtyMemory)]   = Size.Humane(memInfo.TotalScratchDirtyMemory.GetValue(SizeUnit.Bytes)),
                [nameof(MemoryInfo.PhysicalMem)]       = Size.Humane(memInfo.TotalPhysicalMemory.GetValue(SizeUnit.Bytes)),
                [nameof(DirtyMemoryState.IsHighDirty)] = dirtyMemoryState.IsHighDirty,
                ["DirtyMemory"] = Size.Humane(dirtyMemoryState.TotalDirtyInBytes),
                [nameof(MemoryInfo.AvailableMemory)] = Size.Humane(memInfo.AvailableMemory.GetValue(SizeUnit.Bytes)),
                [nameof(MemoryInfo.AvailableMemoryForProcessing)] = Size.Humane(memInfo.AvailableMemoryForProcessing.GetValue(SizeUnit.Bytes)),
                [nameof(MemoryInfo.HighMemLastOneMinute)]         = Size.Humane(memoryUsageRecords.High.LastOneMinute.GetValue(SizeUnit.Bytes)),
                [nameof(MemoryInfo.LowMemLastOneMinute)]          = Size.Humane(memoryUsageRecords.Low.LastOneMinute.GetValue(SizeUnit.Bytes)),
                [nameof(MemoryInfo.HighMemLastFiveMinute)]        = Size.Humane(memoryUsageRecords.High.LastFiveMinutes.GetValue(SizeUnit.Bytes)),
                [nameof(MemoryInfo.LowMemLastFiveMinute)]         = Size.Humane(memoryUsageRecords.Low.LastFiveMinutes.GetValue(SizeUnit.Bytes)),
                [nameof(MemoryInfo.HighMemSinceStartup)]          = Size.Humane(memoryUsageRecords.High.SinceStartup.GetValue(SizeUnit.Bytes)),
                [nameof(MemoryInfo.LowMemSinceStartup)]           = Size.Humane(memoryUsageRecords.Low.SinceStartup.GetValue(SizeUnit.Bytes)),

                [nameof(MemoryInfo.Humane)] = new DynamicJsonValue
                {
                    [nameof(MemoryInfoHumane.WorkingSet)] = Size.Humane(workingSetInBytes),
                    [nameof(MemoryInfoHumane.TotalUnmanagedAllocations)] = Size.Humane(totalUnmanagedAllocations),
                    ["EncryptionBuffers"] = Size.Humane(encryptionBuffers.TotalSize),
                    [nameof(MemoryInfoHumane.ManagedAllocations)] = Size.Humane(managedMemoryInBytes),
                    [nameof(MemoryInfoHumane.TotalMemoryMapped)]  = Size.Humane(totalMapping)
                },

                ["Threads"] = threads,

                [nameof(MemoryInfo.Mappings)] = fileMappings
            };

            return(djv);
        }
Exemple #6
0
        private static void WriteMappings(bool includeMappings, BlittableJsonTextWriter writer, JsonOperationContext context,
                                          Dictionary <string, long> fileMappingSizesByDir, Dictionary <string, Dictionary <string, ConcurrentDictionary <IntPtr, long> > > fileMappingByDir)
        {
            if (includeMappings == false)
            {
                return;
            }

            bool isFirst      = true;
            var  prefixLength = LongestCommonPrefixLength(new List <string>(fileMappingSizesByDir.Keys));

            foreach (var sizes in fileMappingSizesByDir.OrderByDescending(x => x.Value))
            {
                if (fileMappingByDir.TryGetValue(sizes.Key, out Dictionary <string, ConcurrentDictionary <IntPtr, long> > value) == false)
                {
                    continue;
                }

                var details = new DynamicJsonValue();

                var dir = new DynamicJsonValue
                {
                    [nameof(MemoryInfoMappingItem.Directory)]                = sizes.Key.Substring(prefixLength),
                    [nameof(MemoryInfoMappingItem.TotalDirectorySize)]       = sizes.Value,
                    [nameof(MemoryInfoMappingItem.HumaneTotalDirectorySize)] = Size.Humane(sizes.Value),
                    [nameof(MemoryInfoMappingItem.Details)] = details
                };
                foreach (var file in value.OrderBy(x => x.Key))
                {
                    long totalMapped = 0;
                    var  dja         = new DynamicJsonArray();
                    var  dic         = new Dictionary <long, long>();
                    foreach (var mapping in file.Value)
                    {
                        totalMapped += mapping.Value;
                        dic.TryGetValue(mapping.Value, out long prev);
                        dic[mapping.Value] = prev + 1;
                    }

                    foreach (var maps in dic)
                    {
                        dja.Add(new DynamicJsonValue {
                            [nameof(MemoryInfoMappingDetails.Size)] = maps.Key, [nameof(MemoryInfoMappingDetails.Count)] = maps.Value
                        });
                    }

                    var fileSize = GetFileSize(file.Key);
                    details[Path.GetFileName(file.Key)] = new DynamicJsonValue
                    {
                        [nameof(MemoryInfoMappingFileInfo.FileSize)]          = fileSize,
                        [nameof(MemoryInfoMappingFileInfo.HumaneFileSize)]    = Size.Humane(fileSize),
                        [nameof(MemoryInfoMappingFileInfo.TotalMapped)]       = totalMapped,
                        [nameof(MemoryInfoMappingFileInfo.HumaneTotalMapped)] = Size.Humane(totalMapped),
                        [nameof(MemoryInfoMappingFileInfo.Mappings)]          = dja
                    };
                }

                if (isFirst == false)
                {
                    writer.WriteComma();
                }

                isFirst = false;

                context.Write(writer, dir);
            }
        }
Exemple #7
0
        private static void WriteMemoryStats(BlittableJsonTextWriter writer, JsonOperationContext context, bool includeThreads, bool includeMappings)
        {
            writer.WriteStartObject();

            var  memInfo = MemoryInformation.GetMemoryInformationUsingOneTimeSmapsReader();
            long managedMemoryInBytes      = AbstractLowMemoryMonitor.GetManagedMemoryInBytes();
            long totalUnmanagedAllocations = NativeMemory.TotalAllocatedMemory;
            var  encryptionBuffers         = EncryptionBuffersPool.Instance.GetStats();
            var  dirtyMemoryState          = MemoryInformation.GetDirtyMemoryState();
            var  memoryUsageRecords        = MemoryInformation.GetMemoryUsageRecords();

            long totalMapping          = 0;
            var  fileMappingByDir      = new Dictionary <string, Dictionary <string, ConcurrentDictionary <IntPtr, long> > >();
            var  fileMappingSizesByDir = new Dictionary <string, long>();

            foreach (var mapping in NativeMemory.FileMapping)
            {
                var dir = Path.GetDirectoryName(mapping.Key);

                if (fileMappingByDir.TryGetValue(dir, out Dictionary <string, ConcurrentDictionary <IntPtr, long> > value) == false)
                {
                    value = new Dictionary <string, ConcurrentDictionary <IntPtr, long> >();
                    fileMappingByDir[dir] = value;
                }

                value[mapping.Key] = mapping.Value.Value.Info;
                foreach (var singleMapping in mapping.Value.Value.Info)
                {
                    fileMappingSizesByDir.TryGetValue(dir, out long prevSize);
                    fileMappingSizesByDir[dir] = prevSize + singleMapping.Value;
                    totalMapping += singleMapping.Value;
                }
            }

            var djv = new DynamicJsonValue
            {
                [nameof(MemoryInfo.PhysicalMemory)]               = memInfo.TotalPhysicalMemory.ToString(),
                [nameof(MemoryInfo.WorkingSet)]                   = memInfo.WorkingSet.ToString(),
                [nameof(MemoryInfo.ManagedAllocations)]           = Size.Humane(managedMemoryInBytes),
                [nameof(MemoryInfo.UnmanagedAllocations)]         = Size.Humane(totalUnmanagedAllocations),
                [nameof(MemoryInfo.EncryptionBuffers)]            = Size.Humane(encryptionBuffers.TotalSize),
                [nameof(MemoryInfo.MemoryMapped)]                 = Size.Humane(totalMapping),
                [nameof(MemoryInfo.ScratchDirtyMemory)]           = memInfo.TotalScratchDirtyMemory.ToString(),
                [nameof(MemoryInfo.IsHighDirty)]                  = dirtyMemoryState.IsHighDirty,
                [nameof(MemoryInfo.DirtyMemory)]                  = Size.Humane(dirtyMemoryState.TotalDirtyInBytes),
                [nameof(MemoryInfo.AvailableMemory)]              = Size.Humane(memInfo.AvailableMemory.GetValue(SizeUnit.Bytes)),
                [nameof(MemoryInfo.AvailableMemoryForProcessing)] = memInfo.AvailableMemoryForProcessing.ToString(),
                [nameof(MemoryInfo.HighMemLastOneMinute)]         = memoryUsageRecords.High.LastOneMinute.ToString(),
                [nameof(MemoryInfo.LowMemLastOneMinute)]          = memoryUsageRecords.Low.LastOneMinute.ToString(),
                [nameof(MemoryInfo.HighMemLastFiveMinute)]        = memoryUsageRecords.High.LastFiveMinutes.ToString(),
                [nameof(MemoryInfo.LowMemLastFiveMinute)]         = memoryUsageRecords.Low.LastFiveMinutes.ToString(),
                [nameof(MemoryInfo.HighMemSinceStartup)]          = memoryUsageRecords.High.SinceStartup.ToString(),
                [nameof(MemoryInfo.LowMemSinceStartup)]           = memoryUsageRecords.Low.SinceStartup.ToString(),
            };

            writer.WritePropertyName(nameof(MemoryInformation));
            context.Write(writer, djv);

            writer.WriteComma();
            writer.WritePropertyName("Threads");
            writer.WriteStartArray();
            WriteThreads(includeThreads, writer, context);
            writer.WriteEndArray();

            writer.WriteComma();
            writer.WritePropertyName(nameof(MemoryInfo.Mappings));
            writer.WriteStartArray();
            WriteMappings(includeMappings, writer, context, fileMappingSizesByDir, fileMappingByDir);
            writer.WriteEndArray();

            writer.WriteEndObject();
        }