Skip to content

Commit 3608731

Browse files
Minor improvements to Managed SNI tracing (dotnet#3859)
* Minor improvements to Managed SNI tracing * Include ID * More changes
1 parent 6d92a25 commit 3608731

3 files changed

Lines changed: 12 additions & 24 deletions

File tree

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniPacket.netcore.cs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ internal sealed class SniPacket
2727
// _headerOffset is not needed because it is always 0
2828
private byte[] _data;
2929
private SniAsyncCallback _asyncIOCompletionCallback;
30-
#if DEBUG
3130
internal readonly int _id; // in debug mode every packet is assigned a unique id so that the entire lifetime can be tracked when debugging
31+
internal readonly SniHandle _owner; // used in debug builds to check that packets are being returned to the correct pool
32+
33+
#if DEBUG
3234
/// refcount = 0 means that a packet should only exist in the pool
3335
/// refcount = 1 means that a packet is active
3436
/// refcount > 1 means that a packet has been reused in some way and is a serious error
3537
internal int _refCount;
36-
internal readonly SniHandle _owner; // used in debug builds to check that packets are being returned to the correct pool
3738
internal string _traceTag; // used in debug builds to assist tracing what steps the packet has been through
3839

3940
#if TRACE_HISTORY
@@ -60,17 +61,6 @@ public enum Direction
6061
/// </summary>
6162
public bool IsActive => _refCount == 1;
6263

63-
public SniPacket(SniHandle owner, int id)
64-
: this()
65-
{
66-
#if TRACE_HISTORY
67-
_history = new List<History>();
68-
#endif
69-
_id = id;
70-
_owner = owner;
71-
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniPacket), EventType.INFO, "Connection Id {0}, Packet Id {1} instantiated,", args0: _owner?.ConnectionId, args1: _id);
72-
}
73-
7464
// the finalizer is only included in debug builds and is used to ensure that all packets are correctly recycled
7565
// it is not an error if a packet is dropped but it is undesirable so all efforts should be made to make sure we
7666
// do not drop them for the GC to pick up
@@ -83,8 +73,15 @@ public SniPacket(SniHandle owner, int id)
8373
}
8474

8575
#endif
86-
public SniPacket()
76+
77+
public SniPacket(SniHandle owner, int id)
8778
{
79+
#if DEBUG && TRACE_HISTORY
80+
_history = new List<History>();
81+
#endif
82+
_id = id;
83+
_owner = owner;
84+
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniPacket), EventType.INFO, "Connection Id {0}, Packet Id {1} instantiated,", args0: _owner?.ConnectionId, args1: _id);
8885
}
8986

9087
/// <summary>
@@ -286,23 +283,17 @@ private static void ReadFromStreamAsyncContinuation(Task<int> task, object state
286283
if (e != null)
287284
{
288285
SniLoadHandle.LastError = new SniError(SniProviders.TCP_PROV, SniCommon.InternalExceptionError, e);
289-
#if DEBUG
290286
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniPacket), EventType.ERR, "Connection Id {0}, Internal Exception occurred while reading data: {1}", args0: packet._owner?.ConnectionId, args1: e?.Message);
291-
#endif
292287
error = true;
293288
}
294289
else
295290
{
296291
packet._dataLength = task.Result;
297-
#if DEBUG
298292
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniPacket), EventType.INFO, "Connection Id {0}, Packet Id {1} _dataLength {2} read from stream.", args0: packet._owner?.ConnectionId, args1: packet._id, args2: packet._dataLength);
299-
#endif
300293
if (packet._dataLength == 0)
301294
{
302295
SniLoadHandle.LastError = new SniError(SniProviders.TCP_PROV, 0, SniCommon.ConnTerminatedError, Strings.SNI_ERROR_2);
303-
#if DEBUG
304296
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniPacket), EventType.ERR, "Connection Id {0}, No data read from stream, connection was terminated.", args0: packet._owner?.ConnectionId);
305-
#endif
306297
error = true;
307298
}
308299
}

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniPhysicalHandle.netcore.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,8 @@ public override SniPacket RentPacket(int headerSize, int dataSize)
3131
SniPacket packet;
3232
if (!_pool.TryGet(out packet))
3333
{
34-
#if DEBUG
3534
int id = Interlocked.Increment(ref s_packetId);
3635
packet = new SniPacket(this, id);
37-
#else
38-
packet = new SniPacket();
39-
#endif
4036
}
4137
#if DEBUG
4238
else

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3611,6 +3611,7 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
36113611
// to the outstanding GCRoot until AppDomain.Unload.
36123612
// We live with the above for the time being due to the constraints of the current
36133613
// reliability infrastructure provided by the CLR.
3614+
SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObject.ReadAsyncCallback | Info | State Object Id {0}, Entered ReadAsyncCallback.", _objectID);
36143615

36153616
TaskCompletionSource<object> source = _networkPacketTaskSource;
36163617
#if DEBUG

0 commit comments

Comments
 (0)