A microsecond-level performance profiling library for gint.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

libprof.c 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <gint/timer.h>
  2. #include <gint/clock.h>
  3. #include <gint/mpu/tmu.h>
  4. #include <gint/std/stdlib.h>
  5. #include <gint/hardware.h>
  6. #include <libprof.h>
  7. /* Recursion depth of each function currently being executed */
  8. uint8_t *prof_rec = NULL;
  9. /* Time that has elapsed within each function; the value for a given function
  10. is only relevant when it is not executing, due to optimizations */
  11. uint32_t *prof_elapsed = NULL;
  12. /* Timer counter */
  13. uint32_t volatile *prof_tcnt = NULL;
  14. /* Timer ID */
  15. static int prof_timer;
  16. /* prof_init(): Initialize the profiler's data and timer */
  17. int prof_init(int n, int timer)
  18. {
  19. if((unsigned)timer >= 3) return 1;
  20. prof_rec = malloc(n * sizeof *prof_rec);
  21. prof_elapsed = malloc(n * sizeof *prof_elapsed);
  22. int status = timer_setup(timer, 0xffffffff, timer_Po_4, NULL, NULL);
  23. if(!prof_rec || !prof_elapsed || status < 0)
  24. {
  25. prof_quit();
  26. return 1;
  27. }
  28. /* Fix the configuration done by gint by disabling the interrupt */
  29. if(isSH3())
  30. {
  31. SH7705_TMU.TMU[timer].TCR.UNIE = 0;
  32. prof_tcnt = &SH7705_TMU.TMU[timer].TCNT;
  33. }
  34. else
  35. {
  36. SH7305_TMU.TMU[timer].TCR.UNIE = 0;
  37. prof_tcnt = &SH7305_TMU.TMU[timer].TCNT;
  38. }
  39. timer_start(timer);
  40. prof_timer = timer;
  41. return 0;
  42. }
  43. /* prof_quit(): Free the profiler's data and timer */
  44. void prof_quit(void)
  45. {
  46. timer_stop(prof_timer);
  47. free(prof_rec);
  48. free(prof_time);
  49. }
  50. //---
  51. // Post-measurement analysis
  52. //---
  53. /* prof_time(): Time spent in a given context, in microseconds */
  54. uint32_t prof_time(int ctx)
  55. {
  56. int Pphi = clock_freq()->Pphi_f;
  57. uint64_t time = prof_elapsed[ctx];
  58. return (time * 4 * 1000000) / Pphi;
  59. }