1b1994897Sopenharmony_ci#!/bin/bash
2b1994897Sopenharmony_ci# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3b1994897Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
4b1994897Sopenharmony_ci# you may not use this file except in compliance with the License.
5b1994897Sopenharmony_ci# You may obtain a copy of the License at
6b1994897Sopenharmony_ci#
7b1994897Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
8b1994897Sopenharmony_ci#
9b1994897Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
10b1994897Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
11b1994897Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1994897Sopenharmony_ci# See the License for the specific language governing permissions and
13b1994897Sopenharmony_ci# limitations under the License.
14b1994897Sopenharmony_ci
15b1994897Sopenharmony_ciset -e
16b1994897Sopenharmony_ci
17b1994897Sopenharmony_ciTRACKING_PATH=/sys/kernel/debug/tracing
18b1994897Sopenharmony_ci
19b1994897Sopenharmony_ciOUTPUT=$1
20b1994897Sopenharmony_ci
21b1994897Sopenharmony_ciprint_usage() {
22b1994897Sopenharmony_ci    echo "Usage: $0 <trace_output> <tracing_time> <trace_buffer_size_kb>"
23b1994897Sopenharmony_ci}
24b1994897Sopenharmony_ci
25b1994897Sopenharmony_ci# Set write permission for all users on trace_marker
26b1994897Sopenharmony_cisetup_permissions() {
27b1994897Sopenharmony_ci    chmod +rx $(dirname $TRACKING_PATH)
28b1994897Sopenharmony_ci    chmod +rx $TRACKING_PATH
29b1994897Sopenharmony_ci    chmod o+w $TRACKING_PATH/trace_marker
30b1994897Sopenharmony_ci}
31b1994897Sopenharmony_ci
32b1994897Sopenharmony_ciclear_trace() {
33b1994897Sopenharmony_ci    echo > $TRACKING_PATH/trace
34b1994897Sopenharmony_ci}
35b1994897Sopenharmony_ci
36b1994897Sopenharmony_cicategory_enable() {
37b1994897Sopenharmony_ci    echo 1 > $TRACKING_PATH/events/sched/sched_switch/enable
38b1994897Sopenharmony_ci    echo 1 > $TRACKING_PATH/events/sched/sched_wakeup/enable
39b1994897Sopenharmony_ci}
40b1994897Sopenharmony_ci
41b1994897Sopenharmony_cicategory_disable() {
42b1994897Sopenharmony_ci    echo 0 > $TRACKING_PATH/events/sched/sched_switch/enable
43b1994897Sopenharmony_ci    echo 0 > $TRACKING_PATH/events/sched/sched_wakeup/enable
44b1994897Sopenharmony_ci}
45b1994897Sopenharmony_ci
46b1994897Sopenharmony_cistart_trace() {
47b1994897Sopenharmony_ci    echo "Warning: If the PandaVM aborted while writting trace, try to enlarge the trace buffer size here."
48b1994897Sopenharmony_ci    echo $BUFF_SIZE > $TRACKING_PATH/buffer_size_kb
49b1994897Sopenharmony_ci    echo 'global' > $TRACKING_PATH/trace_clock
50b1994897Sopenharmony_ci    echo 'nop' > $TRACKING_PATH/current_tracer
51b1994897Sopenharmony_ci    echo 0 > $TRACKING_PATH/options/overwrite
52b1994897Sopenharmony_ci
53b1994897Sopenharmony_ci    category_enable
54b1994897Sopenharmony_ci    clear_trace
55b1994897Sopenharmony_ci    echo 1 > $TRACKING_PATH/tracing_on
56b1994897Sopenharmony_ci}
57b1994897Sopenharmony_ci
58b1994897Sopenharmony_cidump_trace() {
59b1994897Sopenharmony_ci    cat $TRACKING_PATH/trace > $OUTPUT
60b1994897Sopenharmony_ci    chmod o+w $OUTPUT
61b1994897Sopenharmony_ci    clear_trace
62b1994897Sopenharmony_ci}
63b1994897Sopenharmony_ci
64b1994897Sopenharmony_cistop_trace() {
65b1994897Sopenharmony_ci    echo 0 > $TRACKING_PATH/tracing_on
66b1994897Sopenharmony_ci    category_disable
67b1994897Sopenharmony_ci    dump_trace
68b1994897Sopenharmony_ci    echo 4 > $TRACKING_PATH/buffer_size_kb
69b1994897Sopenharmony_ci}
70b1994897Sopenharmony_ci
71b1994897Sopenharmony_cisigint_handler() {
72b1994897Sopenharmony_ci    stop_trace
73b1994897Sopenharmony_ci    echo
74b1994897Sopenharmony_ci    echo "Stopped"
75b1994897Sopenharmony_ci    exit 0
76b1994897Sopenharmony_ci}
77b1994897Sopenharmony_ci
78b1994897Sopenharmony_ci# main
79b1994897Sopenharmony_ci
80b1994897Sopenharmony_ciif [ $(id -u) != 0 ] ; then
81b1994897Sopenharmony_ci    echo 'Run this script as root or use sudo';
82b1994897Sopenharmony_ci    exit 1;
83b1994897Sopenharmony_cifi;
84b1994897Sopenharmony_ci
85b1994897Sopenharmony_ciif [ -z "$OUTPUT" ] || [ ! -z "$4" ]; then
86b1994897Sopenharmony_ci    print_usage
87b1994897Sopenharmony_ci    exit 1
88b1994897Sopenharmony_cifi;
89b1994897Sopenharmony_ci
90b1994897Sopenharmony_ciTIME_TO_WAIT=$2
91b1994897Sopenharmony_ci
92b1994897Sopenharmony_ciif [ -z "$2" ]; then
93b1994897Sopenharmony_ci    echo "Warning: tracing time is not specified, set to 60 seconds"
94b1994897Sopenharmony_ci    TIME_TO_WAIT=60
95b1994897Sopenharmony_cifi;
96b1994897Sopenharmony_ci
97b1994897Sopenharmony_ciBUFF_SIZE=$3
98b1994897Sopenharmony_ci
99b1994897Sopenharmony_ciif [ -z "$3" ]; then
100b1994897Sopenharmony_ci    echo "Warning: trace buffer size is not specified, set to 8 MB"
101b1994897Sopenharmony_ci    BUFF_SIZE=8192
102b1994897Sopenharmony_cifi;
103b1994897Sopenharmony_ci
104b1994897Sopenharmony_cisetup_permissions
105b1994897Sopenharmony_citrap sigint_handler INT # Tracing will be interrupted on SIGINT
106b1994897Sopenharmony_ci
107b1994897Sopenharmony_cistart_trace
108b1994897Sopenharmony_ciecho "Tracing has started. Press ^C to finish"
109b1994897Sopenharmony_ci
110b1994897Sopenharmony_cisleep $TIME_TO_WAIT
111b1994897Sopenharmony_ci
112b1994897Sopenharmony_cistop_trace
113b1994897Sopenharmony_ciecho "Stopped by timeout"
114