#!/usr/bin/env python3
import os
import re
import sys
import json
import  getopt
import copy
import textwrap
from pagealloc import page_mem


def print_shmem_info():
    message = (
        "\nLinux Shmem Kind Introduction: \n"
        "1. \"[SYSV]\": System V shared memory; These kind of shmem is usually allocated by shmget syscall, "
        "you can use \"ipcs -m\" command to check the related SYSV shared memory and use \"ipcrm\" to"
        " release them.\n"
        "2. \"[MMAP_ANON_SHARED]\": Shmem allocated by mmap(MAP_ANONYMOUS | MAP_SHARE), "
        "this kind of shmem is allocated from \"/dev/zero\" and will be released after user process exit\n"
        "3. \"[TMPFS]\": Shmem allocated by posix shmem, this kind of shmem has real file path, can be released by "
        "delete the related file.\n"
        "4. \"[MEMFD]\": Shmem allocated by memfd_create syscall, this kind of shmem will be released after all process "
        "close the related fd, can iterate /proc/pid/fd to find which process has opened it.\n"
        "5. \"[OTHER]\": Other kind of unusual shmem, include: \n"
        "    a. drm mm object\n"
        "    b. ashmem\n"
        "    c. SGX backing\n"
        "    ...\n"
    )
    print(message)

def get_runtime(podinfo):
    if 'runtime' in podinfo.keys():
        return podinfo['runtime']
    cmd = "which docker 2>&1"
    ret = os.popen(cmd).read()
    if ret.find("no docker in") == -1:
        return "docker"

    cmd = "which crictl 2>&1"
    ret = os.popen(cmd).read()
    if ret.find("no crictl in") == -1:
        return "crictl"
    return "unknow"

def isV1():
    if os.path.exists("/sys/fs/cgroup/cgroup.controllers"):
        return False
    else:
        return True 

def memRoot():
    if isV1():
        return "/sys/fs/cgroup/memory"
    else:
        return "/sys/fs/cgroup"

def container_init(con):
    con["ns"] = ''
    con["fullid"] = ''
    con["cgroup"] = ''
    con["cparent"] = ''
    con["ino"] = 0
    con["uid"] = ''
    con["podname"] = ''
    con["cname"] = ''
    con["files"] = []
    con["type"] = 'cgroup'
    con["rss"] = 0
    con["cache"] = 0
    con["shmem"] = 0
    
def get_container_info(podinfo, cid, con):
    cmd = podinfo["runtime"] + " inspect " + str(cid)
    res = json.loads(os.popen(cmd).read().strip(),encoding='utf-8')
    
    if podinfo['runtime'] == "docker":
        con['fullid'] = res[0]['Id']
        con['type'] = 'docker'
        if 'HostConfig' in res[0] and res[0]['HostConfig'] is not None:
            if 'CgroupParent' in res[0]['HostConfig']:
                con['cparent'] = res[0]['HostConfig']['CgroupParent']
        if 'Config' in res[0] and res[0]['Config'] is not None:
            config = res[0]['Config']
            if 'Labels' in config and config['Labels'] is not None:
                labels = config['Labels']
                if 'io.kubernetes.pod.namespace' in labels and labels['io.kubernetes.pod.namespace'] is not None:
                    con['ns'] = labels['io.kubernetes.pod.namespace']
                    con['uid'] = labels['io.kubernetes.pod.uid']
                    con['podname'] = labels['io.kubernetes.pod.name']
                    con['type'] = 'k8s-docker'
    else:
        con['fullid']  = res['status']['id']
        con['cparent'] = res['info']['runtimeSpec']['linux']['cgroupsPath']
        con['cparent'] = con['cparent'].strip().split(':')[0]
        
        con['ns'] = res['status']['labels']['io.kubernetes.pod.namespace']
        con['uid'] = res['status']['labels']['io.kubernetes.pod.uid']
        con['podname'] = res['status']['labels']['io.kubernetes.pod.name']
        con['type'] = 'k8s'
    if con['podname']  == '':
        con['podname'] = con["cname"]

