
    rC
f_A                     `   d Z ddlmZmZ ddlmZ ddlZddlmZmZ ddlm	Z	m
Z
mZmZmZmZmZ  ej                   ej"                  j%                  d            Z G d d	e      Z G d
 de      Z G d de      Zeej.                  _        ee	e	 ee      e	 ee      e	gej.                  _        dej4                  _        eegej4                  _         ee      ej6                  _        eeegej6                  _        dej8                  _        e ee      egej8                  _        dej:                  _        e ee       ee      gej:                  _        dej<                  _        e ee      egej<                  _        dej>                  _        e ee       ee      gej>                  _        dej@                  _        e ee      ee	gej@                  _        dejB                  _        eeeegejB                  _        dejD                  _        eeegejD                  _        dejF                  _        eeeegejF                  _        dejH                  _        eeeeegejH                  _        dejJ                  _        eeeegejJ                  _         ee      ejL                  _        e ee      eegejL                  _        dejN                  _        e ee      gejN                  _        dejP                  _        eeeeegejP                  _         G d de)      Z*y)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_NAMEMAXN)	StructurePOINTER)c_intc_uintc_longc_charc_char_pc_doublec_void_ppcp_mmvc                   ,    e Zd ZdZdefdeez  fgZd Zy)mmv_instancezX Maps internal to external instance identifiers, within an
        instance domain.
    internalexternalc                     t        j                  |        t        |t              s|j	                  d      }|| _        || _        y Nutf-8)r   __init__
isinstancebytesencoder   r   )selfinstnames      )/usr/lib/python3/dist-packages/pcp/mmv.pyr   zmmv_instance.__init__c   s6    4 $&;;w'D    N)	__name__
__module____qualname____doc__r   r   r   _fields_r    r   r   r   r   \   s*     U#Vk124Hr   r   c                   L    e Zd ZdZdefdefd ee      fdefdefgZd
dZ	d Z
y	)	mmv_indomz Represents 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                     t        j                  |        |!t        |t              s|j	                  d      }|!t        |t              s|j	                  d      }|| _        || _        || _        y r   )r   r   r   r   r   r+   r,   r(   )r   r(   r+   r,   s       r   r   zmmv_indom.__init__u   sa    4 
8U(Cw/H Iu)E!((1I"!r   c                     t        |      | _        t        | j                  z         }t        | j                        D ]0  }||   j                  ||   _        ||   j
                  ||   _        2 || _        y)z7 Update the instances and counts fields for this indom N)lenr)   r   ranger   r   r*   )r   r*   instance_arrayis       r   set_instanceszmmv_indom.set_instances   sm    ^
&36tzz" 	?A)21)>)>N1&)21)>)>N1&	? (r   N) r4   )r    r!   r"   r#   r	   r   r   r   r$   r   r3   r%   r   r   r'   r'   j   sG     6"&!gl34h'X&	(H(r   r'   c            	       R    e Zd ZdZdeez  fdefdefdefdefdefde	fd	e	fgZ
dd
Zy)
mmv_metricz Represents an individual metric to be exported by pmdammv
        Defines complete metric metadata (type, semantics, units and so on)
    r   itemtypeof	semantics	dimensionindomr+   r,   c	                 P   t        j                  |        t        |t              s|j	                  d      }|!t        |t              s|j	                  d      }|!t        |t              s|j	                  d      }|| _        || _        || _        || _        || _	        || _
        y r   )r   r   r   r   r   r+   r,   r8   r;   r7   r   )	r   r   r7   r8   r9   r:   r;   r+   r,   s	            r   r   zmmv_metric.__init__   s    4 $&;;w'D
8U(Cw/H Iu)E!((1I"!
		r   N)r   r4   r4   )r    r!   r"   r#   r   r   r   r   r	   r   r$   r   r%   r   r   r6   r6      sY     +-.5!e$g&&!h'X&(Hr   r6   c                       e Zd 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 Zd Zd Zd Zd Zd Zd Zd Zd Zy)MemoryMappedValuesz Defines 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                     t        |t              s|j                  d      }|| _        || _        || _        g | _        g | _        d | _        y r   )	r   r   r   _name_cluster_flags_metrics_indoms_handle)r   r   flagsclusters       r   r   zMemoryMappedValues.__init__   sD    $&;;w'D
r   c           	         t        | j                        }t        |z         }t        |      D ]  }| j                  |   ||<    t        | j                        }t        |z         }t        |      D ]  }| j                  |   ||<    t        j                  | j                  | j                  | j                  ||||      | _        y)z Initialise the underlying library with metrics/instances.
            On completion of this call, we're all visible to pmdammv.
        N)r/   rC   r6   r0   rD   r'   
LIBPCP_MMVmmv_stats_initr@   rA   rB   rE   )r   count_metricsmetricsr2   count_indomsindomss         r   startzMemoryMappedValues.start   s     DMM*-0}% 	*Aq)GAJ	*4<<(l*-|$ 	(AQF1I	(!00T]]1518-17Gr   c                 ~    | j                   *t        j                  | j                  | j                          d| _         y)z Shut down the underlying library with metrics/instances.
            This closes the mmap file preventing any further updates.
        N)rE   rI   mmv_stats_stopr@   r   s    r   stopzMemoryMappedValues.stop  s-     <<#%%djj$,,?r   c                 D    | j                          | j                          y)z: Cleanly stop-if-running and restart MMV export services. N)rS   rO   rR   s    r   restartzMemoryMappedValues.restart  s    		

r   c                     | j                   yy)z, Property flagging an active memory mapping r      )rE   rR   s    r   startedzMemoryMappedValues.started  s    <<r   c                 T    || _         | j                         r| j                          yy)z; Make a list of instance domains visible to the MMV export N)rD   rX   rU   )r   rN   s     r   
add_indomszMemoryMappedValues.add_indoms  s     <<>LLN r   c                 p    | j                   j                  |       | j                  | j                          y)z> Make an additional instance domain visible to the MMV export N)rD   appendrZ   )r   r;   s     r   	add_indomzMemoryMappedValues.add_indom   s$    E"%r   c                 T    || _         | j                         r| j                          yy)z2 Make a list of metrics visible to the MMV export N)rC   rX   rU   )r   rL   s     r   add_metricszMemoryMappedValues.add_metrics%  s     <<>LLN r   c                 p    | j                   j                  |       | j                  | j                          y)z5 Make an additional metric visible to the MMV export N)rC   r\   r_   )r   metrics     r   
add_metriczMemoryMappedValues.add_metric+  s&    V$'r   c                     |!t        |t              s|j                  d      }|!t        |t              s|j                  d      }t        j	                  | j
                  ||      S )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   rI   mmv_lookup_value_descrE   r   r   r   s      r   lookup_mappingz!MemoryMappedValues.lookup_mapping1  sW     JtU$;;;w'DJtU$;;;w'D//dDIIr   c                 F    t         j                  | j                  ||       y)z. Increment the mapped metric by a given value NrI   mmv_inc_valuerE   r   mappingvalues      r   addzMemoryMappedValues.addA        w>r   c                 F    t         j                  | j                  |d       y)z$ Increment the mapped metric by one rW   Nrh   r   rk   s     r   inczMemoryMappedValues.incE  s      w:r   c                 F    t         j                  | j                  ||       y)z( Set the mapped metric to a given value N)rI   mmv_set_valuerE   rj   s      r   setzMemoryMappedValues.setI  rn   r   c                     |!t        |t              s|j                  d      }t        j	                  | j
                  ||t        |             y)z/ Set the string mapped metric to a given value Nr   )r   r   r   rI   mmv_set_stringrE   r/   rj   s      r   
set_stringzMemoryMappedValues.set_stringM  s;    Zu%=LL)E!!$,,E
Kr   c                 F    t         j                  | j                  |dd      S )z Start a timed interval for the mapped metric
            The opaque handle (mapping) returned is passed to interval_end().
        r   )rI   mmv_stats_interval_startrE   rp   s     r   interval_startz!MemoryMappedValues.interval_startS  s     224<<!QOOr   c                 B    t         j                  | j                  |      S )zA End a timed interval, the metrics time is increased by interval )rI   mmv_stats_interval_endrE   rp   s     r   interval_endzMemoryMappedValues.interval_endY  s    00wGGr   c                     |!t        |t              s|j                  d      }|!t        |t              s|j                  d      }t        j	                  | j
                  |||       y)z9 Lookup the named metric[instance] and add a value to it Nr   )r   r   r   rI   mmv_stats_addrE   r   r   r   rl   s       r   
lookup_addzMemoryMappedValues.lookup_add^  T    JtU$;;;w'DJtU$;;;w'D  tT5Ar   c                     |!t        |t              s|j                  d      }|!t        |t              s|j                  d      }t        j	                  | j
                  ||       y)z5 Lookup the named metric[instance] and add one to it Nr   )r   r   r   rI   mmv_stats_incrE   re   s      r   
lookup_inczMemoryMappedValues.lookup_incf  sR    JtU$;;;w'DJtU$;;;w'D  tT:r   c                     |!t        |t              s|j                  d      }|!t        |t              s|j                  d      }t        j	                  | j
                  |||       y)z5 Lookup the named metric[instance] and set its value Nr   )r   r   r   rI   mmv_stats_setrE   r   s       r   
lookup_setzMemoryMappedValues.lookup_setn  r   r   c                     |!t        |t              s|j                  d      }|!t        |t              s|j                  d      }t        j	                  | j
                  d||      S )z Lookup the named metric[instance] and start an interval
            The opaque handle returned is passed to interval_end().
        Nr   )r   r   r   rI   ry   rE   re   s      r   lookup_interval_startz(MemoryMappedValues.lookup_interval_startv  s`     JtU$;;;w'DJtU$;;;w'D224<<37tE 	Er   c           	      *   |!t        |t              s|j                  d      }|!t        |t              s|j                  d      }t        |t              s|j                  d      }t        j	                  | j
                  |||t        |             y)z< Lookup the named metric[instance] and set its string value Nr   )r   r   r   rI   mmv_stats_set_strlenrE   r/   )r   r   r   ss       r   lookup_set_stringz$MemoryMappedValues.lookup_set_string  sr    JtU$;;;w'DJtU$;;;w'D!U#!A''dD!SVLr   c                     |!t        |t              s|j                  d      }|!t        |t              s|j                  d      }t        j	                  | j
                  ||||       y)a   Lookup 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   rI   mmv_stats_add_fallbackrE   )r   r   r   fallrl   s        r   lookup_add_fallbackz&MemoryMappedValues.lookup_add_fallback  sX     JtU$;;;w'DJtU$;;;w'D))$,,dD%Pr   c                     |!t        |t              s|j                  d      }|!t        |t              s|j                  d      }t        j	                  | j
                  |||       y)z Lookup 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   rI   mmv_stats_inc_fallbackrE   )r   r   r   fallbacks       r   lookup_inc_fallbackz&MemoryMappedValues.lookup_inc_fallback  sV    
 JtU$;;;w'DJtU$;;;w'D))$,,dHMr   N)r   *   )r    r!   r"   r#   r   rO   rS   rU   rX   rZ   r]   r_   rb   rf   rm   rq   rt   rw   rz   r}   r   r   r   r   r   r   r   r%   r   r   r>   r>      s    G"
&
(J ?;?LPH
B;B	EM
Q	Nr   r>   )+r#   	pcp.pmapir   r   cmmvr   ctypesr   r   r   r	   r
   r   r   r   r   CDLLutilfind_libraryrI   r   r'   r6   rJ   restypeargtypesrQ   rd   ri   mmv_inc_atomvaluers   mmv_set_atomvaluerv   r   r   r   r   r   ry   r|   r   objectr>   r%   r   r   <module>r      s  EN +   % N N N V[[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   