Example #1
0
def get_malloc_stats():
    content = ""
    with tempfile.TemporaryFile(mode="w+") as temp_f:
        with redirect_stderr(temp_f.fileno()):
            sys._debugmallocstats()
        temp_f.flush()
        temp_f.seek(0)
        content = temp_f.read()

    assert content != ""

    malloc_stats = iter(content.splitlines())
    skip = 2  # skip class size
    for line in malloc_stats:
        if line == "":
            skip -= 1
        if skip == 0:
            break
    result = {}
    for line in malloc_stats:
        if line.startswith("Total"):
            break
        match = re.match(r"^#\s*(?P<name>[\w ]+\w)\s*=\s*(?P<value>[\d,]+)",
                         line)
        if match is not None:
            group = match.groupdict()
            result[group["name"].replace(" ", "_")] = int(
                group["value"].replace(",", ""))  # remove comma

    return result
def main():
    print("See block/pool/arena stats:")

    # noinspection PyProtectedMember,PyUnresolvedReferences
    sys._debugmallocstats()

    # Make it do memory things!
    data = []
    for i in range(100_000):
        x = i**2
        data.append(list(str(x) * 100))
Example #3
0
    def run(self):
        config = self.config

        self._sample_initial_trajectories()

        #: Previous epoch's validation loss
        self._val_prev = np.nan

        for i in range(config.num_iters):
            log.info('#%d fitting %d samples', i, len(self.states_agg))
            self._fit()

            trajs = None
            if (max(1, i) % config.eval_interval) == 0:
                log.info('#%d evaluating on %d trajectories', i,
                         config.num_eval)
                trajs = self._evaluate()
                with open(self.costs_csv_filename, 'a') as fileobj:
                    fields = map(str, (i, *[trj.cost for trj in trajs]))
                    print(self.costs_csv_delimiter.join(fields), file=fileobj)

            trajs = self._sample_learner_trajs(trajs=trajs)

            log.info('#%d expert solving %d trajectories', i, len(trajs))
            states, actions = self.expert.advice(trajs)

            log.info('expert returned %d samples', len(states))
            self.states_agg = np.vstack(
                (self.states_agg, states))[-config.max_aggregated:]
            self.actions_agg = np.vstack(
                (self.actions_agg, actions))[-config.max_aggregated:]

            if self.print_stats:
                import numba.runtime as nrt
                sys._debugmallocstats()
                st = nrt.rtsys.get_allocation_stats()
                print('NRT allocations:', st.alloc - st.free, file=sys.stderr)
                stats.print_summary(file=sys.stderr)
Example #4
0
def _dump_alloc_info(filename, do_print=True):
    """
    Helper to call ``sys._debugmallocstats()``, sending the output to
    a file instead of directly to stderr.

    If ``do_print`` is enabled, print out the lines from the debug info
    that have information about the arenas and unused pools.
    """
    with open(filename, "w") as f:
        oldstderr = os.dup(2)
        # replace stderr for a bit
        os.dup2(f.fileno(), 2)

        # NOTE: prints detailed allocation system info to stderr, now redirected to a file
        sys._debugmallocstats()

    # restore stderr
    os.dup2(oldstderr, 2)

    if do_print:
        with open(filename, "r") as f:
            for line in f:
                if "arenas" in line or "unused pools" in line:
                    print(line, end="")
import sys


def main():
    print("See block/pool/arena stats:")

    # noinspection PyProtectedMember,PyUnresolvedReferences
    sys._debugmallocstats()

    # Make it do memory things!
    data = []
    for i in range(100_000):
        x = i**2
        data.append(list(str(x) * 100))

    print("After doing lots of stuff!", flush=True)
    # noinspection PyProtectedMember,PyUnresolvedReferences
    sys._debugmallocstats()


if __name__ == '__main__':
    main()
def get_debugmallocstats() -> bytes:
    """Invokes sys._debugmallocstats and captures the output as bytes."""
    stream = io.BytesIO()
    with redirect_stdout.stderr_redirector(stream):
        sys._debugmallocstats()
    return stream.getvalue()
