Esempio n. 1
0
        public override void Print()
        {
            const string asciiHeader = @"       _____                       _____  ____ {0}      |  __ \                     |  __ \|  _ \ {0}      | |__) |__ ___   _____ _ __ | |  | | |_) |{0}      |  _  // _` \ \ / / _ \ '_ \| |  | |  _ < {0}      | | \ \ (_| |\ V /  __/ | | | |__| | |_) |{0}      |_|  \_\__,_| \_/ \___|_| |_|_____/|____/ {0}{0}";

            ConsoleWriteLineWithColor(ConsoleColor.DarkRed, asciiHeader, Environment.NewLine);
            ConsoleWriteLineWithColor(ConsoleColor.Cyan, "      Safe by default, optimized for efficiency");
            _tw.WriteLine();

            const string lineBorder = "+---------------------------------------------------------------+";

            var meminfo = MemoryInformation.GetMemoryInfo();

            ConsoleWriteLineWithColor(ConsoleColor.Yellow, " Build {0}, Version {1}, SemVer {2}, Commit {3}\r\n PID {4}, {5} bits, {6} Cores, Phys Mem {7}, Arch: {8}",
                                      ServerVersion.Build, ServerVersion.Version, ServerVersion.FullVersion, ServerVersion.CommitHash, Process.GetCurrentProcess().Id,
                                      IntPtr.Size * 8, ProcessorInfo.ProcessorCount, meminfo.TotalPhysicalMemory, RuntimeInformation.OSArchitecture);
            ConsoleWriteLineWithColor(ConsoleColor.DarkCyan, " Source Code (git repo): https://github.com/ravendb/ravendb");
            ConsoleWriteWithColor(new ConsoleText {
                Message = " Built with ", ForegroundColor = ConsoleColor.Gray
            },
                                  new ConsoleText {
                Message = "love ", ForegroundColor = ConsoleColor.Red
            },
                                  new ConsoleText {
                Message = "by ", ForegroundColor = ConsoleColor.Gray
            },
                                  new ConsoleText {
                Message = "Hibernating Rhinos ", ForegroundColor = ConsoleColor.Yellow
            },
                                  new ConsoleText {
                Message = "and awesome contributors!", ForegroundColor = ConsoleColor.Gray, IsNewLinePostPended = true
            });
            _tw.WriteLine(lineBorder);
        }
        public static MachineResources GetMachineResources()
        {
            using (var currentProcess = Process.GetCurrentProcess())
            {
                var workingSet = PlatformDetails.RunningOnLinux == false
                        ? currentProcess.WorkingSet64
                        : MemoryInformation.GetRssMemoryUsage(currentProcess.Id);

                var memoryInfoResult = MemoryInformation.GetMemoryInfo();
                var cpuInfo          = CpuUsage.Calculate();

                var machineResources = new MachineResources
                {
                    TotalMemory        = memoryInfoResult.TotalPhysicalMemory.GetValue(SizeUnit.Bytes),
                    SystemCommitLimit  = memoryInfoResult.TotalCommittableMemory.GetValue(SizeUnit.Bytes),
                    CommitedMemory     = memoryInfoResult.CurrentCommitCharge.GetValue(SizeUnit.Bytes),
                    ProcessMemoryUsage = workingSet,
                    IsProcessMemoryRss = PlatformDetails.RunningOnPosix,
                    MachineCpuUsage    = cpuInfo.MachineCpuUsage,
                    ProcessCpuUsage    = Math.Min(cpuInfo.MachineCpuUsage, cpuInfo.ProcessCpuUsage) // min as sometimes +-1% due to time sampling
                };

                return(machineResources);
            }
        }
        public static MachineResources GetMachineResources(SmapsReader smapsReader)
        {
            using (var currentProcess = Process.GetCurrentProcess())
            {
                var memInfo     = MemoryInformation.GetMemoryInfo(smapsReader);
                var isLowMemory = LowMemoryNotification.Instance.IsLowMemory(memInfo);
                var workingSet  = PlatformDetails.RunningOnLinux
                    ? MemoryInformation.GetRssMemoryUsage(currentProcess.Id) - memInfo.AvailableWithoutTotalCleanMemory.GetValue(SizeUnit.Bytes)
                    : currentProcess.WorkingSet64;

                var cpuInfo = CpuUsage.Calculate();

                var machineResources = new MachineResources
                {
                    TotalMemory     = memInfo.TotalPhysicalMemory.GetValue(SizeUnit.Bytes),
                    AvailableMemory = memInfo.AvailableMemory.GetValue(SizeUnit.Bytes),
                    AvailableWithoutTotalCleanMemory = memInfo.AvailableWithoutTotalCleanMemory.GetValue(SizeUnit.Bytes),
                    SystemCommitLimit     = memInfo.TotalCommittableMemory.GetValue(SizeUnit.Bytes),
                    CommittedMemory       = memInfo.CurrentCommitCharge.GetValue(SizeUnit.Bytes),
                    ProcessMemoryUsage    = workingSet,
                    IsWindows             = PlatformDetails.RunningOnPosix == false,
                    IsLowMemory           = isLowMemory,
                    LowMemoryThreshold    = LowMemoryNotification.Instance.LowMemoryThreshold.GetValue(SizeUnit.Bytes),
                    CommitChargeThreshold = LowMemoryNotification.Instance.GetCommitChargeThreshold(memInfo).GetValue(SizeUnit.Bytes),
                    MachineCpuUsage       = cpuInfo.MachineCpuUsage,
                    ProcessCpuUsage       = Math.Min(cpuInfo.MachineCpuUsage, cpuInfo.ProcessCpuUsage) // min as sometimes +-1% due to time sampling
                };

                return(machineResources);
            }
        }
        public static MachineResources GetMachineResources()
        {
            var currentProcess = Process.GetCurrentProcess();
            var workingSet     =
                PlatformDetails.RunningOnPosix == false || PlatformDetails.RunningOnMacOsx
                    ? currentProcess.WorkingSet64
                    : MemoryInformation.GetRssMemoryUsage(currentProcess.Id);
            var memoryInfoResult = MemoryInformation.GetMemoryInfo();
            var installedMemory  = memoryInfoResult.InstalledMemory.GetValue(SizeUnit.Bytes);
            var availableMemory  = memoryInfoResult.AvailableMemory.GetValue(SizeUnit.Bytes);
            var mappedSharedMem  = LowMemoryNotification.GetCurrentProcessMemoryMappedShared();
            var shared           = mappedSharedMem.GetValue(SizeUnit.Bytes);

            var cpuInfo          = CpuUsage.Calculate();
            var machineResources = new MachineResources
            {
                TotalMemory        = installedMemory,
                MachineMemoryUsage = installedMemory - availableMemory,
                ProcessMemoryUsage = workingSet,
                ProcessMemoryExcludingSharedUsage = Math.Max(workingSet - shared, 0),
                MachineCpuUsage = cpuInfo.MachineCpuUsage,
                ProcessCpuUsage = cpuInfo.ProcessCpuUsage
            };

            return(machineResources);
        }
