tprof.c 2.5 KB

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