kprof.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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, "kprof: %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. char *name;
  48. uint32_t *data;
  49. int64_t tbase;
  50. uint32_t sum;
  51. int32_t delta;
  52. Symbol s;
  53. Biobuf outbuf;
  54. Fhdr f;
  55. Dir *d;
  56. struct COUNTER *cp;
  57. if(argc != 3)
  58. error(0, "usage: kprof text data");
  59. /*
  60. * Read symbol table
  61. */
  62. fd = open(argv[1], OREAD);
  63. if(fd < 0)
  64. error(1, argv[1]);
  65. if (!crackhdr(fd, &f))
  66. error(1, "read text header");
  67. if (f.type == FNONE)
  68. error(0, "text file not an a.out");
  69. if (syminit(fd, &f) < 0)
  70. error(1, "syminit");
  71. close(fd);
  72. /*
  73. * Read timing data
  74. */
  75. fd = open(argv[2], OREAD);
  76. if(fd < 0)
  77. error(1, argv[2]);
  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] = beswal(data[i]);
  92. delta = data[0]-data[1];
  93. print("total: %ld in kernel text: %ld outside kernel text: %ld\n",
  94. data[0], delta, data[1]);
  95. if(data[0] == 0)
  96. exits(0);
  97. if (!textsym(&s, 0))
  98. error(0, "no text symbols");
  99. tbase = mach->kbase;
  100. if(tbase != s.value & ~0xFFF)
  101. print("warning: kbase %.8llux != tbase %.8llux\n",
  102. tbase, s.value&~0xFFF);
  103. print("KTZERO %.8llux PGSIZE %dKb\n", tbase, mach->pgsize/1024);
  104. /*
  105. * Accumulate counts for each function
  106. */
  107. cp = 0;
  108. k = 0;
  109. for (i = 0, j = 2; j < n; i++) {
  110. name = s.name; /* save name */
  111. if (!textsym(&s, i)) /* get next symbol */
  112. break;
  113. s.value -= tbase;
  114. s.value /= PCRES;
  115. sum = 0;
  116. while (j < n && j < s.value)
  117. sum += data[j++];
  118. if (sum) {
  119. cp = realloc(cp, (k+1)*sizeof(struct COUNTER));
  120. if (cp == 0)
  121. error(1, "realloc");
  122. cp[k].name = name;
  123. cp[k].time = sum;
  124. k++;
  125. }
  126. }
  127. if (!k)
  128. error(0, "no counts");
  129. cp[k].time = 0; /* "etext" can take no time */
  130. /*
  131. * Sort by time and print
  132. */
  133. qsort(cp, k, sizeof(struct COUNTER), compar);
  134. Binit(&outbuf, 1, OWRITE);
  135. Bprint(&outbuf, "ms %% sym\n");
  136. while(--k>=0)
  137. Bprint(&outbuf, "%ld\t%3lld.%lld\t%s\n",
  138. cp[k].time,
  139. 100LL*cp[k].time/delta,
  140. (1000LL*cp[k].time/delta)%10,
  141. cp[k].name);
  142. exits(0);
  143. }