trace-vmscan-postprocess.pl 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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 <[email protected]>
  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]*) may_writepage=([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]*) zid=([0-9]*) order=([0-9]*)';
  104. my $regex_lru_isolate_default = 'isolate_mode=([0-9]*) order=([0-9]*) nr_requested=([0-9]*) nr_scanned=([0-9]*) nr_taken=([0-9]*) file=([0-9]*)';
  105. 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_|]*)';
  106. my $regex_lru_shrink_active_default = 'lru=([A-Z_]*) nr_scanned=([0-9]*) nr_rotated=([0-9]*) priority=([0-9]*)';
  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/debug/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", "may_writepage",
  168. "gfp_flags");
  169. $regex_direct_end = generate_traceevent_regex(
  170. "vmscan/mm_vmscan_direct_reclaim_end",
  171. $regex_direct_end_default,
  172. "nr_reclaimed");
  173. $regex_kswapd_wake = generate_traceevent_regex(
  174. "vmscan/mm_vmscan_kswapd_wake",
  175. $regex_kswapd_wake_default,
  176. "nid", "order");
  177. $regex_kswapd_sleep = generate_traceevent_regex(
  178. "vmscan/mm_vmscan_kswapd_sleep",
  179. $regex_kswapd_sleep_default,
  180. "nid");
  181. $regex_wakeup_kswapd = generate_traceevent_regex(
  182. "vmscan/mm_vmscan_wakeup_kswapd",
  183. $regex_wakeup_kswapd_default,
  184. "nid", "zid", "order");
  185. $regex_lru_isolate = generate_traceevent_regex(
  186. "vmscan/mm_vmscan_lru_isolate",
  187. $regex_lru_isolate_default,
  188. "isolate_mode", "order",
  189. "nr_requested", "nr_scanned", "nr_taken",
  190. "file");
  191. $regex_lru_shrink_inactive = generate_traceevent_regex(
  192. "vmscan/mm_vmscan_lru_shrink_inactive",
  193. $regex_lru_shrink_inactive_default,
  194. "nid", "zid",
  195. "nr_scanned", "nr_reclaimed", "priority",
  196. "flags");
  197. $regex_lru_shrink_active = generate_traceevent_regex(
  198. "vmscan/mm_vmscan_lru_shrink_active",
  199. $regex_lru_shrink_active_default,
  200. "nid", "zid",
  201. "lru",
  202. "nr_scanned", "nr_rotated", "priority");
  203. $regex_writepage = generate_traceevent_regex(
  204. "vmscan/mm_vmscan_writepage",
  205. $regex_writepage_default,
  206. "page", "pfn", "flags");
  207. sub read_statline($) {
  208. my $pid = $_[0];
  209. my $statline;
  210. if (open(STAT, "/proc/$pid/stat")) {
  211. $statline = <STAT>;
  212. close(STAT);
  213. }
  214. if ($statline eq '') {
  215. $statline = "-1 (UNKNOWN_PROCESS_NAME) R 0";
  216. }
  217. return $statline;
  218. }
  219. sub guess_process_pid($$) {
  220. my $pid = $_[0];
  221. my $statline = $_[1];
  222. if ($pid == 0) {
  223. return "swapper-0";
  224. }
  225. if ($statline !~ /$regex_statname/o) {
  226. die("Failed to math stat line for process name :: $statline");
  227. }
  228. return "$1-$pid";
  229. }
  230. # Convert sec.usec timestamp format
  231. sub timestamp_to_ms($) {
  232. my $timestamp = $_[0];
  233. my ($sec, $usec) = split (/\./, $timestamp);
  234. return ($sec * 1000) + ($usec / 1000);
  235. }
  236. sub process_events {
  237. my $traceevent;
  238. my $process_pid;
  239. my $cpus;
  240. my $timestamp;
  241. my $tracepoint;
  242. my $details;
  243. my $statline;
  244. # Read each line of the event log
  245. EVENT_PROCESS:
  246. while ($traceevent = <STDIN>) {
  247. if ($traceevent =~ /$regex_traceevent/o) {
  248. $process_pid = $1;
  249. $timestamp = $4;
  250. $tracepoint = $5;
  251. $process_pid =~ /(.*)-([0-9]*)$/;
  252. my $process = $1;
  253. my $pid = $2;
  254. if ($process eq "") {
  255. $process = $last_procmap{$pid};
  256. $process_pid = "$process-$pid";
  257. }
  258. $last_procmap{$pid} = $process;
  259. if ($opt_read_procstat) {
  260. $statline = read_statline($pid);
  261. if ($opt_read_procstat && $process eq '') {
  262. $process_pid = guess_process_pid($pid, $statline);
  263. }
  264. }
  265. } else {
  266. next;
  267. }
  268. # Perl Switch() sucks majorly
  269. if ($tracepoint eq "mm_vmscan_direct_reclaim_begin") {
  270. $timestamp = timestamp_to_ms($timestamp);
  271. $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN}++;
  272. $perprocesspid{$process_pid}->{STATE_DIRECT_BEGIN} = $timestamp;
  273. $details = $6;
  274. if ($details !~ /$regex_direct_begin/o) {
  275. print "WARNING: Failed to parse mm_vmscan_direct_reclaim_begin as expected\n";
  276. print " $details\n";
  277. print " $regex_direct_begin\n";
  278. next;
  279. }
  280. my $order = $1;
  281. $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER}[$order]++;
  282. $perprocesspid{$process_pid}->{STATE_DIRECT_ORDER} = $order;
  283. } elsif ($tracepoint eq "mm_vmscan_direct_reclaim_end") {
  284. # Count the event itself
  285. my $index = $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_END};
  286. $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_END}++;
  287. # Record how long direct reclaim took this time
  288. if (defined $perprocesspid{$process_pid}->{STATE_DIRECT_BEGIN}) {
  289. $timestamp = timestamp_to_ms($timestamp);
  290. my $order = $perprocesspid{$process_pid}->{STATE_DIRECT_ORDER};
  291. my $latency = ($timestamp - $perprocesspid{$process_pid}->{STATE_DIRECT_BEGIN});
  292. $perprocesspid{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index] = "$order-$latency";
  293. }
  294. } elsif ($tracepoint eq "mm_vmscan_kswapd_wake") {
  295. $details = $6;
  296. if ($details !~ /$regex_kswapd_wake/o) {
  297. print "WARNING: Failed to parse mm_vmscan_kswapd_wake as expected\n";
  298. print " $details\n";
  299. print " $regex_kswapd_wake\n";
  300. next;
  301. }
  302. my $order = $2;
  303. $perprocesspid{$process_pid}->{STATE_KSWAPD_ORDER} = $order;
  304. if (!$perprocesspid{$process_pid}->{STATE_KSWAPD_BEGIN}) {
  305. $timestamp = timestamp_to_ms($timestamp);
  306. $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE}++;
  307. $perprocesspid{$process_pid}->{STATE_KSWAPD_BEGIN} = $timestamp;
  308. $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE_PERORDER}[$order]++;
  309. } else {
  310. $perprocesspid{$process_pid}->{HIGH_KSWAPD_REWAKEUP}++;
  311. $perprocesspid{$process_pid}->{HIGH_KSWAPD_REWAKEUP_PERORDER}[$order]++;
  312. }
  313. } elsif ($tracepoint eq "mm_vmscan_kswapd_sleep") {
  314. # Count the event itself
  315. my $index = $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_SLEEP};
  316. $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_SLEEP}++;
  317. # Record how long kswapd was awake
  318. $timestamp = timestamp_to_ms($timestamp);
  319. my $order = $perprocesspid{$process_pid}->{STATE_KSWAPD_ORDER};
  320. my $latency = ($timestamp - $perprocesspid{$process_pid}->{STATE_KSWAPD_BEGIN});
  321. $perprocesspid{$process_pid}->{HIGH_KSWAPD_LATENCY}[$index] = "$order-$latency";
  322. $perprocesspid{$process_pid}->{STATE_KSWAPD_BEGIN} = 0;
  323. } elsif ($tracepoint eq "mm_vmscan_wakeup_kswapd") {
  324. $perprocesspid{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD}++;
  325. $details = $6;
  326. if ($details !~ /$regex_wakeup_kswapd/o) {
  327. print "WARNING: Failed to parse mm_vmscan_wakeup_kswapd as expected\n";
  328. print " $details\n";
  329. print " $regex_wakeup_kswapd\n";
  330. next;
  331. }
  332. my $order = $3;
  333. $perprocesspid{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD_PERORDER}[$order]++;
  334. } elsif ($tracepoint eq "mm_vmscan_lru_isolate") {
  335. $details = $6;
  336. if ($details !~ /$regex_lru_isolate/o) {
  337. print "WARNING: Failed to parse mm_vmscan_lru_isolate as expected\n";
  338. print " $details\n";
  339. print " $regex_lru_isolate/o\n";
  340. next;
  341. }
  342. my $isolate_mode = $1;
  343. my $nr_scanned = $4;
  344. my $file = $6;
  345. # To closer match vmstat scanning statistics, only count isolate_both
  346. # and isolate_inactive as scanning. isolate_active is rotation
  347. # isolate_inactive == 1
  348. # isolate_active == 2
  349. # isolate_both == 3
  350. if ($isolate_mode != 2) {
  351. $perprocesspid{$process_pid}->{HIGH_NR_SCANNED} += $nr_scanned;
  352. if ($file == 1) {
  353. $perprocesspid{$process_pid}->{HIGH_NR_FILE_SCANNED} += $nr_scanned;
  354. } else {
  355. $perprocesspid{$process_pid}->{HIGH_NR_ANON_SCANNED} += $nr_scanned;
  356. }
  357. }
  358. } elsif ($tracepoint eq "mm_vmscan_lru_shrink_inactive") {
  359. $details = $6;
  360. if ($details !~ /$regex_lru_shrink_inactive/o) {
  361. print "WARNING: Failed to parse mm_vmscan_lru_shrink_inactive as expected\n";
  362. print " $details\n";
  363. print " $regex_lru_shrink_inactive/o\n";
  364. next;
  365. }
  366. my $nr_reclaimed = $4;
  367. my $flags = $6;
  368. my $file = 0;
  369. if ($flags =~ /RECLAIM_WB_FILE/) {
  370. $file = 1;
  371. }
  372. $perprocesspid{$process_pid}->{HIGH_NR_RECLAIMED} += $nr_reclaimed;
  373. if ($file) {
  374. $perprocesspid{$process_pid}->{HIGH_NR_FILE_RECLAIMED} += $nr_reclaimed;
  375. } else {
  376. $perprocesspid{$process_pid}->{HIGH_NR_ANON_RECLAIMED} += $nr_reclaimed;
  377. }
  378. } elsif ($tracepoint eq "mm_vmscan_writepage") {
  379. $details = $6;
  380. if ($details !~ /$regex_writepage/o) {
  381. print "WARNING: Failed to parse mm_vmscan_writepage as expected\n";
  382. print " $details\n";
  383. print " $regex_writepage\n";
  384. next;
  385. }
  386. my $flags = $3;
  387. my $file = 0;
  388. my $sync_io = 0;
  389. if ($flags =~ /RECLAIM_WB_FILE/) {
  390. $file = 1;
  391. }
  392. if ($flags =~ /RECLAIM_WB_SYNC/) {
  393. $sync_io = 1;
  394. }
  395. if ($sync_io) {
  396. if ($file) {
  397. $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC}++;
  398. } else {
  399. $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC}++;
  400. }
  401. } else {
  402. if ($file) {
  403. $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC}++;
  404. } else {
  405. $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC}++;
  406. }
  407. }
  408. } else {
  409. $perprocesspid{$process_pid}->{EVENT_UNKNOWN}++;
  410. }
  411. if ($sigint_pending) {
  412. last EVENT_PROCESS;
  413. }
  414. }
  415. }
  416. sub dump_stats {
  417. my $hashref = shift;
  418. my %stats = %$hashref;
  419. # Dump per-process stats
  420. my $process_pid;
  421. my $max_strlen = 0;
  422. # Get the maximum process name
  423. foreach $process_pid (keys %perprocesspid) {
  424. my $len = length($process_pid);
  425. if ($len > $max_strlen) {
  426. $max_strlen = $len;
  427. }
  428. }
  429. $max_strlen += 2;
  430. # Work out latencies
  431. printf("\n") if !$opt_ignorepid;
  432. printf("Reclaim latencies expressed as order-latency_in_ms\n") if !$opt_ignorepid;
  433. foreach $process_pid (keys %stats) {
  434. if (!$stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[0] &&
  435. !$stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[0]) {
  436. next;
  437. }
  438. printf "%-" . $max_strlen . "s ", $process_pid if !$opt_ignorepid;
  439. my $index = 0;
  440. while (defined $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index] ||
  441. defined $stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[$index]) {
  442. if ($stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]) {
  443. printf("%s ", $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]) if !$opt_ignorepid;
  444. my ($dummy, $latency) = split(/-/, $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]);
  445. $total_direct_latency += $latency;
  446. } else {
  447. printf("%s ", $stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[$index]) if !$opt_ignorepid;
  448. my ($dummy, $latency) = split(/-/, $stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[$index]);
  449. $total_kswapd_latency += $latency;
  450. }
  451. $index++;
  452. }
  453. print "\n" if !$opt_ignorepid;
  454. }
  455. # Print out process activity
  456. printf("\n");
  457. printf("%-" . $max_strlen . "s %8s %10s %8s %8s %8s %8s %8s %8s\n", "Process", "Direct", "Wokeup", "Pages", "Pages", "Pages", "Pages", "Time");
  458. printf("%-" . $max_strlen . "s %8s %10s %8s %8s %8s %8s %8s %8s\n", "details", "Rclms", "Kswapd", "Scanned", "Rclmed", "Sync-IO", "ASync-IO", "Stalled");
  459. foreach $process_pid (keys %stats) {
  460. if (!$stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN}) {
  461. next;
  462. }
  463. $total_direct_reclaim += $stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN};
  464. $total_wakeup_kswapd += $stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD};
  465. $total_direct_nr_scanned += $stats{$process_pid}->{HIGH_NR_SCANNED};
  466. $total_direct_nr_file_scanned += $stats{$process_pid}->{HIGH_NR_FILE_SCANNED};
  467. $total_direct_nr_anon_scanned += $stats{$process_pid}->{HIGH_NR_ANON_SCANNED};
  468. $total_direct_nr_reclaimed += $stats{$process_pid}->{HIGH_NR_RECLAIMED};
  469. $total_direct_nr_file_reclaimed += $stats{$process_pid}->{HIGH_NR_FILE_RECLAIMED};
  470. $total_direct_nr_anon_reclaimed += $stats{$process_pid}->{HIGH_NR_ANON_RECLAIMED};
  471. $total_direct_writepage_file_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC};
  472. $total_direct_writepage_anon_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC};
  473. $total_direct_writepage_file_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC};
  474. $total_direct_writepage_anon_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC};
  475. my $index = 0;
  476. my $this_reclaim_delay = 0;
  477. while (defined $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]) {
  478. my ($dummy, $latency) = split(/-/, $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]);
  479. $this_reclaim_delay += $latency;
  480. $index++;
  481. }
  482. printf("%-" . $max_strlen . "s %8d %10d %8u %8u %8u %8u %8.3f",
  483. $process_pid,
  484. $stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN},
  485. $stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD},
  486. $stats{$process_pid}->{HIGH_NR_SCANNED},
  487. $stats{$process_pid}->{HIGH_NR_FILE_SCANNED},
  488. $stats{$process_pid}->{HIGH_NR_ANON_SCANNED},
  489. $stats{$process_pid}->{HIGH_NR_RECLAIMED},
  490. $stats{$process_pid}->{HIGH_NR_FILE_RECLAIMED},
  491. $stats{$process_pid}->{HIGH_NR_ANON_RECLAIMED},
  492. $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC},
  493. $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC},
  494. $this_reclaim_delay / 1000);
  495. if ($stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN}) {
  496. print " ";
  497. for (my $order = 0; $order < 20; $order++) {
  498. my $count = $stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER}[$order];
  499. if ($count != 0) {
  500. print "direct-$order=$count ";
  501. }
  502. }
  503. }
  504. if ($stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD}) {
  505. print " ";
  506. for (my $order = 0; $order < 20; $order++) {
  507. my $count = $stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD_PERORDER}[$order];
  508. if ($count != 0) {
  509. print "wakeup-$order=$count ";
  510. }
  511. }
  512. }
  513. print "\n";
  514. }
  515. # Print out kswapd activity
  516. printf("\n");
  517. printf("%-" . $max_strlen . "s %8s %10s %8s %8s %8s %8s\n", "Kswapd", "Kswapd", "Order", "Pages", "Pages", "Pages", "Pages");
  518. printf("%-" . $max_strlen . "s %8s %10s %8s %8s %8s %8s\n", "Instance", "Wakeups", "Re-wakeup", "Scanned", "Rclmed", "Sync-IO", "ASync-IO");
  519. foreach $process_pid (keys %stats) {
  520. if (!$stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE}) {
  521. next;
  522. }
  523. $total_kswapd_wake += $stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE};
  524. $total_kswapd_nr_scanned += $stats{$process_pid}->{HIGH_NR_SCANNED};
  525. $total_kswapd_nr_file_scanned += $stats{$process_pid}->{HIGH_NR_FILE_SCANNED};
  526. $total_kswapd_nr_anon_scanned += $stats{$process_pid}->{HIGH_NR_ANON_SCANNED};
  527. $total_kswapd_nr_reclaimed += $stats{$process_pid}->{HIGH_NR_RECLAIMED};
  528. $total_kswapd_nr_file_reclaimed += $stats{$process_pid}->{HIGH_NR_FILE_RECLAIMED};
  529. $total_kswapd_nr_anon_reclaimed += $stats{$process_pid}->{HIGH_NR_ANON_RECLAIMED};
  530. $total_kswapd_writepage_file_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC};
  531. $total_kswapd_writepage_anon_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC};
  532. $total_kswapd_writepage_file_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC};
  533. $total_kswapd_writepage_anon_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC};
  534. printf("%-" . $max_strlen . "s %8d %10d %8u %8u %8i %8u",
  535. $process_pid,
  536. $stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE},
  537. $stats{$process_pid}->{HIGH_KSWAPD_REWAKEUP},
  538. $stats{$process_pid}->{HIGH_NR_SCANNED},
  539. $stats{$process_pid}->{HIGH_NR_FILE_SCANNED},
  540. $stats{$process_pid}->{HIGH_NR_ANON_SCANNED},
  541. $stats{$process_pid}->{HIGH_NR_RECLAIMED},
  542. $stats{$process_pid}->{HIGH_NR_FILE_RECLAIMED},
  543. $stats{$process_pid}->{HIGH_NR_ANON_RECLAIMED},
  544. $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC},
  545. $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC});
  546. if ($stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE}) {
  547. print " ";
  548. for (my $order = 0; $order < 20; $order++) {
  549. my $count = $stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE_PERORDER}[$order];
  550. if ($count != 0) {
  551. print "wake-$order=$count ";
  552. }
  553. }
  554. }
  555. if ($stats{$process_pid}->{HIGH_KSWAPD_REWAKEUP}) {
  556. print " ";
  557. for (my $order = 0; $order < 20; $order++) {
  558. my $count = $stats{$process_pid}->{HIGH_KSWAPD_REWAKEUP_PERORDER}[$order];
  559. if ($count != 0) {
  560. print "rewake-$order=$count ";
  561. }
  562. }
  563. }
  564. printf("\n");
  565. }
  566. # Print out summaries
  567. $total_direct_latency /= 1000;
  568. $total_kswapd_latency /= 1000;
  569. print "\nSummary\n";
  570. print "Direct reclaims: $total_direct_reclaim\n";
  571. print "Direct reclaim pages scanned: $total_direct_nr_scanned\n";
  572. print "Direct reclaim file pages scanned: $total_direct_nr_file_scanned\n";
  573. print "Direct reclaim anon pages scanned: $total_direct_nr_anon_scanned\n";
  574. print "Direct reclaim pages reclaimed: $total_direct_nr_reclaimed\n";
  575. print "Direct reclaim file pages reclaimed: $total_direct_nr_file_reclaimed\n";
  576. print "Direct reclaim anon pages reclaimed: $total_direct_nr_anon_reclaimed\n";
  577. print "Direct reclaim write file sync I/O: $total_direct_writepage_file_sync\n";
  578. print "Direct reclaim write anon sync I/O: $total_direct_writepage_anon_sync\n";
  579. print "Direct reclaim write file async I/O: $total_direct_writepage_file_async\n";
  580. print "Direct reclaim write anon async I/O: $total_direct_writepage_anon_async\n";
  581. print "Wake kswapd requests: $total_wakeup_kswapd\n";
  582. printf "Time stalled direct reclaim: %-1.2f seconds\n", $total_direct_latency;
  583. print "\n";
  584. print "Kswapd wakeups: $total_kswapd_wake\n";
  585. print "Kswapd pages scanned: $total_kswapd_nr_scanned\n";
  586. print "Kswapd file pages scanned: $total_kswapd_nr_file_scanned\n";
  587. print "Kswapd anon pages scanned: $total_kswapd_nr_anon_scanned\n";
  588. print "Kswapd pages reclaimed: $total_kswapd_nr_reclaimed\n";
  589. print "Kswapd file pages reclaimed: $total_kswapd_nr_file_reclaimed\n";
  590. print "Kswapd anon pages reclaimed: $total_kswapd_nr_anon_reclaimed\n";
  591. print "Kswapd reclaim write file sync I/O: $total_kswapd_writepage_file_sync\n";
  592. print "Kswapd reclaim write anon sync I/O: $total_kswapd_writepage_anon_sync\n";
  593. print "Kswapd reclaim write file async I/O: $total_kswapd_writepage_file_async\n";
  594. print "Kswapd reclaim write anon async I/O: $total_kswapd_writepage_anon_async\n";
  595. printf "Time kswapd awake: %-1.2f seconds\n", $total_kswapd_latency;
  596. }
  597. sub aggregate_perprocesspid() {
  598. my $process_pid;
  599. my $process;
  600. undef %perprocess;
  601. foreach $process_pid (keys %perprocesspid) {
  602. $process = $process_pid;
  603. $process =~ s/-([0-9])*$//;
  604. if ($process eq '') {
  605. $process = "NO_PROCESS_NAME";
  606. }
  607. $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN} += $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN};
  608. $perprocess{$process}->{MM_VMSCAN_KSWAPD_WAKE} += $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE};
  609. $perprocess{$process}->{MM_VMSCAN_WAKEUP_KSWAPD} += $perprocesspid{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD};
  610. $perprocess{$process}->{HIGH_KSWAPD_REWAKEUP} += $perprocesspid{$process_pid}->{HIGH_KSWAPD_REWAKEUP};
  611. $perprocess{$process}->{HIGH_NR_SCANNED} += $perprocesspid{$process_pid}->{HIGH_NR_SCANNED};
  612. $perprocess{$process}->{HIGH_NR_FILE_SCANNED} += $perprocesspid{$process_pid}->{HIGH_NR_FILE_SCANNED};
  613. $perprocess{$process}->{HIGH_NR_ANON_SCANNED} += $perprocesspid{$process_pid}->{HIGH_NR_ANON_SCANNED};
  614. $perprocess{$process}->{HIGH_NR_RECLAIMED} += $perprocesspid{$process_pid}->{HIGH_NR_RECLAIMED};
  615. $perprocess{$process}->{HIGH_NR_FILE_RECLAIMED} += $perprocesspid{$process_pid}->{HIGH_NR_FILE_RECLAIMED};
  616. $perprocess{$process}->{HIGH_NR_ANON_RECLAIMED} += $perprocesspid{$process_pid}->{HIGH_NR_ANON_RECLAIMED};
  617. $perprocess{$process}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC};
  618. $perprocess{$process}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC};
  619. $perprocess{$process}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC};
  620. $perprocess{$process}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC};
  621. for (my $order = 0; $order < 20; $order++) {
  622. $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER}[$order] += $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER}[$order];
  623. $perprocess{$process}->{MM_VMSCAN_WAKEUP_KSWAPD_PERORDER}[$order] += $perprocesspid{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD_PERORDER}[$order];
  624. $perprocess{$process}->{MM_VMSCAN_KSWAPD_WAKE_PERORDER}[$order] += $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE_PERORDER}[$order];
  625. }
  626. # Aggregate direct reclaim latencies
  627. my $wr_index = $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_END};
  628. my $rd_index = 0;
  629. while (defined $perprocesspid{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$rd_index]) {
  630. $perprocess{$process}->{HIGH_DIRECT_RECLAIM_LATENCY}[$wr_index] = $perprocesspid{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$rd_index];
  631. $rd_index++;
  632. $wr_index++;
  633. }
  634. $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_END} = $wr_index;
  635. # Aggregate kswapd latencies
  636. my $wr_index = $perprocess{$process}->{MM_VMSCAN_KSWAPD_SLEEP};
  637. my $rd_index = 0;
  638. while (defined $perprocesspid{$process_pid}->{HIGH_KSWAPD_LATENCY}[$rd_index]) {
  639. $perprocess{$process}->{HIGH_KSWAPD_LATENCY}[$wr_index] = $perprocesspid{$process_pid}->{HIGH_KSWAPD_LATENCY}[$rd_index];
  640. $rd_index++;
  641. $wr_index++;
  642. }
  643. $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_END} = $wr_index;
  644. }
  645. }
  646. sub report() {
  647. if (!$opt_ignorepid) {
  648. dump_stats(\%perprocesspid);
  649. } else {
  650. aggregate_perprocesspid();
  651. dump_stats(\%perprocess);
  652. }
  653. }
  654. # Process events or signals until neither is available
  655. sub signal_loop() {
  656. my $sigint_processed;
  657. do {
  658. $sigint_processed = 0;
  659. process_events();
  660. # Handle pending signals if any
  661. if ($sigint_pending) {
  662. my $current_time = time;
  663. if ($sigint_exit) {
  664. print "Received exit signal\n";
  665. $sigint_pending = 0;
  666. }
  667. if ($sigint_report) {
  668. if ($current_time >= $sigint_received + 2) {
  669. report();
  670. $sigint_report = 0;
  671. $sigint_pending = 0;
  672. $sigint_processed = 1;
  673. }
  674. }
  675. }
  676. } while ($sigint_pending || $sigint_processed);
  677. }
  678. signal_loop();
  679. report();