]> git.proxmox.com Git - ceph.git/blame - ceph/src/spdk/lib/env_ocf/ocf_env.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / lib / env_ocf / ocf_env.c
CommitLineData
9f95a23c
TL
1/*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33#include "ocf/ocf_def.h"
34#include "ocf_env.h"
35
36#include "spdk/crc32.h"
37#include "spdk/env.h"
38#include "spdk_internal/log.h"
39
40/* Number of buffers for mempool
41 * Need to be power of two - 1 for better memory utilization
42 * It depends on memory usage of OCF which
43 * in itself depends on the workload
44 * It is a big number because OCF uses allocators
45 * for every request it sends and recieves
46 */
47#define ENV_ALLOCATOR_NBUFS 32767
48
49/* Use unique index for env allocators */
50static env_atomic g_env_allocator_index = 0;
51
52void *
53env_allocator_new(env_allocator *allocator)
54{
55 void *mem = spdk_mempool_get(allocator->mempool);
56
57 if (spdk_likely(mem)) {
58 memset(mem, 0, allocator->element_size);
59 }
60
61 return mem;
62}
63
64env_allocator *
65env_allocator_create(uint32_t size, const char *name)
66{
67 env_allocator *allocator;
68 char qualified_name[128] = {0};
69
70 snprintf(qualified_name, 128, "ocf_env_%d", env_atomic_inc_return(&g_env_allocator_index));
71
72 allocator = calloc(1, sizeof(*allocator));
73 if (!allocator) {
74 return NULL;
75 }
76
77 allocator->mempool = spdk_mempool_create(qualified_name,
78 ENV_ALLOCATOR_NBUFS, size,
79 SPDK_MEMPOOL_DEFAULT_CACHE_SIZE,
80 SPDK_ENV_SOCKET_ID_ANY);
81
82 if (!allocator->mempool) {
83 free(allocator);
84 return NULL;
85 }
86
87 allocator->element_size = size;
88
89 return allocator;
90}
91
92void
93env_allocator_del(env_allocator *allocator, void *item)
94{
95 spdk_mempool_put(allocator->mempool, item);
96}
97
98void
99env_allocator_destroy(env_allocator *allocator)
100{
101 if (allocator) {
102 if (ENV_ALLOCATOR_NBUFS - spdk_mempool_count(allocator->mempool)) {
103 SPDK_ERRLOG("Not all objects deallocated\n");
104 assert(false);
105 }
106
107 spdk_mempool_free(allocator->mempool);
108 free(allocator);
109 }
110}
f67539c2
TL
111/* *** CRC *** */
112
113uint32_t
114env_crc32(uint32_t crc, uint8_t const *message, size_t len)
115{
116 return spdk_crc32_ieee_update(message, len, crc);
117}
9f95a23c 118
f67539c2
TL
119/* EXECUTION CONTEXTS */
120pthread_mutex_t *exec_context_mutex;
9f95a23c 121
f67539c2 122static void __attribute__((constructor)) init_execution_context(void)
9f95a23c 123{
f67539c2
TL
124 unsigned count = env_get_execution_context_count();
125 unsigned i;
126
127 ENV_BUG_ON(count == 0);
128 exec_context_mutex = malloc(count * sizeof(exec_context_mutex[0]));
129 ENV_BUG_ON(exec_context_mutex == NULL);
130 for (i = 0; i < count; i++) {
131 ENV_BUG_ON(pthread_mutex_init(&exec_context_mutex[i], NULL));
132 }
9f95a23c
TL
133}
134
f67539c2 135static void __attribute__((destructor)) deinit_execution_context(void)
9f95a23c 136{
f67539c2
TL
137 unsigned count = env_get_execution_context_count();
138 unsigned i;
139
140 ENV_BUG_ON(count == 0);
141 ENV_BUG_ON(exec_context_mutex == NULL);
142
143 for (i = 0; i < count; i++) {
144 ENV_BUG_ON(pthread_mutex_destroy(&exec_context_mutex[i]));
9f95a23c 145 }
f67539c2 146 free(exec_context_mutex);
9f95a23c
TL
147}
148
f67539c2
TL
149/* get_execuction_context must assure that after the call finishes, the caller
150 * will not get preempted from current execution context. For userspace env
151 * we simulate this behavior by acquiring per execution context mutex. As a
152 * result the caller might actually get preempted, but no other thread will
153 * execute in this context by the time the caller puts current execution ctx. */
154unsigned env_get_execution_context(void)
9f95a23c 155{
f67539c2
TL
156 unsigned cpu;
157
158 cpu = sched_getcpu();
159 cpu = (cpu == -1) ? 0 : cpu;
160
161 ENV_BUG_ON(pthread_mutex_lock(&exec_context_mutex[cpu]));
162
163 return cpu;
9f95a23c
TL
164}
165
f67539c2
TL
166void env_put_execution_context(unsigned ctx)
167{
168 pthread_mutex_unlock(&exec_context_mutex[ctx]);
169}
9f95a23c 170
f67539c2 171unsigned env_get_execution_context_count(void)
9f95a23c 172{
f67539c2
TL
173 int num = sysconf(_SC_NPROCESSORS_ONLN);
174
175 return (num == -1) ? 0 : num;
9f95a23c 176}