
    iog=                        S r SSKJrJrJrJrJr  SSKJrJ	r	  SSK
JrJrJrJr  SSK
JrJrJrJr  SSKJr  SSKJr  SSKJr  \" \" S	5      5      rS
\R2                  l        S
\R2                  l        \\R8                  l        \\\/\R8                  l        \\R:                  l        \\/\R:                  l        \\R<                  l        \\/\R<                  l        \\R>                  l        \\\\\\/\R>                  l        \\R@                  l        \\\/\R@                  l        \\RB                  l        \\/\RB                  l        \\RD                  l        \/\RD                  l        \\RF                  l        S
\RF                  l        \\RH                  l        \/\RH                  l        \\RJ                  l        \/\RJ                  l        \\RL                  l        \/\RL                  l        \\RN                  l        \\\\\\/\RN                  l        \\RP                  l        \\\/\RP                  l        \\RR                  l        \\\/\RR                  l        \\RT                  l        \\/\RT                  l        \\RV                  l        \\/\RV                  l        \\RX                  l        \\/\RX                  l        \\RZ                  l        \\/\RZ                  l        \\R\                  l        \" \5      /\R\                  l        \\R^                  l        \" \5      /\R^                  l        \\R`                  l        S
\R`                  l        \\Rb                  l        \\\\/\Rb                  l        \\Rd                  l        \\\\\/\Rd                  l         " S S\35      r4 " S S\55      r6g
)aB  Wrapper module for libpcp_import - Performace Co-Pilot Log Import API
#
# Copyright (C) 2012-2022 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 creating a PCP archive:

        import time
        import pmapi
        from pcp import pmi

        # Create a new archive
        log = pmi.pmiLogImport("loadtest")
        log.pmiSetHostname("www.abc.com")
        log.pmiSetTimezone("EST-10")

        # Add a metric with an instance domain
        domain = 60  # Linux kernel
        pmid = log.pmiID(domain, 2, 0)
        indom = log.pmiInDom(domain, 2)
        units = log.pmiUnits(0, 0, 0, 0, 0, 0)
        log.pmiAddMetric("kernel.all.load", pmid, pmapi.PM_TYPE_FLOAT,
                         indom, pmapi.PM_SEM_INSTANT, units)
        log.pmiAddInstance(indom, "1 minute", 1)
        log.pmiAddInstance(indom, "5 minute", 5)
        log.pmiAddInstance(indom, "15 minute", 15)

        # Create a record with a timestamp
        log.pmiPutValue("kernel.all.load", "1 minute", "%f" % 0.01)
        log.pmiPutValue("kernel.all.load", "5 minute", "%f" % 0.05)
        log.pmiPutValue("kernel.all.load", "15 minute", "%f" % 0.15)
        log.pmiWrite(time.time())  # sec since epoch, or datetime, or
        #log.pmiWrite(seconds, useconds)

        del log
    )pmIDpmInDompmUnitspmHighResResultpmResult)pmiErrSymDictPMI_MAXERRMSGLEN)c_intc_uint
c_longlongc_char_p)castcreate_string_bufferPOINTERCDLL)find_library)datetime)modf
pcp_importNc                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )pmiErr   z,
Encapsulation for PMI interface error code
c                    > [         [        U ]
  " U6   [        U5      U l        U(       a#  [        US   [        5      (       a  US   U l        g SU l        g )Nr   )superr   __init__listargs
isinstanceintcode)selfr   	__class__s     )/usr/lib/python3/dist-packages/pcp/pmi.pyr   pmiErr.__init__   sB    fd$d+J	JtAw,,QDIDI    c                      [         U R                     n[        [        5      n[        R                  U R                  U[        5      nU< SU< 3$ ! [         a    S=p Nf = f)N  )r   r    r   r	   LIBPCP_IMPORTpmiErrStr_rKeyError)r!   error_symbolerror_strings      r#   __str__pmiErr.__str__   sd    	-(3L/0@AL(44TYY5EGL '55  	-*,,L<	-s   AA A"!A"c                     U R                   $ N)r    r!   s    r#   errnopmiErr.errno   s    yyr%   )r   r    )
__name__
__module____qualname____firstlineno____doc__r   r.   r3   __static_attributes____classcell__)r"   s   @r#   r   r      s    6 r%   r   c                      \ rS rSrSrS rS r\" \SSS5      r\" \SSS5      r	SS jr
S rS rS	 rS
 r\S 5       r\S 5       r\S 5       r\S 5       rS rS rS rS rS rS rSS jrS rS rS rS rS r\S 5       r S r!Sr"g) pmiLogImport   zfDefines a PCP Log Import archive context
This is used to create a PCP archive from an external source
c                     U R                   $ )zProperty for archive path )_pathr2   s    r#   	read_pathpmiLogImport.read_path   s    zzr%   c                     U R                   $ )z Property for log import context )_ctxr2   s    r#   read_ctxpmiLogImport.read_ctx   s    yyr%   Nc                 $   [        U[        5      (       d  UR                  S5      nXl        [        R
                  " S5      U l        [        R                  [        U5      U5      U l
        U R                  S:  a  [        U R                  5      eg )Nutf-8r   )r   bytesencoder@   r   utcfromtimestamp_epochr)   pmiStartr   rD   r   )r!   pathinherits      r#   r   pmiLogImport.__init__   sk    $&&;;w'D
//2!**8D>7C	99q=## r%   c                     [         (       a3  [         R                  U R                  5        [         R                  5         SU l        g )N)r)   pmiUseContextrD   pmiEndr2   s    r#   __del__pmiLogImport.__del__   s,    =''		2  "	r%   c                    [         R                  U R                  5      nUS:  a  [        U5      e[	        U[
        5      (       d  UR                  S5      n[         R                  [        U5      5      nUS:  a  [        U5      eU$ )z8PMI - set the source host name for a Log Import archive r   rH   )	r)   rS   rD   r   r   rI   rJ   pmiSetHostnamer   )r!   hostnamestatuss      r#   rX   pmiLogImport.pmiSetHostname   sm    ,,TYY7A:. (E**w/H--hx.@AA:. r%   c                    [         R                  U R                  5      nUS:  a  [        U5      e[	        U[
        5      (       d  UR                  S5      n[         R                  [        U5      5      nUS:  a  [        U5      eU$ )z?PMI - set the source timezone for a Log Import archive
        r   rH   )	r)   rS   rD   r   r   rI   rJ   pmiSetTimezoner   )r!   timezonerZ   s      r#   r]   pmiLogImport.pmiSetTimezone   so     ,,TYY7A:. (E**w/H--hx.@AA:. r%   c                     [         R                  U R                  5      nUS:  a  [        U5      e[	        U[
        5      (       d  Sn[         R                  U5      nUS:  a  [        U5      eU$ )z6PMI - set the output archive version (2 or 3)
        r      )r)   rS   rD   r   r   r   pmiSetVersion)r!   versionrZ   s      r#   rb   pmiLogImport.pmiSetVersion   sa     ,,TYY7A:. '3''G,,W5A:. r%   c                 .    [         R                  XU5      $ z7PMI - construct a pmID data structure (helper routine) )r)   pmiID)domainclusteritems      r#   rg   pmiLogImport.pmiID  s     ""6D99r%   c                 ,    [         R                  X5      $ rf   )r)   
pmiCluster)rh   ri   s     r#   rm   pmiLogImport.pmiCluster  s     ''88r%   c                 ,    [         R                  X5      $ )z:PMI - construct a pmInDom data structure (helper routine) )r)   pmiInDom)rh   serials     r#   rp   pmiLogImport.pmiInDom  s     %%f55r%   c                 2    [         R                  XUX4U5      $ )z;PMI - construct a pmiUnits data structure (helper routine) )r)   pmiUnits)	dim_spacedim_time	dim_countscale_space
scale_timescale_counts         r#   rt   pmiLogImport.pmiUnits  s"     %%i9&1{L 	Lr%   c                    [         R                  U R                  5      nUS:  a  [        U5      e[	        U[
        5      (       d  UR                  S5      n[         R                  [        U5      X#XEU5      nUS:  a  [        U5      eU$ )z:PMI - add a new metric definition to a Log Import context r   rH   )	r)   rS   rD   r   r   rI   rJ   pmiAddMetricr   )r!   namepmidtypedindomsemunitsrZ   s           r#   r}   pmiLogImport.pmiAddMetric  sv    ,,TYY7A:. $&&;;w'D++HTN,0ULA:. r%   c                    [         R                  U R                  5      nUS:  a  [        U5      e[	        U[
        5      (       d  UR                  S5      n[         R                  U[        U5      U5      nUS:  a  [        U5      eU$ )z@PMI - add element to an instance domain in a Log Import context r   rH   )	r)   rS   rD   r   r   rI   rJ   pmiAddInstancer   )r!   r   instanceinstidrZ   s        r#   r   pmiLogImport.pmiAddInstance$  sq    ,,TYY7A:. (E**w/H--eXh5GPA:. r%   c                    [         R                  U R                  5      nUS:  a  [        U5      e[	        U[
        5      (       d  UR                  S5      nSnUb1  [	        U[
        5      (       d  UR                  S5      n[        U5      n[	        U[
        5      (       d  UR                  S5      n[         R                  [        U5      U[        U5      5      nUS:  a  [        U5      eU$ )z-PMI - add a value for a metric-instance pair r   rH   N)	r)   rS   rD   r   r   rI   rJ   r   pmiPutValue)r!   r~   instvaluerZ   r   s         r#   r   pmiLogImport.pmiPutValue0  s    ,,TYY7A:. $&&;;w'DdE**{{7+~H%''LL)E**8D>+3Xe_FA:. r%   c                 ~   [         R                  U R                  5      nUS:  a  [        U5      e[	        U[
        5      (       d  UR                  S5      nSnUb1  [	        U[
        5      (       d  UR                  S5      n[        U5      n[         R                  [        U5      U5      nUS:  a  [        U5      eU$ )z1PMI - define a handle for a metric-instance pair r   rH   N)	r)   rS   rD   r   r   rI   rJ   r   pmiGetHandle)r!   r~   r   rZ   r   s        r#   r   pmiLogImport.pmiGetHandleD  s    ,,TYY7A:. $&&;;w'DdE**{{7+~H++HTNHEA:. r%   c                    [         R                  U R                  5      nUS:  a  [        U5      e[	        U[
        5      (       d  UR                  S5      n[         R                  U[        U5      5      nUS:  a  [        U5      eU$ )z:PMI - add a value for a metric-instance pair via a handle r   rH   )	r)   rS   rD   r   r   rI   rJ   pmiPutValueHandler   )r!   handler   rZ   s       r#   r   pmiLogImport.pmiPutValueHandleU  sn    ,,TYY7A:. %''LL)E00%IA:. r%   c                     [         R                  U R                  5      nUS:  a  [        U5      e[         R	                  X5      nUS:  a  [        U5      eU$ ))PMI - flush data to a Log Import archive r   )r)   rS   rD   r   pmiHighResWrite)r!   secnsecrZ   s       r#   r   pmiLogImport.pmiHighResWritea  sM    ,,TYY7A:. ..s9A:. r%   c                    [         R                  U R                  5      nUS:  a  [        U5      eU(       a  U(       d|  [	        U[
        5      (       a%  [        XR                  -
  R                  5       5      n[	        U[        5      (       a+  [        U5      n[        US   5      n[        US   S-  5      nOSn[         R                  X5      nUS:  a  [        U5      eU$ )r   r      i@B )r)   rS   rD   r   r   r   floatrL   total_secondsr   r   	pmiWrite2)r!   r   usecrZ   tss        r#   pmiWritepmiLogImport.pmiWritek  s    ,,TYY7A:. t#x((S;;.==?@#u%%#Y"Q%j2a57?+((3A:. r%   c                     [         R                  U R                  5      nUS:  a  [        U5      e[         R	                  5       nUS:  a  [        U5      eU$ )z4PMI - write a <mark> record to a Log Import archive r   )r)   rS   rD   r   
pmiPutMarkr!   rZ   s     r#   r   pmiLogImport.pmiPutMark~  sK    ,,TYY7A:. ))+A:. r%   c                     [         R                  U R                  5      nUS:  a  [        U5      e[         R	                  [        U[        [        5      5      5      nUS:  a  [        U5      eU$ z0PMI - add a data record to a Log Import archive r   )r)   rS   rD   r   pmiPutResultr   r   r   r!   resultrZ   s      r#   
put_resultpmiLogImport.put_result  sY    ,,TYY7A:. ++D9J,KLA:. r%   c                     [         R                  U R                  5      nUS:  a  [        U5      e[         R	                  [        U[        [        5      5      5      nUS:  a  [        U5      eU$ r   )r)   rS   rD   r   pmiPutHighResResultr   r   r   r   s      r#   put_highres_resultpmiLogImport.put_highres_result  sY    ,,TYY7A:. 224@X3YZA:. r%   c                    [         R                  U R                  5      nUS:  a  [        U5      e[	        U[
        5      (       d  UR                  S5      n[         R                  XU[        U5      5      nUS:  a  [        U5      eU$ )z0PMI - add a text record to a Log Import archive r   rH   )	r)   rS   rD   r   r   rI   rJ   
pmiPutTextr   )r!   typclsidentcontentrZ   s         r#   r   pmiLogImport.pmiPutText  sq    ,,TYY7A:. '5))nnW-G))#E8G;LMA:. r%   c           	      t   [         R                  U R                  5      nUS:  a  [        U5      e[	        U[
        5      (       d  UR                  S5      n[	        U[
        5      (       d  UR                  S5      n[         R                  XU[        U5      [        U5      5      nUS:  a  [        U5      eU$ )z1PMI - add a label record to a Log Import archive r   rH   )	r)   rS   rD   r   r   rI   rJ   pmiPutLabelr   )r!   r   r   r   r~   r   rZ   s          r#   r   pmiLogImport.pmiPutLabel  s    ,,TYY7A:. $&&;;w'D'5))nnW-G**3tXd^XV]M^_A:. r%   c                  ,    [         R                  5         g)z8PMI - dump the current Log Import contexts (diagnostic) N)r)   pmiDump r%   r#   r   pmiLogImport.pmiDump  s     	r%   c                     [         R                  U R                  5      nUS:  a  [        U5      e[         R	                  5       nSU l        US:  a  [        U5      eU$ )z<PMI - close current context and finish a Log Import archive r   rR   )r)   rS   rD   r   rT   r   s     r#   rT   pmiLogImport.pmiEnd  sR    ,,TYY7A:. %%'	A:. r%   )rD   rL   r@   )r   r1   )#r5   r6   r7   r8   r9   rA   rE   propertyrN   ctxr   rU   rX   r]   rb   staticmethodrg   rm   rp   rt   r}   r   r   r   r   r   r   r   r   r   r   r   r   rT   r:   r   r%   r#   r=   r=      s     ItT40D
8T4
.C
$
 : : 9 9 6 6 L L
("
&
    	r%   r=   )7r9   	pcp.pmapir   r   r   r   r   cpmir   r	   ctypesr
   r   r   r   r   r   r   r   ctypes.utilr   r   mathr   r)   r   restypeargtypesrg   rm   rp   rt   r*   rM   rS   rT   rX   r]   rb   r}   r   r   r   r   r   r   r   r   r   r   r   	Exceptionr   objectr=   r   r%   r#   <module>r      s  0f H G 0 6 6 < < $   \,/0
 !%   !%   "    %ue4   #'    %*EN   !!(   #(%.   !(   ueue#    %-   !&+Xu%=   "!&   #+U"3   &+   #(-w   $$    $   ',   $)1
   %',   $)1
   %&+   #(-w   $%*   "$ww'8   # (-   $)0(E(B   %$)   !&.(%C   "%*   "'/&:   #*/   ',18+<   ("'   $.#6    (-   %*4e)<   &,1 ! ! ).5o.F-G ! ! *%*   "'.x'8&9   ##(    $(   !#(    %+VVX$F   !$)   !&,ffh%Q   "Y @S6 Sr%   