2 * Copyright (C) 2013 Andrea Mazzoleni
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #ifndef __RAID_MEMORY_H
16 #define __RAID_MEMORY_H
19 * Memory alignment provided by raid_malloc().
21 * It should guarantee good cache performance everywhere.
23 #define RAID_MALLOC_ALIGN 256
26 * Memory displacement to avoid cache address sharing on contiguous blocks,
27 * used by raid_malloc_vector().
29 * When allocating a sequence of blocks with a size of power of 2,
30 * there is the risk that the addresses of each block are mapped into the
31 * same cache line and prefetching predictor, resulting in a lot of cache
32 * sharing if you access all the blocks in parallel, from the start to the
35 * To avoid this effect, it's better if all the blocks are allocated
36 * with a fixed displacement trying to reduce the cache addresses sharing.
38 * The selected displacement was chosen empirically with some speed tests
39 * with 8/12/16/20/24 data buffers of 256 KB.
41 * These are the results in MB/s with no displacement:
48 * These are the results with displacement resulting in improvments
49 * in the order of 20% or more:
57 #define RAID_MALLOC_DISPLACEMENT (7*256)
61 * Use an alignment suitable for the raid functions.
63 void *raid_malloc(size_t size, void **freeptr);
66 * Arbitrary aligned malloc.
68 void *raid_malloc_align(size_t size, size_t align_size, void **freeptr);
71 * Aligned vector allocation.
72 * Use an alignment suitable for the raid functions.
73 * Returns a vector of @n pointers, each one pointing to a block of
74 * the specified @size.
75 * The first @nd elements are reversed in order.
77 void **raid_malloc_vector(int nd, int n, size_t size, void **freeptr);
80 * Arbitrary aligned vector allocation.
82 void **raid_malloc_vector_align(int nd, int n, size_t size, size_t align_size, size_t displacement_size, void **freeptr);
85 * Fills the memory vector with pseudo-random data based on the specified seed.
87 void raid_mrand_vector(unsigned seed, int n, size_t size, void **vv);
90 * Tests the memory vector for RAM problems.
91 * If a problem is found, it crashes.
93 int raid_mtest_vector(int n, size_t size, void **vv);