def get_container_id(podinfo):
    podinfo["runtime"] = get_runtime(podinfo)
    if podinfo["runtime"] == "unknow":
        return False

    podinfo["container"] = {}
    cmd = podinfo['runtime'] + " ps "
    ret = os.popen(cmd).read().split('\n')
    for line in ret:
        if line.find("CONTAINER") != -1:
            continue
        item = line.split()
        if len(item) < 3:
            continue
        con = {}
        container_init(con)
        con["podid"] = item[-1]
        con["cname"] = item[-1]
        if podinfo['runtime'].find('crictl') != -1:
            con["cname"] = item[-3]
        con["id"] = item[0]
        if len(con["cname"]) == 0:
            con["cname"] = con["id"]

        if podinfo['args']['mode'] == 'cid':
            if item[0] != podinfo['args']['cid']:
                continue
        get_container_info(podinfo, item[0], con)
        if is_target_container(podinfo, con) == False:
            continue
        podinfo['container'][item[0]] = con
        build_cgroup_info(podinfo, item[0])
        build_cgroup_mem(podinfo, item[0], con)
        build_tmp_file(podinfo, item[0], con)

def is_target_container(podinfo,con):
    cid = con['id']
    cpodid = con['podid']
    cpodname = con['podname']
    cmdline = podinfo['args']
    mode = cmdline['mode']

    if mode == 'allcgroup':
        return True
    elif mode == 'pod':
        tpodname = cmdline['podname']
        if cpodname.find(tpodname,0, len(tpodname)) != -1:
            return True
    elif mode == 'cid':
        if cmdline['cid'] == cid:
            return True
    return False

def get_file_ino(filename):
    ret = os.stat(filename)
    return ret.st_ino

def get_k8s_path(podinfo, cid):
    pre = memRoot()
    k8s = "kubepods.slice"
    qos = ["kubepods-burstable.slice","kubepods-besteffort.slice",'']
    cinfo = podinfo['container'][cid]

    if cinfo['type'] == 'k8s':
        runc = 'cri-containerd'
    elif cinfo['type'] == 'k8s-docker':
        runc = 'docker'
    else:
        return None

    for i in qos:
        podpath = pre + '/' + k8s + '/' + i + '/' + cinfo['cparent'] + '/'
        cpath = runc +'-' + cinfo['fullid'].strip()+".scope"
        if not os.path.exists(podpath+cpath):
            continue
        cinfo['cgroup'] = podpath+cpath
        cinfo['ino'] = get_file_ino(podpath+cpath) 
        return True
    fullpath = cinfo['cparent']
    if fullpath.find(cinfo['fullid']) != -1:
        cpath = pre+ '/' + fullpath
        if os.path.exists(cpath):
            cinfo['cgroup'] = cpath
            cinfo['ino'] = get_file_ino(cpath)
    return None
                    
def get_podman_path(podinfo, cid):
    cinfo = podinfo['container'][cid]
    pre = memRoot() +'/machine.slice'
    cpath = pre +'/' +"libpod-" + cinfo['fullid'].strip() + '.scope'
    if not os.path.exists(cpath):
        cinfo['cgroup'] = ''
        cinfo['ino'] = ''
        return None
    cinfo['cgroup'] = cpath
    cinfo['ino'] = get_file_ino(cpath)
    return True

def get_docker_path(podinfo, cid):
    cinfo = podinfo['container'][cid]
    # get docker's cgroup driver to determine cgroup path
    cmd = "docker info"
    ret = os.popen(cmd).read().split('\n')
    cg_driver = "systemd"
    cpath = ""
    for line in ret:
        if line.find("Cgroup Driver") != -1:
            item = line.split()
            cg_driver = item[2].strip()
            break
    if cg_driver == "systemd":
        pre = memRoot()+'/system.slice'
        cpath = pre +'/' +"docker-" + cinfo['fullid'].strip() + '.scope'
    elif cg_driver == "cgroupfs":
        pre = memRoot()+'/docker'
        cpath = pre + '/' + cinfo['fullid'].strip()

    if not os.path.exists(cpath):
        cinfo['cgroup'] = ''
        cinfo['ino'] = ''
        return None
    cinfo['cgroup'] = cpath
    cinfo['ino'] = get_file_ino(cpath)
    return True

