rw-by-pid.pl 5.1 KB

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