
    f                        d dl mZ 	 d dlmZ d dlmZ d dlZ	d dl
mZ d dlZd dlZd dlZd dlZddlmZmZmZmZmZ ddlmZ dd	lmZ dZd
ZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/d Z0d!Z1d"Z2d#Z3d$Z4d%Z5i ed&ed'ed(ed)ed*ed+ed,e d-e!d.e"d/e#d0e$d1e%d2e&d3e'd4e(d5e)d6e*d7e+d8e,d9e-d:e.d;e/d<e0d=e1d>e2d?e3d@e4dAe5dBiZ6dCa7dDZ8dEZ9dF Z:dxdGZ;dxdHZ<dI Z=dJ Z>dK Z?dL Z@dM ZA G dN dOe      ZB G dP dQeB      ZC G dR dSeC      ZD G dT dUeB      ZE G dV dWeE      ZF G dX dYeE      ZG G dZ d[      ZH G d\ d]eE      ZI G d^ d_eE      ZJ G d` daeC      ZK G db dceK      ZL G dd deeE      ZM G df dgeB      ZN G dh dieB      ZO G dj dkeE      ZP G dl dmeE      ZQ G dn doeE      ZR G dp dqeE      ZS G dr dseC      ZT G dt dueB      ZU G dv dw      ZVy# e$ r
 d dlmZ Y w xY w)y    )print_function)MutableMapping)strftimeN)reduce   )lib_RAW_CB_TYPE_LOST_CB_TYPE_RINGBUF_CB_TYPEbcc_perf_buffer_opts)get_online_cpus)get_possible_cpus                     	   
                                                            HASHARRAY
PROG_ARRAYPERF_EVENT_ARRAYPERCPU_HASHPERCPU_ARRAYSTACK_TRACECGROUP_ARRAYLRU_HASHLRU_PERCPU_HASHLPM_TRIEARRAY_OF_MAPSHASH_OF_MAPSDEVMAPSOCKMAPCPUMAPXSKMAPSOCKHASHCGROUP_STORAGEREUSEPORT_SOCKARRAYPERCPU_CGROUP_STORAGEQUEUESTACK
SK_STORAGEDEVMAP_HASH
STRUCT_OPSRINGBUFINODE_STORAGETASK_STORAGE(   A   i  c                 n    d}d}	 ||| z  |z  dz
  kD  s||dz
  kD  rn|dz  }|dz  }"| |kD  r|d d dz   }|S )Nr    r   *+ )valval_maxwidthitexts        +/usr/lib/python3/dist-packages/bcc/table.py_starsrV   c   sf    	ADw&!++UQY	Q	 
 W}CRy3K    c                     t        | |d       S )N)section_bucket)_get_json_hist)valsval_typerY   s      rU   get_json_histr]   o   s    $>>rW   c                 b   g }d}t        t        |             D ]  }| |   dk7  s|} d}d}t        t        |             D ]O  }|dk7  s	||k  s|dz  }i }||d<   t        |      dz
  |d<   t        | |         |d<   |j                  |       |}Q t	        d      ||d}	|r|d   |	|d   <   |	S )	Nr   r   r   zinterval-startzinterval-endcountz%Y-%m-%d %H:%M:%S)tsr\   data)rangelenintappendr   )
r[   r\   rY   	hist_listmax_nonzero_idxrS   indexprevlist_obj	histograms
             rU   rZ   rZ   r   s    IO3t9  7a<O  ED3t9 6a?*AIEH)-H%&'*5zA~H^$ #DGHWX&D   34(T]^I'5a'8	.#$rW   c                    d}d}d}t        |       D ]  \  }}|dkD  r|}||kD  s|} |dk  rd}d}	t        }
nd}d}	t        t        d	z        }
|dkD  rt        ||z         t	        d
|d
z         D ]e  }d
|z  d
z	  }d
|z  d
z
  }||k(  r|d
z  }| |   }|r$|s't        |	||||
t        |||
      fz         d}Ht        |	||||
t        |||
      fz         g y )N@   rM   r       z#     %-19s : count     distributionz%10d -> %-10d : %-8d |%-*s|z-               %-29s : count     distributionz%20d -> %-20d : %-8d |%-*s|r   r   F)	enumerate	stars_maxrd   printrb   rV   )r[   r\   strip_leading_zerolog2_dist_maxidx_maxrQ   rS   vheaderbodystarslowhighrP   s                 rU   _print_log2_histr{      s+   MGG$ $1q5A'w;!$ "}6,@,IM"{fx 1gk" 8Av!mQ!|4K1HC1gdc4e$S'59; ; <%*"$#tS% gu57 7 88rW   c                 D   d}d}d}t        |       D ]  \  }}|dkD  r|}||kD  s|} d}d}	t        }
|dk\  rt        ||z         t        d|dz         D ]I  }| |   }|r#|st        |	|||
t	        |||
      fz         d}-t        |	|||
t	        |||
      fz         K y )Nrm   rM   r   z#     %-13s : count     distributionz        %-10d : %-8d |%-*s|r   F)ro   rp   rq   rb   rV   )r[   r\   rr   rs   rt   rQ   rS   ru   rv   rw   rx   rP   s               rU   _print_linear_histr}      s    MGG$ $1q5A'w;!$ 3F(DE!|fx 1gk" 
<1gdae$S'59; ; <%*"dae$S'59; ; <
<rW   c                 4    	 t         |    S # t        $ r Y yw xY w)Nz	<unknown>)map_type_nameKeyError)ttypes    rU   get_table_type_namer      s$    U## s    	c           	         i dt         j                  dt         j                  dt         j                  dt         j                  dt         j                  dt         j                  dt         j                  dt         j                  d	t         j
                  d
t         j
                  dt         j                  dt         j                  dt         j                  dt         j                  dt         j                  dt         j                  dt         j                  t         j                  t         j                  t         j                  t         j                  t         j                  dz  t         j                  dz  t         j                  d}t        j                  d      }g }t        j                   | j"                  j$                  | j&                        }d}||k  r
t        j(                  | j"                  j$                  | j&                  |      j+                         }t        j,                  d|      }|j/                  d      }|j/                  d      }	t        j,                  d|	      rd}	|j-                  |	      }	 |rA|j1                  |||j/                  d         t3        |j/                  d            z  f       n|j1                  |||	   f       |dz  }||k  r
t?        dt         j@                  fd|i      S # t4        $ r6 t7        d|	z  t8        j:                         t9        j<                  d       Y hw xY w)Nchars8zunsigned charu8zu8 *zchar *shorts16zunsigned shortu16rd   s32enumzunsigned intu32longzunsigned longr   )z	long longs64zunsigned long longu64__int128zunsigned __int128zvoid *z(\S+(?: \S+)*) ?\[([0-9]+)\]$r   z	(.*)#(.*)r   zenum .*zGType: '%s' not recognized. Please define the data with ctypes manually.)filerK   _fields_)!ctc_charc_ubytec_char_pc_shortc_ushortc_intc_uintc_longc_ulong
c_longlongc_ulonglongc_void_precompiler   bpf_perf_event_fieldsbpfmodule_namebpf_perf_event_fielddecodematchgroupre   rd   r   rq   sysstderrexittype	Structure)
	event_map
