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 : 35820698 : tree_find_buffer(struct cache_tree *tree, uint64_t offset)
14 : : {
15 : : uint64_t index;
16 : :
17 [ + + ]: 71237652 : while (tree != NULL) {
18 [ - + - + : 70562395 : index = offset / CACHE_TREE_LEVEL_SIZE(tree->level);
# # # # #
# # # ]
19 [ + + # # ]: 70562395 : if (index >= CACHE_TREE_WIDTH) {
20 : 4052 : return NULL;
21 : : }
22 [ + + # # : 70558343 : if (tree->level == 0) {
# # ]
23 [ # # # # : 35141389 : return tree->u.buffer[index];
# # # # #
# ]
24 : : } else {
25 [ - + # # : 35416954 : offset &= CACHE_TREE_LEVEL_MASK(tree->level);
# # # # #
# ]
26 [ # # # # : 35416954 : tree = tree->u.tree[index];
# # # # ]
27 : : }
28 : : }
29 : :
30 : 675257 : return NULL;
31 : 46 : }
32 : :
33 : : struct cache_buffer *
34 : 12496085 : tree_find_filled_buffer(struct cache_tree *tree, uint64_t offset)
35 : : {
36 : : struct cache_buffer *buf;
37 : :
38 : 12496085 : buf = tree_find_buffer(tree, offset);
39 [ + + + + : 12496085 : if (buf != NULL && buf->bytes_filled > 0) {
# # # # ]
40 : 11757645 : return buf;
41 : : } else {
42 : 738440 : return NULL;
43 : : }
44 : 3 : }
45 : :
46 : : struct cache_tree *
47 : 443285 : tree_insert_buffer(struct cache_tree *root, struct cache_buffer *buffer)
48 : : {
49 : : struct cache_tree *tree;
50 : : uint64_t index, offset;
51 : :
52 [ # # # # ]: 443285 : offset = buffer->offset;
53 [ + + + + : 444928 : while (offset >= CACHE_TREE_LEVEL_SIZE(root->level + 1)) {
# # # # #
# # # #
# ]
54 [ + + # # : 1643 : if (root->present_mask != 0) {
# # ]
55 : 933 : tree = calloc(1, sizeof(*tree));
56 [ + + # # ]: 933 : assert(tree != NULL);
57 [ # # # # : 933 : tree->level = root->level + 1;
# # # # #
# ]
58 [ # # # # : 933 : tree->u.tree[0] = root;
# # # # ]
59 : 933 : root = tree;
60 [ # # # # ]: 933 : root->present_mask = 0x1ULL;
61 : 3 : } else {
62 [ # # ]: 710 : root->level++;
63 : : }
64 : : }
65 : :
66 : 443285 : tree = root;
67 [ + + # # : 826037 : while (tree->level > 0) {
# # ]
68 [ - + - + : 382752 : index = offset / CACHE_TREE_LEVEL_SIZE(tree->level);
# # # # #
# # # ]
69 [ + + # # : 382752 : assert(index < CACHE_TREE_WIDTH);
# # ]
70 [ - + # # : 382752 : offset &= CACHE_TREE_LEVEL_MASK(tree->level);
# # # # #
# ]
71 [ + + # # : 382752 : if (tree->u.tree[index] == NULL) {
# # # # #
# ]
72 [ # # # # : 10385 : tree->u.tree[index] = calloc(1, sizeof(*tree));
# # # # ]
73 [ + + # # : 10385 : assert(tree->u.tree[index] != NULL);
# # # # #
# # # ]
74 [ # # # # : 10385 : tree->u.tree[index]->level = tree->level - 1;
# # # # #
# # # # #
# # # # ]
75 [ - + # # : 10385 : tree->present_mask |= (1ULL << index);
# # ]
76 : 5 : }
77 [ # # # # : 382752 : tree = tree->u.tree[index];
# # # # ]
78 : : }
79 : :
80 [ # # # # ]: 443285 : index = offset / CACHE_BUFFER_SIZE;
81 [ + + # # : 443285 : assert(index < CACHE_TREE_WIDTH);
# # ]
82 [ + + # # : 443285 : assert(tree->u.buffer[index] == NULL);
# # # # #
# # # #
# ]
83 [ # # # # : 443285 : tree->u.buffer[index] = buffer;
# # # # #
# ]
84 [ - + # # : 443285 : tree->present_mask |= (1ULL << index);
# # ]
85 : 443285 : return root;
86 : : }
87 : :
88 : : void
89 : 362558 : tree_remove_buffer(struct cache_tree *tree, struct cache_buffer *buffer)
90 : : {
91 : : struct cache_tree *child;
92 : : uint64_t index;
93 : :
94 [ - + # # : 362558 : index = CACHE_TREE_INDEX(tree->level, buffer->offset);
# # # # #
# # # # #
# # ]
95 : :
96 [ + + # # : 362558 : if (tree->level == 0) {
# # ]
97 [ + + # # : 181816 : assert(tree->u.buffer[index] != NULL);
# # # # #
# # # #
# ]
98 [ - + # # : 181816 : assert(buffer == tree->u.buffer[index]);
# # # # #
# # # #
# ]
99 [ - + # # : 181816 : tree->present_mask &= ~(1ULL << index);
# # ]
100 [ # # # # : 181816 : tree->u.buffer[index] = NULL;
# # # # #
# ]
101 : 181816 : cache_buffer_free(buffer);
102 : 181816 : return;
103 : : }
104 : :
105 [ # # # # : 180742 : child = tree->u.tree[index];
# # # # ]
106 [ + + # # ]: 180742 : assert(child != NULL);
107 : 180742 : tree_remove_buffer(child, buffer);
108 [ + + # # : 180742 : if (child->present_mask == 0) {
# # ]
109 [ - + # # : 2534 : tree->present_mask &= ~(1ULL << index);
# # ]
110 [ # # # # : 2534 : tree->u.tree[index] = NULL;
# # # # ]
111 : 2534 : free(child);
112 : 1 : }
113 : 6 : }
114 : :
115 : : void
116 : 14091 : tree_free_buffers(struct cache_tree *tree)
117 : : {
118 : : struct cache_buffer *buffer;
119 : : struct cache_tree *child;
120 : : uint32_t i;
121 : :
122 [ + + # # : 14091 : if (tree->present_mask == 0) {
# # ]
123 : 0 : return;
124 : : }
125 : :
126 [ + + # # : 14091 : if (tree->level == 0) {
# # ]
127 [ + + # # ]: 580580 : for (i = 0; i < CACHE_TREE_WIDTH; i++) {
128 [ # # # # : 571648 : buffer = tree->u.buffer[i];
# # # # #
# ]
129 [ + + + + : 571648 : if (buffer != NULL && buffer->in_progress == false &&
+ + # # #
# # # ]
130 [ + + # # : 261484 : buffer->bytes_filled == buffer->bytes_flushed) {
# # # # ]
131 : 261477 : cache_buffer_free(buffer);
132 [ # # # # : 261477 : tree->u.buffer[i] = NULL;
# # # # #
# ]
133 [ - + # # : 261477 : tree->present_mask &= ~(1ULL << i);
# # ]
134 : 16 : }
135 : 640 : }
136 : 10 : } else {
137 [ + + # # ]: 335335 : for (i = 0; i < CACHE_TREE_WIDTH; i++) {
138 [ # # # # : 330176 : child = tree->u.tree[i];
# # # # ]
139 [ + + ]: 330176 : if (child != NULL) {
140 : 8837 : tree_free_buffers(child);
141 [ + + # # : 8837 : if (child->present_mask == 0) {
# # ]
142 : 8792 : free(child);
143 [ # # # # : 8792 : tree->u.tree[i] = NULL;
# # # # ]
144 [ - + # # : 8792 : tree->present_mask &= ~(1ULL << i);
# # ]
145 : 9 : }
146 : 9 : }
147 : 384 : }
148 : : }
149 : 16 : }
|