rwtop.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # (c) 2010, Tom Zanussi <tzanussi@gmail.com>
  2. # Licensed under the terms of the GNU GPL License version 2
  3. # read/write top
  4. #
  5. # Periodically displays system-wide r/w call activity, broken down by
  6. # pid. If an [interval] arg is specified, the display will be
  7. # refreshed every [interval] seconds. The default interval is 3
  8. # seconds.
  9. use 5.010000;
  10. use strict;
  11. use warnings;
  12. use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";
  13. use lib "./Perf-Trace-Util/lib";
  14. use Perf::Trace::Core;
  15. use Perf::Trace::Util;
  16. use POSIX qw/SIGALRM SA_RESTART/;
  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. my $sa = POSIX::SigAction->new(\&set_print_pending);
  75. $sa->flags(SA_RESTART);
  76. $sa->safe(1);
  77. POSIX::sigaction(SIGALRM, $sa) or die "Can't set SIGALRM handler: $!\n";
  78. alarm 1;
  79. }
  80. sub trace_end
  81. {
  82. print_unhandled();
  83. print_totals();
  84. }
  85. sub print_check()
  86. {
  87. if ($print_pending == 1) {
  88. $print_pending = 0;
  89. print_totals();
  90. }
  91. }
  92. sub set_print_pending()
  93. {
  94. $print_pending = 1;
  95. alarm $interval;
  96. }
  97. sub print_totals
  98. {
  99. my $count;
  100. $count = 0;
  101. clear_term();
  102. printf("\nread counts by pid:\n\n");
  103. printf("%6s %20s %10s %10s %10s\n", "pid", "comm",
  104. "# reads", "bytes_req", "bytes_read");
  105. printf("%6s %-20s %10s %10s %10s\n", "------", "--------------------",
  106. "----------", "----------", "----------");
  107. foreach my $pid (sort { ($reads{$b}{bytes_read} || 0) <=>
  108. ($reads{$a}{bytes_read} || 0) } keys %reads) {
  109. my $comm = $reads{$pid}{comm} || "";
  110. my $total_reads = $reads{$pid}{total_reads} || 0;
  111. my $bytes_requested = $reads{$pid}{bytes_requested} || 0;
  112. my $bytes_read = $reads{$pid}{bytes_read} || 0;
  113. printf("%6s %-20s %10s %10s %10s\n", $pid, $comm,
  114. $total_reads, $bytes_requested, $bytes_read);
  115. if (++$count == $nlines) {
  116. last;
  117. }
  118. }
  119. $count = 0;
  120. printf("\nwrite counts by pid:\n\n");
  121. printf("%6s %20s %10s %13s\n", "pid", "comm",
  122. "# writes", "bytes_written");
  123. printf("%6s %-20s %10s %13s\n", "------", "--------------------",
  124. "----------", "-------------");
  125. foreach my $pid (sort { ($writes{$b}{bytes_written} || 0) <=>
  126. ($writes{$a}{bytes_written} || 0)} keys %writes) {
  127. my $comm = $writes{$pid}{comm} || "";
  128. my $total_writes = $writes{$pid}{total_writes} || 0;
  129. my $bytes_written = $writes{$pid}{bytes_written} || 0;
  130. printf("%6s %-20s %10s %13s\n", $pid, $comm,
  131. $total_writes, $bytes_written);
  132. if (++$count == $nlines) {
  133. last;
  134. }
  135. }
  136. %reads = ();
  137. %writes = ();
  138. }
  139. my %unhandled;
  140. sub print_unhandled
  141. {
  142. if ((scalar keys %unhandled) == 0) {
  143. return;
  144. }
  145. print "\nunhandled events:\n\n";
  146. printf("%-40s %10s\n", "event", "count");
  147. printf("%-40s %10s\n", "----------------------------------------",
  148. "-----------");
  149. foreach my $event_name (keys %unhandled) {
  150. printf("%-40s %10d\n", $event_name, $unhandled{$event_name});
  151. }
  152. }
  153. sub trace_unhandled
  154. {
  155. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  156. $common_pid, $common_comm) = @_;
  157. $unhandled{$event_name}++;
  158. }