ct_mapping
array_typefields
num_fieldsrS   fieldm
field_name
field_types
             rU   _get_event_classr      s   biibii 	bjj 	bjj	
 	bkk 	bkk 	bjj 	bjj 	bkk 	bkk 	bhh 	bhh 	bhh 	bii 	bii  	bii!" 	bjj#$ !mm mm nn nn!}}q0!~~1 kk1J8 <=JF**9==+?+?QJ	A
j.(()=)=yPQRYY[HH\5)WWQZ
WWQZ
88J
+JZ(
	z:aggaj+ACPQ
O+STUz:j+ABC 	
Q+ j., R\\Oj&%9::  	 [%(ZZ1HHQK	s   AL <MMc                    t        j                  | j                  |      }d}|t        k(  rt	        | ||||      }n|t
        k(  rt        | ||||      }n|t        k(  rt        | ||||      }n|t        k(  rt        | |||||      }nl|t        k(  rt        | ||||fi |}nQ|t        k(  rt        | ||||fi |}n6|t        k(  rt!        | ||||      }n|t"        k(  rt%        | ||||      }n|t&        k(  rt)        | ||||      }n|t*        k(  rt-        | ||||      }n|t.        k(  rt1        | ||||      }n|t2        k(  rt5        | ||||      }n|t6        k(  rt9        | ||||      }n|t:        k(  rt=        | ||||      }nl|t>        k(  rtA        | ||||      }nS|tB        k(  rtE        | ||||      }n:|tF        k(  s	|tH        k(  rtK        | |||      }n|tL        k(  rtO        | |||||      }|dk(  rtQ        d|z        |S )z|Table(bpf, map_id, map_fd, keytype, leaftype, **kwargs)

    Create a python object out of a reference to a bpf table handleNzUnknown table type %d))r   bpf_table_type_idr   BPF_MAP_TYPE_HASH	HashTableBPF_MAP_TYPE_ARRAYArrayBPF_MAP_TYPE_PROG_ARRAY	ProgArrayBPF_MAP_TYPE_PERF_EVENT_ARRAYPerfEventArrayBPF_MAP_TYPE_PERCPU_HASH
PerCpuHashBPF_MAP_TYPE_PERCPU_ARRAYPerCpuArrayBPF_MAP_TYPE_LPM_TRIELpmTrieBPF_MAP_TYPE_STACK_TRACE
StackTraceBPF_MAP_TYPE_LRU_HASHLruHashBPF_MAP_TYPE_LRU_PERCPU_HASHLruPerCpuHashBPF_MAP_TYPE_CGROUP_ARRAYCgroupArrayBPF_MAP_TYPE_DEVMAPDevMapBPF_MAP_TYPE_CPUMAPCpuMapBPF_MAP_TYPE_XSKMAPXskMapBPF_MAP_TYPE_ARRAY_OF_MAPSMapInMapArrayBPF_MAP_TYPE_HASH_OF_MAPSMapInMapHashBPF_MAP_TYPE_QUEUEBPF_MAP_TYPE_STACK
QueueStackBPF_MAP_TYPE_RINGBUFRingBuf	Exception)	r   map_idmap_fdkeytypeleaftypenamekwargsr   ts	            rU   Tabler     s6   
 !!#**f5EA!!c667H=	$	$#vvw9	)	)c667H=	/	/34H	*	*sFFGXHH	+	+VVWhI&I	'	'C(;	*	*sFFGX>	'	'C(;	.	.#vvwA	+	+VVWh?	%	%3:	%	%3:	%	%3:	,	,#vvwA	+	+ffgx@	$	$1C(CsFFH5	&	&C(DADy/%788HrW   c                       e Zd Zd"dZd Zd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd#dZd$dZd Zd"dZd Zd Zd%dZd Zd Zd Zd Z G d de      Zd ZefdZ 	 	 d&dZ!	 	 	 d'd Z"	 	 	 d'd!Z#y)(	TableBaseNc                    || _         || _        || _        || _        || _        t        j                  | j                   j                  | j                        | _        t        j                  | j                   j                  | j                        | _
        i | _        || _        t        t        j                  | j                   j                  | j                              | _        y N)r   r   r   KeyLeafr   r   r   r   bpf_table_flags_idflags_cbsr   rd   bpf_table_max_entries_idmax_entries)selfr   r   r   r   r   r   s          rU   __init__zTableBase.__init__B  s    	**488??DKKH
++DHHOOT[[I
	
s;;DHHOO  rW   c                     | j                   S r   )r   r   s    rU   get_fdzTableBase.get_fdO  s    {{rW   c           	      J   t        j                  t        j                  | j                        dz        }t	        j
                  | j                  j                  | j                  |t        |      t        j                  |            }|dk  rt        d      |j                  S )Nr   r   zCould not printf key)r   create_string_buffersizeofr   r   bpf_table_key_snprintfr   r   r   rc   byrefr   value)r   keybufress       rU   key_sprintfzTableBase.key_sprintfR  su    %%bii&9A&=>(($++s),S288C=B7233yyrW   c           	      J   t        j                  t        j                  | j                        dz        }t	        j
                  | j                  j                  | j                  |t        |      t        j                  |            }|dk  rt        d      |j                  S Nr   r   zCould not printf leafr   r   r   r   r   bpf_table_leaf_snprintfr   r   r   rc   r   r   r   r   leafr   r  s       rU   leaf_sprintfzTableBase.leaf_sprintfZ  u    %%bii		&:Q&>?))$((//4;;*-c(BHHTND7344yyrW   c                     | j                         }t        j                  | j                  j                  | j
                  |t        j                  |            }|dk  rt        d      |S )Nr   zCould not scanf key)	r   r   bpf_table_key_sscanfr   r   r   r   r   r   )r   key_strr   r  s       rU   	key_scanfzTableBase.key_scanfb  sR    hhj&&txxW')xx}67122
rW   c                     | j                         }t        j                  | j                  j                  | j
                  |t        j                  |            }|dk  rt        d      |S Nr   zCould not scanf leaf	r   r   bpf_table_leaf_sscanfr   r   r   r   r   r   r   leaf_strr  r  s       rU   
leaf_scanfzTableBase.leaf_scanfj  R    yy{''h(*87233rW   c                     | j                         }t        j                  | j                  t	        j
                  |      t	        j
                  |            }|dk  rt        |S Nr   r   r   bpf_lookup_elemr   r   r   r   r   r   r  r  s       rU   __getitem__zTableBase.__getitem__r  sF    yy{!!$++rxx}bhhtnM7NrW   c                    t        j                  | j                  t        j                  |      t        j                  |      d      }|dk  r5t        j                  t        j                               }t        d|z        y )Nr   zCould not update table: %s	r   bpf_update_elemr   r   r   osstrerror	get_errnor   )r   r   r  r  errstrs        rU   __setitem__zTableBase.__setitem__y  s]    !!$++rxx}bhhtnaP7[[0F86ABB rW   c                     t        j                  | j                  t        j                  |            }|dk  rt
        y r  )r   bpf_delete_elemr   r   r   r   )r   r   r  s      rU   __delitem__zTableBase.__delitem__  s1    !!$++rxx}=7N rW   c              #   H   K   | D ]
  }	 | |     y # t         $ r Y w xY wwr   r   r   r   s     rU   