Esempio n. 5
0
        public Task GetNodeInfo()
        {
            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    var json = new DynamicJsonValue();
                    using (context.OpenReadTransaction())
                    {
                        json[nameof(NodeInfo.NodeTag)]     = ServerStore.NodeTag;
                        json[nameof(NodeInfo.TopologyId)]  = ServerStore.GetClusterTopology(context).TopologyId;
                        json[nameof(NodeInfo.Certificate)] = ServerStore.Server.Certificate.CertificateForClients;
                        json[nameof(ServerStore.Engine.LastStateChangeReason)] = ServerStore.LastStateChangeReason();
                        json[nameof(NodeInfo.NumberOfCores)] = ProcessorInfo.ProcessorCount;

                        var memoryInformation = MemoryInformation.GetMemoryInfo();
                        json[nameof(NodeInfo.InstalledMemoryInGb)] = memoryInformation.InstalledMemory.GetDoubleValue(SizeUnit.Gigabytes);
                        json[nameof(NodeInfo.UsableMemoryInGb)]    = memoryInformation.TotalPhysicalMemory.GetDoubleValue(SizeUnit.Gigabytes);
                        json[nameof(NodeInfo.BuildInfo)]           = LicenseManager.BuildInfo;
                        json[nameof(NodeInfo.ServerId)]            = ServerStore.GetServerId().ToString();
                        json[nameof(NodeInfo.CurrentState)]        = ServerStore.CurrentRachisState;
                    }
                    context.Write(writer, json);
                    writer.Flush();
                }
            return(Task.CompletedTask);
        }
