#!/bin/sh

set -e

if [ "$(cat /proc/sys/kernel/perf_event_paranoid)" -gt 2 ] ; then \
    echo
    echo "***********************************************************"
    echo "error: not running test suite, /proc/sys/kernel/perf_event_paranoid value too high"
    echo "***********************************************************"
    echo
    exit 0
fi

cd $AUTOPKGTEST_TMP

cat > x.c <<'EOF'
#include <unistd.h>
#include <pthread.h>

void *workerloop(void *p)
{
    for (int i = 0; i < 10000; ++i) {
        usleep(100);
    }
}
void *workerhoop(void *p)
{
    for (int i = 0; i < 100000; ++i) {
        usleep(10);
    }
}


int main(int argc, char *argv[])
{
     pthread_t a, b, c;
     pthread_create(&a, NULL, workerloop, NULL);
     pthread_create(&b, NULL, workerhoop, NULL);
     pthread_create(&c, NULL, workerloop, NULL);
     pthread_join(a, NULL);
     pthread_join(b, NULL);
     pthread_join(c, NULL);
     return 0;
}
EOF

gcc -g -o x x.c -lpthread

coz run --- ./x

rm -f x x.c

if grep -q 'startup	time=' profile.coz ; then
    echo "success: coz ran successfully"
    cat profile.coz |sed 's/^/  /'
    rm profile.coz
else
    echo "error: coz failed to run"
    rm profile.coz
    exit 1
fi
