tprof.c 2.7 KB

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