
    iogA                     d   S r SSKJrJr  SSKJrJr  SSKrSSKJrJ	r	  SSKJ
r
JrJrJrJrJrJr  \R"                  " \R$                  R'                  S5      5      r " S S	\5      r " S
 S\5      r " S S\5      r\\R0                  l        \\
\
\	" \5      \
\	" \5      \
/\R0                  l        S\R6                  l        \\/\R6                  l        \	" \5      \R8                  l        \\\/\R8                  l        S\R:                  l        \\	" \5      \/\R:                  l        S\R<                  l        \\	" \5      \	" \5      /\R<                  l        S\R>                  l        \\	" \5      \/\R>                  l        S\R@                  l        \\	" \5      \	" \5      /\R@                  l        S\RB                  l        \\	" \5      \\
/\RB                  l        S\RD                  l        \\\\/\RD                  l        S\RF                  l        \\\/\RF                  l        S\RH                  l        \\\\/\RH                  l        S\RJ                  l        \\\\\/\RJ                  l        S\RL                  l        \\\\/\RL                  l        \	" \5      \RN                  l        \\	" \5      \\/\RN                  l        S\RP                  l        \\	" \5      /\RP                  l        S\RR                  l        \\\\\/\RR                  l         " S S\*5      r+g)a
  Wrapper module for libpcp_mmv - PCP Memory Mapped Values library
#
# Copyright (C) 2013-2016,2019 Red Hat.
#
# This file is part of the "pcp" module, the python interfaces for the
# Performance Co-Pilot toolkit.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# Example use of this module for instrumenting a python application:

        from pcp import mmv, pmapi
        import cpmapi as pcpapi
        import cmmv as mmvapi

        instances = [
                mmv.mmv_instance(0, "Anvils"),
                mmv.mmv_instance(1, "Rockets"),
                mmv.mmv_instance(2, "Giant_Rubber_Bands")
        ]
        ACME_PRODUCTS_INDOM = 61
        indoms = [
                mmv.mmv_indom(
                    serial = ACME_PRODUCTS_INDOM,
                    shorttext = "Acme products",
                    helptext = "Most popular products produced by the Acme Corporation")
        ]
        indoms[0].set_instances(instances)

        metrics = [
                mmv.mmv_metric(
                    name = "products.count",
                    item = 7,
                    typeof = mmvapi.MMV_TYPE_U64,
                    semantics = mmvapi.MMV_SEM_COUNTER,
                    dimension = pmapi.pmUnits(0,0,1,0,0,pcpapi.PM_COUNT_ONE),
                    indom = ACME_PRODUCTS_INDOM,
                    shorttext = "Acme factory product throughput",
                    helptext =
        "Monotonic increasing counter of products produced in the Acme Corporation
" +
        "factory since starting the Acme production application.  Quality guaranteed."),

                mmv.mmv_metric(
                    name = "products.time",
                    item = 8,
                    typeof = mmvapi.MMV_TYPE_U64,
                    semantics = mmvapi.MMV_SEM_COUNTER,
                    dimension = pmapi.pmUnits(0,1,0,0,pcpapi.PM_TIME_USEC,0),
                    indom = ACME_PRODUCTS_INDOM,
                    shorttext = "Machine time spent producing Acme products")
        ]

        values = mmv.MemoryMappedValues("acme")
        values.add_indoms(indoms)
        values.add_metrics(metrics)

        values.start()
        anvils = values.lookup_mapping("products.count", "Anvils")
        values.set(anvils, 41)
        values.inc(anvils)
        values.stop()
    )pmUnitspmAtomValue)MMV_NAMEMAXMMV_INDOM_NULLN)	StructurePOINTER)c_intc_uintc_longc_charc_char_pc_doublec_void_ppcp_mmvc                   4    \ rS rSrSrS\4S\\-  4/rS r	Sr
