How to pause & resume profiling when using SelfAttach.Attach API

Using the following console program, I expected to see no mention of M1 or M4 in the profiling results (becase profiling should have been paused when those methods were called). However, M1, M2, M3, M4 and M4 were all shown as taking about 500ms to execute.

Is it possible to pause the profiler when using SelfAttach.Attach? If it's not possible, how do I turn this into a feature request?

The reason I'm trying to do this, is because I've deteced that my UI thread is blocked for too long so I want to turn on the profiler to recored what's happening. When the UI threads is unblocked again, I want to pause profiling until next time I detect that it's blocked.

var config = new OpenProfilingConfig()
{
     ProfilingControlKind = ProfilingControlKind.API,
     RedistPath = @"C:\Program Files (x86)\JetBrains\dotTrace\Profiler SDK v5\Redist"
};
SelfAttach.Attach(config);
ProfilingControl.StartPaused();
               
// Give profiler a chance to setup
Console.WriteLine("Press Enter when the profiler is ready");
Console.ReadLine();

M1();

ProfilingControl.Resume();

M2();
M3();

ProfilingControl.Pause();

M4();

ProfilingControl.Resume();

M5();

ProfilingControl.Stop();
ProfilingControl.Detach();

Console.WriteLine("Press Enter to exit");
Console.ReadLine();

1 comment

Wilka, I'm copying my email here - in case if anyone else faces the same problem.
Please install new version of SDK (http://download.jetbrains.com/dottrace/dotTraceProfilingSDK.msi); here's an example of how it can be used now:

using System;
using System.Threading;
using JetBrains.dotTrace.Api;

namespace ApiControl1
{
  class Program
  {
    static void M1() { Thread.Sleep(510); }
    static void M2() { Thread.Sleep(520); }
    static void M3() { Thread.Sleep(530); }
    static void M4() { Thread.Sleep(540); }
    static void M5() { Thread.Sleep(550); }

    static void Main(string[] args)
    {
      var config = new OpenProfilingConfig()
      {
        ProfilingControlKind = ProfilingControlKind.API,
        RedistPath = @"C:\Program Files
(x86)\JetBrains\dotTrace\Profiler SDK v5\Redist",
      };

      SelfAttach.Attach(config);

      // this is the missing part - await until profiler will get activated
      int cnt = 0;
      while (!ProfilingControl.IsActive && cnt++ < 20)
      {
        Thread.Sleep(500);
      }
      if (cnt >= 20)
        throw new Exception("Failed to attach the profiler");

      ProfilingControl.StartPaused();

      // Give profiler a chance to setup
      Console.WriteLine("Press Enter when the profiler is ready");
      Console.ReadLine();

      M1();

      ProfilingControl.Resume();

      M2();
      M3();

      ProfilingControl.Pause();

      M4();

      ProfilingControl.Resume();

      M5();

      ProfilingControl.Stop();
      ProfilingControl.Detach();

      Console.WriteLine("Press Enter to exit");
      Console.ReadLine();
    }
  }
}


And that's how the snapshot will look like:
dtapi_pauseresume_results.png

0

Please sign in to leave a comment.