itervalueszTableBase.itervalues  s6      	C3i		
  s   ""	""c              #   L   K   | D ]  }	 || |   f  y # t         $ r Y w xY wwr   r)  r*  s     rU   	iteritemszTableBase.iteritems  s<      	CDI&&	  s   $	$	!$!$c                 H    | j                         D cg c]  }| c}S c c}w r   )r-  )r   items     rU   itemszTableBase.items  s    !%!12222   	c                 H    | j                         D cg c]  }| c}S c c}w r   r+  r   r   s     rU   valueszTableBase.values      #'??#45%555r1  c                 P    | j                         D ]  }| j                  |        y r   )keysr'  r   ks     rU   clearzTableBase.clear  s%     	 AQ	 rW   c                 &   dx}}|s|st        j                  d      ddfS |s| j                  }n|dk  s|| j                  kD  rt        d      |r | j                  |z         }|r | j
                  |z         }t        j                  |      ||fS )a  Allocate keys and/or values arrays. Useful for in items_*_batch.

        Args:
            alloc_k (bool): True to allocate keys array, False otherwise.
            Default is False.
            alloc_v (bool): True to allocate values array, False otherwise.
            Default is False.
            count (int): number of elements in the array(s) to allocate. If
            count is None then it allocates the maximum number of elements i.e
            self.max_entries.

        Returns:
            tuple: (count, keys, values). Where count is ct.c_uint32,
            and keys and values an instance of ct.Array
        Raises:
            ValueError: If count is less than 1 or greater than
            self.max_entries.
        Nr   r   zWrong count)r   c_uint32r   
ValueErrorr   r   )r   alloc_kalloc_vr_   r8  r5  s         rU   _alloc_keys_valueszTableBase._alloc_keys_values  s    & vwKKND$//$$EQY%$"2"22]++$DHHu$'D'dii%'*FE"D&11rW   c                 (   d}||fD ]O  }|st        |t        j                        st        t	        |      }|dk  s|| j
                  kD  sFt        d       |r$|r"t	        |      t	        |      k7  rt        d      t        j                  |      S )a  Check if the given keys or values have the right type and size.

        Args:
            keys (ct.Array): keys array to check
            values (ct.Array): values array to check
        Returns:
            ct.c_uint32 : the size of the array(s)
        Raises:
            ValueError: If length of arrays is less than 1 or greater than
            self.max_entries, or when both arrays length are different.
            TypeError: If the keys and values are not an instance of ct.Array
        r   r   zArray's length is wrongz(keys array length != values array length)
isinstancer   r   	TypeErrorrc   r   r>  r=  )r   r8  r5  arr_lenelems        rU   _sanity_check_keys_valuesz#TableBase._sanity_check_keys_values  s     6N 	@D!$1#Od)Q;'D,<,<"<$%>??	@ F4yCK' !KLL{{7##rW   c              #   L   K   | j                  d      D ]  \  }}||f  yw)a  Look up all the key-value pairs in the map.

        Args:
            None
        Yields:
            tuple: The tuple of (key,value) for every entries that have
            been looked up.
        Notes: lookup batch on a keys subset is not supported by the kernel.
        FdeleteN)_items_lookup_and_optionally_delete_batchr   r:  ru   s      rU   items_lookup_batchzTableBase.items_lookup_batch  s5      BB%BP 	DAqQK	   "$c                 X   || j                  |      }t        j                  | j                  t	        j
                  |      t	        j
                  |            }|dk7  r3t        dt        j                  t	        j                               z        y| j                         D ]  } y y)a  Delete the key-value pairs related to the keys given as parameters.
        Note that if no key are given, it is faster to call
        lib.bpf_lookup_and_delete_batch than create keys array and then call
        lib.bpf_delete_batch on these keys.

        Args:
            ct_keys (ct.Array): keys array to delete. If an array of keys is
            given then it deletes all the related keys-values.
            If keys is None (default) then it deletes all entries.
        Yields:
            tuple: The tuple of (key,value) for every entries that have
            been deleted.
        Raises:
            Exception: If bpf syscall return value indicates an error.
        N)r8  r   z#BPF_MAP_DELETE_BATCH has failed: %s)rG  r   bpf_delete_batchr   r   r   r   r   r!  r"  items_lookup_and_delete_batch)r   ct_keysct_cntr  _s        rU   items_delete_batchzTableBase.items_delete_batch  s      333AF&&t{{')xx'8')xx'7)C q E"$++blln"=!> ? ? 
 779 rW   c                 P   | j                  ||      }t        j                  | j                  t	        j
                  |      t	        j
                  |      t	        j
                  |            }|dk7  r3t        dt        j                  t	        j                               z        y)aq  Update all the key-value pairs in the map provided.
        The arrays must be the same length, between 1 and the maximum number
        of entries.

        Args:
            ct_keys (ct.Array): keys array to update
            ct_values (ct.Array): values array to update
        Raises:
            Exception: If bpf syscall return value indicates an error.
        )r8  r5  r   z#BPF_MAP_UPDATE_BATCH has failed: %sN)
rG  r   bpf_update_batchr   r   r   r   r   r!  r"  )r   rS  	ct_valuesrT  r  s        rU   items_update_batchzTableBase.items_update_batch  s     //WY/O""4;;#%88G#4#%88I#6#%88F#3%
 1HA kk",,.9: ; ; rW   c              #   L   K   | j                  d      D ]  \  }}||f  yw)a?  Look up and delete all the key-value pairs in the map.

        Args:
            None
        Yields:
            tuple: The tuple of (key,value) for every entries that have
            been looked up and deleted.
        Notes: lookup and delete batch on a keys subset is not supported by
        the kernel.
        TrI  NrK  rM  s      rU   rR  z'TableBase.items_lookup_and_delete_batch#  s5      BB$BO 	DAqQK	rO  c              #   Z  K   |du rt         j                  }d}nt         j                  }d}| j                  dd      \  }}}t	        j
                  d      x}}d}		 |j                  |	z
  |_         || j                  |	rt	        j                  |      ndt	        j                  |      t	        j                  |t	        j                  | j                        |	z        t	        j                  |t	        j                  | j                        |	z        t	        j                  |            }
