rwtop.pl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/perl -w
  2. # (c) 2010, Tom Zanussi <tzanussi@gmail.com>
  3. # Licensed under the terms of the GNU GPL License version 2
  4. # read/write top
  5. #
  6. # Periodically displays system-wide r/w call activity, broken down by
  7. # pid. If an [interval] arg is specified, the display will be
  8. # refreshed every [interval] seconds. The default interval is 3
  9. # seconds.
  10. use 5.010000;
  11. use strict;
  12. use warnings;
  13. use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";
  14. use lib "./Perf-Trace-Util/lib";
  15. use Perf::Trace::Core;
  16. use Perf::Trace::Util;
  17. my $default_interval = 3;
  18. my $nlines = 20;
  19. my $print_thread;
  20. my %reads;
  21. my %writes;
  22. my $interval = shift;
  23. if (!$interval) {
  24. $interval = $default_interval;
  25. }
  26. sub syscalls::sys_exit_read
  27. {
  28. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  29. $common_pid, $common_comm,
  30. $nr, $ret) = @_;
  31. if ($ret > 0) {
  32. $reads{$common_pid}{bytes_read} += $ret;
  33. } else {
  34. if (!defined ($reads{$common_pid}{bytes_read})) {
  35. $reads{$common_pid}{bytes_read} = 0;
  36. }
  37. $reads{$common_pid}{errors}{$ret}++;
  38. }
  39. }
  40. sub syscalls::sys_enter_read
  41. {
  42. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  43. $common_pid, $common_comm,
  44. $nr, $fd, $buf, $count) = @_;
  45. $reads{$common_pid}{bytes_requested} += $count;
  46. $reads{$common_pid}{total_reads}++;
  47. $reads{$common_pid}{comm} = $common_comm;
  48. }
  49. sub syscalls::sys_exit_write
  50. {
  51. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  52. $common_pid, $common_comm,
  53. $nr, $ret) = @_;
  54. if ($ret <= 0) {
  55. $writes{$common_pid}{errors}{$ret}++;
  56. }
  57. }
  58. sub syscalls::sys_enter_write
  59. {
  60. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  61. $common_pid, $common_comm,
  62. $nr, $fd, $buf, $count) = @_;
  63. $writes{$common_pid}{bytes_written} += $count;
  64. $writes{$common_pid}{total_writes}++;
  65. $writes{$common_pid}{comm} = $common_comm;
  66. }
  67. sub trace_begin
  68. {
  69. $SIG{ALRM} = \&print_totals;
  70. alarm 1;
  71. }
  72. sub trace_end
  73. {
  74. print_unhandled();
  75. print_totals();
  76. }
  77. sub print_totals
  78. {
  79. my $count;
  80. $count = 0;
  81. clear_term();
  82. printf("\nread counts by pid:\n\n");
  83. printf("%6s %20s %10s %10s %10s\n", "pid", "comm",
  84. "# reads", "bytes_req", "bytes_read");
  85. printf("%6s %-20s %10s %10s %10s\n", "------", "--------------------",
  86. "----------", "----------", "----------");
  87. foreach my $pid (sort {$reads{$b}{bytes_read} <=>
  88. $reads{$a}{bytes_read}} keys %reads) {
  89. my $comm = $reads{$pid}{comm};
  90. my $total_reads = $reads{$pid}{total_reads};
  91. my $bytes_requested = $reads{$pid}{bytes_requested};
  92. my $bytes_read = $reads{$pid}{bytes_read};
  93. printf("%6s %-20s %10s %10s %10s\n", $pid, $comm,
  94. $total_reads, $bytes_requested, $bytes_read);
  95. if (++$count == $nlines) {
  96. last;
  97. }
  98. }
  99. $count = 0;
  100. printf("\nwrite counts by pid:\n\n");
  101. printf("%6s %20s %10s %13s\n", "pid", "comm",
  102. "# writes", "bytes_written");
  103. printf("%6s %-20s %10s %13s\n", "------", "--------------------",
  104. "----------", "-------------");
  105. foreach my $pid (sort {$writes{$b}{bytes_written} <=>
  106. $writes{$a}{bytes_written}} keys %writes) {
  107. my $comm = $writes{$pid}{comm};
  108. my $total_writes = $writes{$pid}{total_writes};
  109. my $bytes_written = $writes{$pid}{bytes_written};
  110. printf("%6s %-20s %10s %13s\n", $pid, $comm,
  111. $total_writes, $bytes_written);
  112. if (++$count == $nlines) {
  113. last;
  114. }
  115. }
  116. %reads = ();
  117. %writes = ();
  118. alarm $interval;
  119. }
  120. my %unhandled;
  121. sub print_unhandled
  122. {
  123. if ((scalar keys %unhandled) == 0) {
  124. return;
  125. }
  126. print "\nunhandled events:\n\n";
  127. printf("%-40s %10s\n", "event", "count");
  128. printf("%-40s %10s\n", "----------------------------------------",
  129. "-----------");
  130. foreach my $event_name (keys %unhandled) {
  131. printf("%-40s %10d\n", $event_name, $unhandled{$event_name});
  132. }
  133. }
  134. sub trace_unhandled
  135. {
  136. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  137. $common_pid, $common_comm) = @_;
  138. $unhandled{$event_name}++;
  139. }