Esempio n. 6
0
        public MemoryConfiguration()
        {
            var memoryInfo = MemoryInformation.GetMemoryInfo();

            LowMemoryLimit = Size.Min(
                new Size(2, SizeUnit.Gigabytes),
                memoryInfo.TotalPhysicalMemory / 10);
        }
Esempio n. 7
0
        protected override Gauge32 GetData()
        {
            lock (this)
            {
                var memoryInfo = MemoryInformation.GetMemoryInfo(_smapsReader?.Value, extended: true);

                return(new Gauge32(memoryInfo.WorkingSet.GetValue(SizeUnit.Megabytes)));
            }
        }
Esempio n. 8
0
        public MemoryConfiguration()
        {
            var memoryInfo = MemoryInformation.GetMemoryInfo();

            LowMemoryLimit = Size.Min(
                new Size(2, SizeUnit.Gigabytes),
                memoryInfo.TotalPhysicalMemory / 10);

            UseRssInsteadOfMemUsage = PlatformDetails.RunningOnDocker;
        }
        public MemoryConfiguration(RavenConfiguration configuration)
        {
            var memoryInfo = MemoryInformation.GetMemoryInfo();

            // we allow 1 GB by default, or up to 75% of available memory on startup, if less than that is available
            LimitForProcessing = Size.Min(new Size(1024, SizeUnit.Megabytes), memoryInfo.AvailableMemory * 0.75);

            LowMemoryForLinuxDetection = Size.Min(new Size(16, SizeUnit.Megabytes), memoryInfo.AvailableMemory * 0.10);

            MemoryCacheLimit = GetDefaultMemoryCacheLimit(memoryInfo.TotalPhysicalMemory);

            AvailableMemoryForRaisingBatchSizeLimit = Size.Min(new Size(768, SizeUnit.Megabytes), memoryInfo.TotalPhysicalMemory / 2);
        }
        public TransactionMergerConfiguration(bool forceUsing32BitsPager)
        {
            if (IntPtr.Size == sizeof(int) || forceUsing32BitsPager)
            {
                MaxTxSize = new Size(4, SizeUnit.Megabytes);
                return;
            }

            var memoryInfo = MemoryInformation.GetMemoryInfo();

            MaxTxSize = Size.Min(
                new Size(512, SizeUnit.Megabytes),
                memoryInfo.TotalPhysicalMemory / 10);
        }
Esempio n. 11
0
        /// <summary>
        /// This functions queries the different software / hardware records in order to get their properties
        /// </summary>
        /// <returns>0 if success and -1 if an exception arises</returns>
        public int Run()
        {
            try
            {
                int error = 0;

                // Gather generic system information
                PlatformInformation.GetSystemInfo();

                // Get specific CPU information
                GetCPUInfo();

                // Get memory information
                GetMemoryInfo();

                // Compute memory parameters
                MemoryInformation.GetMemoryInfo(System_Memory);
#if DEBUG
                Console.WriteLine(MemoryInformation.ToString());
#endif

                if (Globals.Enable_File_Output)
                {
                    Tools.SaveData(Globals.Output_Filename, $"{MemoryInformation.ToString()}\n", true);
                }

                // Get video controller information
                GetVideoControllerInfo();

                // Get disk information
                GetDiskDriveInfo();

                // Get disk partitions
                GetDiskPartitionInfo();

                return(error);
            }
            catch (Exception ex)
            {
#if DEBUG
                Console.WriteLine($"Exception Message: {ex.Message}");
#endif
                return(-1);
            }
        }
