1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
| unit InstancesMngr;
interface
uses SysUtils, Classes, Contnrs;
//debug unit - Adrien Reboisson
//
//
type
TThObjectClass = class of TObject;
TThInstanceWatch = class
private
FInstances: Cardinal;
FInstanceSize: Cardinal;
FName: string;
FInstancesMax: Cardinal;
public
constructor Create(AName: string; AInstanceSize: Integer);
procedure IncInstances;
procedure DecInstances;
function CountInstances: Cardinal;
function MaxCountedInstances: Cardinal;
function GetSizeAllocated : Cardinal;
function GetMaxSizeAllocated: Cardinal;
property ObjectInstanceSize: Cardinal read FInstanceSize;
property Name: string read FName;
end;
TThInstGlobalStatus = record
CurrentInstancesWatched: Integer;
CurrentInstancesSize: Integer;
MaxInstancesWatched: Integer;
MaxInstancesSize: Integer;
end;
TThInstMngrStatusEvent = procedure (ASender: TObject; AInstanceWatch: TThInstanceWatch) of object;
TThInstMngrGlobalStatusEvent = procedure (ASender: TObject; AGlobalInfo : TThInstGlobalStatus) of object;
TThInstancesManager = class (TThLockObject)
private
FInstancesList: TObjectList;
FOnStatus: TThInstMngrStatusEvent;
FMaxInstancesWatched: Cardinal;
FMaxinstancesSizeWatched: Cardinal;
FOnGlobalStatus: TThInstMngrGlobalStatusEvent;
FActive: Boolean;
function Find(const S: ShortString; var Index: Integer): Boolean;
public
constructor Create;
destructor Destroy; override;
procedure InstanceCreated(AObject: TObject);
procedure InstanceFreed(AObject: TObject);
function GetInstancesStat(AClassName: string): TThInstanceWatch; overload;
function GetInstancesStat(AClass: TThObjectClass): TThInstanceWatch; overload;
function GetMaxInstancesWatched: Cardinal;
function GetMaxInstancesSizeWatched: Cardinal;
property OnStatus: TThInstMngrStatusEvent read FOnStatus write FOnStatus;
property OnGlobalStatus: TThInstMngrGlobalStatusEvent read FOnGlobalStatus write FOnGlobalStatus;
procedure GetWatchResults;
property Active: Boolean read FActive write FActive;
end;
procedure iwInstanceCreated(AObject: TObject);
procedure iwInstanceDestroyed(AObject: TObject);
procedure iwRegisterStatusProcs(AInstProc: TThInstMngrStatusEvent;
AGlobProc : TThInstMngrGlobalStatusEvent);
procedure iwDoCallBack;
procedure iwStartWatch;
procedure iwEndWatch;
implementation
var
GInstancesMngr : TThInstancesManager= nil;
procedure CheckManagerCreated;
begin
if not Assigned(GInstancesMngr) then
GInstancesMngr := TThInstancesManager.Create;
end;
procedure iwStartWatch;
begin
CheckManagerCreated;
GInstancesMngr.Active := True;
end;
procedure iwEndWatch;
begin
CheckManagerCreated;
GInstancesMngr.Active := False;
end;
procedure iwInstanceCreated(AObject: TObject);
begin
CheckManagerCreated;
GInstancesMngr.InstanceCreated(AObject);
end;
procedure iwRegisterStatusProcs(AInstProc: TThInstMngrStatusEvent;
AGlobProc : TThInstMngrGlobalStatusEvent);
begin
CheckManagerCreated;
GInstancesMngr.OnStatus := AInstProc;
GInstancesMngr.OnGlobalStatus := AGlobProc;
end;
procedure iwInstanceDestroyed(AObject: TObject);
begin
CheckManagerCreated;
GInstancesMngr.InstanceFreed(AObject);
end;
procedure iwDoCallBack;
begin
CheckManagerCreated;
GInstancesMngr.GetWatchResults;
end;
{ TThInstancesManager }
function TThInstancesManager.Find(const S: ShortString; var Index: Integer): Boolean;
var
L, H, I : Integer;
C: Integer;
begin
Result := False;
L := 0;
H := FInstancesList.Count - 1;
while L <= H do
begin
I := (L + H) shr 1;
C := AnsiCompareStr(TThInstanceWatch(FInstancesList[I]).Name, S);
if C < 0 then L := I + 1 else
begin
H := I - 1;
if C = 0 then
begin
Result := True;
L := I;
Break;
end;
end;
end;
Index := L;
end;
constructor TThInstancesManager.Create;
begin
inherited;
FInstancesList := TObjectList.Create;
FMaxInstancesWatched := 0;
FMaxinstancesSizeWatched := 0;
FActive := False;
end;
destructor TThInstancesManager.Destroy;
begin
FInstancesList.Free;
inherited;
end;
procedure TThInstancesManager.InstanceCreated(AObject: TObject);
var
LWatch: TThInstanceWatch;
I : Integer;
begin
if Active then
begin
Lock;
try
if Find(AObject.ClassType.ClassName, I) then
LWatch := TThInstanceWatch(FInstancesList[I])
else
begin
LWatch := TThInstanceWatch.Create(AObject.ClassType.ClassName,
AObject.ClassType.InstanceSize);
FInstancesList.Insert(I, LWatch);
end;
LWatch.IncInstances;
finally
Unlock;
end;
end;
end;
procedure TThInstancesManager.InstanceFreed(AObject: TObject);
var
I : Integer;
begin
if Active then
begin
Lock;
try
if Find(AObject.ClassType.ClassName, I) then
TThInstanceWatch(FInstancesList[i]).DecInstances
else
raise Exception.CreateFmt('Instance never watched : %s', [AObject.ClassType.ClassName]);
finally
Unlock;
end;
end;
end;
function TThInstancesManager.GetInstancesStat(
AClassName: string): TThInstanceWatch;
var
I: Integer;
begin
Result := nil;
Lock;
try
if Find(AClassName, I) then
Result := TThInstanceWatch(FInstancesList[I]);
finally
Unlock;
end;
end;
function TThInstancesManager.GetInstancesStat(
AClass: TThObjectClass): TThInstanceWatch;
begin
Result := GetInstancesStat(AClass.ClassName);
end;
procedure TThInstancesManager.GetWatchResults;
var
I: integer;
N, S: Cardinal;
LWatch: TThInstanceWatch;
LStatus: TThInstGlobalStatus;
begin
if (Active) and (not ((@FOnStatus = nil) and (@FOnGlobalStatus = nil))) then
begin
N := 0;
S := 0;
Lock;
try
for I := 0 to Pred(FInstancesList.Count) do
begin
LWatch := TThInstanceWatch(FInstancesList[I]);
Inc(N, LWatch.CountInstances);
Inc(S, LWatch.GetSizeAllocated);
if Assigned(FOnStatus) then
FOnStatus(Self, LWatch);
if N > FMaxInstancesWatched then
FMaxInstancesWatched := N;
if S > FMaxinstancesSizeWatched then
FMaxinstancesSizeWatched := S;
end;
if Assigned (FOnGlobalStatus) then
begin
LStatus.CurrentInstancesWatched := N;
LStatus.CurrentInstancesSize := S;
LStatus.MaxInstancesWatched := FMaxInstancesWatched;
LStatus.MaxInstancesSize := FMaxinstancesSizeWatched;
FOnGlobalStatus(Self, LStatus);
end;
finally
Unlock;
end;
end;
end;
function TThInstancesManager.GetMaxInstancesSizeWatched: Cardinal;
begin
Result := FMaxinstancesSizeWatched;
end;
function TThInstancesManager.GetMaxInstancesWatched: Cardinal;
begin
Result := FMaxInstancesWatched;
end;
{ TThInstanceWatch }
function TThInstanceWatch.CountInstances: Cardinal;
begin
Result := FInstances;
end;
constructor TThInstanceWatch.Create(AName: string; AInstanceSize: Integer);
begin
inherited Create;
FName := AName;
FInstanceSize := AInstanceSize;
FInstances := 0;
FInstancesMax := 0;
end;
procedure TThInstanceWatch.DecInstances;
begin
Dec(FInstances);
end;
function TThInstanceWatch.GetMaxSizeAllocated: Cardinal;
begin
Result := FInstancesMax * FInstanceSize;
end;
function TThInstanceWatch.GetSizeAllocated: Cardinal;
begin
Result := FInstances * FInstanceSize;
end;
procedure TThInstanceWatch.IncInstances;
begin
Inc(FInstances);
if FInstances > FInstancesMax then
Inc(FInstancesMax);
end;
function TThInstanceWatch.MaxCountedInstances: Cardinal;
begin
Result := FInstancesMax;
end;
initialization
iwStartWatch;
finalization
if Assigned(GInstancesMngr) then
GInstancesMngr.Free;
end. |
Partager