diff --git a/configure.ac b/configure.ac index 7cfde10..d39e2ba 100644 --- a/configure.ac +++ b/configure.ac @@ -1,655 +1,672 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.61]) AC_INIT([libqb], m4_esyscmd([build-aux/git-version-gen .tarball-version]), [developers@clusterlabs.org]) AC_CONFIG_AUX_DIR([build-aux]) AC_CONFIG_SRCDIR([lib/ringbuffer.c]) AC_CONFIG_HEADERS([include/config.h include/qb/qbconfig.h]) AC_USE_SYSTEM_EXTENSIONS AM_INIT_AUTOMAKE([-Wno-portability dist-xz]) dnl automake >= 1.11 offers --enable-silent-rules for suppressing the output from dnl normal compilation. When a failure occurs, it will then display the full dnl command line dnl Wrap in m4_ifdef to avoid breaking on older platforms m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])]) LT_PREREQ([2.2.6]) LT_INIT AC_CONFIG_MACRO_DIR([m4]) AC_CANONICAL_HOST AC_PROG_LIBTOOL AC_LANG([C]) dnl Fix default variables - "prefix" variable if not specified if test "$prefix" = "NONE"; then prefix="/usr" if test "$localstatedir" = "\${prefix}/var"; then localstatedir="/var" fi if test "$sysconfdir" = "\${prefix}/etc"; then sysconfdir="/etc" fi if test "$libdir" = "\${exec_prefix}/lib"; then if test -e /usr/lib64; then libdir="/usr/lib64" else libdir="/usr/lib" fi fi fi if test "$srcdir" = "."; then AC_MSG_NOTICE([building in place srcdir:$srcdir]) AC_DEFINE([BUILDING_IN_PLACE], 1, [building in place]) else AC_MSG_NOTICE([building out of tree srcdir:$srcdir]) fi # Checks for programs. # check stolen from gnulib/m4/gnu-make.m4 if ! ${MAKE-make} --version /cannot/make/this >/dev/null 2>&1; then AC_MSG_ERROR([you don't seem to have GNU make; it is required]) fi AC_PROG_CXX AC_PROG_AWK AC_PROG_CC AC_PROG_CPP AM_PROG_CC_C_O AC_PROG_INSTALL AC_PROG_LN_S AC_PROG_MAKE_SET AC_CHECK_PROGS([PKGCONFIG], [pkg-config]) AC_CHECK_PROGS([DOXYGEN], [doxygen]) AM_CONDITIONAL(HAVE_DOXYGEN, test -n "${DOXYGEN}") AC_CHECK_PROGS([SPLINT], [splint]) AM_CONDITIONAL(HAVE_SPLINT, test -n "${SPLINT}") ## local helper functions # this function checks if CC support options passed as # args. Global CFLAGS are ignored during this test. cc_supports_flag() { BACKUP="$CPPFLAGS" CPPFLAGS="$CPPFLAGS $@ -Werror" AC_MSG_CHECKING([whether $CC supports "$@"]) AC_PREPROC_IFELSE([AC_LANG_PROGRAM([])], [RC=0; AC_MSG_RESULT([yes])], [RC=1; AC_MSG_RESULT([no])]) CPPFLAGS="$BACKUP" return $RC } ## cleanup AC_MSG_NOTICE(Sanitizing prefix: ${prefix}) case $prefix in NONE) prefix=/usr/local;; esac AC_MSG_NOTICE(Sanitizing exec_prefix: ${exec_prefix}) case $exec_prefix in NONE) exec_prefix=$prefix;; prefix) exec_prefix=$prefix;; esac # Checks for libraries. dnl librt from glibc NEEDs libpthread dnl so. if test for libpthread after librt dnl it will always be "none needed", but it is not true dnl when linking libraries. Looks like a bug. AC_SEARCH_LIBS([pthread_create], [pthread]) AC_SEARCH_LIBS([mq_open], [rt]) AC_SEARCH_LIBS([dlopen], [dl]) AC_SEARCH_LIBS([socket], [socket]) AC_SEARCH_LIBS([gethostbyname], [nsl]) # look for testing harness "check" PKG_CHECK_MODULES([CHECK], [check >= 0.9.4],[with_check=yes],[with_check=no]) AM_CONDITIONAL(HAVE_CHECK, test "${with_check}" = "yes") # look for GLIB (used for testing integration) PKG_CHECK_MODULES(GLIB, glib-2.0 >= 2.0, have_glib=yes, have_glib=no) AM_CONDITIONAL(HAVE_GLIB, test x$have_glib = xyes) AC_SUBST(GLIB_CFLAGS) AC_SUBST(GLIB_LIBS) if test x"$have_glib" = xyes; then AC_DEFINE_UNQUOTED([HAVE_GLIB], [1], [We have glib]) fi # Checks for header files. AC_HEADER_STDC AC_HEADER_SYS_WAIT AC_CHECK_HEADERS([arpa/inet.h link.h fcntl.h inttypes.h limits.h netinet/in.h \ stdint.h stddef.h stdlib.h string.h strings.h \ dlfcn.h time.h sys/time.h sys/types.h sys/stat.h \ sys/param.h sys/socket.h sys/time.h sys/poll.h sys/epoll.h \ sys/uio.h sys/event.h sys/sockio.h sys/un.h sys/resource.h \ syslog.h errno.h unistd.h sys/mman.h \ sys/sem.h sys/ipc.h sys/msg.h netdb.h]) # Checks for typedefs, structures, and compiler characteristics. AC_TYPE_UID_T AC_C_INLINE AC_TYPE_INT32_T AC_TYPE_INT64_T AC_TYPE_INT8_T AC_TYPE_MODE_T AC_TYPE_PID_T AC_TYPE_SIZE_T AC_TYPE_SSIZE_T AC_TYPE_UINT64_T AC_TYPE_UINT32_T AC_TYPE_UINT16_T AC_TYPE_UINT8_T AC_CHECK_MEMBER([struct sockaddr_un.sun_len], [AC_DEFINE([HAVE_STRUCT_SOCKADDR_UN_SUN_LEN], 1, [Define to 1 if struct sockaddr_un has a member sun_len])], [], [#include ]) AC_MSG_CHECKING(looking for union semun in sys/sem.h) AC_COMPILE_IFELSE([AC_LANG_PROGRAM( [[#include #include #include ]], [[union semun arg; semctl(0, 0, 0, arg);]])], [ AC_MSG_RESULT([yes]) AC_DEFINE_UNQUOTED([HAVE_SEMUN], 1, [Define to 1 if you have union semun.]) ], [ AC_MSG_RESULT([no]) ] ) +AC_MSG_CHECKING(for a working clock_getres(CLOCK_MONOTONIC, &ts)) +AC_RUN_IFELSE([AC_LANG_PROGRAM( +[[#include ]], +[[struct timespec ts; if(clock_getres(CLOCK_MONOTONIC, &ts)) return -1;]])], + [ + AC_MSG_RESULT([yes]) + AC_DEFINE_UNQUOTED([HAVE_CLOCK_GETRES_MONOTONIC], 1, [Define to 1 if clock_getres(CLOCK_MONOTONIC, &ts) works]) + ], + [ + AC_MSG_RESULT([no]) + ] + ) AC_MSG_CHECKING(for MSG_NOSIGNAL) AC_TRY_COMPILE([#include ], [ int f = MSG_NOSIGNAL; ], [ AC_MSG_RESULT(yes) AC_DEFINE(HAVE_MSG_NOSIGNAL, 1, [Define this symbol if you have MSG_NOSIGNAL])], [ AC_MSG_RESULT(no)]) AC_MSG_CHECKING(for SO_NOSIGPIPE ) AC_TRY_COMPILE([#include ], [ int f = SO_NOSIGPIPE; ], [ AC_MSG_RESULT(yes) AC_DEFINE(HAVE_SO_NOSIGPIPE, 1, [Define this symbol if you have SO_NOSIGPIPE]) ], [ AC_MSG_RESULT(no)]) # Checks for library functions. AC_FUNC_CHOWN AC_FUNC_FORK AC_FUNC_MMAP AC_FUNC_STRERROR_R AC_CHECK_FUNCS([alarm clock_gettime ftruncate gettimeofday \ localtime localtime_r memset munmap socket \ strchr strrchr strdup strstr strcasecmp \ poll epoll_create epoll_create1 kqueue \ random rand getrlimit sysconf \ pthread_spin_lock pthread_setschedparam \ pthread_mutexattr_setpshared \ pthread_condattr_setpshared \ sem_timedwait semtimedop \ sched_get_priority_max sched_setscheduler \ getpeerucred getpeereid]) AM_CONDITIONAL(HAVE_SEM_TIMEDWAIT, [test "x$ac_cv_func_sem_timedwait" = xyes]) AM_CONDITIONAL(HAVE_EPOLL, [test "x$ac_cv_func_epoll_create" = xyes]) AM_CONDITIONAL(HAVE_POLL, [test "x$ac_cv_func_poll" = xyes]) AM_CONDITIONAL(HAVE_KQUEUE, [test "x$ac_cv_func_kqueue" = xyes]) AC_CONFIG_LIBOBJ_DIR(lib) AC_REPLACE_FUNCS(strlcpy strlcat strchrnul) ## local defines PACKAGE_FEATURES="" if test x$ac_cv_func_epoll_create = xyes; then PACKAGE_FEATURES="$PACKAGE_FEATURES epoll" fi nongcc_memory_barrier_needed=no arch_force_shmlba=no AC_MSG_CHECKING([for architecture in ${host_cpu}]) case $host_cpu in sparc*) AC_MSG_RESULT([sparc]) AC_DEFINE_UNQUOTED([QB_ARCH_SPARC], [1], [sparc]) nongcc_memory_barrier_needed=yes arch_force_shmlba=yes ;; alpha*) AC_MSG_RESULT([alpha]) AC_DEFINE_UNQUOTED([QB_ARCH_ALPHA], [1], [alpha]) nongcc_memory_barrier_needed=yes ;; powerpc*) AC_MSG_RESULT([powerpc]) AC_DEFINE_UNQUOTED([QB_ARCH_POWERPC], [1], [powerpc]) ac_cv_link_attribute_section=no nongcc_memory_barrier_needed=yes arch_force_shmlba=yes ;; ia64) AC_MSG_RESULT([ia64]) AC_DEFINE_UNQUOTED([QB_ARCH_IA64], [1], [ia64]) nongcc_memory_barrier_needed=yes ;; arm*) AC_MSG_RESULT([arm]) AC_DEFINE_UNQUOTED([QB_ARCH_ARM], [1], [arm]) arch_force_shmlba=yes ;; hppa*) AC_MSG_RESULT([hppa]) AC_DEFINE_UNQUOTED([QB_ARCH_HPPA], [1], [hppa]) ;; mips*) AC_MSG_RESULT([ia64]) AC_DEFINE_UNQUOTED([QB_ARCH_MIPS], [1], [mips]) arch_force_shmlba=yes ;; *) AC_MSG_RESULT([${host_cpu}]) ;; esac if test $arch_force_shmlba = yes; then AC_DEFINE_UNQUOTED([QB_FORCE_SHM_ALIGN], [1], [shared and fixed mmap must align on 16k]) fi # OS detection # THIS SECTION MUST DIE! CP=cp AC_MSG_CHECKING([for os in ${host_os}]) case "$host_os" in *linux*) AC_DEFINE_UNQUOTED([QB_LINUX], [1], [Compiling for Linux platform]) AC_MSG_RESULT([Linux]) ;; *cygwin*) AC_DEFINE_UNQUOTED([QB_CYGWIN], [1], [Compiling for Cygwin platform]) ac_cv_link_attribute_section=no nongcc_memory_barrier_needed=yes gcc_has_builtin_sync_operations=no AC_MSG_RESULT([Cygwin]) ;; darwin*) AC_DEFINE_UNQUOTED([QB_DARWIN], [1], [Compiling for Darwin platform]) CP=rsync ac_cv_link_attribute_section=no dnl Attribute section appears to work here but fails later with: dnl cc1: error in backend: Global variable 'descriptor.4902' dnl has an invalid section specifier '__verbose': mach-o dnl section specifier requires a segment and section dnl separated by a comma AC_DEFINE_UNQUOTED([DISABLE_POSIX_THREAD_PROCESS_SHARED], [1], [Disable _POSIX_THREAD_PROCESS_SHARED]) AC_MSG_RESULT([Darwin]) ;; *bsd*) AC_DEFINE_UNQUOTED([QB_BSD], [1], [Compiling for BSD platform]) case "$host_os" in *netbsd*) # this is because dlopen within a dl_iterate_phdr # callback locks up. ac_cv_link_attribute_section=no AC_DEFINE_UNQUOTED([UNIX_PATH_MAX], [103], [Unix path length]) ;; *openbsd*) AC_DEFINE_UNQUOTED([UNIX_PATH_MAX], [104], [Unix path length]) ;; esac AC_MSG_RESULT([BSD]) ;; *solaris*) ac_cv_link_attribute_section=no AC_DEFINE_UNQUOTED(DISABLE_IPC_SHM, 1, [Disable shared mem ipc]) AC_DEFINE_UNQUOTED([QB_SOLARIS], [1], [Compiling for Solaris platform]) CP=rsync AC_MSG_RESULT([Solaris]) ;; + *gnu*) + AC_DEFINE_UNQUOTED([QB_GNU], [1], + [Compiling for GNU/Hurd platform]) + AC_MSG_RESULT([GNU]) + ;; *) AC_MSG_ERROR([Unsupported OS? hmmmm]) ;; esac AC_MSG_CHECKING([whether GCC supports builtin sync intrinsics]) if test -z "$gcc_has_builtin_sync_operations"; then gcc_has_builtin_sync_operations=no if test x"$GCC" = xyes && test x$have_mingw != xyes; then AC_TRY_LINK([], [int i; __sync_synchronize (); __sync_bool_compare_and_swap (&i, 0, 1); __sync_fetch_and_add (&i, 1); ], [gcc_has_builtin_sync_operations=yes], [gcc_has_builtin_sync_operations=no]) fi fi AC_MSG_RESULT($gcc_has_builtin_sync_operations) AM_CONDITIONAL(HAVE_GCC_BUILTINS_FOR_SYNC_OPERATIONS, [test "x$gcc_has_builtin_sync_operations" = xyes]) if test "x$gcc_has_builtin_sync_operations" = xyes; then AC_DEFINE_UNQUOTED(HAVE_GCC_BUILTINS_FOR_SYNC_OPERATIONS, 1, [have builtin sync operations]) fi # __atomic_XXX AC_MSG_CHECKING([whether GCC supports builtin atomic intrinsics]) if test -z "$gcc_has_builtin_atomic_operations"; then gcc_has_builtin_atomic_operations=no if test x"$GCC" = xyes && test x$have_mingw != xyes; then AC_TRY_LINK([], [int i; __atomic_load_n(&i, __ATOMIC_ACQUIRE); __atomic_exchange_n(&i, 0, __ATOMIC_RELEASE); ], [gcc_has_builtin_atomic_operations=yes], [gcc_has_builtin_atomic_operations=no]) fi fi AC_MSG_RESULT($gcc_has_builtin_atomic_operations) AM_CONDITIONAL(HAVE_GCC_BUILTINS_FOR_ATOMIC_OPERATIONS, [test "x$gcc_has_builtin_atomic_operations" = xyes]) if test "x$gcc_has_builtin_atomic_operations" = xyes; then AC_DEFINE_UNQUOTED(HAVE_GCC_BUILTINS_FOR_ATOMIC_OPERATIONS, 1, [have builtin atomic operations]) fi AC_MSG_CHECKING([whether atomics need memory barrier]) if test -n "$ac_cv_atomic_need_memory_barrier"; then memory_barrier_needed=$ac_cv_atomic_need_memory_barrier else if test x$gcc_has_builtin_sync_operations = xyes; then memory_barrier_needed=yes PACKAGE_FEATURES="$PACKAGE_FEATURES gcc__sync" else memory_barrier_needed=$nongcc_memory_barrier_needed AC_MSG_WARN([-----------------------------]) AC_MSG_WARN([You have gcc but not __sync_bool_compare_and_swap]) AC_MSG_WARN([try CFLAGS="-march= -mtune=native" ./configure]) AC_MSG_WARN([-----------------------------]) fi fi AC_MSG_RESULT($memory_barrier_needed) if test x"$memory_barrier_needed" != xno; then AC_DEFINE_UNQUOTED(QB_ATOMIC_OP_MEMORY_BARRIER_NEEDED, 1, [need atomic memory barrier]) fi LINT_FLAGS="-syntax -weak -unrecog +posixlib +ignoresigns -fcnuse \ -badflag -D__gnuc_va_list=va_list -D__attribute\(x\)= \ -warnposix +matchanyintegral -sysunrecog" # local options AC_ARG_ENABLE([ansi], [ --enable-ansi : force to build with ANSI standards. ], [ default="no" ]) AC_ARG_ENABLE([fatal-warnings], [ --enable-fatal-warnings : enable fatal warnings. ], [ default="no" ]) AC_ARG_ENABLE([debug], [ --enable-debug : enable debug build. ], [ default="no" ]) AC_ARG_ENABLE([coverage], [ --enable-coverage : coverage analysis of the codebase. ], [ default="no" ]) AC_ARG_ENABLE([slow-tests], [ --enable-slow-tests : build and run slow tests. ], [ default="no" ]) if test x"$with_check" = xyes; then AC_ARG_ENABLE([syslog-tests], [ --enable-syslog-tests : build and run syslog tests. ], [ default="no" ]) fi AC_ARG_WITH([socket-dir], [ --with-socket-dir=DIR : socket dir. ], [ SOCKETDIR="$withval" ], [ SOCKETDIR="$localstatedir/run" ]) AC_SUBST(CP) # *FLAGS handling goes here ENV_CFLAGS="$CFLAGS" ENV_CPPFLAGS="$CPPFLAGS" ENV_LDFLAGS="$LDFLAGS" # debug build stuff if test "x${enable_debug}" = xyes; then AC_DEFINE_UNQUOTED([DEBUG], [1], [Compiling Debugging code]) OPT_CFLAGS="-O0" if test "x${GCC}" = xyes; then GDB_FLAGS="-ggdb3" else GDB_FLAGS="-g" fi PACKAGE_FEATURES="$PACKAGE_FEATURES debug" fi # extra warnings EXTRA_WARNINGS="" WARNLIST=" all shadow missing-prototypes missing-declarations strict-prototypes declaration-after-statement pointer-arith write-strings cast-align bad-function-cast missing-format-attribute format=2 format-security no-format-nonliteral no-long-long unsigned-char gnu89-inline no-strict-aliasing " for j in $WARNLIST; do if cc_supports_flag -W$j; then EXTRA_WARNINGS="$EXTRA_WARNINGS -W$j"; fi done # warnings suppression gcc_format_complaints=no if test x"$GCC" = xyes && cc_supports_flag -Wmissing-format-attribute; then gcc_format_complaints=yes AC_DEFINE([HAVE_GCC_MISSING_FORMAT_ATTRIBUTE], [], [gcc supports -Wmissing-format-attribute]) fi if test x"$GCC" = xyes && cc_supports_flag -Wsuggest-attribute=format; then gcc_format_complaints=yes AC_DEFINE([HAVE_GCC_SUGGEST_ATTRIBUTE_FORMAT], [], [gcc supports -Wsuggest-attribute=format]) fi dnl pretend GCC (<4.6) is not capable of format complaints when it does not dnl support diagnostic push/pop pragmas (cannot track state reliably, then) if test x"$gcc_format_complaints" = xyes; then backup_CFLAGS="$CFLAGS" CFLAGS="$CFLAGS -Werror" AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ #pragma GCC diagnostic push #pragma GCC diagnostic pop ]])], AC_DEFINE([HAVE_GCC_FORMAT_COMPLAINTS], [], [gcc can complain about missing format attribute])) CFLAGS="$backup_CFLAGS" fi # --- coverage --- if test "x${enable_coverage}" = xyes && \ cc_supports_flag -ftest-coverage && \ cc_supports_flag -fprofile-arcs ; then AC_MSG_NOTICE([Enabling Coverage (enable -O0 by default)]) OPT_CFLAGS="-O0" COVERAGE_CFLAGS="-ftest-coverage -fprofile-arcs" COVERAGE_LDFLAGS="-ftest-coverage -fprofile-arcs" PACKAGE_FEATURES="$PACKAGE_FEATURES coverage" else COVERAGE_CFLAGS="" COVERAGE_LDFLAGS="" fi if test "x${enable_slow_tests}" = xyes ; then AC_DEFINE([HAVE_SLOW_TESTS], 1,[have slow tests]) AC_MSG_NOTICE([Enabling Slow tests]) fi AM_CONDITIONAL(HAVE_SLOW_TESTS, [test "x${enable_slow_tests}" = xyes]) AC_SUBST(HAVE_SLOW_TESTS) if test "x${enable_syslog_tests}" = xyes ; then AC_DEFINE([HAVE_SYSLOG_TESTS], 1,[have syslog tests]) AC_MSG_NOTICE([Enabling syslog tests]) fi AM_CONDITIONAL(HAVE_SYSLOG_TESTS, [test "x${enable_syslog_tests}" = xyes]) AC_SUBST(HAVE_SYSLOG_TESTS) # --- callsite sections --- if test "x${GCC}" = xyes; then AC_MSG_CHECKING([whether GCC supports __attribute__((section())]) if test "x${ac_cv_link_attribute_section}" = x ; then AC_TRY_LINK([], [static int my_var __attribute__((section("__verbose"))) = 5; if (my_var == 5) return 0; else return -1; ], [gcc_has_attribute_section=yes], [gcc_has_attribute_section=no]) else gcc_has_attribute_section=${ac_cv_link_attribute_section} fi AC_MSG_RESULT($gcc_has_attribute_section) if test $gcc_has_attribute_section = yes; then AC_DEFINE([QB_HAVE_ATTRIBUTE_SECTION], 1, [Enabling code using __attribute__((section))]) PACKAGE_FEATURES="$PACKAGE_FEATURES attribute-section" fi fi # --- ansi --- if test "x${enable_ansi}" = xyes && \ cc_supports_flag -std=iso9899:199409 ; then AC_MSG_NOTICE([Enabling ANSI Compatibility]) ANSI_CPPFLAGS="-ansi -D_GNU_SOURCE -DANSI_ONLY" PACKAGE_FEATURES="$PACKAGE_FEATURES ansi" else ANSI_CPPFLAGS="" fi # --- fatal warnings --- if test "x${enable_fatal_warnings}" = xyes && \ cc_supports_flag -Werror ; then AC_MSG_NOTICE([Enabling Fatal Warnings (-Werror)]) WERROR_CFLAGS="-Werror" PACKAGE_FEATURES="$PACKAGE_FEATURES fatal-warnings" else WERROR_CFLAGS="" fi # final build of *FLAGS CFLAGS="$ENV_CFLAGS $OPT_CFLAGS $GDB_FLAGS \ $COVERAGE_CFLAGS $EXTRA_WARNINGS $WERROR_CFLAGS" CPPFLAGS="$ENV_CPPFLAGS $ANSI_CPPFLAGS" LDFLAGS="$ENV_LDFLAGS $COVERAGE_LDFLAGS" if test -f /usr/share/dict/words ; then HAVE_DICT_WORDS=yes AC_DEFINE([HAVE_DICT_WORDS], 1, "Have /usr/share/dict/words") fi AM_CONDITIONAL([HAVE_DICT_WORDS], [test "x$HAVE_DICT_WORDS" = xyes]) # substitute what we need: AC_SUBST([SOCKETDIR]) AC_SUBST([LINT_FLAGS]) AC_DEFINE_UNQUOTED([SOCKETDIR], "$(eval echo ${SOCKETDIR})", [Socket directory]) AC_DEFINE_UNQUOTED([LOCALSTATEDIR], "$(eval echo ${localstatedir})", [localstate directory]) AC_DEFINE_UNQUOTED([PACKAGE_FEATURES], "${PACKAGE_FEATURES}", [quarterback built-in features]) AC_CONFIG_FILES([Makefile include/Makefile include/qb/Makefile lib/Makefile lib/libqb.pc tools/Makefile tests/Makefile tests/test.conf examples/Makefile docs/Makefile docs/man.dox docs/html.dox]) AC_OUTPUT AC_MSG_RESULT([]) AC_MSG_RESULT([$PACKAGE configuration:]) AC_MSG_RESULT([ Version = ${VERSION}]) AC_MSG_RESULT([ Prefix = ${prefix}]) AC_MSG_RESULT([ Executables = ${sbindir}]) AC_MSG_RESULT([ Man pages = ${mandir}]) AC_MSG_RESULT([ Doc dir = ${docdir}]) AC_MSG_RESULT([ Libraries = ${libdir}]) AC_MSG_RESULT([ Header files = ${includedir}]) AC_MSG_RESULT([ Arch-independent files = ${datadir}]) AC_MSG_RESULT([ State information = ${localstatedir}]) AC_MSG_RESULT([ System configuration = ${sysconfdir}]) AC_MSG_RESULT([ SOCKETDIR = ${SOCKETDIR}]) AC_MSG_RESULT([ Features =${PACKAGE_FEATURES}]) AC_MSG_RESULT([]) AC_MSG_RESULT([$PACKAGE build info:]) AC_MSG_RESULT([ Optimization = ${OPT_CFLAGS}]) AC_MSG_RESULT([ Debug options = ${GDB_CFLAGS}]) AC_MSG_RESULT([ Extra compiler warnings = ${EXTRA_WARNING}]) AC_MSG_RESULT([ Env. defined CFLAG = ${ENV_CFLAGS}]) AC_MSG_RESULT([ Env. defined CPPFLAGS = ${ENV_CPPFLAGS}]) AC_MSG_RESULT([ Env. defined LDFLAGS = ${ENV_LDFLAGS}]) AC_MSG_RESULT([ ANSI defined CPPFLAGS = ${ANSI_CPPFLAGS}]) AC_MSG_RESULT([ Coverage CFLAGS = ${COVERAGE_CFLAGS}]) AC_MSG_RESULT([ Coverage LDFLAGS = ${COVERAGE_LDFLAGS}]) AC_MSG_RESULT([ Fatal War. CFLAGS = ${WERROR_CFLAGS}]) AC_MSG_RESULT([ Final CFLAGS = ${CFLAGS}]) AC_MSG_RESULT([ Final CPPFLAGS = ${CPPFLAGS}]) AC_MSG_RESULT([ Final LDFLAGS = ${LDFLAGS}]) diff --git a/lib/log_thread.c b/lib/log_thread.c index 56008f8..930fb33 100644 --- a/lib/log_thread.c +++ b/lib/log_thread.c @@ -1,279 +1,283 @@ /* * Copyright (C) 2011 Red Hat, Inc. * * All rights reserved. * * Author: Angus Salkeld * * libqb is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * libqb is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with libqb. If not, see . */ #include "os_base.h" #include #include #include #include #include #include "log_int.h" static int wthread_active = QB_FALSE; static int wthread_should_exit = QB_FALSE; static qb_thread_lock_t *logt_wthread_lock = NULL; static QB_LIST_DECLARE(logt_print_finished_records); static int logt_memory_used = 0; static int logt_dropped_messages = 0; static sem_t logt_thread_start; static sem_t logt_print_finished; static int logt_sched_param_queued = QB_FALSE; static int logt_sched_policy; #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX) static struct sched_param logt_sched_param; #endif /* HAVE_PTHREAD_SETSCHEDPARAM && HAVE_SCHED_GET_PRIORITY_MAX */ static pthread_t logt_thread_id = 0; static void *qb_logt_worker_thread(void *data) __attribute__ ((noreturn)); static void * qb_logt_worker_thread(void *data) { struct qb_log_record *rec; int dropped = 0; int res; /* * Signal wthread_create that the initialization process may continue */ sem_post(&logt_thread_start); for (;;) { retry_sem_wait: res = sem_wait(&logt_print_finished); if (res == -1 && errno == EINTR) { goto retry_sem_wait; } else if (res == -1) { /* * This case shouldn't happen */ pthread_exit(NULL); } (void)qb_thread_lock(logt_wthread_lock); if (wthread_should_exit) { int value = -1; (void)sem_getvalue(&logt_print_finished, &value); if (value == 0) { (void)qb_thread_unlock(logt_wthread_lock); pthread_exit(NULL); } } rec = qb_list_first_entry(&logt_print_finished_records, struct qb_log_record, list); qb_list_del(&rec->list); logt_memory_used = logt_memory_used - strlen(rec->buffer) - sizeof(struct qb_log_record) - 1; dropped = logt_dropped_messages; logt_dropped_messages = 0; (void)qb_thread_unlock(logt_wthread_lock); if (dropped) { printf("%d messages lost\n", dropped); } qb_log_thread_log_write(rec->cs, rec->timestamp, rec->buffer); free(rec->buffer); free(rec); } } int32_t qb_log_thread_priority_set(int32_t policy, int32_t priority) { int res = 0; #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX) logt_sched_policy = policy; if (policy == SCHED_OTHER #ifdef SCHED_IDLE || policy == SCHED_IDLE #endif #if defined(SCHED_BATCH) && !defined(QB_DARWIN) || policy == SCHED_BATCH #endif ) { logt_sched_param.sched_priority = 0; } else { logt_sched_param.sched_priority = priority; } if (wthread_active == QB_FALSE) { logt_sched_param_queued = QB_TRUE; } else { res = pthread_setschedparam(logt_thread_id, policy, &logt_sched_param); if (res != 0) { res = -res; } } #endif return res; } int32_t qb_log_thread_start(void) { int res; if (wthread_active) { return 0; } wthread_active = QB_TRUE; sem_init(&logt_thread_start, 0, 0); sem_init(&logt_print_finished, 0, 0); res = pthread_create(&logt_thread_id, NULL, qb_logt_worker_thread, NULL); if (res != 0) { wthread_active = QB_FALSE; return -res; } sem_wait(&logt_thread_start); if (logt_sched_param_queued) { res = qb_log_thread_priority_set(logt_sched_policy, +#if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX) logt_sched_param.sched_priority); +#else + 0); +#endif if (res != 0) { goto cleanup_pthread; } logt_sched_param_queued = QB_FALSE; } logt_wthread_lock = qb_thread_lock_create(QB_THREAD_LOCK_SHORT); if (logt_wthread_lock == NULL) { goto cleanup_pthread; } return 0; cleanup_pthread: wthread_should_exit = QB_TRUE; sem_post(&logt_print_finished); pthread_join(logt_thread_id, NULL); sem_destroy(&logt_print_finished); sem_destroy(&logt_thread_start); return res; } void qb_log_thread_log_post(struct qb_log_callsite *cs, time_t timestamp, const char *buffer) { struct qb_log_record *rec; size_t buf_size; size_t total_size; rec = malloc(sizeof(struct qb_log_record)); if (rec == NULL) { return; } buf_size = strlen(buffer) + 1; total_size = sizeof(struct qb_log_record) + buf_size; rec->cs = cs; rec->buffer = malloc(buf_size); if (rec->buffer == NULL) { goto free_record; } memcpy(rec->buffer, buffer, buf_size); rec->timestamp = timestamp; qb_list_init(&rec->list); (void)qb_thread_lock(logt_wthread_lock); logt_memory_used += total_size; if (logt_memory_used > 512000) { free(rec->buffer); free(rec); logt_memory_used = logt_memory_used - total_size; logt_dropped_messages += 1; (void)qb_thread_unlock(logt_wthread_lock); return; } else { qb_list_add_tail(&rec->list, &logt_print_finished_records); } (void)qb_thread_unlock(logt_wthread_lock); sem_post(&logt_print_finished); return; free_record: free(rec); } void qb_log_thread_stop(void) { int res; int value; struct qb_log_record *rec; if (wthread_active == QB_FALSE && logt_wthread_lock == NULL) { return; } if (wthread_active == QB_FALSE) { for (;;) { res = sem_getvalue(&logt_print_finished, &value); if (res != 0 || value == 0) { break; } sem_wait(&logt_print_finished); (void)qb_thread_lock(logt_wthread_lock); rec = qb_list_first_entry(&logt_print_finished_records, struct qb_log_record, list); qb_list_del(&rec->list); logt_memory_used = logt_memory_used - strlen(rec->buffer) - sizeof(struct qb_log_record) - 1; (void)qb_thread_unlock(logt_wthread_lock); qb_log_thread_log_write(rec->cs, rec->timestamp, rec->buffer); free(rec->buffer); free(rec); } } else { wthread_should_exit = QB_TRUE; sem_post(&logt_print_finished); pthread_join(logt_thread_id, NULL); } (void)qb_thread_lock_destroy(logt_wthread_lock); sem_destroy(&logt_print_finished); sem_destroy(&logt_thread_start); } diff --git a/lib/util.c b/lib/util.c index ef5ba25..6181a25 100644 --- a/lib/util.c +++ b/lib/util.c @@ -1,369 +1,374 @@ /* * Copyright (C) 2010 Red Hat, Inc. * * All rights reserved. * * Author: Angus Salkeld * * libqb is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * libqb is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with libqb. If not, see . */ #include "os_base.h" #include "util_int.h" #include #include #include #include struct qb_thread_lock_s { qb_thread_lock_type_t type; #ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK pthread_spinlock_t spinlock; #endif /* HAVE_PTHREAD_SHARED_SPIN_LOCK */ pthread_mutex_t mutex; }; qb_thread_lock_t * qb_thread_lock_create(qb_thread_lock_type_t type) { struct qb_thread_lock_s *tl = malloc(sizeof(struct qb_thread_lock_s)); int32_t res; if (tl == NULL) { return NULL; } #ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK if (type == QB_THREAD_LOCK_SHORT) { tl->type = QB_THREAD_LOCK_SHORT; res = pthread_spin_init(&tl->spinlock, 1); } else #endif /* HAVE_PTHREAD_SHARED_SPIN_LOCK */ { tl->type = QB_THREAD_LOCK_LONG; res = pthread_mutex_init(&tl->mutex, NULL); } if (res == 0) { return tl; } else { free(tl); return NULL; } } int32_t qb_thread_lock(qb_thread_lock_t * tl) { int32_t res; #ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK if (tl->type == QB_THREAD_LOCK_SHORT) { res = -pthread_spin_lock(&tl->spinlock); } else #endif /* HAVE_PTHREAD_SHARED_SPIN_LOCK */ { res = -pthread_mutex_lock(&tl->mutex); } return res; } int32_t qb_thread_unlock(qb_thread_lock_t * tl) { int32_t res; #ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK if (tl->type == QB_THREAD_LOCK_SHORT) { res = -pthread_spin_unlock(&tl->spinlock); } else #endif { res = -pthread_mutex_unlock(&tl->mutex); } return res; } int32_t qb_thread_trylock(qb_thread_lock_t * tl) { int32_t res; #ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK if (tl->type == QB_THREAD_LOCK_SHORT) { res = -pthread_spin_trylock(&tl->spinlock); } else #endif { res = -pthread_mutex_trylock(&tl->mutex); } return res; } int32_t qb_thread_lock_destroy(qb_thread_lock_t * tl) { int32_t res; #ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK if (tl->type == QB_THREAD_LOCK_SHORT) { res = -pthread_spin_destroy(&tl->spinlock); } else #endif { res = -pthread_mutex_destroy(&tl->mutex); } free(tl); return res; } void qb_timespec_add_ms(struct timespec *ts, int32_t ms) { #ifndef S_SPLINT_S ts->tv_sec += ms / 1000; ts->tv_nsec += (ms % 1000) * QB_TIME_NS_IN_MSEC; if (ts->tv_nsec >= 1000000000L) { ts->tv_sec++; ts->tv_nsec = ts->tv_nsec - 1000000000L; } #endif /* S_SPLINT_S */ } #ifdef HAVE_MONOTONIC_CLOCK uint64_t qb_util_nano_current_get(void) { uint64_t nano_monotonic; struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); nano_monotonic = (ts.tv_sec * QB_TIME_NS_IN_SEC) + (uint64_t) ts.tv_nsec; return (nano_monotonic); } uint64_t qb_util_nano_from_epoch_get(void) { uint64_t nano_monotonic; struct timespec ts; #ifdef CLOCK_REALTIME_COARSE clock_gettime(CLOCK_REALTIME_COARSE, &ts); #else clock_gettime(CLOCK_REALTIME, &ts); #endif nano_monotonic = (ts.tv_sec * QB_TIME_NS_IN_SEC) + (uint64_t) ts.tv_nsec; return (nano_monotonic); } uint64_t qb_util_nano_monotonic_hz(void) { uint64_t nano_monotonic_hz; struct timespec ts; +#if HAVE_CLOCK_GETRES_MONOTONIC clock_getres(CLOCK_MONOTONIC, &ts); +#else + if (clock_getres(CLOCK_REALTIME, &ts) != 0) + qb_util_perror(LOG_ERR,"CLOCK_REALTIME"); +#endif nano_monotonic_hz = QB_TIME_NS_IN_SEC / ((ts.tv_sec * QB_TIME_NS_IN_SEC) + ts.tv_nsec); return (nano_monotonic_hz); } void qb_util_timespec_from_epoch_get(struct timespec *ts) { #ifdef CLOCK_REALTIME_COARSE clock_gettime(CLOCK_REALTIME_COARSE, ts); #else clock_gettime(CLOCK_REALTIME, ts); #endif } #else uint64_t qb_util_nano_current_get(void) { return qb_util_nano_from_epoch_get(); } uint64_t qb_util_nano_monotonic_hz(void) { return sysconf(_SC_CLK_TCK); } void qb_util_timespec_from_epoch_get(struct timespec *ts) { struct timeval time_from_epoch; gettimeofday(&time_from_epoch, 0); #ifndef S_SPLINT_S ts->tv_sec = time_from_epoch.tv_sec; ts->tv_nsec = time_from_epoch.tv_usec * QB_TIME_NS_IN_USEC; #endif /* S_SPLINT_S */ } uint64_t qb_util_nano_from_epoch_get(void) { uint64_t nano_from_epoch; struct timeval time_from_epoch; gettimeofday(&time_from_epoch, 0); nano_from_epoch = ((time_from_epoch.tv_sec * QB_TIME_NS_IN_SEC) + (time_from_epoch.tv_usec * QB_TIME_NS_IN_USEC)); return (nano_from_epoch); } #endif /* HAVE_MONOTONIC_CLOCK */ struct qb_util_stopwatch { uint64_t started; uint64_t stopped; uint32_t split_options; uint32_t split_size; uint32_t split_entries; uint64_t *split_entry_list; }; qb_util_stopwatch_t * qb_util_stopwatch_create(void) { struct qb_util_stopwatch *sw; sw = (struct qb_util_stopwatch *)calloc(1, sizeof(struct qb_util_stopwatch)); return sw; } void qb_util_stopwatch_free(qb_util_stopwatch_t * sw) { free(sw->split_entry_list); free(sw); } void qb_util_stopwatch_start(qb_util_stopwatch_t * sw) { sw->started = qb_util_nano_current_get(); sw->stopped = 0; sw->split_entries = 0; } void qb_util_stopwatch_stop(qb_util_stopwatch_t * sw) { sw->stopped = qb_util_nano_current_get(); } uint64_t qb_util_stopwatch_us_elapsed_get(qb_util_stopwatch_t * sw) { if (sw->stopped == 0 || sw->started == 0) { return 0; } return ((sw->stopped - sw->started) / QB_TIME_NS_IN_USEC); } float qb_util_stopwatch_sec_elapsed_get(qb_util_stopwatch_t * sw) { uint64_t e6; if (sw->stopped == 0 || sw->started == 0) { return 0; } e6 = qb_util_stopwatch_us_elapsed_get(sw); return ((float)e6 / (float)QB_TIME_US_IN_SEC); } int32_t qb_util_stopwatch_split_ctl(qb_util_stopwatch_t *sw, uint32_t max_splits, uint32_t options) { sw->split_size = max_splits; sw->split_options = options; sw->split_entry_list = (uint64_t *)calloc(1, sizeof (uint64_t) * max_splits); if (sw->split_entry_list == NULL) { return -errno; } return 0; } uint64_t qb_util_stopwatch_split(qb_util_stopwatch_t *sw) { uint32_t new_entry_pos; uint64_t time_start; uint64_t time_end; if (sw->split_size == 0) { return 0; } if (!(sw->split_options & QB_UTIL_SW_OVERWRITE) && sw->split_entries == sw->split_size) { return 0; } if (sw->started == 0) { qb_util_stopwatch_start(sw); } new_entry_pos = sw->split_entries % (sw->split_size); sw->split_entry_list[new_entry_pos] = qb_util_nano_current_get(); sw->split_entries++; time_start = sw->split_entry_list[new_entry_pos]; if (sw->split_entries == 1) { /* first entry */ time_end = sw->started; } else if (new_entry_pos == 0) { /* wrap around */ time_end = sw->split_entry_list[sw->split_size - 1]; } else { time_end = sw->split_entry_list[(new_entry_pos - 1) % sw->split_size]; } return (time_start - time_end) / QB_TIME_NS_IN_USEC; } uint32_t qb_util_stopwatch_split_last(qb_util_stopwatch_t *sw) { if (sw->split_entries) { return sw->split_entries - 1; } return sw->split_entries; } uint64_t qb_util_stopwatch_time_split_get(qb_util_stopwatch_t *sw, uint32_t receint, uint32_t older) { uint64_t time_start; uint64_t time_end; if (sw->started == 0 || receint >= sw->split_entries || older >= sw->split_entries || receint < older) { return 0; } if (sw->split_options & QB_UTIL_SW_OVERWRITE && (receint < (sw->split_entries - sw->split_size) || older < (sw->split_entries - sw->split_size))) { return 0; } time_start = sw->split_entry_list[receint % (sw->split_size)]; if (older == receint) { time_end = sw->started; } else { time_end = sw->split_entry_list[older % (sw->split_size)]; } return (time_start - time_end) / QB_TIME_NS_IN_USEC; }