#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# StarPU --- Runtime system for heterogeneous multicore architectures.
#
# Copyright (C) 2020       Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
#
# StarPU is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or (at
# your option) any later version.
#
# StarPU is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See the GNU Lesser General Public License in COPYING.LGPL for more details.
#

import sys

"""
STARPU_FXT_EVENT_DEFINES is generated by configure and is the output of
the following command:
grep -E "#define\s+_STARPU_(MPI_)?FUT_" src/common/fxt.h mpi/src/starpu_mpi_fxt.h | grep 0x | grep -v 0x1 | cut -d : -f 2
"""

fxt_codes_raw = """
@STARPU_FXT_EVENT_DEFINES@
"""

PROGNAME=sys.argv[0]

number_events_path = None

def usage():
    print("Convert event keys in number_events.data to event names")
    print("")
    print("Usage: %s <number_events.data path>" % PROGNAME)
    print("")
    print("Options:")
    print("	-h, --help          display this help and exit")
    print("	-v, --version       output version information and exit")
    print("")
    print("Report bugs to <@PACKAGE_BUGREPORT@>")
    sys.exit(1)


if len(sys.argv) == 2:
    if sys.argv[1] == '-v' or sys.argv[1] == '--version':
        print("%s (@PACKAGE_NAME@) @PACKAGE_VERSION@" % PROGNAME)
        sys.exit(0)
    elif sys.argv[1] == '-h' or sys.argv[1] == '--help':
        usage()
    else:
        number_events_path = sys.argv[1]
else:
    usage()

def man():
    print("Sepecify file containing event stats")
    sys.exit(1)




# Process fxt_code_raw content to ease the conversion:
fxt_codes = dict()
for line in fxt_codes_raw.split("\n"):
    elements = line.split()

    if len(elements) == 3:
        key = int(elements[2][2:], 16)
        assert(key not in fxt_codes)

        fxt_codes[key] = elements[1]


# Convert content of the file:
nb_events = 0

with open(number_events_path, 'r') as f:
    for line in f:
        elements = line.split()
        if len(elements) == 2:
            key = int(elements[0][2:], 16)
            nb = int(elements[1])
            nb_events += nb
            if key in fxt_codes:
                print("%12d    %s" % (nb, fxt_codes[key]))
            else:
                print("%12d    %s" % (nb, elements[0]))

print("       TOTAL:   %d" % nb_events)
