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 "spdk/blobfs.h"
9 : : #include "cache_tree.h"
10 : :
11 : : #include "spdk/queue.h"
12 : : #include "spdk/assert.h"
13 : : #include "spdk/env.h"
14 : : #include "spdk/log.h"
15 : :
16 : : struct cache_buffer *
17 : 68021547 : tree_find_buffer(struct cache_tree *tree, uint64_t offset)
18 : : {
19 : 46 : uint64_t index;
20 : :
21 [ + + ]: 137854916 : while (tree != NULL) {
22 [ + + + + : 133790841 : index = offset / CACHE_TREE_LEVEL_SIZE(tree->level);
+ - + - +
- + - ]
23 [ + + + - ]: 133790841 : if (index >= CACHE_TREE_WIDTH) {
24 : 26981 : return NULL;
25 : : }
26 [ + + + - : 133763860 : if (tree->level == 0) {
+ + ]
27 [ - + - + : 63930491 : return tree->u.buffer[index];
- + - + -
+ ]
28 : : } else {
29 [ + + + - : 69833369 : offset &= CACHE_TREE_LEVEL_MASK(tree->level);
+ - + - +
- ]
30 [ + - + - : 69833369 : tree = tree->u.tree[index];
+ - + - ]
31 : : }
32 : : }
33 : :
34 : 4064075 : return NULL;
35 : 46 : }
36 : :
37 : : struct cache_buffer *
38 : 25553085 : tree_find_filled_buffer(struct cache_tree *tree, uint64_t offset)
39 : : {
40 : 3 : struct cache_buffer *buf;
41 : :
42 : 25553085 : buf = tree_find_buffer(tree, offset);
43 [ + + + + : 25553085 : if (buf != NULL && buf->bytes_filled > 0) {
+ - + + ]
44 : 21276225 : return buf;
45 : : } else {
46 : 4276860 : return NULL;
47 : : }
48 : 3 : }
49 : :
50 : : struct cache_tree *
51 : 1013047 : tree_insert_buffer(struct cache_tree *root, struct cache_buffer *buffer)
52 : : {
53 : 16 : struct cache_tree *tree;
54 : 16 : uint64_t index, offset;
55 : :
56 [ + - + - ]: 1013047 : offset = buffer->offset;
57 [ + + + + : 1018322 : while (offset >= CACHE_TREE_LEVEL_SIZE(root->level + 1)) {
+ - + - +
- + - +
+ ]
58 [ + + + - : 5275 : if (root->present_mask != 0) {
+ - ]
59 : 2347 : tree = calloc(1, sizeof(*tree));
60 [ + + # # ]: 2347 : assert(tree != NULL);
61 [ + - + - : 2347 : tree->level = root->level + 1;
+ - + - +
- ]
62 [ + - + - : 2347 : tree->u.tree[0] = root;
+ - + - ]
63 : 2347 : root = tree;
64 [ + - + - ]: 2347 : root->present_mask = 0x1ULL;
65 : 3 : } else {
66 [ # # ]: 2928 : root->level++;
67 : : }
68 : : }
69 : :
70 : 1013047 : tree = root;
71 [ + + + - : 1891655 : while (tree->level > 0) {
+ + ]
72 [ + + + + : 878608 : index = offset / CACHE_TREE_LEVEL_SIZE(tree->level);
+ - + - +
- + - ]
73 [ + + + - : 878608 : assert(index < CACHE_TREE_WIDTH);
# # ]
74 [ + + + - : 878608 : offset &= CACHE_TREE_LEVEL_MASK(tree->level);
+ - + - +
- ]
75 [ + + + - : 878608 : if (tree->u.tree[index] == NULL) {
+ - + - -
+ ]
76 [ + - + - : 18886 : tree->u.tree[index] = calloc(1, sizeof(*tree));
+ - + - ]
77 [ + + + - : 18886 : assert(tree->u.tree[index] != NULL);
+ - + - +
- # # ]
78 [ + - + - : 18886 : tree->u.tree[index]->level = tree->level - 1;
+ - + - +
- + - + -
+ - + - ]
79 [ + + + - : 18886 : tree->present_mask |= (1ULL << index);
+ - ]
80 : 5 : }
81 [ + - + - : 878608 : tree = tree->u.tree[index];
+ - + - ]
82 : : }
83 : :
84 [ + - + - ]: 1013047 : index = offset / CACHE_BUFFER_SIZE;
85 [ + + + - : 1013047 : assert(index < CACHE_TREE_WIDTH);
# # ]
86 [ + + + - : 1013047 : assert(tree->u.buffer[index] == NULL);
+ - + - +
- + - #
# ]
87 [ + - + - : 1013047 : tree->u.buffer[index] = buffer;
+ - + - +
- ]
88 [ + + + - : 1013047 : tree->present_mask |= (1ULL << index);
+ - ]
89 : 1013063 : return root;
90 : 16 : }
91 : :
92 : : void
93 : 675415 : tree_remove_buffer(struct cache_tree *tree, struct cache_buffer *buffer)
94 : : {
95 : 6 : struct cache_tree *child;
96 : 6 : uint64_t index;
97 : :
98 [ + + + - : 675415 : index = CACHE_TREE_INDEX(tree->level, buffer->offset);
+ - + - +
- + - + -
+ - ]
99 : :
100 [ + + + - : 675415 : if (tree->level == 0) {
+ + ]
101 [ + + + - : 331570 : assert(tree->u.buffer[index] != NULL);
+ - + - +
- + - #
# ]
102 [ + + + - : 331570 : assert(buffer == tree->u.buffer[index]);
+ - + - +
- + - #
# ]
103 [ + + + - : 331570 : tree->present_mask &= ~(1ULL << index);
+ - ]
104 [ + - + - : 331570 : tree->u.buffer[index] = NULL;
+ - + - +
- ]
105 : 331570 : cache_buffer_free(buffer);
106 : 331570 : return;
107 : : }
108 : :
109 [ + - + - : 343845 : child = tree->u.tree[index];
+ - + - ]
110 [ + + # # ]: 343845 : assert(child != NULL);
111 : 343845 : tree_remove_buffer(child, buffer);
112 [ + + + - : 343845 : if (child->present_mask == 0) {
+ + ]
113 [ + + + - : 4792 : tree->present_mask &= ~(1ULL << index);
+ - ]
114 [ + - + - : 4792 : tree->u.tree[index] = NULL;
+ - + - ]
115 : 4792 : free(child);
116 : 1 : }
117 [ - + ]: 6 : }
118 : :
119 : : void
120 : 24012 : tree_free_buffers(struct cache_tree *tree)
121 : : {
122 : 16 : struct cache_buffer *buffer;
123 : 16 : struct cache_tree *child;
124 : 16 : uint32_t i;
125 : :
126 [ + + + - : 24012 : if (tree->present_mask == 0) {
+ - ]
127 : 0 : return;
128 : : }
129 : :
130 [ + + + - : 24012 : if (tree->level == 0) {
+ + ]
131 [ + + + + ]: 1077635 : for (i = 0; i < CACHE_TREE_WIDTH; i++) {
132 [ + - + - : 1061056 : buffer = tree->u.buffer[i];
+ - + - +
- ]
133 [ + + + + : 1061072 : if (buffer != NULL && buffer->in_progress == false &&
+ + + - +
- - + ]
134 [ + + + - : 682162 : buffer->bytes_filled == buffer->bytes_flushed) {
+ - + - ]
135 : 681489 : cache_buffer_free(buffer);
136 [ + - + - : 681489 : tree->u.buffer[i] = NULL;
+ - + - +
- ]
137 [ + + + - : 681489 : tree->present_mask &= ~(1ULL << i);
+ - ]
138 : 16 : }
139 : 640 : }
140 : 10 : } else {
141 [ + + + + ]: 483145 : for (i = 0; i < CACHE_TREE_WIDTH; i++) {
142 [ + - + - : 475712 : child = tree->u.tree[i];
+ - + - ]
143 [ + + ]: 475712 : if (child != NULL) {
144 : 16555 : tree_free_buffers(child);
145 [ + + + - : 16555 : if (child->present_mask == 0) {
- + ]
146 : 16453 : free(child);
147 [ + - + - : 16453 : tree->u.tree[i] = NULL;
+ - + - ]
148 [ + + + - : 16453 : tree->present_mask &= ~(1ULL << i);
+ - ]
149 : 9 : }
150 : 9 : }
151 : 384 : }
152 : : }
153 [ - + ]: 16 : }
|