/*
* Copyright (C) 2009, 2012-2017 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/* $Id$ */
/*! \file */
#include <config.h>
#include <string.h>
#include <isc/platform.h>
#if defined(ISC_PLATFORM_HAVESTDATOMIC)
#include <stdatomic.h>
#endif
/*%
* Local macro confirming prescence of 64-bit
* increment and store operations, just to make
* the later macros simpler
*/
#if (defined(ISC_PLATFORM_HAVESTDATOMIC) && defined(ATOMIC_LONG_LOCK_FREE)) || \
(defined(ISC_PLATFORM_HAVEXADDQ) && defined(ISC_PLATFORM_HAVEATOMICSTOREQ))
#if (defined(ISC_PLATFORM_HAVESTDATOMIC) && defined(ATOMIC_LONG_LOCK_FREE))
#endif
#else
#define ISC_STATS_HAVEATOMICQ 0
#endif
/*%
* Only lock the counters if 64-bit atomic operations are
* not available but cheap atomic lock operations are.
* On a modern 64-bit system this should never be the case.
*
* Normal locks are too expensive to be used whenever a counter
* is updated.
*/
#if !ISC_STATS_HAVEATOMICQ && defined(ISC_RWLOCK_HAVEATOMIC)
#else
#define ISC_STATS_LOCKCOUNTERS 0
#endif
/*%
* If 64-bit atomic operations are not available but
* 32-bit operations are then split the counter into two,
* using the atomic operations to try to ensure that any carry
* from the low word is correctly carried into the high word.
*
* Otherwise, just rely on standard 64-bit data types
* and operations
*/
#if !ISC_STATS_HAVEATOMICQ && ((defined(ISC_PLATFORM_HAVESTDATOMIC) && defined(ATOMIC_INT_LOCK_FREE)) || defined(ISC_PLATFORM_HAVEXADD))
#if (defined(ISC_PLATFORM_HAVESTDATOMIC) && defined(ATOMIC_INT_LOCK_FREE))
#endif
#else
#define ISC_STATS_USEMULTIFIELDS 0
#endif
typedef struct {
#if defined(ISC_STATS_HAVESTDATOMIC)
#else
#endif
} isc_stat_t;
#else
#if defined(ISC_STATS_HAVESTDATOMICQ)
#else
#endif
#endif
struct isc_stats {
/*% Unlocked */
unsigned int magic;
int ncounters;
/*%
* Locked by counterlock or unlocked if efficient rwlock is not
* available.
*/
#endif
/*%
* We don't want to lock the counters while we are dumping, so we first
* copy the current counter values into a local array. This buffer
* will be used as the copy destination. It's allocated on creation
* of the stats structure so that the dump operation won't fail due
* to memory allocation failure.
* XXX: this approach is weird for non-threaded build because the
* additional memory and the copy overhead could be avoided. We prefer
* simplicity here, however, under the assumption that this function
* should be only rarely called.
*/
};
static isc_result_t
return (ISC_R_NOMEMORY);
if (result != ISC_R_SUCCESS)
goto clean_stats;
goto clean_mutex;
}
sizeof(isc_uint64_t) * ncounters);
goto clean_counters;
}
if (result != ISC_R_SUCCESS)
goto clean_copiedcounters;
#endif
return (result);
sizeof(isc_stat_t) * ncounters);
#endif
return (result);
}
void
stats->references++;
}
void
stats->references--;
if (stats->references == 0) {
#endif
return;
}
}
int
}
static inline void
/*
* We use a "read" lock to prevent other threads from reading the
* counter while we "writing" a counter field. The write access itself
* is protected by the atomic operation.
*/
#endif
#if defined(ISC_STATS_HAVESTDATOMIC)
#else
#endif
/*
* If the lower 32-bit field overflows, increment the higher field.
* Note that it's *theoretically* possible that the lower field
* overlaps again before the higher field is incremented. It doesn't
* matter, however, because we don't read the value until
* isc_stats_copy() is called where the whole process is protected
* by the write (exclusive) lock.
*/
#if defined(ISC_STATS_HAVESTDATOMIC)
#else
#endif
}
#if defined(ISC_STATS_HAVESTDATOMICQ)
#else
#endif
#else
#endif
#endif
}
static inline void
#endif
#if defined(ISC_STATS_HAVESTDATOMIC)
#else
#endif
if (prev == 0) {
#if defined(ISC_STATS_HAVESTDATOMIC)
#else
-1);
#endif
}
#if defined(ISC_STATS_HAVESTDATOMICQ)
#else
#endif
#else
#endif
#endif
}
static void
int i;
/*
* We use a "write" lock before "reading" the statistics counters as
* an exclusive lock.
*/
#endif
stats->copiedcounters[i] =
#if defined(ISC_STATS_HAVESTDATOMICQ)
stats->copiedcounters[i] =
#else
/* use xaddq(..., 0) as an atomic load */
stats->copiedcounters[i] =
#endif
#else
#endif
}
#endif
}
}
void
}
void
}
void
{
int i;
if ((options & ISC_STATSDUMP_VERBOSE) == 0 &&
stats->copiedcounters[i] == 0)
continue;
}
}
void
{
/*
* We use a "write" lock before "reading" the statistics counters as
* an exclusive lock.
*/
#endif
#if defined(ISC_STATS_HAVESTDATOMICQ)
#else
#endif
#else
#endif
#endif
}