
    K!              
       l   S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKrSSK	J
r
Jr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  S\4S jrS\4S jrS\\   4S jrS\
\\R6                  4   S\
\\4   S\\   4S jrS\
\\R6                  4   4S jrS rS rS\R@                  S\4S jr!S\RD                  S\4S jr#S\
\\R6                  4   S\S\R@                  S\RD                  S\4
S jr$S\
\\R6                  4   S\S\R@                  S\RD                  4S jr%S\\   S \4S! jr&g)"z#Utilities for debugging task graph.    )absolute_import)division)unicode_literalsN)DictIterator)task_buffer
task_graph)log)
properties)filesreturnc                  f    [         R                  R                  R                  R	                  5       $ )zqWhether task graph debugging is enabled.

Returns:
    bool: True if task graph debugging is enabled else False.
)r   VALUESstorageenable_task_graph_debuggingGetBool     Clib/googlecloudsdk/command_lib/storage/tasks/task_graph_debugger.pyis_task_graph_debugging_enabledr   "   s%     
			"	"	>	>	F	F	HHr   c                  f    [         R                  R                  R                  R	                  5       $ )zGReturns the time interval in seconds between two consecutive snapshots.)r   r   r   &task_graph_debugging_snapshot_durationGetIntr   r   r   #get_time_interval_between_snapshotsr   +   s'     FFMMOr   c               #     #    [         R                  " 5       R                  5        HW  u  pSU  3v   [        R                  " U5       H1  u  p#pESU SU SU 3v   U(       d  M  SUR                  5        3v   M3     MY     g7f)zARetrieve stack traces for all the threads in the current process.z
# Traceback for thread: zFile: "z", line z, in z  N)sys_current_framesitems	tracebackextract_stackstrip)	thread_idstackfilenameline_numbernametexts         r   yield_stack_tracesr)   2   s     
 --/557i&yk
22-6-D-DU-K)thZx}E$@@	4::<.!! .L 8s   AB$Bname_to_threadalive_thread_id_to_namec              #     #    U R                  5        H*  u  p#UR                  5       (       d  M  X!UR                  '   M,     [        5       nSn[        R
                  " S5      nU HP  nUR                  U5      =n(       a  [        UR                  S5      5      nXQ;   d  M<  U(       a	  SX    3v   Uv   MR     U R                  5        H  u  p#UR                  U;  d  M  SU S3v   M!     g7f)z8Yields the stack traces of the alive management threads.Nz^\n# Traceback for thread:(.*)   z
# Traceback for thread:z

