/*========================== begin_copyright_notice ============================
Copyright (C) 2021-2024 Intel Corporation

SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/

/*******************************************************************************************************
 * ICOUNT tool
 *
 * Count dynamic instructions within the trace.
 *
 * NOTE: the tool callbacks might be called from different threads.
 */
#include <stdio.h>
#include <string.h>
#include <vector>

#include "gtreplay_assert.h"
#include "gtreplay_client.h"
#include "knob_parser.h"


// Global variables
uint32_t gMaxNumOfHwThreads = 0;
uint32_t gMaxNumOfTiles = 0;
std::vector<std::vector<uint64_t>> icount;
std::string kernelName;

/*
 * BeforeInsCallback - callback called before instruction execution
 *
 * @params[in] tid - the ID of the GPU HW thread for which the callback is called
 * @params[in] ins - a handle to the current instruction
 * @params[in] state - a handle to the HW Thread state corresponding to tid
 */
void BeforeInsCallback(uint32_t tileId, uint32_t tid, GTReplayIns ins, GTReplayState state, void*)
{
    GTREPLAY_ASSERT(tileId < gMaxNumOfTiles && tid < gMaxNumOfHwThreads);
    // Update the instruction counter corresponding to the current HW thread
    icount[tileId][tid]++;
}

/*
 * AfterInsCallback - callback called after instruction execution
 *                    An illustration - this tool doesn't need to register after instruction callbacks
 *
 * @params[in] tid - the ID of the GPU HW thread for which the callback is called
 * @params[in] ins - a handle to the current instruction
 * @params[in] state - a handle to the HW Thread state corresponding to tid
 */
void AfterInsCallback(uint32_t tileId, uint32_t tid, GTReplayIns ins, GTReplayState state, void*)
{
}

/*
 * OnKernelComplete - callback called upon kernel completion
 *
 * @params[in] kernel - a handle to the kernel
 */
void OnKernelComplete(GTReplayKernel kernel)
{
    uint64_t count = 0;

    // Accumulate counters from all HW threads
    for (uint32_t t = 0; t < gMaxNumOfTiles; t++)
    {
        for (uint32_t i = 0; i < gMaxNumOfHwThreads; i++)
        {
            count += icount[t][i];
        }
    }

    // Print the results
    std::cout << "\n\n=================\n";
    std::cout << "ICOUNT TOOL\n";
    std::cout << "=================\n\n";
    std::cout.imbue(std::locale(""));
    std::cout << "Kernel: " << kernelName << "\n\n";
    std::cout << "TOTAL ICOUNT = " << count << "\n";
}

/*
 * OnKernelBuild - callback called before kernel execution
 *                 The purpose of this callback is to traverse the kernel binary and instrument callbacks
 *
 * @params[in] kernel - a handle to the kernel
 */
void OnKernelBuild(GTReplayKernel kernel)
{
    uint32_t gModelId = GTReplay_GetModel(kernel);

    gMaxNumOfHwThreads = GTReplay_MaxNumOfHWThreads(gModelId);

    gMaxNumOfTiles = GTReplay_MaxNumOfTiles(kernel);
    GTREPLAY_ASSERT(gMaxNumOfTiles);

    // Allocate a buffer of counters - one per each possible HW thread
    icount.resize(gMaxNumOfTiles);
    for (uint32_t i = 0; i < gMaxNumOfTiles; i++)
    {
        icount[i].resize(gMaxNumOfHwThreads, 0);
    }

    // Traverse all the basic blocks 
    for (GTReplayBbl bbl = GTReplay_BblHead(kernel); GTReplay_BblValid(bbl); bbl = GTReplay_BblNext(bbl))
    {
        // Traverse all the instructions within the basic blocks 
        for (GTReplayIns ins = GTReplay_InsHead(bbl); GTReplay_InsValid(ins); ins = GTReplay_InsNext(ins))
        {
            // Register callback to be called before instruction execution
            GTReplay_RegisterCallbackBeforeIns(kernel, ins, BeforeInsCallback, NULL);
        }
    }

    uint32_t kernelNameSize = 0;
    GTReplay_GetKernelName(kernel, &kernelNameSize, nullptr);

    char* buf = new char[kernelNameSize + 1]();
    GTReplay_GetKernelName(kernel, &kernelNameSize, buf);

    kernelName = std::string(buf);
    
    delete[] buf;
}

/*
 * GTReplay_Entry - tool entry point
 */
extern "C"
DLLEXP void FASTCALL GTReplay_Entry(int argc, const char *argv[])
{
    // configure GTReplay
    ConfigureGTReplay(argc, argv);
    
    // register OnKernelBuild and OnKernelComplete callbacks
    GTReplay_RegisterOnKernelBuildCallback(OnKernelBuild);
    GTReplay_RegisterOnKernelCompleteCallback(OnKernelComplete);

    // Start GTReplay
    GTReplay_Start();
}
