kprof.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. ulong tbase, sum;
  42. long delta;
  43. Symbol s;
  44. Biobuf outbuf;
  45. Fhdr f;
  46. Dir *d;
  47. struct COUNTER *cp;
  48. if(argc != 3)
  49. error(0, "usage: kprof text data");
  50. /*
  51. * Read symbol table
  52. */
  53. fd = open(argv[1], OREAD);
  54. if(fd < 0)
  55. error(1, argv[1]);
  56. if (!crackhdr(fd, &f))
  57. error(1, "read text header");
  58. if (f.type == FNONE)
  59. error(0, "text file not an a.out");
  60. if (syminit(fd, &f) < 0)
  61. error(1, "syminit");
  62. close(fd);
  63. /*
  64. * Read timing data
  65. */
  66. fd = open(argv[2], OREAD);
  67. if(fd < 0)
  68. error(1, argv[2]);
  69. d = dirfstat(fd);
  70. if(d == nil)
  71. error(1, "stat");
  72. n = d->length/sizeof(data[0]);
  73. if(n < 2)
  74. error(0, "data file too short");
  75. data = malloc(d->length);
  76. if(data == 0)
  77. error(1, "malloc");
  78. if(read(fd, data, d->length) < 0)
  79. error(1, "text read");
  80. close(fd);
  81. for(i=0; i<n; i++)
  82. data[i] = beswal(data[i]);
  83. delta = data[0]-data[1];
  84. print("total: %ld in kernel text: %ld outside kernel text: %ld\n",
  85. data[0], delta, data[1]);
  86. if(data[0] == 0)
  87. exits(0);
  88. if (!textsym(&s, 0))
  89. error(0, "no text symbols");
  90. tbase = s.value & ~(mach->pgsize-1); /* align down to page */
  91. print("KTZERO %.8lux\n", tbase);
  92. /*
  93. * Accumulate counts for each function
  94. */
  95. cp = 0;
  96. k = 0;
  97. for (i = 0, j = (s.value-tbase)/PCRES+2; j < n; i++) {
  98. name = s.name; /* save name */
  99. if (!textsym(&s, i)) /* get next symbol */
  100. break;
  101. sum = 0;
  102. while (j < n && j*PCRES < s.value-tbase)
  103. sum += data[j++];
  104. if (sum) {
  105. cp = realloc(cp, (k+1)*sizeof(struct COUNTER));
  106. if (cp == 0)
  107. error(1, "realloc");
  108. cp[k].name = name;
  109. cp[k].time = sum;
  110. k++;
  111. }
  112. }
  113. if (!k)
  114. error(0, "no counts");
  115. cp[k].time = 0; /* "etext" can take no time */
  116. /*
  117. * Sort by time and print
  118. */
  119. qsort(cp, k, sizeof(struct COUNTER), compar);
  120. Binit(&outbuf, 1, OWRITE);
  121. Bprint(&outbuf, "ms %% sym\n");
  122. while(--k>=0)
  123. Bprint(&outbuf, "%ld\t%3ld.%ld\t%s\n",
  124. cp[k].time,
  125. 100*cp[k].time/delta,
  126. (1000*cp[k].time/delta)%10,
  127. cp[k].name);
  128. exits(0);
  129. }