o
     b_A                     @   s\  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ejdZG dd	 d	eZG d
d deZG dd deZeej_ee	e	eee	eee	gej_dej_eegej_eeej_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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j!_eeeegej!_dej"_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gej%_eeej&_eeeeegej&_dej'_eeegej'_dej(_eeeeegej(_G dd de)Z*dS )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                   @   s,   e Zd ZdZdefdee fgZdd ZdS )mmv_instancezX Maps internal to external instance identifiers, within an
        instance domain.
    internalexternalc                 C   s.   t |  t|ts|d}|| _|| _d S Nutf-8)r   __init__
isinstancebytesencoder   r   )selfinstname r   )/usr/lib/python3/dist-packages/pcp/mmv.pyr   c   s
   



zmmv_instance.__init__N)	__name__
__module____qualname____doc__r   r
   r   _fields_r   r   r   r   r   r   \   s    
r   c                   @   sH   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d	Z	d
d Z
dS )	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helptext c                 C   sX   t |  |d urt|ts|d}|d ur!t|ts!|d}|| _|| _|| _d S r   )r   r   r   r   r   r&   r'   r#   )r   r#   r&   r'   r   r   r   r   u   s   



zmmv_indom.__init__c                 C   sP   t || _t| j  }t| jD ]}|| j|| _|| j|| _q|| _dS )z7 Update the instances and counts fields for this indom N)lenr$   r   ranger   r   r%   )r   r%   instance_arrayir   r   r   set_instances   s   

zmmv_indom.set_instancesN)r(   r(   )r   r   r   r    r   r   r   r   r!   r   r-   r   r   r   r   r"   j   s    


r"   c                	   @   sR   e Zd ZdZdee 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dZdS )
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'   r   r(   c	           	      C   s~   t |  t|ts|d}|d urt|ts|d}|d ur+t|ts+|d}|| _|| _|| _|| _|| _	|| _
d S r   )r   r   r   r   r   r&   r'   r0   r3   r/   r   )	r   r   r/   r0   r1   r2   r3   r&   r'   r   r   r   r      s   





zmmv_metric.__init__N)r   r(   r(   )r   r   r   r    r
   r   r   r   r   r   r!   r   r   r   r   r   r.      s    
	r.   c                   @   s   e Zd ZdZd3ddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2S )4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)
    r   *   c                 C   s<   t |ts
|d}|| _|| _|| _g | _g | _d | _d S r   )	r   r   r   _name_cluster_flags_metrics_indoms_handle)r   r   flagsclusterr   r   r   r      s   


zMemoryMappedValues.__init__c              	   C   s   t | j}t|  }t|D ]	}| j| ||< qt | j}t|  }t|D ]	}| j| ||< q&t| j| j	| j
||||| _dS )z Initialise the underlying library with metrics/instances.
            On completion of this call, we're all visible to pmdammv.
        N)r)   r9   r.   r*   r:   r"   
LIBPCP_MMVmmv_stats_initr6   r7   r8   r;   )r   count_metricsmetricsr,   count_indomsindomsr   r   r   start   s   




zMemoryMappedValues.startc                 C   s$   | j durt| j| j  d| _ dS )z Shut down the underlying library with metrics/instances.
            This closes the mmap file preventing any further updates.
        N)r;   r>   mmv_stats_stopr6   r   r   r   r   stop  s   

zMemoryMappedValues.stopc                 C   s   |    |   dS )z: Cleanly stop-if-running and restart MMV export services. N)rG   rD   rF   r   r   r   restart  s   zMemoryMappedValues.restartc                 C   s   | j du rdS dS )z, Property flagging an active memory mapping Nr      )r;   rF   r   r   r   started  s   
zMemoryMappedValues.startedc                 C      || _ |  r|   dS dS )z; Make a list of instance domains visible to the MMV export N)r:   rJ   rH   )r   rC   r   r   r   
add_indoms     zMemoryMappedValues.add_indomsc                 C      | j | | | j  dS )z> Make an additional instance domain visible to the MMV export N)r:   appendrL   )r   r3   r   r   r   	add_indom      zMemoryMappedValues.add_indomc                 C   rK   )z2 Make a list of metrics visible to the MMV export N)r9   rJ   rH   )r   rA   r   r   r   add_metrics%  rM   zMemoryMappedValues.add_metricsc                 C   rN   )z5 Make an additional metric visible to the MMV export N)r9   rO   rR   )r   metricr   r   r   
add_metric+  rQ   zMemoryMappedValues.add_metricc                 C   sH   |durt |ts|d}|durt |ts|d}t| 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.
        Nr   )r   r   r   r>   mmv_lookup_value_descr;   r   r   r   r   r   r   lookup_mapping1  s
   