Example #7
0
def sys_func():
    lists = sys.argv  # 传递给Python脚本的命令行参数列表 => python p.py -> ['p.py'] / python p.py a 1 -> ['p.py', 'a', '1'] / 程序内执行 -> ['']
    strs = sys.getdefaultencoding()  # 默认字符集名称
    strs = sys.getfilesystemencoding()  # 系统文件名字符集名称
    num = sys.getrefcount(object)  # 返回object的引用计数(比实际多1个)
    dicts = sys.modules  # 已加载的模块, 可修改, 但不能通过修改返回的字典进行修改
    lists = sys.path  # 模块搜索路径
    sys.path.append("./test")  # 动态添加模块搜索路径
    strs = sys.platform  # 平台标识符(系统身份进行详细的检查,推荐使用) Linux:'linux' / Windows:'win32' / Cygwin:'cygwin' / Mac OS X:'darwin'
    strs = sys.version  # python解释器版本
    lists = sys.thread_info  # 线程信息
    num = sys.api_version  # 解释器C API版本

    types, value, back = sys.exc_info()  # 捕获异常 详见 异常 文章的 excep() 代码块第二小部分(http://blog.csdn.net/rozol/article/details/69313164)
    sys.excepthook(types, value, back)  # 打印异常
    types = sys.last_type
    value = sys.last_value
    back = sys.last_traceback
    # sys.exit([arg]) // 引发SystemExit异常退出Python(可以try), 范围[0,127], None==0, "string"==1
    sys.exit(0)

    num = sys.getrecursionlimit()  # 最大递归数(堆栈最大深度), 详见 函数 文章(http://blog.csdn.net/rozol/article/details/69242050)
    sys.setrecursionlimit(5000)  # 修改最大递归数

    fnum = sys.getswitchinterval()  # 获取线程切换间隔
    sys.setswitchinterval(0.005)  # 设置线程切换间隔, 单位秒
    num = sys.getcheckinterval()  # 解释器的检查间隔
    sys.setcheckinterval(100)  # 设置解释器检查间隔, 执行(默认)100个虚拟指令执行一次检查, 值为<=0时,检查每个虚拟指令

    # sys.stdin // 标准输入流
    strs = sys.stdin.readline()[:-1]
    # sys.stdout // 标准出入输出
    sys.stdout.write(">>")
    sys.stdout.flush()
    # sys.stderr // 标注错误流
    sys.stderr.write(">>")

    # ---

    lists = sys.builtin_module_names  # 所有模块 (注:非导入模块)
    path = sys.base_exec_prefix  # Python安装路径
    path = sys.base_prefix  # 同base_exec_prefix
    path = sys.exec_prefix  # 同base_exec_prefix
    path = sys.prefix  # 同base_exec_prefix
    path = sys.executable  # Python解释器的绝对路径

    strs = ys.byteorder  # 本机字节顺序指示器, big-endian(最高有效字节在第一位)值为'big', little-endian(最低有效字节在第一位)值为'little'
    strs = sys.copyright  # python版权
    num = sys.hexversion  # 16进制版本号
    lists = sys.implementation  # 当前运行的解释器的信息
    num = sys.getallocatedblocks()  # 解释器当前分配的内存块的数量
    boolean = sys.dont_write_bytecode  # 是否不会尝试导入源模块是写入.pyc文件 (False会写入.pyc文件)
    # sys.getsizeof(object[, default]) // 返回对象的大小bit, 只计算自身内存消耗,不计算引用对象的内存消耗, 调用对象的__sizeof__(), default没有获取到默认返回值
    num = sys.getsizeof(object)
    boolean = sys.is_finalizing()  # 解释器是否正在被关机
    num = sys.maxsize  # 最大整数值(2 ** 31 -1), 与系统有关
    num = sys.maxunicode  # 最大Unicode值的整数 (1114111)
    strs = sys.ps1  # 解释器主提示符
    strs = sys.ps2  # 解释器次提示符

    sys.call_tracing(func, ("arg",))  # 调用函数
    sys._clear_type_cache()  # 清除内部类型缓存
    sys._debugmallocstats()  # 打印CPython内存分配器状态的低级信息

    sys.setprofile(profilefunc)  # 设置profile函数, 默认None
    sys.getprofile()  # 获取profile函数
    sys.settrace(tracefunc)  # 设置跟踪函数, def tracefunc(frame、event 和arg):
    sys.gettrace()  # 获取跟踪函数, 默认None
    sys.set_coroutine_wrapper(wrapper)  # 设置包装 def wrapper(coro):
    sys.get_coroutine_wrapper()  # 包装, 默认None
Example #8
0
from abc import ABCMeta


class MyAbcs(ABCMeta):
    pass

class MyAbc:
    pass


import sys

# 返回解释器
print(sys._debugmallocstats())
# 返回当前解释器已经分配的内存块数
print(sys.getallocatedblocks())

ab = MyAbc()
print(sys.getrefcount(ab))  # result + 1
print(sys.getsizeof('h'))   # obj size
print(sys.getswitchinterval())  # 返回线程 切换时间 0.05
print(sys.implementation)   # 
print(sys.modules)      # 显示已经加载的模块