#include <aos/debug.h>
#include <aos/performance.h>
#include <aos/systime.h>
#include <string.h>

void perf_init(struct performance_context *c, const char *name) {
    c->count = 0;
    c->name = name;
}

inline void perf_add_measurement(struct performance_context *c, const char *tag, systime_t timestamp)
{
    assert(c->count < PERFORMANCE_MEASUREMENT_COUNT_MAX);
    
    __asm volatile (
        "dmb sy\n"
    );
    c->measurements[c->count].tag = tag;
    c->measurements[c->count].timestamp = timestamp;
    c->count++;
    __asm volatile (
        "dmb sy\n"
    );
}

inline void perf_add_now(struct performance_context *c, const char *tag)
{
    systime_t now = systime_now();
    perf_add_measurement(c, tag, now);
}

void perf_print(struct performance_context *c)
{
    for (size_t i = 0; i < c->count; i++) {
        debug_printf("MEASUREMENT %s:%s %lu\n", c->name, c->measurements[i].tag, c->measurements[i].timestamp);
    }
}
