o
    i                     @  s   d dl mZ d dlZd dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ er4d dlmZ zd d	lmZ W n eyQ   eejd
dd dZY nw d dlmZ d dlmZ d dlmZ eeZG dd dZdS )    )annotationsN)TracebackType)TYPE_CHECKING)Optional)cast)Self)	pprof_pb2)	_memalloczfailed to import memallocTexc_info)ddup)config)	collectorc                   @  sl   e Zd ZdZ			d)d*d
dZd+ddZd,ddZd-ddZd.d/ddZd+ddZ	d+dd Z
d0d1d'd(ZdS )2MemoryCollectorzMemory allocation collector.N
max_nframeOptional[int]heap_sample_sizeignore_profilerOptional[bool]returnNonec                 C  sT   t t|d ur|ntj| _t t|d ur|ntjj| _t t|d ur#|ntj	| _	d S N)
r   intr   
max_framesr   heapsample_sizer   boolr   )selfr   r   r    r   X/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/profiling/collector/memalloc.py__init__$   s   zMemoryCollector.__init__c                 C  sR   t du rtjzt | j| j W dS  ty(   t   t | j| j Y dS w )z!Start collecting memory profiles.N)r	   r   CollectorUnavailablestartr   r   RuntimeErrorstopr   r   r   r   r"   1   s   zMemoryCollector.startr   c                 C  s   |    | S r   )r"   r%   r   r   r   	__enter__?   s   zMemoryCollector.__enter__exc_typeOptional[type[BaseException]]	exc_valueOptional[BaseException]	tracebackOptional[TracebackType]c                 C  s   |    d S r   )r$   )r   r'   r)   r+   r   r   r   __exit__C   s   zMemoryCollector.__exit__timeoutOptional[float]c                 C  s   d S r   r   )r   r.   r   r   r   joinK   s   zMemoryCollector.joinc                 C  s>   t d urzt   W d S  ty   tjddd Y d S w d S )Nz-Failed to stop memalloc profiling on shutdownTr
   )r	   r$   r#   LOGdebugr%   r   r   r   r$   N   s   zMemoryCollector.stopc              	   C  sL   zt du r	tdt   W dS  ttfy%   tjdt dd Y dS w )z2Take a snapshot of collected data, to be exported.NzMemalloc is not initializedz-Unable to collect heap events from process %dTr
   )r	   
ValueErrorr   r#   r1   r2   osgetpidr%   r   r   r   snapshotU   s   zMemoryCollector.snapshotToutput_filenamestrassert_samplesr   pprof_pb2.Profilec                 C  sF   |    t  zddlm} W n ty   tdw |j||dS )aO  Export samples to profile, upload, and parse the pprof profile.

        This is similar to test_snapshot() but exports to the profile and returns
        the parsed pprof profile instead of Python objects.

        Args:
            output_filename: The pprof output filename prefix (without .pid.counter suffix)
            assert_samples: Whether to assert that the profile contains samples

        Returns:
            Parsed pprof profile object (pprof_pb2.Profile)

        Raises:
            ImportError: If pprof_utils is not available (only available in test environment)
        r   )pprof_utilsz_pprof_utils is not available. snapshot_and_parse_pprof() is only available in test environment.)r9   )r6   r   uploadtests.profiling.collectorr;   ImportErrorparse_newest_profile)r   r7   r9   r;   r   r   r   snapshot_and_parse_pprof`   s   z(MemoryCollector.snapshot_and_parse_pprof)NNN)r   r   r   r   r   r   r   r   )r   r   )r   r   )r'   r(   r)   r*   r+   r,   r   r   r   )r.   r/   r   r   )T)r7   r8   r9   r   r   r:   )__name__
__module____qualname____doc__r    r"   r&   r-   r0   r$   r6   r@   r   r   r   r   r   !   s    




r   )
__future__r   loggingr4   typesr   typingr   r   r   typing_extensionsr   r=   r   ddtrace.profiling.collectorr	   r>   	getLoggerrA   r2   "ddtrace.internal.datadog.profilingr   #ddtrace.internal.settings.profilingr   ddtrace.profilingr   r1   r   r   r   r   r   <module>   s*   
