def _get_request_object(self): obj_id = request.args.get('id', type=long) if not obj_id: return None return objgraph.at(obj_id)
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 __call__(self, environ, start_response): request = webob.Request(environ) if request.path_info.startswith(self.signature): query = request.GET.get('query') obj_type = request.GET.get('object_type') obj_address = request.GET.get('object_address') if obj_address: # Create a graph for the object leaking = [objgraph.at(int(obj_address))] filename = tempfile.mktemp(suffix='.png') objgraph.show_refs(leaking, filename=filename) output = open(filename, 'r').read() os.unlink(filename) start_response( '200 Ok', [('Content-Type', 'image/png')]) return output output = StringIO() leaking = objgraph.get_leaking_objects() formats.htmlHeader(output, self.signature, request.host_url, query) output.write('<h3>Memory usage</h3>') output.write('<table>' + ''.join( map(lambda info: MEMORY_LINE.format(*info), objgraph.most_common_types(10, leaking))) + '</table>') if obj_type: # Display detail about leaking objects of a given type. output.write('<h4>Memory detail for %s</h4>' % obj_type) output.write('<ul>') for obj in leaking: if type(obj).__name__ == obj_type: output.write(OBJECT_LINE.format( id(obj), cgi.escape(str(obj)))) output.write('</ul>') output.write('<h3>Timing</h3>') formats.htmlFormat(output, query=query) start_response( '200 Ok', [('Content-Type', 'text/html; charset=UTF8')]) return output.getvalue() scale = self.find(request.path_info) scale.requests.mark() with scale.latency.time(): response = request.get_response(self.app) result = scale.statuses(response.status_int) if result is not None: result.mark() start_response( response.status, [a for a in response.headers.iteritems()]) return response.app_iter