protected override int GetCurrentPressure()
        {
            int  num  = GC.CollectionCount(2);
            SRef ref2 = this._sizedRef;

            if ((num != this._gen2Count) && (ref2 != null))
            {
                this._gen2Count = num;
                this._idx      ^= 1;
                this._cacheSizeSampleTimes[this._idx] = DateTime.UtcNow;
                this._cacheSizeSamples[this._idx]     = ref2.ApproximateSize;
                ApplicationManager applicationManager = HostingEnvironment.GetApplicationManager();
                if (applicationManager != null)
                {
                    long sizeUpdate = this._cacheSizeSamples[this._idx] - this._cacheSizeSamples[this._idx ^ 1];
                    this._totalCacheSize = applicationManager.GetUpdatedTotalCacheSize(sizeUpdate);
                }
                else
                {
                    this._totalCacheSize = this._cacheSizeSamples[this._idx];
                }
            }
            if (this._memoryLimit <= 0L)
            {
                return(0);
            }
            long num3 = this._cacheSizeSamples[this._idx];

            if (num3 > this._memoryLimit)
            {
                num3 = this._memoryLimit;
            }
            PerfCounters.SetCounter(AppPerfCounter.CACHE_PERCENT_PROC_MEM_LIMIT_USED, (int)(num3 >> 10));
            return((int)((num3 * 100L) / this._memoryLimit));
        }
        internal void Dispose()
        {
            SRefMultiple sref = _sizedRef;

            if (sref != null && Interlocked.CompareExchange(ref _sizedRef, null, sref) == sref)
            {
                sref.Dispose();
            }
            ApplicationManager appManager = HostingEnvironment.GetApplicationManager();

            if (appManager != null)
            {
                long sizeUpdate = (0 - _cacheSizeSamples[_idx]);
                appManager.GetUpdatedTotalCacheSize(sizeUpdate);
            }
        }
        internal void Dispose()
        {
            SRef comparand = this._sizedRef;

            if ((comparand != null) && (Interlocked.CompareExchange <SRef>(ref this._sizedRef, null, comparand) == comparand))
            {
                comparand.Dispose();
            }
            ApplicationManager applicationManager = HostingEnvironment.GetApplicationManager();

            if (applicationManager != null)
            {
                long sizeUpdate = -this._cacheSizeSamples[this._idx];
                applicationManager.GetUpdatedTotalCacheSize(sizeUpdate);
            }
        }
예제 #4
0
        // Need StringComparer implementation. It's very expensive to port the actual BCL code here
        // Instead use the default AppDomain, because it doesn't have string hash randomization enabled.
        // Marshal the call to reuse the default StringComparer behavior.
        // PERF isn't optimal, so apply consideration!
        internal static int GetNonRandomizedStringComparerHashCode(string s)
        {
            // Preserve the default behavior when string hash randomization is off
            if (!AppSettings.UseRandomizedStringHashAlgorithm)
            {
                return(StringComparer.InvariantCultureIgnoreCase.GetHashCode(s));
            }

            ApplicationManager appManager = HostingEnvironment.GetApplicationManager();

            if (appManager != null)
            {
                // Cross AppDomain call may cause marshaling of IPrincipal to fail. So strip out the exectuion context
                int hashCode = 0;
                ExecutionContextUtil.RunInNullExecutionContext(delegate {
                    hashCode = appManager.GetNonRandomizedStringComparerHashCode(s, ignoreCase: true);
                });

                return(hashCode);
            }

            // Fall back to non-compat result
            return(GetStringHashCode(s.ToLower(CultureInfo.InvariantCulture)));
        }
        override protected int GetCurrentPressure()
        {
            // Call GetUpdatedTotalCacheSize to update the total
            // cache size, if there has been a recent Gen 2 Collection.
            // This update must happen, otherwise the CacheManager won't
            // know the total cache size.
            int          gen2Count = GC.CollectionCount(2);
            SRefMultiple sref      = _sizedRef;

            if (gen2Count != _gen2Count && sref != null)
            {
                // update _gen2Count
                _gen2Count = gen2Count;

                // the SizedRef is only updated after a Gen2 Collection

                // increment the index (it's either 1 or 0)
                Debug.Assert(SAMPLE_COUNT == 2);
                _idx = _idx ^ 1;
                // remember the sample time
                _cacheSizeSampleTimes[_idx] = DateTime.UtcNow;
                // remember the sample value
                _cacheSizeSamples[_idx] = sref.ApproximateSize;
#if DBG
                Debug.Trace("CacheMemory", "SizedRef.ApproximateSize=" + _cacheSizeSamples[_idx]);
#endif
                // we may not be "hosted"
                ApplicationManager appManager = HostingEnvironment.GetApplicationManager();
                if (appManager != null)
                {
                    // update total cache size
                    long sizeUpdate = _cacheSizeSamples[_idx] - _cacheSizeSamples[_idx ^ 1];
                    _totalCacheSize = appManager.GetUpdatedTotalCacheSize(sizeUpdate);
                }
                else
                {
                    // if we're not hosted, this cache's size is the total cache size
                    _totalCacheSize = _cacheSizeSamples[_idx];
                }
            }

            // if there's no memory limit, then there's nothing more to do
            if (_memoryLimit <= 0)
            {
                return(0);
            }

            long cacheSize = _cacheSizeSamples[_idx];

            // use _memoryLimit as an upper bound so that pressure is a percentage (between 0 and 100, inclusive).
            if (cacheSize > _memoryLimit)
            {
                cacheSize = _memoryLimit;
            }

            // PerfCounter: Cache Percentage Process Memory Limit Used
            //    = memory used by this process / process memory limit at pressureHigh
            // Set private bytes used in kilobytes because the counter is a DWORD

            //
            PerfCounters.SetCounter(AppPerfCounter.CACHE_PERCENT_PROC_MEM_LIMIT_USED, (int)(cacheSize >> KILOBYTE_SHIFT));

            int result = (int)(cacheSize * 100 / _memoryLimit);
            return(result);
        }