Esempio n. 12
0
        public void WhenActualSwapSmallerThenMinSwapConfigured_ShouldRaiseNotification()
        {
            var memoryInfoResult = MemoryInformation.GetMemoryInfo();
            var minSwapConfig    = memoryInfoResult.TotalSwapSize + new Sparrow.Size(130, SizeUnit.Megabytes);
            var customSettings   = new Dictionary <string, string>
            {
                [RavenConfiguration.GetKey(x => x.PerformanceHints.MinSwapSize)] = minSwapConfig.GetValue(SizeUnit.Megabytes).ToString()
            };

            UseNewLocalServer(customSettings);

            using var dis = Server.ServerStore.NotificationCenter.GetStored(out var alerts, postponed: false);
            var alertsList = alerts.ToList();

            var isLowSwapSizeRaised = alertsList.Any(a => a.Json[nameof(AlertRaised.AlertType)].ToString() == AlertType.LowSwapSize.ToString());

            Assert.True(isLowSwapSizeRaised, $"Actual swap {memoryInfoResult.TotalSwapSize}, min swap config {minSwapConfig}, total memory {memoryInfoResult.TotalPhysicalMemory}");
        }
Esempio n. 13
0
        public static MachineResources GetMachineResources(SmapsReader smapsReader, ICpuUsageCalculator cpuUsageCalculator)
        {
            var memInfo = MemoryInformation.GetMemoryInfo(smapsReader, extendedInfo: true);
            var cpuInfo = cpuUsageCalculator.Calculate();

            var machineResources = new MachineResources
            {
                TotalMemory     = memInfo.TotalPhysicalMemory.GetValue(SizeUnit.Bytes),
                AvailableMemory = memInfo.AvailableMemory.GetValue(SizeUnit.Bytes),
                AvailableWithoutTotalCleanMemory = memInfo.AvailableWithoutTotalCleanMemory.GetValue(SizeUnit.Bytes),
                SystemCommitLimit     = memInfo.TotalCommittableMemory.GetValue(SizeUnit.Bytes),
                CommittedMemory       = memInfo.CurrentCommitCharge.GetValue(SizeUnit.Bytes),
                ProcessMemoryUsage    = memInfo.WorkingSet.GetValue(SizeUnit.Bytes),
                IsWindows             = PlatformDetails.RunningOnPosix == false,
                IsLowMemory           = LowMemoryNotification.Instance.IsLowMemory(memInfo, smapsReader),
                LowMemoryThreshold    = LowMemoryNotification.Instance.LowMemoryThreshold.GetValue(SizeUnit.Bytes),
                CommitChargeThreshold = LowMemoryNotification.Instance.GetCommitChargeThreshold(memInfo).GetValue(SizeUnit.Bytes),
                MachineCpuUsage       = cpuInfo.MachineCpuUsage,
                ProcessCpuUsage       = cpuInfo.ProcessCpuUsage
            };

            return(machineResources);
        }
Esempio n. 14
0
        private static bool CommandInfo(List <string> args, RavenCli cli)
        {
            new ClusterMessage(Console.Out, cli._server.ServerStore).Print();
            var memoryInfo = MemoryInformation.GetMemoryInfo();

            WriteText(
                $" Build {ServerVersion.Build}, Version {ServerVersion.Version}, SemVer {ServerVersion.FullVersion}, Commit {ServerVersion.CommitHash}" +
                Environment.NewLine +
                $" PID {Process.GetCurrentProcess().Id}, {IntPtr.Size * 8} bits, {ProcessorInfo.ProcessorCount} Cores, Arch: {RuntimeInformation.OSArchitecture}" +
                Environment.NewLine +
                $" {memoryInfo.TotalPhysicalMemory} Physical Memory, {memoryInfo.AvailableMemory} Available Memory" +
                Environment.NewLine +
                $" {RuntimeSettings.Describe()}",
                ConsoleColor.Cyan, cli);

            var bitsNum = IntPtr.Size * 8;

            if (bitsNum == 64 && cli._server.Configuration.Storage.ForceUsing32BitsPager)
            {
                WriteText(" Running in 32 bits mode", ConsoleColor.DarkCyan, cli);
            }

            return(true);
        }
