You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 rivejä
3.3 KiB

6 kuukautta sitten
  1. #!/usr/bin/perl
  2. #!/usr/local/bin/perl
  3. use Getopt::Std;
  4. #######################################################################
  5. # checktrace - trace file consistency checker and balancer.
  6. #
  7. # Copyright (c) 2002, R. Bryant and D. O'Hallaron, All rights reserved.
  8. # May not be used, modified, or copied without permission.
  9. #
  10. # This script reads a Malloc Lab trace file, checks it for consistency,
  11. # and outputs a balanced version by appending any necessary free requests.
  12. #
  13. #######################################################################
  14. $| = 1; # autoflush output on every print statement
  15. #
  16. # void usage(void) - print help message and terminate
  17. #
  18. sub usage
  19. {
  20. printf STDERR "$_[0]\n";
  21. printf STDERR "Usage: $0 [-hs]\n";
  22. printf STDERR "Options:\n";
  23. printf STDERR " -h Print this message\n";
  24. printf STDERR " -s Emit only a brief summary\n";
  25. die "\n" ;
  26. }
  27. ##############
  28. # Main routine
  29. ##############
  30. #
  31. # Parse and check the command line arguments
  32. #
  33. getopts('hs');
  34. if ($opt_h) {
  35. usage("");
  36. }
  37. $summary = $opt_s;
  38. #
  39. # HASH keeps a running tally of outstanding alloc/realloc
  40. # requests. When a free request is encountered, the corresponding
  41. # hash entry is deleted. When we are finished reading the trace,
  42. # what is left are the unmatched alloc/realloc requests.
  43. #
  44. %HASH = ();
  45. # Read the trace header values
  46. $heap_size = <STDIN>;
  47. chomp($heap_size);
  48. $num_blocks = <STDIN>;
  49. chomp($num_blocks);
  50. $old_num_ops = <STDIN>;
  51. chomp($old_num_ops);
  52. $weight = <STDIN>;
  53. chomp($weight);
  54. #
  55. # Find any allocate requests that don't have a matching free requests
  56. #
  57. $linenum = 4;
  58. $requestnum = 0;
  59. while ($line = <STDIN>) {
  60. chomp($line);
  61. $linenum++;
  62. ($cmd, $id, $size) = split(" ", $line);
  63. # ignore blank lines
  64. if (!$cmd) {
  65. next;
  66. }
  67. # save the line for output later
  68. $lines[$requestnum++] = $line;
  69. #ignore realloc requests, as long as they are preceeded by an alloc request
  70. if ($cmd eq "r") {
  71. if (!$HASH{$id}) {
  72. die "$0: ERROR[$linenum]: realloc without previous alloc\n";
  73. }
  74. next;
  75. }
  76. if ($cmd eq "a" and $HASH{$id} eq "a") {
  77. die "$0: ERROR[$linenum]: allocate with no intervening free.\n";
  78. }
  79. if ($cmd eq "a" and $HASH{$id} eq "f") {
  80. die "$0: ERROR[$linenum]: reused ID $id.\n";
  81. }
  82. if ($cmd eq "f" and !exists($HASH{$id})) {
  83. die "$0: ERROR[$linenum]: freeing unallocated block.\n";
  84. next;
  85. }
  86. if ($cmd eq "f" and !$HASH{$id} eq "f") {
  87. die "$0: ERROR[$linenum]: freeing already freed block.\n";
  88. next;
  89. }
  90. if ($cmd eq "f") {
  91. delete $HASH{$id};
  92. }
  93. else {
  94. $HASH{$id} = $cmd;
  95. }
  96. }
  97. #
  98. # If called with -s argument , print a brief balance summary and exit
  99. #
  100. if ($summary) {
  101. if (!%HASH) {
  102. print "Balanced trace.\n";
  103. }
  104. else {
  105. print "Unbalanced tree.\n";
  106. }
  107. exit;
  108. }
  109. #
  110. # Output a balanced version of the trace
  111. #
  112. $new_ops = keys %HASH;
  113. $new_num_ops = $old_num_ops + $new_ops;
  114. print "$heap_size\n";
  115. print "$num_blocks\n";
  116. print "$new_num_ops\n";
  117. print "$weight\n";
  118. # print the old requests
  119. foreach $item (@lines) {
  120. print "$item\n";
  121. }
  122. # print a set of free requests that will balance the trace
  123. foreach $key (sort keys %HASH) {
  124. if ($HASH{$key} ne "a" and $HASH{$key} ne "r") {
  125. die "$0: ERROR: Invalid free request in residue.\n";
  126. }
  127. print "f $key\n";
  128. }
  129. exit;