rwtop.pl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 $print_pending = 0;
  21. my %reads;
  22. my %writes;
  23. my $interval = shift;
  24. if (!$interval) {
  25. $interval = $default_interval;
  26. }
  27. sub syscalls::sys_exit_read
  28. {
  29. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  30. $common_pid, $common_comm,
  31. $nr, $ret) = @_;
  32. print_check();
  33. if ($ret > 0) {
  34. $reads{$common_pid}{bytes_read} += $ret;
  35. } else {
  36. if (!defined ($reads{$common_pid}{bytes_read})) {
  37. $reads{$common_pid}{bytes_read} = 0;
  38. }
  39. $reads{$common_pid}{errors}{$ret}++;
  40. }
  41. }
  42. sub syscalls::sys_enter_read
  43. {
  44. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  45. $common_pid, $common_comm,
  46. $nr, $fd, $buf, $count) = @_;
  47. print_check();
  48. $reads{$common_pid}{bytes_requested} += $count;
  49. $reads{$common_pid}{total_reads}++;
  50. $reads{$common_pid}{comm} = $common_comm;
  51. }
  52. sub syscalls::sys_exit_write
  53. {
  54. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  55. $common_pid, $common_comm,
  56. $nr, $ret) = @_;
  57. print_check();
  58. if ($ret <= 0) {
  59. $writes{$common_pid}{errors}{$ret}++;
  60. }
  61. }
  62. sub syscalls::sys_enter_write
  63. {
  64. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  65. $common_pid, $common_comm,
  66. $nr, $fd, $buf, $count) = @_;
  67. print_check();
  68. $writes{$common_pid}{bytes_written} += $count;
  69. $writes{$common_pid}{total_writes}++;
  70. $writes{$common_pid}{comm} = $common_comm;
  71. }
  72. sub trace_begin
  73. {
  74. $SIG{ALRM} = \&set_print_pending;
  75. alarm 1;
  76. }
  77. sub trace_end
  78. {
  79. print_unhandled();
  80. print_totals();
  81. }
  82. sub print_check()
  83. {
  84. if ($print_pending == 1) {
  85. $print_pending = 0;
  86. print_totals();
  87. }
  88. }
  89. sub set_print_pending()
  90. {
  91. $print_pending = 1;
  92. alarm $interval;
  93. }
  94. sub print_totals
  95. {
  96. my $count;
  97. $count = 0;
  98. clear_term();
  99. printf("\nread counts by pid:\n\n");
  100. printf("%6s %20s %10s %10s %10s\n", "pid", "comm",
  101. "# reads", "bytes_req", "bytes_read");
  102. printf("%6s %-20s %10s %10s %10s\n", "------", "--------------------",
  103. "----------", "----------", "----------");
  104. foreach my $pid (sort { ($reads{$b}{bytes_read} || 0) <=>
  105. ($reads{$a}{bytes_read} || 0) } keys %reads) {
  106. my $comm = $reads{$pid}{comm} || "";
  107. my $total_reads = $reads{$pid}{total_reads} || 0;
  108. my $bytes_requested = $reads{$pid}{bytes_requested} || 0;
  109. my $bytes_read = $reads{$pid}{bytes_read} || 0;
  110. printf("%6s %-20s %10s %10s %10s\n", $pid, $comm,
  111. $total_reads, $bytes_requested, $bytes_read);
  112. if (++$count == $nlines) {
  113. last;
  114. }
  115. }
  116. $count = 0;
  117. printf("\nwrite counts by pid:\n\n");
  118. printf("%6s %20s %10s %13s\n", "pid", "comm",
  119. "# writes", "bytes_written");
  120. printf("%6s %-20s %10s %13s\n", "------", "--------------------",
  121. "----------", "-------------");
  122. foreach my $pid (sort { ($writes{$b}{bytes_written} || 0) <=>
  123. ($writes{$a}{bytes_written} || 0)} keys %writes) {
  124. my $comm = $writes{$pid}{comm} || "";
  125. my $total_writes = $writes{$pid}{total_writes} || 0;
  126. my $bytes_written = $writes{$pid}{bytes_written} || 0;
  127. printf("%6s %-20s %10s %13s\n", $pid, $comm,
  128. $total_writes, $bytes_written);
  129. if (++$count == $nlines) {
  130. last;
  131. }
  132. }
  133. %reads = ();
  134. %writes = ();
  135. }
  136. my %unhandled;
  137. sub print_unhandled
  138. {
  139. if ((scalar keys %unhandled) == 0) {
  140. return;
  141. }
  142. print "\nunhandled events:\n\n";
  143. printf("%-40s %10s\n", "event", "count");
  144. printf("%-40s %10s\n", "----------------------------------------",
  145. "-----------");
  146. foreach my $event_name (keys %unhandled) {
  147. printf("%-40s %10d\n", $event_name, $unhandled{$event_name});
  148. }
  149. }
  150. sub trace_unhandled
  151. {
  152. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  153. $common_pid, $common_comm) = @_;
  154. $unhandled{$event_name}++;
  155. }