HierarchicalProfiler0.Mod 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. MODULE HierarchicalProfiler0; (** AUTHOR "staubesv"; PURPOSE "Platform-specific part of the hierarchical profiler"; *)
  2. IMPORT
  3. SYSTEM, Machine, Objects, Modules, Kernel;
  4. CONST
  5. HandlerNotInstalled = 0;
  6. HandlerInstalled = 1;
  7. (* Time to wait when unloading the module before it is actually unloaded. This avoids that a pending interrupt
  8. calls 'HandleTimer' when the module is already unloaded. *)
  9. WaitTime = 100; (* ms *)
  10. TYPE
  11. Callback = PROCEDURE (id : LONGINT; process : Objects.Process; pc, bp, lowAdr, highAdr : ADDRESS);
  12. VAR
  13. callback : Callback;
  14. state : LONGINT;
  15. (* First level interrupt handler. Called 1000 times per second by each processor *)
  16. PROCEDURE HandleTimer(id: LONGINT; CONST state: Machine.State);
  17. VAR process : Objects.Process;
  18. BEGIN
  19. process := Objects.running[id];
  20. callback(id, process, state.PC, state.BP, state.SP, LONGINT(0FFFFFFFFH));
  21. END HandleTimer;
  22. (** Start profiling. If the profiler is already running, it is stopped and the sample data is discarded before re-starting it *)
  23. PROCEDURE Enable*(proc : Callback);
  24. BEGIN {EXCLUSIVE}
  25. ASSERT(proc # NIL);
  26. ASSERT(state = HandlerNotInstalled);
  27. Machine.InstallEventHandler(HandleTimer);
  28. state := HandlerInstalled;
  29. callback := proc;
  30. END Enable;
  31. (** Stop profiling. The profile data is not discarded. It can be retrieved using the procedure 'GetProfile' *)
  32. PROCEDURE Disable*;
  33. VAR timer : Kernel.Timer;
  34. BEGIN {EXCLUSIVE}
  35. ASSERT(state = HandlerInstalled);
  36. Machine.InstallEventHandler(NIL);
  37. NEW(timer); timer.Sleep(WaitTime);
  38. END Disable;
  39. PROCEDURE Cleanup;
  40. BEGIN
  41. Machine.InstallEventHandler(NIL);
  42. END Cleanup;
  43. BEGIN
  44. callback := NIL;
  45. state := HandlerNotInstalled;
  46. Modules.InstallTermHandler(Cleanup);
  47. END HierarchicalProfiler0.