trace-vmscan-postprocess.pl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. #!/usr/bin/perl
  2. # This is a POC for reading the text representation of trace output related to
  3. # page reclaim. It makes an attempt to extract some high-level information on
  4. # what is going on. The accuracy of the parser may vary
  5. #
  6. # Example usage: trace-vmscan-postprocess.pl < /sys/kernel/debug/tracing/trace_pipe
  7. # other options
  8. # --read-procstat If the trace lacks process info, get it from /proc
  9. # --ignore-pid Aggregate processes of the same name together
  10. #
  11. # Copyright (c) IBM Corporation 2009
  12. # Author: Mel Gorman <mel@csn.ul.ie>
  13. use strict;
  14. use Getopt::Long;
  15. # Tracepoint events
  16. use constant MM_VMSCAN_DIRECT_RECLAIM_BEGIN => 1;
  17. use constant MM_VMSCAN_DIRECT_RECLAIM_END => 2;
  18. use constant MM_VMSCAN_KSWAPD_WAKE => 3;
  19. use constant MM_VMSCAN_KSWAPD_SLEEP => 4;
  20. use constant MM_VMSCAN_LRU_SHRINK_ACTIVE => 5;
  21. use constant MM_VMSCAN_LRU_SHRINK_INACTIVE => 6;
  22. use constant MM_VMSCAN_LRU_ISOLATE => 7;
  23. use constant MM_VMSCAN_WRITEPAGE_FILE_SYNC => 8;
  24. use constant MM_VMSCAN_WRITEPAGE_ANON_SYNC => 9;
  25. use constant MM_VMSCAN_WRITEPAGE_FILE_ASYNC => 10;
  26. use constant MM_VMSCAN_WRITEPAGE_ANON_ASYNC => 11;
  27. use constant MM_VMSCAN_WRITEPAGE_ASYNC => 12;
  28. use constant EVENT_UNKNOWN => 13;
  29. # Per-order events
  30. use constant MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER => 11;
  31. use constant MM_VMSCAN_WAKEUP_KSWAPD_PERORDER => 12;
  32. use constant MM_VMSCAN_KSWAPD_WAKE_PERORDER => 13;
  33. use constant HIGH_KSWAPD_REWAKEUP_PERORDER => 14;
  34. # Constants used to track state
  35. use constant STATE_DIRECT_BEGIN => 15;
  36. use constant STATE_DIRECT_ORDER => 16;
  37. use constant STATE_KSWAPD_BEGIN => 17;
  38. use constant STATE_KSWAPD_ORDER => 18;
  39. # High-level events extrapolated from tracepoints
  40. use constant HIGH_DIRECT_RECLAIM_LATENCY => 19;
  41. use constant HIGH_KSWAPD_LATENCY => 20;
  42. use constant HIGH_KSWAPD_REWAKEUP => 21;
  43. use constant HIGH_NR_SCANNED => 22;
  44. use constant HIGH_NR_TAKEN => 23;
  45. use constant HIGH_NR_RECLAIM => 24;
  46. use constant HIGH_NR_CONTIG_DIRTY => 25;
  47. my %perprocesspid;
  48. my %perprocess;
  49. my %last_procmap;
  50. my $opt_ignorepid;
  51. my $opt_read_procstat;
  52. my $total_wakeup_kswapd;
  53. my ($total_direct_reclaim, $total_direct_nr_scanned);
  54. my ($total_direct_latency, $total_kswapd_latency);
  55. my ($total_direct_writepage_file_sync, $total_direct_writepage_file_async);
  56. my ($total_direct_writepage_anon_sync, $total_direct_writepage_anon_async);
  57. my ($total_kswapd_nr_scanned, $total_kswapd_wake);
  58. my ($total_kswapd_writepage_file_sync, $total_kswapd_writepage_file_async);
  59. my ($total_kswapd_writepage_anon_sync, $total_kswapd_writepage_anon_async);
  60. # Catch sigint and exit on request
  61. my $sigint_report = 0;
  62. my $sigint_exit = 0;
  63. my $sigint_pending = 0;
  64. my $sigint_received = 0;
  65. sub sigint_handler {
  66. my $current_time = time;
  67. if ($current_time - 2 > $sigint_received) {
  68. print "SIGINT received, report pending. Hit ctrl-c again to exit\n";
  69. $sigint_report = 1;
  70. } else {
  71. if (!$sigint_exit) {
  72. print "Second SIGINT received quickly, exiting\n";
  73. }
  74. $sigint_exit++;
  75. }
  76. if ($sigint_exit > 3) {
  77. print "Many SIGINTs received, exiting now without report\n";
  78. exit;
  79. }
  80. $sigint_received = $current_time;
  81. $sigint_pending = 1;
  82. }
  83. $SIG{INT} = "sigint_handler";
  84. # Parse command line options
  85. GetOptions(
  86. 'ignore-pid' => \$opt_ignorepid,
  87. 'read-procstat' => \$opt_read_procstat,
  88. );
  89. # Defaults for dynamically discovered regex's
  90. my $regex_direct_begin_default = 'order=([0-9]*) may_writepage=([0-9]*) gfp_flags=([A-Z_|]*)';
  91. my $regex_direct_end_default = 'nr_reclaimed=([0-9]*)';
  92. my $regex_kswapd_wake_default = 'nid=([0-9]*) order=([0-9]*)';
  93. my $regex_kswapd_sleep_default = 'nid=([0-9]*)';
  94. my $regex_wakeup_kswapd_default = 'nid=([0-9]*) zid=([0-9]*) order=([0-9]*)';
  95. my $regex_lru_isolate_default = 'isolate_mode=([0-9]*) order=([0-9]*) nr_requested=([0-9]*) nr_scanned=([0-9]*) nr_taken=([0-9]*) contig_taken=([0-9]*) contig_dirty=([0-9]*) contig_failed=([0-9]*)';
  96. my $regex_lru_shrink_inactive_default = 'lru=([A-Z_]*) nr_scanned=([0-9]*) nr_reclaimed=([0-9]*) priority=([0-9]*)';
  97. my $regex_lru_shrink_active_default = 'lru=([A-Z_]*) nr_scanned=([0-9]*) nr_rotated=([0-9]*) priority=([0-9]*)';
  98. my $regex_writepage_default = 'page=([0-9a-f]*) pfn=([0-9]*) flags=([A-Z_|]*)';
  99. # Dyanically discovered regex
  100. my $regex_direct_begin;
  101. my $regex_direct_end;
  102. my $regex_kswapd_wake;
  103. my $regex_kswapd_sleep;
  104. my $regex_wakeup_kswapd;
  105. my $regex_lru_isolate;
  106. my $regex_lru_shrink_inactive;
  107. my $regex_lru_shrink_active;
  108. my $regex_writepage;
  109. # Static regex used. Specified like this for readability and for use with /o
  110. # (process_pid) (cpus ) ( time ) (tpoint ) (details)
  111. my $regex_traceevent = '\s*([a-zA-Z0-9-]*)\s*(\[[0-9]*\])\s*([0-9.]*):\s*([a-zA-Z_]*):\s*(.*)';
  112. my $regex_statname = '[-0-9]*\s\((.*)\).*';
  113. my $regex_statppid = '[-0-9]*\s\(.*\)\s[A-Za-z]\s([0-9]*).*';
  114. sub generate_traceevent_regex {
  115. my $event = shift;
  116. my $default = shift;
  117. my $regex;
  118. # Read the event format or use the default
  119. if (!open (FORMAT, "/sys/kernel/debug/tracing/events/$event/format")) {
  120. print("WARNING: Event $event format string not found\n");
  121. return $default;
  122. } else {
  123. my $line;
  124. while (!eof(FORMAT)) {
  125. $line = <FORMAT>;
  126. $line =~ s/, REC->.*//;
  127. if ($line =~ /^print fmt:\s"(.*)".*/) {
  128. $regex = $1;
  129. $regex =~ s/%s/\([0-9a-zA-Z|_]*\)/g;
  130. $regex =~ s/%p/\([0-9a-f]*\)/g;
  131. $regex =~ s/%d/\([-0-9]*\)/g;
  132. $regex =~ s/%ld/\([-0-9]*\)/g;
  133. $regex =~ s/%lu/\([0-9]*\)/g;
  134. }
  135. }
  136. }
  137. # Can't handle the print_flags stuff but in the context of this
  138. # script, it really doesn't matter
  139. $regex =~ s/\(REC.*\) \? __print_flags.*//;
  140. # Verify fields are in the right order
  141. my $tuple;
  142. foreach $tuple (split /\s/, $regex) {
  143. my ($key, $value) = split(/=/, $tuple);
  144. my $expected = shift;
  145. if ($key ne $expected) {
  146. print("WARNING: Format not as expected for event $event '$key' != '$expected'\n");
  147. $regex =~ s/$key=\((.*)\)/$key=$1/;
  148. }
  149. }
  150. if (defined shift) {
  151. die("Fewer fields than expected in format");
  152. }
  153. return $regex;
  154. }
  155. $regex_direct_begin = generate_traceevent_regex(
  156. "vmscan/mm_vmscan_direct_reclaim_begin",
  157. $regex_direct_begin_default,
  158. "order", "may_writepage",
  159. "gfp_flags");
  160. $regex_direct_end = generate_traceevent_regex(
  161. "vmscan/mm_vmscan_direct_reclaim_end",
  162. $regex_direct_end_default,
  163. "nr_reclaimed");
  164. $regex_kswapd_wake = generate_traceevent_regex(
  165. "vmscan/mm_vmscan_kswapd_wake",
  166. $regex_kswapd_wake_default,
  167. "nid", "order");
  168. $regex_kswapd_sleep = generate_traceevent_regex(
  169. "vmscan/mm_vmscan_kswapd_sleep",
  170. $regex_kswapd_sleep_default,
  171. "nid");
  172. $regex_wakeup_kswapd = generate_traceevent_regex(
  173. "vmscan/mm_vmscan_wakeup_kswapd",
  174. $regex_wakeup_kswapd_default,
  175. "nid", "zid", "order");
  176. $regex_lru_isolate = generate_traceevent_regex(
  177. "vmscan/mm_vmscan_lru_isolate",
  178. $regex_lru_isolate_default,
  179. "isolate_mode", "order",
  180. "nr_requested", "nr_scanned", "nr_taken",
  181. "contig_taken", "contig_dirty", "contig_failed");
  182. $regex_lru_shrink_inactive = generate_traceevent_regex(
  183. "vmscan/mm_vmscan_lru_shrink_inactive",
  184. $regex_lru_shrink_inactive_default,
  185. "nid", "zid",
  186. "lru",
  187. "nr_scanned", "nr_reclaimed", "priority");
  188. $regex_lru_shrink_active = generate_traceevent_regex(
  189. "vmscan/mm_vmscan_lru_shrink_active",
  190. $regex_lru_shrink_active_default,
  191. "nid", "zid",
  192. "lru",
  193. "nr_scanned", "nr_rotated", "priority");
  194. $regex_writepage = generate_traceevent_regex(
  195. "vmscan/mm_vmscan_writepage",
  196. $regex_writepage_default,
  197. "page", "pfn", "flags");
  198. sub read_statline($) {
  199. my $pid = $_[0];
  200. my $statline;
  201. if (open(STAT, "/proc/$pid/stat")) {
  202. $statline = <STAT>;
  203. close(STAT);
  204. }
  205. if ($statline eq '') {
  206. $statline = "-1 (UNKNOWN_PROCESS_NAME) R 0";
  207. }
  208. return $statline;
  209. }
  210. sub guess_process_pid($$) {
  211. my $pid = $_[0];
  212. my $statline = $_[1];
  213. if ($pid == 0) {
  214. return "swapper-0";
  215. }
  216. if ($statline !~ /$regex_statname/o) {
  217. die("Failed to math stat line for process name :: $statline");
  218. }
  219. return "$1-$pid";
  220. }
  221. # Convert sec.usec timestamp format
  222. sub timestamp_to_ms($) {
  223. my $timestamp = $_[0];
  224. my ($sec, $usec) = split (/\./, $timestamp);
  225. return ($sec * 1000) + ($usec / 1000);
  226. }
  227. sub process_events {
  228. my $traceevent;
  229. my $process_pid;
  230. my $cpus;
  231. my $timestamp;
  232. my $tracepoint;
  233. my $details;
  234. my $statline;
  235. # Read each line of the event log
  236. EVENT_PROCESS:
  237. while ($traceevent = <STDIN>) {
  238. if ($traceevent =~ /$regex_traceevent/o) {
  239. $process_pid = $1;
  240. $timestamp = $3;
  241. $tracepoint = $4;
  242. $process_pid =~ /(.*)-([0-9]*)$/;
  243. my $process = $1;
  244. my $pid = $2;
  245. if ($process eq "") {
  246. $process = $last_procmap{$pid};
  247. $process_pid = "$process-$pid";
  248. }
  249. $last_procmap{$pid} = $process;
  250. if ($opt_read_procstat) {
  251. $statline = read_statline($pid);
  252. if ($opt_read_procstat && $process eq '') {
  253. $process_pid = guess_process_pid($pid, $statline);
  254. }
  255. }
  256. } else {
  257. next;
  258. }
  259. # Perl Switch() sucks majorly
  260. if ($tracepoint eq "mm_vmscan_direct_reclaim_begin") {
  261. $timestamp = timestamp_to_ms($timestamp);
  262. $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN}++;
  263. $perprocesspid{$process_pid}->{STATE_DIRECT_BEGIN} = $timestamp;
  264. $details = $5;
  265. if ($details !~ /$regex_direct_begin/o) {
  266. print "WARNING: Failed to parse mm_vmscan_direct_reclaim_begin as expected\n";
  267. print " $details\n";
  268. print " $regex_direct_begin\n";
  269. next;
  270. }
  271. my $order = $1;
  272. $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER}[$order]++;
  273. $perprocesspid{$process_pid}->{STATE_DIRECT_ORDER} = $order;
  274. } elsif ($tracepoint eq "mm_vmscan_direct_reclaim_end") {
  275. # Count the event itself
  276. my $index = $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_END};
  277. $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_END}++;
  278. # Record how long direct reclaim took this time
  279. if (defined $perprocesspid{$process_pid}->{STATE_DIRECT_BEGIN}) {
  280. $timestamp = timestamp_to_ms($timestamp);
  281. my $order = $perprocesspid{$process_pid}->{STATE_DIRECT_ORDER};
  282. my $latency = ($timestamp - $perprocesspid{$process_pid}->{STATE_DIRECT_BEGIN});
  283. $perprocesspid{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index] = "$order-$latency";
  284. }
  285. } elsif ($tracepoint eq "mm_vmscan_kswapd_wake") {
  286. $details = $5;
  287. if ($details !~ /$regex_kswapd_wake/o) {
  288. print "WARNING: Failed to parse mm_vmscan_kswapd_wake as expected\n";
  289. print " $details\n";
  290. print " $regex_kswapd_wake\n";
  291. next;
  292. }
  293. my $order = $2;
  294. $perprocesspid{$process_pid}->{STATE_KSWAPD_ORDER} = $order;
  295. if (!$perprocesspid{$process_pid}->{STATE_KSWAPD_BEGIN}) {
  296. $timestamp = timestamp_to_ms($timestamp);
  297. $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE}++;
  298. $perprocesspid{$process_pid}->{STATE_KSWAPD_BEGIN} = $timestamp;
  299. $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE_PERORDER}[$order]++;
  300. } else {
  301. $perprocesspid{$process_pid}->{HIGH_KSWAPD_REWAKEUP}++;
  302. $perprocesspid{$process_pid}->{HIGH_KSWAPD_REWAKEUP_PERORDER}[$order]++;
  303. }
  304. } elsif ($tracepoint eq "mm_vmscan_kswapd_sleep") {
  305. # Count the event itself
  306. my $index = $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_SLEEP};
  307. $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_SLEEP}++;
  308. # Record how long kswapd was awake
  309. $timestamp = timestamp_to_ms($timestamp);
  310. my $order = $perprocesspid{$process_pid}->{STATE_KSWAPD_ORDER};
  311. my $latency = ($timestamp - $perprocesspid{$process_pid}->{STATE_KSWAPD_BEGIN});
  312. $perprocesspid{$process_pid}->{HIGH_KSWAPD_LATENCY}[$index] = "$order-$latency";
  313. $perprocesspid{$process_pid}->{STATE_KSWAPD_BEGIN} = 0;
  314. } elsif ($tracepoint eq "mm_vmscan_wakeup_kswapd") {
  315. $perprocesspid{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD}++;
  316. $details = $5;
  317. if ($details !~ /$regex_wakeup_kswapd/o) {
  318. print "WARNING: Failed to parse mm_vmscan_wakeup_kswapd as expected\n";
  319. print " $details\n";
  320. print " $regex_wakeup_kswapd\n";
  321. next;
  322. }
  323. my $order = $3;
  324. $perprocesspid{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD_PERORDER}[$order]++;
  325. } elsif ($tracepoint eq "mm_vmscan_lru_isolate") {
  326. $details = $5;
  327. if ($details !~ /$regex_lru_isolate/o) {
  328. print "WARNING: Failed to parse mm_vmscan_lru_isolate as expected\n";
  329. print " $details\n";
  330. print " $regex_lru_isolate/o\n";
  331. next;
  332. }
  333. my $nr_scanned = $4;
  334. my $nr_contig_dirty = $7;
  335. $perprocesspid{$process_pid}->{HIGH_NR_SCANNED} += $nr_scanned;
  336. $perprocesspid{$process_pid}->{HIGH_NR_CONTIG_DIRTY} += $nr_contig_dirty;
  337. } elsif ($tracepoint eq "mm_vmscan_writepage") {
  338. $details = $5;
  339. if ($details !~ /$regex_writepage/o) {
  340. print "WARNING: Failed to parse mm_vmscan_writepage as expected\n";
  341. print " $details\n";
  342. print " $regex_writepage\n";
  343. next;
  344. }
  345. my $flags = $3;
  346. my $file = 0;
  347. my $sync_io = 0;
  348. if ($flags =~ /RECLAIM_WB_FILE/) {
  349. $file = 1;
  350. }
  351. if ($flags =~ /RECLAIM_WB_SYNC/) {
  352. $sync_io = 1;
  353. }
  354. if ($sync_io) {
  355. if ($file) {
  356. $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC}++;
  357. } else {
  358. $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC}++;
  359. }
  360. } else {
  361. if ($file) {
  362. $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC}++;
  363. } else {
  364. $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC}++;
  365. }
  366. }
  367. } else {
  368. $perprocesspid{$process_pid}->{EVENT_UNKNOWN}++;
  369. }
  370. if ($sigint_pending) {
  371. last EVENT_PROCESS;
  372. }
  373. }
  374. }
  375. sub dump_stats {
  376. my $hashref = shift;
  377. my %stats = %$hashref;
  378. # Dump per-process stats
  379. my $process_pid;
  380. my $max_strlen = 0;
  381. # Get the maximum process name
  382. foreach $process_pid (keys %perprocesspid) {
  383. my $len = length($process_pid);
  384. if ($len > $max_strlen) {
  385. $max_strlen = $len;
  386. }
  387. }
  388. $max_strlen += 2;
  389. # Work out latencies
  390. printf("\n") if !$opt_ignorepid;
  391. printf("Reclaim latencies expressed as order-latency_in_ms\n") if !$opt_ignorepid;
  392. foreach $process_pid (keys %stats) {
  393. if (!$stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[0] &&
  394. !$stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[0]) {
  395. next;
  396. }
  397. printf "%-" . $max_strlen . "s ", $process_pid if !$opt_ignorepid;
  398. my $index = 0;
  399. while (defined $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index] ||
  400. defined $stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[$index]) {
  401. if ($stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]) {
  402. printf("%s ", $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]) if !$opt_ignorepid;
  403. my ($dummy, $latency) = split(/-/, $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]);
  404. $total_direct_latency += $latency;
  405. } else {
  406. printf("%s ", $stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[$index]) if !$opt_ignorepid;
  407. my ($dummy, $latency) = split(/-/, $stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[$index]);
  408. $total_kswapd_latency += $latency;
  409. }
  410. $index++;
  411. }
  412. print "\n" if !$opt_ignorepid;
  413. }
  414. # Print out process activity
  415. printf("\n");
  416. printf("%-" . $max_strlen . "s %8s %10s %8s %8s %8s %8s %8s\n", "Process", "Direct", "Wokeup", "Pages", "Pages", "Pages", "Time");
  417. printf("%-" . $max_strlen . "s %8s %10s %8s %8s %8s %8s %8s\n", "details", "Rclms", "Kswapd", "Scanned", "Sync-IO", "ASync-IO", "Stalled");
  418. foreach $process_pid (keys %stats) {
  419. if (!$stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN}) {
  420. next;
  421. }
  422. $total_direct_reclaim += $stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN};
  423. $total_wakeup_kswapd += $stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD};
  424. $total_direct_nr_scanned += $stats{$process_pid}->{HIGH_NR_SCANNED};
  425. $total_direct_writepage_file_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC};
  426. $total_direct_writepage_anon_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC};
  427. $total_direct_writepage_file_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC};
  428. $total_direct_writepage_anon_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC};
  429. my $index = 0;
  430. my $this_reclaim_delay = 0;
  431. while (defined $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]) {
  432. my ($dummy, $latency) = split(/-/, $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]);
  433. $this_reclaim_delay += $latency;
  434. $index++;
  435. }
  436. printf("%-" . $max_strlen . "s %8d %10d %8u %8u %8u %8.3f",
  437. $process_pid,
  438. $stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN},
  439. $stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD},
  440. $stats{$process_pid}->{HIGH_NR_SCANNED},
  441. $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC},
  442. $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC},
  443. $this_reclaim_delay / 1000);
  444. if ($stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN}) {
  445. print " ";
  446. for (my $order = 0; $order < 20; $order++) {
  447. my $count = $stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER}[$order];
  448. if ($count != 0) {
  449. print "direct-$order=$count ";
  450. }
  451. }
  452. }
  453. if ($stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD}) {
  454. print " ";
  455. for (my $order = 0; $order < 20; $order++) {
  456. my $count = $stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD_PERORDER}[$order];
  457. if ($count != 0) {
  458. print "wakeup-$order=$count ";
  459. }
  460. }
  461. }
  462. if ($stats{$process_pid}->{HIGH_NR_CONTIG_DIRTY}) {
  463. print " ";
  464. my $count = $stats{$process_pid}->{HIGH_NR_CONTIG_DIRTY};
  465. if ($count != 0) {
  466. print "contig-dirty=$count ";
  467. }
  468. }
  469. print "\n";
  470. }
  471. # Print out kswapd activity
  472. printf("\n");
  473. printf("%-" . $max_strlen . "s %8s %10s %8s %8s %8s %8s\n", "Kswapd", "Kswapd", "Order", "Pages", "Pages", "Pages");
  474. printf("%-" . $max_strlen . "s %8s %10s %8s %8s %8s %8s\n", "Instance", "Wakeups", "Re-wakeup", "Scanned", "Sync-IO", "ASync-IO");
  475. foreach $process_pid (keys %stats) {
  476. if (!$stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE}) {
  477. next;
  478. }
  479. $total_kswapd_wake += $stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE};
  480. $total_kswapd_nr_scanned += $stats{$process_pid}->{HIGH_NR_SCANNED};
  481. $total_kswapd_writepage_file_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC};
  482. $total_kswapd_writepage_anon_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC};
  483. $total_kswapd_writepage_file_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC};
  484. $total_kswapd_writepage_anon_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC};
  485. printf("%-" . $max_strlen . "s %8d %10d %8u %8i %8u",
  486. $process_pid,
  487. $stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE},
  488. $stats{$process_pid}->{HIGH_KSWAPD_REWAKEUP},
  489. $stats{$process_pid}->{HIGH_NR_SCANNED},
  490. $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC},
  491. $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC});
  492. if ($stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE}) {
  493. print " ";
  494. for (my $order = 0; $order < 20; $order++) {
  495. my $count = $stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE_PERORDER}[$order];
  496. if ($count != 0) {
  497. print "wake-$order=$count ";
  498. }
  499. }
  500. }
  501. if ($stats{$process_pid}->{HIGH_KSWAPD_REWAKEUP}) {
  502. print " ";
  503. for (my $order = 0; $order < 20; $order++) {
  504. my $count = $stats{$process_pid}->{HIGH_KSWAPD_REWAKEUP_PERORDER}[$order];
  505. if ($count != 0) {
  506. print "rewake-$order=$count ";
  507. }
  508. }
  509. }
  510. printf("\n");
  511. }
  512. # Print out summaries
  513. $total_direct_latency /= 1000;
  514. $total_kswapd_latency /= 1000;
  515. print "\nSummary\n";
  516. print "Direct reclaims: $total_direct_reclaim\n";
  517. print "Direct reclaim pages scanned: $total_direct_nr_scanned\n";
  518. print "Direct reclaim write file sync I/O: $total_direct_writepage_file_sync\n";
  519. print "Direct reclaim write anon sync I/O: $total_direct_writepage_anon_sync\n";
  520. print "Direct reclaim write file async I/O: $total_direct_writepage_file_async\n";
  521. print "Direct reclaim write anon async I/O: $total_direct_writepage_anon_async\n";
  522. print "Wake kswapd requests: $total_wakeup_kswapd\n";
  523. printf "Time stalled direct reclaim: %-1.2f seconds\n", $total_direct_latency;
  524. print "\n";
  525. print "Kswapd wakeups: $total_kswapd_wake\n";
  526. print "Kswapd pages scanned: $total_kswapd_nr_scanned\n";
  527. print "Kswapd reclaim write file sync I/O: $total_kswapd_writepage_file_sync\n";
  528. print "Kswapd reclaim write anon sync I/O: $total_kswapd_writepage_anon_sync\n";
  529. print "Kswapd reclaim write file async I/O: $total_kswapd_writepage_file_async\n";
  530. print "Kswapd reclaim write anon async I/O: $total_kswapd_writepage_anon_async\n";
  531. printf "Time kswapd awake: %-1.2f seconds\n", $total_kswapd_latency;
  532. }
  533. sub aggregate_perprocesspid() {
  534. my $process_pid;
  535. my $process;
  536. undef %perprocess;
  537. foreach $process_pid (keys %perprocesspid) {
  538. $process = $process_pid;
  539. $process =~ s/-([0-9])*$//;
  540. if ($process eq '') {
  541. $process = "NO_PROCESS_NAME";
  542. }
  543. $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN} += $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN};
  544. $perprocess{$process}->{MM_VMSCAN_KSWAPD_WAKE} += $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE};
  545. $perprocess{$process}->{MM_VMSCAN_WAKEUP_KSWAPD} += $perprocesspid{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD};
  546. $perprocess{$process}->{HIGH_KSWAPD_REWAKEUP} += $perprocesspid{$process_pid}->{HIGH_KSWAPD_REWAKEUP};
  547. $perprocess{$process}->{HIGH_NR_SCANNED} += $perprocesspid{$process_pid}->{HIGH_NR_SCANNED};
  548. $perprocess{$process}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC};
  549. $perprocess{$process}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC};
  550. $perprocess{$process}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC};
  551. $perprocess{$process}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC};
  552. for (my $order = 0; $order < 20; $order++) {
  553. $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER}[$order] += $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER}[$order];
  554. $perprocess{$process}->{MM_VMSCAN_WAKEUP_KSWAPD_PERORDER}[$order] += $perprocesspid{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD_PERORDER}[$order];
  555. $perprocess{$process}->{MM_VMSCAN_KSWAPD_WAKE_PERORDER}[$order] += $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE_PERORDER}[$order];
  556. }
  557. # Aggregate direct reclaim latencies
  558. my $wr_index = $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_END};
  559. my $rd_index = 0;
  560. while (defined $perprocesspid{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$rd_index]) {
  561. $perprocess{$process}->{HIGH_DIRECT_RECLAIM_LATENCY}[$wr_index] = $perprocesspid{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$rd_index];
  562. $rd_index++;
  563. $wr_index++;
  564. }
  565. $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_END} = $wr_index;
  566. # Aggregate kswapd latencies
  567. my $wr_index = $perprocess{$process}->{MM_VMSCAN_KSWAPD_SLEEP};
  568. my $rd_index = 0;
  569. while (defined $perprocesspid{$process_pid}->{HIGH_KSWAPD_LATENCY}[$rd_index]) {
  570. $perprocess{$process}->{HIGH_KSWAPD_LATENCY}[$wr_index] = $perprocesspid{$process_pid}->{HIGH_KSWAPD_LATENCY}[$rd_index];
  571. $rd_index++;
  572. $wr_index++;
  573. }
  574. $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_END} = $wr_index;
  575. }
  576. }
  577. sub report() {
  578. if (!$opt_ignorepid) {
  579. dump_stats(\%perprocesspid);
  580. } else {
  581. aggregate_perprocesspid();
  582. dump_stats(\%perprocess);
  583. }
  584. }
  585. # Process events or signals until neither is available
  586. sub signal_loop() {
  587. my $sigint_processed;
  588. do {
  589. $sigint_processed = 0;
  590. process_events();
  591. # Handle pending signals if any
  592. if ($sigint_pending) {
  593. my $current_time = time;
  594. if ($sigint_exit) {
  595. print "Received exit signal\n";
  596. $sigint_pending = 0;
  597. }
  598. if ($sigint_report) {
  599. if ($current_time >= $sigint_received + 2) {
  600. report();
  601. $sigint_report = 0;
  602. $sigint_pending = 0;
  603. $sigint_processed = 1;
  604. }
  605. }
  606. }
  607. } while ($sigint_pending || $sigint_processed);
  608. }
  609. signal_loop();
  610. report();