Skip to content

Commit 719cd2e

Browse files
committed
bridge: Adjust metrics channel to PCP 7 timespec API change
PCP 7.0 (PMAPI_VERSION_4) changed the API, in particular wrt. timespecs: performancecopilot/pcp@02c8fb5656ab635c68
1 parent 1c63dd9 commit 719cd2e

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/cockpit/channels/pcp.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ def float_to_timeval(timestamp: float) -> 'pmapi.timeval':
185185
usec = int((timestamp % 1000) * 1000)
186186
return pmapi.timeval(sec, usec)
187187

188+
@staticmethod
189+
def float_to_timespec(timestamp: float) -> 'pmapi.timespec':
190+
ts = pmapi.timespec()
191+
ts.tv_sec = int(timestamp / 1000)
192+
ts.tv_nsec = int((timestamp % 1000) * 1000000)
193+
return ts
194+
188195
@staticmethod
189196
def get_context_and_name(source: str) -> 'tuple[str, str]':
190197
if source == "":
@@ -377,8 +384,13 @@ def send_meta(self, results: 'pmapi.pmResult', context: 'pmapi.pmContext') -> No
377384
metrics.append(desc)
378385

379386
now = int(time.time()) * 1000
380-
timestamp = int(results.contents.timestamp.tv_sec * 1000
381-
+ results.contents.timestamp.tv_usec / 1000)
387+
388+
# PCP 7.0+: timespec with nanoseconds; PCP < 7.0: timeval with microseconds
389+
ms = (results.contents.timestamp.tv_nsec / 1000000
390+
if hasattr(results.contents.timestamp, 'tv_nsec')
391+
else results.contents.timestamp.tv_usec / 1000)
392+
timestamp = int(results.contents.timestamp.tv_sec * 1000 + ms)
393+
382394
self.send_json(source=self.source,
383395
interval=self.interval,
384396
timestamp=timestamp,
@@ -631,8 +643,15 @@ def sample_archives(self, archives: 'Sequence[ArchiveInfo]') -> None:
631643

632644
context = archive.context
633645
try:
634-
context.pmSetMode(c_api.PM_MODE_INTERP | c_api.PM_XTB_SET(c_api.PM_TIME_MSEC),
635-
self.float_to_timeval(timestamp), self.interval)
646+
if hasattr(c_api, 'PM_XTB_SET'):
647+
# PCP < 7.0: use XTB encoding with timeval
648+
context.pmSetMode(c_api.PM_MODE_INTERP | c_api.PM_XTB_SET(c_api.PM_TIME_MSEC),
649+
self.float_to_timeval(timestamp), self.interval)
650+
else:
651+
# PCP 7.0 removed PM_XTB_SET, use new timespec-based API
652+
context.pmSetMode(c_api.PM_MODE_INTERP,
653+
self.float_to_timespec(timestamp),
654+
self.float_to_timespec(self.interval))
636655
except pmapi.pmErr as exc:
637656
raise ChannelError('internal-error', message=str(exc)) from None
638657

0 commit comments

Comments
 (0)