kprof.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <mach.h>
  5. #define PCRES 8
  6. struct COUNTER
  7. {
  8. char *name; /* function name */
  9. long time; /* ticks spent there */
  10. };
  11. void
  12. error(int perr, char *s)
  13. {
  14. fprint(2, "kprof: %s", s);
  15. if(perr){
  16. fprint(2, ": ");
  17. perror(0);
  18. }else
  19. fprint(2, "\n");
  20. exits(s);
  21. }
  22. int
  23. compar(void *va, void *vb)
  24. {
  25. struct COUNTER *a, *b;
  26. a = va;
  27. b = vb;
  28. if(a->time < b->time)
  29. return -1;
  30. if(a->time == b->time)
  31. return 0;
  32. return 1;
  33. }
  34. void
  35. main(int argc, char *argv[])
  36. {
  37. int fd;
  38. long i, j, k, n;
  39. char *name;
  40. ulong *data;
  41. vlong tbase;
  42. ulong sum;
  43. long delta;
  44. Symbol s;
  45. Biobuf outbuf;
  46. Fhdr f;
  47. Dir *d;
  48. struct COUNTER *cp;
  49. if(argc != 3)
  50. error(0, "usage: kprof text data");
  51. /*
  52. * Read symbol table
  53. */
  54. fd = open(argv[1], OREAD);
  55. if(fd < 0)
  56. error(1, argv[1]);
  57. if (!crackhdr(fd, &f))
  58. error(1, "read text header");
  59. if (f.type == FNONE)
  60. error(0, "text file not an a.out");
  61. if (syminit(fd, &f) < 0)
  62. error(1, "syminit");
  63. close(fd);
  64. /*
  65. * Read timing data
  66. */
  67. fd = open(argv[2], OREAD);
  68. if(fd < 0)
  69. error(1, argv[2]);
  70. d = dirfstat(fd);
  71. if(d == nil)
  72. error(1, "stat");
  73. n = d->length/sizeof(data[0]);
  74. if(n < 2)
  75. error(0, "data file too short");
  76. data = malloc(d->length);
  77. if(data == 0)
  78. error(1, "malloc");
  79. if(read(fd, data, d->length) < 0)
  80. error(1, "text read");
  81. close(fd);
  82. for(i=0; i<n; i++)
  83. data[i] = beswal(data[i]);
  84. delta = data[0]-data[1];
  85. print("total: %ld in kernel text: %ld outside kernel text: %ld\n",
  86. data[0], delta, data[1]);
  87. if(data[0] == 0)
  88. exits(0);
  89. if (!textsym(&s, 0))
  90. error(0, "no text symbols");
  91. tbase = s.value & ~(mach->pgsize-1); /* align down to page */
  92. print("KTZERO %.8llux\n", tbase);
  93. /*
  94. * Accumulate counts for each function
  95. */
  96. cp = 0;
  97. k = 0;
  98. for (i = 0, j = (s.value-tbase)/PCRES+2; j < n; i++) {
  99. name = s.name; /* save name */
  100. if (!textsym(&s, i)) /* get next symbol */
  101. break;
  102. sum = 0;
  103. while (j < n && j*PCRES < s.value-tbase)
  104. sum += data[j++];
  105. if (sum) {
  106. cp = realloc(cp, (k+1)*sizeof(struct COUNTER));
  107. if (cp == 0)
  108. error(1, "realloc");
  109. cp[k].name = name;
  110. cp[k].time = sum;
  111. k++;
  112. }
  113. }
  114. if (!k)
  115. error(0, "no counts");
  116. cp[k].time = 0; /* "etext" can take no time */
  117. /*
  118. * Sort by time and print
  119. */
  120. qsort(cp, k, sizeof(struct COUNTER), compar);
  121. Binit(&outbuf, 1, OWRITE);
  122. Bprint(&outbuf, "ms %% sym\n");
  123. while(--k>=0)
  124. Bprint(&outbuf, "%ld\t%3ld.%ld\t%s\n",
  125. cp[k].time,
  126. 100*cp[k].time/delta,
  127. (1000*cp[k].time/delta)%10,
  128. cp[k].name);
  129. exits(0);
  130. }