def get_cgroup_path(podinfo, cid):
    cinfo = podinfo['container'][cid]
    return True

def build_cgroup_mem(podinfo, cid, con):
    cinfo = con
    if not os.path.exists(cinfo['cgroup']):
        return True
    stat = cinfo['cgroup'] + '/' + 'memory.stat'
    fd = open(stat, 'r')
    ret = fd.read().strip().split('\n')
    filecache = 0
    mem = {}
    for line in ret:
        item = line.strip().split()
        if len(item) != 2:
            continue
        mem[item[0]] = item[1]
    if 'cache' in mem.keys():
        cinfo['cache'] = int(mem['cache'])/1024
    if 'rss' in mem.keys():
        cinfo['rss'] = int(mem['rss'])/1024
    if 'shmem' in mem.keys():
        cinfo['shmem'] = int(mem['shmem'])/1024

    if 'shmem' not in mem.keys():
        cinfo['shmem'] = int(mem['cache']) - int(mem['inactive_file']) - int(mem['active_file'])
    #print("cache={} rss = {} shmem = {}".format(cinfo['cache'], cinfo['rss'], cinfo['shmem']))

def build_tmp_file(podinfo, cid, con):
    cinfo = con
    if not os.path.exists(cinfo['cgroup']):
        return True
    if cinfo['cache'] < 10:
        return True

    fd = open('/tmp/.memcg.txt','a+')
    fd.write(cinfo['cgroup'] +'\n')
    fd.close()


def dump2json(res,filename):
    jsonStr = json.dumps(res)
    if not os.path.exists(os.path.dirname(filename)):
        os.popen("mkdir -p "+os.path.dirname(filename)).read()
    with open(filename, 'w+') as jsonFile:
        jsonFile.write(jsonStr)

def podmem_to_json(podinfo, cinodes, files):
    output = {}
    output['data'] = {}
    output['subline'] = {}
    cmdline = podinfo['args']
    mode = cmdline['mode']
    if mode == 'cid':
        output['type'] = 'cid'
    elif mode == 'pod':
        output['type'] = 'pod'
    elif mode == 'allcgroup':
        output['type'] = 'cid'

    pod = {}
    for key, cid in podinfo['container'].items():    
        new_cid = {}
        new_cid['sort_file'] = {}
        if len(cid['files']) == 0:
            continue
        new_cid['sort_file'] = cid['files']
        new_cid['id'] = cid['id']
        new_cid['cname'] = cid['cname']
        new_cid['cache'] = cid['cache']
        new_cid['rss'] = cid['rss']
        new_cid['shmem'] = cid['shmem']
        new_cid['podname'] = cid['podname']
        if cid['podname'] not in pod.keys():
            pod[cid['podname']] = []
        pod[cid['podname']].append(new_cid)
    output['data'] = pod
    podinfo['output'] = output

def cgroup_to_json(podinfo, cinodes, files):
    output = {}
    output['data'] = {}
    output['subline'] = {}
    cmdline = podinfo['args']
    mode = cmdline['mode']
    if not (mode=='cgroup' or mode == 'system'):
        return False
 
    if mode == 'system':
        output['type'] = 'system'
        output['subline'] == 'system'
    elif mode == 'cgroup':
        output['type'] = 'cgroup'
    elif mode == 'cid':
        output['type'] = 'cid'
    elif mode == 'pod':
        output['type'] = 'pod'
    elif mode == 'allcgroup':
        output['type'] = 'cid'

    if mode == 'cgroup':
        output['subline'] = cmdline['cgroup'].strip().split('/')[-1]
    output['data'] = {}
    if mode=='cgroup':
        output['data'][output['subline']] = []
        tmp = {}
        tmp['sort_file'] = files
        output['data'][output['subline']].append(tmp)
    elif mode == 'system':
        output['data']['system'] = []
        tmp = {}
        tmp['sort_file'] = files
        output['data']['system'].append(tmp)
    podinfo['output'] = output
    return True

