Skip to content
Snippets Groups Projects
Commit fdf45ab6 authored by Jan Tattermusch's avatar Jan Tattermusch
Browse files

support threadpool tracing

parent aaefa958
No related branches found
No related tags found
No related merge requests found
...@@ -37,6 +37,7 @@ using System.Linq; ...@@ -37,6 +37,7 @@ using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Grpc.Core.Logging; using Grpc.Core.Logging;
using Grpc.Core.Profiling;
using Grpc.Core.Utils; using Grpc.Core.Utils;
namespace Grpc.Core.Internal namespace Grpc.Core.Internal
...@@ -54,6 +55,8 @@ namespace Grpc.Core.Internal ...@@ -54,6 +55,8 @@ namespace Grpc.Core.Internal
readonly int poolSize; readonly int poolSize;
readonly int completionQueueCount; readonly int completionQueueCount;
readonly List<BasicProfiler> threadProfilers = new List<BasicProfiler>(); // profilers assigned to threadpool threads
bool stopRequested; bool stopRequested;
IReadOnlyCollection<CompletionQueueSafeHandle> completionQueues; IReadOnlyCollection<CompletionQueueSafeHandle> completionQueues;
...@@ -82,7 +85,8 @@ namespace Grpc.Core.Internal ...@@ -82,7 +85,8 @@ namespace Grpc.Core.Internal
for (int i = 0; i < poolSize; i++) for (int i = 0; i < poolSize; i++)
{ {
threads.Add(CreateAndStartThread(i)); var optionalProfiler = i < threadProfilers.Count ? threadProfilers[i] : null;
threads.Add(CreateAndStartThread(i, optionalProfiler));
} }
} }
} }
...@@ -111,6 +115,11 @@ namespace Grpc.Core.Internal ...@@ -111,6 +115,11 @@ namespace Grpc.Core.Internal
{ {
cq.Dispose(); cq.Dispose();
} }
for (int i = 0; i < threadProfilers.Count; i++)
{
threadProfilers[i].Dump(string.Format("grpc_trace_thread_{0}.txt", i));
}
}); });
} }
...@@ -137,12 +146,12 @@ namespace Grpc.Core.Internal ...@@ -137,12 +146,12 @@ namespace Grpc.Core.Internal
} }
} }
private Thread CreateAndStartThread(int threadIndex) private Thread CreateAndStartThread(int threadIndex, IProfiler optionalProfiler)
{ {
var cqIndex = threadIndex % completionQueues.Count; var cqIndex = threadIndex % completionQueues.Count;
var cq = completionQueues.ElementAt(cqIndex); var cq = completionQueues.ElementAt(cqIndex);
var thread = new Thread(new ThreadStart(() => RunHandlerLoop(cq))); var thread = new Thread(new ThreadStart(() => RunHandlerLoop(cq, optionalProfiler)));
thread.IsBackground = true; thread.IsBackground = true;
thread.Name = string.Format("grpc {0} (cq {1})", threadIndex, cqIndex); thread.Name = string.Format("grpc {0} (cq {1})", threadIndex, cqIndex);
thread.Start(); thread.Start();
...@@ -153,8 +162,13 @@ namespace Grpc.Core.Internal ...@@ -153,8 +162,13 @@ namespace Grpc.Core.Internal
/// <summary> /// <summary>
/// Body of the polling thread. /// Body of the polling thread.
/// </summary> /// </summary>
private void RunHandlerLoop(CompletionQueueSafeHandle cq) private void RunHandlerLoop(CompletionQueueSafeHandle cq, IProfiler optionalProfiler)
{ {
if (optionalProfiler != null)
{
Profilers.SetForCurrentThread(optionalProfiler);
}
CompletionQueueEvent ev; CompletionQueueEvent ev;
do do
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment