• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/system/bin/sh
2
3# Copyright (C) 2021 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
18# Periodically arms a perfetto trace config for mm_events
19# The config is triggered by the mm_events kmem_activity trigger
20# This script gets executed as a oneshot service from perfetto.rc
21# when persist.mm_events.enabled is set to true.
22
23DEFAULT_TRACE_CONFIG=<<EOF
24unique_session_name: "perfetto_mm_events_session"
25
26bugreport_score: 100
27
28buffers: {
29  size_kb: 512
30  fill_policy: DISCARD
31}
32
33data_sources: {
34  config {
35    name: "linux.sys_stats"
36    sys_stats_config {
37      vmstat_period_ms: 500
38      vmstat_counters: VMSTAT_NR_FREE_PAGES
39      vmstat_counters: VMSTAT_NR_SLAB_RECLAIMABLE
40      vmstat_counters: VMSTAT_NR_SLAB_UNRECLAIMABLE
41      vmstat_counters: VMSTAT_NR_ACTIVE_FILE
42      vmstat_counters: VMSTAT_NR_INACTIVE_FILE
43      vmstat_counters: VMSTAT_NR_ACTIVE_ANON
44      vmstat_counters: VMSTAT_NR_INACTIVE_ANON
45      vmstat_counters: VMSTAT_WORKINGSET_REFAULT
46      vmstat_counters: VMSTAT_WORKINGSET_ACTIVATE
47      vmstat_counters: VMSTAT_NR_FILE_PAGES
48      vmstat_counters: VMSTAT_PGPGIN
49      vmstat_counters: VMSTAT_PGPGOUT
50      vmstat_counters: VMSTAT_PSWPIN
51      vmstat_counters: VMSTAT_PSWPOUT
52      vmstat_counters: VMSTAT_PGSTEAL_KSWAPD_DMA
53      vmstat_counters: VMSTAT_PGSTEAL_KSWAPD_NORMAL
54      vmstat_counters: VMSTAT_PGSTEAL_KSWAPD_MOVABLE
55      vmstat_counters: VMSTAT_PGSTEAL_DIRECT_DMA
56      vmstat_counters: VMSTAT_PGSTEAL_DIRECT_NORMAL
57      vmstat_counters: VMSTAT_PGSTEAL_DIRECT_MOVABLE
58      vmstat_counters: VMSTAT_PGSCAN_KSWAPD_DMA
59      vmstat_counters: VMSTAT_PGSCAN_KSWAPD_NORMAL
60      vmstat_counters: VMSTAT_PGSCAN_KSWAPD_MOVABLE
61      vmstat_counters: VMSTAT_PGSCAN_DIRECT_DMA
62      vmstat_counters: VMSTAT_PGSCAN_DIRECT_NORMAL
63      vmstat_counters: VMSTAT_PGSCAN_DIRECT_MOVABLE
64      vmstat_counters: VMSTAT_COMPACT_MIGRATE_SCANNED
65      vmstat_counters: VMSTAT_COMPACT_FREE_SCANNED
66    }
67  }
68}
69
70data_sources: {
71  config {
72    name: "linux.ftrace"
73    ftrace_config {
74      ftrace_events: "vmscan/mm_vmscan_kswapd_wake"
75      ftrace_events: "vmscan/mm_vmscan_kswapd_sleep"
76      ftrace_events: "vmscan/mm_vmscan_direct_reclaim_begin"
77      ftrace_events: "vmscan/mm_vmscan_direct_reclaim_end"
78      ftrace_events: "compaction/mm_compaction_begin"
79      ftrace_events: "compaction/mm_compaction_end"
80    }
81  }
82}
83
84trigger_config {
85  trigger_mode: START_TRACING
86  trigger_timeout_ms: 3600000
87  triggers {
88    name: "kmem_activity"
89    stop_delay_ms: 360000
90  }
91}
92EOF
93
94VENDOR_TRACE_CONFIG="/vendor/etc/mm_events.cfg"
95
96BASE_SLEEP=30
97SLEEP=$BASE_SLEEP
98BACKOFF_MULTIPLIER=2
99CONSECUTIVE_FAILURES=0
100FAILURES_THRESHOLD=10
101
102# Keep the mm events perfetto trace config armed
103while :
104do
105    sleep $SLEEP
106
107    # If an alternate vendor trace config exists use that instead of the default.
108    if [ -f "$VENDOR_TRACE_CONFIG" ]; then
109        /system/bin/perfetto -c "$VENDOR_TRACE_CONFIG" --txt -o /dev/null
110        EXIT_CODE=$?
111    else
112        echo "$DEFAULT_TRACE_CONFIG" | /system/bin/perfetto -c - --txt -o /dev/null
113        EXIT_CODE=$?
114    fi
115
116    if [[ $EXIT_CODE -ne 0 ]]; then
117        SLEEP=$(($SLEEP * $BACKOFF_MULTIPLIER))
118
119        CONSECUTIVE_FAILURES=$(($CONSECUTIVE_FAILURES + 1))
120        if [ $CONSECUTIVE_FAILURES -ge $FAILURES_THRESHOLD ]; then
121            exit 1
122        fi
123    else
124        SLEEP=$BASE_SLEEP
125        CONSECUTIVE_FAILURES=0
126    fi
127done
128