cannam@127: /* cannam@127: * Copyright (c) 2000 Matteo Frigo cannam@127: * Copyright (c) 2000 Massachusetts Institute of Technology cannam@127: * cannam@127: * This program is free software; you can redistribute it and/or modify cannam@127: * it under the terms of the GNU General Public License as published by cannam@127: * the Free Software Foundation; either version 2 of the License, or cannam@127: * (at your option) any later version. cannam@127: * cannam@127: * This program is distributed in the hope that it will be useful, cannam@127: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@127: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@127: * GNU General Public License for more details. cannam@127: * cannam@127: * You should have received a copy of the GNU General Public License cannam@127: * along with this program; if not, write to the Free Software cannam@127: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA cannam@127: * cannam@127: */ cannam@127: cannam@127: #include "bench.h" cannam@127: #include cannam@127: #include cannam@127: #include cannam@127: #include cannam@127: cannam@127: #if defined(HAVE_MALLOC_H) cannam@127: # include cannam@127: #endif cannam@127: cannam@127: #if defined(HAVE_DECL_MEMALIGN) && !HAVE_DECL_MEMALIGN cannam@127: extern void *memalign(size_t, size_t); cannam@127: #endif cannam@127: cannam@127: #if defined(HAVE_DECL_POSIX_MEMALIGN) && !HAVE_DECL_POSIX_MEMALIGN cannam@127: extern int posix_memalign(void **, size_t, size_t); cannam@127: #endif cannam@127: cannam@127: void bench_assertion_failed(const char *s, int line, const char *file) cannam@127: { cannam@127: ovtpvt_err("bench: %s:%d: assertion failed: %s\n", file, line, s); cannam@127: bench_exit(EXIT_FAILURE); cannam@127: } cannam@127: cannam@127: #ifdef HAVE_DRAND48 cannam@127: # if defined(HAVE_DECL_DRAND48) && !HAVE_DECL_DRAND48 cannam@127: extern double drand48(void); cannam@127: # endif cannam@127: double bench_drand(void) cannam@127: { cannam@127: return drand48() - 0.5; cannam@127: } cannam@127: # if defined(HAVE_DECL_SRAND48) && !HAVE_DECL_SRAND48 cannam@127: extern void srand48(long); cannam@127: # endif cannam@127: void bench_srand(int seed) cannam@127: { cannam@127: srand48(seed); cannam@127: } cannam@127: #else cannam@127: double bench_drand(void) cannam@127: { cannam@127: double d = rand(); cannam@127: return (d / (double) RAND_MAX) - 0.5; cannam@127: } cannam@127: void bench_srand(int seed) cannam@127: { cannam@127: srand(seed); cannam@127: } cannam@127: #endif cannam@127: cannam@127: /********************************************************** cannam@127: * DEBUGGING CODE cannam@127: **********************************************************/ cannam@127: #ifdef BENCH_DEBUG cannam@127: static int bench_malloc_cnt = 0; cannam@127: cannam@127: /* cannam@127: * debugging malloc/free. Initialize every malloced and freed area to cannam@127: * random values, just to make sure we are not using uninitialized cannam@127: * pointers. Also check for writes past the ends of allocated blocks, cannam@127: * and a couple of other things. cannam@127: * cannam@127: * This code is a quick and dirty hack -- use at your own risk. cannam@127: */ cannam@127: cannam@127: static int bench_malloc_total = 0, bench_malloc_max = 0, bench_malloc_cnt_max = 0; cannam@127: cannam@127: #define MAGIC ((size_t)0xABadCafe) cannam@127: #define PAD_FACTOR 2 cannam@127: #define TWO_SIZE_T (2 * sizeof(size_t)) cannam@127: cannam@127: #define VERBOSE_ALLOCATION 0 cannam@127: cannam@127: #if VERBOSE_ALLOCATION cannam@127: #define WHEN_VERBOSE(a) a cannam@127: #else cannam@127: #define WHEN_VERBOSE(a) cannam@127: #endif cannam@127: cannam@127: void *bench_malloc(size_t n) cannam@127: { cannam@127: char *p; cannam@127: size_t i; cannam@127: cannam@127: bench_malloc_total += n; cannam@127: cannam@127: if (bench_malloc_total > bench_malloc_max) cannam@127: bench_malloc_max = bench_malloc_total; cannam@127: cannam@127: p = (char *) malloc(PAD_FACTOR * n + TWO_SIZE_T); cannam@127: BENCH_ASSERT(p); cannam@127: cannam@127: /* store the size in a known position */ cannam@127: ((size_t *) p)[0] = n; cannam@127: ((size_t *) p)[1] = MAGIC; cannam@127: for (i = 0; i < PAD_FACTOR * n; i++) cannam@127: p[i + TWO_SIZE_T] = (char) (i ^ 0xDEADBEEF); cannam@127: cannam@127: ++bench_malloc_cnt; cannam@127: cannam@127: if (bench_malloc_cnt > bench_malloc_cnt_max) cannam@127: bench_malloc_cnt_max = bench_malloc_cnt; cannam@127: cannam@127: /* skip the size we stored previously */ cannam@127: return (void *) (p + TWO_SIZE_T); cannam@127: } cannam@127: cannam@127: void bench_free(void *p) cannam@127: { cannam@127: char *q; cannam@127: cannam@127: BENCH_ASSERT(p); cannam@127: cannam@127: q = ((char *) p) - TWO_SIZE_T; cannam@127: BENCH_ASSERT(q); cannam@127: cannam@127: { cannam@127: size_t n = ((size_t *) q)[0]; cannam@127: size_t magic = ((size_t *) q)[1]; cannam@127: size_t i; cannam@127: cannam@127: ((size_t *) q)[0] = 0; /* set to zero to detect duplicate free's */ cannam@127: cannam@127: BENCH_ASSERT(magic == MAGIC); cannam@127: ((size_t *) q)[1] = ~MAGIC; cannam@127: cannam@127: bench_malloc_total -= n; cannam@127: BENCH_ASSERT(bench_malloc_total >= 0); cannam@127: cannam@127: /* check for writing past end of array: */ cannam@127: for (i = n; i < PAD_FACTOR * n; ++i) cannam@127: if (q[i + TWO_SIZE_T] != (char) (i ^ 0xDEADBEEF)) { cannam@127: BENCH_ASSERT(0 /* array bounds overwritten */); cannam@127: } cannam@127: for (i = 0; i < PAD_FACTOR * n; ++i) cannam@127: q[i + TWO_SIZE_T] = (char) (i ^ 0xBEEFDEAD); cannam@127: cannam@127: --bench_malloc_cnt; cannam@127: cannam@127: BENCH_ASSERT(bench_malloc_cnt >= 0); cannam@127: cannam@127: BENCH_ASSERT( cannam@127: (bench_malloc_cnt == 0 && bench_malloc_total == 0) || cannam@127: (bench_malloc_cnt > 0 && bench_malloc_total > 0)); cannam@127: cannam@127: free(q); cannam@127: } cannam@127: } cannam@127: cannam@127: #else cannam@127: /********************************************************** cannam@127: * NON DEBUGGING CODE cannam@127: **********************************************************/ cannam@127: /* production version, no hacks */ cannam@127: cannam@127: #define MIN_ALIGNMENT 128 /* must be power of two */ cannam@127: cannam@127: #define real_free free /* memalign and malloc use ordinary free */ cannam@127: cannam@127: void *bench_malloc(size_t n) cannam@127: { cannam@127: void *p; cannam@127: if (n == 0) n = 1; cannam@127: cannam@127: #if defined(WITH_OUR_MALLOC) cannam@127: /* Our own aligned malloc/free. Assumes sizeof(void*) is cannam@127: a power of two <= 8 and that malloc is at least cannam@127: sizeof(void*)-aligned. Assumes size_t = uintptr_t. */ cannam@127: { cannam@127: void *p0; cannam@127: if ((p0 = malloc(n + MIN_ALIGNMENT))) { cannam@127: p = (void *) (((size_t) p0 + MIN_ALIGNMENT) & (~((size_t) (MIN_ALIGNMENT - 1)))); cannam@127: *((void **) p - 1) = p0; cannam@127: } cannam@127: else cannam@127: p = (void *) 0; cannam@127: } cannam@127: #elif defined(HAVE_MEMALIGN) cannam@127: p = memalign(MIN_ALIGNMENT, n); cannam@127: #elif defined(HAVE_POSIX_MEMALIGN) cannam@127: /* note: posix_memalign is broken in glibc 2.2.5: it constrains cannam@127: the size, not the alignment, to be (power of two) * sizeof(void*). cannam@127: The bug seems to have been fixed as of glibc 2.3.1. */ cannam@127: if (posix_memalign(&p, MIN_ALIGNMENT, n)) cannam@127: p = (void*) 0; cannam@127: #elif defined(__ICC) || defined(__INTEL_COMPILER) || defined(HAVE__MM_MALLOC) cannam@127: /* Intel's C compiler defines _mm_malloc and _mm_free intrinsics */ cannam@127: p = (void *) _mm_malloc(n, MIN_ALIGNMENT); cannam@127: # undef real_free cannam@127: # define real_free _mm_free cannam@127: #else cannam@127: p = malloc(n); cannam@127: #endif cannam@127: cannam@127: BENCH_ASSERT(p); cannam@127: return p; cannam@127: } cannam@127: cannam@127: void bench_free(void *p) cannam@127: { cannam@127: #ifdef WITH_OUR_MALLOC cannam@127: if (p) free(*((void **) p - 1)); cannam@127: #else cannam@127: real_free(p); cannam@127: #endif cannam@127: } cannam@127: cannam@127: #endif cannam@127: cannam@127: void bench_free0(void *p) cannam@127: { cannam@127: if (p) bench_free(p); cannam@127: }