Commit d2341541 authored by David Chinner's avatar David Chinner Committed by Lachlan McIlroy

[XFS] Use power-of-2 sized buffers to reduce overhead

Now that the ktrace_enter() code is using atomics, the non-power-of-2
buffer sizes - which require modulus operations to get the index - are
showing up as using substantial CPU in the profiles.

Force the buffer sizes to be rounded up to the nearest power of two and
use masking rather than modulus operations to convert the index counter to
the buffer index. This reduces ktrace_enter overhead to 8% of a CPU time,
and again almost halves the trace intensive test runtime.

SGI-PV: 977546
SGI-Modid: xfs-linux-melb:xfs-kern:30538a
Signed-off-by: default avatarDavid Chinner <dgc@sgi.com>
Signed-off-by: default avatarChristoph Hellwig <hch@infradead.org>
Signed-off-by: default avatarLachlan McIlroy <lachlan@sgi.com>
parent 6ee4752f
...@@ -24,7 +24,7 @@ static int ktrace_zentries; ...@@ -24,7 +24,7 @@ static int ktrace_zentries;
void __init void __init
ktrace_init(int zentries) ktrace_init(int zentries)
{ {
ktrace_zentries = zentries; ktrace_zentries = roundup_pow_of_two(zentries);
ktrace_hdr_zone = kmem_zone_init(sizeof(ktrace_t), ktrace_hdr_zone = kmem_zone_init(sizeof(ktrace_t),
"ktrace_hdr"); "ktrace_hdr");
...@@ -47,13 +47,16 @@ ktrace_uninit(void) ...@@ -47,13 +47,16 @@ ktrace_uninit(void)
* ktrace_alloc() * ktrace_alloc()
* *
* Allocate a ktrace header and enough buffering for the given * Allocate a ktrace header and enough buffering for the given
* number of entries. * number of entries. Round the number of entries up to a
* power of 2 so we can do fast masking to get the index from
* the atomic index counter.
*/ */
ktrace_t * ktrace_t *
ktrace_alloc(int nentries, unsigned int __nocast sleep) ktrace_alloc(int nentries, unsigned int __nocast sleep)
{ {
ktrace_t *ktp; ktrace_t *ktp;
ktrace_entry_t *ktep; ktrace_entry_t *ktep;
int entries;
ktp = (ktrace_t*)kmem_zone_alloc(ktrace_hdr_zone, sleep); ktp = (ktrace_t*)kmem_zone_alloc(ktrace_hdr_zone, sleep);
...@@ -70,11 +73,12 @@ ktrace_alloc(int nentries, unsigned int __nocast sleep) ...@@ -70,11 +73,12 @@ ktrace_alloc(int nentries, unsigned int __nocast sleep)
/* /*
* Special treatment for buffers with the ktrace_zentries entries * Special treatment for buffers with the ktrace_zentries entries
*/ */
if (nentries == ktrace_zentries) { entries = roundup_pow_of_two(nentries);
if (entries == ktrace_zentries) {
ktep = (ktrace_entry_t*)kmem_zone_zalloc(ktrace_ent_zone, ktep = (ktrace_entry_t*)kmem_zone_zalloc(ktrace_ent_zone,
sleep); sleep);
} else { } else {
ktep = (ktrace_entry_t*)kmem_zalloc((nentries * sizeof(*ktep)), ktep = (ktrace_entry_t*)kmem_zalloc((entries * sizeof(*ktep)),
sleep | KM_LARGE); sleep | KM_LARGE);
} }
...@@ -91,7 +95,9 @@ ktrace_alloc(int nentries, unsigned int __nocast sleep) ...@@ -91,7 +95,9 @@ ktrace_alloc(int nentries, unsigned int __nocast sleep)
} }
ktp->kt_entries = ktep; ktp->kt_entries = ktep;
ktp->kt_nentries = nentries; ktp->kt_nentries = entries;
ASSERT(is_power_of_2(entries));
ktp->kt_index_mask = entries - 1;
atomic_set(&ktp->kt_index, 0); atomic_set(&ktp->kt_index, 0);
ktp->kt_rollover = 0; ktp->kt_rollover = 0;
return ktp; return ktp;
...@@ -160,7 +166,7 @@ ktrace_enter( ...@@ -160,7 +166,7 @@ ktrace_enter(
* Grab an entry by pushing the index up to the next one. * Grab an entry by pushing the index up to the next one.
*/ */
index = atomic_add_return(1, &ktp->kt_index); index = atomic_add_return(1, &ktp->kt_index);
index = (index - 1) % ktp->kt_nentries; index = (index - 1) & ktp->kt_index_mask;
if (!ktp->kt_rollover && index == ktp->kt_nentries - 1) if (!ktp->kt_rollover && index == ktp->kt_nentries - 1)
ktp->kt_rollover = 1; ktp->kt_rollover = 1;
...@@ -197,7 +203,7 @@ ktrace_nentries( ...@@ -197,7 +203,7 @@ ktrace_nentries(
if (ktp == NULL) if (ktp == NULL)
return 0; return 0;
index = atomic_read(&ktp->kt_index) % ktp->kt_nentries; index = atomic_read(&ktp->kt_index) & ktp->kt_index_mask;
return (ktp->kt_rollover ? ktp->kt_nentries : index); return (ktp->kt_rollover ? ktp->kt_nentries : index);
} }
...@@ -223,7 +229,7 @@ ktrace_first(ktrace_t *ktp, ktrace_snap_t *ktsp) ...@@ -223,7 +229,7 @@ ktrace_first(ktrace_t *ktp, ktrace_snap_t *ktsp)
int nentries; int nentries;
if (ktp->kt_rollover) if (ktp->kt_rollover)
index = atomic_read(&ktp->kt_index) % ktp->kt_nentries; index = atomic_read(&ktp->kt_index) & ktp->kt_index_mask;
else else
index = 0; index = 0;
......
...@@ -31,6 +31,7 @@ typedef struct ktrace_entry { ...@@ -31,6 +31,7 @@ typedef struct ktrace_entry {
typedef struct ktrace { typedef struct ktrace {
int kt_nentries; /* number of entries in trace buf */ int kt_nentries; /* number of entries in trace buf */
atomic_t kt_index; /* current index in entries */ atomic_t kt_index; /* current index in entries */
unsigned int kt_index_mask;
int kt_rollover; int kt_rollover;
ktrace_entry_t *kt_entries; /* buffer of entries */ ktrace_entry_t *kt_entries; /* buffer of entries */
} ktrace_t; } ktrace_t;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment