LCOV - code coverage report
Current view: top level - spdk/lib/blobfs - tree.c (source / functions) Hit Total Coverage
Test: Combined Lines: 90 91 98.9 %
Date: 2024-12-09 13:57:27 Functions: 5 5 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 72 442 16.3 %

           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                 :   31070398 : tree_find_buffer(struct cache_tree *tree, uint64_t offset)
      14                 :            : {
      15                 :            :         uint64_t index;
      16                 :            : 
      17         [ +  + ]:   61786169 :         while (tree != NULL) {
      18   [ -  +  -  +  :   60950772 :                 index = offset / CACHE_TREE_LEVEL_SIZE(tree->level);
          #  #  #  #  #  
                #  #  # ]
      19   [ +  +  #  # ]:   60950772 :                 if (index >= CACHE_TREE_WIDTH) {
      20                 :       5544 :                         return NULL;
      21                 :            :                 }
      22   [ +  +  #  #  :   60945228 :                 if (tree->level == 0) {
                   #  # ]
      23   [ #  #  #  #  :   30229457 :                         return tree->u.buffer[index];
          #  #  #  #  #  
                      # ]
      24                 :            :                 } else {
      25   [ -  +  #  #  :   30715771 :                         offset &= CACHE_TREE_LEVEL_MASK(tree->level);
          #  #  #  #  #  
                      # ]
      26   [ #  #  #  #  :   30715771 :                         tree = tree->u.tree[index];
             #  #  #  # ]
      27                 :            :                 }
      28                 :            :         }
      29                 :            : 
      30                 :     835397 :         return NULL;
      31                 :         46 : }
      32                 :            : 
      33                 :            : struct cache_buffer *
      34                 :   10967583 : tree_find_filled_buffer(struct cache_tree *tree, uint64_t offset)
      35                 :            : {
      36                 :            :         struct cache_buffer *buf;
      37                 :            : 
      38                 :   10967583 :         buf = tree_find_buffer(tree, offset);
      39   [ +  +  +  +  :   10967583 :         if (buf != NULL && buf->bytes_filled > 0) {
             #  #  #  # ]
      40                 :   10034232 :                 return buf;
      41                 :            :         } else {
      42                 :     933351 :                 return NULL;
      43                 :            :         }
      44                 :          3 : }
      45                 :            : 
      46                 :            : struct cache_tree *
      47                 :     385332 : tree_insert_buffer(struct cache_tree *root, struct cache_buffer *buffer)
      48                 :            : {
      49                 :            :         struct cache_tree *tree;
      50                 :            :         uint64_t index, offset;
      51                 :            : 
      52   [ #  #  #  # ]:     385332 :         offset = buffer->offset;
      53   [ +  +  +  +  :     386964 :         while (offset >= CACHE_TREE_LEVEL_SIZE(root->level + 1)) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      54   [ +  +  #  #  :       1632 :                 if (root->present_mask != 0) {
                   #  # ]
      55                 :        839 :                         tree = calloc(1, sizeof(*tree));
      56   [ +  +  #  # ]:        839 :                         assert(tree != NULL);
      57   [ #  #  #  #  :        839 :                         tree->level = root->level + 1;
          #  #  #  #  #  
                      # ]
      58   [ #  #  #  #  :        839 :                         tree->u.tree[0] = root;
             #  #  #  # ]
      59                 :        839 :                         root = tree;
      60   [ #  #  #  # ]:        839 :                         root->present_mask = 0x1ULL;
      61                 :          3 :                 } else {
      62         [ #  # ]:        793 :                         root->level++;
      63                 :            :                 }
      64                 :            :         }
      65                 :            : 
      66                 :     385332 :         tree = root;
      67   [ +  +  #  #  :     717981 :         while (tree->level > 0) {
                   #  # ]
      68   [ -  +  -  +  :     332649 :                 index = offset / CACHE_TREE_LEVEL_SIZE(tree->level);
          #  #  #  #  #  
                #  #  # ]
      69   [ +  +  #  #  :     332649 :                 assert(index < CACHE_TREE_WIDTH);
                   #  # ]
      70   [ -  +  #  #  :     332649 :                 offset &= CACHE_TREE_LEVEL_MASK(tree->level);
          #  #  #  #  #  
                      # ]
      71   [ +  +  #  #  :     332649 :                 if (tree->u.tree[index] == NULL) {
          #  #  #  #  #  
                      # ]
      72   [ #  #  #  #  :       7712 :                         tree->u.tree[index] = calloc(1, sizeof(*tree));
             #  #  #  # ]
      73   [ +  +  #  #  :       7712 :                         assert(tree->u.tree[index] != NULL);
          #  #  #  #  #  
                #  #  # ]
      74   [ #  #  #  #  :       7712 :                         tree->u.tree[index]->level = tree->level - 1;
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
      75   [ -  +  #  #  :       7712 :                         tree->present_mask |= (1ULL << index);
                   #  # ]
      76                 :          5 :                 }
      77   [ #  #  #  #  :     332649 :                 tree = tree->u.tree[index];
             #  #  #  # ]
      78                 :            :         }
      79                 :            : 
      80   [ #  #  #  # ]:     385332 :         index = offset / CACHE_BUFFER_SIZE;
      81   [ +  +  #  #  :     385332 :         assert(index < CACHE_TREE_WIDTH);
                   #  # ]
      82   [ +  +  #  #  :     385332 :         assert(tree->u.buffer[index] == NULL);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      83   [ #  #  #  #  :     385332 :         tree->u.buffer[index] = buffer;
          #  #  #  #  #  
                      # ]
      84   [ -  +  #  #  :     385332 :         tree->present_mask |= (1ULL << index);
                   #  # ]
      85                 :     385332 :         return root;
      86                 :            : }
      87                 :            : 
      88                 :            : void
      89                 :     309739 : tree_remove_buffer(struct cache_tree *tree, struct cache_buffer *buffer)
      90                 :            : {
      91                 :            :         struct cache_tree *child;
      92                 :            :         uint64_t index;
      93                 :            : 
      94   [ -  +  #  #  :     309739 :         index = CACHE_TREE_INDEX(tree->level, buffer->offset);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      95                 :            : 
      96   [ +  +  #  #  :     309739 :         if (tree->level == 0) {
                   #  # ]
      97   [ +  +  #  #  :     155348 :                 assert(tree->u.buffer[index] != NULL);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      98   [ -  +  #  #  :     155348 :                 assert(buffer == tree->u.buffer[index]);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      99   [ -  +  #  #  :     155348 :                 tree->present_mask &= ~(1ULL << index);
                   #  # ]
     100   [ #  #  #  #  :     155348 :                 tree->u.buffer[index] = NULL;
          #  #  #  #  #  
                      # ]
     101                 :     155348 :                 cache_buffer_free(buffer);
     102                 :     155348 :                 return;
     103                 :            :         }
     104                 :            : 
     105   [ #  #  #  #  :     154391 :         child = tree->u.tree[index];
             #  #  #  # ]
     106   [ +  +  #  # ]:     154391 :         assert(child != NULL);
     107                 :     154391 :         tree_remove_buffer(child, buffer);
     108   [ +  +  #  #  :     154391 :         if (child->present_mask == 0) {
                   #  # ]
     109   [ -  +  #  #  :       2116 :                 tree->present_mask &= ~(1ULL << index);
                   #  # ]
     110   [ #  #  #  #  :       2116 :                 tree->u.tree[index] = NULL;
             #  #  #  # ]
     111                 :       2116 :                 free(child);
     112                 :          1 :         }
     113                 :          6 : }
     114                 :            : 
     115                 :            : void
     116                 :       9843 : tree_free_buffers(struct cache_tree *tree)
     117                 :            : {
     118                 :            :         struct cache_buffer *buffer;
     119                 :            :         struct cache_tree *child;
     120                 :            :         uint32_t i;
     121                 :            : 
     122   [ +  +  #  #  :       9843 :         if (tree->present_mask == 0) {
                   #  # ]
     123                 :          0 :                 return;
     124                 :            :         }
     125                 :            : 
     126   [ +  +  #  #  :       9843 :         if (tree->level == 0) {
                   #  # ]
     127   [ +  +  #  # ]:     424645 :                 for (i = 0; i < CACHE_TREE_WIDTH; i++) {
     128   [ #  #  #  #  :     418112 :                         buffer = tree->u.buffer[i];
          #  #  #  #  #  
                      # ]
     129   [ +  +  +  +  :     418112 :                         if (buffer != NULL && buffer->in_progress == false &&
          +  +  #  #  #  
                #  #  # ]
     130   [ +  +  #  #  :     230000 :                             buffer->bytes_filled == buffer->bytes_flushed) {
             #  #  #  # ]
     131                 :     229992 :                                 cache_buffer_free(buffer);
     132   [ #  #  #  #  :     229992 :                                 tree->u.buffer[i] = NULL;
          #  #  #  #  #  
                      # ]
     133   [ -  +  #  #  :     229992 :                                 tree->present_mask &= ~(1ULL << i);
                   #  # ]
     134                 :         16 :                         }
     135                 :        640 :                 }
     136                 :         10 :         } else {
     137   [ +  +  #  # ]:     215150 :                 for (i = 0; i < CACHE_TREE_WIDTH; i++) {
     138   [ #  #  #  #  :     211840 :                         child = tree->u.tree[i];
             #  #  #  # ]
     139         [ +  + ]:     211840 :                         if (child != NULL) {
     140                 :       6476 :                                 tree_free_buffers(child);
     141   [ +  +  #  #  :       6476 :                                 if (child->present_mask == 0) {
                   #  # ]
     142                 :       6443 :                                         free(child);
     143   [ #  #  #  #  :       6443 :                                         tree->u.tree[i] = NULL;
             #  #  #  # ]
     144   [ -  +  #  #  :       6443 :                                         tree->present_mask &= ~(1ULL << i);
                   #  # ]
     145                 :          9 :                                 }
     146                 :          9 :                         }
     147                 :        384 :                 }
     148                 :            :         }
     149                 :         16 : }

Generated by: LCOV version 1.14