def dump_objgrpah(objgraph_file): new_ids = objgraph.get_new_ids() new_ids_list = new_ids['list'] new_objs = objgraph.at_addrs(new_ids_list) objgraph.show_backrefs(new_objs, highlight=inspect.isclass, refcounts=True, filename=objgraph_file) new_ids = objgraph.get_new_ids()
def new_item_refmaps(collect=True, types=None, skip=2, max_per_type=10, max_total=50, tags=None): log.debug('new_item_refmaps(%s)', collect) if collect: log.debug(' forcing garbage collection') gc.collect() pid = os.getpid() log.debug(' finding new_ids') new_ids = objgraph.get_new_ids() # dict of sets if STATE['new_item_refmaps'] < skip: log.debug(' query #%d < %d; not analyzing yet', STATE['new_item_refmaps'], skip) STATE['new_item_refmaps'] += 1 else: done = False events = list() if types is None: types = new_ids.keys() total_count = 0 for type in types: type_count = 0 log.debug(' finding backref chains for items of type=%s', type) for item_id in new_ids.get(type, []): item = objgraph.at(item_id) name = _obj_name(item) log.debug(' finding backrefs chain for item=%s', name) refmap = objgraph.find_backref_chain(item, objgraph.is_proper_module) refmap = [_obj_name(x) for x in refmap] events.append({ 'time': _now(), 'pid': pid, 'type': type, 'name': name, 'chain': refmap }) total_count += 1 type_count += 1 if type_count > max_per_type: log.debug('reached max_per_type=%d limit', max_per_type) done = True if total_count > max_total: log.debug('reached max_total=%d limit', max_total) done = True if done: break if done: break if events: _apply_tags(tags, *events) return {'sourcetype': 'memleak.new_item_refmaps', 'events': events}
def test_get_new_ids_skip_update(self): x = type('MyClass', (), {'__module__': 'mymodule'})() # noqa new_ids = objgraph.get_new_ids(limit=0) self.assertIn(id(x), new_ids['MyClass']) new_ids = objgraph.get_new_ids(skip_update=True, limit=0) self.assertIn(id(x), new_ids['MyClass'])
def setUp(self): objgraph.get_new_ids(limit=0, shortnames=True)
def foo(): """Foo function called during IDs counting.""" x = Exception() y = queue.Queue() z = queue.LifoQueue() objgraph.get_new_ids()
"""Zjištění konstrukce objektů v paměti.""" # # (C) Copyright 2021 Pavel Tisnovsky # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Public License v1.0 # which accompanies this distribution, and is available at # http://www.eclipse.org/legal/epl-v10.html # # Contributors: # Pavel Tisnovsky # import objgraph import queue objgraph.get_new_ids() def foo(): """Foo function called during IDs counting.""" x = Exception() y = queue.Queue() z = queue.LifoQueue() objgraph.get_new_ids() foo() objgraph.get_new_ids()
def foo(): x = Exception() y = queue.Queue() z = queue.LifoQueue() objgraph.get_new_ids()
def test_at_addrs(self): a = [0, 1, 2] new_ids = objgraph.get_new_ids(limit=0) new_lists = objgraph.at_addrs(new_ids['list']) self.assertIn(a, new_lists)
def show_whats_new(): _ = objgraph.get_new_ids(limit=5) # store current objects in _state _ = objgraph.get_new_ids(limit=5) # current_ids become old_ids in _state a = [1, 2, 3] r = objgraph.get_new_ids(limit=5)
def __call__(self, req): taskid = req.headers['X-Neutron-Profiler-taskid'] action = req.headers['X-Neutron-Profiler-Action'] iteration = req.headers.get('X-Neutron-Profiler-Iteration') profiler_type = req.headers.get('X-Neutron-Profiler-Type') obj_graph = True if profiler_type == 'objgraph' else False objcount = (True if (profiler_type in ['objcount', 'objgraph']) else False) calltrace = (True if (not profiler_type or profiler_type == 'calltrace') else False) trace_path = os.path.join(cfg.CONF.trace_profiler.trace_path, taskid) ensure_dir(trace_path) LOG.info( "Trace Profiler pid %s taskid %s action %s iteration %s" " profiler_type %s", os.getpid(), taskid, action, iteration, profiler_type) if action == 'start': if calltrace: GreenletProfiler.set_clock_type('cpu') GreenletProfiler.start() if objcount and iteration: objcount_dict[iteration] = len(gc.get_objects()) if obj_graph: objgraph.get_new_ids() LOG.info("Trace Profiler.start profiling %s ", os.getpid()) elif action == 'snapshot': if iteration: if objcount: objcount_dict[iteration] = len(gc.get_objects()) if obj_graph: objgraph_file = os.path.join( trace_path, "{}-{}-{}-objgraph.dot".format(socket.gethostname(), os.getpid(), iteration)) dump_objgrpah(objgraph_file) elif action == 'stop': LOG.info("Trace Profiler.stop profiling %s ", os.getpid()) if calltrace: trace_file = os.path.join( trace_path, "{}-{}".format(socket.gethostname(), os.getpid())) GreenletProfiler.stop() stats = GreenletProfiler.get_func_stats() LOG.info("Trace Profiler.writing to trace file %s ", trace_file) stats.save(trace_file, cfg.CONF.trace_profiler.trace_format) GreenletProfiler.clear_stats() if objcount: objcount_file = os.path.join( trace_path, "{}-{}-objcount".format(socket.gethostname(), os.getpid())) if iteration: objcount_dict[iteration] = len(gc.get_objects()) with open(objcount_file, 'w') as fp: json.dump(objcount_dict, fp) objcount_dict.clear() if obj_graph: objgraph_file = os.path.join( trace_path, "{}-{}-objgraph.dot".format(socket.gethostname(), os.getpid())) dump_objgrpah(objgraph_file) else: LOG.warning( "Invalid profiler action %(action)s with " " taskid %(taskid)s", { "action": action, "taskid": taskid })
def __enter__(self): gc.collect() objgraph.get_new_ids()
def set_dots_button(): global loop_no global first_set_app global app global board_frame, msg_main_frame, msg_frame, sqs global width, height, min_xlen, nx, ny global n_rearrange_cycles, rearrange_cycle global players, sp global move_no_label global snapshot1, snapshot2 # tracemalloc instances global board_change global run_game loop_no += 1 SlTrace.lg("\nLoop %d" % loop_no) SlTrace.lg("Memory Used: %.0f MB, Change: %.2f MB" % (SlTrace.getMemory()/1.e6, SlTrace.getMemoryChange()/1.e6)) SlTrace.lg("Dots Set Button", "button") if SlTrace.trace("pgm_stack"): stack = traceback.extract_stack() SlTrace.lg("pgm_stack depth=%d" % len(stack)) if SlTrace.trace("pgm_stack_list"): list_len = 14 print_list = traceback.format_list(stack) for line in print_list[-list_len:]: SlTrace.lg(" " + line) app = setup_app() app.update_form() min_xlen = app.get_component_val("figure_size", "min", min_xlen) min_xlen = float(min_xlen) min_xlen = str(min_xlen) min_ylen = min_xlen ###rects.append(rect1) ###rects.append(rect2) xmin = .1*float(width) xmax = .9*float(width) xlen = (xmax-xmin)/float(nx) min_xlen = float(min_xlen) if xlen < min_xlen: SlTrace.lg("nx=%d xlen(%.0f) set to %.0f" % (nx, xlen, min_xlen)) xlen = min_xlen ymin = .1*float(height) ymax = .9*float(height) ylen = (ymax-ymin)/float(ny) min_ylen = float(min_ylen) if ylen < min_ylen: SlTrace.lg("ny=%d ylen(%.0f) set to %.0f" % (ny, ylen, min_ylen)) ylen = min_ylen if board_change: if board_frame is not None: board_frame.destroy() board_frame = None if msg_main_frame is not None: msg_main_frame.destroy() msg_main_frame = None if sqs is not None: sqs.destroy() sqs = None if sp is not None: sp.destroy() sp = None board_frame = Frame(mw, width=width, height=height, bg="", colormap="new") board_frame.pack(side="top", fill=NONE, expand=NO) msg_main_frame = Frame(mw) msg_main_frame.pack(side="bottom", expand=NO, fill=BOTH) msg_frame = Frame(msg_main_frame) msg_frame.pack(side="bottom", expand=YES, fill=BOTH) board_change = False if sqs is None: sqs = SelectDots(board_frame, mw=mw, image_hash=image_hash, display_game=display_game, nrows=ny, ncols=nx, width=width, height=height, check_mod=check_mod) sqs.display() if sp is None: if command_stream is not None: command_stream.reset() # Reset stream sp = SelectPlay(board=sqs, msg_frame=msg_frame, display_game=display_game, results_file=rF, mw=mw, start_run=False, numgame=numgame, game_control=game_control, cmd_stream=command_stream, player_control=player_control, profile_running=profile_running, on_exit=pgm_exit, on_end=end_game, move_first=1, before_move=before_move, after_move=after_move, show_ties=show_ties, undo_micro_move=undo_micro_move, undo_len=undo_len) ###if command_stream is not None: ### command_stream.set_play_control(sp) ### command_stream.set_cmd_stream_proc(sp.cmd_stream_proc) if player_control is not None: player_control.set_play_control(sp) score_window.set_play_control(sp) sp.set_stroke_move(stroke_move) if first_set_app: if run_resets: sp.reset_score() if show_players: show_players_window(display=show_players) player_control.set_all_scores(0) ###if show_score: ### show_score_window() first_set_app = False if SlTrace.trace("memory"): ###obj_list = objgraph.show_most_common_types(limit=20) ###SlTrace.lg("objgraph=%s" % obj_list) global memory_trace if not memory_trace: tracemalloc.Filter(True, "select*") tracemalloc.start() tracemalloc.Filter(True, "select*") memory_trace = True snapshot = tracemalloc.take_snapshot() top_stats = snapshot.statistics('lineno', True) SlTrace.lg("[ Top 25 ]") for stat in top_stats[:25]: SlTrace.lg(str(stat)) nc = gc.collect() SlTrace.lg("gc.collect=%d" % nc) nc = gc.collect() SlTrace.lg("gc.collect=%d" % nc) ###obj_list = objgraph.show_most_common_types(limit=20) ###SlTrace.lg("objgraph=%s" % obj_list) objgraph.show_growth(limit=20, file=SlTrace.getLogFile()) objgraph.show_growth(limit=20) objgraph.get_new_ids(file=SlTrace.getLogFile()) objgraph.get_new_ids() ###obj = objgraph.by_type('SelectPlayer') ###objgraph.show_backrefs([obj], max_depth=10) SlTrace.lg("gc.garbage:%s" % gc.garbage) if snapshot1 is None and snapshot2 is None: snapshot1 = tracemalloc.take_snapshot() elif snapshot2 is None: snapshot2 = tracemalloc.take_snapshot() else: snapshot1 = snapshot2 snapshot2 = tracemalloc.take_snapshot() if snapshot2 is not None: top_stats = snapshot2.compare_to(snapshot1, 'lineno', True) SlTrace.lg("[ Top 25 differences]") for stat in top_stats[:25]: SlTrace.lg(str(stat)) snapshot1 = snapshot2 snapshot2 = None """ Setup for next game """ sp.reset() if run_game: run_game = False # Clear after running mw.after(0, sp.running_loop)
def test_get_new_ids_long_typename(self): objgraph.get_new_ids(limit=0, shortnames=False) x = type('MyClass', (), {'__module__': 'mymodule'})() # noqa new_ids = objgraph.get_new_ids(limit=0) self.assertIn(id(x), new_ids['mymodule.MyClass'])
def set_squares_button(): global loop_no global first_set_app global app global board_frame, msg_frame, sqs, board_canvas global width, height, min_xlen, nx, ny global n_rearrange_cycles, rearrange_cycle global players, sp global move_no_label global snapshot1, snapshot2 # tracemalloc instances loop_no += 1 SlTrace.lg("\nLoop %d" % loop_no) SlTrace.lg("Memory Used: %.0f MB, Change: %.2f MB" % (SlTrace.getMemory()/1.e6, SlTrace.getMemoryChange()/1.e6)) SlTrace.lg("Squares Set Button", "button") if SlTrace.trace("pgm_stack"): stack = traceback.extract_stack() SlTrace.lg("pgm_stack depth=%d" % len(stack)) if SlTrace.trace("pgm_stack_list"): list_len = 14 print_list = traceback.format_list(stack) for line in print_list[-list_len:]: SlTrace.lg(" " + line) app = setup_app(app) if board_canvas is not None: SlTrace.lg("delete board_canvas") board_canvas.delete() board_canvas = None if board_frame is not None: board_frame.destroy() board_frame = None if msg_frame is not None: msg_frame.destroy() msg_frame = None if sp is not None: sp.destroy() sp = None app.update_form() rects = [] rects_rows = [] # So we can pass row, col rects_cols = [] min_xlen = app.get_component_val("figure_size", "min", min_xlen) min_xlen = float(min_xlen) min_xlen = str(min_xlen) min_ylen = min_xlen ###rects.append(rect1) ###rects.append(rect2) xmin = .1*float(width) xmax = .9*float(width) xlen = (xmax-xmin)/float(nx) min_xlen = float(min_xlen) if xlen < min_xlen: SlTrace.lg("nx=%d xlen(%.0f) set to %.0f" % (nx, xlen, min_xlen)) xlen = min_xlen ymin = .1*float(height) ymax = .9*float(height) ylen = (ymax-ymin)/float(ny) min_ylen = float(min_ylen) if ylen < min_ylen: SlTrace.lg("ny=%d ylen(%.0f) set to %.0f" % (ny, ylen, min_ylen)) ylen = min_ylen board_frame = Frame(mw, width=width, height=height, bg="", colormap="new") board_frame.pack() msg_frame = Frame(mw) msg_frame.pack(side="bottom") board_canvas = Canvas(board_frame, width=width, height=height) board_canvas.pack() if sp is not None and sp.cur_message is not None: sp.cur_message.destroy() sp.msg = None sqs = SelectSquares(board_canvas, mw=mw, nrows=ny, ncols=nx, width=width, height=height, check_mod=check_mod) sqs.display() sp = SelectPlay(board=sqs, msg_frame=msg_frame, mw=mw, start_run=False, game_control=game_control, on_exit=pgm_exit, on_end=end_game, move_first=1, before_move=before_move, after_move=after_move, show_ties=show_ties) sp.set_stroke_move(stroke_move) if first_set_app: if run_resets: sp.reset_score() if show_players: show_players_window() if show_score: show_score_window() first_set_app = False if SlTrace.trace("memory"): ###obj_list = objgraph.show_most_common_types(limit=20) ###SlTrace.lg("objgraph=%s" % obj_list) snapshot = tracemalloc.take_snapshot() top_stats = snapshot.statistics('lineno', True) SlTrace.lg("[ Top 25 ]") for stat in top_stats[:25]: SlTrace.lg(str(stat)) nc = gc.collect() SlTrace.lg("gc.collect=%d" % nc) nc = gc.collect() SlTrace.lg("gc.collect=%d" % nc) ###obj_list = objgraph.show_most_common_types(limit=20) ###SlTrace.lg("objgraph=%s" % obj_list) objgraph.show_growth(limit=20, file=SlTrace.getLogFile()) objgraph.show_growth(limit=20) objgraph.get_new_ids(file=SlTrace.getLogFile()) objgraph.get_new_ids() ###obj = objgraph.by_type('SelectPlayer') ###objgraph.show_backrefs([obj], max_depth=10) SlTrace.lg("gc.garbage:%s" % gc.garbage) if snapshot1 is None and snapshot2 is None: snapshot1 = tracemalloc.take_snapshot() elif snapshot2 is None: snapshot2 = tracemalloc.take_snapshot() else: snapshot1 = snapshot2 snapshot2 = tracemalloc.take_snapshot() if snapshot2 is not None: top_stats = snapshot2.compare_to(snapshot1, 'lineno', True) SlTrace.lg("[ Top 25 differences]") for stat in top_stats[:25]: SlTrace.lg(str(stat)) snapshot1 = snapshot2 snapshot2 = None if run_game: mw.after(0, sp.running_loop)