# Thread z6 is not running. Cannot get stack trace at the moment.)	r   is_aliveidentr)   recompilematchintgroup)	r*   r+   thread_namethreadall_threads_stack_tracescurrent_thread_idthread_id_patternlinethread_id_matchs	            r   %_yield_management_thread_stack_tracesr<   ?   s     
 ,113k.9fll+ 4 01jj!BC&d+11$777o33A674	.ABD	
 j ' ,113k||22} %  4s   )C-A.C-!;C- C-#management_threads_name_to_functionc                     [         R                  R                  S5        0 n[        X5      nU H"  n[         R                  R                  U5        M$     g)z.Prints stack traces of the management threads.z=Initiating stack trace information of the management threads.N)r   statusPrintr<   )r=   r+   stack_tracesr:   s       r   print_management_thread_stacksrB   a   sL     **E 6), dJJT r   c                     [         R                  " U 5      nU(       ad  [        R
                  R                  S5        UR                  5        H0  n[        R
                  R                  UR                  5       5        M2     g[        R
                  R                  S5        g! [         aB  n[        R                  " SU 35        [        R
                  R                  S5         SnAgSnAff = f)z*Prints stack traces of the worker threads.z Error reading stack trace file: z#No stack traces could be retrieved.Nz)Printing stack traces for worker threads:z1No stack traces found. No worker threads running.)	r   ReadFileContentsIOErrorr   errorr?   r@   
splitlinesr"   )stack_trace_file_pathrA   er:   s       r    print_worker_thread_stack_tracesrJ   p   s    ))*?@L JJ@A'')	jjtzz|$ * JJHI 
 II045JJ:;
s   B# #
C/-8C**C/c                 $   [         R                  R                  SU R                  5        35        [         R                  R                  SUR                  5        35        [         R                  R                  SUR                  5        35        g)zPrints the size of the queues.zTask Queue size: zTask Status Queue size: zTask Output Queue size: N)r   r?   r@   qsize)
task_queuetask_status_queuetask_output_queues      r   print_queue_sizerP      sp    **&z'7'7'9&:;<**-.?.E.E.G-HIJ**-.?.E.E.G-HIJr   r
   c                 6    U R                   R                  5       $ )z"Checks if the task graph is empty.)is_emptyis_setr	   s    r   _is_task_graph_emptyrT      s    				#	#	%%r   task__bufferc                 (    U R                  5       S:H  $ )z#Checks if the task buffer is empty.r   )size)rU   s    r   _is_task_buffer_emptyrX      s    					!!r   stack_trace_filedelay_secondsc                 >   [        U5      n[        U5      nSnU(       d  U(       a  U(       d  [        U 5        [        U5        [        R
                  R                  [        U5      5        [        R
                  R                  [        U5      5        [        U5      n[        U5      nSnU R                  5        H  nUR                  5       (       d  M  Sn  O   [        R                  " 5       n	U	R                  U5        U(       a  M  U(       d  M  U(       d  M  gg)a  The main worker function for the task graph debugging framework.

Prints the stack traces of the management threads involved namely
iterator_to_buffer, buffer_to_queue and task_output_handler.Captures and
prints the contents of the task graph and task buffer.
Also prints the stack traces of the worker threads if they are running at the
particular snapshot taken.

Args:
  management_threads_name_to_function: A dictionary of management thread name
    to the thread function.
  stack_trace_file: Path to the file containing the stack traces of the worker
    threads.
  task_graph: The task graph object.
  task__buffer: The task buffer object.
  delay_seconds: The time interval between two consecutive snapshots.
TFN)rT   rX   rB   rJ   r   r?   r@   strvaluesr.   	threadingEventwait)
r=   rY   r
   rU   rZ   is_task_graph_emptyis_task_buffer_emptyis_some_management_thread_aliver6   events
             r   task_graph_debugger_workerre      s    0 -Z8.|< %)! & !"#FG$%56JJS_%JJS&'.z:0>&+#5<<>			*.' ? OOE	JJ}+ &%  !!r   c           	           [         R                  " [        U UUU[        5       4S9nUR	                  5         g! [
         a#  n[        R                  " SU 35         SnAgSnAff = f)z)Starts a thread for task graph debugging.)targetargszError starting thread: N)r^   Threadre   r   start	Exceptionr   rF   )r=   rY   r
   rU   thread_for_task_graph_debuggingrI   s         r   %start_thread_for_task_graph_debuggingrm      sg    -&/&6&6)//1
	'# $))+	 -II's+,,-s   69 
A&A!!A&rA   rH   c                 .   U(       d  g / nU  H-  nUR                  5       nU(       d  M  UR                  U5        M/     SR                  U5      n[        R                  " X5        g! [
         a#  n[        R                  " SU 35         SnAgSnAff = f)zWrites stack traces to a file.N
z2An error occurred while writing stack trace file: )r"   appendjoinr   WriteFileContentsrk   r   rF   )rA   rH   stripped_stack_entriesentrystripped_entrycontentrI   s          r   write_stack_traces_to_filerw      s     

H{{}n	%%n5 
 ii./G	1;	 HIIB1#FGGHs   A' <A' '
B1BB)'__doc__
__future__r   r   r   r0   r   r^   r    typingr   r   (googlecloudsdk.command_lib.storage.tasksr   r
   task_graph_modulegooglecloudsdk.corer   r   googlecloudsdk.core.utilr   boolr   r3   r   r\   r)   ri   r<   rB   rJ   rP   	TaskGraphrT   
TaskBufferrX   re   rm   rw   r   r   r   <module>r      s   * &  ' 	 
   ! @ T # * *I IS 
"HSM 
"i.../!#s(^ c]D)-c93C3C.C)DJ&K&%6%@%@ &T &
"(>(> "4 "
4)-c93C3C.C)D44 "++4 ((	4
 4n-)-c93C3C.C)D-- "++- ((	-0H3-H8;Hr   