def pod_mem_run(podinfo):
    cmdline = podinfo['args']
    cache_bin = os.getenv("SYSAK_WORK_PATH");
    cache_bin += "/tools/memcache -m "
    if cmdline['target_file'] != '':
        cmd = cache_bin + ' -b %s' % cmdline['target_file']
        rate = " -r " + str(cmdline['rate']) + " -t " + str(cmdline['top'])
        cmd += rate
    else:
        cmd = cache_bin +" -f /tmp/.memcg.txt"
        rate = " -r " + str(cmdline['rate']) + " -t " + str(cmdline['top'])
        cmd += rate
        if cmdline['mode'] == 'system':
            cmd += ' -a 1 '
        if cmdline['only_shmem'] == 1:
            cmd += ' -o '

    print("\nrun memory check and it will take a while")
    ret = os.popen(cmd).read().strip().split('\n')
    inodes = {}
    files = []
    if os.path.exists('/tmp/.memcg.txt'):
        os.unlink('/tmp/.memcg.txt')

    if cmdline['target_file'] != '':
        print("\n".join(ret))
        return
    
    if (cmdline['only_shmem'] == 1):
        print_shmem_info()
    
    pattern = re.compile(r'(\S+?)=(.*?)(?=\s\S+=|$)')

    file_num = 1
    for line in ret:
        if line.find('inode=') == -1:
            continue
        
        matches = pattern.findall(line)
        if len(matches) < 10:
            continue

        tmp = {}
        for key, value in matches:
            if key == 'inode':
                tmp['inode'] = int(value)
            elif key == 'file':
                tmp['file'] = value
            elif key in ['cinode', 'cached', 'size', 'active', 'inactive', 'shmem', 'delete', 'dirty']:
                tmp[key] = int(value)
            elif key == 'shmem_kind':
                tmp[key] = str(value)
            elif key == 'cgcached':
                tmp[key] = int(value)
                
        cinode = tmp.get('cinode')
        files.append(tmp)
        if not cinode in inodes.keys():
            inodes[cinode] = []
        inodes[cinode].append(tmp)
        
        if cmdline['output'] == 'json':
            continue
        if cmdline['mode'] == 'system':
            print("{}. file path: {}".format(file_num, tmp['file']))
            res = "file cache info: "
            res += "size: {}kb cached: {}kb ".format(tmp['size'], tmp['cached'])
            res +=  "active: {}kb inactive: {}kb ".format(tmp['active'],tmp['inactive']) 
            res +=  "shmem: {} shmem_kind: {} delete: {} dirty: {}kb".format(tmp['shmem'], tmp['shmem_kind'], tmp['delete'], tmp['dirty'])
            print(res)
            file_num += 1
    mode = podinfo['args']['mode']
    
    for cid,cinfo in podinfo['container'].items():
        inode = cinfo['ino']
        if not inode  in inodes.keys():
            continue
        cinfo['files'] = inodes[inode]

        if cmdline['output'] == 'json':
            continue
        print('\n')
        if mode == 'cgroup':
            print("cgroup path: {}".format(cid))
        elif mode == 'pod':
            print("container name: {} podname:{}".format(cinfo['cname'], cinfo['podname']))
        elif mode == 'cid':
            print("container name {}".format(cinfo['cname']))
        elif mode == 'allcgroup':
            out = "container name:%s"%(cinfo['cname'])
            if cinfo['podname'] != '':
                out += ' podname:%s'%(cinfo['podname'])
            print(out)
        file_num = 1
        for tmp in cinfo['files']:
            print("{}. file path: {}".format(file_num, tmp['file']))
            res = "file cache info: "
            res += "size: {}kb cached: {}kb cgcached: {}kb ".format(tmp['size'], tmp['cached'], tmp['cgcached'])
            res +=  "active: {}kb inactive: {}kb ".format(tmp['active'],tmp['inactive']) 
            res +=  "shmem: {} shmem_kind: {} delete: {} dirty: {}kb".format(tmp['shmem'], tmp['shmem_kind'], tmp['delete'], tmp['dirty'])
            print(res)
            file_num += 1

    if cmdline['output'] != 'json':
        return True
    if cgroup_to_json(podinfo, inodes, files) == False:
        podmem_to_json(podinfo, inodes, files)
    dump2json(podinfo['output'], cmdline['output_file'])
 
