#!/usr/bin/env python3
import sys
import re

def remove_find(regex, s):
    '''removes content in *match* object from string *s*
    
    returns string without find'''
    m = re.search(regex, s)
    if m:
        start,end = m.span()
        return (s[:start]+s[end:]).strip()
    return s.strip()

if __name__ == '__main__':
    # 1. test arguments
    args = ' '.join(sys.argv[1:])
    
    args = remove_find(r'-O -g (:?CLOCK|MEM) -[cC] S0:0', args)
    args = remove_find(r'-m', args)
    args = remove_find(r'[a-zA-Z\-0-9/\._=]+/kernel(:? [0-9]+(:?\.[0-9]+)?)+$', args)
    args = remove_find(r'-f', args)
    
    if args == '-a':
        print('''    Group name  Description
--------------------------------------------------------------------------------
        MEM_SP  Overview of arithmetic and main memory performance
          NUMA  Local and remote data transfers
CYCLE_ACTIVITY  Cycle Activities
            HA  Main memory bandwidth in MBytes/s seen from Home agent
        ENERGY  Power and Energy consumption
     TLB_INSTR  L1 Instruction TLB miss rate/ratio
          DATA  Load to store ratio
        ICACHE  Instruction cache miss rate/ratio
       L2CACHE  L2 cache miss rate/ratio
            L2  L2 cache bandwidth in MBytes/s
     FLOPS_AVX  Packed AVX MFLOP/s
           MEM  Main memory bandwidth in MBytes/s
        BRANCH  Branch prediction miss rate/ratio
      FLOPS_SP  Single Precision MFLOP/s
        MEM_DP  Overview of arithmetic and main memory performance
       L3CACHE  L3 cache miss rate/ratio
   FALSE_SHARE  False sharing
      TLB_DATA  L2 data TLB miss rate/ratio
           QPI  QPI Link Layer data
        CACHES  Cache bandwidth in MBytes/s
        DIVIDE  Divide unit information
           TMA  Top down cycle allocation
         CLOCK  Power and Energy consumption
          UOPS  UOPs execution info
      FLOPS_DP  Double Precision MFLOP/s
  CYCLE_STALLS  Cycle Activities (Stalls)
            L3  L3 cache bandwidth in MBytes/s''')
        sys.exit(0)
    elif len(args) > 0:
        print('Could not remove all arguments. Remaining:', args)
        sys.exit(1)
    
    # 2. return static output
    # From phinally$ likwid-perfctr -f -O -g MEM -C S0:0 -m 
    #                examples/kernels/2d-5pt.likwid_marked 1000 1000 10.0
    print('''STATIC DUMMY STATIC DUMMY STATIC DUMMY
--------------------------------------------------------------------------------
CPU name:       Intel(R) Xeon(R) CPU E5-2680 0 @ 2.70GHz
CPU type:       Intel Xeon SandyBridge EN/EP processor
CPU clock:      2.70 GHz
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
STRUCT,Info,5
1,MEM,loop
Region Info,Core 0,
RDTSC Runtime [s],10.23456,
call count,1,
CPU clock,2.700118 MHz,
TABLE,Group 1 Raw,MEM,11
Event,Counter,Core 0
INSTR_RETIRED_ANY,FIXC0,3.574329e+07
CPU_CLK_UNHALTED_CORE,FIXC1,2.494123e+07
CPU_CLK_UNHALTED_REF,FIXC2,2.494122e+07
CAS_COUNT_RD,MBOX0C0,5.642000e+03
CAS_COUNT_WR,MBOX0C1,3.698000e+03
CAS_COUNT_RD,MBOX1C0,5.446000e+03
CAS_COUNT_WR,MBOX1C1,3.583000e+03
CAS_COUNT_RD,MBOX2C0,6.895000e+03
CAS_COUNT_WR,MBOX2C1,4.874000e+03
CAS_COUNT_RD,MBOX3C0,5.763000e+03
CAS_COUNT_WR,MBOX3C1,3.710000e+03
TABLE,Group 1 Metric,MEM,10
Metric,Core 0,
Runtime (RDTSC) [s],10.23456,
Runtime unhalted [s],10.23456,
Clock [MHz],2.700119e+03,
CPI,6.977878e-01,
Memory read bandwidth [MBytes/s],2.342234e+02,
Memory read data volume [GBytes],0.0013371337,
Memory write bandwidth [MBytes/s],1.111111e+02,
Memory write data volume [GBytes],0.00111111,
Memory bandwidth [MBytes/s],2.727272e+02,
Memory data volume [GBytes],0.002525252,
STATIC DUMMY STATIC DUMMY STATIC DUMMY''')

    # 3. exit with 0
    sys.exit(0)