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-11-19 02:26:41 Functions: 5 5 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 63 442 14.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 "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                 :   82983203 : tree_find_buffer(struct cache_tree *tree, uint64_t offset)
      18                 :            : {
      19                 :            :         uint64_t index;
      20                 :            : 
      21         [ +  + ]:  166583008 :         while (tree != NULL) {
      22   [ -  +  -  +  :  163597685 :                 index = offset / CACHE_TREE_LEVEL_SIZE(tree->level);
          #  #  #  #  #  
                #  #  # ]
      23   [ +  +  #  # ]:  163597685 :                 if (index >= CACHE_TREE_WIDTH) {
      24                 :      28301 :                         return NULL;
      25                 :            :                 }
      26   [ +  +  #  #  :  163569384 :                 if (tree->level == 0) {
                   #  # ]
      27   [ #  #  #  #  :   79969579 :                         return tree->u.buffer[index];
          #  #  #  #  #  
                      # ]
      28                 :            :                 } else {
      29   [ -  +  #  #  :   83599805 :                         offset &= CACHE_TREE_LEVEL_MASK(tree->level);
          #  #  #  #  #  
                      # ]
      30   [ #  #  #  #  :   83599805 :                         tree = tree->u.tree[index];
             #  #  #  # ]
      31                 :            :                 }
      32                 :            :         }
      33                 :            : 
      34                 :    2985323 :         return NULL;
      35                 :          0 : }
      36                 :            : 
      37                 :            : struct cache_buffer *
      38                 :   29975221 : tree_find_filled_buffer(struct cache_tree *tree, uint64_t offset)
      39                 :            : {
      40                 :            :         struct cache_buffer *buf;
      41                 :            : 
      42                 :   29975221 :         buf = tree_find_buffer(tree, offset);
      43   [ +  +  +  +  :   29975221 :         if (buf != NULL && buf->bytes_filled > 0) {
             #  #  #  # ]
      44                 :   26516908 :                 return buf;
      45                 :            :         } else {
      46                 :    3458313 :                 return NULL;
      47                 :            :         }
      48                 :          0 : }
      49                 :            : 
      50                 :            : struct cache_tree *
      51                 :    1179499 : tree_insert_buffer(struct cache_tree *root, struct cache_buffer *buffer)
      52                 :            : {
      53                 :            :         struct cache_tree *tree;
      54                 :            :         uint64_t index, offset;
      55                 :            : 
      56   [ #  #  #  # ]:    1179499 :         offset = buffer->offset;
      57   [ +  +  +  +  :    1185343 :         while (offset >= CACHE_TREE_LEVEL_SIZE(root->level + 1)) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      58   [ +  +  #  #  :       5844 :                 if (root->present_mask != 0) {
                   #  # ]
      59                 :       2658 :                         tree = calloc(1, sizeof(*tree));
      60   [ -  +  #  # ]:       2658 :                         assert(tree != NULL);
      61   [ #  #  #  #  :       2658 :                         tree->level = root->level + 1;
          #  #  #  #  #  
                      # ]
      62   [ #  #  #  #  :       2658 :                         tree->u.tree[0] = root;
             #  #  #  # ]
      63                 :       2658 :                         root = tree;
      64   [ #  #  #  # ]:       2658 :                         root->present_mask = 0x1ULL;
      65                 :          0 :                 } else {
      66         [ #  # ]:       3186 :                         root->level++;
      67                 :            :                 }
      68                 :            :         }
      69                 :            : 
      70                 :    1179499 :         tree = root;
      71   [ +  +  #  #  :    2194647 :         while (tree->level > 0) {
                   #  # ]
      72   [ -  +  -  +  :    1015148 :                 index = offset / CACHE_TREE_LEVEL_SIZE(tree->level);
          #  #  #  #  #  
                #  #  # ]
      73   [ -  +  #  #  :    1015148 :                 assert(index < CACHE_TREE_WIDTH);
                   #  # ]
      74   [ -  +  #  #  :    1015148 :                 offset &= CACHE_TREE_LEVEL_MASK(tree->level);
          #  #  #  #  #  
                      # ]
      75   [ +  +  #  #  :    1015148 :                 if (tree->u.tree[index] == NULL) {
          #  #  #  #  #  
                      # ]
      76   [ #  #  #  #  :      23432 :                         tree->u.tree[index] = calloc(1, sizeof(*tree));
             #  #  #  # ]
      77   [ -  +  #  #  :      23432 :                         assert(tree->u.tree[index] != NULL);
          #  #  #  #  #  
                #  #  # ]
      78   [ #  #  #  #  :      23432 :                         tree->u.tree[index]->level = tree->level - 1;
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
      79   [ -  +  #  #  :      23432 :                         tree->present_mask |= (1ULL << index);
                   #  # ]
      80                 :          0 :                 }
      81   [ #  #  #  #  :    1015148 :                 tree = tree->u.tree[index];
             #  #  #  # ]
      82                 :            :         }
      83                 :            : 
      84   [ #  #  #  # ]:    1179499 :         index = offset / CACHE_BUFFER_SIZE;
      85   [ -  +  #  #  :    1179499 :         assert(index < CACHE_TREE_WIDTH);
                   #  # ]
      86   [ -  +  #  #  :    1179499 :         assert(tree->u.buffer[index] == NULL);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      87   [ #  #  #  #  :    1179499 :         tree->u.buffer[index] = buffer;
          #  #  #  #  #  
                      # ]
      88   [ -  +  #  #  :    1179499 :         tree->present_mask |= (1ULL << index);
                   #  # ]
      89                 :    1179499 :         return root;
      90                 :            : }
      91                 :            : 
      92                 :            : void
      93                 :     830622 : tree_remove_buffer(struct cache_tree *tree, struct cache_buffer *buffer)
      94                 :            : {
      95                 :            :         struct cache_tree *child;
      96                 :            :         uint64_t index;
      97                 :            : 
      98   [ -  +  #  #  :     830622 :         index = CACHE_TREE_INDEX(tree->level, buffer->offset);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      99                 :            : 
     100   [ +  +  #  #  :     830622 :         if (tree->level == 0) {
                   #  # ]
     101   [ -  +  #  #  :     412597 :                 assert(tree->u.buffer[index] != NULL);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     102   [ -  +  #  #  :     412597 :                 assert(buffer == tree->u.buffer[index]);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     103   [ -  +  #  #  :     412597 :                 tree->present_mask &= ~(1ULL << index);
                   #  # ]
     104   [ #  #  #  #  :     412597 :                 tree->u.buffer[index] = NULL;
          #  #  #  #  #  
                      # ]
     105                 :     412597 :                 cache_buffer_free(buffer);
     106                 :     412597 :                 return;
     107                 :            :         }
     108                 :            : 
     109   [ #  #  #  #  :     418025 :         child = tree->u.tree[index];
             #  #  #  # ]
     110   [ -  +  #  # ]:     418025 :         assert(child != NULL);
     111                 :     418025 :         tree_remove_buffer(child, buffer);
     112   [ +  +  #  #  :     418025 :         if (child->present_mask == 0) {
                   #  # ]
     113   [ -  +  #  #  :       5694 :                 tree->present_mask &= ~(1ULL << index);
                   #  # ]
     114   [ #  #  #  #  :       5694 :                 tree->u.tree[index] = NULL;
             #  #  #  # ]
     115                 :       5694 :                 free(child);
     116                 :          0 :         }
     117                 :          0 : }
     118                 :            : 
     119                 :            : void
     120                 :      30509 : tree_free_buffers(struct cache_tree *tree)
     121                 :            : {
     122                 :            :         struct cache_buffer *buffer;
     123                 :            :         struct cache_tree *child;
     124                 :            :         uint32_t i;
     125                 :            : 
     126   [ -  +  #  #  :      30509 :         if (tree->present_mask == 0) {
                   #  # ]
     127                 :          0 :                 return;
     128                 :            :         }
     129                 :            : 
     130   [ +  +  #  #  :      30509 :         if (tree->level == 0) {
                   #  # ]
     131   [ +  +  #  # ]:    1326325 :                 for (i = 0; i < CACHE_TREE_WIDTH; i++) {
     132   [ #  #  #  #  :    1305920 :                         buffer = tree->u.buffer[i];
          #  #  #  #  #  
                      # ]
     133   [ +  +  +  +  :    1305920 :                         if (buffer != NULL && buffer->in_progress == false &&
          +  +  #  #  #  
                #  #  # ]
     134   [ +  -  #  #  :     766908 :                             buffer->bytes_filled == buffer->bytes_flushed) {
             #  #  #  # ]
     135                 :     766908 :                                 cache_buffer_free(buffer);
     136   [ #  #  #  #  :     766908 :                                 tree->u.buffer[i] = NULL;
          #  #  #  #  #  
                      # ]
     137   [ -  +  #  #  :     766908 :                                 tree->present_mask &= ~(1ULL << i);
                   #  # ]
     138                 :          0 :                         }
     139                 :          0 :                 }
     140                 :          0 :         } else {
     141   [ +  +  #  # ]:     656760 :                 for (i = 0; i < CACHE_TREE_WIDTH; i++) {
     142   [ #  #  #  #  :     646656 :                         child = tree->u.tree[i];
             #  #  #  # ]
     143         [ +  + ]:     646656 :                         if (child != NULL) {
     144                 :      20444 :                                 tree_free_buffers(child);
     145   [ +  +  #  #  :      20444 :                                 if (child->present_mask == 0) {
                   #  # ]
     146                 :      20402 :                                         free(child);
     147   [ #  #  #  #  :      20402 :                                         tree->u.tree[i] = NULL;
             #  #  #  # ]
     148   [ -  +  #  #  :      20402 :                                         tree->present_mask &= ~(1ULL << i);
                   #  # ]
     149                 :          0 :                                 }
     150                 :          0 :                         }
     151                 :          0 :                 }
     152                 :            :         }
     153                 :          0 : }

Generated by: LCOV version 1.15