g)mmv_instance\   zKMaps internal to external instance identifiers, within an
instance domain.
internalexternalc                     [         R                  " U 5        [        U[        5      (       d  UR	                  S5      nX l        Xl        g Nutf-8)r   __init__
isinstancebytesencoder   r   )selfinstnames      )/usr/lib/python3/dist-packages/pcp/mmv.pyr   mmv_instance.__init__c   s5    4 $&&;;w'D    )r   r   N)__name__
__module____qualname____firstlineno____doc__r	   r   r   _fields_r   __static_attributes__ r"   r    r   r   \   s*     U#Vk124Hr"   r   c                   V    \ rS rSrSrS\4S\4S\" \5      4S\4S\4/r	SS jr
S	 rS
rg)	mmv_indomj   zRepresents an instance domain (for set valued metrics)
Instance domains have associated instances - integer/string pairs.
Defines complete indom metadata (instances, count, text and so on)
serialcount	instances	shorttexthelptextc                     [         R                  " U 5        Ub&  [        U[        5      (       d  UR	                  S5      nUb&  [        U[        5      (       d  UR	                  S5      nX l        X0l        Xl        g r   )r   r   r   r   r   r1   r2   r.   )r   r.   r1   r2   s       r    r   mmv_indom.__init__u   sa    4 
8U(C(Cw/H Iu)E)E!((1I" r"   c                     [        U5      U l        [        U R                  -  " 5       n[        U R                  5       H-  nX   R                  X#   l        X   R
                  X#   l        M/     X l        g)z6Update the instances and counts fields for this indom N)lenr/   r   ranger   r   r0   )r   r0   instance_arrayis       r    set_instancesmmv_indom.set_instances   s^    ^
&36tzz"A)2)>)>N&)2)>)>N& # (r"   )r/   r2   r0   r.   r1   N) r<   )r#   r$   r%   r&   r'   r
   r   r   r   r(   r   r:   r)   r*   r"   r    r,   r,   j   sG     6"&!gl34h'X&	(H(r"   r,   c            	       b    \ rS rSrSrS\\-  4S\4S\4S\4S\4S\	4S	\
4S
\
4/r\SS4S jrSrg)
mmv_metric   z~Represents an individual metric to be exported by pmdammv
Defines complete metric metadata (type, semantics, units and so on)
r   itemtypeof	semantics	dimensionindomr1   r2   r<   c	                 t   [         R                  " U 5        [        U[        5      (       d  UR	                  S5      nXl        Ub&  [        U[        5      (       d  UR	                  S5      nXl        Ub&  [        U[        5      (       d  UR	                  S5      nXpl        X@l        X0l	        X`l
        X l        g r   )r   r   r   r   r   r   r2   r1   rB   rA   rD   r@   )	r   r   r@   rA   rB   rC   rD   r1   r2   s	            r    r   mmv_metric.__init__   s    4 $&&;;w'D	
8U(C(Cw/H  Iu)E)E!((1I""
	r"   )r2   rD   r@   r   rB   r1   rA   N)r#   r$   r%   r&   r'   r   r   r	   r   r
   r   r(   r   r   r)   r*   r"   r    r>   r>      sf     +-.5!e$g&&!h'X&(H HVacnp r"   r>   c                       \ rS rSrSrSS jrS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rSrg)MemoryMappedValues   zDefines a set of PCP Memory Mapped Value (MMV) metrics

Creates PCP metrics from an instrumented python script
via pmdammv (Performance Metrics Domain Agent for MMV)
c                     [        U[        5      (       d  UR                  S5      nXl        X0l        X l        / U l        / U l        S U l        g r   )	r   r   r   _name_cluster_flags_metrics_indoms_handle)r   r   flagsclusters       r    r   MemoryMappedValues.__init__   sA    $&&;;w'D
r"   c           	         [        U R                  5      nUS:  a  [        U-  " 5       OSn[        U5       H  nU R                  U   X#'   M     [        U R                  5      nUS:  a  [
        U-  " 5       OSn[        U5       H  nU R                  U   XS'   M     [        R                  U R                  U R                  U R                  X!XT5      U l        g)ztInitialise the underlying library with metrics/instances.
On completion of this call, we're all visible to pmdammv.
r   N)r6   rN   r>   r7   rO   r,   
LIBPCP_MMVmmv_stats_initrK   rL   rM   rP   )r   count_metricsmetricsr9   count_indomsindomss         r    startMemoryMappedValues.start   s     DMM*4AA4E:-04}%Aq)GJ &4<<(1=1A)l*-t|$AQFI %!00T]]151817Gr"   c                     U R                   b*  [        R                  U R                  U R                   5        SU l         g)zsShut down the underlying library with metrics/instances.
This closes the mmap file preventing any further updates.
N)rP   rU   mmv_stats_stoprK   r   s    r    stopMemoryMappedValues.stop  s-     <<#%%djj$,,?r"   c                 D    U R                  5         U R                  5         g)z9Cleanly stop-if-running and restart MMV export services. N)r`   r[   r_   s    r    restartMemoryMappedValues.restart  s    		

