Skip to content

Commit

Permalink
python api: fixes and improvements to MMV wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
natoscott committed Aug 5, 2024
1 parent a9b7c9b commit a153ee2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/python/mmv.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ MOD_INIT(cmmv)
dict_add(dict, "MMV_SEM_INSTANT", MMV_SEM_INSTANT);
dict_add(dict, "MMV_SEM_DISCRETE", MMV_SEM_DISCRETE);

dict_add(dict, "MMV_INDOM_NULL", MMV_INDOM_NULL);

dict_add(dict, "MMV_FLAG_NOPREFIX", MMV_FLAG_NOPREFIX);
dict_add(dict, "MMV_FLAG_PROCESS", MMV_FLAG_PROCESS);
dict_add(dict, "MMV_FLAG_SENTINEL", MMV_FLAG_SENTINEL);
Expand Down
15 changes: 8 additions & 7 deletions src/python/pcp/mmv.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"""

from pcp.pmapi import pmUnits, pmAtomValue
from cmmv import MMV_NAMEMAX
from cmmv import MMV_NAMEMAX, MMV_INDOM_NULL

import ctypes
from ctypes import Structure, POINTER
Expand Down Expand Up @@ -121,7 +121,7 @@ def __init__(self, serial, shorttext='', helptext=''):
if shorttext is not None and not isinstance(shorttext, bytes):
shorttext = shorttext.encode('utf-8')
self.shorttext = shorttext
self.helptext = shorttext
self.helptext = helptext
self.serial = serial

def set_instances(self, instances):
Expand All @@ -146,20 +146,21 @@ class mmv_metric(Structure):
("shorttext", c_char_p),
("helptext", c_char_p)]

def __init__(self, name, item, typeof, semantics, dimension, indom=0, shorttext='', helptext=''): # pylint: disable=R0913
def __init__(self, name, item, typeof, semantics, dimension, indom=MMV_INDOM_NULL, shorttext='', helptext=''): # pylint: disable=R0913
Structure.__init__(self)
if not isinstance(name, bytes):
name = name.encode('utf-8')
self.name = name
if helptext is not None and not isinstance(helptext, bytes):
helptext = helptext.encode('utf-8')
self.helptext = helptext
if shorttext is not None and not isinstance(shorttext, bytes):
shorttext = shorttext.encode('utf-8')
self.shorttext = shorttext
self.helptext = shorttext
self.semantics = semantics
self.typeof = typeof
self.indom = indom
self.item = item
self.name = name

##
# PCP Memory Mapped Value Services
Expand Down Expand Up @@ -248,11 +249,11 @@ def start(self):
On completion of this call, we're all visible to pmdammv.
"""
count_metrics = len(self._metrics)
metrics = (mmv_metric * count_metrics)()
metrics = (mmv_metric * count_metrics)() if count_metrics > 0 else None
for i in range(count_metrics):
metrics[i] = self._metrics[i]
count_indoms = len(self._indoms)
indoms = (mmv_indom * count_indoms)()
indoms = (mmv_indom * count_indoms)() if count_indoms > 0 else None
for i in range(count_indoms):
indoms[i] = self._indoms[i]
self._handle = LIBPCP_MMV.mmv_stats_init(self._name, self._cluster,
Expand Down
25 changes: 14 additions & 11 deletions src/python/pcp/pmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# Example use of this module for creating a PCP archive:
import math
import time
import pmapi
from pcp import pmi
Expand All @@ -43,21 +42,21 @@
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)
timetuple = math.modf(time.time())
useconds = int(timetuple[0] * 1000000)
seconds = int(timetuple[1])
log.pmiWrite(seconds, useconds)
log.pmiWrite(time.time()) # float seconds since epoch, or
#log.pmiWrite(seconds, useconds)
del log
"""

from pcp.pmapi import pmID, pmInDom, pmUnits, pmHighResResult, pmResult
from cpmi import pmiErrSymDict, PMI_MAXERRMSGLEN

import ctypes
from ctypes import cast, c_int, c_uint, c_longlong, c_char_p, POINTER
from ctypes import c_int, c_uint, c_longlong, c_char_p
from ctypes import cast, create_string_buffer, POINTER, CDLL
from ctypes.util import find_library
from math import modf

# Performance Co-Pilot PMI library (C)
LIBPCP_IMPORT = ctypes.CDLL(ctypes.util.find_library("pcp_import"))
LIBPCP_IMPORT = CDLL(find_library("pcp_import"))

##
# PMI Log Import Services
Expand Down Expand Up @@ -156,7 +155,7 @@ def __init__(self, *args):
def __str__(self):
try:
error_symbol = pmiErrSymDict[self.code]
error_string = ctypes.create_string_buffer(PMI_MAXERRMSGLEN)
error_string = create_string_buffer(PMI_MAXERRMSGLEN)
error_string = LIBPCP_IMPORT.pmiErrStr_r(self.code, error_string,
PMI_MAXERRMSGLEN)
except KeyError:
Expand Down Expand Up @@ -360,11 +359,15 @@ def pmiHighResWrite(self, sec, nsec):
raise pmiErr(status)
return status

def pmiWrite(self, sec, usec):
def pmiWrite(self, sec, usec=None):
"""PMI - flush data to a Log Import archive """
status = LIBPCP_IMPORT.pmiUseContext(self._ctx)
if status < 0:
raise pmiErr(status)
if sec and not usec and isinstance(sec, float):
ts = modf(sec)
sec = ts[1]
usec = ts[0] * 1000000
status = LIBPCP_IMPORT.pmiWrite2(sec, usec)
if status < 0:
raise pmiErr(status)
Expand Down

0 comments on commit a153ee2

Please sign in to comment.