public void Refresh()
            {
                var clock = new CodeClock();

                clock.Start();

                var currentProcess = Process.GetCurrentProcess();

                currentProcess.Refresh();

                ProcessVirtualMemoryBytes = currentProcess.VirtualMemorySize64;
                ProcessPrivateBytes       = currentProcess.PrivateMemorySize64;
                ProcessWorkingSetBytes    = currentProcess.WorkingSet64;
                GCTotalBytesAllocated     = GC.GetTotalMemory(false);
                SystemFreeMemoryBytes     = SystemResources.GetAvailableMemory(SizeUnits.Bytes);

                HighWaterMarkBytes         = MemoryManagementSettings.Default.HighWatermarkMegaBytes * OneMegabyte;
                LowWaterMarkBytes          = MemoryManagementSettings.Default.LowWatermarkMegaBytes * OneMegabyte;
                HeldMemoryToCollectPercent = MemoryManagementSettings.Default.HeldMemoryToCollectPercent / 100.0;

                x64MinimumFreeSystemMemoryBytes = MemoryManagementSettings.Default.x64MinimumFreeSystemMemoryMegabytes * OneMegabyte;
                x64MaxMemoryUsagePercent        = MemoryManagementSettings.Default.x64MaxMemoryUsagePercent / 100.0;
                x64MaxMemoryToCollectBytes      = MemoryManagementSettings.Default.x64MaxMemoryToCollectMegabytes * OneMegabyte;

                MemoryManagerLargeObjectBytesCount = MemoryManager.LargeObjectBytesCount;

                clock.Stop();

                PerformanceReportBroker.PublishReport("Memory", "UpdateMemoryInfo", clock.Seconds);
            }
        private long GetMaxVirtualMemorySizeBytes()
        {
            long currentVirtualMemorySize        = _processVirtualMemoryBytes;
            long maxTheoreticalVirtualMemorySize = currentVirtualMemorySize + SystemResources.GetAvailableMemory(SizeUnits.Bytes);
            long maxVirtualMemorySizeBytes;

            if (Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") == x86Architecture)
            {
                long maxSystemVirtualMemorySize = Is3GigEnabled() ? ThreeGigabytes : TwoGigabytes;
                maxVirtualMemorySizeBytes = Math.Min(maxTheoreticalVirtualMemorySize, maxSystemVirtualMemorySize);
            }
            else
            {
                //let's not get greedy :)
                maxVirtualMemorySizeBytes = Math.Min(EightGigabytes, maxTheoreticalVirtualMemorySize);
            }

            return(maxVirtualMemorySizeBytes);
        }
Esempio n. 3
0
        private void PollProcessInfo(object ignore)
        {
            do
            {
                var sync = _synchronizationContext;
                if (sync == null)
                {
                    break;
                }

                //Calling refresh takes a long time, which is why it's async
                Process process = Process.GetCurrentProcess();
                process.Refresh();

                var systemFree = SystemResources.GetAvailableMemory(SizeUnits.Bytes);

                sync.Post(delegate
                {
                    if (_synchronizationContext == null)
                    {
                        return;
                    }

                    SystemFreeMemoryMB     = String.Format("{0:F3}", systemFree / 1024F / 1024F);
                    ProcessPrivateBytesMB  = String.Format("{0:F3}", process.PrivateMemorySize64 / 1024F / 1024F);
                    ProcessVirtualMemoryMB = String.Format("{0:F3}", process.VirtualMemorySize64 / 1024F / 1024F);
                    ProcessWorkingSetMB    = String.Format("{0:F3}", process.WorkingSet64 / 1024F / 1024F);

                    NotifyPropertyChanged("SystemFreeMemoryMB");
                    NotifyPropertyChanged("ProcessPrivateBytesMB");
                    NotifyPropertyChanged("ProcessVirtualMemoryMB");
                    NotifyPropertyChanged("ProcessWorkingSetMB");

                    CleanupDeadLargeObjects();
                }, null);

                lock (_waitLock)
                {
                    Monitor.Wait(_waitLock, 3000);
                }
            } while(true);
        }
Esempio n. 4
0
        private void RetrieveFrame(Frame frame)
        {
            if (_stopAllActivity)
            {
                return;
            }

            try
            {
                //just return if the available memory is getting low - only retrieve and decompress on-demand now.
                if (SystemResources.GetAvailableMemory(SizeUnits.Megabytes) < Prefetch.Default.AvailableMemoryLimitMegabytes)
                {
                    return;
                }

                Interlocked.Increment(ref _activeRetrieveThreads);

                //TODO (CR May 2010): do we need to do this all the time?
                string message = String.Format("Retrieving Frame (active threads: {0})", Thread.VolatileRead(ref _activeRetrieveThreads));
                Trace.WriteLine(message);
                Console.WriteLine(message);

                //TODO: try to trigger header retrieval for data luts?
                frame.GetNormalizedPixelData();
            }
            catch (OutOfMemoryException)
            {
                _stopAllActivity = true;
                Platform.Log(LogLevel.Error, "Out of memory trying to retrieve pixel data.  Prefetching will not resume unless memory becomes available.");
            }
            catch (Exception e)
            {
                Platform.Log(LogLevel.Error, e, "Error retrieving frame pixel data.");
            }
            finally
            {
                Interlocked.Decrement(ref _activeRetrieveThreads);
            }
        }
Esempio n. 5
0
        private static void CheckMemoryAvailable()
        {
            ApplicationServiceSettings settings = new ApplicationServiceSettings();

            if (settings.MinimumFreeMemoryMB <= 0)
            {
                return;
            }

            bool memoryAvailable = SystemResources.GetAvailableMemory(SizeUnits.Megabytes) > settings.MinimumFreeMemoryMB;

            if (!memoryAvailable)
            {
                string error = String.Format(
                    "Application server out of resources.  Minimum free memory not available ({0}MB required, {1}MB available).",
                    settings.MinimumFreeMemoryMB,
                    SystemResources.GetAvailableMemory(SizeUnits.Megabytes));

                Platform.Log(LogLevel.Warn, error);
                throw new FaultException <OutOfResourceFault>(new OutOfResourceFault {
                    ErrorMessage = error
                });
            }
        }
Esempio n. 6
0
        /// <summary>
        /// The processing thread.
        /// </summary>
        /// <remarks>
        /// This method queries the database for WorkQueue entries to work on, and then uses
        /// a thread pool to process the entries.
        /// </remarks>
        public void Run()
        {
            // Force the alert to be displayed right away, if it happens
            DateTime lastLog = Platform.Time.AddMinutes(-61);

            if (!_threadPool.Active)
            {
                _threadPool.Start();
            }

            Platform.Log(LogLevel.Info, "Work Queue Processor running...");

            while (true)
            {
                if (_stop)
                {
                    return;
                }

                bool threadsAvailable = _threadPool.CanQueueItem;
                bool memoryAvailable  = WorkQueueSettings.Instance.WorkQueueMinimumFreeMemoryMB == 0
                                        ||
                                        SystemResources.GetAvailableMemory(SizeUnits.Megabytes) >
                                        WorkQueueSettings.Instance.WorkQueueMinimumFreeMemoryMB;

                if (threadsAvailable && memoryAvailable)
                {
                    try
                    {
                        Model.WorkQueue queueListItem = GetWorkQueueItem(ServerPlatform.ProcessorId);
                        if (queueListItem == null)
                        {
                            /* No result found, or reach max queue entries for each type */
                            _terminateEvent.WaitOne(WorkQueueSettings.Instance.WorkQueueQueryDelay, false);
                            continue;
                        }

                        if (!_extensions.ContainsKey(queueListItem.WorkQueueTypeEnum))
                        {
                            Platform.Log(LogLevel.Error,
                                         "No extensions loaded for WorkQueue item type: {0}.  Failing item.",
                                         queueListItem.WorkQueueTypeEnum);

                            //Just fail the WorkQueue item, not much else we can do
                            FailQueueItem(queueListItem, "No plugin to handle WorkQueue type: " + queueListItem.WorkQueueTypeEnum);
                            continue;
                        }

                        try
                        {
                            IWorkQueueProcessorFactory factory   = _extensions[queueListItem.WorkQueueTypeEnum];
                            IWorkQueueItemProcessor    processor = factory.GetItemProcessor();

                            // Enqueue the actual processing of the item to the thread pool.
                            _threadPool.Enqueue(processor, queueListItem, ExecuteProcessor);
                        }
                        catch (Exception e)
                        {
                            Platform.Log(LogLevel.Error, e, "Unexpected exception creating WorkQueue processor.");
                            FailQueueItem(queueListItem, "Failure getting WorkQueue processor: " + e.Message);
                            continue;
                        }
                    }
                    catch (Exception e)
                    {
                        // Wait for only 1.5 seconds
                        Platform.Log(LogLevel.Error, e, "Exception occured when processing WorkQueue item.");
                        _terminateEvent.WaitOne(3000, false);
                    }
                }
                else
                {
                    if ((lastLog.AddMinutes(60) < Platform.Time) && !memoryAvailable)
                    {
                        lastLog = Platform.Time;
                        Platform.Log(LogLevel.Error, "Unable to process WorkQueue entries, Minimum memory not available, minimum MB required: {0}, current MB available:{1}",
                                     WorkQueueSettings.Instance.WorkQueueMinimumFreeMemoryMB,
                                     SystemResources.GetAvailableMemory(SizeUnits.Megabytes));

                        ServerPlatform.Alert(AlertCategory.Application, AlertLevel.Critical, "WorkQueue", AlertTypeCodes.NoResources,
                                             null, TimeSpan.Zero,
                                             "Unable to process WorkQueue entries, Minimum memory not available, minimum MB required: {0}, current MB available:{1}",
                                             WorkQueueSettings.Instance.WorkQueueMinimumFreeMemoryMB,
                                             SystemResources.GetAvailableMemory(SizeUnits.Megabytes));
                    }
                    // wait for new opening in the pool or termination
                    WaitHandle.WaitAny(new WaitHandle[] { _threadStop, _terminateEvent }, 3000, false);
                    _threadStop.Reset();
                }
            }
        }