#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# description: Test trace remote reset
# requires: remotes/test

. $TEST_DIR/remotes/functions

check_reset()
{
    write_event_path="write_event"
    taskset=""

    clear_trace

    # Is the buffer empty?
    output=$(dump_trace_pipe)
    test $(wc -l $output | cut -d ' ' -f1) -eq 0

    if $(echo $(pwd) | grep -q "per_cpu/cpu"); then
        write_event_path="../../write_event"
        cpu_id=$(echo $(pwd) | sed -e 's/.*per_cpu\/cpu//')
        taskset="taskset -c $cpu_id"
    fi
    rm $output

    # Can we properly write a new event?
    $taskset echo 7890 > $write_event_path
    output=$(dump_trace_pipe)
    test $(wc -l $output | cut -d ' ' -f1) -eq 1
    grep -q "id=7890" $output
    rm $output
}

test_global_interface()
{
    output=$(mktemp $TMPDIR/remote_test.XXXXXX)

    # Confidence check
    echo 123456 > write_event
    output=$(dump_trace_pipe)
    grep -q "id=123456" $output
    rm $output

    # Reset single event
    echo 1 > write_event
    check_reset

    # Reset lost events
    for i in $(seq 1 10000); do
        echo 1 > write_event
    done
    check_reset
}

test_percpu_interface()
{
    [ "$(get_cpu_ids | wc -l)" -ge 2 ] || return 0

    for cpu in $(get_cpu_ids); do
        taskset -c $cpu echo 1 > write_event
    done

    check_non_empty=0
    for cpu in $(get_cpu_ids); do
        cd per_cpu/cpu$cpu/

        if [ $check_non_empty -eq 0 ]; then
            check_reset
            check_non_empty=1
        else
            # Check we have only reset 1 CPU
            output=$(dump_trace_pipe)
            test $(wc -l $output | cut -d ' ' -f1) -eq 1
            rm $output
        fi
        cd -
    done
}

test_reset()
{
    test_global_interface
    test_percpu_interface
}

if [ -z "$SOURCE_REMOTE_TEST" ]; then
    set -e
    setup_remote_test
    test_reset
fi
