Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2023 Intel Corporation.
3 : : * All rights reserved.
4 : : */
5 : : #include "spdk/stdinc.h"
6 : :
7 : : #include "spdk_internal/mock.h"
8 : :
9 : : #include "spdk/thread.h"
10 : :
11 : 6 : DEFINE_STUB(spdk_iobuf_initialize, int, (void), 0);
12 : 30 : DEFINE_STUB(spdk_iobuf_register_module, int, (const char *name), 0);
13 : 18 : DEFINE_STUB(spdk_iobuf_unregister_module, int, (const char *name), 0);
14 : 96 : DEFINE_STUB_V(spdk_iobuf_channel_fini, (struct spdk_iobuf_channel *ch));
15 : 0 : DEFINE_STUB(spdk_iobuf_for_each_entry, int, (struct spdk_iobuf_channel *ch,
16 : : struct spdk_iobuf_pool *pool, spdk_iobuf_for_each_entry_fn cb_fn, void *cb_ctx), 0);
17 : 0 : DEFINE_STUB_V(spdk_iobuf_entry_abort, (struct spdk_iobuf_channel *ch,
18 : : struct spdk_iobuf_entry *entry, uint64_t len));
19 : :
20 : : struct ut_iobuf {
21 : : struct spdk_iobuf_opts opts;
22 : : uint32_t small_pool_count;
23 : : uint32_t large_pool_count;
24 : : };
25 : :
26 : : static struct ut_iobuf g_iobuf = {
27 : : .small_pool_count = 32,
28 : : .large_pool_count = 32
29 : : };
30 : : static spdk_iobuf_entry_stailq_t g_iobuf_entries;
31 : :
32 : : int
33 : 12 : spdk_iobuf_set_opts(const struct spdk_iobuf_opts *opts)
34 : : {
35 : 12 : g_iobuf.opts = *opts;
36 [ + - + - : 12 : g_iobuf.small_pool_count = opts->small_pool_count;
+ - ]
37 [ + - + - : 12 : g_iobuf.large_pool_count = opts->large_pool_count;
+ - ]
38 : 12 : return 0;
39 : : }
40 : :
41 : : void
42 : 42 : spdk_iobuf_get_opts(struct spdk_iobuf_opts *opts)
43 : : {
44 : 42 : *opts = g_iobuf.opts;
45 : 42 : }
46 : :
47 : : void
48 : 6 : spdk_iobuf_finish(spdk_iobuf_finish_cb cb_fn, void *cb_arg)
49 : : {
50 [ - + + - ]: 6 : cb_fn(cb_arg);
51 : 6 : }
52 : :
53 : : int
54 : 96 : spdk_iobuf_channel_init(struct spdk_iobuf_channel *ch, const char *name,
55 : : uint32_t small_cache_size, uint32_t large_cache_size)
56 : : {
57 [ + - ]: 96 : STAILQ_INIT(&g_iobuf_entries);
58 [ + - + - : 96 : ch->small.cache_count = small_cache_size;
+ - ]
59 [ + - + - : 96 : ch->small.cache_size = small_cache_size;
+ - ]
60 [ + - + - : 96 : ch->large.cache_count = large_cache_size;
+ - ]
61 [ + - + - : 96 : ch->large.cache_size = large_cache_size;
+ - ]
62 : 96 : return 0;
63 : : }
64 : :
65 : : DEFINE_RETURN_MOCK(spdk_iobuf_get, void *);
66 : : void *
67 : 690 : spdk_iobuf_get(struct spdk_iobuf_channel *ch, uint64_t len,
68 : : struct spdk_iobuf_entry *entry, spdk_iobuf_get_cb cb_fn)
69 : : {
70 : 115 : struct spdk_iobuf_pool *pool;
71 : 115 : uint32_t *count;
72 : 115 : void *buf;
73 : :
74 [ + + + + ]: 690 : HANDLE_RETURN_MOCK(spdk_iobuf_get);
75 : :
76 [ + + + + ]: 234 : if (len > g_iobuf.opts.small_bufsize) {
77 [ + - ]: 30 : pool = &ch->large;
78 : 30 : count = &g_iobuf.large_pool_count;
79 : 5 : } else {
80 [ + - ]: 204 : pool = &ch->small;
81 : 204 : count = &g_iobuf.small_pool_count;
82 : : }
83 : :
84 [ + + + - : 234 : if (pool->cache_count > 0) {
+ + ]
85 : 144 : buf = calloc(1, len);
86 : 144 : CU_ASSERT(buf != NULL);
87 [ + - ]: 144 : pool->cache_count--;
88 : 144 : return buf;
89 : : }
90 : :
91 [ + + + + ]: 90 : if (*count == 0) {
92 [ + + ]: 30 : if (entry) {
93 [ - + - + ]: 30 : entry->cb_fn = cb_fn;
94 [ - + - + : 30 : STAILQ_INSERT_TAIL(&g_iobuf_entries, entry, stailq);
- + - + -
+ - + - +
- + ]
95 : 5 : }
96 : :
97 : 30 : return NULL;
98 : : }
99 : :
100 : 60 : buf = calloc(1, len);
101 : 60 : CU_ASSERT(buf != NULL);
102 : 60 : (*count)--;
103 : 60 : return buf;
104 : 115 : }
105 : :
106 : : void
107 : 234 : spdk_iobuf_put(struct spdk_iobuf_channel *ch, void *buf, uint64_t len)
108 : : {
109 : 39 : struct spdk_iobuf_entry *entry;
110 : 39 : struct spdk_iobuf_pool *pool;
111 : 39 : uint32_t *count;
112 : :
113 [ + + + + ]: 234 : if (len > g_iobuf.opts.small_bufsize) {
114 [ + - ]: 30 : pool = &ch->large;
115 : 30 : count = &g_iobuf.large_pool_count;
116 : 5 : } else {
117 [ + - ]: 204 : pool = &ch->small;
118 : 204 : count = &g_iobuf.small_pool_count;
119 : : }
120 : :
121 [ + + ]: 234 : if (!STAILQ_EMPTY(&g_iobuf_entries)) {
122 : 30 : entry = STAILQ_FIRST(&g_iobuf_entries);
123 [ + - + - : 30 : STAILQ_REMOVE_HEAD(&g_iobuf_entries, stailq);
+ - - + +
- ]
124 [ + - + - : 30 : entry->cb_fn(entry, buf);
- + + - ]
125 : 30 : return;
126 : : }
127 : :
128 [ + + + - : 204 : if (pool->cache_count < pool->cache_size) {
+ - + - +
+ ]
129 [ + - ]: 156 : pool->cache_count++;
130 : 26 : } else {
131 : 48 : (*count)++;
132 : : }
133 : :
134 : 204 : free(buf);
135 [ - + ]: 39 : }
|