rwtop.pl 4.4 KB

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