Best Python code snippet using avocado_python
utils_memory.py
Source:utils_memory.py  
...59    return read_from_meminfo('HugePages_Rsvd')60def get_num_anon_huge_pages(pid=0):61    if int(pid) > 1:62        # get AnonHugePages usage of specified process63        return read_from_smaps(pid, 'AnonHugePages')64    else:65        # invalid pid, so return AnonHugePages of the host66        return read_from_meminfo('AnonHugePages')67def get_transparent_hugepage():68    UPSTREAM_THP_PATH = "/sys/kernel/mm/transparent_hugepage"69    RH_THP_PATH = "/sys/kernel/mm/redhat_transparent_hugepage"70    if os.path.isdir(UPSTREAM_THP_PATH):71        thp_path = UPSTREAM_THP_PATH72    elif os.path.isdir(RH_THP_PATH):73        thp_path = RH_THP_PATH74    else:75        raise exceptions.TestFail("transparent hugepage Not supported")76    out = process.system_output('cat %s/enabled' % thp_path)77    if out[0] == "[always]":78        return 'always'79    elif out[1] == "[madvise]":80        return 'madvise'81    else:82        return 'never'83def set_num_huge_pages(num):84    process.system('/sbin/sysctl vm.nr_hugepages=%d' % num)85def set_transparent_hugepage(sflag):86    """87    sflag only can be set always, madvise or never.88    """89    flags = ['always', 'madvise', 'never']90    if sflag not in flags:91        raise exceptions.TestFail("specify wrong parameter")92    UPSTREAM_THP_PATH = "/sys/kernel/mm/transparent_hugepage"93    RH_THP_PATH = "/sys/kernel/mm/redhat_transparent_hugepage"94    if os.path.isdir(UPSTREAM_THP_PATH):95        thp_path = UPSTREAM_THP_PATH96    elif os.path.isdir(RH_THP_PATH):97        thp_path = RH_THP_PATH98    else:99        raise exceptions.TestFail("transparent hugepage Not supported")100    ret = os.system("echo %s > %s/enabled" % (sflag, thp_path))101    if ret != 0:102        raise exceptions.TestFail("setting transparent_hugepage failed")103def drop_caches():104    """Writes back all dirty pages to disk and clears all the caches."""105    process.run("sync", verbose=False)106    # We ignore failures here as this will fail on 2.6.11 kernels.107    process.run("echo 3 > /proc/sys/vm/drop_caches", ignore_status=True,108                verbose=False)109def read_from_vmstat(key):110    """111    Get specific item value from vmstat112    :param key: The item you want to check from vmstat113    :type key: String114    :return: The value of the item115    :rtype: int116    """117    vmstat = open("/proc/vmstat")118    vmstat_info = vmstat.read()119    vmstat.close()120    return int(re.findall("%s\s+(\d+)" % key, vmstat_info)[0])121def read_from_smaps(pid, key):122    """123    Get specific item value from the smaps of a process include all sections.124    :param pid: Process id125    :type pid: String126    :param key: The item you want to check from smaps127    :type key: String128    :return: The value of the item in kb129    :rtype: int130    """131    smaps = open("/proc/%s/smaps" % pid)132    smaps_info = smaps.read()133    smaps.close()134    memory_size = 0135    for each_number in re.findall("%s:\s+(\d+)" % key, smaps_info):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
