Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2017 Intel Corporation.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include "spdk/stdinc.h"
7 : :
8 : : #include "cache_tree.h"
9 : :
10 : : #include "spdk/assert.h"
11 : :
12 : : struct cache_buffer *
13 : 14756417 : tree_find_buffer(struct cache_tree *tree, uint64_t offset)
14 : : {
15 : : uint64_t index;
16 : :
17 [ + + ]: 29999762 : while (tree != NULL) {
18 [ - + - + : 28913856 : index = offset / CACHE_TREE_LEVEL_SIZE(tree->level);
# # # # #
# # # ]
19 [ + + # # ]: 28913856 : if (index >= CACHE_TREE_WIDTH) {
20 : 7972 : return NULL;
21 : : }
22 [ + + # # : 28905884 : if (tree->level == 0) {
# # ]
23 [ # # # # : 13662539 : return tree->u.buffer[index];
# # # # #
# ]
24 : : } else {
25 [ - + # # : 15243345 : offset &= CACHE_TREE_LEVEL_MASK(tree->level);
# # # # #
# ]
26 [ # # # # : 15243345 : tree = tree->u.tree[index];
# # # # ]
27 : : }
28 : : }
29 : :
30 : 1085906 : return NULL;
31 : 46 : }
32 : :
33 : : struct cache_buffer *
34 : 5752335 : tree_find_filled_buffer(struct cache_tree *tree, uint64_t offset)
35 : : {
36 : : struct cache_buffer *buf;
37 : :
38 : 5752335 : buf = tree_find_buffer(tree, offset);
39 [ + + + + : 5752335 : if (buf != NULL && buf->bytes_filled > 0) {
# # # # ]
40 : 4520262 : return buf;
41 : : } else {
42 : 1232073 : return NULL;
43 : : }
44 : 3 : }
45 : :
46 : : struct cache_tree *
47 : 221825 : tree_insert_buffer(struct cache_tree *root, struct cache_buffer *buffer)
48 : : {
49 : : struct cache_tree *tree;
50 : : uint64_t index, offset;
51 : :
52 [ # # # # ]: 221825 : offset = buffer->offset;
53 [ + + + + : 222909 : while (offset >= CACHE_TREE_LEVEL_SIZE(root->level + 1)) {
# # # # #
# # # #
# ]
54 [ + + # # : 1084 : if (root->present_mask != 0) {
# # ]
55 : 482 : tree = calloc(1, sizeof(*tree));
56 [ + + # # ]: 482 : assert(tree != NULL);
57 [ # # # # : 482 : tree->level = root->level + 1;
# # # # #
# ]
58 [ # # # # : 482 : tree->u.tree[0] = root;
# # # # ]
59 : 482 : root = tree;
60 [ # # # # ]: 482 : root->present_mask = 0x1ULL;
61 : 3 : } else {
62 [ # # ]: 602 : root->level++;
63 : : }
64 : : }
65 : :
66 : 221825 : tree = root;
67 [ + + # # : 418120 : while (tree->level > 0) {
# # ]
68 [ - + - + : 196295 : index = offset / CACHE_TREE_LEVEL_SIZE(tree->level);
# # # # #
# # # ]
69 [ + + # # : 196295 : assert(index < CACHE_TREE_WIDTH);
# # ]
70 [ - + # # : 196295 : offset &= CACHE_TREE_LEVEL_MASK(tree->level);
# # # # #
# ]
71 [ + + # # : 196295 : if (tree->u.tree[index] == NULL) {
# # # # #
# ]
72 [ # # # # : 5158 : tree->u.tree[index] = calloc(1, sizeof(*tree));
# # # # ]
73 [ + + # # : 5158 : assert(tree->u.tree[index] != NULL);
# # # # #
# # # ]
74 [ # # # # : 5158 : tree->u.tree[index]->level = tree->level - 1;
# # # # #
# # # # #
# # # # ]
75 [ - + # # : 5158 : tree->present_mask |= (1ULL << index);
# # ]
76 : 5 : }
77 [ # # # # : 196295 : tree = tree->u.tree[index];
# # # # ]
78 : : }
79 : :
80 [ # # # # ]: 221825 : index = offset / CACHE_BUFFER_SIZE;
81 [ + + # # : 221825 : assert(index < CACHE_TREE_WIDTH);
# # ]
82 [ + + # # : 221825 : assert(tree->u.buffer[index] == NULL);
# # # # #
# # # #
# ]
83 [ # # # # : 221825 : tree->u.buffer[index] = buffer;
# # # # #
# ]
84 [ - + # # : 221825 : tree->present_mask |= (1ULL << index);
# # ]
85 : 221825 : return root;
86 : : }
87 : :
88 : : void
89 : 143502 : tree_remove_buffer(struct cache_tree *tree, struct cache_buffer *buffer)
90 : : {
91 : : struct cache_tree *child;
92 : : uint64_t index;
93 : :
94 [ - + # # : 143502 : index = CACHE_TREE_INDEX(tree->level, buffer->offset);
# # # # #
# # # # #
# # ]
95 : :
96 [ + + # # : 143502 : if (tree->level == 0) {
# # ]
97 [ + + # # : 70250 : assert(tree->u.buffer[index] != NULL);
# # # # #
# # # #
# ]
98 [ - + # # : 70250 : assert(buffer == tree->u.buffer[index]);
# # # # #
# # # #
# ]
99 [ - + # # : 70250 : tree->present_mask &= ~(1ULL << index);
# # ]
100 [ # # # # : 70250 : tree->u.buffer[index] = NULL;
# # # # #
# ]
101 : 70250 : cache_buffer_free(buffer);
102 : 70250 : return;
103 : : }
104 : :
105 [ # # # # : 73252 : child = tree->u.tree[index];
# # # # ]
106 [ + + # # ]: 73252 : assert(child != NULL);
107 : 73252 : tree_remove_buffer(child, buffer);
108 [ + + # # : 73252 : if (child->present_mask == 0) {
# # ]
109 [ - + # # : 966 : tree->present_mask &= ~(1ULL << index);
# # ]
110 [ # # # # : 966 : tree->u.tree[index] = NULL;
# # # # ]
111 : 966 : free(child);
112 : 1 : }
113 : 6 : }
114 : :
115 : : void
116 : 7316 : tree_free_buffers(struct cache_tree *tree)
117 : : {
118 : : struct cache_buffer *buffer;
119 : : struct cache_tree *child;
120 : : uint32_t i;
121 : :
122 [ + + # # : 7316 : if (tree->present_mask == 0) {
# # ]
123 : 0 : return;
124 : : }
125 : :
126 [ + + # # : 7316 : if (tree->level == 0) {
# # ]
127 [ + + # # ]: 306345 : for (i = 0; i < CACHE_TREE_WIDTH; i++) {
128 [ # # # # : 301632 : buffer = tree->u.buffer[i];
# # # # #
# ]
129 [ + + + + : 301632 : if (buffer != NULL && buffer->in_progress == false &&
+ + # # #
# # # ]
130 [ + + # # : 151585 : buffer->bytes_filled == buffer->bytes_flushed) {
# # # # ]
131 : 151583 : cache_buffer_free(buffer);
132 [ # # # # : 151583 : tree->u.buffer[i] = NULL;
# # # # #
# ]
133 [ - + # # : 151583 : tree->present_mask &= ~(1ULL << i);
# # ]
134 : 16 : }
135 : 640 : }
136 : 10 : } else {
137 [ + + # # ]: 169195 : for (i = 0; i < CACHE_TREE_WIDTH; i++) {
138 [ # # # # : 166592 : child = tree->u.tree[i];
# # # # ]
139 [ + + ]: 166592 : if (child != NULL) {
140 : 4707 : tree_free_buffers(child);
141 [ + + # # : 4707 : if (child->present_mask == 0) {
# # ]
142 : 4682 : free(child);
143 [ # # # # : 4682 : tree->u.tree[i] = NULL;
# # # # ]
144 [ - + # # : 4682 : tree->present_mask &= ~(1ULL << i);
# # ]
145 : 9 : }
146 : 9 : }
147 : 384 : }
148 : : }
149 : 16 : }
|