linux-rt-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] rt-tests: cyclictest: truncate shm files to zero when USR2 received
@ 2020-02-18 19:55 John Kacur
  2020-02-18 19:55 ` [PATCH 2/2] rt-tests: Add the get_cyclictest_snapshot.py utility John Kacur
  0 siblings, 1 reply; 2+ messages in thread
From: John Kacur @ 2020-02-18 19:55 UTC (permalink / raw)
  To: rt-users; +Cc: Clark Williams, John Kacur

If SIGUSR2 is sent more than once to cyclictest, truncate the shared
memory file for writing the latest snapshot.

Signed-off-by: John Kacur <jkacur@redhat.com>
---
 src/cyclictest/cyclictest.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/cyclictest/cyclictest.c b/src/cyclictest/cyclictest.c
index 2f07426389b3..01ae72f8cdf1 100644
--- a/src/cyclictest/cyclictest.c
+++ b/src/cyclictest/cyclictest.c
@@ -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;
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/2] rt-tests: Add the get_cyclictest_snapshot.py utility
  2020-02-18 19:55 [PATCH 1/2] rt-tests: cyclictest: truncate shm files to zero when USR2 received John Kacur
@ 2020-02-18 19:55 ` John Kacur
  0 siblings, 0 replies; 2+ messages in thread
From: John Kacur @ 2020-02-18 19:55 UTC (permalink / raw)
  To: rt-users; +Cc: Clark Williams, John Kacur

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 <jkacur@redhat.com>
---
 src/cyclictest/get_cyclictest_snapshot.py | 76 +++++++++++++++++++++++
 1 file changed, 76 insertions(+)
 create mode 100755 src/cyclictest/get_cyclictest_snapshot.py

diff --git a/src/cyclictest/get_cyclictest_snapshot.py b/src/cyclictest/get_cyclictest_snapshot.py
new file mode 100755
index 000000000000..7dddfc5d65b0
--- /dev/null
+++ b/src/cyclictest/get_cyclictest_snapshot.py
@@ -0,0 +1,76 @@
+#!/usr/bin/env python3
+
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (C) 2020 John Kacur <jkacur@redhat.com>
+
+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()
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-02-18 19:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-18 19:55 [PATCH 1/2] rt-tests: cyclictest: truncate shm files to zero when USR2 received John Kacur
2020-02-18 19:55 ` [PATCH 2/2] rt-tests: Add the get_cyclictest_snapshot.py utility John Kacur

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).