t	        j                         }|	|j                  z  }	|
dk7  r6|t        j                  k7  r#t        |dt!        j"                  |            |
dk7  rn"|	|j                  k(  rn|j                  dk(  rnVt%        d|	      D ]h  }||   }||   }t'        |t        j(                        s| j                  |      }t'        |t        j(                        s| j                  |      }||f j yw)a  Look up and optionally delete all the key-value pairs in the map.

        Args:
            delete (bool) : look up and delete the key-value pairs when True,
            else just look up.
        Yields:
            tuple: The tuple of (key,value) for every entries that have
            been looked up and deleted.
        Raises:
            Exception: If bpf syscall return value indicates an error.
        Notes: lookup and delete batch on a keys subset is not supported by
        the kernel.
        TBPF_MAP_LOOKUP_AND_DELETE_BATCHBPF_MAP_LOOKUP_BATCH)r?  r@  r   Nz has failed: )r   bpf_lookup_and_delete_batchbpf_lookup_batchrA  r   r=  r   r   r   r   r   r   r"  errnoENOENTr   r   r!  rb   rC  r   )r   rJ  	bpf_batchbpf_cmdct_buf_sizerS  rY  ct_out_batchrT  totalr  errcoderS   r:  ru   s                  rU   rL  z3TableBase._items_lookup_and_optionally_delete_batch2  s     T>77I7G,,I,G +/*A*A$JN +B +P'Wi "A.v&,,u4FLDKK6;BHH\2HH\2HHWbii.AE.IJHHY		$))0Du0LMHHV,C llnGV\\!EqW4w79{{77K!M N N ax)))||q  1 4 q% 	A
A!Aa.HHQKa.IIaLa&L	s   H)H+c                 f    t        | j                               D ]  }| j                         | |<    y r   )listr8  r   r9  s     rU   zerozTableBase.zeroo  s-    
 diik" 	"AiikDG	"rW   c                 ,    t         j                  |       S r   )r   Iterr   s    rU   __iter__zTableBase.__iter__w  s    ~~d##rW   c                 "    | j                         S r   rn  r   s    rU   iterzTableBase.iterz      4==?*rW   c                 "    | j                         S r   rp  r   s    rU   r8  zTableBase.keys{  rr  rW   c                   $    e Zd Zd Zd Zd Zd Zy)TableBase.Iterc                      || _         d | _        y r   )tabler   )r   rw  s     rU   r   zTableBase.Iter.__init__~  s    DJDHrW   c                     | S r   rO   r   s    rU   rn  zTableBase.Iter.__iter__      KrW   c                 "    | j                         S r   nextr   s    rU   __next__zTableBase.Iter.__next__      99;rW   c                 n    | j                   j                  | j                        | _        | j                  S r   )rw  r|  r   r   s    rU   r|  zTableBase.Iter.next  s#    zztxx0DH88OrW   N__name__
__module____qualname__r   rn  r}  r|  rO   rW   rU   rm  ru  }  s    				rW   rm  c                 z   | j                         }|Rt        j                  | j                  t	        j
                  |      t	        j                  | j                               }nGt        j                  | j                  t	        j
                  |      t	        j
                  |            }|dk  r
t               |S r  )	r   r   bpf_get_first_keyr   r   r   r   bpf_get_next_keyStopIteration)r   r   next_keyr  s       rU   r|  zTableBase.next  s    88:;''RXXh5G(*		$(((;=C &&t{{BHHSM')xx'9;C 7/!rW   c                 8   | j                   j                  d   d   }| j                   j                  d   d   }|dk(  r>t        | j                   j                        dk(  r| j                   j                  d   d   }| j                         D ]Q  \  }}	t	        ||      }
|r ||
      }
|j                  |
dg|z        x}||
<   t	        ||      }|	j                  ||<   S t        |j                               }|r ||      }|D ]  }
|j                  |
        y )Nr   r   __pad_1r   r   )
r   r   rc   r0  getattrgetr   rj  r8  re   )r   tmpbuckets	bucket_fnbucket_sort_fn	index_maxf1f2r:  ru   bucketr[   slotbuckets_lsts                 rU   decode_c_structzTableBase.decode_c_struct  s   XXq!!$XXq!!$ ?s488#4#45:""1%a(BJJL 	!DAqQ^F"6*!$!y!AAD3v;1b>DDJ	! 388:&(5K! 	#FNN6"	#rW   c                    t        | j                         t        j                        rLi }g }| j	                  ||||       |D ].  }||   }	|r| ||      f}
n||f}
t        t        |	||
             0 ydgt        z  }	| j                         D ]  \  }}|j                  |	|j                  <     t        t        |	|             y)a.  print_json_hist(val_type="value", section_header="Bucket ptr",
                                   section_print_fn=None, bucket_fn=None,
                                   bucket_sort_fn=None):

                Prints a table as a json histogram. The table must be stored as
                log2. The val_type argument is optional, and is a column header.
                If the histogram has a secondary key, the dictionary will be split by secondary key
                If section_print_fn is not None, it will be passed the bucket value
                to format into a string as it sees fit. If bucket_fn is not None,
                it will be used to produce a bucket value for the histogram keys.
                If bucket_sort_fn is not None, it will be used to sort the buckets
                before iterating them, and it is useful when there are multiple fields
                in the secondary key.
                The maximum index allowed is log2_index_max (65), which will
                accommodate any 64-bit integer in the histogram.
                r   N)
rC  r   r   r   r  rq   rZ   log2_index_maxr0  r   )r   r\   section_headersection_print_fnr  r  r  r  r  r[   rY   r:  ru   s                rU   print_json_histzTableBase.print_json_hist  s    $ dhhj",,/CG  gy.I! F6{#&46Fv6N%ON&4f%=NnT8^DEF 3'D

 (1 !QWW(.x01rW   c           	         t        | j                         t        j                        r]i }g }| j	                  ||||       |D ]?  }	||	   }
|rt        d|d ||	             nt        d|d|	       t        |
||       A ydgt        z  }
| j                         D ]  \  }}|j                  |
|j                  <     t        |
||       y)a  print_log2_hist(val_type="value", section_header="Bucket ptr",
                           section_print_fn=None, bucket_fn=None,
                           strip_leading_zero=None, bucket_sort_fn=None):

        Prints a table as a log2 histogram. The table must be stored as
        log2. The val_type argument is optional, and is a column header.
        If the histogram has a secondary key, multiple tables will print
        and section_header can be used as a header description for each.
        If section_print_fn is not None, it will be passed the bucket value
        to format into a string as it sees fit. If bucket_fn is not None,
        it will be used to produce a bucket value for the histogram keys.
        If the value of strip_leading_zero is not False, prints a histogram
        that is omitted leading zeros from the beginning.
        If bucket_sort_fn is not None, it will be used to sort the buckets
        before iterating them, and it is useful when there are multiple fields
        in the secondary key.
        The maximum index allowed is log2_index_max (65), which will
        accommodate any 64-bit integer in the histogram.
        
 = r   N)
rC  r   r   r   r  rq   r{   r  r0  r   r   r\   r  r  r  rr   r  r  r  r  r[   r:  ru   s                rU   print_log2_histzTableBase.print_log2_hist  s    , dhhj",,/CG  gy.I! E6{#(02 3 @A x1CDE 3'D

 (1 !QWW(T8-?@rW   c           	          t        | j                         t        j                        rbi }g }| j	                  ||||t
               |D ]?  }	||	   }
