How can I track task queues in .NET TaskSchedulers (via AppDomain)

As a developer, I would like to keep track of the size (and progress) of work in task queues in TaskSchedulers so that I can assess whether the experimental slowdown is due to the workload due to the size or, possibly, stopping the tasks scheduled.

Usually I just add a debugger and check the size of the tasks, but:

  • The application works under mono and during production, so I can not connect to it using visual studio
  • I would like to present the results of the analysis of this data as input to monitor the health of the service

I went through the docs and found

  • TaskScheduler.GetScheduledTasks , which passes this information to the debugger. It is protected (which I could get around), but it also seems to require some guarantees regarding frozen threads, which I cannot respect. However, I am ready to use inconsistent data.
  • how to get a list of running tasks in .net 4.0 . Which focuses on running tasks that are not interesting to me. I am interested in the size of the backlog and whether the work is going on.

I'm ready:

  • Use code intended for other purposes (for example, for a debugger)
  • Accept that I get inconsistent data (this is for statistics and analysis).

The things I don't want to do are the following:

  • Add tracking for each task created.
    • Some of the codes are third-party and cannot be reorganized.
    • , , , .
  • TaskScheduler
    • TaskSchedulers, max- concurrency. .
+4
2

, TaskScheduler.GetScheduledTasks , .

MSDN ():

, , , GetScheduledTasksForDebugger: internal Task[] GetScheduledTasksForDebugger(). - . , internal static TaskScheduler[] GetTaskSchedulersForDebugger(). TaskScheduler. GetScheduledTasksForDebugger .

? , , GetScheduledTasksForDebugger , , , .

+1

GetScheduledTasksForDebugger:

public static class ScheduledTaskAccess
{
    public static Task[] GetScheduledTasksForDebugger(this TaskScheduler ts)
    {
        var mi = ts.GetType().GetMethod("GetScheduledTasksForDebugger", BindingFlags.NonPublic | BindingFlags.Instance);
        if (mi == null)
            return null;
        return (Task[])mi.Invoke(ts, new object[0]);
    }
    public static TaskScheduler[] GetTaskSchedulersForDebugger()
    {
        var mi = typeof(TaskScheduler).GetMethod("GetTaskSchedulersForDebugger", BindingFlags.NonPublic | BindingFlags.Static);
        if (mi == null)
            return null;
        return (TaskScheduler[])mi.Invoke(null, new object[0]);
    }
}

IEqualityComparer, , . :)

, , , CPU-time, , task-switchcount,...

+1

All Articles