Add lib/getrusage.c
authorJens Axboe <axboe@kernel.dk>
Thu, 24 Jan 2013 21:20:09 +0000 (14:20 -0700)
committerJens Axboe <axboe@kernel.dk>
Thu, 24 Jan 2013 21:20:09 +0000 (14:20 -0700)
Abstract out the SELF/THREAD complication.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
Makefile
backend.c
configure
lib/getrusage.c [new file with mode: 0644]
lib/getrusage.h [new file with mode: 0644]
stat.c

index 22ea022a8ea62e7cf7a9a35f4c0eb1b5463661b9..1492516a034951eb068c0e698ef89e381c49e409 100644 (file)
--- 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 \
index 2362123a5abf57cdccbc12ba15c5d0268ba7b2b9..87810e89d696ea7f453469eff59a486620dbdcab 100644 (file)
--- 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;
index f730dfdb369947d6b1d77d98ff0e78ffe5a5ae13..59899dbda9f99d53370ad2751412b3a16572f62d 100755 (executable)
--- 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 <sched.h>
 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 <sched.h>
 int main(int argc, char **argv)
 {
@@ -601,7 +602,6 @@ sync_file_range="no"
 cat > $TMPC << EOF
 #include <stdio.h>
 #include <unistd.h>
-#define _GNU_SOURCE
 #include <fcntl.h>
 #include <linux/fs.h>
 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 <stdio.h>
 #include <fcntl.h>
 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 <sys/time.h>
+#include <sys/resource.h>
+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 (file)
index 0000000..96dcf6d
--- /dev/null
@@ -0,0 +1,14 @@
+#include <errno.h>
+#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 (file)
index 0000000..49e6427
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef FIO_GETRUSAGE_H
+#define FIO_GETRUSAGE_H
+
+#include <sys/time.h>
+#include <sys/resource.h>
+
+extern int fio_getrusage(struct rusage *ru);
+
+#endif
diff --git a/stat.c b/stat.c
index 522901a993e89f70d6f075ac9e5c2ebd52e77eae..8e1034b513fd162dbc539eacdcf2640cfa4ff88d 100644 (file)
--- a/stat.c
+++ b/stat.c
 #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,