|rt        d|d ||	             nt        d|d|	       t        |
||       A ydgt
        z  }
| j                         D ]  \  }}	 |j                  |
|j                  <   ! t        |
||       y# t        $ r t        d|j                  t
        fz        w xY w)a  print_linear_hist(val_type="value", section_header="Bucket ptr",
                           section_print_fn=None, bucket_fn=None,
                           strip_leading_zero=None, bucket_sort_fn=None)

        Prints a table as a linear histogram. This is intended to span integer
        ranges, eg, from 0 to 100. The val_type argument is optional, and is a
        column header.  If the histogram has a secondary key, multiple tables
        will print and section_header can be used as a header description for
        each.  If section_print_fn is not None, it will be passed the bucket
        value to format into a string as it sees fit. If bucket_fn is not None,
        it will be used to produce a bucket value for the histogram keys.
        If the value of strip_leading_zero is not False, prints a histogram
        that is omitted leading zeros from the beginning.
        If bucket_sort_fn is not None, it will be used to sort the buckets
        before iterating them, and it is useful when there are multiple fields
        in the secondary key.
        The maximum index allowed is linear_index_max (1025), which is hoped
        to be sufficient for integer ranges spanned.
        r  r  r   z5Index in print_linear_hist() of %d exceeds max of %d.N)rC  r   r   r   r  linear_index_maxrq   r}   r0  r   
IndexErrorr  s                rU   print_linear_histzTableBase.print_linear_hist  s   , dhhj",,/CG  gy.JZ[! G6{#(02 3 @A"43EFG 3))D

 M1M$%GGDMM tX/AB " M % '-12:J0K&L M MMs   ,C(C=r   )FFN)NN)T)r   
Bucket ptrNNN)r   r  NNNN)$r  r  r  r   r   r  r	  r  r  r  r$  r'  r+  r-  r0  r5  r;  rA  rG  rN  rV  rZ  rR  rL  rk  rn  rq  r8  objectrm  r|  r  r  r  r  r  rO   rW   rU   r   r   @  s    C36 
!2F$<<;*;z"$ +*
v 
 R` #. @LNR"2H @LFJ&AP BNFJ-CrW   r   c                   $     e Zd Z fdZd Z xZS )r   c                 ,    t        t        | 
  |i | y r   )superr   r   r   argsr   	__class__s      rU   r   zHashTable.__init__+      i'88rW   c                 "    d}| D ]  }|dz  }	 |S Nr   r   rO   r   rS   r:  s      rU   __len__zHashTable.__len__.      AqAvqrW   r  r  r  r   r  __classcell__r  s   @rU   r   r   *  s    9rW   r   c                        e Zd Z fdZ xZS )r   c                 ,    t        t        | 
  |i | y r   )r  r   r   r  s      rU   r   zLruHash.__init__4      gt%t6v6rW   r  r  r  r   r  r  s   @rU   r   r   3  s    7 7rW   r   c                   j     e Zd Z fdZd Zd Z fdZ fdZ fdZd Z	d Z
 G d	 d
e      Z xZS )	ArrayBasec                 ,    t        t        | 
  |i | y r   )r  r  r   r  s      rU   r   zArrayBase.__init__8  r  rW   c                     t        |t              r$|dk  rt        |       |z   }| j                  |      }t        |t        j
                        st        d      |j                  t        |       k\  rt        d      |S )Nr   z#Array index must be an integer typezArray index out of range)rC  rd   rc   r   r   _SimpleCDatar  r   r*  s     rU   _normalize_keyzArrayBase._normalize_key;  sh    c3Qw$i#o((3-C#r/BCC99D	!788
rW   c                     | j                   S r   )r   r   s    rU   r  zArrayBase.__len__F  s    rW   c                 L    | j                  |      }t        t        |   |      S r   )r  r  r  r  r   r   r  s     rU   r  zArrayBase.__getitem__I  s%    !!#&Y1#66rW   c                 P    | j                  |      }t        t        |   ||       y r   )r  r  r  r$  r   r   r  r  s      rU   r$  zArrayBase.__setitem__M  s$    !!#&i*35rW   c                 N    | j                  |      }t        t        |   |       y r   )r  r  r  r'  r  s     rU   r'  zArrayBase.__delitem__Q  s"    !!#&i*3/rW   c                     | j                  |      }| j                         }t        j                  | j                  t        j                  |      t        j                  |      d      }|dk  rt        d      y )Nr   zCould not clear item)r  r   r   r  r   r   r   r   r  s       rU   	clearitemzArrayBase.clearitemU  s_    !!#&yy{!!$++rxx}bhhtnaP7233 rW   c                 B    t         j                  | | j                        S r   )r  rm  r   r   s    rU   rn  zArrayBase.__iter__\  s    ~~dDHH--rW   c                   $    e Zd Zd Zd Zd Zd Zy)ArrayBase.Iterc                 .    || _         || _        d| _        y NrM   )r   rw  rS   )r   rw  r   s      rU   r   zArrayBase.Iter.__init__`  s    DHDJDFrW   c                     | S r   rO   r   s    rU   rn  zArrayBase.Iter.__iter__e  ry  rW   c                 "    | j                         S r   r{  r   s    rU   r}  zArrayBase.Iter.__next__g  r~  rW   c                     | xj                   dz  c_         | j                   t        | j                        k(  r
t               | j	                  | j                         S Nr   )rS   rc   rw  r  r   r   s    rU   r|  zArrayBase.Iter.nexti  s>    FFaKFvvTZZ(#o%88DFF##rW   Nr  rO   rW   rU   rm  r  _  s    	
			$rW   rm  )r  r  r  r   r  r  r  r$  r'  r  rn  r  rm  r  r  s   @rU   r  r  7  s<    9	 7604.$v $ $rW   r  c                   $     e Zd Z fdZd Z xZS )r   c                 ,    t        t        | 
  |i | y r   )r  r   r   r  s      rU   r   zArray.__init__p  s    eT#T4V4rW   c                 &    | j                  |       y r   r  r*  s     rU   r'  zArray.__delitem__s      srW   )r  r  r  r   r'  r  r  s   @rU   r   r   o  s    5rW   r   c                   (     e Zd Z fdZ fdZ xZS )r   c                 ,    t        t        | 
  |i | y r   )r  r   r   r  s      rU   r   zProgArray.__init__x  r  rW   c                     t        |t              r| j                  |      }t        || j                  j                        r| j                  |j
                        }t        t        | #  ||       y r   )	rC  rd   r   r   Functionfdr  r   r$  r  s      rU   r$  zProgArray.__setitem__{  sQ    dC 99T?DdDHH--.99TWW%Di*35rW   r  r  r  r   r$  r  r  s   @rU   r   r   w  s    96 6rW   r   c                   *    e Zd Zd Zd Zd Zd Zd Zy)FileDescc                 6    ||dk  rt        d      || _        y )Nr   zInvalid file descriptor)r   r  )r   r  s     rU   r   zFileDesc.__init__  s     JBF566rW   c                     | j                   7| j                   dk\  r't        j                  | j                          d | _         y y y r  )r  r   closer   s    rU   clean_upzFileDesc.clean_up  s5    GGdgglHHTWWDG '3rW   c                 $    | j                          y r   r  r   s    rU   __del__zFileDesc.__del__      rW   c                     | S r   rO   r   r  r   s      rU   	__enter__zFileDesc.__enter__  s    rW   c                 $    | j                          y r   r  r  s      rU   __exit__zFileDesc.__exit__  r  rW   N)r  r  r  r   r  r  r  r  rO   rW   rU   r  r    s    

