tprof.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. file = smprint("/proc/%s/profile", argv[1]);
  74. fd = open(file, OREAD);
  75. if(fd < 0)
  76. error(1, file);
  77. free(file);
  78. d = dirfstat(fd);
  79. if(d == nil)
  80. error(1, "stat");
  81. n = d->length/sizeof(data[0]);
  82. if(n < 2)
  83. error(0, "data file too short");
  84. data = malloc(d->length);
  85. if(data == 0)
  86. error(1, "malloc");
  87. if(read(fd, data, d->length) < 0)
  88. error(1, "text read");
  89. close(fd);
  90. for(i=0; i<n; i++)
  91. data[i] = machdata->swal(data[i]);
  92. delta = data[0]-data[1];
  93. print("total: %ld\n", data[0]);
  94. if(data[0] == 0)
  95. exits(0);
  96. if (!textsym(&s, 0))
  97. error(0, "no text symbols");
  98. tbase = s.value & ~(mach->pgsize-1); /* align down to page */
  99. print("TEXT %.8lux\n", tbase);
  100. /*
  101. * Accumulate counts for each function
  102. */
  103. cp = 0;
  104. k = 0;
  105. for (i = 0, j = (s.value-tbase)/PCRES+2; j < n; i++) {
  106. name = s.name; /* save name */
  107. if (!textsym(&s, i)) /* get next symbol */
  108. break;
  109. sum = 0;
  110. while (j < n && j*PCRES < s.value-tbase)
  111. sum += data[j++];
  112. if (sum) {
  113. cp = realloc(cp, (k+1)*sizeof(struct COUNTER));
  114. if (cp == 0)
  115. error(1, "realloc");
  116. cp[k].name = name;
  117. cp[k].time = sum;
  118. k++;
  119. }
  120. }
  121. if (!k)
  122. error(0, "no counts");
  123. cp[k].time = 0; /* "etext" can take no time */
  124. /*
  125. * Sort by time and print
  126. */
  127. qsort(cp, k, sizeof(struct COUNTER), compar);
  128. Binit(&outbuf, 1, OWRITE);
  129. Bprint(&outbuf, " ms %% sym\n");
  130. while(--k>=0)
  131. Bprint(&outbuf, "%6ld\t%3ld.%ld\t%s\n",
  132. cp[k].time,
  133. 100*cp[k].time/delta,
  134. (1000*cp[k].time/delta)%10,
  135. cp[k].name);
  136. exits(0);
  137. }