]> git.sesse.net Git - bcachefs-tools-debian/blob - raid/memory.h
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[bcachefs-tools-debian] / raid / memory.h
1 /*
2  * Copyright (C) 2013 Andrea Mazzoleni
3  *
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.
8  *
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.
13  */
14
15 #ifndef __RAID_MEMORY_H
16 #define __RAID_MEMORY_H
17
18 /**
19  * Memory alignment provided by raid_malloc().
20  *
21  * It should guarantee good cache performance everywhere.
22  */
23 #define RAID_MALLOC_ALIGN 256
24
25 /**
26  * Memory displacement to avoid cache address sharing on contiguous blocks,
27  * used by raid_malloc_vector().
28  *
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
33  * end.
34  *
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.
37  *
38  * The selected displacement was chosen empirically with some speed tests
39  * with 8/12/16/20/24 data buffers of 256 KB.
40  *
41  * These are the results in MB/s with no displacement:
42  *
43  *            sse2
44  *    gen1   15368 [MB/s]
45  *    gen2    6814 [MB/s]
46  *    genz    3033 [MB/s]
47  *
48  * These are the results with displacement resulting in improvments
49  * in the order of 20% or more:
50  *
51  *            sse2
52  *    gen1   21936 [MB/s]
53  *    gen2   11902 [MB/s]
54  *    genz    5838 [MB/s]
55  *
56  */
57 #define RAID_MALLOC_DISPLACEMENT (7*256)
58
59 /**
60  * Aligned malloc.
61  * Use an alignment suitable for the raid functions.
62  */
63 void *raid_malloc(size_t size, void **freeptr);
64
65 /**
66  * Arbitrary aligned malloc.
67  */
68 void *raid_malloc_align(size_t size, size_t align_size, void **freeptr);
69
70 /**
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.
76  */
77 void **raid_malloc_vector(int nd, int n, size_t size, void **freeptr);
78
79 /**
80  * Arbitrary aligned vector allocation.
81  */
82 void **raid_malloc_vector_align(int nd, int n, size_t size, size_t align_size, size_t displacement_size, void **freeptr);
83
84 /**
85  * Fills the memory vector with pseudo-random data based on the specified seed.
86  */
87 void raid_mrand_vector(unsigned seed, int n, size_t size, void **vv);
88
89 /**
90  * Tests the memory vector for RAM problems.
91  * If a problem is found, it crashes.
92  */
93 int raid_mtest_vector(int n, size_t size, void **vv);
94
95 #endif
96