#
# basic linux software metrics
#
# for most of these it would be much nicer to measure durations, but
# perf stat doesn't support that
#
from __future__ import print_function
import os, sys

class CS:
    name = "Context switches"
    desc = " Number of context switches between threads or processes on a CPU."
    nogroup = True
    subplot = "Scheduler"
    def compute(self, EV):
        self.val = EV("cs", 1)
        self.thresh = self.val > 0

class MinorFaults:
    name = "Minor faults"
    desc = " Page faults not leading to disk IO, such as allocation of memory."
    nogroup = True
    subplot = "Faults"
    def compute(self, EV):
        self.val = EV("minor-faults", 1)
        self.thresh = self.val > 0

class MajorFaults:
    name = "Major faults"
    desc = """
Page faults leading to disk IO, such as loading executable text or do mmap'ed IO."""
    nogroup = True
    subplot = "Faults"
    def compute(self, EV):
        self.val = EV("major-faults", 1)
        self.thresh = self.val > 0

class Migrations:
    name = "Migrations"
    desc = " Number of thread/process migrations to another CPU."
    nogroup = True
    subplot = "Scheduler"
    def compute(self, EV):
        self.val = EV("migrations", 1)
        self.thresh = self.val > 0

# The events below require trace points, so typically root.

class BadEV(Exception):
    pass

def C(ev):
    if not os.path.exists("/sys/kernel/debug/tracing/events" + ev.replace(":", "/")):
        raise BadEV()
    return ev

class Syscalls:
    name = "Syscalls"
    desc = " Number of syscalls, not including vsyscalls such as gettimeofday."
    nogroup = True
    subplot = "OS metrics"
    def compute(self, EV):
        try:
            self.val = EV(C("raw_syscalls:sys_enter"), 1)
        except BadEV:
            self.val = 0
        self.thresh = self.val > 0

class Interrupts:
    name = "Interrupts"
    desc = """
Number of interrupts, including NMIs, excluding exceptions. These are interrupts caused by hardware,
typically to indicate IO. This includes performance counter sampling interrupts."""
    nogroup = True
    subplot = "Interrupts"
    # can overcount with shared vectors
    def compute(self, EV):
        try:
            self.val = EV(C("irq:irq_handler_entry"), 1) + EV(C("nmi:nmi_handler"), 1)
        except BadEV:
            self.val = 0
        self.thresh = self.val > 0

# XXX on older kernels will not count TLB flushes, when they still had an
# own vector
class IPIs:
    name = "IPIs"
    desc = """
Number of inter-processor-interrupts (IPIs). These are caused by software, for example to flush TLBs,
finish IOs on the originating CPU, flush per CPU software caches (such as slab allocator caches)
or force reschedules."""
    nogroup = True
    subplot = "Interrupts"
    # can overcount with shared vectors
    def compute(self, EV):
        try:
            self.val = (EV(C("irq_vectors:call_function_entry"), 1) +
                        EV(C("irq_vectors:call_function_single_entry"), 1) +
                        EV(C("irq_vectors:reschedule_entry"), 1))
        except BadEV:
            self.val = 0
        self.thresh = self.val > 0

class Workqueues:
    name = "Workqueues"
    desc = " Work queue item executions. These are tasks executed by the kernel in the background."
    nogroup = True
    subplot = "Interrupts"
    def compute(self, EV):
        try:
            self.val = EV(C("workqueue:workqueue_execute_start"), 1)
        except BadEV:
            self.val = 0
        self.thresh = self.val > 0

class BlockIOs:
    name = "BlockIOs"
    desc = " Block IOs issued. This counts the number of block IO requests inserted into a queue."
    nogroup = True
    subplot = "IO"
    def compute(self, EV):
        try:
            self.val = EV(C("block:block_rq_insert"), 1)
        except BadEV:
            self.val = 0
        self.thresh = self.val > 0

class NetworkTX:
    name = "NetworkTX"
    desc = " Network packets send to a network device. Aggregated (TSO/GRO) packets are counted as single packets."
    nogroup = True
    subplot = "IO"
    def compute(self, EV):
        try:
            self.val = EV(C("net:net_dev_start_xmit"), 1)
        except BadEV:
            self.val = 0
        self.thresh = self.val > 0

class NetworkRX:
    name = "NetworkRX"
    desc = " Network packets received from a network device. Aggregated (GRO) packets are counted as single packets."
    nogroup = True
    subplot = "IO"
    def compute(self, EV):
        try:
            self.val = (EV(C("net:netif_rx"), 1) +
                        EV(C("net:netif_receive_skb"), 1) +
                        EV(C("net:napi_gro_receive_entry"), 1) +
                        EV(C("net:napi_gro_frags_entry"), 1))
        except BadEV:
            self.val = 0
        self.thresh = self.val > 0

# trace events
# sched stats? (non perf)
#   yield percentage
#   % sched to idle
#   ttw local vs remote
#   wait for cpu time
# sched wakeup
# sched iowait
# filemap add to page cache
# compaction begin
# page_alloc
# start reclaim
# XXX large page alloc
# intel_gpu_freq_change
# XXX gpu
# kvm:kvm_exit

warned = False

class Setup:
    def __init__(self, r):
        r.force_metric(CS())
        r.force_metric(MinorFaults())
        r.force_metric(MajorFaults())
        r.force_metric(Migrations())
        global warned
        if os.path.exists("/sys/kernel/debug/tracing/events"):
            r.force_metric(Syscalls())
            r.force_metric(Interrupts())
            r.force_metric(IPIs())
            r.force_metric(Workqueues())
            r.force_metric(BlockIOs())
            r.force_metric(NetworkTX())
            r.force_metric(NetworkRX())
        elif sys.argv[0].find("toplev") >= 0 and "--import" not in sys.argv and not warned:
            warned = True
            print("Need to be root for trace point Linux software metrics.", file=sys.stderr)
