003 File Manager
Current Path:
/usr/src/contrib/ntp/lib/isc
usr
/
src
/
contrib
/
ntp
/
lib
/
isc
/
📁
..
📄
Atffile
(90 B)
📁
alpha
📄
api
(129 B)
📄
app_api.c
(3.17 KB)
📄
assertions.c
(3.4 KB)
📄
backtrace-emptytbl.c
(1.33 KB)
📄
backtrace.c
(7.53 KB)
📄
base32.c
(9.64 KB)
📄
base64.c
(6.54 KB)
📄
bitstring.c
(3.41 KB)
📄
buffer.c
(10.11 KB)
📄
bufferlist.c
(1.71 KB)
📄
commandline.c
(6.84 KB)
📄
entropy.c
(29.08 KB)
📄
error.c
(2.91 KB)
📄
event.c
(2.47 KB)
📄
fsaccess.c
(2.7 KB)
📄
hash.c
(10.75 KB)
📄
heap.c
(6.47 KB)
📄
hex.c
(4.78 KB)
📄
hmacmd5.c
(3.77 KB)
📄
hmacsha.c
(15.6 KB)
📄
httpd.c
(26.31 KB)
📁
ia64
📁
include
📄
inet_aton.c
(6.4 KB)
📄
inet_ntop.c
(5.29 KB)
📄
inet_pton.c
(5.35 KB)
📄
iterated_hash.c
(1.44 KB)
📄
lex.c
(22.34 KB)
📄
lfsr.c
(3.6 KB)
📄
lib.c
(2.67 KB)
📄
log.c
(44.84 KB)
📄
md5.c
(8.57 KB)
📄
mem.c
(58.91 KB)
📄
mem_api.c
(6.58 KB)
📁
mips
📄
mutexblock.c
(1.54 KB)
📄
netaddr.c
(9.91 KB)
📄
netscope.c
(2.31 KB)
📁
nls
📁
noatomic
📁
nothreads
📄
ondestroy.c
(2.16 KB)
📄
parseint.c
(2.04 KB)
📄
portset.c
(3.43 KB)
📁
powerpc
📄
print.c
(12.16 KB)
📁
pthreads
📄
quota.c
(2.37 KB)
📄
radix.c
(16.77 KB)
📄
random.c
(2.61 KB)
📄
ratelimiter.c
(8.15 KB)
📄
refcount.c
(1.21 KB)
📄
region.c
(1.39 KB)
📄
result.c
(5.81 KB)
📄
rwlock.c
(22.19 KB)
📄
serial.c
(1.73 KB)
📄
sha1.c
(10.41 KB)
📄
sha2.c
(40.33 KB)
📄
sockaddr.c
(13.42 KB)
📄
socket_api.c
(5.57 KB)
📁
sparc64
📄
stats.c
(8.26 KB)
📄
string.c
(5.33 KB)
📄
strtoul.c
(4.23 KB)
📄
symtab.c
(7.21 KB)
📄
task.c
(45.62 KB)
📄
task_api.c
(5.62 KB)
📄
task_p.h
(1.27 KB)
📄
taskpool.c
(4.4 KB)
📁
tests
📄
timer.c
(26.04 KB)
📄
timer_api.c
(3.49 KB)
📄
timer_p.h
(1.13 KB)
📄
tsmemcmp.c
(2.04 KB)
📁
unix
📄
version.c
(1.11 KB)
📁
win32
📁
x86_32
📁
x86_64
Editing: stats.c
/* * Copyright (C) 2009, 2012 Internet Systems Consortium, Inc. ("ISC") * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ /* $Id$ */ /*! \file */ #include <config.h> #include <string.h> #include <isc/atomic.h> #include <isc/buffer.h> #include <isc/magic.h> #include <isc/mem.h> #include <isc/platform.h> #include <isc/print.h> #include <isc/rwlock.h> #include <isc/stats.h> #include <isc/util.h> #define ISC_STATS_MAGIC ISC_MAGIC('S', 't', 'a', 't') #define ISC_STATS_VALID(x) ISC_MAGIC_VALID(x, ISC_STATS_MAGIC) #ifndef ISC_STATS_USEMULTIFIELDS #if defined(ISC_RWLOCK_USEATOMIC) && defined(ISC_PLATFORM_HAVEXADD) && !defined(ISC_PLATFORM_HAVEXADDQ) #define ISC_STATS_USEMULTIFIELDS 1 #else #define ISC_STATS_USEMULTIFIELDS 0 #endif #endif /* ISC_STATS_USEMULTIFIELDS */ #if ISC_STATS_USEMULTIFIELDS typedef struct { isc_uint32_t hi; isc_uint32_t lo; } isc_stat_t; #else typedef isc_uint64_t isc_stat_t; #endif struct isc_stats { /*% Unlocked */ unsigned int magic; isc_mem_t *mctx; int ncounters; isc_mutex_t lock; unsigned int references; /* locked by lock */ /*% * Locked by counterlock or unlocked if efficient rwlock is not * available. */ #ifdef ISC_RWLOCK_USEATOMIC isc_rwlock_t counterlock; #endif isc_stat_t *counters; /*% * 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. */ isc_uint64_t *copiedcounters; }; static isc_result_t create_stats(isc_mem_t *mctx, int ncounters, isc_stats_t **statsp) { isc_stats_t *stats; isc_result_t result = ISC_R_SUCCESS; REQUIRE(statsp != NULL && *statsp == NULL); stats = isc_mem_get(mctx, sizeof(*stats)); if (stats == NULL) return (ISC_R_NOMEMORY); result = isc_mutex_init(&stats->lock); if (result != ISC_R_SUCCESS) goto clean_stats; stats->counters = isc_mem_get(mctx, sizeof(isc_stat_t) * ncounters); if (stats->counters == NULL) { result = ISC_R_NOMEMORY; goto clean_mutex; } stats->copiedcounters = isc_mem_get(mctx, sizeof(isc_uint64_t) * ncounters); if (stats->copiedcounters == NULL) { result = ISC_R_NOMEMORY; goto clean_counters; } #ifdef ISC_RWLOCK_USEATOMIC result = isc_rwlock_init(&stats->counterlock, 0, 0); if (result != ISC_R_SUCCESS) goto clean_copiedcounters; #endif stats->references = 1; memset(stats->counters, 0, sizeof(isc_stat_t) * ncounters); stats->mctx = NULL; isc_mem_attach(mctx, &stats->mctx); stats->ncounters = ncounters; stats->magic = ISC_STATS_MAGIC; *statsp = stats; return (result); clean_counters: isc_mem_put(mctx, stats->counters, sizeof(isc_stat_t) * ncounters); #ifdef ISC_RWLOCK_USEATOMIC clean_copiedcounters: isc_mem_put(mctx, stats->copiedcounters, sizeof(isc_stat_t) * ncounters); #endif clean_mutex: DESTROYLOCK(&stats->lock); clean_stats: isc_mem_put(mctx, stats, sizeof(*stats)); return (result); } void isc_stats_attach(isc_stats_t *stats, isc_stats_t **statsp) { REQUIRE(ISC_STATS_VALID(stats)); REQUIRE(statsp != NULL && *statsp == NULL); LOCK(&stats->lock); stats->references++; UNLOCK(&stats->lock); *statsp = stats; } void isc_stats_detach(isc_stats_t **statsp) { isc_stats_t *stats; REQUIRE(statsp != NULL && ISC_STATS_VALID(*statsp)); stats = *statsp; *statsp = NULL; LOCK(&stats->lock); stats->references--; UNLOCK(&stats->lock); if (stats->references == 0) { isc_mem_put(stats->mctx, stats->copiedcounters, sizeof(isc_stat_t) * stats->ncounters); isc_mem_put(stats->mctx, stats->counters, sizeof(isc_stat_t) * stats->ncounters); DESTROYLOCK(&stats->lock); #ifdef ISC_RWLOCK_USEATOMIC isc_rwlock_destroy(&stats->counterlock); #endif isc_mem_putanddetach(&stats->mctx, stats, sizeof(*stats)); } } int isc_stats_ncounters(isc_stats_t *stats) { REQUIRE(ISC_STATS_VALID(stats)); return (stats->ncounters); } static inline void incrementcounter(isc_stats_t *stats, int counter) { isc_int32_t prev; #ifdef ISC_RWLOCK_USEATOMIC /* * 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. */ isc_rwlock_lock(&stats->counterlock, isc_rwlocktype_read); #endif #if ISC_STATS_USEMULTIFIELDS prev = isc_atomic_xadd((isc_int32_t *)&stats->counters[counter].lo, 1); /* * 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 (prev == (isc_int32_t)0xffffffff) isc_atomic_xadd((isc_int32_t *)&stats->counters[counter].hi, 1); #elif defined(ISC_PLATFORM_HAVEXADDQ) UNUSED(prev); isc_atomic_xaddq((isc_int64_t *)&stats->counters[counter], 1); #else UNUSED(prev); stats->counters[counter]++; #endif #ifdef ISC_RWLOCK_USEATOMIC isc_rwlock_unlock(&stats->counterlock, isc_rwlocktype_read); #endif } static inline void decrementcounter(isc_stats_t *stats, int counter) { isc_int32_t prev; #ifdef ISC_RWLOCK_USEATOMIC isc_rwlock_lock(&stats->counterlock, isc_rwlocktype_read); #endif #if ISC_STATS_USEMULTIFIELDS prev = isc_atomic_xadd((isc_int32_t *)&stats->counters[counter].lo, -1); if (prev == 0) isc_atomic_xadd((isc_int32_t *)&stats->counters[counter].hi, -1); #elif defined(ISC_PLATFORM_HAVEXADDQ) UNUSED(prev); isc_atomic_xaddq((isc_int64_t *)&stats->counters[counter], -1); #else UNUSED(prev); stats->counters[counter]--; #endif #ifdef ISC_RWLOCK_USEATOMIC isc_rwlock_unlock(&stats->counterlock, isc_rwlocktype_read); #endif } static void copy_counters(isc_stats_t *stats) { int i; #ifdef ISC_RWLOCK_USEATOMIC /* * We use a "write" lock before "reading" the statistics counters as * an exclusive lock. */ isc_rwlock_lock(&stats->counterlock, isc_rwlocktype_write); #endif #if ISC_STATS_USEMULTIFIELDS for (i = 0; i < stats->ncounters; i++) { stats->copiedcounters[i] = (isc_uint64_t)(stats->counters[i].hi) << 32 | stats->counters[i].lo; } #else UNUSED(i); memcpy(stats->copiedcounters, stats->counters, stats->ncounters * sizeof(isc_stat_t)); #endif #ifdef ISC_RWLOCK_USEATOMIC isc_rwlock_unlock(&stats->counterlock, isc_rwlocktype_write); #endif } isc_result_t isc_stats_create(isc_mem_t *mctx, isc_stats_t **statsp, int ncounters) { REQUIRE(statsp != NULL && *statsp == NULL); return (create_stats(mctx, ncounters, statsp)); } void isc_stats_increment(isc_stats_t *stats, isc_statscounter_t counter) { REQUIRE(ISC_STATS_VALID(stats)); REQUIRE(counter < stats->ncounters); incrementcounter(stats, (int)counter); } void isc_stats_decrement(isc_stats_t *stats, isc_statscounter_t counter) { REQUIRE(ISC_STATS_VALID(stats)); REQUIRE(counter < stats->ncounters); decrementcounter(stats, (int)counter); } void isc_stats_dump(isc_stats_t *stats, isc_stats_dumper_t dump_fn, void *arg, unsigned int options) { int i; REQUIRE(ISC_STATS_VALID(stats)); copy_counters(stats); for (i = 0; i < stats->ncounters; i++) { if ((options & ISC_STATSDUMP_VERBOSE) == 0 && stats->copiedcounters[i] == 0) continue; dump_fn((isc_statscounter_t)i, stats->copiedcounters[i], arg); } }
Upload File
Create Folder