Re: [PATCH 2/2] cyclictest: simplify rstat_setup
John Kacur <[email protected]>
| Newsgroups | org.kernel.vger.linux-rt-users |
|---|---|
| Message-ID | <[email protected]> |
On Sun, 1 Mar 2026, Lukas Beckmann wrote: > The shm file is truncated to zero each time before it is written. > It does not make sense to truncate it to something non zero on setup or > to mlock it. > > Signed-off-by: Lukas Beckmann <[email protected]> > --- > src/cyclictest/cyclictest.c | 89 ++++++------------------------------- > 1 file changed, 13 insertions(+), 76 deletions(-) > > diff --git a/src/cyclictest/cyclictest.c b/src/cyclictest/cyclictest.c > index 960c905..7b959ac 100644 > --- a/src/cyclictest/cyclictest.c > +++ b/src/cyclictest/cyclictest.c > @@ -1771,33 +1771,6 @@ static void trigger_update(struct thread_param *par, int diff, int64_t ts) > pthread_mutex_unlock(&trigger_lock); > } > > -/* Running status shared memory open */ > -static int rstat_shm_open(void) > -{ > - int fd; > - pid_t pid; > - > - pid = getpid(); > - > - snprintf(shm_name, SHM_BUF_SIZE, "%s%d", "/cyclictest", pid); > - > - errno = 0; > - fd = shm_unlink(shm_name); > - > - if ((fd == -1) && (errno != ENOENT)) { > - fprintf(stderr, "ERROR: shm_unlink %s\n", strerror(errno)); > - return fd; > - } > - > - errno = 9; > - fd = shm_open(shm_name, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); > - if (fd == -1) > - fprintf(stderr, "ERROR: shm_open %s\n", strerror(errno)); > - > - rstat_fd = fd; > - > - return fd; > -} > > static int rstat_ftruncate(int fd, off_t len) > { > @@ -1811,62 +1784,26 @@ static int rstat_ftruncate(int fd, off_t len) > return err; > } > > -static void *rstat_mmap(int fd) > -{ > - void *mptr; > > - errno = 0; > - mptr = mmap(0, _SC_PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); > +static void rstat_setup(void) > +{ > + int fd; > + pid_t pid; > > - if (mptr == (void*)-1) > - fprintf(stderr, "ERROR: mmap, %s\n", strerror(errno)); > + pid = getpid(); > > - return mptr; > -} > + snprintf(shm_name, SHM_BUF_SIZE, "%s%d", "/cyclictest", pid); > > -static int rstat_mlock(void *mptr) > -{ > - int err; errno = 0 before the call to shm_unlink for good defensive programming other than that the patch looks good, I will do a little more testing > + fd = shm_unlink(shm_name); > > - errno = 0; > - err = mlock(mptr, _SC_PAGE_SIZE); > - if (err == -1) > - fprintf(stderr, "ERROR, mlock %s\n", strerror(errno)); > + if ((fd == -1) && (errno != ENOENT)) > + fprintf(stderr, "ERROR: shm_unlink %s\n", strerror(errno)); > > - return err; > -} > + fd = shm_open(shm_name, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); > + if (fd == -1) > + fprintf(stderr, "ERROR: shm_open %s\n", strerror(errno)); > > -static void rstat_setup(void) > -{ > - int res; > - void *mptr = NULL; > - > - int sfd = rstat_shm_open(); > - if (sfd < 0) > - goto rstat_err; > - > - res = rstat_ftruncate(sfd, _SC_PAGE_SIZE); > - if (res) > - goto rstat_err1; > - > - mptr = rstat_mmap(sfd); > - if (mptr == MAP_FAILED) > - goto rstat_err1; > - > - res = rstat_mlock(mptr); > - if (res) > - goto rstat_err2; > - > - return; > - > -rstat_err2: > - munmap(mptr, _SC_PAGE_SIZE); > -rstat_err1: > - close(sfd); > - shm_unlink(shm_name); > -rstat_err: > - rstat_fd = -1; > - return; > + rstat_fd = fd; > } > > static void write_stats(FILE *f, void *data) > -- > 2.53.0 > > >