trace-vmscan-postprocess.pl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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 $isolate_mode = $1;
  336. my $nr_scanned = $4;
  337. my $nr_contig_dirty = $7;
  338. # To closer match vmstat scanning statistics, only count isolate_both
  339. # and isolate_inactive as scanning. isolate_active is rotation
  340. # isolate_inactive == 1
  341. # isolate_active == 2
  342. # isolate_both == 3
  343. if ($isolate_mode != 2) {
  344. $perprocesspid{$process_pid}->{HIGH_NR_SCANNED} += $nr_scanned;
  345. }
  346. $perprocesspid{$process_pid}->{HIGH_NR_CONTIG_DIRTY} += $nr_contig_dirty;
  347. } elsif ($tracepoint eq "mm_vmscan_lru_shrink_inactive") {
  348. $details = $5;
  349. if ($details !~ /$regex_lru_shrink_inactive/o) {
  350. print "WARNING: Failed to parse mm_vmscan_lru_shrink_inactive as expected\n";
  351. print " $details\n";
  352. print " $regex_lru_shrink_inactive/o\n";
  353. next;
  354. }
  355. my $nr_reclaimed = $4;
  356. $perprocesspid{$process_pid}->{HIGH_NR_RECLAIMED} += $nr_reclaimed;
  357. } elsif ($tracepoint eq "mm_vmscan_writepage") {
  358. $details = $5;
  359. if ($details !~ /$regex_writepage/o) {
  360. print "WARNING: Failed to parse mm_vmscan_writepage as expected\n";
  361. print " $details\n";
  362. print " $regex_writepage\n";
  363. next;
  364. }
  365. my $flags = $3;
  366. my $file = 0;
  367. my $sync_io = 0;
  368. if ($flags =~ /RECLAIM_WB_FILE/) {
  369. $file = 1;
  370. }
  371. if ($flags =~ /RECLAIM_WB_SYNC/) {
  372. $sync_io = 1;
  373. }
  374. if ($sync_io) {
  375. if ($file) {
  376. $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC}++;
  377. } else {
  378. $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC}++;
  379. }
  380. } else {
  381. if ($file) {
  382. $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC}++;
  383. } else {
  384. $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC}++;
  385. }
  386. }
  387. } else {
  388. $perprocesspid{$process_pid}->{EVENT_UNKNOWN}++;
  389. }
  390. if ($sigint_pending) {
  391. last EVENT_PROCESS;
  392. }
  393. }
  394. }
  395. sub dump_stats {
  396. my $hashref = shift;
  397. my %stats = %$hashref;
  398. # Dump per-process stats
  399. my $process_pid;
  400. my $max_strlen = 0;
  401. # Get the maximum process name
  402. foreach $process_pid (keys %perprocesspid) {
  403. my $len = length($process_pid);
  404. if ($len > $max_strlen) {
  405. $max_strlen = $len;
  406. }
  407. }
  408. $max_strlen += 2;
  409. # Work out latencies
  410. printf("\n") if !$opt_ignorepid;
  411. printf("Reclaim latencies expressed as order-latency_in_ms\n") if !$opt_ignorepid;
  412. foreach $process_pid (keys %stats) {
  413. if (!$stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[0] &&
  414. !$stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[0]) {
  415. next;
  416. }
  417. printf "%-" . $max_strlen . "s ", $process_pid if !$opt_ignorepid;
  418. my $index = 0;
  419. while (defined $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index] ||
  420. defined $stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[$index]) {
  421. if ($stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]) {
  422. printf("%s ", $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]) if !$opt_ignorepid;
  423. my ($dummy, $latency) = split(/-/, $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]);
  424. $total_direct_latency += $latency;
  425. } else {
  426. printf("%s ", $stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[$index]) if !$opt_ignorepid;
  427. my ($dummy, $latency) = split(/-/, $stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[$index]);
  428. $total_kswapd_latency += $latency;
  429. }
  430. $index++;
  431. }
  432. print "\n" if !$opt_ignorepid;
  433. }
  434. # Print out process activity
  435. printf("\n");
  436. printf("%-" . $max_strlen . "s %8s %10s %8s %8s %8s %8s %8s %8s\n", "Process", "Direct", "Wokeup", "Pages", "Pages", "Pages", "Pages", "Time");
  437. printf("%-" . $max_strlen . "s %8s %10s %8s %8s %8s %8s %8s %8s\n", "details", "Rclms", "Kswapd", "Scanned", "Rclmed", "Sync-IO", "ASync-IO", "Stalled");
  438. foreach $process_pid (keys %stats) {
  439. if (!$stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN}) {
  440. next;
  441. }
  442. $total_direct_reclaim += $stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN};
  443. $total_wakeup_kswapd += $stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD};
  444. $total_direct_nr_scanned += $stats{$process_pid}->{HIGH_NR_SCANNED};
  445. $total_direct_nr_reclaimed += $stats{$process_pid}->{HIGH_NR_RECLAIMED};
  446. $total_direct_writepage_file_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC};
  447. $total_direct_writepage_anon_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC};
  448. $total_direct_writepage_file_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC};
  449. $total_direct_writepage_anon_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC};
  450. my $index = 0;
  451. my $this_reclaim_delay = 0;
  452. while (defined $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]) {
  453. my ($dummy, $latency) = split(/-/, $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]);
  454. $this_reclaim_delay += $latency;
  455. $index++;
  456. }
  457. printf("%-" . $max_strlen . "s %8d %10d %8u %8u %8u %8u %8.3f",
  458. $process_pid,
  459. $stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN},
  460. $stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD},
  461. $stats{$process_pid}->{HIGH_NR_SCANNED},
  462. $stats{$process_pid}->{HIGH_NR_RECLAIMED},
  463. $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC},
  464. $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC},
  465. $this_reclaim_delay / 1000);
  466. if ($stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN}) {
  467. print " ";
  468. for (my $order = 0; $order < 20; $order++) {
  469. my $count = $stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER}[$order];
  470. if ($count != 0) {
  471. print "direct-$order=$count ";
  472. }
  473. }
  474. }
  475. if ($stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD}) {
  476. print " ";
  477. for (my $order = 0; $order < 20; $order++) {
  478. my $count = $stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD_PERORDER}[$order];
  479. if ($count != 0) {
  480. print "wakeup-$order=$count ";
  481. }
  482. }
  483. }
  484. if ($stats{$process_pid}->{HIGH_NR_CONTIG_DIRTY}) {
  485. print " ";
  486. my $count = $stats{$process_pid}->{HIGH_NR_CONTIG_DIRTY};
  487. if ($count != 0) {
  488. print "contig-dirty=$count ";
  489. }
  490. }
  491. print "\n";
  492. }
  493. # Print out kswapd activity
  494. printf("\n");
  495. printf("%-" . $max_strlen . "s %8s %10s %8s %8s %8s %8s\n", "Kswapd", "Kswapd", "Order", "Pages", "Pages", "Pages", "Pages");
  496. printf("%-" . $max_strlen . "s %8s %10s %8s %8s %8s %8s\n", "Instance", "Wakeups", "Re-wakeup", "Scanned", "Rclmed", "Sync-IO", "ASync-IO");
  497. foreach $process_pid (keys %stats) {
  498. if (!$stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE}) {
  499. next;
  500. }
  501. $total_kswapd_wake += $stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE};
  502. $total_kswapd_nr_scanned += $stats{$process_pid}->{HIGH_NR_SCANNED};
  503. $total_kswapd_nr_reclaimed += $stats{$process_pid}->{HIGH_NR_RECLAIMED};
  504. $total_kswapd_writepage_file_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC};
  505. $total_kswapd_writepage_anon_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC};
  506. $total_kswapd_writepage_file_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC};
  507. $total_kswapd_writepage_anon_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC};
  508. printf("%-" . $max_strlen . "s %8d %10d %8u %8u %8i %8u",
  509. $process_pid,
  510. $stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE},
  511. $stats{$process_pid}->{HIGH_KSWAPD_REWAKEUP},
  512. $stats{$process_pid}->{HIGH_NR_SCANNED},
  513. $stats{$process_pid}->{HIGH_NR_RECLAIMED},
  514. $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC},
  515. $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC});
  516. if ($stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE}) {
  517. print " ";
  518. for (my $order = 0; $order < 20; $order++) {
  519. my $count = $stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE_PERORDER}[$order];
  520. if ($count != 0) {
  521. print "wake-$order=$count ";
  522. }
  523. }
  524. }
  525. if ($stats{$process_pid}->{HIGH_KSWAPD_REWAKEUP}) {
  526. print " ";
  527. for (my $order = 0; $order < 20; $order++) {
  528. my $count = $stats{$process_pid}->{HIGH_KSWAPD_REWAKEUP_PERORDER}[$order];
  529. if ($count != 0) {
  530. print "rewake-$order=$count ";
  531. }
  532. }
  533. }
  534. printf("\n");
  535. }
  536. # Print out summaries
  537. $total_direct_latency /= 1000;
  538. $total_kswapd_latency /= 1000;
  539. print "\nSummary\n";
  540. print "Direct reclaims: $total_direct_reclaim\n";
  541. print "Direct reclaim pages scanned: $total_direct_nr_scanned\n";
  542. print "Direct reclaim pages reclaimed: $total_direct_nr_reclaimed\n";
  543. print "Direct reclaim write file sync I/O: $total_direct_writepage_file_sync\n";
  544. print "Direct reclaim write anon sync I/O: $total_direct_writepage_anon_sync\n";
  545. print "Direct reclaim write file async I/O: $total_direct_writepage_file_async\n";
  546. print "Direct reclaim write anon async I/O: $total_direct_writepage_anon_async\n";
  547. print "Wake kswapd requests: $total_wakeup_kswapd\n";
  548. printf "Time stalled direct reclaim: %-1.2f seconds\n", $total_direct_latency;
  549. print "\n";
  550. print "Kswapd wakeups: $total_kswapd_wake\n";
  551. print "Kswapd pages scanned: $total_kswapd_nr_scanned\n";
  552. print "Kswapd pages reclaimed: $total_kswapd_nr_reclaimed\n";
  553. print "Kswapd reclaim write file sync I/O: $total_kswapd_writepage_file_sync\n";
  554. print "Kswapd reclaim write anon sync I/O: $total_kswapd_writepage_anon_sync\n";
  555. print "Kswapd reclaim write file async I/O: $total_kswapd_writepage_file_async\n";
  556. print "Kswapd reclaim write anon async I/O: $total_kswapd_writepage_anon_async\n";
  557. printf "Time kswapd awake: %-1.2f seconds\n", $total_kswapd_latency;
  558. }
  559. sub aggregate_perprocesspid() {
  560. my $process_pid;
  561. my $process;
  562. undef %perprocess;
  563. foreach $process_pid (keys %perprocesspid) {
  564. $process = $process_pid;
  565. $process =~ s/-([0-9])*$//;
  566. if ($process eq '') {
  567. $process = "NO_PROCESS_NAME";
  568. }
  569. $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN} += $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN};
  570. $perprocess{$process}->{MM_VMSCAN_KSWAPD_WAKE} += $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE};
  571. $perprocess{$process}->{MM_VMSCAN_WAKEUP_KSWAPD} += $perprocesspid{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD};
  572. $perprocess{$process}->{HIGH_KSWAPD_REWAKEUP} += $perprocesspid{$process_pid}->{HIGH_KSWAPD_REWAKEUP};
  573. $perprocess{$process}->{HIGH_NR_SCANNED} += $perprocesspid{$process_pid}->{HIGH_NR_SCANNED};
  574. $perprocess{$process}->{HIGH_NR_RECLAIMED} += $perprocesspid{$process_pid}->{HIGH_NR_RECLAIMED};
  575. $perprocess{$process}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC};
  576. $perprocess{$process}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC};
  577. $perprocess{$process}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC};
  578. $perprocess{$process}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC};
  579. for (my $order = 0; $order < 20; $order++) {
  580. $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER}[$order] += $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER}[$order];
  581. $perprocess{$process}->{MM_VMSCAN_WAKEUP_KSWAPD_PERORDER}[$order] += $perprocesspid{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD_PERORDER}[$order];
  582. $perprocess{$process}->{MM_VMSCAN_KSWAPD_WAKE_PERORDER}[$order] += $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE_PERORDER}[$order];
  583. }
  584. # Aggregate direct reclaim latencies
  585. my $wr_index = $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_END};
  586. my $rd_index = 0;
  587. while (defined $perprocesspid{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$rd_index]) {
  588. $perprocess{$process}->{HIGH_DIRECT_RECLAIM_LATENCY}[$wr_index] = $perprocesspid{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$rd_index];
  589. $rd_index++;
  590. $wr_index++;
  591. }
  592. $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_END} = $wr_index;
  593. # Aggregate kswapd latencies
  594. my $wr_index = $perprocess{$process}->{MM_VMSCAN_KSWAPD_SLEEP};
  595. my $rd_index = 0;
  596. while (defined $perprocesspid{$process_pid}->{HIGH_KSWAPD_LATENCY}[$rd_index]) {
  597. $perprocess{$process}->{HIGH_KSWAPD_LATENCY}[$wr_index] = $perprocesspid{$process_pid}->{HIGH_KSWAPD_LATENCY}[$rd_index];
  598. $rd_index++;
  599. $wr_index++;
  600. }
  601. $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_END} = $wr_index;
  602. }
  603. }
  604. sub report() {
  605. if (!$opt_ignorepid) {
  606. dump_stats(\%perprocesspid);
  607. } else {
  608. aggregate_perprocesspid();
  609. dump_stats(\%perprocess);
  610. }
  611. }
  612. # Process events or signals until neither is available
  613. sub signal_loop() {
  614. my $sigint_processed;
  615. do {
  616. $sigint_processed = 0;
  617. process_events();
  618. # Handle pending signals if any
  619. if ($sigint_pending) {
  620. my $current_time = time;
  621. if ($sigint_exit) {
  622. print "Received exit signal\n";
  623. $sigint_pending = 0;
  624. }
  625. if ($sigint_report) {
  626. if ($current_time >= $sigint_received + 2) {
  627. report();
  628. $sigint_report = 0;
  629. $sigint_pending = 0;
  630. $sigint_processed = 1;
  631. }
  632. }
  633. }
  634. } while ($sigint_pending || $sigint_processed);
  635. }
  636. signal_loop();
  637. report();