LCOV - code coverage report
Current view: top level - spdk/lib/log - log_deprecated.c (source / functions) Hit Total Coverage
Test: Combined Lines: 54 58 93.1 %
Date: 2024-12-07 05:23:13 Functions: 9 9 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 70 148 47.3 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       3                 :            :  */
       4                 :            : 
       5                 :            : #include "spdk/stdinc.h"
       6                 :            : #include "spdk/string.h"
       7                 :            : #include "spdk/thread.h"
       8                 :            : #include "spdk/util.h"
       9                 :            : #include "spdk/log.h"
      10                 :            : 
      11                 :            : struct spdk_deprecation {
      12                 :            :         const char tag[64];
      13                 :            :         const char desc[64];
      14                 :            :         const char remove[16];
      15                 :            :         TAILQ_ENTRY(spdk_deprecation) link;
      16                 :            :         uint64_t hits;
      17                 :            :         /* How often (nanoseconds) to log. */
      18                 :            :         uint64_t interval;
      19                 :            :         /* How many messages were not logged due to rate limiting */
      20                 :            :         uint32_t deferred;
      21                 :            :         /* CLOCK_MONOTONIC microseconds since g_deprecation_epoch when last warning was logged */
      22                 :            :         uint64_t last_log;
      23                 :            : };
      24                 :            : 
      25                 :            : static TAILQ_HEAD(, spdk_deprecation) g_deprecations = TAILQ_HEAD_INITIALIZER(g_deprecations);
      26                 :            : static struct timespec g_deprecation_epoch;
      27                 :            : 
      28                 :            : static void
      29                 :            : __attribute__((constructor))
      30                 :       2926 : deprecation_init(void)
      31                 :            : {
      32         [ +  + ]:       2926 :         clock_gettime(CLOCK_MONOTONIC, &g_deprecation_epoch);
      33                 :       2926 : }
      34                 :            : 
      35                 :            : static inline uint64_t
      36                 :         78 : get_ns_since_epoch(void)
      37                 :            : {
      38                 :         42 :         struct timespec now;
      39                 :            : 
      40         [ +  + ]:         78 :         clock_gettime(CLOCK_MONOTONIC, &now);
      41         [ +  - ]:         96 :         return (now.tv_sec - g_deprecation_epoch.tv_sec) * SPDK_SEC_TO_NSEC +
      42   [ +  -  +  -  :         87 :                now.tv_nsec - g_deprecation_epoch.tv_nsec;
                   +  - ]
      43                 :            : }
      44                 :            : 
      45                 :            : int
      46                 :      18075 : spdk_log_deprecation_register(const char *tag, const char *description, const char *remove_release,
      47                 :            :                               uint32_t rate_limit_seconds, struct spdk_deprecation **depp)
      48                 :            : {
      49                 :            :         struct spdk_deprecation *dep;
      50                 :            : 
      51   [ +  +  +  +  :      18075 :         assert(strnlen(tag, sizeof(dep->tag)) < sizeof(dep->tag));
                   #  # ]
      52   [ +  +  +  +  :      18075 :         assert(strnlen(description, sizeof(dep->desc)) < sizeof(dep->desc));
                   #  # ]
      53   [ +  +  +  +  :      18075 :         assert(strnlen(remove_release, sizeof(dep->remove)) < sizeof(dep->remove));
                   #  # ]
      54                 :            : 
      55                 :      18075 :         dep = calloc(1, sizeof(*dep));
      56         [ +  + ]:      18075 :         if (dep == NULL) {
      57                 :          0 :                 return -ENOMEM;
      58                 :            :         }
      59                 :            : 
      60         [ +  - ]:      18075 :         snprintf((char *)dep->tag, sizeof(dep->tag), "%s", tag);
      61         [ +  - ]:      18075 :         snprintf((char *)dep->desc, sizeof(dep->desc), "%s", description);
      62         [ +  - ]:      18075 :         snprintf((char *)dep->remove, sizeof(dep->remove), "%s", remove_release);
      63   [ +  -  +  - ]:      18075 :         dep->interval = rate_limit_seconds * SPDK_SEC_TO_NSEC;
      64                 :            : 
      65   [ +  -  +  -  :      18075 :         TAILQ_INSERT_TAIL(&g_deprecations, dep, link);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
      66         [ +  - ]:      18075 :         *depp = dep;
      67                 :      18075 :         return 0;
      68                 :        862 : }
      69                 :            : 
      70                 :            : /*
      71                 :            :  * There is potential for races between pthreads leading to over or under reporting of times that
      72                 :            :  * deprecated code was hit. If this function is called in a hot path where that is likely, we care
      73                 :            :  * more about performance than accuracy of the error counts. The important thing is that at least
      74                 :            :  * one of the racing updates the hits counter to non-zero and the warning is logged at least once.
      75                 :            :  */
      76                 :            : void
      77                 :         78 : spdk_log_deprecated(struct spdk_deprecation *dep, const char *file, uint32_t line, const char *func)
      78                 :            : {
      79                 :         78 :         uint64_t now = get_ns_since_epoch();
      80                 :            : 
      81         [ +  + ]:         78 :         if (dep == NULL) {
      82                 :          0 :                 SPDK_ERRLOG("NULL deprecation passed from %s:%u:%s\n", file, line, func);
      83         [ #  # ]:          0 :                 assert(false);
      84                 :            :                 return;
      85                 :            :         }
      86                 :            : 
      87         [ +  - ]:         78 :         dep->hits++;
      88                 :            : 
      89   [ +  +  +  -  :         78 :         if (dep->interval != 0) {
                   +  - ]
      90   [ +  +  +  +  :         12 :                 if (dep->last_log != 0 && now < dep->last_log + dep->interval) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      91         [ #  # ]:          4 :                         dep->deferred++;
      92                 :          4 :                         return;
      93                 :            :                 }
      94                 :          2 :         }
      95                 :            : 
      96   [ +  -  +  - ]:         74 :         dep->last_log = now;
      97                 :            : 
      98                 :         84 :         spdk_log(SPDK_LOG_WARN, file, line, func, "%s: deprecated feature %s to be removed in %s\n",
      99   [ +  -  +  -  :         74 :                  dep->tag, dep->desc, dep->remove);
                   +  - ]
     100   [ +  +  +  -  :         74 :         if (dep->deferred != 0) {
                   +  - ]
     101   [ #  #  #  #  :          4 :                 SPDK_WARNLOG("%s: %u messages suppressed\n", dep->tag, dep->deferred);
                   #  # ]
     102   [ #  #  #  # ]:          4 :                 dep->deferred = 0;
     103                 :          1 :         }
     104                 :         11 : }
     105                 :            : 
     106                 :            : int
     107                 :       2299 : spdk_log_for_each_deprecation(void *ctx, spdk_log_for_each_deprecation_fn fn)
     108                 :            : {
     109                 :            :         struct spdk_deprecation *dep;
     110                 :       2299 :         int rc = 0;
     111                 :            : 
     112   [ +  +  +  -  :      17615 :         TAILQ_FOREACH(dep, &g_deprecations, link) {
             +  -  +  - ]
     113   [ -  +  +  - ]:      15316 :                 rc = fn(ctx, dep);
     114         [ -  + ]:      15316 :                 if (rc != 0) {
     115                 :          0 :                         break;
     116                 :            :                 }
     117                 :        740 :         }
     118                 :            : 
     119                 :       2299 :         return rc;
     120                 :            : }
     121                 :            : 
     122                 :            : const char *
     123                 :         12 : spdk_deprecation_get_tag(const struct spdk_deprecation *deprecation)
     124                 :            : {
     125         [ #  # ]:         12 :         return deprecation->tag;
     126                 :            : }
     127                 :            : 
     128                 :            : const char *
     129                 :         12 : spdk_deprecation_get_description(const struct spdk_deprecation *deprecation)
     130                 :            : {
     131         [ #  # ]:         12 :         return deprecation->desc;
     132                 :            : }
     133                 :            : 
     134                 :            : const char *
     135                 :         12 : spdk_deprecation_get_remove_release(const struct spdk_deprecation *deprecation)
     136                 :            : {
     137         [ #  # ]:         12 :         return deprecation->remove;
     138                 :            : }
     139                 :            : 
     140                 :            : uint64_t
     141                 :      15316 : spdk_deprecation_get_hits(const struct spdk_deprecation *deprecation)
     142                 :            : {
     143   [ +  -  +  - ]:      15316 :         return deprecation->hits;
     144                 :            : }

Generated by: LCOV version 1.15