stats.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
/*
* Copyright (C) 2009, 2012-2016 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>
/*%
* Local macro confirming prescence of 64-bit
* increment and store operations, just to make
* the later macros simpler
*/
#if defined(ISC_PLATFORM_HAVEXADDQ) && defined(ISC_PLATFORM_HAVEATOMICSTOREQ)
#define ISC_STATS_HAVEATOMICQ 1
#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)
#define ISC_STATS_LOCKCOUNTERS 1
#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_HAVEXADD)
#define ISC_STATS_USEMULTIFIELDS 1
#else
#define ISC_STATS_USEMULTIFIELDS 0
#endif
typedef struct {
} isc_stat_t;
#else
typedef isc_uint64_t isc_stat_t;
#endif
struct isc_stats {
/*% Unlocked */
unsigned int magic;
int ncounters;
unsigned int references; /* locked by lock */
/*%
* 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 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.
*/
#else
#endif
#endif
}
static inline void
#endif
if (prev == 0)
-1);
#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] =
/* use xaddq(..., 0) as an atomic load */
stats->copiedcounters[i] =
#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
#else
#endif
#endif
}