Example #1
0
        public Task MemorySmaps()
        {
            if (PlatformDetails.RunningOnLinux == false)
            {
                using (ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
                    using (var process = Process.GetCurrentProcess())
                    {
                        var sharedClean = MemoryInformation.GetSharedCleanInBytes(process);
                        var rc          = Win32MemoryQueryMethods.GetMaps();
                        var djv         = new DynamicJsonValue
                        {
                            ["Totals"] = new DynamicJsonValue
                            {
                                ["WorkingSet"]          = process.WorkingSet64,
                                ["SharedClean"]         = Sizes.Humane(sharedClean),
                                ["PrivateClean"]        = "N/A",
                                ["TotalClean"]          = rc.ProcessClean,
                                ["RssHumanly"]          = Sizes.Humane(process.WorkingSet64),
                                ["SharedCleanHumanly"]  = Sizes.Humane(sharedClean),
                                ["PrivateCleanHumanly"] = "N/A",
                                ["TotalCleanHumanly"]   = Sizes.Humane(rc.ProcessClean)
                            },
                            ["Details"] = rc.Json
                        };
                        using (var write = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                        {
                            context.Write(write, djv);
                        }
                        return(Task.CompletedTask);
                    }
            }

            using (ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
            {
                var buffers = new[]
                {
                    ArrayPool <byte> .Shared.Rent(SmapsReader.BufferSize),
                    ArrayPool <byte> .Shared.Rent(SmapsReader.BufferSize)
                };
                try
                {
                    var result     = new SmapsReader(buffers).CalculateMemUsageFromSmaps <SmapsReaderJsonResults>();
                    var procStatus = MemoryInformation.GetMemoryUsageFromProcStatus();
                    var djv        = new DynamicJsonValue
                    {
                        ["Totals"] = new DynamicJsonValue
                        {
                            ["WorkingSet"]            = result.Rss,
                            ["SharedClean"]           = result.SharedClean,
                            ["PrivateClean"]          = result.PrivateClean,
                            ["TotalClean"]            = result.SharedClean + result.PrivateClean,
                            ["TotalDirty"]            = result.TotalDirty, // This includes not only r-ws buffer and voron files, but also dotnet's and heap dirty memory
                            ["WorkingSetSwap"]        = result.Swap,       // Swap values sum for r-ws entries only
                            ["Swap"]                  = procStatus.Swap,
                            ["RssHumanly"]            = Sizes.Humane(result.Rss),
                            ["SwapHumanly"]           = Sizes.Humane(procStatus.Swap),
                            ["WorkingSetSwapHumanly"] = Sizes.Humane(result.Swap),
                            ["SharedCleanHumanly"]    = Sizes.Humane(result.SharedClean),
                            ["PrivateCleanHumanly"]   = Sizes.Humane(result.PrivateClean),
                            ["TotalCleanHumanly"]     = Sizes.Humane(result.SharedClean + result.PrivateClean)
                        },
                        ["Details"] = result.SmapsResults.ReturnResults()
                    };

                    using (var write = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        context.Write(write, djv);
                    }

                    return(Task.CompletedTask);
                }
                finally
                {
                    ArrayPool <byte> .Shared.Return(buffers[0]);

                    ArrayPool <byte> .Shared.Return(buffers[1]);
                }
            }
        }