#!/usr/bin/env sh
#
# ----------------------------------------------------------------------------
#
# Copyright 2019 IBM Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ----------------------------------------------------------------------------
#

set -eu

die() { echo "$*" >&2 ; exit 1 ; }

echo "> checking events"
command -v perf >/dev/null 2>&1 || die "Error: perf is not installed"

chop=$(printenv chop)
testbin=$(printenv testbin)
testdir=$(printenv testdir)

test_event() {
    event=
    ! perf stat -e "$1" ls >/dev/null 2>&1 || event="$1"
}
    
export event
export period=1000000

test_sample() {
    for event in cycles cpu-clock task-clock; do
        rm -rf chop.db
        echo "> Testing $event"
        test_event $event
        if [ -z "$event" ]; then echo "> skip $event"; continue; fi

        echo "using $event@$period"

        set +e
        echo "> chop sample -events $event -period $period $testbin $*"
        # shellcheck disable=SC2068
        "$chop" sample -events "$event" -period "$period" "$testbin" $@ > /dev/null
        error=$?
        set -e

        if [ "$error" -ne 0 ]; then continue; fi;
        
        exec_time=10
        clock_freq=$(grep "cpu MHz static" /proc/cpuinfo | tail -n 1 | cut -d ":" -f 2)
        num_samples=$(sqlite3 chop.db 'select count(*) from sample;')
        
        expected_samples=
        case $event in
            cycles)
                expected_samples=$(echo "$clock_freq * $exec_time" | bc -l) ;;
            cpu-clock)
                expected_samples=$(echo "1000 * $exec_time" | bc -l) ;;
            task-clock)
                expected_samples=$(echo "1000 * $exec_time" | bc -l) ;;
        esac
        
        margin=0.2
        min_samples=$(echo "$expected_samples $margin" \
            | awk '{printf("%d", $1 * (1-$2))}')
        max_samples=$(echo "$expected_samples $margin" \
            | awk '{printf("%d", $1 * (1+$2))}')
        
        echo "Execution time: $exec_time seconds"
        echo "Sampling event: $event"
        echo "Sampling period: $period"
        echo "Clock frequency: $clock_freq MHz"
        echo "Number of samples: $num_samples"
        echo "Expected samples: $min_samples - $max_samples"
        
        test "$num_samples" -ge "$min_samples" || \
            die "Error: less than $min_samples samples ($num_samples)"
        test "$num_samples" -le "$max_samples" || \
            die "Error: more than $max_samples samples ($num_samples)"

        return
    done;
}

# shellcheck disable=SC1090,SC1091
. "$testdir/sample.spec"
