tprof.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <mach.h>
  13. #define PCRES 8
  14. struct COUNTER
  15. {
  16. char *name; /* function name */
  17. int32_t time; /* ticks spent there */
  18. };
  19. void
  20. error(int perr, char *s)
  21. {
  22. fprint(2, "tprof: %s", s);
  23. if(perr){
  24. fprint(2, ": ");
  25. perror(0);
  26. }else
  27. fprint(2, "\n");
  28. exits(s);
  29. }
  30. int
  31. compar(const void *va, const void *vb)
  32. {
  33. struct COUNTER *a, *b;
  34. a = va;
  35. b = vb;
  36. if(a->time < b->time)
  37. return -1;
  38. if(a->time == b->time)
  39. return 0;
  40. return 1;
  41. }
  42. void
  43. main(int argc, char *argv[])
  44. {
  45. int fd;
  46. int32_t i, j, k, n;
  47. Dir *d;
  48. char *name;
  49. uint32_t *data;
  50. uint32_t tbase, sum;
  51. int32_t delta;
  52. Symbol s;
  53. Biobuf outbuf;
  54. Fhdr f;
  55. struct COUNTER *cp;
  56. char filebuf[128], *file;
  57. if(argc != 2 && argc != 3)
  58. error(0, "usage: tprof pid [binary]");
  59. /*
  60. * Read symbol table
  61. */
  62. if(argc == 2){
  63. file = filebuf;
  64. snprint(filebuf, sizeof filebuf, "/proc/%s/text", argv[1]);
  65. }else
  66. file = argv[2];
  67. fd = open(file, OREAD);
  68. if(fd < 0)
  69. error(1, file);
  70. if (!crackhdr(fd, &f))
  71. error(1, "read text header");
  72. if (f.type == FNONE)
  73. error(0, "text file not an a.out");
  74. machbytype(f.type);
  75. if (syminit(fd, &f) < 0)
  76. error(1, "syminit");
  77. close(fd);
  78. /*
  79. * Read timing data
  80. */
  81. file = smprint("/proc/%s/profile", argv[1]);
  82. fd = open(file, OREAD);
  83. if(fd < 0)
  84. error(1, file);
  85. free(file);
  86. d = dirfstat(fd);
  87. if(d == nil)
  88. error(1, "stat");
  89. n = d->length/sizeof(data[0]);
  90. if(n < 2)
  91. error(0, "data file too short");
  92. data = malloc(d->length);
  93. if(data == 0)
  94. error(1, "malloc");
  95. if(read(fd, data, d->length) < 0)
  96. error(1, "text read");
  97. close(fd);
  98. for(i=0; i<n; i++)
  99. data[i] = machdata->swal(data[i]);
  100. delta = data[0]-data[1];
  101. print("total: %ld\n", data[0]);
  102. if(data[0] == 0)
  103. exits(0);
  104. if (!textsym(&s, 0))
  105. error(0, "no text symbols");
  106. tbase = s.value & ~(mach->pgsize-1); /* align down to page */
  107. print("TEXT %.8lux\n", tbase);
  108. /*
  109. * Accumulate counts for each function
  110. */
  111. cp = 0;
  112. k = 0;
  113. for (i = 0, j = (s.value-tbase)/PCRES+2; j < n; i++) {
  114. name = s.name; /* save name */
  115. if (!textsym(&s, i)) /* get next symbol */
  116. break;
  117. sum = 0;
  118. while (j < n && j*PCRES < s.value-tbase)
  119. sum += data[j++];
  120. if (sum) {
  121. cp = realloc(cp, (k+1)*sizeof(struct COUNTER));
  122. if (cp == 0)
  123. error(1, "realloc");
  124. cp[k].name = name;
  125. cp[k].time = sum;
  126. k++;
  127. }
  128. }
  129. if (!k)
  130. error(0, "no counts");
  131. cp[k].time = 0; /* "etext" can take no time */
  132. /*
  133. * Sort by time and print
  134. */
  135. qsort(cp, k, sizeof(struct COUNTER), compar);
  136. Binit(&outbuf, 1, OWRITE);
  137. Bprint(&outbuf, " ms %% sym\n");
  138. while(--k>=0)
  139. Bprint(&outbuf, "%6ld\t%3lld.%lld\t%s\n",
  140. cp[k].time,
  141. 100LL*cp[k].time/delta,
  142. (1000LL*cp[k].time/delta)%10,
  143. cp[k].name);
  144. exits(0);
  145. }