rW   r  c                   (     e Zd Z fdZ fdZ xZS )r   c                 ,    t        t        | 
  |i | y r   )r  r   r   r  s      rU   r   zCgroupArray.__init__  s    k4)4:6:rW   c                    t        |t              r$t        t        |   || j                  |             y t        |t              rdt        t        j                  |t        j                              5 }t        t        |   || j                  |j                               d d d        y t        d      # 1 sw Y   y xY w)Nz1Cgroup array key must be either FD or cgroup path)rC  rd   r  r   r$  r   strr  r   openO_RDONLYr  r   )r   r   r  fr  s       rU   r$  zCgroupArray.__setitem__  s    dC +t0diioFc""''$45 Kk44S$))ADD/JK K OPPK Ks   3.B55B>r  r  s   @rU   r   r     s    ;Q QrW   r   c                   R     e Zd Z fdZd Z fdZd Zd	dZd Zd
dZ	d
dZ
 xZS )r   c                 H    t        t        | 
  |i | i | _        d | _        y r   )r  r   r   _open_key_fds_event_classr  s      rU   r   zPerfEventArray.__init__  s'    nd,d=f= rW   c                 ^    t        | j                  j                               }|D ]  }| |=  y r   )rj  r  r8  )r   r8  r   s      rU   r  zPerfEventArray.__del__  s1    D&&++-. 	CS		rW   c                    || j                   vry t        t        |   |       t	        |       |f}|| j
                  j                  v rQt        j                  | j
                  j                  |          | j
                  j                  |= | j                  |= n"t        j                  | j                   |          | j                   |= y r   )r  r  r   r'  idr   perf_buffersr   perf_reader_freer   bpf_close_perf_event_fd)r   r   key_idr  s      rU   r'  zPerfEventArray.__delitem__  s    d(((nd/4T(CTXX***  !6!6v!>?%%f-		# ''(:(:3(?@s#rW   c                     | j                   dk(  rt        |       | _         t        j                  |t        j                  | j                               j
                  S )a	  event(data)

        When perf buffers are opened to receive custom perf event,
        the underlying event data struct which is defined in C in
        the BPF program can be deduced via this function. This avoids
        redundant definitions in Python.
        Nr  r   r   castPOINTERcontentsr   ra   s     rU   eventzPerfEventArray.event  E     $ 0 6DwwtRZZ(9(9:;DDDrW   c                 x    ||dz
  z  dk7  rt        d      t               D ]  }| j                  |||||        y)az  open_perf_buffers(callback)

        Opens a set of per-cpu ring buffer to receive custom perf event
        data from the bpf program. The callback will be invoked for each
        event submitted from the kernel, up to millions per second. Use
        page_cnt to change the size of the per-cpu ring buffer. The value
        must be a power of two and defaults to 8.
        r   r   z+Perf buffer page_cnt must be a power of twoN)r   r   _open_perf_buffer)r   callbackpage_cntlost_cbwakeup_eventsrS   s         rU   open_perf_bufferzPerfEventArray.open_perf_buffer  sK     x!|$)IJJ " 	RA""1h'=Q	RrW   c           	      ,   fd}fd}t        |      }rt        |      nt        j                  d t              }	t	               }