def build_cgroup_info(podinfo, cid):
    ctype = podinfo['container'][cid]['type']
    if ctype=='k8s' or ctype == 'k8s-docker':
        get_k8s_path(podinfo, cid)
    elif ctype == 'docker':
        get_docker_path(podinfo, cid)
        if podinfo['container'][cid]['cgroup'] == '':
            get_podman_path(podinfo, cid)
    elif ctype == 'cgroup':
        get_cgroup_path(podinfo, cid)
    else:
        print("get error for cgroup path : {}".format(cid))

def handle_args(podinfo, argv):
    cmdline = {}
    cmdline['rate'] = 1
    cmdline['scan'] = 0
    cmdline['podname'] = ''
    cmdline['cid'] = ''
    cmdline['cgroup'] = ''
    cmdline['mode'] = 'allcgroup'
    cmdline['output'] = 'stdio'
    cmdline['top'] = 10
    cmdline['target_file'] = ''
    cmdline['only_shmem'] = 0
    try:
        opts, args = getopt.getopt(argv,"hmj:r:sap:c:f:t:b:o")
    except getopt.GetoptError:
        print('get opt error')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print("help for usage:")
            print("-p: analysis pod pagecache(sysak podmem -p ack-node-problem-detector)")
            print("-c: analysis container pagecache(sysak podmem -c bd2146176a5ce)")
            print("-a: analysis all container pagecache(sysak podmem -f /sys/fs/cgroup/memory/system.slice)")
            print("-f: analysis cgroup pagecache(sysak podmem -f /sys/fs/cgroup/memory/system.slice)")
            print("-s: analysis system pagecache(sysak podmem -s )")
            print("-j: dump result to json file (sysak podmem -s -j ./test.json)")
            print("-r: set sample rate ,default set to 1 (sysak podmem -s -r 2)")
            print("-t: output filecache top ,default for top 10 (sysak podmem -s -t 20)")
            print("-m: analysis pod recv-Q  and send-Q memory")
            print("-b: find cgroups for file cache of a specific file")
            print("-o: only analysis share memory (sysak podmem -s -o)")
            sys.exit(2) 
        elif opt == '-r':
            cmdline['rate'] = int(arg)
        elif opt == '-p':
            cmdline['podname'] = arg
            cmdline['mode'] = 'pod'
        elif opt == '-c':
            cmdline['cid'] = arg
            cmdline['mode'] = 'cid'
        elif opt == '-f':
            cmdline['cgroup'] = arg
            cmdline['mode'] = 'cgroup'
        elif opt == '-s':
            cmdline['mode'] = 'system'
        elif opt == '-a':
            cmdline['mode'] = 'allcgroup'
        elif opt == '-m':
            page_mem()
            sys.exit(2) 
        elif opt == '-t':
            cmdline['top'] = int(arg)
        elif opt == '-j':
            cmdline['output'] = 'json'
            cmdline['output_file'] = arg
        elif opt == '-b':
            cmdline['mode'] = 'system'
            cmdline['target_file'] = arg
        elif opt == '-o':
            cmdline['only_shmem'] = 1
    podinfo['args'] = cmdline 

def build_cgroup(podinfo):
    cgroup = podinfo['args']['cgroup']
    con = {}
    container_init(con)
    con['cgroup'] = cgroup
    con['ino'] = get_file_ino(cgroup)
    build_cgroup_mem(podinfo, cgroup, con)
    build_tmp_file(podinfo, cgroup, con)
    podinfo['container'][cgroup] = con

def check_k8s_env(podinfo):

    if not os.path.exists("/proc/kpagecgroup"):
        podinfo['args']['mode'] = 'system'
        return True
    if podinfo['args']['mode'] != 'allcgroup':
        return True

    if len(podinfo["container"]) > 0:
        return True

    podinfo['args']['mode'] = 'system'
    return True
 
if __name__ == "__main__":
    podinfo = {}
    podinfo['container'] = {}
    podinfo['output'] = {}
    handle_args(podinfo, sys.argv[1:])

    if podinfo['args']['mode'] == 'cgroup':
        build_cgroup(podinfo)
    else:
        get_container_id(podinfo)
    check_k8s_env(podinfo)
    pod_mem_run(podinfo)