Esempio n. 15
0
 public override MemoryInfoResult GetMemoryInfo(bool extended = false)
 {
     return(MemoryInformation.GetMemoryInfo(extended ? _smapsReader : null, extended: extended));
 }
Esempio n. 16
0
        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);
            }
        }
Esempio n. 17
0
        public static bool TryIncreasingMemoryUsageForThread(NativeMemory.ThreadStats threadStats,
                                                             ref Size currentMaximumAllowedMemory,
                                                             bool isRunningOn32Bits,
                                                             Logger logger,
                                                             out ProcessMemoryUsage currentUsage)
        {
            if (isRunningOn32Bits)
            {
                currentUsage = null;
                return(false);
            }

            var currentlyAllocated = new Size(threadStats.TotalAllocated, SizeUnit.Bytes);

            //TODO: This has to be exposed via debug endpoint

            // we run out our memory quota, so we need to see if we can increase it or break
            var memoryInfoResult = MemoryInformation.GetMemoryInfo();

            using (var currentProcess = Process.GetCurrentProcess())
            {
                // a lot of the memory that we use is actually from memory mapped files, as such, we can
                // rely on the OS to page it out (without needing to write, since it is read only in this case)
                // so we try to calculate how much such memory we can use with this assumption
                var memoryMappedSize = new Size(currentProcess.WorkingSet64 - currentProcess.PrivateMemorySize64, SizeUnit.Bytes);

                currentUsage = new ProcessMemoryUsage(currentProcess.WorkingSet64, currentProcess.PrivateMemorySize64);

                if (memoryMappedSize < Size.Zero)
                {
                    // in this case, we are likely paging, our working set is smaller than the memory we allocated
                    // it isn't _neccesarily_ a bad thing, we might be paged on allocated memory we aren't using, but
                    // at any rate, we'll ignore that and just use the actual physical memory available
                    memoryMappedSize = Size.Zero;
                }
                var minMemoryToLeaveForMemoryMappedFiles = memoryInfoResult.TotalPhysicalMemory / 4;

                var memoryAssumedFreeOrCheapToFree = (memoryInfoResult.AvailableMemory + memoryMappedSize - minMemoryToLeaveForMemoryMappedFiles);

                // there isn't enough available memory to try, we want to leave some out for other things
                if (memoryAssumedFreeOrCheapToFree < memoryInfoResult.TotalPhysicalMemory / 10)
                {
                    if (logger.IsInfoEnabled)
                    {
                        logger.Info(
                            $"{threadStats.Name} which is already using {currentlyAllocated}/{currentMaximumAllowedMemory} and the system has" +
                            $"{memoryInfoResult.AvailableMemory}/{memoryInfoResult.TotalPhysicalMemory} free RAM. Also have ~{memoryMappedSize} in mmap " +
                            "files that can be cleanly released, not enough to proceed in batch.");
                    }

                    return(false);
                }

                // If there isn't enough here to double our current allocation, we won't allocate any more
                // we do this check in this way to prevent multiple indexes of hitting this at the
                // same time and each thinking that they have enough space
                if (memoryAssumedFreeOrCheapToFree < currentMaximumAllowedMemory)
                {
                    // TODO: We probably need to make a note of this in log & expose in stats
                    // TODO: to explain why we aren't increasing the memory in use

                    if (logger.IsInfoEnabled)
                    {
                        logger.Info(
                            $"{threadStats} which is already using {currentlyAllocated}/{currentMaximumAllowedMemory} and the system has" +
                            $"{memoryInfoResult.AvailableMemory}/{memoryInfoResult.TotalPhysicalMemory} free RAM. Also have ~{memoryMappedSize} in mmap " +
                            "files that can be cleanly released, not enough to proceed in batch.");
                    }
                    return(false);
                }

                // even though we have twice as much memory as we have current allocated, we will
                // only increment by 16MB to avoid over allocation by multiple indexes. This way,
                // we'll check often as we go along this
                var oldBudget = currentMaximumAllowedMemory;
                currentMaximumAllowedMemory = currentlyAllocated + new Size(16, SizeUnit.Megabytes);

                if (logger.IsInfoEnabled)
                {
                    logger.Info(
                        $"Increasing memory budget for {threadStats.Name} which is using  {currentlyAllocated}/{oldBudget} and the system has" +
                        $"{memoryInfoResult.AvailableMemory}/{memoryInfoResult.TotalPhysicalMemory} free RAM with {memoryMappedSize} in mmap " +
                        $"files that can be cleanly released. Budget increased to {currentMaximumAllowedMemory}");
                }

                return(true);
            }
        }
