def send(self, *args, **kwargs): result = super(_WrappedSocket, self).send(*args, **kwargs) try: # defensive nw.get_counters().o += int(result) except: pass return result
def recv_into(self, *args, **kwargs): result = super(_WrappedSocket, self).recv_into(*args, **kwargs) try: # defensive nw.get_counters().i += int(result) except: pass return result
def sendall(self, *args, **kwargs): result = super(_WrappedSocket, self).sendall(*args, **kwargs) # update nw_out after socket operation finished successfully try: # defensive nw.get_counters().o += len(args[0]) except: pass return result
def recv_from(self, *args, **kwargs): result = super(_WrappedSocket, self).recv_from(*args, **kwargs) try: # defensive # first item is a string or bytes representing the data received nw.get_counters().i += len(result[0]) except: pass return result
def _ssl_sock_read(*args, **kwargs): """ From ssl.SSLSocket.read docstring: > Read up to 'len' bytes from the SSL object and return them. If 'buffer' is provided, read into this buffer and return the number of bytes read. So, it is possible that ssl_read returns an integer or a byte/string. """ try: result = kwargs.pop("_result") if isinstance(result, int): nw.get_counters().i += result else: nw.get_counters().i += len(result) except: pass
def start( builtins=True, profile_cpu=True, profile_memory=True, profile_nw=False, profile_timespan=False, instrumented_funcs={}, timespan_selectors={}, timespan_threshold=MAX_TIMESPAN_THRESHOLD, # ms apm_extended_trace=False, apm_timespan_limit_per_rule=0, apm_timespan_limit_global=0, probe=None, ctx_var=None): global _max_prefix_cache if not isinstance(timespan_selectors, dict): raise BlackfireProfilerException( "timespan_selectors shall be an instance of 'dict'") if not isinstance(instrumented_funcs, dict): raise BlackfireProfilerException( "instrumented_funcs shall be an instance of 'dict'") # in fact we can use this cache this forever but the idea is maybe the sys.path # changes in some way and it would be nice to see the effect between every # start/stop pair. _max_prefix_cache = {} # TODO: disable timespan profiling for context-aware profiling if ctx_var is not None: if not apm_extended_trace: profile_timespan = False # Use tracemalloc for some libraries like numpy. They have their own allocators # and use tracemalloc APIs to track the allocations. See: PyTraceMalloc_Track API # There might be a possibility that a general memory tracer API might be # investigated in the future: # https://mail.python.org/archives/list/[email protected]/thread/BHOIDGRUWPM5WEOB3EIDPOJLDMU4WQ4F/ use_tracemalloc = False if profile_memory and TRACEMALLOC_AVAIL: if any([import_module(tm) for tm in TRACEMALLOC_REQUIRED_MODULES]) or \ os.environ.get('BLACKFIRE_USE_TRACEMALLOC'): use_tracemalloc = True tracemalloc.start() _bfext.start( builtins, profile_cpu, profile_memory, profile_nw, profile_timespan, use_tracemalloc, instrumented_funcs, timespan_selectors, timespan_threshold, apm_extended_trace, apm_timespan_limit_per_rule, apm_timespan_limit_global, probe, nw.get_counters(), ctx_var, )
def _ssl_sock_write(*args, **kwargs): # ssl.SSLSocket.write returns the number of bytes written try: nw.get_counters().o += kwargs.pop("_result") except: pass