11#nullable enable
22using System . Text ;
3- using TorchSharp ;
43
54namespace TorchSharp
65{
7- public interface ILifetimeStatistics
6+
7+ /// <summary>
8+ /// Keeps track of the combined Tensor and PackedSequence statistics for the current thread. Can be queried to figure out performance/memory issues.
9+ /// </summary>
10+ public class ThreadDisposeScopeStatistics
811 {
912 /// <summary>
10- /// The number of disposables that were created on this thread, but weren't captured by a DisposeScope.
13+ /// The total number of Tensors and PackedSequence instances that were created on this thread, but weren't
14+ /// captured by a DisposeScope.
1115 /// </summary>
12- long CreatedOutsideScopeCount { get ; }
16+ public long CreatedOutsideScopeCount => TensorStatistics . CreatedOutsideScopeCount +
17+ PackedSequenceStatistics . CreatedOutsideScopeCount ;
18+
1319 /// <summary>
14- /// The number of disposables that were Disposed on this thread, but weren't captured by a DisposeScope.
20+ /// The number of Tensors and PackedSequence instances that were Disposed on this thread, but weren't
21+ /// captured by a DisposeScope.
1522 /// </summary>
16- long DisposedOutsideScopeCount { get ; }
23+ public long DisposedOutsideScopeCount => TensorStatistics . DisposedOutsideScopeCount +
24+ PackedSequenceStatistics . DisposedOutsideScopeCount ;
1725 /// <summary>
18- /// The number of disposables that were created on this thread and were captured by a DisposeScope.
26+ /// The number of Tensors and PackedSequence instances that were created on this thread and were captured
27+ /// by a DisposeScope.
1928 /// </summary>
20- long CreatedInScopeCount { get ; }
29+ public long CreatedInScopeCount => TensorStatistics . CreatedInScopeCount +
30+ PackedSequenceStatistics . CreatedInScopeCount ;
31+
2132 /// <summary>
22- /// The number of disposables that were disposed on this thread and were disposed while in a DisposeScope.
33+ /// The number of Tensors and PackedSequence instances that were disposed on this thread and were disposed
34+ /// while in a DisposeScope.
2335 /// </summary>
24- long DisposedInScopeCount { get ; }
36+ public long DisposedInScopeCount => TensorStatistics . DisposedInScopeCount +
37+ PackedSequenceStatistics . DisposedInScopeCount ;
38+
2539 /// <summary>
26- /// The number of disposables that were created on this thread outside a DisposeScope, and then
27- /// eventually were attached to one.
40+ /// The number of Tensors and PackedSequence instances that were created on this thread outside a DisposeScope,
41+ /// and then eventually were attached to one.
2842 /// </summary>
29- long AttachedToScopeCount { get ; }
43+ public long AttachedToScopeCount => TensorStatistics . AttachedToScopeCount +
44+ PackedSequenceStatistics . AttachedToScopeCount ;
45+
3046 /// <summary>
31- /// Number of disposables that were once included in the scope, but were subsequently detached.
47+ /// Number of Tensors and PackedSequence instances that were once included in a DisposeScope, but were
48+ /// subsequently detached.
3249 /// </summary>
33- long DetachedFromScopeCount { get ; }
50+ public long DetachedFromScopeCount => TensorStatistics . DetachedFromScopeCount +
51+ PackedSequenceStatistics . DetachedFromScopeCount ;
52+
3453 /// <summary>
35- /// The number of disposables that are currently live on the current thread.
54+ /// Exact number of Tensors and PackedSequence instances that are still live. Is the difference of all
55+ /// created objects minus all disposed objects.
3656 /// </summary>
37- long ThreadTotalLiveCount { get ; }
57+ public long ThreadTotalLiveCount => TensorStatistics . ThreadTotalLiveCount +
58+ PackedSequenceStatistics . ThreadTotalLiveCount ;
59+
3860 /// <summary>
39- /// Resets the counts for the current thread. See ThreadTotalLiveCount etc. Mainly used in tests to make sure
40- /// we get a clean slate on the thread.
61+ /// Resets the counts for the current thread. See ThreadTotalLiveCount etc. Mainly used in tests and memory
62+ /// leak debugging to make sure we get a clean slate on the thread.
4163 /// </summary>
42- void Reset ( ) ;
64+ public void Reset ( )
65+ {
66+ TensorStatistics . Reset ( ) ;
67+ PackedSequenceStatistics . Reset ( ) ;
68+ }
4369 /// <summary>
4470 /// A debug printout of all the properties and their values, suitable for a log or console output
4571 /// </summary>
4672 /// <returns></returns>
47- string ToString ( ) ;
48- }
49-
50- /// <summary>
51- /// Keeps track of the combined Tensor and PackedSequence statistics for the current thread. Can be queried to figure out performance/memory issues.
52- /// </summary>
53- public class ThreadDisposeScopeStatistics : ILifetimeStatistics
54- {
55- public long CreatedOutsideScopeCount => _TensorStatistics . CreatedOutsideScopeCount +
56- _PackedSequenceStatistics . CreatedOutsideScopeCount ;
57-
58- public long DisposedOutsideScopeCount => _TensorStatistics . DisposedOutsideScopeCount +
59- _PackedSequenceStatistics . DisposedOutsideScopeCount ;
60- public long CreatedInScopeCount => _TensorStatistics . CreatedInScopeCount +
61- _PackedSequenceStatistics . CreatedInScopeCount ;
62-
63- public long DisposedInScopeCount => _TensorStatistics . DisposedInScopeCount +
64- _PackedSequenceStatistics . DisposedInScopeCount ;
65-
66- public long AttachedToScopeCount => _TensorStatistics . AttachedToScopeCount +
67- _PackedSequenceStatistics . AttachedToScopeCount ;
68-
69- public long DetachedFromScopeCount => _TensorStatistics . DetachedFromScopeCount +
70- _PackedSequenceStatistics . DetachedFromScopeCount ;
71-
72- public long ThreadTotalLiveCount => _TensorStatistics . ThreadTotalLiveCount +
73- _PackedSequenceStatistics . ThreadTotalLiveCount ;
74-
75- public void Reset ( )
76- {
77- _TensorStatistics . Reset ( ) ;
78- _PackedSequenceStatistics . Reset ( ) ;
79- }
80-
8173 public override string ToString ( )
8274 {
83- return LifetimeStatisticsUtil . DebugString ( this ) ;
75+ var sb = new StringBuilder ( ) ;
76+ sb . Append ( "ThreadTotalLiveCount: " + ThreadTotalLiveCount ) ;
77+ sb . Append ( "; CreatedOutsideScopeCount: " + CreatedOutsideScopeCount ) ;
78+ sb . Append ( "; DisposedOutsideScopeCount: " + DisposedOutsideScopeCount ) ;
79+ sb . Append ( "; CreatedInScopeCount: " + CreatedInScopeCount ) ;
80+ sb . Append ( "; DisposedInScopeCount: " + DisposedInScopeCount ) ;
81+ sb . Append ( "; AttachedToScopeCount: " + AttachedToScopeCount ) ;
82+ sb . Append ( "; DetachedFromScopeCount: " + DetachedFromScopeCount ) ;
83+ return sb . ToString ( ) ;
8484 }
8585 /// <summary>
8686 /// Keeps track of the Tensor statistics for the current thread. Can be queried to figure out performance/memory issues.
8787 /// </summary>
88- public ILifetimeStatistics TensorStatistics => _TensorStatistics ;
89- internal LifetimeStatistics _TensorStatistics { get ; set ; } = new LifetimeStatistics ( ) ;
88+ public LifetimeStatistics TensorStatistics { get ; } = new LifetimeStatistics ( ) ;
9089 /// <summary>
9190 /// Keeps track of the PackedSequence statistics for the current thread. Can be queried to figure out performance/memory issues.
9291 /// </summary>
93- public ILifetimeStatistics PackedSequenceStatistics => _PackedSequenceStatistics ;
94- internal LifetimeStatistics _PackedSequenceStatistics { get ; set ; } = new LifetimeStatistics ( ) ;
92+ public LifetimeStatistics PackedSequenceStatistics { get ; } = new LifetimeStatistics ( ) ;
9593 }
9694
97- public class LifetimeStatistics : ILifetimeStatistics
95+ public class LifetimeStatistics
9896 {
97+ /// <summary>
98+ /// The number of disposables that were created on this thread, but weren't captured by a DisposeScope.
99+ /// </summary>
99100 public long CreatedOutsideScopeCount { get ; internal set ; }
101+ /// <summary>
102+ /// The number of disposables that were Disposed on this thread, but weren't captured by a DisposeScope.
103+ /// </summary>
100104 public long DisposedOutsideScopeCount { get ; internal set ; }
105+ /// <summary>
106+ /// The number of disposables that were created on this thread and were captured by a DisposeScope.
107+ /// </summary>
101108 public long CreatedInScopeCount { get ; internal set ; }
109+ /// <summary>
110+ /// The number of disposables that were disposed on this thread and were disposed while in a DisposeScope.
111+ /// </summary>
102112 public long DisposedInScopeCount { get ; internal set ; }
113+ /// <summary>
114+ /// The number of disposables that were created on this thread outside a DisposeScope, and then
115+ /// eventually were attached to one.
116+ /// </summary>
103117 public long AttachedToScopeCount { get ; internal set ; }
118+ /// <summary>
119+ /// Number of disposables that were once included in a DisposeScope, but were subsequently detached.
120+ /// </summary>
104121 public long DetachedFromScopeCount { get ; internal set ; }
105122 /// <summary>
106123 /// Exact number of objects that are still live. Is the difference of all created objects
107124 /// minus all disposed objects.
108125 /// </summary>
109- public long ThreadTotalLiveCount => ( CreatedInScopeCount - DisposedInScopeCount ) + ( CreatedOutsideScopeCount - DisposedOutsideScopeCount ) ;
126+ public long ThreadTotalLiveCount => ( CreatedInScopeCount - DisposedInScopeCount ) +
127+ ( CreatedOutsideScopeCount - DisposedOutsideScopeCount ) ;
110128
129+ /// <summary>
130+ /// Resets the counts for the current thread. See ThreadTotalLiveCount etc. Mainly used in tests to make sure
131+ /// we get a clean slate on the thread.
132+ /// </summary>
111133 public void Reset ( )
112134 {
113135 CreatedOutsideScopeCount = 0 ;
@@ -117,23 +139,20 @@ public void Reset()
117139 AttachedToScopeCount = 0 ;
118140 DetachedFromScopeCount = 0 ;
119141 }
142+ /// <summary>
143+ /// A debug printout of all the properties and their values, suitable for a log or console output
144+ /// </summary>
145+ /// <returns></returns>
120146 public override string ToString ( )
121- {
122- return LifetimeStatisticsUtil . DebugString ( this ) ;
123- }
124- }
125- static class LifetimeStatisticsUtil
126- {
127- public static string DebugString ( this ILifetimeStatistics statistics )
128147 {
129148 var sb = new StringBuilder ( ) ;
130- sb . Append ( "ThreadTotalLiveCount: " + statistics . ThreadTotalLiveCount ) ;
131- sb . Append ( "; CreatedOutsideScopeCount: " + statistics . CreatedOutsideScopeCount ) ;
132- sb . Append ( "; DisposedOutsideScopeCount: " + statistics . DisposedOutsideScopeCount ) ;
133- sb . Append ( "; CreatedInScopeCount: " + statistics . CreatedInScopeCount ) ;
134- sb . Append ( "; DisposedInScopeCount: " + statistics . DisposedInScopeCount ) ;
135- sb . Append ( "; AttachedToScopeCount: " + statistics . AttachedToScopeCount ) ;
136- sb . Append ( "; DetachedFromScopeCount: " + statistics . DetachedFromScopeCount ) ;
149+ sb . Append ( "ThreadTotalLiveCount: " + ThreadTotalLiveCount ) ;
150+ sb . Append ( "; CreatedOutsideScopeCount: " + CreatedOutsideScopeCount ) ;
151+ sb . Append ( "; DisposedOutsideScopeCount: " + DisposedOutsideScopeCount ) ;
152+ sb . Append ( "; CreatedInScopeCount: " + CreatedInScopeCount ) ;
153+ sb . Append ( "; DisposedInScopeCount: " + DisposedInScopeCount ) ;
154+ sb . Append ( "; AttachedToScopeCount: " + AttachedToScopeCount ) ;
155+ sb . Append ( "; DetachedFromScopeCount: " + DetachedFromScopeCount ) ;
137156 return sb . ToString ( ) ;
138157 }
139158 }
0 commit comments