1c25ce589SFinn Behrens#!/usr/bin/env perl
2b898cc70SMel Gorman# This is a POC for reading the text representation of trace output related to
3b898cc70SMel Gorman# page reclaim. It makes an attempt to extract some high-level information on
4b898cc70SMel Gorman# what is going on. The accuracy of the parser may vary
5b898cc70SMel Gorman#
6*2abfcd29SRoss Zwisler# Example usage: trace-vmscan-postprocess.pl < /sys/kernel/tracing/trace_pipe
7b898cc70SMel Gorman# other options
8b898cc70SMel Gorman#   --read-procstat	If the trace lacks process info, get it from /proc
9b898cc70SMel Gorman#   --ignore-pid	Aggregate processes of the same name together
10b898cc70SMel Gorman#
11b898cc70SMel Gorman# Copyright (c) IBM Corporation 2009
12b898cc70SMel Gorman# Author: Mel Gorman <mel@csn.ul.ie>
13b898cc70SMel Gormanuse strict;
14b898cc70SMel Gormanuse Getopt::Long;
15b898cc70SMel Gorman
16b898cc70SMel Gorman# Tracepoint events
17b898cc70SMel Gormanuse constant MM_VMSCAN_DIRECT_RECLAIM_BEGIN	=> 1;
18b898cc70SMel Gormanuse constant MM_VMSCAN_DIRECT_RECLAIM_END	=> 2;
19b898cc70SMel Gormanuse constant MM_VMSCAN_KSWAPD_WAKE		=> 3;
20b898cc70SMel Gormanuse constant MM_VMSCAN_KSWAPD_SLEEP		=> 4;
21b898cc70SMel Gormanuse constant MM_VMSCAN_LRU_SHRINK_ACTIVE	=> 5;
22b898cc70SMel Gormanuse constant MM_VMSCAN_LRU_SHRINK_INACTIVE	=> 6;
23b898cc70SMel Gormanuse constant MM_VMSCAN_LRU_ISOLATE		=> 7;
24b898cc70SMel Gormanuse constant MM_VMSCAN_WRITEPAGE_FILE_SYNC	=> 8;
25b898cc70SMel Gormanuse constant MM_VMSCAN_WRITEPAGE_ANON_SYNC	=> 9;
26b898cc70SMel Gormanuse constant MM_VMSCAN_WRITEPAGE_FILE_ASYNC	=> 10;
27b898cc70SMel Gormanuse constant MM_VMSCAN_WRITEPAGE_ANON_ASYNC	=> 11;
28b898cc70SMel Gormanuse constant MM_VMSCAN_WRITEPAGE_ASYNC		=> 12;
29b898cc70SMel Gormanuse constant EVENT_UNKNOWN			=> 13;
30b898cc70SMel Gorman
31b898cc70SMel Gorman# Per-order events
32b898cc70SMel Gormanuse constant MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER => 11;
33b898cc70SMel Gormanuse constant MM_VMSCAN_WAKEUP_KSWAPD_PERORDER 	=> 12;
34b898cc70SMel Gormanuse constant MM_VMSCAN_KSWAPD_WAKE_PERORDER	=> 13;
35b898cc70SMel Gormanuse constant HIGH_KSWAPD_REWAKEUP_PERORDER	=> 14;
36b898cc70SMel Gorman
37b898cc70SMel Gorman# Constants used to track state
38b898cc70SMel Gormanuse constant STATE_DIRECT_BEGIN 		=> 15;
39b898cc70SMel Gormanuse constant STATE_DIRECT_ORDER 		=> 16;
40b898cc70SMel Gormanuse constant STATE_KSWAPD_BEGIN			=> 17;
41b898cc70SMel Gormanuse constant STATE_KSWAPD_ORDER			=> 18;
42b898cc70SMel Gorman
43b898cc70SMel Gorman# High-level events extrapolated from tracepoints
44b898cc70SMel Gormanuse constant HIGH_DIRECT_RECLAIM_LATENCY	=> 19;
45b898cc70SMel Gormanuse constant HIGH_KSWAPD_LATENCY		=> 20;
46b898cc70SMel Gormanuse constant HIGH_KSWAPD_REWAKEUP		=> 21;
47b898cc70SMel Gormanuse constant HIGH_NR_SCANNED			=> 22;
48b898cc70SMel Gormanuse constant HIGH_NR_TAKEN			=> 23;
49e11da5b4SMel Gormanuse constant HIGH_NR_RECLAIMED			=> 24;
502c51856cSChen Yuconguse constant HIGH_NR_FILE_SCANNED		=> 25;
512c51856cSChen Yuconguse constant HIGH_NR_ANON_SCANNED		=> 26;
522c51856cSChen Yuconguse constant HIGH_NR_FILE_RECLAIMED		=> 27;
532c51856cSChen Yuconguse constant HIGH_NR_ANON_RECLAIMED		=> 28;
54b898cc70SMel Gorman
55b898cc70SMel Gormanmy %perprocesspid;
56b898cc70SMel Gormanmy %perprocess;
57b898cc70SMel Gormanmy %last_procmap;
58b898cc70SMel Gormanmy $opt_ignorepid;
59b898cc70SMel Gormanmy $opt_read_procstat;
60b898cc70SMel Gorman
61b898cc70SMel Gormanmy $total_wakeup_kswapd;
62b898cc70SMel Gormanmy ($total_direct_reclaim, $total_direct_nr_scanned);
632c51856cSChen Yucongmy ($total_direct_nr_file_scanned, $total_direct_nr_anon_scanned);
64b898cc70SMel Gormanmy ($total_direct_latency, $total_kswapd_latency);
65e11da5b4SMel Gormanmy ($total_direct_nr_reclaimed);
662c51856cSChen Yucongmy ($total_direct_nr_file_reclaimed, $total_direct_nr_anon_reclaimed);
67b898cc70SMel Gormanmy ($total_direct_writepage_file_sync, $total_direct_writepage_file_async);
68b898cc70SMel Gormanmy ($total_direct_writepage_anon_sync, $total_direct_writepage_anon_async);
69b898cc70SMel Gormanmy ($total_kswapd_nr_scanned, $total_kswapd_wake);
702c51856cSChen Yucongmy ($total_kswapd_nr_file_scanned, $total_kswapd_nr_anon_scanned);
71b898cc70SMel Gormanmy ($total_kswapd_writepage_file_sync, $total_kswapd_writepage_file_async);
72b898cc70SMel Gormanmy ($total_kswapd_writepage_anon_sync, $total_kswapd_writepage_anon_async);
73e11da5b4SMel Gormanmy ($total_kswapd_nr_reclaimed);
742c51856cSChen Yucongmy ($total_kswapd_nr_file_reclaimed, $total_kswapd_nr_anon_reclaimed);
75b898cc70SMel Gorman
76b898cc70SMel Gorman# Catch sigint and exit on request
77b898cc70SMel Gormanmy $sigint_report = 0;
78b898cc70SMel Gormanmy $sigint_exit = 0;
79b898cc70SMel Gormanmy $sigint_pending = 0;
80b898cc70SMel Gormanmy $sigint_received = 0;
81b898cc70SMel Gormansub sigint_handler {
82b898cc70SMel Gorman	my $current_time = time;
83b898cc70SMel Gorman	if ($current_time - 2 > $sigint_received) {
84b898cc70SMel Gorman		print "SIGINT received, report pending. Hit ctrl-c again to exit\n";
85b898cc70SMel Gorman		$sigint_report = 1;
86b898cc70SMel Gorman	} else {
87b898cc70SMel Gorman		if (!$sigint_exit) {
88b898cc70SMel Gorman			print "Second SIGINT received quickly, exiting\n";
89b898cc70SMel Gorman		}
90b898cc70SMel Gorman		$sigint_exit++;
91b898cc70SMel Gorman	}
92b898cc70SMel Gorman
93b898cc70SMel Gorman	if ($sigint_exit > 3) {
94b898cc70SMel Gorman		print "Many SIGINTs received, exiting now without report\n";
95b898cc70SMel Gorman		exit;
96b898cc70SMel Gorman	}
97b898cc70SMel Gorman
98b898cc70SMel Gorman	$sigint_received = $current_time;
99b898cc70SMel Gorman	$sigint_pending = 1;
100b898cc70SMel Gorman}
101b898cc70SMel Gorman$SIG{INT} = "sigint_handler";
102b898cc70SMel Gorman
103b898cc70SMel Gorman# Parse command line options
104b898cc70SMel GormanGetOptions(
105b898cc70SMel Gorman	'ignore-pid'	 =>	\$opt_ignorepid,
106b898cc70SMel Gorman	'read-procstat'	 =>	\$opt_read_procstat,
107b898cc70SMel Gorman);
108b898cc70SMel Gorman
109b898cc70SMel Gorman# Defaults for dynamically discovered regex's
110b898cc70SMel Gormanmy $regex_direct_begin_default = 'order=([0-9]*) may_writepage=([0-9]*) gfp_flags=([A-Z_|]*)';
111b898cc70SMel Gormanmy $regex_direct_end_default = 'nr_reclaimed=([0-9]*)';
112b898cc70SMel Gormanmy $regex_kswapd_wake_default = 'nid=([0-9]*) order=([0-9]*)';
113b898cc70SMel Gormanmy $regex_kswapd_sleep_default = 'nid=([0-9]*)';
1145ecd9d40SDavid Rientjesmy $regex_wakeup_kswapd_default = 'nid=([0-9]*) zid=([0-9]*) order=([0-9]*) gfp_flags=([A-Z_|]*)';
11593607e5aSMichal Hockomy $regex_lru_isolate_default = 'isolate_mode=([0-9]*) classzone_idx=([0-9]*) order=([0-9]*) nr_requested=([0-9]*) nr_scanned=([0-9]*) nr_skipped=([0-9]*) nr_taken=([0-9]*) lru=([a-z_]*)';
116886cf190SKirill Tkhaimy $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_|]*)';
117b898cc70SMel Gormanmy $regex_lru_shrink_active_default = 'lru=([A-Z_]*) nr_scanned=([0-9]*) nr_rotated=([0-9]*) priority=([0-9]*)';
118b898cc70SMel Gormanmy $regex_writepage_default = 'page=([0-9a-f]*) pfn=([0-9]*) flags=([A-Z_|]*)';
119b898cc70SMel Gorman
120b898cc70SMel Gorman# Dyanically discovered regex
121b898cc70SMel Gormanmy $regex_direct_begin;
122b898cc70SMel Gormanmy $regex_direct_end;
123b898cc70SMel Gormanmy $regex_kswapd_wake;
124b898cc70SMel Gormanmy $regex_kswapd_sleep;
125b898cc70SMel Gormanmy $regex_wakeup_kswapd;
126b898cc70SMel Gormanmy $regex_lru_isolate;
127b898cc70SMel Gormanmy $regex_lru_shrink_inactive;
128b898cc70SMel Gormanmy $regex_lru_shrink_active;
129b898cc70SMel Gormanmy $regex_writepage;
130b898cc70SMel Gorman
131b898cc70SMel Gorman# Static regex used. Specified like this for readability and for use with /o
132b898cc70SMel Gorman#                      (process_pid)     (cpus      )   ( time  )   (tpoint    ) (details)
133bd727816SVinayak Menonmy $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*(.*)';
134b898cc70SMel Gormanmy $regex_statname = '[-0-9]*\s\((.*)\).*';
135b898cc70SMel Gormanmy $regex_statppid = '[-0-9]*\s\(.*\)\s[A-Za-z]\s([0-9]*).*';
136b898cc70SMel Gorman
137b898cc70SMel Gormansub generate_traceevent_regex {
138b898cc70SMel Gorman	my $event = shift;
139b898cc70SMel Gorman	my $default = shift;
140b898cc70SMel Gorman	my $regex;
141b898cc70SMel Gorman
142b898cc70SMel Gorman	# Read the event format or use the default
143*2abfcd29SRoss Zwisler	if (!open (FORMAT, "/sys/kernel/tracing/events/$event/format")) {
144b898cc70SMel Gorman		print("WARNING: Event $event format string not found\n");
145b898cc70SMel Gorman		return $default;
146b898cc70SMel Gorman	} else {
147b898cc70SMel Gorman		my $line;
148b898cc70SMel Gorman		while (!eof(FORMAT)) {
149b898cc70SMel Gorman			$line = <FORMAT>;
150b898cc70SMel Gorman			$line =~ s/, REC->.*//;
151b898cc70SMel Gorman			if ($line =~ /^print fmt:\s"(.*)".*/) {
152b898cc70SMel Gorman				$regex = $1;
153b898cc70SMel Gorman				$regex =~ s/%s/\([0-9a-zA-Z|_]*\)/g;
154b898cc70SMel Gorman				$regex =~ s/%p/\([0-9a-f]*\)/g;
155b898cc70SMel Gorman				$regex =~ s/%d/\([-0-9]*\)/g;
156b898cc70SMel Gorman				$regex =~ s/%ld/\([-0-9]*\)/g;
157b898cc70SMel Gorman				$regex =~ s/%lu/\([0-9]*\)/g;
158b898cc70SMel Gorman			}
159b898cc70SMel Gorman		}
160b898cc70SMel Gorman	}
161b898cc70SMel Gorman
162b898cc70SMel Gorman	# Can't handle the print_flags stuff but in the context of this
163b898cc70SMel Gorman	# script, it really doesn't matter
164b898cc70SMel Gorman	$regex =~ s/\(REC.*\) \? __print_flags.*//;
165b898cc70SMel Gorman
166b898cc70SMel Gorman	# Verify fields are in the right order
167b898cc70SMel Gorman	my $tuple;
168b898cc70SMel Gorman	foreach $tuple (split /\s/, $regex) {
169b898cc70SMel Gorman		my ($key, $value) = split(/=/, $tuple);
170b898cc70SMel Gorman		my $expected = shift;
171b898cc70SMel Gorman		if ($key ne $expected) {
172b898cc70SMel Gorman			print("WARNING: Format not as expected for event $event '$key' != '$expected'\n");
173b898cc70SMel Gorman			$regex =~ s/$key=\((.*)\)/$key=$1/;
174b898cc70SMel Gorman		}
175b898cc70SMel Gorman	}
176b898cc70SMel Gorman
177b898cc70SMel Gorman	if (defined shift) {
178b898cc70SMel Gorman		die("Fewer fields than expected in format");
179b898cc70SMel Gorman	}
180b898cc70SMel Gorman
181b898cc70SMel Gorman	return $regex;
182b898cc70SMel Gorman}
183b898cc70SMel Gorman
184b898cc70SMel Gorman$regex_direct_begin = generate_traceevent_regex(
185b898cc70SMel Gorman			"vmscan/mm_vmscan_direct_reclaim_begin",
186b898cc70SMel Gorman			$regex_direct_begin_default,
187b898cc70SMel Gorman			"order", "may_writepage",
188b898cc70SMel Gorman			"gfp_flags");
189b898cc70SMel Gorman$regex_direct_end = generate_traceevent_regex(
190b898cc70SMel Gorman			"vmscan/mm_vmscan_direct_reclaim_end",
191b898cc70SMel Gorman			$regex_direct_end_default,
192b898cc70SMel Gorman			"nr_reclaimed");
193b898cc70SMel Gorman$regex_kswapd_wake = generate_traceevent_regex(
194b898cc70SMel Gorman			"vmscan/mm_vmscan_kswapd_wake",
195b898cc70SMel Gorman			$regex_kswapd_wake_default,
196b898cc70SMel Gorman			"nid", "order");
197b898cc70SMel Gorman$regex_kswapd_sleep = generate_traceevent_regex(
198b898cc70SMel Gorman			"vmscan/mm_vmscan_kswapd_sleep",
199b898cc70SMel Gorman			$regex_kswapd_sleep_default,
200b898cc70SMel Gorman			"nid");
201b898cc70SMel Gorman$regex_wakeup_kswapd = generate_traceevent_regex(
202b898cc70SMel Gorman			"vmscan/mm_vmscan_wakeup_kswapd",
203b898cc70SMel Gorman			$regex_wakeup_kswapd_default,
2045ecd9d40SDavid Rientjes			"nid", "zid", "order", "gfp_flags");
205b898cc70SMel Gorman$regex_lru_isolate = generate_traceevent_regex(
206b898cc70SMel Gorman			"vmscan/mm_vmscan_lru_isolate",
207b898cc70SMel Gorman			$regex_lru_isolate_default,
20893607e5aSMichal Hocko			"isolate_mode", "classzone_idx", "order",
20993607e5aSMichal Hocko			"nr_requested", "nr_scanned", "nr_skipped", "nr_taken",
21093607e5aSMichal Hocko			"lru");
211b898cc70SMel Gorman$regex_lru_shrink_inactive = generate_traceevent_regex(
212b898cc70SMel Gorman			"vmscan/mm_vmscan_lru_shrink_inactive",
213b898cc70SMel Gorman			$regex_lru_shrink_inactive_default,
21493607e5aSMichal Hocko			"nid", "nr_scanned", "nr_reclaimed", "nr_dirty", "nr_writeback",
215886cf190SKirill Tkhai			"nr_congested", "nr_immediate", "nr_activate_anon",
216886cf190SKirill Tkhai			"nr_activate_file", "nr_ref_keep",
21793607e5aSMichal Hocko			"nr_unmap_fail", "priority", "flags");
218b898cc70SMel Gorman$regex_lru_shrink_active = generate_traceevent_regex(
219b898cc70SMel Gorman			"vmscan/mm_vmscan_lru_shrink_active",
220b898cc70SMel Gorman			$regex_lru_shrink_active_default,
221b898cc70SMel Gorman			"nid", "zid",
222b898cc70SMel Gorman			"lru",
223b898cc70SMel Gorman			"nr_scanned", "nr_rotated", "priority");
224b898cc70SMel Gorman$regex_writepage = generate_traceevent_regex(
225b898cc70SMel Gorman			"vmscan/mm_vmscan_writepage",
226b898cc70SMel Gorman			$regex_writepage_default,
227b898cc70SMel Gorman			"page", "pfn", "flags");
228b898cc70SMel Gorman
229b898cc70SMel Gormansub read_statline($) {
230b898cc70SMel Gorman	my $pid = $_[0];
231b898cc70SMel Gorman	my $statline;
232b898cc70SMel Gorman
233b898cc70SMel Gorman	if (open(STAT, "/proc/$pid/stat")) {
234b898cc70SMel Gorman		$statline = <STAT>;
235b898cc70SMel Gorman		close(STAT);
236b898cc70SMel Gorman	}
237b898cc70SMel Gorman
238b898cc70SMel Gorman	if ($statline eq '') {
239b898cc70SMel Gorman		$statline = "-1 (UNKNOWN_PROCESS_NAME) R 0";
240b898cc70SMel Gorman	}
241b898cc70SMel Gorman
242b898cc70SMel Gorman	return $statline;
243b898cc70SMel Gorman}
244b898cc70SMel Gorman
245b898cc70SMel Gormansub guess_process_pid($$) {
246b898cc70SMel Gorman	my $pid = $_[0];
247b898cc70SMel Gorman	my $statline = $_[1];
248b898cc70SMel Gorman
249b898cc70SMel Gorman	if ($pid == 0) {
250b898cc70SMel Gorman		return "swapper-0";
251b898cc70SMel Gorman	}
252b898cc70SMel Gorman
253b898cc70SMel Gorman	if ($statline !~ /$regex_statname/o) {
254b898cc70SMel Gorman		die("Failed to math stat line for process name :: $statline");
255b898cc70SMel Gorman	}
256b898cc70SMel Gorman	return "$1-$pid";
257b898cc70SMel Gorman}
258b898cc70SMel Gorman
259b898cc70SMel Gorman# Convert sec.usec timestamp format
260b898cc70SMel Gormansub timestamp_to_ms($) {
261b898cc70SMel Gorman	my $timestamp = $_[0];
262b898cc70SMel Gorman
263b898cc70SMel Gorman	my ($sec, $usec) = split (/\./, $timestamp);
264b898cc70SMel Gorman	return ($sec * 1000) + ($usec / 1000);
265b898cc70SMel Gorman}
266b898cc70SMel Gorman
267b898cc70SMel Gormansub process_events {
268b898cc70SMel Gorman	my $traceevent;
269b898cc70SMel Gorman	my $process_pid;
270b898cc70SMel Gorman	my $cpus;
271b898cc70SMel Gorman	my $timestamp;
272b898cc70SMel Gorman	my $tracepoint;
273b898cc70SMel Gorman	my $details;
274b898cc70SMel Gorman	my $statline;
275b898cc70SMel Gorman
276b898cc70SMel Gorman	# Read each line of the event log
277b898cc70SMel GormanEVENT_PROCESS:
278b898cc70SMel Gorman	while ($traceevent = <STDIN>) {
279b898cc70SMel Gorman		if ($traceevent =~ /$regex_traceevent/o) {
280b898cc70SMel Gorman			$process_pid = $1;
281bd727816SVinayak Menon			$timestamp = $4;
282bd727816SVinayak Menon			$tracepoint = $5;
283b898cc70SMel Gorman
284b898cc70SMel Gorman			$process_pid =~ /(.*)-([0-9]*)$/;
285b898cc70SMel Gorman			my $process = $1;
286b898cc70SMel Gorman			my $pid = $2;
287b898cc70SMel Gorman
288b898cc70SMel Gorman			if ($process eq "") {
289b898cc70SMel Gorman				$process = $last_procmap{$pid};
290b898cc70SMel Gorman				$process_pid = "$process-$pid";
291b898cc70SMel Gorman			}
292b898cc70SMel Gorman			$last_procmap{$pid} = $process;
293b898cc70SMel Gorman
294b898cc70SMel Gorman			if ($opt_read_procstat) {
295b898cc70SMel Gorman				$statline = read_statline($pid);
296b898cc70SMel Gorman				if ($opt_read_procstat && $process eq '') {
297b898cc70SMel Gorman					$process_pid = guess_process_pid($pid, $statline);
298b898cc70SMel Gorman				}
299b898cc70SMel Gorman			}
300b898cc70SMel Gorman		} else {
301b898cc70SMel Gorman			next;
302b898cc70SMel Gorman		}
303b898cc70SMel Gorman
304b898cc70SMel Gorman		# Perl Switch() sucks majorly
305b898cc70SMel Gorman		if ($tracepoint eq "mm_vmscan_direct_reclaim_begin") {
306b898cc70SMel Gorman			$timestamp = timestamp_to_ms($timestamp);
307b898cc70SMel Gorman			$perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN}++;
308b898cc70SMel Gorman			$perprocesspid{$process_pid}->{STATE_DIRECT_BEGIN} = $timestamp;
309b898cc70SMel Gorman
310bd727816SVinayak Menon			$details = $6;
311b898cc70SMel Gorman			if ($details !~ /$regex_direct_begin/o) {
312b898cc70SMel Gorman				print "WARNING: Failed to parse mm_vmscan_direct_reclaim_begin as expected\n";
313b898cc70SMel Gorman				print "         $details\n";
314b898cc70SMel Gorman				print "         $regex_direct_begin\n";
315b898cc70SMel Gorman				next;
316b898cc70SMel Gorman			}
317b898cc70SMel Gorman			my $order = $1;
318b898cc70SMel Gorman			$perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER}[$order]++;
319b898cc70SMel Gorman			$perprocesspid{$process_pid}->{STATE_DIRECT_ORDER} = $order;
320b898cc70SMel Gorman		} elsif ($tracepoint eq "mm_vmscan_direct_reclaim_end") {
321b898cc70SMel Gorman			# Count the event itself
322b898cc70SMel Gorman			my $index = $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_END};
323b898cc70SMel Gorman			$perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_END}++;
324b898cc70SMel Gorman
325b898cc70SMel Gorman			# Record how long direct reclaim took this time
326b898cc70SMel Gorman			if (defined $perprocesspid{$process_pid}->{STATE_DIRECT_BEGIN}) {
327b898cc70SMel Gorman				$timestamp = timestamp_to_ms($timestamp);
328b898cc70SMel Gorman				my $order = $perprocesspid{$process_pid}->{STATE_DIRECT_ORDER};
329b898cc70SMel Gorman				my $latency = ($timestamp - $perprocesspid{$process_pid}->{STATE_DIRECT_BEGIN});
330b898cc70SMel Gorman				$perprocesspid{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index] = "$order-$latency";
331b898cc70SMel Gorman			}
332b898cc70SMel Gorman		} elsif ($tracepoint eq "mm_vmscan_kswapd_wake") {
333bd727816SVinayak Menon			$details = $6;
334b898cc70SMel Gorman			if ($details !~ /$regex_kswapd_wake/o) {
335b898cc70SMel Gorman				print "WARNING: Failed to parse mm_vmscan_kswapd_wake as expected\n";
336b898cc70SMel Gorman				print "         $details\n";
337b898cc70SMel Gorman				print "         $regex_kswapd_wake\n";
338b898cc70SMel Gorman				next;
339b898cc70SMel Gorman			}
340b898cc70SMel Gorman
341b898cc70SMel Gorman			my $order = $2;
342b898cc70SMel Gorman			$perprocesspid{$process_pid}->{STATE_KSWAPD_ORDER} = $order;
343b898cc70SMel Gorman			if (!$perprocesspid{$process_pid}->{STATE_KSWAPD_BEGIN}) {
344b898cc70SMel Gorman				$timestamp = timestamp_to_ms($timestamp);
345b898cc70SMel Gorman				$perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE}++;
346b898cc70SMel Gorman				$perprocesspid{$process_pid}->{STATE_KSWAPD_BEGIN} = $timestamp;
347b898cc70SMel Gorman				$perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE_PERORDER}[$order]++;
348b898cc70SMel Gorman			} else {
349b898cc70SMel Gorman				$perprocesspid{$process_pid}->{HIGH_KSWAPD_REWAKEUP}++;
350b898cc70SMel Gorman				$perprocesspid{$process_pid}->{HIGH_KSWAPD_REWAKEUP_PERORDER}[$order]++;
351b898cc70SMel Gorman			}
352b898cc70SMel Gorman		} elsif ($tracepoint eq "mm_vmscan_kswapd_sleep") {
353b898cc70SMel Gorman
354b898cc70SMel Gorman			# Count the event itself
355b898cc70SMel Gorman			my $index = $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_SLEEP};
356b898cc70SMel Gorman			$perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_SLEEP}++;
357b898cc70SMel Gorman
358b898cc70SMel Gorman			# Record how long kswapd was awake
359b898cc70SMel Gorman			$timestamp = timestamp_to_ms($timestamp);
360b898cc70SMel Gorman			my $order = $perprocesspid{$process_pid}->{STATE_KSWAPD_ORDER};
361b898cc70SMel Gorman			my $latency = ($timestamp - $perprocesspid{$process_pid}->{STATE_KSWAPD_BEGIN});
362b898cc70SMel Gorman			$perprocesspid{$process_pid}->{HIGH_KSWAPD_LATENCY}[$index] = "$order-$latency";
363b898cc70SMel Gorman			$perprocesspid{$process_pid}->{STATE_KSWAPD_BEGIN} = 0;
364b898cc70SMel Gorman		} elsif ($tracepoint eq "mm_vmscan_wakeup_kswapd") {
365b898cc70SMel Gorman			$perprocesspid{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD}++;
366b898cc70SMel Gorman
367bd727816SVinayak Menon			$details = $6;
368b898cc70SMel Gorman			if ($details !~ /$regex_wakeup_kswapd/o) {
369b898cc70SMel Gorman				print "WARNING: Failed to parse mm_vmscan_wakeup_kswapd as expected\n";
370b898cc70SMel Gorman				print "         $details\n";
371b898cc70SMel Gorman				print "         $regex_wakeup_kswapd\n";
372b898cc70SMel Gorman				next;
373b898cc70SMel Gorman			}
374b898cc70SMel Gorman			my $order = $3;
375b898cc70SMel Gorman			$perprocesspid{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD_PERORDER}[$order]++;
376b898cc70SMel Gorman		} elsif ($tracepoint eq "mm_vmscan_lru_isolate") {
377bd727816SVinayak Menon			$details = $6;
378b898cc70SMel Gorman			if ($details !~ /$regex_lru_isolate/o) {
379b898cc70SMel Gorman				print "WARNING: Failed to parse mm_vmscan_lru_isolate as expected\n";
380b898cc70SMel Gorman				print "         $details\n";
381b898cc70SMel Gorman				print "         $regex_lru_isolate/o\n";
382b898cc70SMel Gorman				next;
383b898cc70SMel Gorman			}
3847a2d19bcSMel Gorman			my $isolate_mode = $1;
38593607e5aSMichal Hocko			my $nr_scanned = $5;
38693607e5aSMichal Hocko			my $file = $8;
3877a2d19bcSMel Gorman
3887a2d19bcSMel Gorman			# To closer match vmstat scanning statistics, only count isolate_both
3897a2d19bcSMel Gorman			# and isolate_inactive as scanning. isolate_active is rotation
3904356f21dSMinchan Kim			# isolate_inactive == 1
3914356f21dSMinchan Kim			# isolate_active   == 2
3924356f21dSMinchan Kim			# isolate_both     == 3
3934356f21dSMinchan Kim			if ($isolate_mode != 2) {
394b898cc70SMel Gorman				$perprocesspid{$process_pid}->{HIGH_NR_SCANNED} += $nr_scanned;
39593607e5aSMichal Hocko				if ($file =~ /_file/) {
3962c51856cSChen Yucong					$perprocesspid{$process_pid}->{HIGH_NR_FILE_SCANNED} += $nr_scanned;
3972c51856cSChen Yucong				} else {
3982c51856cSChen Yucong					$perprocesspid{$process_pid}->{HIGH_NR_ANON_SCANNED} += $nr_scanned;
3992c51856cSChen Yucong				}
4007a2d19bcSMel Gorman			}
401e11da5b4SMel Gorman		} elsif ($tracepoint eq "mm_vmscan_lru_shrink_inactive") {
402bd727816SVinayak Menon			$details = $6;
403e11da5b4SMel Gorman			if ($details !~ /$regex_lru_shrink_inactive/o) {
404e11da5b4SMel Gorman				print "WARNING: Failed to parse mm_vmscan_lru_shrink_inactive as expected\n";
405e11da5b4SMel Gorman				print "         $details\n";
406e11da5b4SMel Gorman				print "         $regex_lru_shrink_inactive/o\n";
407e11da5b4SMel Gorman				next;
408e11da5b4SMel Gorman			}
4092c51856cSChen Yucong
41093607e5aSMichal Hocko			my $nr_reclaimed = $3;
411886cf190SKirill Tkhai			my $flags = $13;
4122c51856cSChen Yucong			my $file = 0;
4132c51856cSChen Yucong			if ($flags =~ /RECLAIM_WB_FILE/) {
4142c51856cSChen Yucong				$file = 1;
4152c51856cSChen Yucong			}
416e11da5b4SMel Gorman			$perprocesspid{$process_pid}->{HIGH_NR_RECLAIMED} += $nr_reclaimed;
4172c51856cSChen Yucong			if ($file) {
4182c51856cSChen Yucong				$perprocesspid{$process_pid}->{HIGH_NR_FILE_RECLAIMED} += $nr_reclaimed;
4192c51856cSChen Yucong			} else {
4202c51856cSChen Yucong				$perprocesspid{$process_pid}->{HIGH_NR_ANON_RECLAIMED} += $nr_reclaimed;
4212c51856cSChen Yucong			}
422b898cc70SMel Gorman		} elsif ($tracepoint eq "mm_vmscan_writepage") {
423bd727816SVinayak Menon			$details = $6;
424b898cc70SMel Gorman			if ($details !~ /$regex_writepage/o) {
425b898cc70SMel Gorman				print "WARNING: Failed to parse mm_vmscan_writepage as expected\n";
426b898cc70SMel Gorman				print "         $details\n";
427b898cc70SMel Gorman				print "         $regex_writepage\n";
428b898cc70SMel Gorman				next;
429b898cc70SMel Gorman			}
430b898cc70SMel Gorman
431b898cc70SMel Gorman			my $flags = $3;
432b898cc70SMel Gorman			my $file = 0;
433b898cc70SMel Gorman			my $sync_io = 0;
434b898cc70SMel Gorman			if ($flags =~ /RECLAIM_WB_FILE/) {
435b898cc70SMel Gorman				$file = 1;
436b898cc70SMel Gorman			}
437b898cc70SMel Gorman			if ($flags =~ /RECLAIM_WB_SYNC/) {
438b898cc70SMel Gorman				$sync_io = 1;
439b898cc70SMel Gorman			}
440b898cc70SMel Gorman			if ($sync_io) {
441b898cc70SMel Gorman				if ($file) {
442b898cc70SMel Gorman					$perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC}++;
443b898cc70SMel Gorman				} else {
444b898cc70SMel Gorman					$perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC}++;
445b898cc70SMel Gorman				}
446b898cc70SMel Gorman			} else {
447b898cc70SMel Gorman				if ($file) {
448b898cc70SMel Gorman					$perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC}++;
449b898cc70SMel Gorman				} else {
450b898cc70SMel Gorman					$perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC}++;
451b898cc70SMel Gorman				}
452b898cc70SMel Gorman			}
453b898cc70SMel Gorman		} else {
454b898cc70SMel Gorman			$perprocesspid{$process_pid}->{EVENT_UNKNOWN}++;
455b898cc70SMel Gorman		}
456b898cc70SMel Gorman
457b898cc70SMel Gorman		if ($sigint_pending) {
458b898cc70SMel Gorman			last EVENT_PROCESS;
459b898cc70SMel Gorman		}
460b898cc70SMel Gorman	}
461b898cc70SMel Gorman}
462b898cc70SMel Gorman
463b898cc70SMel Gormansub dump_stats {
464b898cc70SMel Gorman	my $hashref = shift;
465b898cc70SMel Gorman	my %stats = %$hashref;
466b898cc70SMel Gorman
467b898cc70SMel Gorman	# Dump per-process stats
468b898cc70SMel Gorman	my $process_pid;
469b898cc70SMel Gorman	my $max_strlen = 0;
470b898cc70SMel Gorman
471b898cc70SMel Gorman	# Get the maximum process name
472b898cc70SMel Gorman	foreach $process_pid (keys %perprocesspid) {
473b898cc70SMel Gorman		my $len = length($process_pid);
474b898cc70SMel Gorman		if ($len > $max_strlen) {
475b898cc70SMel Gorman			$max_strlen = $len;
476b898cc70SMel Gorman		}
477b898cc70SMel Gorman	}
478b898cc70SMel Gorman	$max_strlen += 2;
479b898cc70SMel Gorman
480b898cc70SMel Gorman	# Work out latencies
481b898cc70SMel Gorman	printf("\n") if !$opt_ignorepid;
482b898cc70SMel Gorman	printf("Reclaim latencies expressed as order-latency_in_ms\n") if !$opt_ignorepid;
483b898cc70SMel Gorman	foreach $process_pid (keys %stats) {
484b898cc70SMel Gorman
485b898cc70SMel Gorman		if (!$stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[0] &&
486b898cc70SMel Gorman				!$stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[0]) {
487b898cc70SMel Gorman			next;
488b898cc70SMel Gorman		}
489b898cc70SMel Gorman
490b898cc70SMel Gorman		printf "%-" . $max_strlen . "s ", $process_pid if !$opt_ignorepid;
491b898cc70SMel Gorman		my $index = 0;
492b898cc70SMel Gorman		while (defined $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index] ||
493b898cc70SMel Gorman			defined $stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[$index]) {
494b898cc70SMel Gorman
495b898cc70SMel Gorman			if ($stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]) {
496b898cc70SMel Gorman				printf("%s ", $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]) if !$opt_ignorepid;
497b898cc70SMel Gorman				my ($dummy, $latency) = split(/-/, $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]);
498b898cc70SMel Gorman				$total_direct_latency += $latency;
499b898cc70SMel Gorman			} else {
500b898cc70SMel Gorman				printf("%s ", $stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[$index]) if !$opt_ignorepid;
501b898cc70SMel Gorman				my ($dummy, $latency) = split(/-/, $stats{$process_pid}->{HIGH_KSWAPD_LATENCY}[$index]);
502b898cc70SMel Gorman				$total_kswapd_latency += $latency;
503b898cc70SMel Gorman			}
504b898cc70SMel Gorman			$index++;
505b898cc70SMel Gorman		}
506b898cc70SMel Gorman		print "\n" if !$opt_ignorepid;
507b898cc70SMel Gorman	}
508b898cc70SMel Gorman
509b898cc70SMel Gorman	# Print out process activity
510b898cc70SMel Gorman	printf("\n");
511e11da5b4SMel Gorman	printf("%-" . $max_strlen . "s %8s %10s   %8s %8s  %8s %8s %8s %8s\n", "Process", "Direct",  "Wokeup", "Pages",   "Pages",   "Pages",   "Pages",     "Time");
512e11da5b4SMel Gorman	printf("%-" . $max_strlen . "s %8s %10s   %8s %8s  %8s %8s %8s %8s\n", "details", "Rclms",   "Kswapd", "Scanned", "Rclmed",  "Sync-IO", "ASync-IO",  "Stalled");
513b898cc70SMel Gorman	foreach $process_pid (keys %stats) {
514b898cc70SMel Gorman
515b898cc70SMel Gorman		if (!$stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN}) {
516b898cc70SMel Gorman			next;
517b898cc70SMel Gorman		}
518b898cc70SMel Gorman
519b898cc70SMel Gorman		$total_direct_reclaim += $stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN};
520b898cc70SMel Gorman		$total_wakeup_kswapd += $stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD};
521b898cc70SMel Gorman		$total_direct_nr_scanned += $stats{$process_pid}->{HIGH_NR_SCANNED};
5222c51856cSChen Yucong		$total_direct_nr_file_scanned += $stats{$process_pid}->{HIGH_NR_FILE_SCANNED};
5232c51856cSChen Yucong		$total_direct_nr_anon_scanned += $stats{$process_pid}->{HIGH_NR_ANON_SCANNED};
524e11da5b4SMel Gorman		$total_direct_nr_reclaimed += $stats{$process_pid}->{HIGH_NR_RECLAIMED};
5252c51856cSChen Yucong		$total_direct_nr_file_reclaimed += $stats{$process_pid}->{HIGH_NR_FILE_RECLAIMED};
5262c51856cSChen Yucong		$total_direct_nr_anon_reclaimed += $stats{$process_pid}->{HIGH_NR_ANON_RECLAIMED};
527b898cc70SMel Gorman		$total_direct_writepage_file_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC};
528b898cc70SMel Gorman		$total_direct_writepage_anon_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC};
529b898cc70SMel Gorman		$total_direct_writepage_file_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC};
530b898cc70SMel Gorman
531b898cc70SMel Gorman		$total_direct_writepage_anon_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC};
532b898cc70SMel Gorman
533b898cc70SMel Gorman		my $index = 0;
534b898cc70SMel Gorman		my $this_reclaim_delay = 0;
535b898cc70SMel Gorman		while (defined $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]) {
536b898cc70SMel Gorman			 my ($dummy, $latency) = split(/-/, $stats{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$index]);
537b898cc70SMel Gorman			$this_reclaim_delay += $latency;
538b898cc70SMel Gorman			$index++;
539b898cc70SMel Gorman		}
540b898cc70SMel Gorman
541e11da5b4SMel Gorman		printf("%-" . $max_strlen . "s %8d %10d   %8u %8u  %8u %8u %8.3f",
542b898cc70SMel Gorman			$process_pid,
543b898cc70SMel Gorman			$stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN},
544b898cc70SMel Gorman			$stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD},
545b898cc70SMel Gorman			$stats{$process_pid}->{HIGH_NR_SCANNED},
5462c51856cSChen Yucong			$stats{$process_pid}->{HIGH_NR_FILE_SCANNED},
5472c51856cSChen Yucong			$stats{$process_pid}->{HIGH_NR_ANON_SCANNED},
548e11da5b4SMel Gorman			$stats{$process_pid}->{HIGH_NR_RECLAIMED},
5492c51856cSChen Yucong			$stats{$process_pid}->{HIGH_NR_FILE_RECLAIMED},
5502c51856cSChen Yucong			$stats{$process_pid}->{HIGH_NR_ANON_RECLAIMED},
551b898cc70SMel Gorman			$stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC},
552b898cc70SMel Gorman			$stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC},
553b898cc70SMel Gorman			$this_reclaim_delay / 1000);
554b898cc70SMel Gorman
555b898cc70SMel Gorman		if ($stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN}) {
556b898cc70SMel Gorman			print "      ";
557b898cc70SMel Gorman			for (my $order = 0; $order < 20; $order++) {
558b898cc70SMel Gorman				my $count = $stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER}[$order];
559b898cc70SMel Gorman				if ($count != 0) {
560b898cc70SMel Gorman					print "direct-$order=$count ";
561b898cc70SMel Gorman				}
562b898cc70SMel Gorman			}
563b898cc70SMel Gorman		}
564b898cc70SMel Gorman		if ($stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD}) {
565b898cc70SMel Gorman			print "      ";
566b898cc70SMel Gorman			for (my $order = 0; $order < 20; $order++) {
567b898cc70SMel Gorman				my $count = $stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD_PERORDER}[$order];
568b898cc70SMel Gorman				if ($count != 0) {
569b898cc70SMel Gorman					print "wakeup-$order=$count ";
570b898cc70SMel Gorman				}
571b898cc70SMel Gorman			}
572b898cc70SMel Gorman		}
573b898cc70SMel Gorman
574b898cc70SMel Gorman		print "\n";
575b898cc70SMel Gorman	}
576b898cc70SMel Gorman
577b898cc70SMel Gorman	# Print out kswapd activity
578b898cc70SMel Gorman	printf("\n");
579e11da5b4SMel Gorman	printf("%-" . $max_strlen . "s %8s %10s   %8s   %8s %8s %8s\n", "Kswapd",   "Kswapd",  "Order",     "Pages",   "Pages",   "Pages",  "Pages");
580e11da5b4SMel Gorman	printf("%-" . $max_strlen . "s %8s %10s   %8s   %8s %8s %8s\n", "Instance", "Wakeups", "Re-wakeup", "Scanned", "Rclmed",  "Sync-IO", "ASync-IO");
581b898cc70SMel Gorman	foreach $process_pid (keys %stats) {
582b898cc70SMel Gorman
583b898cc70SMel Gorman		if (!$stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE}) {
584b898cc70SMel Gorman			next;
585b898cc70SMel Gorman		}
586b898cc70SMel Gorman
587b898cc70SMel Gorman		$total_kswapd_wake += $stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE};
588b898cc70SMel Gorman		$total_kswapd_nr_scanned += $stats{$process_pid}->{HIGH_NR_SCANNED};
5892c51856cSChen Yucong		$total_kswapd_nr_file_scanned += $stats{$process_pid}->{HIGH_NR_FILE_SCANNED};
5902c51856cSChen Yucong		$total_kswapd_nr_anon_scanned += $stats{$process_pid}->{HIGH_NR_ANON_SCANNED};
591e11da5b4SMel Gorman		$total_kswapd_nr_reclaimed += $stats{$process_pid}->{HIGH_NR_RECLAIMED};
5922c51856cSChen Yucong		$total_kswapd_nr_file_reclaimed += $stats{$process_pid}->{HIGH_NR_FILE_RECLAIMED};
5932c51856cSChen Yucong		$total_kswapd_nr_anon_reclaimed += $stats{$process_pid}->{HIGH_NR_ANON_RECLAIMED};
594b898cc70SMel Gorman		$total_kswapd_writepage_file_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC};
595b898cc70SMel Gorman		$total_kswapd_writepage_anon_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC};
596b898cc70SMel Gorman		$total_kswapd_writepage_file_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC};
597b898cc70SMel Gorman		$total_kswapd_writepage_anon_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC};
598b898cc70SMel Gorman
599e11da5b4SMel Gorman		printf("%-" . $max_strlen . "s %8d %10d   %8u %8u  %8i %8u",
600b898cc70SMel Gorman			$process_pid,
601b898cc70SMel Gorman			$stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE},
602b898cc70SMel Gorman			$stats{$process_pid}->{HIGH_KSWAPD_REWAKEUP},
603b898cc70SMel Gorman			$stats{$process_pid}->{HIGH_NR_SCANNED},
6042c51856cSChen Yucong			$stats{$process_pid}->{HIGH_NR_FILE_SCANNED},
6052c51856cSChen Yucong			$stats{$process_pid}->{HIGH_NR_ANON_SCANNED},
606e11da5b4SMel Gorman			$stats{$process_pid}->{HIGH_NR_RECLAIMED},
6072c51856cSChen Yucong			$stats{$process_pid}->{HIGH_NR_FILE_RECLAIMED},
6082c51856cSChen Yucong			$stats{$process_pid}->{HIGH_NR_ANON_RECLAIMED},
609b898cc70SMel Gorman			$stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC},
610b898cc70SMel Gorman			$stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC});
611b898cc70SMel Gorman
612b898cc70SMel Gorman		if ($stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE}) {
613b898cc70SMel Gorman			print "      ";
614b898cc70SMel Gorman			for (my $order = 0; $order < 20; $order++) {
615b898cc70SMel Gorman				my $count = $stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE_PERORDER}[$order];
616b898cc70SMel Gorman				if ($count != 0) {
617b898cc70SMel Gorman					print "wake-$order=$count ";
618b898cc70SMel Gorman				}
619b898cc70SMel Gorman			}
620b898cc70SMel Gorman		}
621b898cc70SMel Gorman		if ($stats{$process_pid}->{HIGH_KSWAPD_REWAKEUP}) {
622b898cc70SMel Gorman			print "      ";
623b898cc70SMel Gorman			for (my $order = 0; $order < 20; $order++) {
624b898cc70SMel Gorman				my $count = $stats{$process_pid}->{HIGH_KSWAPD_REWAKEUP_PERORDER}[$order];
625b898cc70SMel Gorman				if ($count != 0) {
626b898cc70SMel Gorman					print "rewake-$order=$count ";
627b898cc70SMel Gorman				}
628b898cc70SMel Gorman			}
629b898cc70SMel Gorman		}
630b898cc70SMel Gorman		printf("\n");
631b898cc70SMel Gorman	}
632b898cc70SMel Gorman
633b898cc70SMel Gorman	# Print out summaries
634b898cc70SMel Gorman	$total_direct_latency /= 1000;
635b898cc70SMel Gorman	$total_kswapd_latency /= 1000;
636b898cc70SMel Gorman	print "\nSummary\n";
637b898cc70SMel Gorman	print "Direct reclaims:     			$total_direct_reclaim\n";
638b898cc70SMel Gorman	print "Direct reclaim pages scanned:		$total_direct_nr_scanned\n";
6392c51856cSChen Yucong	print "Direct reclaim file pages scanned:	$total_direct_nr_file_scanned\n";
6402c51856cSChen Yucong	print "Direct reclaim anon pages scanned:	$total_direct_nr_anon_scanned\n";
641e11da5b4SMel Gorman	print "Direct reclaim pages reclaimed:		$total_direct_nr_reclaimed\n";
6422c51856cSChen Yucong	print "Direct reclaim file pages reclaimed:	$total_direct_nr_file_reclaimed\n";
6432c51856cSChen Yucong	print "Direct reclaim anon pages reclaimed:	$total_direct_nr_anon_reclaimed\n";
644b898cc70SMel Gorman	print "Direct reclaim write file sync I/O:	$total_direct_writepage_file_sync\n";
645b898cc70SMel Gorman	print "Direct reclaim write anon sync I/O:	$total_direct_writepage_anon_sync\n";
646b898cc70SMel Gorman	print "Direct reclaim write file async I/O:	$total_direct_writepage_file_async\n";
647b898cc70SMel Gorman	print "Direct reclaim write anon async I/O:	$total_direct_writepage_anon_async\n";
648b898cc70SMel Gorman	print "Wake kswapd requests:			$total_wakeup_kswapd\n";
649b898cc70SMel Gorman	printf "Time stalled direct reclaim: 		%-1.2f seconds\n", $total_direct_latency;
650b898cc70SMel Gorman	print "\n";
651b898cc70SMel Gorman	print "Kswapd wakeups:				$total_kswapd_wake\n";
652b898cc70SMel Gorman	print "Kswapd pages scanned:			$total_kswapd_nr_scanned\n";
6532c51856cSChen Yucong	print "Kswapd file pages scanned:		$total_kswapd_nr_file_scanned\n";
6542c51856cSChen Yucong	print "Kswapd anon pages scanned:		$total_kswapd_nr_anon_scanned\n";
655e11da5b4SMel Gorman	print "Kswapd pages reclaimed:			$total_kswapd_nr_reclaimed\n";
6562c51856cSChen Yucong	print "Kswapd file pages reclaimed:		$total_kswapd_nr_file_reclaimed\n";
6572c51856cSChen Yucong	print "Kswapd anon pages reclaimed:		$total_kswapd_nr_anon_reclaimed\n";
658b898cc70SMel Gorman	print "Kswapd reclaim write file sync I/O:	$total_kswapd_writepage_file_sync\n";
659b898cc70SMel Gorman	print "Kswapd reclaim write anon sync I/O:	$total_kswapd_writepage_anon_sync\n";
660b898cc70SMel Gorman	print "Kswapd reclaim write file async I/O:	$total_kswapd_writepage_file_async\n";
661b898cc70SMel Gorman	print "Kswapd reclaim write anon async I/O:	$total_kswapd_writepage_anon_async\n";
662b898cc70SMel Gorman	printf "Time kswapd awake:			%-1.2f seconds\n", $total_kswapd_latency;
663b898cc70SMel Gorman}
664b898cc70SMel Gorman
665b898cc70SMel Gormansub aggregate_perprocesspid() {
666b898cc70SMel Gorman	my $process_pid;
667b898cc70SMel Gorman	my $process;
668b898cc70SMel Gorman	undef %perprocess;
669b898cc70SMel Gorman
670b898cc70SMel Gorman	foreach $process_pid (keys %perprocesspid) {
671b898cc70SMel Gorman		$process = $process_pid;
672b898cc70SMel Gorman		$process =~ s/-([0-9])*$//;
673b898cc70SMel Gorman		if ($process eq '') {
674b898cc70SMel Gorman			$process = "NO_PROCESS_NAME";
675b898cc70SMel Gorman		}
676b898cc70SMel Gorman
677b898cc70SMel Gorman		$perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN} += $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN};
678b898cc70SMel Gorman		$perprocess{$process}->{MM_VMSCAN_KSWAPD_WAKE} += $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE};
679b898cc70SMel Gorman		$perprocess{$process}->{MM_VMSCAN_WAKEUP_KSWAPD} += $perprocesspid{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD};
680b898cc70SMel Gorman		$perprocess{$process}->{HIGH_KSWAPD_REWAKEUP} += $perprocesspid{$process_pid}->{HIGH_KSWAPD_REWAKEUP};
681b898cc70SMel Gorman		$perprocess{$process}->{HIGH_NR_SCANNED} += $perprocesspid{$process_pid}->{HIGH_NR_SCANNED};
6822c51856cSChen Yucong		$perprocess{$process}->{HIGH_NR_FILE_SCANNED} += $perprocesspid{$process_pid}->{HIGH_NR_FILE_SCANNED};
6832c51856cSChen Yucong		$perprocess{$process}->{HIGH_NR_ANON_SCANNED} += $perprocesspid{$process_pid}->{HIGH_NR_ANON_SCANNED};
684e11da5b4SMel Gorman		$perprocess{$process}->{HIGH_NR_RECLAIMED} += $perprocesspid{$process_pid}->{HIGH_NR_RECLAIMED};
6852c51856cSChen Yucong		$perprocess{$process}->{HIGH_NR_FILE_RECLAIMED} += $perprocesspid{$process_pid}->{HIGH_NR_FILE_RECLAIMED};
6862c51856cSChen Yucong		$perprocess{$process}->{HIGH_NR_ANON_RECLAIMED} += $perprocesspid{$process_pid}->{HIGH_NR_ANON_RECLAIMED};
687b898cc70SMel Gorman		$perprocess{$process}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC};
688b898cc70SMel Gorman		$perprocess{$process}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC};
689b898cc70SMel Gorman		$perprocess{$process}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC};
690b898cc70SMel Gorman		$perprocess{$process}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC};
691b898cc70SMel Gorman
692b898cc70SMel Gorman		for (my $order = 0; $order < 20; $order++) {
693b898cc70SMel Gorman			$perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER}[$order] += $perprocesspid{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN_PERORDER}[$order];
694b898cc70SMel Gorman			$perprocess{$process}->{MM_VMSCAN_WAKEUP_KSWAPD_PERORDER}[$order] += $perprocesspid{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD_PERORDER}[$order];
695b898cc70SMel Gorman			$perprocess{$process}->{MM_VMSCAN_KSWAPD_WAKE_PERORDER}[$order] += $perprocesspid{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE_PERORDER}[$order];
696b898cc70SMel Gorman
697b898cc70SMel Gorman		}
698b898cc70SMel Gorman
699b898cc70SMel Gorman		# Aggregate direct reclaim latencies
700b898cc70SMel Gorman		my $wr_index = $perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_END};
701b898cc70SMel Gorman		my $rd_index = 0;
702b898cc70SMel Gorman		while (defined $perprocesspid{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$rd_index]) {
703b898cc70SMel Gorman			$perprocess{$process}->{HIGH_DIRECT_RECLAIM_LATENCY}[$wr_index] = $perprocesspid{$process_pid}->{HIGH_DIRECT_RECLAIM_LATENCY}[$rd_index];
704b898cc70SMel Gorman			$rd_index++;
705b898cc70SMel Gorman			$wr_index++;
706b898cc70SMel Gorman		}
707b898cc70SMel Gorman		$perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_END} = $wr_index;
708b898cc70SMel Gorman
709b898cc70SMel Gorman		# Aggregate kswapd latencies
710b898cc70SMel Gorman		my $wr_index = $perprocess{$process}->{MM_VMSCAN_KSWAPD_SLEEP};
711b898cc70SMel Gorman		my $rd_index = 0;
712b898cc70SMel Gorman		while (defined $perprocesspid{$process_pid}->{HIGH_KSWAPD_LATENCY}[$rd_index]) {
713b898cc70SMel Gorman			$perprocess{$process}->{HIGH_KSWAPD_LATENCY}[$wr_index] = $perprocesspid{$process_pid}->{HIGH_KSWAPD_LATENCY}[$rd_index];
714b898cc70SMel Gorman			$rd_index++;
715b898cc70SMel Gorman			$wr_index++;
716b898cc70SMel Gorman		}
717b898cc70SMel Gorman		$perprocess{$process}->{MM_VMSCAN_DIRECT_RECLAIM_END} = $wr_index;
718b898cc70SMel Gorman	}
719b898cc70SMel Gorman}
720b898cc70SMel Gorman
721b898cc70SMel Gormansub report() {
722b898cc70SMel Gorman	if (!$opt_ignorepid) {
723b898cc70SMel Gorman		dump_stats(\%perprocesspid);
724b898cc70SMel Gorman	} else {
725b898cc70SMel Gorman		aggregate_perprocesspid();
726b898cc70SMel Gorman		dump_stats(\%perprocess);
727b898cc70SMel Gorman	}
728b898cc70SMel Gorman}
729b898cc70SMel Gorman
730b898cc70SMel Gorman# Process events or signals until neither is available
731b898cc70SMel Gormansub signal_loop() {
732b898cc70SMel Gorman	my $sigint_processed;
733b898cc70SMel Gorman	do {
734b898cc70SMel Gorman		$sigint_processed = 0;
735b898cc70SMel Gorman		process_events();
736b898cc70SMel Gorman
737b898cc70SMel Gorman		# Handle pending signals if any
738b898cc70SMel Gorman		if ($sigint_pending) {
739b898cc70SMel Gorman			my $current_time = time;
740b898cc70SMel Gorman
741b898cc70SMel Gorman			if ($sigint_exit) {
742b898cc70SMel Gorman				print "Received exit signal\n";
743b898cc70SMel Gorman				$sigint_pending = 0;
744b898cc70SMel Gorman			}
745b898cc70SMel Gorman			if ($sigint_report) {
746b898cc70SMel Gorman				if ($current_time >= $sigint_received + 2) {
747b898cc70SMel Gorman					report();
748b898cc70SMel Gorman					$sigint_report = 0;
749b898cc70SMel Gorman					$sigint_pending = 0;
750b898cc70SMel Gorman					$sigint_processed = 1;
751b898cc70SMel Gorman				}
752b898cc70SMel Gorman			}
753b898cc70SMel Gorman		}
754b898cc70SMel Gorman	} while ($sigint_pending || $sigint_processed);
755b898cc70SMel Gorman}
756b898cc70SMel Gorman
757b898cc70SMel Gormansignal_loop();
758b898cc70SMel Gormanreport();
759