trace-vmscan-postprocess.pl 25 KB

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