From 44404c5a7f90aa42c3228b56b6f686f15a50fb29 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Thu, 24 Jan 2013 14:20:09 -0700 Subject: [PATCH] Add lib/getrusage.c Abstract out the SELF/THREAD complication. Signed-off-by: Jens Axboe --- Makefile | 8 +++++--- backend.c | 7 ++----- configure | 29 +++++++++++++++++++++++++---- lib/getrusage.c | 14 ++++++++++++++ lib/getrusage.h | 9 +++++++++ stat.c | 8 ++------ 6 files changed, 57 insertions(+), 18 deletions(-) create mode 100644 lib/getrusage.c create mode 100644 lib/getrusage.h diff --git a/Makefile b/Makefile index 22ea022a..1492516a 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,5 @@ DEBUGFLAGS = -D_FORTIFY_SOURCE=2 -DFIO_INC_DEBUG -CPPFLAGS= -D_GNU_SOURCE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 \ - $(DEBUGFLAGS) +CPPFLAGS= -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 $(DEBUGFLAGS) OPTFLAGS= -O3 -g -ffast-math $(EXTFLAGS) CFLAGS = -std=gnu99 -Wwrite-strings -Wall $(OPTFLAGS) LIBS = -lm $(EXTLIBS) @@ -29,7 +28,7 @@ SOURCE := gettime.c fio.c ioengines.c init.c stat.c log.c time.c filesetup.c \ engines/mmap.c engines/sync.c engines/null.c engines/net.c \ memalign.c server.c client.c iolog.c backend.c libfio.c flow.c \ json.c lib/zipf.c lib/axmap.c lib/lfsr.c gettime-thread.c \ - helpers.c lib/flist_sort.c lib/hweight.c + helpers.c lib/flist_sort.c lib/hweight.c lib/getrusage.c ifdef CONFIG_64BIT_LLP64 CFLAGS += -DBITS_PER_LONG=32 @@ -142,6 +141,9 @@ endif ifdef CONFIG_LINUX_FALLOCATE CFLAGS += -DCONFIG_LINUX_FALLOCATE endif +ifdef CONFIG_RUSAGE_THREAD + CFLAGS += -DCONFIG_RUSAGE_THREAD +endif ifeq ($(UNAME), Linux) SOURCE += diskutil.c fifo.c blktrace.c cgroup.c trim.c engines/sg.c \ diff --git a/backend.c b/backend.c index 2362123a..87810e89 100644 --- a/backend.c +++ b/backend.c @@ -50,6 +50,7 @@ #include "lib/rand.h" #include "memalign.h" #include "server.h" +#include "lib/getrusage.h" static pthread_t disk_util_thread; static struct fio_mutex *disk_thread_mutex; @@ -1210,11 +1211,7 @@ static void *thread_main(void *data) } fio_gettime(&td->epoch, NULL); -#ifdef RUSAGE_THREAD - getrusage(RUSAGE_THREAD, &td->ru_start); -#else - getrusage(RUSAGE_SELF, &td->ru_start); -#endif + fio_getrusage(&td->ru_start); clear_state = 0; while (keep_running(td)) { uint64_t verify_bytes; diff --git a/configure b/configure index f730dfdb..59899dbd 100755 --- a/configure +++ b/configure @@ -26,6 +26,9 @@ rm -rf config.log config_host_mak="config-host.mak" config_host_ld="config-host.ld" +# Default CFLAGS +CFLAGS="-D_GNU_SOURCE" + # Print a helpful header at the top of config.log echo "# FIO configure log $(date)" >> config.log printf "# Configured with:" >> config.log @@ -480,7 +483,6 @@ echo "POSIX fallocate $posix_fallocate" linux_2arg_affinity="no" linux_3arg_affinity="no" cat > $TMPC << EOF -#define _GNU_SOURCE #include int main(int argc, char **argv) { @@ -492,7 +494,6 @@ if compile_prog "" "" "sched_setaffinity(,,)"; then linux_3arg_affinity="yes" else cat > $TMPC << EOF -#define _GNU_SOURCE #include int main(int argc, char **argv) { @@ -601,7 +602,6 @@ sync_file_range="no" cat > $TMPC << EOF #include #include -#define _GNU_SOURCE #include #include int main(int argc, char **argv) @@ -642,7 +642,6 @@ echo "EXT4 move extent $ext4_me" # splice probe linux_splice="no" cat > $TMPC << EOF -#define _GNU_SOURCE #include #include int main(int argc, char **argv) @@ -789,6 +788,24 @@ if compile_prog "" "" "__thread"; then fi echo "__thread $tls_thread" +########################################## +# Check whether we have getrusage(RUSAGE_THREAD) +rusage_thread="no" +cat > $TMPC << EOF +#include +#include +int main(int argc, char **argv) +{ + struct rusage ru; + getrusage(RUSAGE_THREAD, &ru); + return 0; +} +EOF +if compile_prog "" "" "RUSAGE_THREAD"; then + rusage_thread="yes" +fi +echo "RUSAGE_THREAD $rusage_thread" + ############################################################################# echo "# Automatically generated by configure - do not modify" > $config_host_mak @@ -889,6 +906,10 @@ fi if test "$tls_thread" = "yes" ; then echo "CONFIG_TLS_THREAD=y" >> $config_host_mak fi +if test "$rusage_thread" = "yes" ; then + echo "CONFIG_RUSAGE_THREAD=y" >> $config_host_mak +fi echo "LIBS+=$LIBS" >> $config_host_mak echo "CC=$cc" >> $config_host_mak +echo "CFLAGS=$CFLAGS" >> $config_host_mak diff --git a/lib/getrusage.c b/lib/getrusage.c new file mode 100644 index 00000000..96dcf6d2 --- /dev/null +++ b/lib/getrusage.c @@ -0,0 +1,14 @@ +#include +#include "getrusage.h" + +int fio_getrusage(struct rusage *ru) +{ +#ifdef CONFIG_RUSAGE_THREAD + if (!getrusage(RUSAGE_THREAD, ru)) + return 0; + if (errno != EINVAL) + return -1; + /* Fall through to RUSAGE_SELF */ +#endif + return getrusage(RUSAGE_SELF, ru); +} diff --git a/lib/getrusage.h b/lib/getrusage.h new file mode 100644 index 00000000..49e64270 --- /dev/null +++ b/lib/getrusage.h @@ -0,0 +1,9 @@ +#ifndef FIO_GETRUSAGE_H +#define FIO_GETRUSAGE_H + +#include +#include + +extern int fio_getrusage(struct rusage *ru); + +#endif diff --git a/stat.c b/stat.c index 522901a9..8e1034b5 100644 --- a/stat.c +++ b/stat.c @@ -11,17 +11,13 @@ #include "diskutil.h" #include "lib/ieee754.h" #include "json.h" +#include "lib/getrusage.h" void update_rusage_stat(struct thread_data *td) { struct thread_stat *ts = &td->ts; -#ifdef RUSAGE_THREAD - getrusage(RUSAGE_THREAD, &td->ru_end); -#else - getrusage(RUSAGE_SELF, &td->ru_end); -#endif - + fio_getrusage(&td->ru_end); ts->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime); ts->sys_time += mtime_since(&td->ru_start.ru_stime, -- 2.25.1