Skip to content

Commit

Permalink
Add lib/getrusage.c
Browse files Browse the repository at this point in the history
Abstract out the SELF/THREAD complication.

Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
axboe committed Jan 24, 2013
1 parent 7732a09 commit 44404c5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 18 deletions.
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down
7 changes: 2 additions & 5 deletions backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 25 additions & 4 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
14 changes: 14 additions & 0 deletions lib/getrusage.c
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 9 additions & 0 deletions lib/getrusage.h
Original file line number Diff line number Diff line change
@@ -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
8 changes: 2 additions & 6 deletions stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 44404c5

Please sign in to comment.