From b5c9144da8156b4c446e648f1cbd268caa232a42 Mon Sep 17 00:00:00 2001 From: Sun Serega Date: Fri, 8 Dec 2023 22:03:51 +0200 Subject: [PATCH] stabilize `ExecDebug` --- Modules.Packed/OpenCLABC.pas | 46 +++++++++++++++---- Modules/OpenCLABC.pas | 46 +++++++++++++++---- Packing/Descriptions/OpenCLABC.predoc | 46 +++++++++++++++---- .../07#GPUCommandContainer/ExecCache.pas" | 6 ++- Tests/Tester.pas | 2 +- 5 files changed, 119 insertions(+), 27 deletions(-) diff --git a/Modules.Packed/OpenCLABC.pas b/Modules.Packed/OpenCLABC.pas index 14d252c3..36162783 100644 --- a/Modules.Packed/OpenCLABC.pas +++ b/Modules.Packed/OpenCLABC.pas @@ -1492,9 +1492,12 @@ EventRetainReleaseData = record private static procedure RegisterExecCacheTry(command: object; is_new: boolean; descr: string) := ExecCacheTries.GetOrAdd(MakeName(command), name->new ConcurrentQueue<(boolean,string)>).Enqueue((is_new,descr)); + private static procedure DisposeAllCommands; public static procedure ReportExecCache(otp: System.IO.TextWriter := Console.Out) := lock otp do begin + DisposeAllCommands; + otp.WriteLine(System.Environment.StackTrace); foreach var kvp in ExecCacheTries.OrderBy(kvp->kvp.Key) do @@ -18886,9 +18889,10 @@ CLKernelArgPrivateCommon = record ExecCommandCLKernelCacheEntry = record k: CLKernel; cache: CLKernelArgCache; - last_use: DateTime; + last_use: TimeSpan; + static last_use_timer := Stopwatch.StartNew; - procedure Bump := last_use := DateTime.Now; + procedure Bump := last_use := last_use_timer.Elapsed; procedure TryRelease({$ifdef ExecDebug}command: object{$endif}) := if k<>nil then begin @@ -18943,19 +18947,30 @@ ExecCommandCLKernelCache = record end; end; - public procedure Release({$ifdef ExecDebug}command: object{$endif}) := - for var i := 0 to cache_size-1 do - data[i].TryRelease({$ifdef ExecDebug}command{$endif}); + public procedure Release({$ifdef ExecDebug}command: object{$endif}); + begin + if data=nil then raise new OpenCLABCInternalException($''); + for var i := 0 to cache_size-1 do + data[i].TryRelease({$ifdef ExecDebug}command{$endif}); + data := nil; + data_ind := nil; + end; end; - EnqueueableExecCommand = abstract class(GPUCommand) + EnqueueableExecCommand = abstract class(GPUCommand, IDisposable) private args: array of CLKernelArg; private const_args_setters: array of CLKernelArgSetter; private args_c, args_non_const_c: integer; + {$ifdef ExecDebug} + private static All := new ConcurrentBag; + {$endif ExecDebug} protected constructor(args: array of CLKernelArg); begin + {$ifdef ExecDebug} + All.Add(self); + {$endif ExecDebug} args := args.ToArray; self.args := args; self.const_args_setters := new CLKernelArgSetter[args.Length]; @@ -19095,11 +19110,26 @@ ExecCommandCLKernelCache = record if post_enq_act<>nil then Result.AddAction(post_enq_act); end; - protected procedure Finalize; override := - k_cache.Release({$ifdef ExecDebug}self{$endif}); + public procedure Dispose; + begin + if k_cache.data=nil then exit; + k_cache.Release({$ifdef ExecDebug}self{$endif}); + GC.SuppressFinalize(self); + end; + protected procedure Finalize; override := Dispose; end; +{$ifdef ExecDebug} +static procedure ExecDebug.DisposeAllCommands := + while true do + begin + var c: EnqueueableExecCommand; + if not EnqueueableExecCommand.All.TryTake(c) then break; + c.Dispose; + end; +{$endif ExecDebug} + {$endregion ExecCommand} {$region GetCommand} diff --git a/Modules/OpenCLABC.pas b/Modules/OpenCLABC.pas index 1f7e1f9b..b310db85 100644 --- a/Modules/OpenCLABC.pas +++ b/Modules/OpenCLABC.pas @@ -1380,9 +1380,12 @@ EventRetainReleaseData = record private static procedure RegisterExecCacheTry(command: object; is_new: boolean; descr: string) := ExecCacheTries.GetOrAdd(MakeName(command), name->new ConcurrentQueue<(boolean,string)>).Enqueue((is_new,descr)); + private static procedure DisposeAllCommands; public static procedure ReportExecCache(otp: System.IO.TextWriter := Console.Out) := lock otp do begin + DisposeAllCommands; + otp.WriteLine(System.Environment.StackTrace); foreach var kvp in ExecCacheTries.OrderBy(kvp->kvp.Key) do @@ -10324,9 +10327,10 @@ CLKernelArgPrivateCommon = record ExecCommandCLKernelCacheEntry = record k: CLKernel; cache: CLKernelArgCache; - last_use: DateTime; + last_use: TimeSpan; + static last_use_timer := Stopwatch.StartNew; - procedure Bump := last_use := DateTime.Now; + procedure Bump := last_use := last_use_timer.Elapsed; procedure TryRelease({$ifdef ExecDebug}command: object{$endif}) := if k<>nil then begin @@ -10381,19 +10385,30 @@ ExecCommandCLKernelCache = record end; end; - public procedure Release({$ifdef ExecDebug}command: object{$endif}) := - for var i := 0 to cache_size-1 do - data[i].TryRelease({$ifdef ExecDebug}command{$endif}); + public procedure Release({$ifdef ExecDebug}command: object{$endif}); + begin + if data=nil then raise new OpenCLABCInternalException($''); + for var i := 0 to cache_size-1 do + data[i].TryRelease({$ifdef ExecDebug}command{$endif}); + data := nil; + data_ind := nil; + end; end; - EnqueueableExecCommand = abstract class(GPUCommand) + EnqueueableExecCommand = abstract class(GPUCommand, IDisposable) private args: array of CLKernelArg; private const_args_setters: array of CLKernelArgSetter; private args_c, args_non_const_c: integer; + {$ifdef ExecDebug} + private static All := new ConcurrentBag; + {$endif ExecDebug} protected constructor(args: array of CLKernelArg); begin + {$ifdef ExecDebug} + All.Add(self); + {$endif ExecDebug} args := args.ToArray; self.args := args; self.const_args_setters := new CLKernelArgSetter[args.Length]; @@ -10533,11 +10548,26 @@ ExecCommandCLKernelCache = record if post_enq_act<>nil then Result.AddAction(post_enq_act); end; - protected procedure Finalize; override := - k_cache.Release({$ifdef ExecDebug}self{$endif}); + public procedure Dispose; + begin + if k_cache.data=nil then exit; + k_cache.Release({$ifdef ExecDebug}self{$endif}); + GC.SuppressFinalize(self); + end; + protected procedure Finalize; override := Dispose; end; +{$ifdef ExecDebug} +static procedure ExecDebug.DisposeAllCommands := + while true do + begin + var c: EnqueueableExecCommand; + if not EnqueueableExecCommand.All.TryTake(c) then break; + c.Dispose; + end; +{$endif ExecDebug} + {$endregion ExecCommand} {$region GetCommand} diff --git a/Packing/Descriptions/OpenCLABC.predoc b/Packing/Descriptions/OpenCLABC.predoc index a54c5e06..000847c8 100644 --- a/Packing/Descriptions/OpenCLABC.predoc +++ b/Packing/Descriptions/OpenCLABC.predoc @@ -1390,9 +1390,12 @@ type private static procedure RegisterExecCacheTry(command: object; is_new: boolean; descr: string) := ExecCacheTries.GetOrAdd(MakeName(command), name->new ConcurrentQueue<(boolean,string)>).Enqueue((is_new,descr)); + private static procedure DisposeAllCommands; public static procedure ReportExecCache(otp: System.IO.TextWriter := Console.Out) := lock otp do begin + DisposeAllCommands; + otp.WriteLine(System.Environment.StackTrace); foreach var kvp in ExecCacheTries.OrderBy(kvp->kvp.Key) do @@ -17820,9 +17823,10 @@ type ExecCommandCLKernelCacheEntry = record k: CLKernel; cache: CLKernelArgCache; - last_use: DateTime; + last_use: TimeSpan; + static last_use_timer := Stopwatch.StartNew; - procedure Bump := last_use := DateTime.Now; + procedure Bump := last_use := last_use_timer.Elapsed; procedure TryRelease({$ifdef ExecDebug}command: object{$endif}) := if k<>nil then begin @@ -17877,19 +17881,30 @@ type end; end; - public procedure Release({$ifdef ExecDebug}command: object{$endif}) := - for var i := 0 to cache_size-1 do - data[i].TryRelease({$ifdef ExecDebug}command{$endif}); + public procedure Release({$ifdef ExecDebug}command: object{$endif}); + begin + if data=nil then raise new OpenCLABCInternalException($''); + for var i := 0 to cache_size-1 do + data[i].TryRelease({$ifdef ExecDebug}command{$endif}); + data := nil; + data_ind := nil; + end; end; - EnqueueableExecCommand = abstract class(GPUCommand) + EnqueueableExecCommand = abstract class(GPUCommand, IDisposable) private args: array of CLKernelArg; private const_args_setters: array of CLKernelArgSetter; private args_c, args_non_const_c: integer; + {$ifdef ExecDebug} + private static All := new ConcurrentBag; + {$endif ExecDebug} protected constructor(args: array of CLKernelArg); begin + {$ifdef ExecDebug} + All.Add(self); + {$endif ExecDebug} args := args.ToArray; self.args := args; self.const_args_setters := new CLKernelArgSetter[args.Length]; @@ -18029,11 +18044,26 @@ type if post_enq_act<>nil then Result.AddAction(post_enq_act); end; - protected procedure Finalize; override := - k_cache.Release({$ifdef ExecDebug}self{$endif}); + public procedure Dispose; + begin + if k_cache.data=nil then exit; + k_cache.Release({$ifdef ExecDebug}self{$endif}); + GC.SuppressFinalize(self); + end; + protected procedure Finalize; override := Dispose; end; +{$ifdef ExecDebug} +static procedure ExecDebug.DisposeAllCommands := + while true do + begin + var c: EnqueueableExecCommand; + if not EnqueueableExecCommand.All.TryTake(c) then break; + c.Dispose; + end; +{$endif ExecDebug} + {$endregion ExecCommand} {$region GetCommand} diff --git "a/Tests/Exec/CLABC/02#\320\222\321\213\320\277\320\276\320\273\320\275\320\265\320\275\320\270\320\265 \320\276\321\207\320\265\321\200\320\265\320\264\320\265\320\271/07#GPUCommandContainer/ExecCache.pas" "b/Tests/Exec/CLABC/02#\320\222\321\213\320\277\320\276\320\273\320\275\320\265\320\275\320\270\320\265 \320\276\321\207\320\265\321\200\320\265\320\264\320\265\320\271/07#GPUCommandContainer/ExecCache.pas" index 4e87ddb5..04aa4981 100644 --- "a/Tests/Exec/CLABC/02#\320\222\321\213\320\277\320\276\320\273\320\275\320\265\320\275\320\270\320\265 \320\276\321\207\320\265\321\200\320\265\320\264\320\265\320\271/07#GPUCommandContainer/ExecCache.pas" +++ "b/Tests/Exec/CLABC/02#\320\222\321\213\320\277\320\276\320\273\320\275\320\265\320\275\320\270\320\265 \320\276\321\207\320\265\321\200\320\265\320\264\320\265\320\271/07#GPUCommandContainer/ExecCache.pas" @@ -2,7 +2,7 @@ procedure Test(inp: CommandQueue); begin - var Q := CLKernelCCQ.Create(inp).ThenExec1(1, 0).DiscardResult; + var Q := inp.MakeCCQ.ThenExec1(1, 0).DiscardResult; CLContext.Default.SyncInvoke(Q); CLContext.Default.SyncInvoke(Q); end; @@ -15,4 +15,6 @@ procedure Test(inp: CommandQueue); Test(new ParameterQueue('k2', code['k2'])); Test(HFQ(()->code['k3'], false)); -ExecDebug.ReportExecCache; \ No newline at end of file +{$ifdef ForceMaxDebug} +ExecDebug.ReportExecCache; +{$endif ForceMaxDebug} \ No newline at end of file diff --git a/Tests/Tester.pas b/Tests/Tester.pas index 5505ec67..6fab03ce 100644 --- a/Tests/Tester.pas +++ b/Tests/Tester.pas @@ -389,7 +389,7 @@ if 'OpenCLABC' in allowed_modules then begin - yield (lvl+1, 'CLContext', $'{TimeToText(cl_context_gen_time)} ({TimeToText(get_cl_context_gen_time_sum)})'); + yield (lvl+1, 'CLContext', $'{TimeToText(cl_context_gen_time)} ({get_cl_context_gen_time_sum.TotalSeconds:N4})'); yield cl_context_gen_comp.MakeLogLines(lvl+2, 'Comp').Single; yield cl_context_gen_exec.MakeLogLines(lvl+2, 'Exec').Single; end;