grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026a ùº†j•Rã@s@dZddlZddlZddlZddlZddlmZddlmZm Z m Z m Z m Z ddl mZddlmZmZmZmZmZddlmZmZmZmZeedƒƒZeej_eeeegej_eej_eeeeegej_eej_eeeeƒeeƒgej_eej_eeeeƒeeƒgej_eej _eeeeeeƒeeƒeeƒgej _eej!_eegej!_eej"_eegej"_eej#_eee ƒƒegej#_eej$_ee ƒegej$_eej%_eee ƒƒegej%_eej&_gej&_d d „Z#d d „Z$d d„Z%dd„Z&Gdd„deƒZ'Gdd„deƒZ(Gdd„deƒZ)Gdd„de ƒZ*Gdd„de+ƒZ,Gdd„de,ƒZ-dS)aWrapper module for libpcp_pmda - Performace Co-Pilot Domain Agent API # # Copyright (C) 2013-2015,2017-2021 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. # # See pmdasimple.py for an example use of this module. éN)Ú pmContext)ÚpmInDomÚpmDescÚpmUnitsÚpmErrÚ pmLabelSet)Ú find_library)ÚCDLLÚc_intÚc_longÚc_char_pÚc_void_p)Ú addressofÚbyrefÚPOINTERÚ StructureZpcp_pmdacCs.ttƒƒ}t t|ƒ|¡}|dkr*t|ƒ‚|S©Nr)rrÚ LIBPCP_PMDAÚ pmdaAddLabelsrr©ÚlabelZresult_pÚstatus©rú./usr/lib64/python3.9/site-packages/pcp/pmda.pyrHs  rcCs t ||¡}|dkrt|ƒ‚|Sr)rÚpmdaAddLabelFlagsr)ÚlabelsÚflagsrrrrrOs rcCs.ttƒƒ}t t|ƒ|¡}|dkr*t|ƒ‚|Sr)rrrÚ pmdaAddNotesrrrrrrrUs  rcCst ¡}|dkrt|ƒ‚|Sr)rÚpmdaGetContextr)rrrrr\src@s0eZdZdZdefdefgZdd„Zdd„ZdS) Ú pmdaMetricz5 Structure describing a metric definition for a PMDA Úm_userÚm_desccCs<t |¡d|_||j_||j_||j_||j_||j_dS©N) rÚ__init__r r!ÚpmidÚtypeÚindomÚsemÚunits)Úselfr$Ztypeofr&r'r(rrrr#ks zpmdaMetric.__init__cCsdt|ƒ|jj|jjfS)Nz!pmdaMetric@%#lx pmid=%#lx type=%d)rr!r$r%©r)rrrÚ__str__tszpmdaMetric.__str__N) Ú__name__Ú __module__Ú __qualname__Ú__doc__r rÚ_fields_r#r+rrrrrfs ÿ rc@s0eZdZdZdefdefgZdd„Zdd„ZdS) Ú pmdaInstidz6 Structure describing an instance (index/name) a PMDA Úi_instÚi_namecCs t |¡||_| d¡|_dS)Núutf-8)rr#r2Úencoder3)r)ZinstidÚnamerrrr#|s zpmdaInstid.__init__cCsdt|ƒ|j|jfS)Nz pmdaInstid@%#lx index=%d name=%s)rr2r3r*rrrr+�szpmdaInstid.__str__N) r,r-r.r/r r r0r#r+rrrrr1ws ÿr1c@s’eZdZdZdefdefdeeƒfgZdd„Z dd„Z d d „Z d d „Z d d„Z dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„ZdS) Ú pmdaIndomz7 Structure describing an instance domain within a PMDA Úit_indomÚ it_numinstÚit_setcCs8t |¡d|_d|_||_| ||¡| ||¡dSr)rr#r9r:r8Ú load_indomÚ set_instances©r)r&Úinstsrrrr#Šs   zpmdaIndom.__init__ccsˆ|jdkrPt |jtj¡t |jtj¡}|dkr4q„| |¡}|r||fVqn4t|jƒD](}|j |j }| |¡}|rZ||fVqZdSr) r9rÚ pmdaCacheOpr8ÚcpmdaZPMDA_CACHE_WALK_REWINDZPMDA_CACHE_WALK_NEXTÚinst_name_lookupÚranger:r2)r)Úinstr6ÚirrrÚ__iter__’s" ÿÿ   zpmdaIndom.__iter__cCsv|jdkr@tƒ}t |j|t|ƒd¡}|tjkrrt|j   ¡ƒSn2|jdkrr|j D] }|j |krPt|j   ¡ƒSqPdSr)r9r rÚpmdaCacheLookupr8rr@ÚPMDA_CACHE_ACTIVEÚstrÚvalueÚdecoder:r2r3)r)Úinstancer6ÚstsrCrrrrA¦s  ÿ    zpmdaIndom.inst_name_lookupcCst|tƒrt |tj¡dSr")Ú isinstanceÚdictrr?r@ÚPMDA_CACHE_LOADr=rrrr;³s zpmdaIndom.load_indomcCs|jdkrt |jtj¡dS)Néÿÿÿÿ)r9rr?r8r@rOr*rrrÚload·s zpmdaIndom.loadcCsXt|ƒ}t|ƒ}t|ƒD]$}||j||_||j||_q||_||_t ¡dSr") Úlenr1rBr2r3r:r9r@Úset_need_refresh)r)r>Zinstance_countZinstance_arrayrDrrrÚset_list_instances»s  zpmdaIndom.set_list_instancesc CsXt |tj¡| ¡D](}| d¡}t |tj|t||ƒ¡qt |tj ¡d|_ dS)Nr4rP) rr?r@ÚPMDA_CACHE_INACTIVEÚkeysr5ÚpmdaCacheStoreZPMDA_CACHE_ADDrZPMDA_CACHE_SAVEr9)r)r&r>ÚkeyZkey8rrrÚset_dict_instancesÅs   zpmdaIndom.set_dict_instancescCsF|durd|_n2t|tƒr.d|_| ||¡nt|ƒ|_| |¡dS)NrrP)r9rMrNrYrRrTr=rrrr<Ís  zpmdaIndom.set_instancescCsdt|ƒ|j|jfS)Nz pmdaIndom@%#lx indom=%#lx num=%d)rr8r9r*rrrr+×szpmdaIndom.__str__cCs:|jdkr,t |jtj¡}|dkr6t|ƒ‚n ttjƒ‚dSr) r9rr?r8r@rOrÚcpmapiÚ PM_ERR_NYI)r)rLrrrÚ cache_loadÚs   zpmdaIndom.cache_loadcCs*|jdkrt |jtj¡n ttjƒ‚dSr) r9rr?r8r@rGrrZr[r*rrrÚcache_mark_activeâs zpmdaIndom.cache_mark_activecCs*|jdkrt |jtj¡n ttjƒ‚dSr) r9rr?r8r@rUrrZr[r*rrrÚcache_mark_inactiveès zpmdaIndom.cache_mark_inactivecCs8|jdkr*t |j|¡}|dkr4t|ƒ‚n ttjƒ‚dSr)r9rÚpmdaCacheResizer8rrZr[)r)ZmaximumrLrrrÚ cache_resizeîs   zpmdaIndom.cache_resizeN)r,r-r.r/rr rr1r0r#rErAr;rQrTrYr<r+r\r]r^r`rrrrr7„s$ þ   r7c@seZdZdZdd„ZdS)Ú pmdaUnitszF Wrapper class for PMDAs defining their metrics (avoids pmapi import) c Cst |||||||¡dSr")rr#)r)ZdimSZdimTZdimCZscaleSZscaleTZscaleCrrrr#øszpmdaUnits.__init__N)r,r-r.r/r#rrrrraösrac@s|eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z ddd„Z dd„Z ddd„Z dd„Z dd„Zdd„Zdd„ZdS) ÚMetricDispatchaÖ Helper for PMDA class which manages metric dispatch table, metric namespace, descriptors and help text. Overall strategy is to interface to the C code in python/pmda.c here, using a void* handle to the PMDA dispatch structure (allocated and managed in C code). In addition, several dictionaries for metric related strings are managed here (names, help text) - cached for quick lookups in callback routines. cCsPg|_i|_i|_i|_g|_i|_i|_i|_i|_i|_ t   ||||¡dSr") Ú _indomtableÚ_indomsÚ_indom_onelineÚ_indom_helptextÚ _metrictableÚ_metricsÚ _metric_namesÚ_metric_names_mapÚ_metric_onelineÚ_metric_helptextr@Z init_dispatch)r)Údomainr6ÚlogfileÚhelpfilerrrr#szMetricDispatch.__init__cCs.|jdd…=|j ¡|j ¡|j ¡dSr")rcrdÚclearrerfr*rrrÚ clear_indomss   zMetricDispatch.clear_indomscCsB|jdd…=|j ¡|j ¡|j ¡|j ¡|j ¡dSr")rgrhrprirjrkrlr*rrrÚ clear_metrics2s      zMetricDispatch.clear_metricscCs| ¡t ¡dSr")rrr@rSr*rrrÚ reset_metrics<szMetricDispatch.reset_metricscCst |j¡dSr")r@Ú pmns_refreshrir*rrrrtCszMetricDispatch.pmns_refreshcCs t ¡dSr")r@Ú connect_pmcdr*rrrruFszMetricDispatch.connect_pmcdÚcCsz|jj}||jvrtd|ƒ‚||jvr0tdƒ‚|j |¡||j|<||j|<||j|<||j|<||j |<t   ¡dS)Nz.attempt to add_metric with an existing name=%sz+attempt to add_metric with an existing PMID) r!r$rjÚKeyErrorrhrgÚappendrirkrlr@rS)r)r6ÚmetricÚonelineÚtextr$rrrÚ add_metricIs         zMetricDispatch.add_metriccCs„|jj}||jvrtd|ƒ‚||jvr0tdƒ‚|j |¡|j |¡|j |¡|j |¡|j  |¡|j  |¡t   ¡dS)Nz-attempt to remove non-existant metric name=%sz/attempt to remove_metric with non-existant PMID) r!r$rjrwrhrgÚremoveÚpoprirkrlr@rS)r)r6ryr$rrrÚ remove_metricXs         zMetricDispatch.remove_metriccCsR|j}|jD]}|j|kr tdƒ‚q |j |¡||j|<||j|<||j|<dS)Nz(attempt to add_indom with an existing ID)r8rcrwrxrdrerf)r)r&rzr{ZindomidÚentryrrrÚ add_indomgs      zMetricDispatch.add_indomcCsht|tƒr|j}|}n|}t||ƒ}t|tƒrZt|jƒD] \}}|j|kr8||j|<qZq8||j|<dSr")rMr7r8ÚlistÚ enumeratercrd)r)r&r>r8Z replacementrDr€rrrÚ replace_indomqs     zMetricDispatch.replace_indomcCs@|j|}|jdkr„ƒZ%ed?d@„ƒZ&edAdB„ƒZ'edCdD„ƒZ(edEdF„ƒZ)edGdH„ƒZ*dS)JÚPMDAzj Defines a PCP performance metrics domain agent Used to add new metrics into the PCP toolkit. cCs|jS)z Property for name of this PMDA )Ú_namer*rrrÚ read_name¬szPMDA.read_namecCs|jS)z0 Property for unique domain number of this PMDA )Ú_domainr*rrrÚ read_domain°szPMDA.read_domainNcCsL||_||_|s|d}d|}|s6dt d¡|f}t |||||¡dS)Nz.logZpmdaz %s/%s/helpZ PCP_PMDAS_DIR)r‹r�ÚPCPZ pmGetConfigrbr#)r)r6rmrnroZpmdanamerrrr#½sz PMDA.__init__cCsdS)zÀ Probe the domain to see if the PMDA could be activated Used by pmcheck(1) - see man page for meaning of (int) return codes - as part of PMDA specific scripts. écrr*rrrÚ domain_probeÌszPMDA.domain_probecCstd|j ¡|jfƒdS)zH Write out the domain.h file (used during installation) z #define %s %dN)Úprintr‹Úupperr�r*rrrÚ domain_writeÔszPMDA.domain_writecsb|j‰‡fdd„ˆDƒ}|dk}d}|r4d}tdƒ|D]}td|||jfƒq8|r^tdƒd S) zI Write out the namespace file (used during installation) csh|]}ˆ| d¡d’qS)Ú.r)Úsplit)Ú.0rX©ZpmnsrrÚ ßóz"PMDA.pmns_write..Úrootrvú zroot {z %s%s %d:*:*ú}N)rir’r�)r)r›ÚprefixesÚindentZleadÚprefixrr˜rÚ pmns_writeÚszPMDA.pmns_writecCs t ¡dS)zF Tell PMCD the PMDA is not ready to process requests. N)r@Ú pmda_notreadyr*rrrr¢êszPMDA.pmda_notreadycCs t ¡dS)zB Tell PMCD the PMDA is ready to process requests. N)r@Ú pmda_readyr*rrrr£ðszPMDA.pmda_readycCs²dtjvr6| ¡}t|tƒr*t t|ƒ¡t d¡nxdtjvrJ| ¡nddtjvrf| tjd¡nH|  ¡t   |j ¡t   |j¡t  |j¡t  |j¡t  |j|j¡dS)aÆ All the real work happens herein; we can be called in one of three situations, determined by environment variables. First one is for pmcheck(1), next two are part of the agent Install process (where the domain.h and namespace files need to be created). The fourth case is the real mccoy, where an agent is actually being started by pmcd/dbpmda and makes use of libpcp_pmda to talk PCP protocol. ÚPCP_PYTHON_PROBEéZPCP_PYTHON_DOMAINZPCP_PYTHON_PMNSN)ÚosÚenvironr‘rMÚintÚsysÚexitr”r¡rtr@Zpmid_oneline_refreshrkZpmid_longtext_refreshrlZindom_oneline_refreshreZindom_longtext_refreshrfZ pmda_dispatchrcrg)r)ÚresultrrrÚrunös          zPMDA.runcCs t |¡Sr")r@Ú set_fetch)Zfetchrrrr­szPMDA.set_fetchcCs t |¡Sr")r@Ú set_label)rrrrr®szPMDA.set_labelcCs t |¡Sr")r@Ú set_notes)Znotesrrrr¯szPMDA.set_notescCs t |¡Sr")r@Ú set_refresh)Zrefreshrrrr°szPMDA.set_refreshcCs t |¡Sr")r@Ú set_instance)rKrrrr± szPMDA.set_instancecCs t |¡Sr")r@Úset_fetch_callback)Zfetch_callbackrrrr²$szPMDA.set_fetch_callbackcCs t |¡Sr")r@Úset_label_callback)Zlabel_callbackrrrr³(szPMDA.set_label_callbackcCs t |¡Sr")r@Úset_notes_callback)Znotes_callbackrrrr´,szPMDA.set_notes_callbackcCs t |¡Sr")r@Úset_attribute_callback)Zattribute_callbackrrrrµ0szPMDA.set_attribute_callbackcCs t |¡Sr")r@Úset_store_callback)Zstore_callbackrrrr¶4szPMDA.set_store_callbackcCs t |¡Sr")r@Úset_endcontext_callback)Zendcontext_callbackrrrr·8szPMDA.set_endcontext_callbackcCs t |¡Sr")r@Úset_refresh_all)Z refresh_allrrrr¸<szPMDA.set_refresh_allcCs t |¡Sr")r@Úset_refresh_metrics)Zrefresh_metricsrrrr¹@szPMDA.set_refresh_metricscCs t ¡dSr")r@Úset_notify_changerrrrrºDszPMDA.set_notify_changecCsdtjvrtjSt |¡S)Nr¤)r¦r§rZZPM_ERR_NOTCONNZpmSetProcessIdentity)ÚusernamerrrÚset_userHs z PMDA.set_usercCs t ||¡Sr")r@r†)r‡rˆrrrr$Nsz PMDA.pmidcCst |||¡Sr")r@Ú pmid_build)rmr‡rˆrrrr½RszPMDA.pmid_buildcCs t |¡Sr")r@Ú pmid_cluster)r‡rrrr¾VszPMDA.pmid_clustercCs t |¡Sr")r@Z pmda_indom)Úserialrrrr&Zsz PMDA.indomcCs t ||¡Sr")r@Ú indom_build)rmr¿rrrrÀ^szPMDA.indom_buildcCst ||||||¡Sr")r@Z pmda_units)Z dim_spaceZdim_timeZ dim_countZ scale_spaceZ scale_timeZ scale_countrrrr(bsz PMDA.unitscCs t |¡Sr")r@Z pmda_uptime)ZnowrrrÚuptimefsz PMDA.uptimecCs t |¡Sr")r@Zpmda_set_comm_flags)rrrrÚset_comm_flagsjszPMDA.set_comm_flagscCs t |¡Sr")r@Zpmda_log©ÚmessagerrrÚlognszPMDA.logcCs t |¡Sr")r@Zpmda_dbgrÃrrrÚdbgrszPMDA.dbgcCs t |¡Sr")r@Zpmda_errrÃrrrÚerrvszPMDA.err)NN)+r,r-r.r/rŒrŽÚpropertyr6rmr#r‘r”r¡r¢r£r¬Ú staticmethodr­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ÇrrrrrФs€                          rŠ).r/r¦r©rZr@Z pcp.pmapirr�rrrrrZ ctypes.utilrZctypesr r r r r rrrrrrWZrestypeÚargtypesZpmdaCacheStoreKeyrFZpmdaCacheLookupNameZpmdaCacheLookupKeyr?r_rrrrrr1r7raÚobjectrbrŠrrrrÚs`    ÿÿÿ þ    r %