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