*** Add a get_cpu() library ***
cover *** Add a get_cpu() library ***
I have added a get_cpu() library which should just do the right thing for you whether your system uses sched_getcpu() (the most common case), getcpu() or the vsyscall version of getcpu().
I have tested on Fedora, RHEL5 both 32 and 64-bit versions which cover all of the cases above. I would really appreciate more testing and reports on other distributions. Of course comments and patches are also welcome.
While doing this I changed the Makefile quite extensively. I hope you will find the changes simply and unify the Make process considerably. This could be further improved too - for example by creating a build directory.
In addition to the changes below, Clark has fixed the odd CRLF issue, and has a patch queued-up from Carsten that was missed last time.
Clark, you can pull the following changes from:git://git.kernel.org/pub/scm/linux/kernel/git/jkacur/rt-tests.git
branch: rt-tests-dev
Thanks
Carsten Emde (1):
Completely remove VERSION_STRING until we have a better solution.
John Kacur (3):
rt-tests: Makefile - Changes to unify and simplify the Makefile
rt-tests: Add a get_cpu() function to the library.
rt-tests: Clean-up - protect rt-utils.h
Makefile | 53 ++++++++++++++++++++------------ src/backfire/backfire.c | 2 +- src/backfire/sendme.c | 9 +++-- src/cyclictest/cyclictest.c | 1 - src/lib/rt-get_cpu.c | 25 +++++++++++++++ src/lib/rt-get_cpu.h | 46 ++++++++++++++++++++++++++++ src/lib/rt-utils.h | 5 +++ src/pi_tests/pi_stress.c | 7 +--- src/ptsematest/ptsematest.c | 12 +++---- src/rt-migrate-test/rt-migrate-test.c | 2 +- src/sigwaittest/sigwaittest.c | 8 +++-- src/svsematest/svsematest.c | 31 ++++--------------- 12 files changed, 135 insertions(+), 66 deletions(-) create mode 100644 src/lib/rt-get_cpu.c create mode 100644 src/lib/rt-get_cpu.h
PATCH 1/4 rt-tests: Makefile - Changes to unify and simplify the Makefile
- These changes simplify the Makefile. For example, notice that we no longer need to specify the full path to the source file
- These changes also unify the Makefile, for example, every program gets VERSION_STRING as an floating point number.
- Due to the above change I had to make a number of changes in the programs that expected VERSION_STRING as a string.
- By unifying what we CFLAGS, to include -D_GNU_SOURCE, I had to remove __USE_GNU which is reduncant in a number of files.
--- Makefile | 52 ++++++++++++++++++++------------ src/backfire/sendme.c | 2 - src/cyclictest/cyclictest.c | 1 - src/pi_tests/pi_stress.c | 7 +--- src/ptsematest/ptsematest.c | 1 - src/rt-migrate-test/rt-migrate-test.c | 2 +- src/sigwaittest/sigwaittest.c | 1 - src/svsematest/svsematest.c | 1 - 8 files changed, 35 insertions(+), 32 deletions(-)
Makefile +32 -20
@@ -10,48 +10,60 @@ bindir ?= $(prefix)/bin mandir ?= $(prefix)/share/man srcdir ?= $(prefix)/src -CFLAGS = -Wall -Wno-nonnull -Isrc/lib +CFLAGS = -D_GNU_SOURCE -Wall -Wno-nonnull -Isrc/lib + ifndef DEBUG CFLAGS += -O2 else CFLAGS += -O0 -g endif -UTILS = src/lib/rt-utils.o +VPATH = src/cyclictest: +VPATH += src/signaltest: +VPATH += src/pi_tests: +VPATH += src/rt-migrate-test: +VPATH += src/ptsematest: +VPATH += src/sigwaittest: +VPATH += src/svsematest: +VPATH += src/backfire: +VPATH += src/lib + +%.o: %.c + $(CC) -D VERSION_STRING=$(VERSION_STRING) -c $< $(CFLAGS) .PHONY: all all: $(TARGETS) -cyclictest: src/cyclictest/cyclictest.c $(UTILS) - $(CC) $(CFLAGS) -D VERSION_STRING=$(VERSION_STRING) $^ -o $@ $(LIBS) +cyclictest: cyclictest.o rt-utils.o + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) -signaltest: src/signaltest/signaltest.c $(UTILS) - $(CC) $(CFLAGS) -D VERSION_STRING=$(VERSION_STRING) $^ -o $@ $(LIBS) +signaltest: signaltest.o rt-utils.o + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) -classic_pi: src/pi_tests/classic_pi.c - $(CC) $(CFLAGS) -D_GNU_SOURCE -D VERSION_STRING=\"$(VERSION_STRING)\" $^ -o $@ $(LIBS) +classic_pi: classic_pi.o + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) -pi_stress: src/pi_tests/pi_stress.c - $(CC) $(CFLAGS) -D_GNU_SOURCE -D VERSION_STRING=\"$(VERSION_STRING)\" $^ -o $@ $(LIBS) +pi_stress: pi_stress.o + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) hwlatdetect: src/hwlatdetect/hwlatdetect.py chmod +x src/hwlatdetect/hwlatdetect.py ln -s src/hwlatdetect/hwlatdetect.py hwlatdetect -rt-migrate-test: src/rt-migrate-test/rt-migrate-test.c - $(CC) $(CFLAGS) -D_GNU_SOURCE -D VERSION_STRING=\"$(VERSION_STRING)\" $^ -o $@ $(LIBS) +rt-migrate-test: rt-migrate-test.o + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) -ptsematest: src/ptsematest/ptsematest.c $(UTILS) - $(CC) $(CFLAGS) -D VERSION_STRING=$(VERSION_STRING) $^ -o $@ $(LIBS) +ptsematest: ptsematest.o rt-utils.o + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) -sigwaittest: src/sigwaittest/sigwaittest.c $(UTILS) - $(CC) $(CFLAGS) -D VERSION_STRING=$(VERSION_STRING) $^ -o $@ $(LIBS) +sigwaittest: sigwaittest.o rt-utils.o + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) -svsematest: src/svsematest/svsematest.c $(UTILS) - $(CC) $(CFLAGS) -D VERSION_STRING=$(VERSION_STRING) $^ -o $@ $(LIBS) +svsematest: svsematest.o rt-utils.o + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) -sendme: src/backfire/sendme.c $(UTILS) - $(CC) $(CFLAGS) -D VERSION_STRING=$(VERSION_STRING) $^ -o $@ $(LIBS) +sendme: sendme.o rt-utils.o + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) CLEANUP = $(TARGETS) *.o .depend *.*~ *.orig *.rej rt-tests.spec CLEANUP += $(if $(wildcard .git), ChangeLog)
src/backfire/sendme.c +0 -2
@@ -22,7 +22,6 @@#include <stdlib.h> #include <unistd.h> #include <limits.h> -#define __USE_GNU#include <fcntl.h> #include <getopt.h> #include <signal.h>
@@ -31,7 +30,6 @@#include <time.h> #include "rt-utils.h" -#define _GNU_SOURCE#include <utmpx.h> #include <sys/types.h> #include <sys/stat.h>
src/cyclictest/cyclictest.c +0 -1
@@ -13,7 +13,6 @@#include <stdio.h> #include <stdlib.h> #include <unistd.h> -#define __USE_GNU#include <fcntl.h> #include <getopt.h> #include <pthread.h>
src/pi_tests/pi_stress.c +2 -5
@@ -55,10 +55,6 @@#include <sys/wait.h> #include <termios.h> -/* version */ -const char *version = -"pi_stress v" VERSION_STRING " (" __DATE__" " __TIME__")" ; -/* conversions */ #define USEC_PER_SEC 1000000#define NSEC_PER_SEC 1000000000
@@ -1230,7 +1226,8 @@ void process_command_line(int argc, char **argv) debugging =1 ;break ;case 'V' : -puts (version); +printf ("pi_stress v%1.2f " ,VERSION_STRING ); +printf ("(%s %s)\n" , __DATE__, __TIME__);exit (0 );case 'u' : uniprocessor =1 ;
src/ptsematest/ptsematest.c +0 -1
@@ -36,7 +36,6 @@#include <utmpx.h> #include "rt-utils.h" -#define __USE_GNU#include <pthread.h> #define gettid () syscall(__NR_gettid)
src/rt-migrate-test/rt-migrate-test.c +1 -1
@@ -162,7 +162,7 @@ static void usage(char **argv) p--; p++; -printf ("%s %s\n" , p,VERSION_STRING ); +printf ("%s %1.2f\n" , p,VERSION_STRING );printf ("Usage:\n" "%s <options> nr_tasks\n\n" "-p prio --prio prio base priority to start RT tasks with (2) \n"
src/sigwaittest/sigwaittest.c +0 -1
@@ -38,7 +38,6 @@#include <utmpx.h> #include "rt-utils.h" -#define __USE_GNU#include <pthread.h> #define gettid () syscall(__NR_gettid)
src/svsematest/svsematest.c +0 -1
@@ -24,7 +24,6 @@#include <stdlib.h> #include <unistd.h> #include <limits.h> -#define __USE_GNU#include <fcntl.h> #include <getopt.h> #include <pthread.h>
Signature · 1 line
1.6.5.2
PATCH 2/4 rt-tests: Add a get_cpu() function to the library.
Add a get_cpu() function to the library. Most platforms will simply use sched_getcpu() However, if you have a glibc < 2.6 then 64-bits will use vsyscall for getcpu (if available). 32-bits will use getcpu() (if available)
--- Makefile | 17 ++++++++------- src/backfire/sendme.c | 7 ++++- src/lib/rt-get_cpu.c | 25 ++++++++++++++++++++++ src/lib/rt-get_cpu.h | 46 +++++++++++++++++++++++++++++++++++++++++ src/ptsematest/ptsematest.c | 11 ++++----- src/sigwaittest/sigwaittest.c | 7 ++++- src/svsematest/svsematest.c | 30 ++++++-------------------- 7 files changed, 102 insertions(+), 41 deletions(-) create mode 100644 src/lib/rt-get_cpu.c create mode 100644 src/lib/rt-get_cpu.h
Makefile +9 -8
@@ -4,6 +4,7 @@ TARGETS = cyclictest signaltest classic_pi pi_stress \ hwlatdetect rt-migrate-test ptsematest sigwaittest svsematest \ sendme LIBS = -lpthread -lrt +EXTRA_LIBS ?= -ldl # for get_cpu DESTDIR ?= prefix ?= /usr/local bindir ?= $(prefix)/bin
@@ -53,17 +54,17 @@ hwlatdetect: src/hwlatdetect/hwlatdetect.py rt-migrate-test: rt-migrate-test.o $(CC) $(CFLAGS) -o $@ $^ $(LIBS) -ptsematest: ptsematest.o rt-utils.o - $(CC) $(CFLAGS) -o $@ $^ $(LIBS) +ptsematest: ptsematest.o rt-utils.o rt-get_cpu.o + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) $(EXTRA_LIBS) -sigwaittest: sigwaittest.o rt-utils.o - $(CC) $(CFLAGS) -o $@ $^ $(LIBS) +sigwaittest: sigwaittest.o rt-utils.o rt-get_cpu.o + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) $(EXTRA_LIBS) -svsematest: svsematest.o rt-utils.o - $(CC) $(CFLAGS) -o $@ $^ $(LIBS) +svsematest: svsematest.o rt-utils.o rt-get_cpu.o + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) $(EXTRA_LIBS) -sendme: sendme.o rt-utils.o - $(CC) $(CFLAGS) -o $@ $^ $(LIBS) +sendme: sendme.o rt-utils.o rt-get_cpu.o + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) $(EXTRA_LIBS) CLEANUP = $(TARGETS) *.o .depend *.*~ *.orig *.rej rt-tests.spec CLEANUP += $(if $(wildcard .git), ChangeLog)
src/backfire/sendme.c +5 -2
@@ -29,6 +29,7 @@#include <string.h> #include <time.h> #include "rt-utils.h" +#include "rt-get_cpu.h" #include <utmpx.h> #include <sys/types.h>
@@ -195,8 +196,10 @@ int main(int argc, char *argv[])if (setaffinity !=AFFINITY_UNSPECIFIED ) {CPU_ZERO (&mask); -if (setaffinity ==AFFINITY_USECURRENT ) - affinity =sched_getcpu (); +if (setaffinity ==AFFINITY_USECURRENT ) { +get_cpu_setup (); + affinity =get_cpu (); + }CPU_SET (affinity, &mask);if (sched_setaffinity (0 ,sizeof (mask), &mask) ==-1 ) fprintf(stderr, "WARNING: Could not set CPU affinity "
src/lib/rt-get_cpu.c new file +25 -0
@@ -0,0 +1,25 @@ +#include "rt-get_cpu.h" +#ifdef __NR_getcpu +#elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ + && __GLIBC__>=2 && __GLIBC_MINOR__>=6 +#else +int (*get_cpu)(void ); +int (*get_cpu_vdsop)(unsigned int *,unsigned int *,void *); + +int get_cpu_setup (void ) +{ +void *handle =dlopen ("linux-vdso.so.1" ,RTLD_LAZY ); + get_cpu_vdsop =NULL ; +if (handle) { + get_cpu_vdsop =dlsym (handle,"getcpu" ); +dlclose (handle); +if (get_cpu_vdsop) { + get_cpu = getcpu_vdso; +return 0 ; + } + } +return -1 ; +} + +#endif +
src/lib/rt-get_cpu.h new file +46 -0
46 lines not on this page · raw message
src/ptsematest/ptsematest.c +5 -6
@@ -35,6 +35,7 @@#include <linux/unistd.h> #include <utmpx.h> #include "rt-utils.h" +#include "rt-get_cpu.h" #include <pthread.h>
@@ -106,9 +107,8 @@ void *semathread(void *param) par->samples++;if (par->max_cycles && par->samples >= par->max_cycles) par->shutdown =1 ; -if (mustgetcpu) { - par->cpu =sched_getcpu (); - } +if (mustgetcpu) + par->cpu =get_cpu (); }else {/* Receiver */ if (!first) {
@@ -148,9 +148,8 @@ void *semathread(void *param)if (par->max_cycles && par->samples >= par->max_cycles) par->shutdown =1 ; -if (mustgetcpu) { - par->cpu =sched_getcpu (); - } +if (mustgetcpu) + par->cpu =get_cpu ();nanosleep (&par->delay,NULL );pthread_mutex_unlock (&syncmutex[par->num]); }
src/sigwaittest/sigwaittest.c +5 -2
@@ -37,6 +37,7 @@#include <linux/unistd.h> #include <utmpx.h> #include "rt-utils.h" +#include "rt-get_cpu.h" #include <pthread.h>
@@ -138,7 +139,7 @@ void *semathread(void *param) par->shutdown =1 ;if (mustgetcpu) { - par->cpu =sched_getcpu (); + par->cpu =get_cpu (); }sigwait (&sigset, &sig); }else {
@@ -162,7 +163,7 @@ void *semathread(void *param) par->shutdown =1 ;if (mustgetcpu) { - par->cpu =sched_getcpu (); + par->cpu =get_cpu (); }/* * Latency is the time spent between sending and
@@ -358,6 +359,8 @@ int main(int argc, char *argv[])return 1 ; } +get_cpu_setup ();/* init get_cpu() */ +if (mustfork) {int shmem;
src/svsematest/svsematest.c +7 -23
@@ -41,13 +41,9 @@#include <sys/time.h> #include <sys/mman.h> #include "rt-utils.h" - -#define HAS_SCHED_GETCPU +#include "rt-get_cpu.h" #define gettid () syscall(__NR_gettid) -#ifndef HAS_SCHED_GETCPU -#define getcpu (cpu, node, cache) syscall(__NR_getcpu, cpu, node, cache) -#endif #define USEC_PER_SEC 1000000
@@ -143,15 +139,8 @@ void *semathread(void *param)if (par->max_cycles && par->samples >= par->max_cycles) par->shutdown =1 ; -if (mustgetcpu) { -#ifdef HAS_SCHED_GETCPU - par->cpu =sched_getcpu (); -#else -int c, s; - s =getcpu (&c,NULL ,NULL ); - par->cpu = (s ==-1 ) ? s : c; -#endif - } +if (mustgetcpu) + par->cpu =get_cpu (); sb.sem_num =SEM_WAIT_FOR_RECEIVER ; sb.sem_op =SEM_LOCK ;
@@ -182,15 +171,8 @@ void *semathread(void *param)if (par->max_cycles && par->samples >= par->max_cycles) par->shutdown =1 ; -if (mustgetcpu) { -#ifdef HAS_SCHED_GETCPU - par->cpu =sched_getcpu (); -#else -int c, s; - s =getcpu (&c,NULL ,NULL ); - par->cpu = (s ==-1 ) ? s : c; -#endif - } +if (mustgetcpu) + par->cpu =get_cpu ();timersub (&par->received, &neighbor->unblocked, &par->diff);
@@ -408,6 +390,8 @@ int main(int argc, char *argv[])return 1 ; } +get_cpu_setup (); +if (mustfork) {int shmem;
Signature · 1 line
1.6.5.2
PATCH 3/4 rt-tests: Clean-up - protect rt-utils.h
Clean-up: Protect rt-utils.h with #ifndef RT_UTILS_H
--- src/lib/rt-utils.h | 5 +++++ 1 files changed, 5 insertions(+), 0 deletions(-)
src/lib/rt-utils.h +5 -0
@@ -1,6 +1,11 @@ +#ifndef RT_UTILS.H +#define RT_UTILS .H +#define _STR (x) #x#define STR (x) _STR(x)#define MAX_PATH 256int check_privs (void );char *get_debugfileprefix (void ); + +#endif /* RT_UTILS.H */
Signature · 1 line
1.6.5.2
PATCH 4/4 Completely remove VERSION_STRING until we have a better solution.
From: Carsten Emde <[email protected]>
--- src/backfire/backfire.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
src/backfire/backfire.c +1 -1
@@ -132,7 +132,7 @@ static int __init backfire_init(void)if (ret)printk (KERN_ERR "backfire: can't register dynamic misc device\n" );else -printk (KERN_INFO "backfire driver v__VERSION_STRING__ misc device %d\n" , +printk (KERN_INFO "backfire driver misc device %d\n" , backfire_dev.minor);return ret; }
Signature · 1 line
1.6.5.2
Quoted 12 lines · On Mon, 21 Dec 2009 22:45:46 +0100 John Kacur <[email protected]> wrote:
I have added a
get_cpu()library which should just do the right thing for you whether your system usessched_getcpu()(the most common case),getcpu()or the vsyscall version ofgetcpu().I have tested on Fedora, RHEL5 both 32 and 64-bit versions which cover all of the cases above. I would really appreciate more testing and reports on other distributions. Of course comments and patches are also welcome.
While doing this I changed the
Makefilequite extensively. I hope you will find the changes simply and unify the Make process considerably. This could be further improved too - for example by creating a build directory.
Quoted 7 lines
In addition to the changes below, Clark has fixed the odd CRLF issue, and has a patch queued-up from Carsten that was missed last time.
Clark, you can pull the following changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/jkacur/rt-tests.git
branch: rt-tests-dev
Pulled and merged.
Tarball rt-tests-0.58.tar.gz pushed to kernel.org:
http://www.kernel.org/pub/linux//kernel/people/clrkwllms/rt-tests/rt-tests-0.58.tar.gz
Git tree updated with tag v0.58:
git://git.kernel.org/pub/scm/linux/kernel/git/clrkwllms/rt-tests.git
Clark
Re: [PATCH 3/4] rt-tests: Clean-up - protect rt-utils.h
In reply to John KacurQuoted 3 lines · John Kacur
Clean-up: Protect
rt-utils.hwith #ifndefRT_UTILS_H[..]
+#ifndef RT_UTILS.H
Macro names should only consist of alphanumeric characters and underscores, i.e. 'a-z', 'A-Z', '0-9', and '_', and the first character should not be a digit.
src/lib/rt-utils.h +3 -3
@@ -1,5 +1,5 @@ -#ifndef RT_UTILS.H -#define RT_UTILS .H +#ifndef RT_UTILS_H +#define RT_UTILS_H #define _STR (x) #x#define STR (x) _STR(x)
@@ -8,4 +8,4 @@int check_privs (void );char *get_debugfileprefix (void ); -#endif /* RT_UTILS.H */ +#endif /* RT_UTILS_H */
Re: [PATCH 3/4] rt-tests: Clean-up - protect rt-utils.h
Quoted 9 lines · On Tue, 22 Dec 2009 00:09:35 +0100 Carsten Emde <[email protected]> wrote:
Quoted 3 lines · John Kacur
Clean-up: Protect
rt-utils.hwith #ifndefRT_UTILS_H[..]
+#ifndef RT_UTILS.HMacro names should only consist of alphanumeric characters and underscores, i.e. 'a-z', 'A-Z', '0-9', and '_', and the first character should not be a digit.
Yeah, I caught that and also pushed it out of application name space with two leading underscores.
Clark
Re: [PATCH 3/4] rt-tests: Clean-up - protect rt-utils.h
In reply to Carsten EmdeQuoted 29 lines · Carsten Emde
Quoted 3 lines · John Kacur
Clean-up: Protect
rt-utils.hwith #ifndefRT_UTILS_H[..]
+#ifndef RT_UTILS.HMacro names should only consist of alphanumeric characters and underscores, i.e. 'a-z', 'A-Z', '0-9', and '_', and the first character should not be a digit.
Signed-off-by Carsten Emde <[email protected]>src/lib/rt-utils.h +3
-4@@ -1,5 +1,5 @@ -#ifndef RT_UTILS.H -#define RT_UTILS .H +#ifndef RT_UTILS_H +#define RT_UTILS_H #define _STR(x) #x #define STR(x) _STR(x)@@ -8,4 +8,4 @@ int check_privs(void); char *get_debugfileprefix(void); -#endif /* RT_UTILS.H */ +#endif /* RT_UTILS_H */ --
Quoted 4 lines
To unsubscribe from this list: send the line "unsubscribe linux-rt-users" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Thanks Carsten.
That was a good sign that I was getting tired! I think Clark already fixed that up.
John
--
To unsubscribe from this list: send the line "unsubscribe linux-rt-users" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 3/4] rt-tests: Clean-up - protect rt-utils.h
In reply to Clark WilliamsClark,
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Clean-up: Protect
rt-utils.hwith #ifndefRT_UTILS_H[..]
+#ifndef RT_UTILS.HMacro names should only consist of alphanumeric characters and underscores, i.e. 'a-z', 'A-Z', '0-9', and '_', and the first character should not be a digit.
Yeah, I caught that and also pushed it out of application name space with two leading underscores.
Great.
I did some first tests on 0.58, and all works very well now. Thanks a lot, you and John, for all the work!
As a very minor issue, the below patch still didn't make it into your tree.
Carsten.
-=--------------------------------------------------------------------=-
Explain in the sendme error message how to make the backfire module, if not available.
src/backfire/sendme.c +6 -1
@@ -209,7 +209,12 @@ int main(int argc, char *argv[]) path =open ("/dev/backfire" ,O_RDWR );if (path <0 ) {fprintf (stderr,"ERROR: Could not access backfire device, " -"try 'modprobe backfire'\n" ); +"try 'modprobe backfire'.\n" +"If the module backfire can't be loaded, " +"it may need to be built first.\n" +"Execute 'cd src/backfire; make' in the " +"rt-tests directory (requires rt-tests\n" +"sources and kernel-devel package).\n" );return 1 ; } fl.l_type =F_WRLCK ;