r"   c                      U R                   c  gg)z+Property flagging an active memory mapping r      )rP   r_   s    r    startedMemoryMappedValues.started  s    <<r"   c                 \    Xl         U R                  5       (       a  U R                  5         gg)z:Make a list of instance domains visible to the MMV export N)rO   rg   rc   )r   rZ   s     r    
add_indomsMemoryMappedValues.add_indoms  s     <<>>LLN r"   c                 p    U R                   R                  U5        U R                  U R                   5        g)z=Make an additional instance domain visible to the MMV export N)rO   appendrj   )r   rD   s     r    	add_indomMemoryMappedValues.add_indom!  s$    E"%r"   c                 \    Xl         U R                  5       (       a  U R                  5         gg)z1Make a list of metrics visible to the MMV export N)rN   rg   rc   )r   rX   s     r    add_metricsMemoryMappedValues.add_metrics&  s     <<>>LLN r"   c                 p    U R                   R                  U5        U R                  U R                   5        g)z4Make an additional metric visible to the MMV export N)rN   rm   rq   )r   metrics     r    
add_metricMemoryMappedValues.add_metric,  s&    V$'r"   c                     Ub&  [        U[        5      (       d  UR                  S5      nUb&  [        U[        5      (       d  UR                  S5      n[        R	                  U R
                  X5      $ )a  Find the memory mapping for a given metric name and instance

This handle can be used to directly manipulate metric values
by other interfaces in this module.  This is the *preferred*
technique for manipulating MMV values.  It is more efficient
and the alternative (name/inst lookups) is made available as
a convenience only for situations where performance will not
be affected by repeated (linear) name/inst lookups.
r   )r   r   r   rU   mmv_lookup_value_descrP   r   r   r   s      r    lookup_mapping!MemoryMappedValues.lookup_mapping2  s[     JtU$;$;;;w'DJtU$;$;;;w'D//dIIr"   c                 D    [         R                  U R                  X5        g)z-Increment the mapped metric by a given value NrU   mmv_inc_valuerP   r   mappingvalues      r    addMemoryMappedValues.addB        w>r"   c                 F    [         R                  U R                  US5        g)z#Increment the mapped metric by one rf   Nr}   r   r   s     r    incMemoryMappedValues.incF  s      w:r"   c                 D    [         R                  U R                  X5        g)z'Set the mapped metric to a given value N)rU   mmv_set_valuerP   r   s      r    setMemoryMappedValues.setJ  r   r"   c                     Ub&  [        U[        5      (       d  UR                  S5      n[        R	                  U R
                  X[        U5      5        g)z.Set the string mapped metric to a given value Nr   )r   r   r   rU   mmv_set_stringrP   r6   r   s      r    
set_stringMemoryMappedValues.set_stringN  s<    Zu%=%=LL)E!!$,,E
Kr"   c                 F    [         R                  U R                  USS5      $ )zoStart a timed interval for the mapped metric
The opaque handle (mapping) returned is passed to interval_end().
r   )rU   mmv_stats_interval_startrP   r   s     r    interval_start!MemoryMappedValues.interval_startT  s     224<<!QOOr"   c                 B    [         R                  U R                  U5      $ )z@End a timed interval, the metrics time is increased by interval )rU   mmv_stats_interval_endrP   r   s     r    interval_endMemoryMappedValues.interval_endZ  s    00wGGr"   c                     Ub&  [        U[        5      (       d  UR                  S5      nUb&  [        U[        5      (       d  UR                  S5      n[        R	                  U R
                  XU5        g)z8Lookup the named metric[instance] and add a value to it Nr   )r   r   r   rU   mmv_stats_addrP   r   r   r   r   s       r    
lookup_addMemoryMappedValues.lookup_add_  X    JtU$;$;;;w'DJtU$;$;;;w'D  t5Ar"   c                     Ub&  [        U[        5      (       d  UR                  S5      nUb&  [        U[        5      (       d  UR                  S5      n[        R	                  U R
                  X5        g)z4Lookup the named metric[instance] and add one to it Nr   )r   r   r   rU   mmv_stats_incrP   ry   s      r    
lookup_incMemoryMappedValues.lookup_incg  sV    JtU$;$;;;w'DJtU$;$;;;w'D  t:r"   c                     Ub&  [        U[        5      (       d  UR                  S5      nUb&  [        U[        5      (       d  UR                  S5      n[        R	                  U R
                  XU5        g)z4Lookup the named metric[instance] and set its value Nr   )r   r   r   rU   mmv_stats_setrP   r   s       r    
lookup_setMemoryMappedValues.lookup_seto  r   r"   c                     Ub&  [        U[        5      (       d  UR                  S5      nUb&  [        U[        5      (       d  UR                  S5      n[        R	                  U R
                  SX5      $ )zpLookup the named metric[instance] and start an interval