d|
_        |
_        ||
_        t        j                  ||	d |t        j                  |
            }|st        d      t        j                  |      }| j                  |      | | j                        <   || j                  j                   t#        |       f<   ||	f| j$                  <   d| j&                  <   y )Nc                     	  ||       y # t         $ r4}|j                  t        j                  k(  rt                n|Y d }~y d }~ww xY wr   IOErrorra  EPIPEr   )rU  ra   sizeer  cpus       rU   raw_cb_z1PerfEventArray._open_perf_buffer.<locals>.raw_cb_  sC    dD) 77ekk)FG s   
 	A*AAc                     	  |       y # t         $ r4}|j                  t        j                  k(  rt                n|Y d }~y d }~ww xY wr   r  )rU  lostr  r
  s      rU   lost_cb_z2PerfEventArray._open_perf_buffer.<locals>.lost_cb_  s>     77ekk)FG s    	A	*AA	rM   zCould not open perf buffer)r	   r
   r   r   r   pidr  r  r   bpf_open_perf_buffer_optsr   r   perf_reader_fdr   r   r   r  r  r   r  )r   r  r  r	  r
  r  r  r  fnlost_fnoptsreaderr  s    `` `        rU   r  z PerfEventArray._open_perf_buffer  s    		 '"-4-)"''$:V#%*..r7D(BHHUYN[899'"iimTXXc]17r$xo.g		#"$3rW   c                     t        j                  ||||      }|dk  rt        d      | j                  |      | | j	                  |      <   || j
                  |<   y )Nr   zbpf_open_perf_event failed)r   bpf_open_perf_eventr   r   r   r  )r   r  typconfigr  r  s         rU   _open_perf_eventzPerfEventArray._open_perf_event  sU    $$S&#s;6899"iimTXXc]"$3rW   c                 J    t               D ]  }| j                  ||||        y)zopen_perf_event(typ, config)

        Configures the table such that calls from the bpf program to
        table.perf_read(CUR_CPU_IDENTIFIER) will return the hardware
        counter denoted by event ev on the local cpu.
        N)r   r$  )r   r"  r#  r  rS   s        rU   open_perf_eventzPerfEventArray.open_perf_event  s*     !" 	7A!!!S&#6	7rW   )r   Nr   )rM   )r  r  r  r   r  r'  r  r  r  r$  r&  r  r  s   @rU   r   r     s.    !

$ 
ER  %D%7rW   r   c                   J     e Zd Z fdZ fdZd Z fdZd Zd Zd Z	 xZ
S )r   c                 j   |j                  dd       | _        t        t        |   |i | | j
                  | _        t        t                     | _	        t        j                  | j                        dz  | _        | j                  dk(  r| j                  | j                  z  | _        y | j                  t        j                  k(  r#t        j                  | j                  z  | _        y | j                  t        j                  k(  r#t        j                   | j                  z  | _        y t#        d      Nreducerr   r   zLeaf must be aligned to 8 bytes)popr*  r  r   r   r   sLeafrc   r   	total_cpur   r   	alignmentr   c_uint64r   c_int64r  r  s      rU   r   zPerCpuHash.__init__  s    zz)T2j$($9&9YY
.014::.2>>Q

T^^3DI zzRYY&KK$..8	rxx'JJ7	 !BCCrW   c                     t         t        |   |      }| j                  dk(  r|}|S  | j                  | j
                  z         }t        d| j
                        D ]
  }||   ||<    |S r  )r  r   r  r.  r,  r-  rb   r   r   resultretrS   r  s        rU   getvaluezPerCpuHash.getvalue#  sq    z44S9>>QC
 
 /4::.1C1dnn- #A#
rW   c                     | j                   r%t        | j                   | j                  |            S | j                  |      S r   r*  r   r5  r*  s     rU   r  zPerCpuHash.__getitem__-  s2    <<$,,c(:;;==%%rW   c                 .    t         t        |   ||       y r   )r  r   r$  r  s      rU   r$  zPerCpuHash.__setitem__3  s    j$+C6rW   c                     t        | j                         t        j                        rt	        d      | j                  t        | j                  |                  S Nz6Leaf must be an integer type for default sum functionsrC  r   r   r   r  r,  sumr5  r*  s     rU   r<  zPerCpuHash.sum6  ?    diik2<<0UVVzz#dmmC0122rW   c                     t        | j                         t        j                        rt	        d      | j                  t        | j                  |                  S Nz6Leaf must be an integer type for default max functionsrC  r   r   r   r  r,  maxr5  r*  s     rU   rA  zPerCpuHash.max;  r=  rW   c                 V    | j                  |      }|j                  | j                  z  S r   r<  r   r-  r   r   r3  s      rU   averagezPerCpuHash.average@  "    #||dnn,,rW   )r  r  r  r   r5  r  r$  r<  rA  rE  r  r  s   @rU   r   r     s'    D$&73
3
-rW   r   c                        e Zd Z fdZ xZS )r   c                 ,    t        t        | 
  |i | y r   )r  r   r   r  s      rU   r   zLruPerCpuHash.__init__E      mT+T<V<rW   r  r  s   @rU   r   r   D      = =rW   r   c                   P     e Zd Z fdZ fdZd Z fdZd Zd Zd Z	d Z
 xZS )	r   c                 j   |j                  dd       | _        t        t        |   |i | | j
                  | _        t        t                     | _	        t        j                  | j                        dz  | _        | j                  dk(  r| j                  | j                  z  | _        y | j                  t        j                  k(  r#t        j                  | j                  z  | _        y | j                  t        j                  k(  r#t        j                   | j                  z  | _        y t#        d      r)  )r+  r*  r  r   r   r   r,  rc   r   r-  r   r   r.  r   r/  r   r0  r  r  s      rU   r   zPerCpuArray.__init__I  s    zz)T2k4)4:6:YY
.014::.2>>Q

T^^3DI zzRYY&KK$..8	rxx'JJ7	 !BCCrW   c                     t         t        |   |      }| j                  dk(  r|}|S  | j                  | j
                  z         }t        d| j
                        D ]
  }||   ||<    |S r  )r  r   r  r.  r,  r-  rb   r2  s        rU   r5  zPerCpuArray.getvalue[  sq    {D5c:>>QC
 
 /4::.1C1dnn- #A#
rW   c                     | j                   r%t        | j                   | j                  |            S | j                  |      S r   r7  r*  s     rU   r  zPerCpuArray.__getitem__e  s2    LL$,,c(:;;==%%rW   c                 .    t         t        |   ||       y r   )r  r   r$  r  s      rU   r$  zPerCpuArray.__setitem__k  s    k4,S$7rW   c                 &    | j                  |       y r   r  r*  s     rU   r'  zPerCpuArray.__delitem__n  r  rW   c                     t        | j                         t        j                        rt	        d      | j                  t        | j                  |                  S r:  r;  r*  s     rU   r<  zPerCpuArray.sumr  r=  rW   c                     t        | j                         t        j                        rt	        d      | j                  t        | j                  |                  S r?  r@  r*  s     rU   rA  zPerCpuArray.maxw  r=  rW   c                 V    | j                  |      }|j                  | j                  z  S r   rC  rD  s      rU   rE  zPerCpuArray.average|  rF  rW   )r  r  r  r   r5  r  r$  r'  r<  rA  rE  r  r  s   @rU   r   r   H  s,    D$&83
3
-rW   r   c                   $     e Zd Z fdZd Z xZS )r   c                 ,    t        t        | 
  |i | y r   )r  r   r   r  s      rU   r   zLpmTrie.__init__  r  rW   c                     t         r   )NotImplementedErrorr   s    rU   r  zLpmTrie.__len__  s    !!rW   r  r  s   @rU   r   r     s    7"rW   r   c                   \     e Zd ZdZdZdZdZdZ fdZ G d de	      Z
dd	Zd
 Zd Z xZS )r      rn   r   r   r   c                 ,    t        t        | 
  |i | y r   )r  r   r   r  s      rU   r   zStackTrace.__init__  s    j$($9&9rW   c                   &    e Zd ZddZd Zd Zd Zy)StackTrace.StackWalkerNc                 <    || _         d| _        || _        || _        y r  )stacknresolver   )r   r^  r   r`  s       rU   r   zStackTrace.StackWalker.__init__  s    DJDF"DLDJrW   c                     | S r   rO   r   s    rU   rn  zStackTrace.StackWalker.__iter__  ry  rW   c                 "    | j                         S r   r{  r   s    rU   r}  zStackTrace.StackWalker.__next__  r~  rW   c                 $   | xj                   dz  c_         | j                   t        j                  k(  r
t               | j                  t        j
                  z  rg| j                  j                  | j                      }|j                  t        j                  k(  s|j                  t        j                  k(  r-t               | j                  j                  | j                      }|dk(  r
t               | j                  r| j                  |      S |S )Nr   r   )r_  r   	MAX_DEPTHr  r   BPF_F_STACK_BUILD_IDr^  tracestatusBPF_STACK_BUILD_ID_IPBPF_STACK_BUILD_ID_EMPTYipr`  )r   addrs     rU   r|  zStackTrace.StackWalker.next  s    FFaKFvv---#o%zzJ;;;ZZ%%dff-d
 @ @@
 C CC%'ZZ]]466*dqy#o%)-4<<%?4?rW   r   r  rO   rW   rU   StackWalkerr\    s    				@rW   rl  c                 h    t         j                  | | j                  |         | j                  |      S r   )r   rl  r   r   )r   stack_idr`  s      rU   walkzStackTrace.walk  s)    %%d488H+=&>

