@@ -643,8 +643,10 @@ private WriteResult WriteMessageInsideLock(PhysicalConnection physical, Message
643643 Multiplexer ? . OnInfoMessage ( $ "reentrant call to WriteMessageTakingWriteLock for { message . CommandAndKey } , { existingMessage . CommandAndKey } is still active") ;
644644 return WriteResult . NoConnectionAvailable ;
645645 }
646+ #if DEBUG
646647 int startWriteTime = Environment . TickCount ;
647648 try
649+ #endif
648650 {
649651 physical . SetWriting ( ) ;
650652 var messageIsSent = false ;
@@ -679,6 +681,7 @@ private WriteResult WriteMessageInsideLock(PhysicalConnection physical, Message
679681 return WriteMessageToServerInsideWriteLock ( physical , message ) ;
680682 }
681683 }
684+ #if DEBUG
682685 finally
683686 {
684687 int endWriteTime = Environment . TickCount ;
@@ -689,10 +692,12 @@ private WriteResult WriteMessageInsideLock(PhysicalConnection physical, Message
689692 _maxWriteCommand = message ? . Command ?? default ;
690693 }
691694 }
695+ #endif
692696 }
693-
697+ #if DEBUG
694698 private volatile int _maxWriteTime = - 1 ;
695699 private RedisCommand _maxWriteCommand ;
700+ #endif
696701
697702 [ Obsolete ( "prefer async" ) ]
698703 internal WriteResult WriteMessageTakingWriteLockSync ( PhysicalConnection physical , Message message )
@@ -755,10 +760,14 @@ private bool PushToBacklog(Message message, bool onlyIfExists)
755760 private void StartBacklogProcessor ( )
756761 {
757762 var sched = Multiplexer . SocketManager ? . SchedulerPool ?? DedicatedThreadPoolPipeScheduler . Default ;
763+ #if DEBUG
758764 _backlogProcessorRequestedTime = Environment . TickCount ;
765+ #endif
759766 sched . Schedule ( s_ProcessBacklog , _weakRefThis ) ;
760767 }
768+ #if DEBUG
761769 private volatile int _backlogProcessorRequestedTime ;
770+ #endif
762771
763772 private static readonly Action < object > s_ProcessBacklog = s =>
764773 {
@@ -807,20 +816,26 @@ private void ProcessBacklog()
807816 LockToken token = default ;
808817 try
809818 {
819+ #if DEBUG
810820 int tryToAcquireTime = Environment . TickCount ;
811821 var msToStartWorker = unchecked ( tryToAcquireTime - _backlogProcessorRequestedTime ) ;
812822 int failureCount = 0 ;
823+ #endif
813824 while ( true )
814825 {
815826 // try and get the lock; if unsuccessful, check for termination
816827 token = _singleWriterMutex . TryWait ( ) ;
817828 if ( token ) break ; // got the lock
818829 lock ( _backlog ) { if ( _backlog . Count == 0 ) return ; }
830+ #if DEBUG
819831 failureCount ++ ;
832+ #endif
820833 }
821834 _backlogStatus = BacklogStatus . Started ;
835+ #if DEBUG
822836 int acquiredTime = Environment . TickCount ;
823837 var msToGetLock = unchecked ( acquiredTime - tryToAcquireTime ) ;
838+ #endif
824839
825840 // so now we are the writer; write some things!
826841 Message message ;
@@ -841,14 +856,15 @@ private void ProcessBacklog()
841856 {
842857 _backlogStatus = BacklogStatus . RecordingTimeout ;
843858 var ex = Multiplexer . GetException ( WriteResult . TimeoutBeforeWrite , message , ServerEndPoint ) ;
859+ #if DEBUG // additional tracking
844860 ex . Data [ "Redis-BacklogStartDelay" ] = msToStartWorker ;
845861 ex . Data [ "Redis-BacklogGetLockDelay" ] = msToGetLock ;
846862 if ( failureCount != 0 ) ex . Data [ "Redis-BacklogFailCount" ] = failureCount ;
847863 if ( _maxWriteTime >= 0 ) ex . Data [ "Redis-MaxWrite" ] = _maxWriteTime . ToString ( ) + "ms, " + _maxWriteCommand . ToString ( ) ;
848864 var maxFlush = physical ? . MaxFlushTime ?? - 1 ;
849865 if ( maxFlush >= 0 ) ex . Data [ "Redis-MaxFlush" ] = maxFlush . ToString ( ) + "ms, " + ( physical ? . MaxFlushBytes ?? - 1 ) . ToString ( ) ;
850866 if ( _maxLockDuration >= 0 ) ex . Data [ "Redis-MaxLockDuration" ] = _maxLockDuration ;
851-
867+ #endif
852868 message . SetExceptionAndComplete ( ex , this ) ;
853869 }
854870 else
@@ -974,26 +990,33 @@ internal ValueTask<WriteResult> WriteMessageTakingWriteLockAsync(PhysicalConnect
974990 {
975991 if ( releaseLock & token . Success )
976992 {
993+ #if DEBUG
977994 RecordLockDuration ( lockTaken ) ;
995+ #endif
978996 token . Dispose ( ) ;
979997 }
980998 }
981999 }
1000+ #if DEBUG
9821001 private void RecordLockDuration ( int lockTaken )
9831002 {
1003+
9841004 var lockDuration = unchecked ( Environment . TickCount - lockTaken ) ;
9851005 if ( lockDuration > _maxLockDuration ) _maxLockDuration = lockDuration ;
9861006 }
9871007 volatile int _maxLockDuration = - 1 ;
1008+ #endif
9881009
989- private async ValueTask < WriteResult > WriteMessageTakingWriteLockAsync_Awaited ( ValueTask < LockToken > pending , PhysicalConnection physical , Message message )
1010+ private async ValueTask < WriteResult > WriteMessageTakingWriteLockAsync_Awaited ( ValueTask < LockToken > pending , PhysicalConnection physical , Message message )
9901011 {
9911012 try
9921013 {
9931014 using ( var token = await pending . ForAwait ( ) )
9941015 {
9951016 if ( ! token . Success ) return TimedOutBeforeWrite ( message ) ;
1017+ #if DEBUG
9961018 int lockTaken = Environment . TickCount ;
1019+ #endif
9971020 var result = WriteMessageInsideLock ( physical , message ) ;
9981021
9991022 if ( result == WriteResult . Success )
@@ -1004,7 +1027,9 @@ private async ValueTask<WriteResult> WriteMessageTakingWriteLockAsync_Awaited(Va
10041027 UnmarkActiveMessage ( message ) ;
10051028 physical . SetIdle ( ) ;
10061029
1030+ #if DEBUG
10071031 RecordLockDuration ( lockTaken ) ;
1032+ #endif
10081033 return result ;
10091034 }
10101035 }
@@ -1026,7 +1051,9 @@ private async ValueTask<WriteResult> CompleteWriteAndReleaseLockAsync(LockToken
10261051 return result ;
10271052 }
10281053 catch ( Exception ex ) { return HandleWriteException ( message , ex ) ; }
1054+ #if DEBUG
10291055 finally { RecordLockDuration ( lockTaken ) ; }
1056+ #endif
10301057 }
10311058 }
10321059
0 commit comments