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 6 : deprecation_init(void)
31 : {
32 6 : clock_gettime(CLOCK_MONOTONIC, &g_deprecation_epoch);
33 6 : }
34 :
35 : static inline uint64_t
36 7 : get_ns_since_epoch(void)
37 : {
38 7 : struct timespec now;
39 :
40 7 : clock_gettime(CLOCK_MONOTONIC, &now);
41 7 : return (now.tv_sec - g_deprecation_epoch.tv_sec) * SPDK_SEC_TO_NSEC +
42 7 : now.tv_nsec - g_deprecation_epoch.tv_nsec;
43 7 : }
44 :
45 : int
46 11 : 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 11 : struct spdk_deprecation *dep;
50 :
51 11 : assert(strnlen(tag, sizeof(dep->tag)) < sizeof(dep->tag));
52 11 : assert(strnlen(description, sizeof(dep->desc)) < sizeof(dep->desc));
53 11 : assert(strnlen(remove_release, sizeof(dep->remove)) < sizeof(dep->remove));
54 :
55 11 : dep = calloc(1, sizeof(*dep));
56 11 : if (dep == NULL) {
57 0 : return -ENOMEM;
58 : }
59 :
60 11 : snprintf((char *)dep->tag, sizeof(dep->tag), "%s", tag);
61 11 : snprintf((char *)dep->desc, sizeof(dep->desc), "%s", description);
62 11 : snprintf((char *)dep->remove, sizeof(dep->remove), "%s", remove_release);
63 11 : dep->interval = rate_limit_seconds * SPDK_SEC_TO_NSEC;
64 :
65 11 : TAILQ_INSERT_TAIL(&g_deprecations, dep, link);
66 11 : *depp = dep;
67 11 : return 0;
68 11 : }
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 7 : spdk_log_deprecated(struct spdk_deprecation *dep, const char *file, uint32_t line, const char *func)
78 : {
79 7 : uint64_t now = get_ns_since_epoch();
80 :
81 7 : 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 7 : dep->hits++;
88 :
89 7 : if (dep->interval != 0) {
90 3 : if (dep->last_log != 0 && now < dep->last_log + dep->interval) {
91 1 : dep->deferred++;
92 1 : return;
93 : }
94 2 : }
95 :
96 6 : dep->last_log = now;
97 :
98 12 : spdk_log(SPDK_LOG_WARN, file, line, func, "%s: deprecated feature %s to be removed in %s\n",
99 6 : dep->tag, dep->desc, dep->remove);
100 6 : if (dep->deferred != 0) {
101 1 : SPDK_WARNLOG("%s: %u messages suppressed\n", dep->tag, dep->deferred);
102 1 : dep->deferred = 0;
103 1 : }
104 7 : }
105 :
106 : int
107 1 : spdk_log_for_each_deprecation(void *ctx, spdk_log_for_each_deprecation_fn fn)
108 : {
109 1 : struct spdk_deprecation *dep;
110 1 : int rc = 0;
111 :
112 4 : TAILQ_FOREACH(dep, &g_deprecations, link) {
113 3 : rc = fn(ctx, dep);
114 3 : if (rc != 0) {
115 0 : break;
116 : }
117 3 : }
118 :
119 2 : return rc;
120 1 : }
121 :
122 : const char *
123 3 : spdk_deprecation_get_tag(const struct spdk_deprecation *deprecation)
124 : {
125 3 : return deprecation->tag;
126 : }
127 :
128 : const char *
129 3 : spdk_deprecation_get_description(const struct spdk_deprecation *deprecation)
130 : {
131 3 : return deprecation->desc;
132 : }
133 :
134 : const char *
135 3 : spdk_deprecation_get_remove_release(const struct spdk_deprecation *deprecation)
136 : {
137 3 : return deprecation->remove;
138 : }
139 :
140 : uint64_t
141 3 : spdk_deprecation_get_hits(const struct spdk_deprecation *deprecation)
142 : {
143 3 : return deprecation->hits;
144 : }
|