Skip to content

Commit

Permalink
Fix: Sampling prof after rerun project
Browse files Browse the repository at this point in the history
Fix: old GetCallStack
Ref: Lock in TAbstractContainer
  • Loading branch information
yavfast@gmail.com committed Jun 28, 2014
1 parent 89120bc commit 0f2aacb
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 192 deletions.
1 change: 1 addition & 0 deletions ClassUtils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,4 @@ initialization
finalization
FreeAndNil(TGradientInfoList._GradientInfoList);
end.

68 changes: 66 additions & 2 deletions Collections/Collections.Base.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1616,8 +1616,11 @@ TRefCountedObject = class abstract(TInterfacedObject, IInterface)
TAbstractContainer = class abstract(TRefCountedObject)
private
FVersion: NativeInt;

FLock: TMREWSync;
FThreadSafe: LongBool;
protected
procedure SetThreadSafe(const Value: LongBool); virtual;

/// <summary>Returns the number of elements in the collection.</summary>
/// <returns>A positive value specifying the number of elements in the collection.</returns>
/// <remarks>A call to this method can be costly because some
Expand All @@ -1630,6 +1633,9 @@ TAbstractContainer = class abstract(TRefCountedObject)
public
const CDefaultSize = 32;

constructor Create(const AThreadSafe: LongBool = False);
destructor Destroy; override;

/// <summary>Returns the current version of the collection.</summary>
/// <returns>An integer value specifying the current "structural version" of the collection.</returns>
/// <remarks>This function returns a number that is modified by the implementing collection each time
Expand All @@ -1648,6 +1654,15 @@ TAbstractContainer = class abstract(TRefCountedObject)
/// <remarks>Accesing this property can be costly because some
/// collections cannot detect the number of stored elements directly, resorting to enumerating themselves.</remarks>
property Count: NativeInt read GetCount;

procedure LockForRead; inline;
procedure UnLockForRead; inline;

procedure LockForWrite; inline;
procedure UnLockForWrite; inline;

property Lock: TMREWSync read FLock;
property ThreadSafe: LongBool read FThreadSafe write SetThreadSafe;
end;