GTTrW   c                 "    d}| D ]  }|dz  }	 |S r  rO   r  s      rU   r  zStackTrace.__len__  r  rW   c                      y r   rO   r   s    rU   r;  zStackTrace.clear      rW   r   )r  r  r  rd  re  ri  BPF_STACK_BUILD_ID_VALIDrh  r   r  rl  ro  r  r;  r  r  s   @rU   r   r     sC    I  ! :@f @>U
rW   r   c                        e Zd Z fdZ xZS )r   c                 ,    t        t        | 
  |i | y r   )r  r   r   r  s      rU   r   zDevMap.__init__      fd$d5f5rW   r  r  s   @rU   r   r         6 6rW   r   c                        e Zd Z fdZ xZS )r   c                 ,    t        t        | 
  |i | y r   )r  r   r   r  s      rU   r   zCpuMap.__init__  rv  rW   r  r  s   @rU   r   r     rw  rW   r   c                        e Zd Z fdZ xZS )r   c                 ,    t        t        | 
  |i | y r   )r  r   r   r  s      rU   r   zXskMap.__init__  rv  rW   r  r  s   @rU   r   r     rw  rW   r   c                        e Zd Z fdZ xZS )r   c                 ,    t        t        | 
  |i | y r   )r  r   r   r  s      rU   r   zMapInMapArray.__init__  rI  rW   r  r  s   @rU   r   r     rJ  rW   r   c                        e Zd Z fdZ xZS )r   c                 ,    t        t        | 
  |i | y r   )r  r   r   r  s      rU   r   zMapInMapHash.__init__  s    lD*D;F;rW   r  r  s   @rU   r   r     s    < <rW   r   c                   >     e Zd Z fdZd Zd Zd Zd ZddZ xZ	S )r   c                 H    t        t        | 
  |i | d | _        d | _        y r   )r  r   r   _ringbufr  r  s      rU   r   zRingBuf.__init__  s&    gt%t6v6 rW   c                      y r   rO   r*  s     rU   	__delitemzRingBuf.__delitem  rr  rW   c                      y r   rO   r   s    rU   r  zRingBuf.__del__  rr  rW   c                      yr  rO   r   s    rU   r  zRingBuf.__len__  s    rW   c                     | j                   dk(  rt        |       | _         t        j                  |t        j                  | j                               j
                  S )a  event(data)

        When ring buffers are opened to receive custom event,
        the underlying event data struct which is defined in C in
        the BPF program can be deduced via this function. This avoids
        redundant definitions in Python.
        Nr  r  s     rU   r  zRingBuf.event  r  rW   c                     fd}t        |      }| j                  j                  | j                  ||       || j                  d<   y)zopen_ring_buffer(callback)

        Opens a ring buffer to receive custom event data from the bpf program.
        The callback will be invoked for each event submitted from the kernel,
        up to millions per second.
        c                     	  | ||      }	 t        |      }|S #  d}Y |S xY w# t        $ r5}|j                  t        j                  k(  rt	                n|Y d }~S d }~ww xY wr  )rd   r  ra  r  r   )ctxra   r  r4  r  r  s        rU   ringbuf_cb_z-RingBuf.open_ring_buffer.<locals>.ringbuf_cb_  su    sD$/c(C JC J  77ekk)FG  Js$   
%  "% % 	A#*AA#r   N)r   r   _open_ring_bufferr   r   )r   r  r  r  r  s    `   rU   open_ring_bufferzRingBuf.open_ring_buffer  s;    	" k*""4;;C8		!rW   r   )
r  r  r  r   _RingBuf__delitemr  r  r  r  r  r  s   @rU   r   r     s"    !

ErW   r   c                   B    e Zd ZdZd Zd Zd ZddZd Zd Z	d Z
d	 Zy
)r   r   c                    || _         || _        || _        || _        t	        j
                  | j                   j                  | j                        | _        t	        j                  | j                   j                  | j                        | _	        t        t	        j                  | j                   j                  | j                              | _        y r   )r   r   r   r   r   r   r   r   r   r   rd   r   r   )r   r   r   r   r   s        rU   r   zQueueStack.__init__  s    	**488??DKKH
++DHHOOT[[I
s;;DHHOO  rW   c           	      J   t        j                  t        j                  | j                        dz        }t	        j
                  | j                  j                  | j                  |t        |      t        j                  |            }|dk  rt        d      |j                  S r  r  r  s       rU   r	  zQueueStack.leaf_sprintf  r
  rW   c                     | j                         }t        j                  | j                  j                  | j
                  |t        j                  |            }|dk  rt        d      |S r  r  r  s       rU   r  zQueueStack.leaf_scanf  r  rW   c                     t        j                  | j                  d t        j                  |      |      }|dk  r5t        j                  t        j                               }t        d|z        y )Nr   zCould not push to table: %sr  )r   r  r   r  r#  s        rU   pushzQueueStack.push'  sU    !!$++tRXXd^UK7[[0F9FBCC rW   c                     | j                         }t        j                  | j                  d t	        j
                  |            }|dk  rt        d      |S )Nr   zCould not pop from table)r   r   bpf_lookup_and_deleter   r   r   r   r   r  r  s      rU   r+  zQueueStack.pop-  sE    yy{''T288D>J7566rW   c                     | j                         }t        j                  | j                  d t	        j
                  |            }|dk  rt        d      |S )Nr   zCould not peek tabler  r  s      rU   peekzQueueStack.peek4  sE    yy{!!$++tRXXd^D7122rW   c              #   ~   K   | j                   }|r	 | j                          |dz  }|ry y # t        $ r Y y w xY wwr  )r   r+  r   )r   cnts     rU   r+  zQueueStack.itervalues;  sF     hhj!q   s    =. ==	:=:=c                 H    | j                         D cg c]  }| c}S c c}w r   r3  r4  s     rU   r5  zQueueStack.valuesE  r6  r1  N)r   )r  r  r  	BPF_EXISTr   r	  r  r  r+  r  r+  r5  rO   rW   rU   r   r   	  s0    ID6rW   r   r   )W
__future__r   collections.abcr   ImportErrorcollectionstimer   ctypesr   	functoolsr   r   ra  r   r   libbccr   r	   r
   r   r   utilsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   BPF_MAP_TYPE_SOCKMAPr   r   BPF_MAP_TYPE_SOCKHASHBPF_MAP_TYPE_CGROUP_STORAGE BPF_MAP_TYPE_REUSEPORT_SOCKARRAY"BPF_MAP_TYPE_PERCPU_CGROUP_STORAGEr   r   BPF_MAP_TYPE_SK_STORAGEBPF_MAP_TYPE_DEVMAP_HASHBPF_MAP_TYPE_STRUCT_OPSr   BPF_MAP_TYPE_INODE_STORAGEBPF_MAP_TYPE_TASK_STORAGEr   rp   r  r  rV   r]   rZ   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   r   rO   rW   rU   <module>r     sW   &+.    	  	 
 \ \ " $    !      !           #%  %' "        v \ "#5	
 m ~ m ~ : !"3 :  ~  )  !" #$ :!1$&;&(?\m\)~;@ 	 
?2$8L<:8;v-`gC gCT	 7i 76$	 6$pI 	6	 	6 (Q) Qi7Y i7X2- 2-h=J =6-) 6-p"i "2 2h6Y 66Y 66Y 6=I =<9 <7i 7r=6 =6o'  +**+s   G G&%G&