/*
 *               Copyright (C) 2006, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

#include <ioslibc.h>
#include "sc.h"
#include "perf.h"

/*
 * perfReset -- reset performance statistics
 */
void
perfReset(PerfInfo *pi)
{
    pi->start = 0;
    pi->stop = 0;
    pi->min = 0;
    pi->max = 0;
    pi->min_count = 0;
    pi->max_count = 0;
    pi->sum = 0;
    pi->avg = 0;
    pi->count = 0;
}

/*
 * perfInit -- initialize performance structure
 */
void
perfInit(PerfInfo *pi, char *name) 
{
    perfReset(pi);
    strncpy(pi->name,name,sizeof(pi->name));
}

/*
 * perfStart -- start measuring a timing interval
 *              using the SYS_TIMER_REG
 */
u32
perfStart(PerfInfo *pi)
{
    pi->start = IO_READ(SYS_TIMER_REG);
    return pi->start;
}

/*
 * perfStop -- finish measuring a timing interval
 *             using the SYS_TIMER_REG
 */
u32
perfStop(PerfInfo *pi)
{
    u32 delta;

    /* read interval end time */
    pi->stop = IO_READ(SYS_TIMER_REG);
    /* computer interval length */
    if ( pi->stop >= pi->start ) {
        delta = pi->stop - pi->start;
    } else {
        delta = 0xffffffff - pi->start + pi->stop;
    }
    /* update statistics */
    pi->sum += delta;
    if ( pi->count == 0 || delta < pi->min ) {
        pi->min = delta;
        pi->min_count = pi->count;
    }
    if ( pi->count == 0 || delta > pi->max ) {
        pi->max = delta;
        pi->max_count = pi->count;
    }
    
    pi->count++;
    return delta;
}

/*
 * perfAvg -- return average interval duration in msec
 */
u32
perfAvg(PerfInfo *pi)
{
    if ( pi->count )
        pi->avg = pi->sum / pi->count;
    else
        pi->avg = 0;
    return pi->avg / PERF_TICKS_PER_MSEC;
}

/*
 * perfMax -- return maximum interval duration in msec
 */
u32
perfMax(PerfInfo *pi)
{
    return pi->max / PERF_TICKS_PER_MSEC;
}

/*
 * perfMin -- return minimum interval duration in msec
 */
u32
perfMin(PerfInfo *pi)
{
    return pi->min / PERF_TICKS_PER_MSEC;
}

/*
 * perfCount -- return number of intervals measured
 */
u32
perfCount(PerfInfo *pi)
{
    return pi->count;
}

/*
 * perfPrint -- print statistics
 */

/* compute time to 1-usec accuracy */
#define PERF_USEC(i)  ( ( ( (i) % PERF_TICKS_PER_MSEC ) * 1000 ) / PERF_TICKS_PER_MSEC )

void perfPrint(PerfInfo *pi)
{
    // sum is a 64-bit number and the available printf() doesn't support them;
    // let sum_ms be 'sum' expressed in msec, then
    // sum_ms < 2^64 / 504 ~= 3.7e16 < 1e17 < 1e9 * 1e9
    // thus, sum_ms can be printed as numbers between 0 and 1e9-1
    u64 sum_ms = pi->sum / PERF_TICKS_PER_MSEC;
    u32 sum_ms_h = sum_ms / 1e9;
    u32 sum_ms_l = sum_ms - sum_ms_h * 1e9;
    u32 sum_ms_frac = PERF_USEC(pi->sum);
    
    printf("PERF: %s: count=%d, min/max=%d.%03d@%d/%d.%03d@%d msec, sum/avg=",
           pi->name,perfCount(pi),
           perfMin(pi),PERF_USEC(pi->min),pi->min_count,
           perfMax(pi),PERF_USEC(pi->max),pi->max_count);
    if ( sum_ms_h )
        printf("%d%d.%03d",sum_ms_h,sum_ms_l,sum_ms_frac);
    else
        printf("%d.%03d",sum_ms_l,sum_ms_frac);
    printf("/%d.%03d msec\n",perfAvg(pi),PERF_USEC(pi->avg));
}

