rt-tests: cyclictest: truncate shm files to zero when USR2 received
PATCH 1/2 rt-tests: cyclictest: truncate shm files to zero when USR2 received
If SIGUSR2 is sent more than once to cyclictest, truncate the shared memory file for writing the latest snapshot.
Signed-off-by John Kacur <[email protected]>
--- src/cyclictest/cyclictest.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
src/cyclictest/cyclictest.c +5 -3
@@ -234,6 +234,7 @@ static void rstat_setup(void);static int latency_target_fd =-1 ;static int32_t latency_target_value =0 ; +static int rstat_ftruncate (int fd,off_t len);static int rstat_fd =-1 ;/* strlen("/cyclictest") + digits in max pid len + '\0' */ #define SHM_BUF_SIZE 19
@@ -1487,6 +1488,7 @@ static void sighand(int sig)fprintf (stderr,"ERROR: rstat_fd not valid\n" );return ; } +rstat_ftruncate (rstat_fd,0 ); quiet =0 ;dprintf (rstat_fd,"#---------------------------\n" );dprintf (rstat_fd,"# cyclictest current status:\n" );
@@ -1835,12 +1837,12 @@ static int rstat_shm_open(void)return fd; } -static int rstat_ftruncate (int fd) +static int rstat_ftruncate (int fd,off_t len) {int err; errno =0 ; - err =ftruncate (fd, _SC_PAGE_SIZE); + err =ftruncate (fd, len);if (err) {fprintf (stderr,"ftruncate error %s\n" ,strerror (errno)); }
@@ -1885,7 +1887,7 @@ static void rstat_setup(void)if (sfd <0 ) goto rstat_err; - res =rstat_ftruncate (sfd); + res =rstat_ftruncate (sfd, _SC_PAGE_SIZE);if (res) goto rstat_err1;
Signature · 1 line
2.20.1
PATCH 2/2 rt-tests: Add the get_cyclictest_snapshot.py utility
Add a python script to get the current status of running cyclictest instances.
There are a few options as explained in the online help, but you can simply run it without any options to get the status of all instances.
Signed-off-by John Kacur <[email protected]>
--- src/cyclictest/get_cyclictest_snapshot.py | 76 +++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100755 src/cyclictest/get_cyclictest_snapshot.py
src/cyclictest/get_cyclictest_snapshot.py new file +76 -0
@@ -0,0 +1,76 @@ +#!/usr/bin/env python3 + +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (C) 2020 John Kacur <[email protected]> + +import subprocess, signal, argparse, re, glob, sys + +parser = argparse.ArgumentParser(description='Get a snapshot of running instances of cyclictest') +parser.add_argument('-l', '--list', action='store_true', help='list the main pid(s) of running instances of cyclictest') +parser.add_argument('-s', '--snapshot', nargs='*', metavar='pid', help='take a snapshot of running instances of cyclictest') +parser.add_argument('-p', '--print', nargs='*', metavar='pid', help='print the snapshots') +args = parser.parse_args() + + +class Snapshot: + + def __init__(self): + self.pids = [] + self.shm_files = [] + self.refresh() + + def refresh(self): + self.pids = [] + self.shm_files = glob.glob('/dev/shm/cyclictest*') + self.shm_files.sort() + for shm_file in self.shm_files: + pid = re.search('[0-9]*$', shm_file).group() + self.pids += [pid] + + # Send USR2 to all running instances of cyclictest or just to + # a specific pid (spid) if specified + def take_snapshot(self, spids=None): + for pid in self.pids: + if (spids == None) or (pid in spids): + # print("kill -s USR2 ", pid) + subprocess.run(["kill", "-s", "USR2", pid]) + + def print_pids(self): + for pid in self.pids: + print(pid) + + # Print the data in /dev/shm/cyclictest* + def print(self, spids=None): + if spids == None: + for shm_file in self.shm_files: + with open(shm_file, 'r') as f: + data = f.read() + print(data) + else: + for spid in spids: + if spid in self.pids: + shm_file = '/dev/shm/cyclictest' + spid + with open(shm_file, 'r') as f: + data = f.read() + print(data) + +snapshot = Snapshot() + +if args.list: + snapshot.print_pids() + +if args.snapshot != None: + if len(args.snapshot) == 0: + snapshot.take_snapshot() + else: + snapshot.take_snapshot(args.snapshot) + +if args.print != None: + if len(args.print) == 0: + snapshot.print() + else: + snapshot.print(args.print) + +if len(sys.argv) == 1: + snapshot.take_snapshot() + snapshot.print()
Signature · 1 line
2.20.1