z!MemoryMappedValues.lookup_mappingc                 C      t | j|| dS )z. Increment the mapped metric by a given value Nr>   mmv_inc_valuer;   r   mappingvaluer   r   r   addA     zMemoryMappedValues.addc                 C   s   t | j|d dS )z$ Increment the mapped metric by one rI   NrY   r   r\   r   r   r   incE  r_   zMemoryMappedValues.incc                 C   rX   )z( Set the mapped metric to a given value N)r>   mmv_set_valuer;   r[   r   r   r   setI  r_   zMemoryMappedValues.setc                 C   s6   |durt |ts|d}t| j||t| dS )z/ Set the string mapped metric to a given value Nr   )r   r   r   r>   mmv_set_stringr;   r)   r[   r   r   r   
set_stringM  s   
zMemoryMappedValues.set_stringc                 C   s   t | j|ddS )z Start a timed interval for the mapped metric
            The opaque handle (mapping) returned is passed to interval_end().
        r   )r>   mmv_stats_interval_startr;   r`   r   r   r   interval_startS  s   z!MemoryMappedValues.interval_startc                 C   s   t | j|S )zA End a timed interval, the metrics time is increased by interval )r>   mmv_stats_interval_endr;   r`   r   r   r   interval_endY  s   zMemoryMappedValues.interval_endc                 C   N   |durt |ts|d}|durt |ts|d}t| j||| dS )z9 Lookup the named metric[instance] and add a value to it Nr   )r   r   r   r>   mmv_stats_addr;   r   r   r   r]   r   r   r   
lookup_add^  
   

zMemoryMappedValues.lookup_addc                 C   sL   |durt |ts|d}|durt |ts|d}t| j|| dS )z5 Lookup the named metric[instance] and add one to it Nr   )r   r   r   r>   mmv_stats_incr;   rV   r   r   r   
lookup_incf  s
   

zMemoryMappedValues.lookup_incc                 C   rj   )z5 Lookup the named metric[instance] and set its value Nr   )r   r   r   r>   mmv_stats_setr;   rl   r   r   r   
lookup_setn  rn   zMemoryMappedValues.lookup_setc                 C   sJ   |durt |ts|d}|durt |ts|d}t| 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   r>   rf   r;   rV   r   r   r   lookup_interval_startv  s   

z(MemoryMappedValues.lookup_interval_startc                 C   sh   |durt |ts|d}|durt |ts|d}t |ts&|d}t| j|||t| dS )z< Lookup the named metric[instance] and set its string value Nr   )r   r   r   r>   mmv_stats_set_strlenr;   r)   )r   r   r   sr   r   r   lookup_set_string  s   



z$MemoryMappedValues.lookup_set_stringc                 C   sP   |durt |ts|d}|durt |ts|d}t| j|||| dS )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   r>   mmv_stats_add_fallbackr;   )r   r   r   fallr]   r   r   r   lookup_add_fallback  s
   

z&MemoryMappedValues.lookup_add_fallbackc                 C   rj   )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   r>   mmv_stats_inc_fallbackr;   )r   r   r   fallbackr   r   r   lookup_inc_fallback  s
   

z&MemoryMappedValues.lookup_inc_fallbackN)r   r5   )r   r   r   r    r   rD   rG   rH   rJ   rL   rP   rR   rT   rW   r^   ra   rc   re   rg   ri   rm   rp   rr   rs   rv   ry   r|   r   r   r   r   r4      s2    


r4   )+r    	pcp.pmapir   r   cmmvr   ctypesr   r   r   r   r	   r
   r   r   r   CDLLutilfind_libraryr>   r   r"   r.   r?   restypeargtypesrE   rU   rZ   mmv_inc_atomvaluerb   mmv_set_atomvaluerd   rk   ro   rq   rw   rz   rf   rh   rt   objectr4   r   r   r   r   <module>   sd   G$


