LCOV - code coverage report
Current view: top level - spdk/lib/blobfs - tree.c (source / functions) Hit Total Coverage
Test: Combined Lines: 77 91 84.6 %
Date: 2024-12-09 16:12:14 Functions: 5 5 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 64 442 14.5 %

           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                 :   19981676 : tree_find_buffer(struct cache_tree *tree, uint64_t offset)
      14                 :            : {
      15                 :            :         uint64_t index;
      16                 :            : 
      17         [ +  + ]:   39660640 :         while (tree != NULL) {
      18   [ -  +  -  +  :   38716063 :                 index = offset / CACHE_TREE_LEVEL_SIZE(tree->level);
          #  #  #  #  #  
                #  #  # ]
      19   [ +  +  #  # ]:   38716063 :                 if (index >= CACHE_TREE_WIDTH) {
      20                 :       5007 :                         return NULL;
      21                 :            :                 }
      22   [ +  +  #  #  :   38711056 :                 if (tree->level == 0) {
                   #  # ]
      23   [ #  #  #  #  :   19032092 :                         return tree->u.buffer[index];
          #  #  #  #  #  
                      # ]
      24                 :            :                 } else {
      25   [ -  +  #  #  :   19678964 :                         offset &= CACHE_TREE_LEVEL_MASK(tree->level);
          #  #  #  #  #  
                      # ]
      26   [ #  #  #  #  :   19678964 :                         tree = tree->u.tree[index];
             #  #  #  # ]
      27                 :            :                 }
      28                 :            :         }
      29                 :            : 
      30                 :     944577 :         return NULL;
      31                 :          0 : }
      32                 :            : 
      33                 :            : struct cache_buffer *
      34                 :    7430039 : tree_find_filled_buffer(struct cache_tree *tree, uint64_t offset)
      35                 :            : {
      36                 :            :         struct cache_buffer *buf;
      37                 :            : 
      38                 :    7430039 :         buf = tree_find_buffer(tree, offset);
      39   [ +  +  +  +  :    7430039 :         if (buf != NULL && buf->bytes_filled > 0) {
             #  #  #  # ]
      40                 :    6346957 :                 return buf;
      41                 :            :         } else {
      42                 :    1083082 :                 return NULL;
      43                 :            :         }
      44                 :          0 : }
      45                 :            : 
      46                 :            : struct cache_tree *
      47                 :     281365 : tree_insert_buffer(struct cache_tree *root, struct cache_buffer *buffer)
      48                 :            : {
      49                 :            :         struct cache_tree *tree;
      50                 :            :         uint64_t index, offset;
      51                 :            : 
      52   [ #  #  #  # ]:     281365 :         offset = buffer->offset;
      53   [ +  +  +  +  :     282650 :         while (offset >= CACHE_TREE_LEVEL_SIZE(root->level + 1)) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      54   [ +  +  #  #  :       1285 :                 if (root->present_mask != 0) {
                   #  # ]
      55                 :        641 :                         tree = calloc(1, sizeof(*tree));
      56   [ -  +  #  # ]:        641 :                         assert(tree != NULL);
      57   [ #  #  #  #  :        641 :                         tree->level = root->level + 1;
          #  #  #  #  #  
                      # ]
      58   [ #  #  #  #  :        641 :                         tree->u.tree[0] = root;
             #  #  #  # ]
      59                 :        641 :                         root = tree;
      60   [ #  #  #  # ]:        641 :                         root->present_mask = 0x1ULL;
      61                 :          0 :                 } else {
      62         [ #  # ]:        644 :                         root->level++;
      63                 :            :                 }
      64                 :            :         }
      65                 :            : 
      66                 :     281365 :         tree = root;
      67   [ +  +  #  #  :     521453 :         while (tree->level > 0) {
                   #  # ]
      68   [ -  +  -  +  :     240088 :                 index = offset / CACHE_TREE_LEVEL_SIZE(tree->level);
          #  #  #  #  #  
                #  #  # ]
      69   [ -  +  #  #  :     240088 :                 assert(index < CACHE_TREE_WIDTH);
                   #  # ]
      70   [ -  +  #  #  :     240088 :                 offset &= CACHE_TREE_LEVEL_MASK(tree->level);
          #  #  #  #  #  
                      # ]
      71   [ +  +  #  #  :     240088 :                 if (tree->u.tree[index] == NULL) {
          #  #  #  #  #  
                      # ]
      72   [ #  #  #  #  :       6968 :                         tree->u.tree[index] = calloc(1, sizeof(*tree));
             #  #  #  # ]
      73   [ -  +  #  #  :       6968 :                         assert(tree->u.tree[index] != NULL);
          #  #  #  #  #  
                #  #  # ]
      74   [ #  #  #  #  :       6968 :                         tree->u.tree[index]->level = tree->level - 1;
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
      75   [ -  +  #  #  :       6968 :                         tree->present_mask |= (1ULL << index);
                   #  # ]
      76                 :          0 :                 }
      77   [ #  #  #  #  :     240088 :                 tree = tree->u.tree[index];
             #  #  #  # ]
      78                 :            :         }
      79                 :            : 
      80   [ #  #  #  # ]:     281365 :         index = offset / CACHE_BUFFER_SIZE;
      81   [ -  +  #  #  :     281365 :         assert(index < CACHE_TREE_WIDTH);
                   #  # ]
      82   [ -  +  #  #  :     281365 :         assert(tree->u.buffer[index] == NULL);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      83   [ #  #  #  #  :     281365 :         tree->u.buffer[index] = buffer;
          #  #  #  #  #  
                      # ]
      84   [ -  +  #  #  :     281365 :         tree->present_mask |= (1ULL << index);
                   #  # ]
      85                 :     281365 :         return root;
      86                 :            : }
      87                 :            : 
      88                 :            : void
      89                 :     195861 : tree_remove_buffer(struct cache_tree *tree, struct cache_buffer *buffer)
      90                 :            : {
      91                 :            :         struct cache_tree *child;
      92                 :            :         uint64_t index;
      93                 :            : 
      94   [ -  +  #  #  :     195861 :         index = CACHE_TREE_INDEX(tree->level, buffer->offset);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      95                 :            : 
      96   [ +  +  #  #  :     195861 :         if (tree->level == 0) {
                   #  # ]
      97   [ -  +  #  #  :      98243 :                 assert(tree->u.buffer[index] != NULL);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      98   [ -  +  #  #  :      98243 :                 assert(buffer == tree->u.buffer[index]);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      99   [ -  +  #  #  :      98243 :                 tree->present_mask &= ~(1ULL << index);
                   #  # ]
     100   [ #  #  #  #  :      98243 :                 tree->u.buffer[index] = NULL;
          #  #  #  #  #  
                      # ]
     101                 :      98243 :                 cache_buffer_free(buffer);
     102                 :      98243 :                 return;
     103                 :            :         }
     104                 :            : 
     105   [ #  #  #  #  :      97618 :         child = tree->u.tree[index];
             #  #  #  # ]
     106   [ -  +  #  # ]:      97618 :         assert(child != NULL);
     107                 :      97618 :         tree_remove_buffer(child, buffer);
     108   [ +  +  #  #  :      97618 :         if (child->present_mask == 0) {
                   #  # ]
     109   [ -  +  #  #  :       1340 :                 tree->present_mask &= ~(1ULL << index);
                   #  # ]
     110   [ #  #  #  #  :       1340 :                 tree->u.tree[index] = NULL;
             #  #  #  # ]
     111                 :       1340 :                 free(child);
     112                 :          0 :         }
     113                 :          0 : }
     114                 :            : 
     115                 :            : void
     116                 :      10108 : tree_free_buffers(struct cache_tree *tree)
     117                 :            : {
     118                 :            :         struct cache_buffer *buffer;
     119                 :            :         struct cache_tree *child;
     120                 :            :         uint32_t i;
     121                 :            : 
     122   [ -  +  #  #  :      10108 :         if (tree->present_mask == 0) {
                   #  # ]
     123                 :          0 :                 return;
     124                 :            :         }
     125                 :            : 
     126   [ +  +  #  #  :      10108 :         if (tree->level == 0) {
                   #  # ]
     127   [ +  +  #  # ]:     414245 :                 for (i = 0; i < CACHE_TREE_WIDTH; i++) {
     128   [ #  #  #  #  :     407872 :                         buffer = tree->u.buffer[i];
          #  #  #  #  #  
                      # ]
     129   [ +  +  +  +  :     407872 :                         if (buffer != NULL && buffer->in_progress == false &&
          +  +  #  #  #  
                #  #  # ]
     130   [ +  +  #  #  :     183133 :                             buffer->bytes_filled == buffer->bytes_flushed) {
             #  #  #  # ]
     131                 :     183128 :                                 cache_buffer_free(buffer);
     132   [ #  #  #  #  :     183128 :                                 tree->u.buffer[i] = NULL;
          #  #  #  #  #  
                      # ]
     133   [ -  +  #  #  :     183128 :                                 tree->present_mask &= ~(1ULL << i);
                   #  # ]
     134                 :          0 :                         }
     135                 :          0 :                 }
     136                 :          0 :         } else {
     137   [ +  +  #  # ]:     242775 :                 for (i = 0; i < CACHE_TREE_WIDTH; i++) {
     138   [ #  #  #  #  :     239040 :                         child = tree->u.tree[i];
             #  #  #  # ]
     139         [ +  + ]:     239040 :                         if (child != NULL) {
     140                 :       6288 :                                 tree_free_buffers(child);
     141   [ +  +  #  #  :       6288 :                                 if (child->present_mask == 0) {
                   #  # ]
     142                 :       6275 :                                         free(child);
     143   [ #  #  #  #  :       6275 :                                         tree->u.tree[i] = NULL;
             #  #  #  # ]
     144   [ -  +  #  #  :       6275 :                                         tree->present_mask &= ~(1ULL << i);
                   #  # ]
     145                 :          0 :                                 }
     146                 :          0 :                         }
     147                 :          0 :                 }
     148                 :            :         }
     149                 :          0 : }

Generated by: LCOV version 1.14