rw-by-pid.pl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # (c) 2009, Tom Zanussi <tzanussi@gmail.com>
  2. # Licensed under the terms of the GNU GPL License version 2
  3. # Display r/w activity for all processes
  4. # The common_* event handler fields are the most useful fields common to
  5. # all events. They don't necessarily correspond to the 'common_*' fields
  6. # in the status files. Those fields not available as handler params can
  7. # be retrieved via script functions of the form get_common_*().
  8. use 5.010000;
  9. use strict;
  10. use warnings;
  11. use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";
  12. use lib "./Perf-Trace-Util/lib";
  13. use Perf::Trace::Core;
  14. use Perf::Trace::Util;
  15. my %reads;
  16. my %writes;
  17. sub syscalls::sys_exit_read
  18. {
  19. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  20. $common_pid, $common_comm,
  21. $nr, $ret) = @_;
  22. if ($ret > 0) {
  23. $reads{$common_pid}{bytes_read} += $ret;
  24. } else {
  25. if (!defined ($reads{$common_pid}{bytes_read})) {
  26. $reads{$common_pid}{bytes_read} = 0;
  27. }
  28. $reads{$common_pid}{errors}{$ret}++;
  29. }
  30. }
  31. sub syscalls::sys_enter_read
  32. {
  33. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  34. $common_pid, $common_comm,
  35. $nr, $fd, $buf, $count) = @_;
  36. $reads{$common_pid}{bytes_requested} += $count;
  37. $reads{$common_pid}{total_reads}++;
  38. $reads{$common_pid}{comm} = $common_comm;
  39. }
  40. sub syscalls::sys_exit_write
  41. {
  42. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  43. $common_pid, $common_comm,
  44. $nr, $ret) = @_;
  45. if ($ret <= 0) {
  46. $writes{$common_pid}{errors}{$ret}++;
  47. }
  48. }
  49. sub syscalls::sys_enter_write
  50. {
  51. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  52. $common_pid, $common_comm,
  53. $nr, $fd, $buf, $count) = @_;
  54. $writes{$common_pid}{bytes_written} += $count;
  55. $writes{$common_pid}{total_writes}++;
  56. $writes{$common_pid}{comm} = $common_comm;
  57. }
  58. sub trace_end
  59. {
  60. printf("read counts by pid:\n\n");
  61. printf("%6s %20s %10s %10s %10s\n", "pid", "comm",
  62. "# reads", "bytes_requested", "bytes_read");
  63. printf("%6s %-20s %10s %10s %10s\n", "------", "--------------------",
  64. "-----------", "----------", "----------");
  65. foreach my $pid (sort { ($reads{$b}{bytes_read} || 0) <=>
  66. ($reads{$a}{bytes_read} || 0) } keys %reads) {
  67. my $comm = $reads{$pid}{comm} || "";
  68. my $total_reads = $reads{$pid}{total_reads} || 0;
  69. my $bytes_requested = $reads{$pid}{bytes_requested} || 0;
  70. my $bytes_read = $reads{$pid}{bytes_read} || 0;
  71. printf("%6s %-20s %10s %10s %10s\n", $pid, $comm,
  72. $total_reads, $bytes_requested, $bytes_read);
  73. }
  74. printf("\nfailed reads by pid:\n\n");
  75. printf("%6s %20s %6s %10s\n", "pid", "comm", "error #", "# errors");
  76. printf("%6s %20s %6s %10s\n", "------", "--------------------",
  77. "------", "----------");
  78. my @errcounts = ();
  79. foreach my $pid (keys %reads) {
  80. foreach my $error (keys %{$reads{$pid}{errors}}) {
  81. my $comm = $reads{$pid}{comm} || "";
  82. my $errcount = $reads{$pid}{errors}{$error} || 0;
  83. push @errcounts, [$pid, $comm, $error, $errcount];
  84. }
  85. }
  86. @errcounts = sort { $b->[3] <=> $a->[3] } @errcounts;
  87. for my $i (0 .. $#errcounts) {
  88. printf("%6d %-20s %6d %10s\n", $errcounts[$i][0],
  89. $errcounts[$i][1], $errcounts[$i][2], $errcounts[$i][3]);
  90. }
  91. printf("\nwrite counts by pid:\n\n");
  92. printf("%6s %20s %10s %10s\n", "pid", "comm",
  93. "# writes", "bytes_written");
  94. printf("%6s %-20s %10s %10s\n", "------", "--------------------",
  95. "-----------", "----------");
  96. foreach my $pid (sort { ($writes{$b}{bytes_written} || 0) <=>
  97. ($writes{$a}{bytes_written} || 0)} keys %writes) {
  98. my $comm = $writes{$pid}{comm} || "";
  99. my $total_writes = $writes{$pid}{total_writes} || 0;
  100. my $bytes_written = $writes{$pid}{bytes_written} || 0;
  101. printf("%6s %-20s %10s %10s\n", $pid, $comm,
  102. $total_writes, $bytes_written);
  103. }
  104. printf("\nfailed writes by pid:\n\n");
  105. printf("%6s %20s %6s %10s\n", "pid", "comm", "error #", "# errors");
  106. printf("%6s %20s %6s %10s\n", "------", "--------------------",
  107. "------", "----------");
  108. @errcounts = ();
  109. foreach my $pid (keys %writes) {
  110. foreach my $error (keys %{$writes{$pid}{errors}}) {
  111. my $comm = $writes{$pid}{comm} || "";
  112. my $errcount = $writes{$pid}{errors}{$error} || 0;
  113. push @errcounts, [$pid, $comm, $error, $errcount];
  114. }
  115. }
  116. @errcounts = sort { $b->[3] <=> $a->[3] } @errcounts;
  117. for my $i (0 .. $#errcounts) {
  118. printf("%6d %-20s %6d %10s\n", $errcounts[$i][0],
  119. $errcounts[$i][1], $errcounts[$i][2], $errcounts[$i][3]);
  120. }
  121. print_unhandled();
  122. }
  123. my %unhandled;
  124. sub print_unhandled
  125. {
  126. if ((scalar keys %unhandled) == 0) {
  127. return;
  128. }
  129. print "\nunhandled events:\n\n";
  130. printf("%-40s %10s\n", "event", "count");
  131. printf("%-40s %10s\n", "----------------------------------------",
  132. "-----------");
  133. foreach my $event_name (keys %unhandled) {
  134. printf("%-40s %10d\n", $event_name, $unhandled{$event_name});
  135. }
  136. }
  137. sub trace_unhandled
  138. {
  139. my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs,
  140. $common_pid, $common_comm) = @_;
  141. $unhandled{$event_name}++;
  142. }