trace-vmscan-postprocess.pl 28 KB

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