#region S# License
/******************************************************************************************
NOTICE!!! This program and source code is owned and licensed by
StockSharp, LLC, www.stocksharp.com
Viewing or use of this code requires your acceptance of the license
agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE
Removal of this comment is a violation of the license agreement.
Project: StockSharp.Logging.Logging
File: MemoryStatisticsValue.cs
Created: 2015, 11, 11, 2:32 PM
Copyright 2010 by StockSharp, LLC
*******************************************************************************************/
#endregion S# License
namespace StockSharp.Logging
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Ecng.Collections;
using Ecng.Common;
using Ecng.Serialization;
using StockSharp.Localization;
///
/// The interface for tracking the number of active objects of the particular type.
///
public interface IMemoryStatisticsValue
{
///
/// Name.
///
string Name { get; }
///
/// The number of active objects.
///
int ObjectCount { get; }
///
/// To clear active objects.
///
/// Whether to clear the objects counter.
void Clear(bool resetCounter = false);
}
///
/// The class for tracking the number of active objects of the particular type.
///
/// The object type.
public class MemoryStatisticsValue : IPersistable, IMemoryStatisticsValue
{
//private readonly MemoryStatistics _parent;
private readonly CachedSynchronizedSet _objects = new CachedSynchronizedSet();
///
/// Initializes a new instance of the .
///
/// Name.
public MemoryStatisticsValue(string name/*, MemoryStatistics parent*/)
{
if (name.IsEmpty())
throw new ArgumentNullException(nameof(name));
//if (parent == null)
// throw new ArgumentNullException(nameof(parent));
Name = name;
//_parent = parent;
}
///
/// Active objects.
///
public T[] Objects => _objects.Cache;
///
public string Name { get; }
private int _objectCount;
///
public int ObjectCount => _objectCount;
///
/// To check that they are going to delete a previously deleted object.
///
public bool ThrowOnRemoveDeleted { get; set; }
///
/// To log the objects creating and deletion. The default is off.
///
public bool IsTraceObjects { get; set; }
private bool _isObjectTracking;
///
/// Whether the storage of objects available through is on. The default is off.
///
public bool IsObjectTracking
{
get => _isObjectTracking;
set
{
if (!_isObjectTracking && value)
Clear();
_isObjectTracking = value;
}
}
///
/// To add a new object.
///
/// The new object.
public void Add(T obj)
{
Interlocked.Increment(ref _objectCount);
//if (IsTraceObjects)
// _parent.AddDebugLog("Создан({1}) {0} типа {2}.", obj, ObjectCount, obj.GetType().Name);
if (!IsObjectTracking)
return;
if (!_objects.TryAdd(obj))
throw new ArgumentException(LocalizedStrings.ObjectWasAlreadyAdded.Put(obj));
}
///
/// To add new objects.
///
/// Active objects.
public void Add(IEnumerable objects)
{
var count = objects.Count();
Interlocked.Add(ref _objectCount, count);
if (!IsObjectTracking)
return;
var obj = objects.FirstOrDefault(o => !_objects.TryAdd(o));
if (!obj.IsNull())
throw new ArgumentException(LocalizedStrings.ObjectWasAlreadyAdded.Put(obj));
}
///
/// To delete the active object.
///
/// The active object.
public void Remove(T obj)
{
Interlocked.Decrement(ref _objectCount);
//if (IsTraceObjects)
// _parent.AddDebugLog("Удален({1}) {0} типа {2}.", obj, ObjectCount, obj.GetType().Name);
if (!IsObjectTracking)
return;
var found = _objects.Remove(obj);
if (ThrowOnRemoveDeleted && !found)
throw new ArgumentException(LocalizedStrings.ObjectWasAlreadyDeleted.Put(obj));
}
///
/// To change , reducing it by the number of deleted objects.
///
/// The number of deleted objects.
public void Remove(int count)
{
Interlocked.Exchange(ref _objectCount, _objectCount - count);
}
///
/// To delete active objects.
///
/// Active objects.
public void Remove(IEnumerable objects)
{
Remove(objects.Count());
//if (IsTraceObjects)
// _parent.AddDebugLog("Удаление {0} объектов типа {1}.", count, typeof(T).Name);
if (!IsObjectTracking)
return;
var hasDeleted = false;
foreach (var obj in objects)
{
if (!_objects.Remove(obj))
hasDeleted = true;
}
if (ThrowOnRemoveDeleted && hasDeleted)
throw new ArgumentException(LocalizedStrings.SomeObjectWasDeleted);
}
///
/// To clear active objects .
///
/// Whether to clear the objects counter.
public void Clear(bool resetCounter = false)
{
if (resetCounter)
_objectCount = 0;
if (!IsObjectTracking)
return;
// TODO: WeakReference?
_objects.Clear();
}
///
/// Load settings.
///
/// Settings storage.
public void Load(SettingsStorage storage)
{
IsObjectTracking = storage.GetValue(nameof(IsObjectTracking));
ThrowOnRemoveDeleted = storage.GetValue(nameof(ThrowOnRemoveDeleted));
IsTraceObjects = storage.GetValue(nameof(IsTraceObjects));
}
///
/// Save settings.
///
/// Settings storage.
public void Save(SettingsStorage storage)
{
storage.SetValue(nameof(IsObjectTracking), IsObjectTracking);
storage.SetValue(nameof(ThrowOnRemoveDeleted), ThrowOnRemoveDeleted);
storage.SetValue(nameof(IsTraceObjects), IsTraceObjects);
}
}
}