#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# description: event tracing - enable/disable with subsystem level files
# requires: set_event events/sched/enable
# flags: instance

do_reset() {
    echo > set_event
    clear_trace
}

fail() { #msg
    echo $1
    exit_fail
}

# As reading trace can last forever, simply look for 3 different
# events then exit out of reading the file. If there's not 3 different
# events, then the test has failed.
check_unique() {
    cat trace | grep -v '^#' | awk '
	BEGIN { cnt = 0; }
	{
	    for (i = 0; i < cnt; i++) {
		if (event[i] == $5) {
		    break;
		}
	    }
	    if (i == cnt) {
		event[cnt++] = $5;
		if (cnt > 2) {
		    exit;
		}
	    }
	}
	END {
	    printf "%d", cnt;
	}'
}

echo 'sched:*' > set_event

yield

count=`check_unique`
if [ $count -lt 3 ]; then
    fail "at least fork, exec and exit events should be recorded"
fi

do_reset

echo 1 > events/sched/enable

yield

count=`check_unique`
if [ $count -lt 3 ]; then
    fail "at least fork, exec and exit events should be recorded"
fi

do_reset

echo 0 > events/sched/enable

yield

count=`head -n 100 trace | grep -v ^# | awk '{ print $5 }' | sort -u | wc -l`
if [ $count -ne 0 ]; then
    fail "any of scheduler events should not be recorded"
fi

exit 0