Esempio n. 18
0
 private object CalculateMemoryInfoExtended()
 {
     return(MemoryInformation.GetMemoryInfo(_smapsReader, extended: true));
 }
Esempio n. 19
0
 private object CalculateMemoryInfo()
 {
     return(MemoryInformation.GetMemoryInfo());
 }
Esempio n. 20
0
 private static object CalculateMemoryInfo()
 => MemoryInformation.GetMemoryInfo();
Esempio n. 21
0
        public static bool TryIncreasingMemoryUsageForThread(NativeMemory.ThreadStats threadStats,
                                                             ref Size currentMaximumAllowedMemory,
                                                             Size currentlyInUse,
                                                             bool isRunningOn32Bits,
                                                             Logger logger,
                                                             out ProcessMemoryUsage currentUsage)
        {
            if (isRunningOn32Bits)
            {
                currentUsage = null;
                return(false);
            }

            // we run out our memory quota, so we need to see if we can increase it or break
            var memoryInfoResult = MemoryInformation.GetMemoryInfo();

            using (GetProcessMemoryUsage(out currentUsage, out var mappedSharedMem))
            {
                var memoryAssumedFreeOrCheapToFree = memoryInfoResult.AvailableMemory + mappedSharedMem;

                // there isn't enough available memory to try, we want to leave some out for other things
                if (memoryAssumedFreeOrCheapToFree <
                    Size.Min(memoryInfoResult.TotalPhysicalMemory / 50, new Size(1, SizeUnit.Gigabytes)))
                {
                    if (logger.IsInfoEnabled)
                    {
                        logger.Info(
                            $"{threadStats.Name} which is already using {currentlyInUse}/{currentMaximumAllowedMemory} and the system has " +
                            $"{memoryInfoResult.AvailableMemory}/{memoryInfoResult.TotalPhysicalMemory} free RAM. Also have ~{mappedSharedMem} in mmap " +
                            "files that can be cleanly released, not enough to proceed in batch.");
                    }

                    return(false);
                }

                // If there isn't enough here to double our current allocation, we won't allocate any more
                // we do this check in this way to prevent multiple indexes of hitting this at the
                // same time and each thinking that they have enough space
                if (memoryAssumedFreeOrCheapToFree < currentMaximumAllowedMemory)
                {
                    if (logger.IsInfoEnabled)
                    {
                        logger.Info(
                            $"{threadStats} which is already using {currentlyInUse}/{currentMaximumAllowedMemory} and the system has" +
                            $"{memoryInfoResult.AvailableMemory}/{memoryInfoResult.TotalPhysicalMemory} free RAM. Also have ~{mappedSharedMem} in mmap " +
                            "files that can be cleanly released, not enough to proceed in batch.");
                    }
                    return(false);
                }

                // even though we have twice as much memory as we have current allocated, we will
                // only increment by 16MB to avoid over allocation by multiple indexes. This way,
                // we'll check often as we go along this
                var oldBudget = currentMaximumAllowedMemory;
                currentMaximumAllowedMemory = currentlyInUse + new Size(16, SizeUnit.Megabytes);

                if (logger.IsInfoEnabled)
                {
                    logger.Info(
                        $"Increasing memory budget for {threadStats.Name} which is using  {currentlyInUse}/{oldBudget} and the system has" +
                        $"{memoryInfoResult.AvailableMemory}/{memoryInfoResult.TotalPhysicalMemory} free RAM with {mappedSharedMem} in mmap " +
                        $"files that can be cleanly released. Budget increased to {currentMaximumAllowedMemory}");
                }

                return(true);
            }
        }