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 : 23686355 : tree_find_buffer(struct cache_tree *tree, uint64_t offset)
14 : : {
15 : : uint64_t index;
16 : :
17 [ + + ]: 47151387 : while (tree != NULL) {
18 [ - + - + : 46345027 : index = offset / CACHE_TREE_LEVEL_SIZE(tree->level);
# # # # #
# # # ]
19 [ + + # # ]: 46345027 : if (index >= CACHE_TREE_WIDTH) {
20 : 4835 : return NULL;
21 : : }
22 [ + + # # : 46340192 : if (tree->level == 0) {
# # ]
23 [ # # # # : 22875160 : return tree->u.buffer[index];
# # # # #
# ]
24 : : } else {
25 [ - + # # : 23465032 : offset &= CACHE_TREE_LEVEL_MASK(tree->level);
# # # # #
# ]
26 [ # # # # : 23465032 : tree = tree->u.tree[index];
# # # # ]
27 : : }
28 : : }
29 : :
30 : 806360 : return NULL;
31 : 46 : }
32 : :
33 : : struct cache_buffer *
34 : 8520880 : tree_find_filled_buffer(struct cache_tree *tree, uint64_t offset)
35 : : {
36 : : struct cache_buffer *buf;
37 : :
38 : 8520880 : buf = tree_find_buffer(tree, offset);
39 [ + + + + : 8520880 : if (buf != NULL && buf->bytes_filled > 0) {
# # # # ]
40 : 7581150 : return buf;
41 : : } else {
42 : 939730 : return NULL;
43 : : }
44 : 3 : }
45 : :
46 : : struct cache_tree *
47 : 305822 : tree_insert_buffer(struct cache_tree *root, struct cache_buffer *buffer)
48 : : {
49 : : struct cache_tree *tree;
50 : : uint64_t index, offset;
51 : :
52 [ # # # # ]: 305822 : offset = buffer->offset;
53 [ + + + + : 307283 : while (offset >= CACHE_TREE_LEVEL_SIZE(root->level + 1)) {
# # # # #
# # # #
# ]
54 [ + + # # : 1461 : if (root->present_mask != 0) {
# # ]
55 : 696 : tree = calloc(1, sizeof(*tree));
56 [ + + # # ]: 696 : assert(tree != NULL);
57 [ # # # # : 696 : tree->level = root->level + 1;
# # # # #
# ]
58 [ # # # # : 696 : tree->u.tree[0] = root;
# # # # ]
59 : 696 : root = tree;
60 [ # # # # ]: 696 : root->present_mask = 0x1ULL;
61 : 3 : } else {
62 [ # # ]: 765 : root->level++;
63 : : }
64 : : }
65 : :
66 : 305822 : tree = root;
67 [ + + # # : 567094 : while (tree->level > 0) {
# # ]
68 [ - + - + : 261272 : index = offset / CACHE_TREE_LEVEL_SIZE(tree->level);
# # # # #
# # # ]
69 [ + + # # : 261272 : assert(index < CACHE_TREE_WIDTH);
# # ]
70 [ - + # # : 261272 : offset &= CACHE_TREE_LEVEL_MASK(tree->level);
# # # # #
# ]
71 [ + + # # : 261272 : if (tree->u.tree[index] == NULL) {
# # # # #
# ]
72 [ # # # # : 6671 : tree->u.tree[index] = calloc(1, sizeof(*tree));
# # # # ]
73 [ + + # # : 6671 : assert(tree->u.tree[index] != NULL);
# # # # #
# # # ]
74 [ # # # # : 6671 : tree->u.tree[index]->level = tree->level - 1;
# # # # #
# # # # #
# # # # ]
75 [ - + # # : 6671 : tree->present_mask |= (1ULL << index);
# # ]
76 : 5 : }
77 [ # # # # : 261272 : tree = tree->u.tree[index];
# # # # ]
78 : : }
79 : :
80 [ # # # # ]: 305822 : index = offset / CACHE_BUFFER_SIZE;
81 [ + + # # : 305822 : assert(index < CACHE_TREE_WIDTH);
# # ]
82 [ + + # # : 305822 : assert(tree->u.buffer[index] == NULL);
# # # # #
# # # #
# ]
83 [ # # # # : 305822 : tree->u.buffer[index] = buffer;
# # # # #
# ]
84 [ - + # # : 305822 : tree->present_mask |= (1ULL << index);
# # ]
85 : 305822 : return root;
86 : : }
87 : :
88 : : void
89 : 234385 : tree_remove_buffer(struct cache_tree *tree, struct cache_buffer *buffer)
90 : : {
91 : : struct cache_tree *child;
92 : : uint64_t index;
93 : :
94 [ - + # # : 234385 : index = CACHE_TREE_INDEX(tree->level, buffer->offset);
# # # # #
# # # # #
# # ]
95 : :
96 [ + + # # : 234385 : if (tree->level == 0) {
# # ]
97 [ + + # # : 117470 : assert(tree->u.buffer[index] != NULL);
# # # # #
# # # #
# ]
98 [ - + # # : 117470 : assert(buffer == tree->u.buffer[index]);
# # # # #
# # # #
# ]
99 [ - + # # : 117470 : tree->present_mask &= ~(1ULL << index);
# # ]
100 [ # # # # : 117470 : tree->u.buffer[index] = NULL;
# # # # #
# ]
101 : 117470 : cache_buffer_free(buffer);
102 : 117470 : return;
103 : : }
104 : :
105 [ # # # # : 116915 : child = tree->u.tree[index];
# # # # ]
106 [ + + # # ]: 116915 : assert(child != NULL);
107 : 116915 : tree_remove_buffer(child, buffer);
108 [ + + # # : 116915 : if (child->present_mask == 0) {
# # ]
109 [ - + # # : 1614 : tree->present_mask &= ~(1ULL << index);
# # ]
110 [ # # # # : 1614 : tree->u.tree[index] = NULL;
# # # # ]
111 : 1614 : free(child);
112 : 1 : }
113 : 6 : }
114 : :
115 : : void
116 : 9028 : tree_free_buffers(struct cache_tree *tree)
117 : : {
118 : : struct cache_buffer *buffer;
119 : : struct cache_tree *child;
120 : : uint32_t i;
121 : :
122 [ + + # # : 9028 : if (tree->present_mask == 0) {
# # ]
123 : 0 : return;
124 : : }
125 : :
126 [ + + # # : 9028 : if (tree->level == 0) {
# # ]
127 [ + + # # ]: 379990 : for (i = 0; i < CACHE_TREE_WIDTH; i++) {
128 [ # # # # : 374144 : buffer = tree->u.buffer[i];
# # # # #
# ]
129 [ + + + + : 374144 : if (buffer != NULL && buffer->in_progress == false &&
+ + # # #
# # # ]
130 [ + - # # : 188360 : buffer->bytes_filled == buffer->bytes_flushed) {
# # # # ]
131 : 188360 : cache_buffer_free(buffer);
132 [ # # # # : 188360 : tree->u.buffer[i] = NULL;
# # # # #
# ]
133 [ - + # # : 188360 : tree->present_mask &= ~(1ULL << i);
# # ]
134 : 16 : }
135 : 640 : }
136 : 10 : } else {
137 [ + + # # ]: 206830 : for (i = 0; i < CACHE_TREE_WIDTH; i++) {
138 [ # # # # : 203648 : child = tree->u.tree[i];
# # # # ]
139 [ + + ]: 203648 : if (child != NULL) {
140 : 5795 : tree_free_buffers(child);
141 [ + + # # : 5795 : if (child->present_mask == 0) {
# # ]
142 : 5761 : free(child);
143 [ # # # # : 5761 : tree->u.tree[i] = NULL;
# # # # ]
144 [ - + # # : 5761 : tree->present_mask &= ~(1ULL << i);
# # ]
145 : 9 : }
146 : 9 : }
147 : 384 : }
148 : : }
149 : 16 : }
|