The opaque handle returned is passed to interval_end().
Nr   )r   r   r   rU   r   rP   ry   s      r    lookup_interval_start(MemoryMappedValues.lookup_interval_startw  sd     JtU$;$;;;w'DJtU$;$;;;w'D224<<37E 	Er"   c           	      J   Ub&  [        U[        5      (       d  UR                  S5      nUb&  [        U[        5      (       d  UR                  S5      n[        U[        5      (       d  UR                  S5      n[        R	                  U R
                  XU[        U5      5        g)z;Lookup the named metric[instance] and set its string value Nr   )r   r   r   rU   mmv_stats_set_strlenrP   r6   )r   r   r   ss       r    lookup_set_string$MemoryMappedValues.lookup_set_string  sy    JtU$;$;;;w'DJtU$;$;;;w'D!U##!A''d!SVLr"   c                     Ub&  [        U[        5      (       d  UR                  S5      nUb&  [        U[        5      (       d  UR                  S5      n[        R	                  U R
                  XX45        g)zLookup the named metric[instance] and set its value if found
If instance is not found, fallback to using a second instance
One example use is: add value to bucketN else use a catch-all
                    bucket such as "other"
Nr   )r   r   r   rU   mmv_stats_add_fallbackrP   )r   r   r   fallr   s        r    lookup_add_fallback&MemoryMappedValues.lookup_add_fallback  sZ     JtU$;$;;;w'DJtU$;$;;;w'D))$,,DPr"   c                     Ub&  [        U[        5      (       d  UR                  S5      nUb&  [        U[        5      (       d  UR                  S5      n[        R	                  U R
                  XU5        g)zLookup the named metric[instance] and increment its value if found
If instance is not found, fallback to using a second instance
One sample use is: inc value of BucketA, else inc a catch-all
Nr   )r   r   r   rU   mmv_stats_inc_fallbackrP   )r   r   r   fallbacks       r    lookup_inc_fallback&MemoryMappedValues.lookup_inc_fallback  sZ    
 JtU$;$;;;w'DJtU$;$;;;w'D))$,,HMr"   )rL   rM   rP   rO   rN   rK   N)r   *   )r#   r$   r%   r&   r'   r   r[   r`   rc   rg   rj   rn   rq   ru   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r)   r*   r"   r    rH   rH      s    G"
&
(J ?;?LPH
B;B	EM
Q	Nr"   rH   ),r'   	pcp.pmapir   r   cmmvr   r   ctypesr   r   r	   r
   r   r   r   r   r   CDLLutilfind_libraryrU   r   r,   r>   rV   restypeargtypesr^   rx   r~   mmv_inc_atomvaluer   mmv_set_atomvaluer   r   r   r   r   r   r   r   r   objectrH   r*   r"   r    <module>r      s  EN + ,  % N N N [[11)<=
9 (	 (< @ %-
   !eUJ	 2E&;
   " %)
   !&.%9
   "+2;+?
     (-5x,J
     )#'
    %-w{/CX$N
   !'+
   $)17;3GQ\I](^
   %#'
    %-w{/CX$N
   !'+
   $)17;3GQ\I](^
   %$(
   !gk"He&5
   " $(
    %-x8$L
   !#'
    %-x$B
   !#'
    %-x8$L
   !,0
 ! ! )h(H.6
 ! ! * -1
 ! ! )h(.,
 ! ! * /6k.B
 # # +gk"Hh08
 # # , -1
 ! ! ).68L-M
 ! ! **.
   'h(F,4
   ({N {Nr"   