Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / BufferManager.cs / 1 / BufferManager.cs
//---------------------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------------------- namespace System.ServiceModel.Channels { using System; using System.ServiceModel; using System.Collections.Generic; using System.Threading; public abstract class BufferManager { public abstract byte[] TakeBuffer(int bufferSize); public abstract void ReturnBuffer(byte[] buffer); public abstract void Clear(); public static BufferManager CreateBufferManager(long maxBufferPoolSize, int maxBufferSize) { if (maxBufferPoolSize == 0) { return GCBufferManager.Value; } else { return new PooledBufferManager(maxBufferPoolSize, maxBufferSize); } } class PooledBufferManager : BufferManager { int[] bufferSizes; BufferPool[] bufferPools; object tuningLock = new object(); long memoryLimit; long remainingMemory; bool areQuotasBeingTuned; int totalMisses; const int minBufferSize = 128; const int maxMissesBeforeTuning = 8; const int initialBufferCount = 1; public PooledBufferManager(long maxMemoryToPool, int maxBufferSize) { if (maxMemoryToPool < 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("maxMemoryToPool", maxMemoryToPool, SR.GetString(SR.ValueMustBeNonNegative))); } if (maxBufferSize < 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("maxBufferSize", maxBufferSize, SR.GetString(SR.ValueMustBeNonNegative))); } memoryLimit = maxMemoryToPool; remainingMemory = maxMemoryToPool; ListbufferPoolList = new List (); for (int bufferSize = minBufferSize; ; ) { long bufferCountLong = remainingMemory / bufferSize; int bufferCount = bufferCountLong > int.MaxValue ? int.MaxValue : (int)bufferCountLong; if (bufferCount > initialBufferCount) bufferCount = initialBufferCount; bufferPoolList.Add(new BufferPool(bufferSize, bufferCount)); remainingMemory -= (long)bufferCount * bufferSize; if (bufferSize >= maxBufferSize) break; long newBufferSizeLong = (long)bufferSize * 2; if (newBufferSizeLong > (long)maxBufferSize) { bufferSize = maxBufferSize; } else { bufferSize = (int)newBufferSizeLong; } } bufferPools = bufferPoolList.ToArray(); bufferSizes = new int[bufferPools.Length]; for (int i = 0; i < bufferPools.Length; i++) { bufferSizes[i] = bufferPools[i].BufferSize; } } public override void Clear() { for (int i = 0; i < bufferPools.Length; i++) { BufferPool bufferPool = bufferPools[i]; bufferPool.Clear(); } } void ChangeQuota(ref BufferPool bufferPool, int delta) { BufferPool oldBufferPool = bufferPool; int newLimit = oldBufferPool.Limit + delta; BufferPool newBufferPool = new BufferPool(oldBufferPool.BufferSize, newLimit); for (int i = 0; i < newLimit; i++) { byte[] buffer = oldBufferPool.Take(); if (buffer == null) break; newBufferPool.Return(buffer); newBufferPool.IncrementCount(); } remainingMemory -= oldBufferPool.BufferSize * delta; bufferPool = newBufferPool; } void DecreaseQuota(ref BufferPool bufferPool) { ChangeQuota(ref bufferPool, -1); } int FindMostExcessivePool() { long maxBytesInExcess = 0; int index = -1; for (int i = 0; i < bufferPools.Length; i++) { BufferPool bufferPool = bufferPools[i]; if (bufferPool.Peak < bufferPool.Limit) { long bytesInExcess = (bufferPool.Limit - bufferPool.Peak) * (long)bufferPool.BufferSize; if (bytesInExcess > maxBytesInExcess) { index = i; maxBytesInExcess = bytesInExcess; } } } return index; } int FindMostStarvedPool() { long maxBytesMissed = 0; int index = -1; for (int i = 0; i < bufferPools.Length; i++) { BufferPool bufferPool = bufferPools[i]; if (bufferPool.Peak == bufferPool.Limit) { long bytesMissed = bufferPool.Misses * (long)bufferPool.BufferSize; if (bytesMissed > maxBytesMissed) { index = i; maxBytesMissed = bytesMissed; } } } return index; } BufferPool FindPool(int desiredBufferSize) { for (int i = 0; i < bufferSizes.Length; i++) { if (desiredBufferSize <= bufferSizes[i]) { return bufferPools[i]; } } return null; } void IncreaseQuota(ref BufferPool bufferPool) { ChangeQuota(ref bufferPool, 1); } public override void ReturnBuffer(byte[] buffer) { if (buffer == null) throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("buffer"); BufferPool bufferPool = FindPool(buffer.Length); if (bufferPool != null) { if (buffer.Length != bufferPool.BufferSize) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.BufferIsNotRightSizeForBufferManager), "buffer")); } if (bufferPool.Return(buffer)) { bufferPool.IncrementCount(); } } } byte[] AllocNewBuffer(int bufferSize) { return DiagnosticUtility.Utility.AllocateByteArray(bufferSize); } public override byte[] TakeBuffer(int bufferSize) { if (bufferSize < 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("bufferSize", bufferSize, SR.GetString(SR.ValueMustBeNonNegative))); } BufferPool bufferPool = FindPool(bufferSize); if (bufferPool != null) { byte[] buffer = bufferPool.Take(); if (buffer != null) { bufferPool.DecrementCount(); return buffer; } if (bufferPool.Peak == bufferPool.Limit) { bufferPool.Misses++; if (++totalMisses >= maxMissesBeforeTuning) { TuneQuotas(); } } return AllocNewBuffer(bufferPool.BufferSize); } else { return AllocNewBuffer(bufferSize); } } void TuneQuotas() { if (areQuotasBeingTuned) return; bool lockHeld = false; try { try { } finally { lockHeld = Monitor.TryEnter(tuningLock); } // Don't bother if another thread already has the lock if (!lockHeld || areQuotasBeingTuned) return; areQuotasBeingTuned = true; } finally { if (lockHeld) { Monitor.Exit(tuningLock); } } // find the "poorest" pool int starvedIndex = FindMostStarvedPool(); if (starvedIndex >= 0) { BufferPool starvedBufferPool = bufferPools[starvedIndex]; if (remainingMemory < starvedBufferPool.BufferSize) { // find the "richest" pool int excessiveIndex = FindMostExcessivePool(); if (excessiveIndex >= 0) { // steal from the richest DecreaseQuota(ref bufferPools[excessiveIndex]); } } if (remainingMemory >= starvedBufferPool.BufferSize) { // give to the poorest IncreaseQuota(ref bufferPools[starvedIndex]); } } // reset statistics for (int i = 0; i < bufferPools.Length; i++) { BufferPool bufferPool = bufferPools[i]; bufferPool.Misses = 0; } totalMisses = 0; areQuotasBeingTuned = false; } class BufferPool { int bufferSize; int count; int limit; int misses; int peak; SynchronizedPool pool; public BufferPool(int bufferSize, int limit) { pool = new SynchronizedPool (limit); this.bufferSize = bufferSize; this.limit = limit; } public int BufferSize { get { return bufferSize; } } public int Limit { get { return limit; } } public int Misses { get { return misses; } set { this.misses = value; } } public int Peak { get { return peak; } } public void Clear() { pool.Clear(); count = 0; } public void DecrementCount() { int newValue = count - 1; if (newValue >= 0) { count = newValue; } } public void IncrementCount() { int newValue = count + 1; if (newValue <= limit) { count = newValue; if (newValue > peak) { peak = newValue; } } } public bool Return(byte[] buffer) { return pool.Return(buffer); } public byte[] Take() { return pool.Take(); } } } class GCBufferManager : BufferManager { static GCBufferManager value = new GCBufferManager(); GCBufferManager() { } public static GCBufferManager Value { get { return value; } } public override void Clear() { } public override byte[] TakeBuffer(int bufferSize) { if (bufferSize < 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("bufferSize", bufferSize, SR.GetString(SR.ValueMustBeNonNegative))); } return DiagnosticUtility.Utility.AllocateByteArray(bufferSize); } public override void ReturnBuffer(byte[] buffer) { if (buffer == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("buffer"); } // do nothing, GC will reclaim this buffer } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- AtomicFile.cs
- AvTraceFormat.cs
- TypeLibConverter.cs
- DesignSurfaceManager.cs
- CodeCommentStatementCollection.cs
- InputBinder.cs
- objectresult_tresulttype.cs
- NativeRecognizer.cs
- TextContainerHelper.cs
- HostVisual.cs
- NamedPipeChannelListener.cs
- ValidatingPropertiesEventArgs.cs
- ProviderConnectionPointCollection.cs
- _TransmitFileOverlappedAsyncResult.cs
- ActivationArguments.cs
- SqlDataSource.cs
- FontCacheUtil.cs
- ImageFormatConverter.cs
- OneOfTypeConst.cs
- DataGridViewColumnCollection.cs
- DtrList.cs
- CodeTypeReferenceExpression.cs
- InstancePersistenceCommandException.cs
- RouteItem.cs
- MimePart.cs
- XmlSchemaGroupRef.cs
- SafeBitVector32.cs
- SQLCharsStorage.cs
- ParserOptions.cs
- Permission.cs
- GroupBox.cs
- Table.cs
- HtmlForm.cs
- Component.cs
- OperationAbortedException.cs
- StylusDevice.cs
- GcHandle.cs
- FirstMatchCodeGroup.cs
- ButtonBase.cs
- ExpressionEditorAttribute.cs
- ConfigXmlComment.cs
- FieldAccessException.cs
- SqlDataSourceEnumerator.cs
- DataGridViewCheckBoxCell.cs
- DateTimeOffset.cs
- ClientTargetSection.cs
- TaskExtensions.cs
- Config.cs
- WorkflowTransactionOptions.cs
- ExpressionReplacer.cs
- BindingsCollection.cs
- PropertyAccessVisitor.cs
- ExpressionEditor.cs
- UIntPtr.cs
- XmlSecureResolver.cs
- ExtenderControl.cs
- EventProviderWriter.cs
- SQLString.cs
- DetailsViewPagerRow.cs
- Scheduling.cs
- DataTableClearEvent.cs
- SoapDocumentMethodAttribute.cs
- ServiceMetadataContractBehavior.cs
- SqlUnionizer.cs
- EncodingStreamWrapper.cs
- PagedDataSource.cs
- InvalidMessageContractException.cs
- SrgsElementFactory.cs
- SmiConnection.cs
- XmlDictionaryReader.cs
- HtmlToClrEventProxy.cs
- CollectionsUtil.cs
- NumericPagerField.cs
- ButtonBase.cs
- CompositeScriptReference.cs
- XmlIterators.cs
- RowToFieldTransformer.cs
- SystemColorTracker.cs
- PaperSize.cs
- TrackingMemoryStream.cs
- DtrList.cs
- DataGridViewCheckBoxCell.cs
- HwndSourceParameters.cs
- ListViewGroup.cs
- Point4DConverter.cs
- HttpInputStream.cs
- IntMinMaxAggregationOperator.cs
- StringComparer.cs
- CreateParams.cs
- BuildManager.cs
- DesignerValidationSummaryAdapter.cs
- RightsManagementPermission.cs
- DataViewListener.cs
- CustomAttributeSerializer.cs
- SimpleHandlerBuildProvider.cs
- TextPenaltyModule.cs
- TextDecorationCollection.cs
- XPathSelfQuery.cs
- TemplateBamlTreeBuilder.cs
- Propagator.Evaluator.cs