1#!/bin/bash
2#
3# Copyright OpenEmbedded Contributors
4#
5# SPDX-License-Identifier: MIT
6#
7# oe-time-dd-test records how much time it takes to
8# write <count> number of kilobytes to the filesystem.
9# It also records the number of processes that are in
10# running (R), uninterruptible sleep (D) and interruptible
11# sleep (S) state from the output of "top" command.
12# The purporse of this script is to find which part of
13# the build system puts stress on the filesystem io and
14# log all the processes.
15usage() {
16	echo "$0 is used to detect i/o latency and runs commands to display host information."
17	echo "The following commands are run in order:"
18	echo "1) top -c -b -n1 -w 512"
19	echo "2) iostat -y -z -x 5 1"
20	echo "3) tail -30 tmp*/log/cooker/*/console-latest.log to gather cooker log."
21	echo " "
22	echo "Options:"
23	echo "-c | --count <amount>		dd (transfer) <amount> KiB of data within specified timeout to detect latency."
24	echo "				Must enable -t option."
25	echo "-t | --timeout <time>		timeout in seconds for the <count> amount of data to be transferred."
26	echo "-l | --log-only			run the commands without performing the data transfer."
27	echo "-h | --help			show help"
28
29}
30
31run_cmds() {
32    echo "start: top output"
33	top -c -b -n1 -w 512
34	echo "end: top output"
35	echo "start: iostat"
36	iostat -y -z -x 5 1
37	echo "end: iostat"
38	echo "start: cooker log"
39	tail -30 tmp*/log/cooker/*/console-latest.log
40	echo "end: cooker log"
41}
42
43if [ $# -lt 1 ]; then
44	usage
45	exit 1
46fi
47
48re_c='^[0-9]+$'
49#re_t='^[0-9]+([.][0-9]+)?$'
50
51while [[ $# -gt 0 ]]; do
52	key="$1"
53
54	case $key in
55		-c|--count)
56			COUNT=$2
57			shift
58			shift
59			if ! [[ $COUNT =~ $re_c ]] || [[ $COUNT -le 0 ]] ; then
60				usage
61				exit 1
62			fi
63			;;
64		-t|--timeout)
65			TIMEOUT=$2
66			shift
67			shift
68			if ! [[ $TIMEOUT =~ $re_c ]] || [[ $TIMEOUT -le 0 ]] ; then
69				usage
70				exit 1
71			fi
72			;;
73		-l|--log-only)
74			LOG_ONLY="true"
75			shift
76			shift
77			;;
78		-h|--help)
79			usage
80			exit 0
81			;;
82		*)
83			usage
84			exit 1
85			;;
86	esac
87done
88
89
90if [ "$LOG_ONLY" = "true" ] ; then
91    uptime
92    run_cmds
93    exit
94fi
95
96if [ -z ${TIMEOUT+x} ] || [ -z ${COUNT+x} ] ; then
97    usage
98    exit 1
99fi
100
101uptime
102echo "Timeout used: ${TIMEOUT}"
103timeout ${TIMEOUT} dd if=/dev/zero of=oe-time-dd-test.dat bs=1024 count=${COUNT} conv=fsync
104if [ $? -ne 0 ]; then
105    run_cmds
106fi
107