/// <summary>Base class for all collections.</summary>
Expand Down Expand Up @@ -3808,9 +3823,54 @@ function TEnexExtOps<T>.Select<TOut>(const ASelector: TFunc<T, TOut>; const ARul

{ TAbstractContainer }

constructor TAbstractContainer.Create(const AThreadSafe: LongBool);
begin
inherited Create;

FLock := TMREWSync.Create;
ThreadSafe := AThreadSafe;
end;

destructor TAbstractContainer.Destroy;
begin
FThreadSafe := False;
FreeAndNil(FLock);

inherited;
end;

procedure TAbstractContainer.LockForRead;
begin
if FThreadSafe then
FLock.BeginRead;
end;

procedure TAbstractContainer.LockForWrite;
begin
if FThreadSafe then
FLock.BeginWrite;
end;

procedure TAbstractContainer.NotifyCollectionChanged;
begin
Inc(FVersion);
AtomicIncrement(FVersion);
end;

procedure TAbstractContainer.SetThreadSafe(const Value: LongBool);
begin
FThreadSafe := Value;
end;

procedure TAbstractContainer.UnLockForRead;
begin
if FThreadSafe then
FLock.EndRead;
end;

procedure TAbstractContainer.UnLockForWrite;
begin
if FThreadSafe then
FLock.EndWrite;
end;

function TAbstractContainer.Version: NativeInt;
Expand Down Expand Up @@ -4088,6 +4148,8 @@ function TSequence<T>.Concat(const ACollection: ISequence<T>): ISequence<T>;

constructor TSequence<T>.Create(const ARules: TRules<T>);
begin
inherited Create;

FElementRules := ARules;
end;

Expand Down Expand Up @@ -5077,6 +5139,8 @@ function TAssociation<TKey, TValue>.CompareValues(const ALeft, ARight: TValue):

constructor TAssociation<TKey, TValue>.Create(const AKeyRules: TRules<TKey>; const AValueRules: TRules<TValue>);
begin
inherited Create;

FKeyRules := AKeyRules;
FValueRules := AValueRules;
end;
Expand Down
42 changes: 1 addition & 41 deletions Collections/Collections.Dictionaries.pas
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,6 @@ TEntry = record
FFreeCount: NativeInt;
FFreeList: NativeInt;

FLock: TMREWSync;
FThreadSafe: Boolean;

{ Internal }
procedure InitializeInternals(const ACapacity: NativeInt);
procedure Insert(const AKey: TKey; const AValue: TValue; const AShouldAdd: Boolean = true);
Expand Down Expand Up @@ -289,12 +286,6 @@ TEntry = record

destructor Destroy; override;

procedure LockForRead; inline;
procedure UnLockForRead; inline;

procedure LockForWrite; inline;
procedure UnLockForWrite; inline;

/// <summary>Clears the contents of the dictionary.</summary>
procedure Clear(); override;

Expand Down Expand Up @@ -339,8 +330,6 @@ TEntry = record
/// <exception cref="SysUtils|EArgumentOutOfRangeException"><paramref name="AStartIndex"/> is out of bounds.</exception>
/// <exception cref="Collections.Base|EArgumentOutOfSpaceException">The array is not long enough.</exception>
procedure CopyTo(var AArray: array of TPair<TKey,TValue>; const AStartIndex: NativeInt); overload; override;

property Lock: TMREWSync read FLock;
end;

/// <summary>The generic <c>dictionary</c> collection designed to store objects.</summary>
Expand Down Expand Up @@ -1049,8 +1038,7 @@ constructor TDictionary<TKey, TValue>.Create(const AKeyRules: TRules<TKey>;
begin
inherited Create(AKeyRules, AValueRules);

FLock := TMREWSync.Create;
FThreadSafe := AThreadSafe;
ThreadSafe := AThreadSafe;

if AInitialCapacity <= 0 then
InitializeInternals(CDefaultSize)
Expand All @@ -1060,11 +1048,7 @@ constructor TDictionary<TKey, TValue>.Create(const AKeyRules: TRules<TKey>;

destructor TDictionary<TKey, TValue>.Destroy;
begin
LockForWrite;
inherited Destroy;
UnLockForWrite;

FreeAndNil(FLock);
end;

constructor TDictionary<TKey, TValue>.Create(const AInitialCapacity: NativeInt = TAbstractContainer.CDefaultSize;
Expand Down Expand Up @@ -1243,18 +1227,6 @@ procedure TDictionary<TKey, TValue>.Insert(const AKey: TKey; const AValue: TValu
UnLockForRead;
end;

procedure TDictionary<TKey, TValue>.LockForRead;
begin
if FThreadSafe then
FLock.BeginRead;
end;

procedure TDictionary<TKey, TValue>.LockForWrite;
begin
if FThreadSafe then
FLock.BeginWrite;
end;

procedure TDictionary<TKey, TValue>.Remove(const AKey: TKey);
begin
LockForWrite;
Expand Down Expand Up @@ -1392,18 +1364,6 @@ function TDictionary<TKey, TValue>.TryGetValue(const AKey: TKey; out AFoundValue
end;


procedure TDictionary<TKey, TValue>.UnLockForRead;
begin
if FThreadSafe then
FLock.EndRead;
end;

procedure TDictionary<TKey, TValue>.UnLockForWrite;
begin
if FThreadSafe then
FLock.EndWrite;
end;

{ TDictionary<TKey, TValue>.TEnumerator }

function TDictionary<TKey, TValue>.TEnumerator.TryMoveNext(out ACurrent: TPair<TKey, TValue>): Boolean;
Expand Down
40 changes: 1 addition & 39 deletions Collections/Collections.Queues.pas
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ TEnumerator = class(TAbstractEnumerator<T>)
FLength: NativeInt;
FArray: TArray<T>;

FLock: TMREWSync;
FThreadSafe: Boolean;

procedure SetCapacity(const ANewCapacity : NativeInt);
protected
/// <summary>Returns the number of elements in the queue.</summary>
Expand Down Expand Up @@ -118,12 +115,6 @@ TEnumerator = class(TAbstractEnumerator<T>)

destructor Destroy; override;

procedure LockForRead; inline;
procedure UnLockForRead; inline;

procedure LockForWrite; inline;
procedure UnLockForWrite; inline;

/// <summary>Clears the contents of the queue.</summary>
procedure Clear(); override;

Expand Down Expand Up @@ -961,8 +952,7 @@ constructor TQueue<T>.Create(const ARules: TRules<T>; const AInitialCapacity: Na
begin
inherited Create(ARules);

FLock := TMREWSync.Create;
FThreadSafe := AThreadSafe;
ThreadSafe := AThreadSafe;

LockForWrite;

Expand Down Expand Up @@ -1137,11 +1127,7 @@ function TQueue<T>.Dequeue: T;

destructor TQueue<T>.Destroy;
begin
LockForWrite;
inherited Destroy;
UnLockForWrite;

FreeAndNil(FLock);
end;

function TQueue<T>.GetCapacity: NativeInt;
Expand Down Expand Up @@ -1217,18 +1203,6 @@ function TQueue<T>.LastOrDefault(const ADefault: T): T;
UnLockForRead;
end;

procedure TQueue<T>.LockForRead;
begin
if FThreadSafe then
FLock.BeginRead;
end;

procedure TQueue<T>.LockForWrite;
begin
if FThreadSafe then
FLock.BeginWrite;
end;

function TQueue<T>.Max: T;
var
I, LH: NativeInt;
Expand Down Expand Up @@ -1357,18 +1331,6 @@ function TQueue<T>.SingleOrDefault(const ADefault: T): T;
end;
end;

procedure TQueue<T>.UnLockForRead;
begin
if FThreadSafe then
FLock.EndRead;
end;

procedure TQueue<T>.UnLockForWrite;
begin
if FThreadSafe then
FLock.EndWrite;
end;

{ TQueue<T>.TEnumerator }

function TQueue<T>.TEnumerator.TryMoveNext(out ACurrent: T): Boolean;
Expand Down
8 changes: 4 additions & 4 deletions DbgCodeProfiler.pas
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ function TDbgCodeProfiler.ProcessTrackBreakPoint(DebugEvent: PDebugEvent): LongB
// --- Ðåãèñòðèðóåì âûçûâàåìóþ ôóíêöèþ â ïðîöåññå --- //
TInterlocked.Increment(gvDebuger.ProcessData.DbgTrackEventCount);

TrackFuncInfo := TCodeTrackFuncInfo(gvDebuger.ProcessData^.DbgTrackFuncList.GetTrackFuncInfo(TrackBp^.FuncInfo));
gvDebuger.ProcessData^.DbgTrackUnitList.CheckTrackFuncInfo(TrackFuncInfo);
TrackFuncInfo := TCodeTrackFuncInfo(gvDebuger.ProcessData.DbgTrackFuncList.GetTrackFuncInfo(TrackBp^.FuncInfo));
gvDebuger.ProcessData.DbgTrackUnitList.CheckTrackFuncInfo(TrackFuncInfo);

TrackFuncInfo.IncCallCount;
TrackFuncInfo.TrackUnitInfo.IncCallCount;
Expand All @@ -158,8 +158,8 @@ function TDbgCodeProfiler.ProcessTrackBreakPoint(DebugEvent: PDebugEvent): LongB
ParentFuncInfo := TFuncInfo(ParentCallFuncInfo.FuncInfo);
if Assigned(ParentFuncInfo) then
begin
ParentTrackFuncInfo := TCodeTrackFuncInfo(gvDebuger.ProcessData^.DbgTrackFuncList.GetTrackFuncInfo(ParentFuncInfo));
gvDebuger.ProcessData^.DbgTrackUnitList.CheckTrackFuncInfo(ParentTrackFuncInfo);
ParentTrackFuncInfo := TCodeTrackFuncInfo(gvDebuger.ProcessData.DbgTrackFuncList.GetTrackFuncInfo(ParentFuncInfo));
gvDebuger.ProcessData.DbgTrackUnitList.CheckTrackFuncInfo(ParentTrackFuncInfo);

ParentTrackFuncInfo.AddChildCall(Address);
end;
Expand Down
2 changes: 1 addition & 1 deletion DbgMemoryProfiler.pas
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TDbgMemoryProfiler = class

implementation

uses Debuger;
uses Debuger, Collections.Base;

const
_MAX_MEM_INFO_BUF_COUNT = 512;
Expand Down
22 changes: 12 additions & 10 deletions DbgSamplingProfiler.pas
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ procedure TDbgSamplingProfiler.AddThreadSamplingInfo(ThreadData: PThreadData);
if Res = 0 then
begin
if gvDebuger.UpdateThreadContext(ThreadData, CONTEXT_CONTROL) then
gvDebuger.GetCallStackEx(ThreadData, Stack);
begin
gvDebuger.GetCallStack(ThreadData, Stack);
end;
end;

ResumeThread(ThreadData^.ThreadHandle);
Expand Down Expand Up @@ -145,12 +147,12 @@ procedure TDbgSamplingProfiler.ProcessDbgSamplingInfo;
I: Integer;
Threads: TDbgActiveThreads;
begin
CPUTime := _QueryProcessCycleTime(gvDebuger.ProcessData^.AttachedProcessHandle);
CPUTime := _QueryProcessCycleTime(gvDebuger.ProcessData.AttachedProcessHandle);
// TODO: Êîíòðîëü çàãðóçêè CPU
if CPUTime > gvDebuger.ProcessData^.SamplingCPUTime then
if CPUTime > gvDebuger.ProcessData.SamplingCPUTime then
begin
gvDebuger.ProcessData^.SamplingCPUTime := CPUTime;
TInterlocked.Increment(gvDebuger.ProcessData^.SamplingCount);
gvDebuger.ProcessData.SamplingCPUTime := CPUTime;
TInterlocked.Increment(gvDebuger.ProcessData.SamplingCount);

gvDebuger.GetActiveThreads(Threads);

Expand Down Expand Up @@ -224,13 +226,13 @@ procedure TDbgSamplingProfiler.ProcessThreadSamplingAddress(ThData: PThreadData;
end;

// --- Ðåãèñòðèðóåì âûçûâàåìóþ ôóíêöèþ â ïðîöåññå --- //
TrackFuncInfo := TCodeTrackFuncInfo(gvDebuger.ProcessData^.DbgTrackFuncList.GetTrackFuncInfo(FuncInfo));
gvDebuger.ProcessData^.DbgTrackUnitList.CheckTrackFuncInfo(TrackFuncInfo);
TrackFuncInfo := TCodeTrackFuncInfo(gvDebuger.ProcessData.DbgTrackFuncList.GetTrackFuncInfo(FuncInfo));
gvDebuger.ProcessData.DbgTrackUnitList.CheckTrackFuncInfo(TrackFuncInfo);

TrackFuncInfo.IncCallCount;

// Äîáàâëåíèå â ñïèñîê àêòèâíûõ þíèòîâ
gvDebuger.ProcessData^.DbgTrackUsedUnitList.AddOrSetValue(UnitInfo, TrackFuncInfo.TrackUnitInfo);
gvDebuger.ProcessData.DbgTrackUsedUnitList.AddOrSetValue(UnitInfo, TrackFuncInfo.TrackUnitInfo);

// Äîáàâëÿåì ëèíê ñ òåêóùåé ôóíêöèè íà ðîäèòåëüñêóþ
ParentCallFuncInfo := TrackFuncInfo.AddParentCall(ParentFuncAddr);
Expand All @@ -241,8 +243,8 @@ procedure TDbgSamplingProfiler.ProcessThreadSamplingAddress(ThData: PThreadData;
ParentFuncInfo := TFuncInfo(ParentCallFuncInfo.FuncInfo);
if Assigned(ParentFuncInfo) then
begin
ParentTrackFuncInfo := TCodeTrackFuncInfo(gvDebuger.ProcessData^.DbgTrackFuncList.GetTrackFuncInfo(ParentFuncInfo));
gvDebuger.ProcessData^.DbgTrackUnitList.CheckTrackFuncInfo(ParentTrackFuncInfo);
ParentTrackFuncInfo := TCodeTrackFuncInfo(gvDebuger.ProcessData.DbgTrackFuncList.GetTrackFuncInfo(ParentFuncInfo));
gvDebuger.ProcessData.DbgTrackUnitList.CheckTrackFuncInfo(ParentTrackFuncInfo);

ParentTrackFuncInfo.AddChildCall(FuncAddr);
end;
Expand Down
2 changes: 1 addition & 1 deletion DbgSyncObjsProfiler.pas
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function TDbgSyncObjsProfiler.ProcessSyncObjsInfoQueue: LongBool;
if FSyncObjsInfoQueue.Count < _MAX_SYNC_OBJS_INFO_BUF_COUNT then
begin
Buf := FSyncObjsInfoQueue.First;
if (gvDebuger.ProcessData^.CurDbgPointIdx - Buf^.DbgPointIdx) <= 2 then
if (gvDebuger.ProcessData.CurDbgPointIdx - Buf^.DbgPointIdx) <= 2 then
Exit;
end;

Expand Down
Loading

0 comments on commit 0f2aacb

Please sign in to comment.