utils: Add trinity-trace
authorJiho Chu <jiho.chu@samsung.com>
Fri, 2 Sep 2022 02:00:23 +0000 (11:00 +0900)
committer추지호/NPU Lab(SR)/삼성전자 <jiho.chu@samsung.com>
Tue, 6 Sep 2022 04:09:41 +0000 (13:09 +0900)
trace util helps to enable/disable of trinity ftrace events.
It records events druing executable is running or sometime, and
then print out the records.

Signed-off-by: Jiho Chu <jiho.chu@samsung.com>
utils/meson.build
utils/trinity_trace/meson.build [new file with mode: 0644]
utils/trinity_trace/trinity-trace [new file with mode: 0755]

index 7dd011139bf90602f2a677cb5572b1263aba79f6..354e704af6089f98cb1e6ee79e976065ead1d10b 100644 (file)
@@ -2,3 +2,4 @@ subdir('model_inspect')
 subdir('trinity_smi')
 subdir('trinity_cuse')
 subdir('trinity_test')
+subdir('trinity_trace')
diff --git a/utils/trinity_trace/meson.build b/utils/trinity_trace/meson.build
new file mode 100644 (file)
index 0000000..810b793
--- /dev/null
@@ -0,0 +1 @@
+install_data(sources : 'trinity-trace', install_dir : join_paths(ne_bindir, 'utils'))
diff --git a/utils/trinity_trace/trinity-trace b/utils/trinity_trace/trinity-trace
new file mode 100755 (executable)
index 0000000..6a225c3
--- /dev/null
@@ -0,0 +1,104 @@
+#!/bin/bash
+
+##
+# @file   trinity-trace.sh
+# @brief  trace trinity device driver events
+# @see    https://github.sec.samsung.net/AIP/NPU_SystemService
+# @author Jiho Chu <jiho.chu@samsung.com>
+#
+
+# envs
+FTRACE_PATH=/sys/kernel/debug/tracing
+
+# global var
+TRACE_DURATION=2147483647L
+TRACE_STOP=false
+TRACE_BUFFER_SIZE=4096
+TRACE_OUTPUT=
+TRACE_EXECUTABLE=
+
+print_help() {
+       echo -e "Trace trinity device driver"
+       echo -e "Usage: $0 [-h] [-b SIZE] [-o OUTPUT] [-t TIME]"
+       echo -e "\th: show help"
+       echo -e "\tb: set trace buffer size"
+       echo -e "\to: output file path"
+       echo -e "\tt: trace duration (sec)"
+}
+
+sig_handler() {
+       echo "detect SIGINT"
+       TRACE_STOP=true
+}
+
+setup() {
+       echo 0 > $FTRACE_PATH/tracing_on
+       echo nop > $FTRACE_PATH/current_tracer
+
+       echo 0 > $FTRACE_PATH/events/enable
+       echo $TRACE_BUFFER_SIZE > $FTRACE_PATH/buffer_size_kb
+
+       echo 0 > $FTRACE_PATH/trace
+
+       trap sig_handler SIGINT
+}
+
+start() {
+       echo 1 > $FTRACE_PATH/events/trinity/enable
+       echo 1 > $FTRACE_PATH/tracing_on
+       echo "trace start"
+       
+       if [ ! -z "$TRACE_EXECUTABLE" ]; then
+               $TRACE_EXECUTABLE
+               TRACE_STOP=true
+       fi
+
+       while [ $TRACE_STOP = false ]; do
+               TRACE_DURATION=$(($TRACE_DURATION - 1))
+               [ $TRACE_DURATION -le "0" ] && break
+               sleep 1
+       done
+}
+
+stop() {
+       echo 0 > $FTRACE_PATH/tracing_on
+       echo "trace finish"
+}
+
+print_trace() {
+       if [ ! -z "$TRACE_OUTPUT" ]; then
+               cat $FTRACE_PATH/trace > "$TRACE_OUTPUT"
+               echo "trace is recorded to $TRACE_OUTPUT"
+       else
+               cat $FTRACE_PATH/trace
+       fi
+}
+
+while getopts "b:ho:p:t:" opt; do
+       case $opt in
+       b)
+               TRACE_BUFFER_SIZE=$OPTARG
+       ;;
+       h)
+               print_help
+               exit 0
+       ;;
+       o)
+               TRACE_OUTPUT=$OPTARG
+       ;;
+       p)
+               TRACE_EXECUTABLE=$OPTARG
+       ;;
+       t)
+               TRACE_DURATION=$OPTARG
+       ;;
+       esac
+done
+
+[ ! -d "$FTRACE_PATH/events/trinity" ] && echo "trinity ftrace is not enabled" && exit 1
+[ $TRACE_DURATION -gt "0" ] && echo -e "Press 'Ctrl+C' to complete trace"
+
+setup
+start
+stop
+print_trace