

global probe_id[5]

global sampling_threshold[1]
global sampling_counter[2048]
global sampling_flag[2048]

# Recursion arrays omitted

# Timed Sampling omitted
global stopwatch_on = 0

probe process("cmp").begin {
    # Probe name -> Probe ID
    probe_id["main"] = 0
    probe_id["test"] = 1
    probe_id["BEFORE_CYCLE"] = 2
    probe_id["TEST_SINGLE"] = 3
    probe_id["TEST_SINGLE2"] = 4

    # Probe name -> Probe sampling threshold
    sampling_threshold["main"] = 2

    if (!stopwatch_on) {
        stopwatch_on = 1
        start_stopwatch("timestamp")
    }
    printf("7 %d %d %d %d;%s\n", tid(), pid(), ppid(), read_stopwatch_ns("timestamp"), execname())
}

probe process("cmp").end
{
    printf("8 %d %d %d %d;%s\n", tid(), pid(), ppid(), read_stopwatch_ns("timestamp"), execname())
}


probe process("cmp").thread.begin {
    printf("5 %d %d %d;%s\n", tid(), pid(), read_stopwatch_ns("timestamp"), execname())
}
    
probe process("cmp").thread.end {
    printf("6 %d %d %d;%s\n", tid(), pid(), read_stopwatch_ns("timestamp"), execname())
    delete sampling_counter[tid(), *]
    delete sampling_flag[tid(), *]
}

probe process("cmp").function("test").call?
{
    pname = ppfunc()
    tid = tid()
    printf("0 %d %d;%d\n", tid, read_stopwatch_ns("timestamp"), probe_id[pname])
}

probe process("cmp").function("test").return?
{
    pname = ppfunc()
    tid = tid()
    printf("1 %d %d;%d\n", tid, read_stopwatch_ns("timestamp"), probe_id[pname])
}

probe process("cmp").mark("BEFORE_CYCLE")?
{
    pname = ppfunc()
    tid = tid()
    printf("3 %d %d;%d\n", tid, read_stopwatch_ns("timestamp"), probe_id[pname])
}

probe process("cmp").mark("BEFORE_CYCLE_end")?
{
    pname = ppfunc()
    tid = tid()
    printf("4 %d %d;%d\n", tid, read_stopwatch_ns("timestamp"), probe_id[pname])
}

probe process("cmp").mark("TEST_SINGLE")?,
      process("cmp").mark("TEST_SINGLE2")?
{
    pname = ppfunc()
    tid = tid()
    printf("2 %d %d;%d\n", tid, read_stopwatch_ns("timestamp"), probe_id[pname])
}

probe process("cmp").function("main").call?
{
    pname = ppfunc()
    tid = tid()
    
    counter = sampling_counter[tid, pname]
    if (counter == 0 || counter == sampling_threshold[pname]) {
        sampling_counter[tid, pname] = 0
        sampling_flag[tid, pname] ++
        printf("0 %d %d;%d\n", tid, read_stopwatch_ns("timestamp"), probe_id[pname])
    }
    sampling_counter[tid, pname] ++

}

probe process("cmp").function("main").return?
{
    pname = ppfunc()
    tid = tid()
    
    if (sampling_flag[tid, pname] > 0) {
        printf("1 %d %d;%d\n", tid, read_stopwatch_ns("timestamp"), probe_id[pname])
        sampling_flag[tid, pname] --
    }

}
