tally.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Gathering run-time statistics
  3. */
  4. /* $Id$ */
  5. #include <stdio.h>
  6. #include "global.h"
  7. #include "linfil.h"
  8. #include "alloc.h"
  9. struct line_tally { /* one for each line */
  10. long lt_cnt; /* counts entrances */
  11. long lt_instr; /* counts instructions */
  12. };
  13. struct file_tally { /* one for each file */
  14. struct file_tally *next;
  15. ptr ft_fil; /* file name */
  16. long ft_limit; /* size of line array */
  17. struct line_tally *ft_line; /* pointer to line array */
  18. };
  19. PRIVATE struct file_tally *first_tally; /* start of chain */
  20. PRIVATE struct file_tally *file; /* present file */
  21. PRIVATE long lastLIN;
  22. PRIVATE tally_newFIL();
  23. PRIVATE enlarge();
  24. tally()
  25. {
  26. if (!FIL)
  27. return;
  28. if (!file || FIL != file->ft_fil) {
  29. tally_newFIL(FIL);
  30. file->ft_fil = FIL;
  31. lastLIN = -1;
  32. }
  33. if (LIN != lastLIN) {
  34. if (LIN >= file->ft_limit) {
  35. enlarge(file, LIN);
  36. }
  37. file->ft_line[LIN].lt_cnt++;
  38. lastLIN = LIN;
  39. }
  40. file->ft_line[LIN].lt_instr++;
  41. }
  42. PRIVATE tally_newFIL(f)
  43. ptr f;
  44. {
  45. struct file_tally **hook = &first_tally;
  46. while (*hook) {
  47. if ((*hook)->ft_fil == f)
  48. break;
  49. hook = &(*hook)->next;
  50. }
  51. if (!*hook) {
  52. /* first time we see this file */
  53. /* construct a new entry */
  54. struct file_tally *nt = (struct file_tally *)
  55. Malloc((size) sizeof (struct file_tally), "file_tally");
  56. nt->next = (struct file_tally *)0;
  57. nt->ft_fil = f;
  58. nt->ft_limit = 1; /* provisional length */
  59. nt->ft_line = (struct line_tally *)
  60. Malloc((size) sizeof (struct line_tally),
  61. "struct line_tally");
  62. nt->ft_line[0].lt_cnt = 0;
  63. nt->ft_line[0].lt_instr = 0;
  64. /* and hook it in */
  65. *hook = nt;
  66. }
  67. file = *hook;
  68. }
  69. PRIVATE enlarge(ft, l)
  70. struct file_tally *ft;
  71. long l;
  72. {
  73. long limit = allocfrac(l < 100 ? 100 : l);
  74. if (limit <= ft->ft_limit)
  75. return;
  76. ft->ft_line = (struct line_tally *)
  77. Realloc((char *)ft->ft_line,
  78. (size)(limit*sizeof (struct line_tally)),
  79. "array line_tally");
  80. while (ft->ft_limit < limit) {
  81. ft->ft_line[ft->ft_limit].lt_cnt = 0;
  82. ft->ft_line[ft->ft_limit].lt_instr = 0;
  83. ft->ft_limit++;
  84. }
  85. }
  86. PRIVATE FILE *tally_fp;
  87. out_tally()
  88. {
  89. struct file_tally **hook = &first_tally;
  90. if (!*hook)
  91. return;
  92. tally_fp = fopen("int.tally", "w");
  93. if (!tally_fp)
  94. return;
  95. while (*hook) {
  96. struct file_tally *ft = *hook;
  97. register long i;
  98. fprintf(tally_fp, "%s:\n", dt_fname(ft->ft_fil));
  99. for (i = 0; i < ft->ft_limit; i++) {
  100. struct line_tally *lt = &ft->ft_line[i];
  101. if (lt->lt_cnt) {
  102. /* we visited this line */
  103. fprintf(tally_fp, "\t%ld\t%ld\t%ld\n",
  104. i, lt->lt_cnt, lt->lt_instr);
  105. }
  106. }
  107. fprintf(tally_fp, "\n");
  108. hook = &(*hook)->next;
  109. }
  110. fclose(tally_fp);
  111. tally_fp = 0;
  112. }