/*
 *               Copyright (C) 2005, 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 <iostypes.h>
#include <ioslibc.h>
#include <sc/sc.h>
#include "thread.h"
#include "message.h"
#include "timer.h"
#include "resource.h"
#include "memory.h"

#if defined(GET_USAGE)
char *__processes[] = {"kernel", "eticket", "fs", "usb", "audio", "viewer", "nc", "gba", "vn", "devmon", "ec"};
#endif

IOSError
iosGetUsage(u32 flags, char *buf, u32 len)
{
#if defined(GET_USAGE)
    char *start = buf;
    char *end = start + len;

    /* Run as TID 0 to prevent access violations */
    u32 tid = SYS_SMP_TID(IO_READ(SYS_SMP_REG));
    IO_WRITE(SYS_SMP_REG, SYS_SMP_SET_TID);
    IO_READ(DAX_START); /* Memory barrier */

    if (flags & IOS_USAGE_THREADS) {
        start = __iosGetThreadsUsage(start, end);
    }
    if (flags & IOS_USAGE_QUEUES) {
        start = __iosGetQueuesUsage(start, end);
    }
    if (flags & IOS_USAGE_TIMERS) {
        start = __iosGetTimersUsage(start, end);
    }
    if (flags & IOS_USAGE_RESOURCES) {
        start = __iosGetResourcesUsage(start, end);
    }
    if (flags & IOS_USAGE_HEAPS) {
        start = __iosGetHeapsUsage(start, end);
    }

    IO_WRITE(SYS_SMP_REG, SYS_SMP_SET_TID | (tid << SYS_SMP_TID_SHIFT));
#endif
    return IOS_ERROR_OK;
}
