Skip to content

Commit a124963

Browse files
Merge pull request #849 from mmichal10/refcnt-per-cpu
Refcnt per cpu
2 parents bb4d673 + b02481c commit a124963

22 files changed

+392
-243
lines changed
Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,54 @@
44
* SPDX-License-Identifier: BSD-3-Clause
55
*/
66

7-
#include "../utils/utils_refcnt.h"
7+
#include "ocf_env_refcnt.h"
88

9-
void ocf_refcnt_init(struct ocf_refcnt *rc)
9+
int env_refcnt_init(struct env_refcnt *rc, const char *name, size_t name_len)
1010
{
1111
env_atomic_set(&rc->counter, 0);
1212
env_atomic_set(&rc->freeze, 0);
1313
env_atomic_set(&rc->callback, 0);
1414
rc->cb = NULL;
15+
16+
return 0;
1517
}
1618

17-
int ocf_refcnt_dec(struct ocf_refcnt *rc)
19+
void env_refcnt_deinit(struct env_refcnt *rc)
20+
{
21+
22+
}
23+
24+
void env_refcnt_dec(struct env_refcnt *rc)
1825
{
1926
int val = env_atomic_dec_return(&rc->counter);
2027
ENV_BUG_ON(val < 0);
2128

2229
if (!val && env_atomic_cmpxchg(&rc->callback, 1, 0))
2330
rc->cb(rc->priv);
24-
25-
return val;
2631
}
2732

28-
int ocf_refcnt_inc(struct ocf_refcnt *rc)
33+
bool env_refcnt_inc(struct env_refcnt *rc)
2934
{
3035
int val;
3136

3237
if (!env_atomic_read(&rc->freeze)) {
3338
val = env_atomic_inc_return(&rc->counter);
3439
if (!env_atomic_read(&rc->freeze))
35-
return val;
40+
return !!val;
3641
else
37-
ocf_refcnt_dec(rc);
42+
env_refcnt_dec(rc);
3843
}
3944

4045
return 0;
4146
}
4247

4348

44-
void ocf_refcnt_freeze(struct ocf_refcnt *rc)
49+
void env_refcnt_freeze(struct env_refcnt *rc)
4550
{
4651
env_atomic_inc(&rc->freeze);
4752
}
4853

49-
void ocf_refcnt_register_zero_cb(struct ocf_refcnt *rc, ocf_refcnt_cb_t cb,
54+
void env_refcnt_register_zero_cb(struct env_refcnt *rc, env_refcnt_cb_t cb,
5055
void *priv)
5156
{
5257
ENV_BUG_ON(!env_atomic_read(&rc->freeze));
@@ -56,21 +61,21 @@ void ocf_refcnt_register_zero_cb(struct ocf_refcnt *rc, ocf_refcnt_cb_t cb,
5661
rc->cb = cb;
5762
rc->priv = priv;
5863
env_atomic_set(&rc->callback, 1);
59-
ocf_refcnt_dec(rc);
64+
env_refcnt_dec(rc);
6065
}
6166

62-
void ocf_refcnt_unfreeze(struct ocf_refcnt *rc)
67+
void env_refcnt_unfreeze(struct env_refcnt *rc)
6368
{
6469
int val = env_atomic_dec_return(&rc->freeze);
6570
ENV_BUG_ON(val < 0);
6671
}
6772

68-
bool ocf_refcnt_frozen(struct ocf_refcnt *rc)
73+
bool env_refcnt_frozen(struct env_refcnt *rc)
6974
{
7075
return !!env_atomic_read(&rc->freeze);
7176
}
7277

73-
bool ocf_refcnt_zeroed(struct ocf_refcnt *rc)
78+
bool env_refcnt_zeroed(struct env_refcnt *rc)
7479
{
7580
return (env_atomic_read(&rc->counter) == 0);
7681
}

env/posix/ocf_env_refcnt.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright(c) 2019-2021 Intel Corporation
3+
* Copyright(c) 2024-2025 Huawei Technologies
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#ifndef __OCF_ENV_REFCNT_H__
8+
#define __OCF_ENV_REFCNT_H__
9+
10+
#include "ocf_env.h"
11+
12+
typedef void (*env_refcnt_cb_t)(void *priv);
13+
14+
struct env_refcnt
15+
{
16+
env_atomic counter __attribute__((aligned(64)));
17+
env_atomic freeze;
18+
env_atomic callback;
19+
env_refcnt_cb_t cb;
20+
void *priv;
21+
};
22+
23+
/* Initialize reference counter */
24+
int env_refcnt_init(struct env_refcnt *rc, const char *name, size_t name_len);
25+
26+
void env_refcnt_deinit(struct env_refcnt *rc);
27+
28+
/* Try to increment counter. Returns true if successfull, false
29+
* if counter is frozen */
30+
bool env_refcnt_inc(struct env_refcnt *rc);
31+
32+
/* Decrement reference counter */
33+
void env_refcnt_dec(struct env_refcnt *rc);
34+
35+
/* Disallow incrementing of underlying counter - attempts to increment counter
36+
* will be failing until env_refcnt_unfreeze is calleed.
37+
* It's ok to call freeze multiple times, in which case counter is frozen
38+
* until all freeze calls are offset by a corresponding unfreeze.*/
39+
void env_refcnt_freeze(struct env_refcnt *rc);
40+
41+
/* Cancel the effect of single env_refcnt_freeze call */
42+
void env_refcnt_unfreeze(struct env_refcnt *rc);
43+
44+
bool env_refcnt_frozen(struct env_refcnt *rc);
45+
46+
bool env_refcnt_zeroed(struct env_refcnt *rc);
47+
48+
/* Register callback to be called when reference counter drops to 0.
49+
* Must be called after counter is frozen.
50+
* Cannot be called until previously regsitered callback had fired. */
51+
void env_refcnt_register_zero_cb(struct env_refcnt *rc, env_refcnt_cb_t cb,
52+
void *priv);
53+
54+
#endif // __OCF_ENV_REFCNT_H__

src/cleaning/acp_structs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
22
* Copyright(c) 2012-2021 Intel Corporation
3+
* Copyright(c) 2023-2025 Huawei Technologies Co., Ltd.
34
* SPDX-License-Identifier: BSD-3-Clause
45
*/
56
#ifndef __CLEANING_AGGRESSIVE_STRUCTS_H__
67
#define __CLEANING_AGGRESSIVE_STRUCTS_H__
78

8-
#include "../utils/utils_cleaner.h"
9+
#include "ocf_env_headers.h"
910

1011
/* TODO: remove acp metadata */
1112
struct acp_cleaning_policy_meta {

src/cleaning/cleaning.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright(c) 2012-2022 Intel Corporation
3+
* Copyright(c) 2023-2025 Huawei Technologies Co., Ltd.
34
* SPDX-License-Identifier: BSD-3-Clause
45
*/
56

@@ -9,8 +10,8 @@
910
#include "alru_structs.h"
1011
#include "nop_structs.h"
1112
#include "acp_structs.h"
13+
#include "ocf_env_refcnt.h"
1214
#include "ocf/ocf_cleaner.h"
13-
#include "../utils/utils_refcnt.h"
1415

1516
#define CLEANING_POLICY_CONFIG_BYTES 256
1617
#define CLEANING_POLICY_TYPE_MAX 4
@@ -40,7 +41,7 @@ struct cleaning_policy_meta {
4041
};
4142

4243
struct ocf_cleaner {
43-
struct ocf_refcnt refcnt __attribute__((aligned(64)));
44+
struct env_refcnt refcnt;
4445
ocf_cleaning_t policy;
4546
void *cleaning_policy_context;
4647
ocf_queue_t io_queue;

src/cleaning/cleaning_ops.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright(c) 2012-2022 Intel Corporation
3+
* Copyright(c) 2023-2025 Huawei Technologies Co., Ltd.
34
* SPDX-License-Identifier: BSD-3-Clause
45
*/
56

@@ -9,7 +10,7 @@
910
#include "../metadata/metadata_superblock.h"
1011
#include "../metadata/metadata_structs.h"
1112
#include "../ocf_cache_priv.h"
12-
#include "../utils/utils_refcnt.h"
13+
#include "ocf_env_refcnt.h"
1314

1415
struct cleaning_policy_ops {
1516
void (*setup)(ocf_cache_t cache);
@@ -125,7 +126,7 @@ static inline int ocf_cleaning_add_core(ocf_cache_t cache,
125126
ocf_cleaning_t policy;
126127
int result = 0;
127128

128-
if (unlikely(!ocf_refcnt_inc(&cache->cleaner.refcnt)))
129+
if (unlikely(!env_refcnt_inc(&cache->cleaner.refcnt)))
129130
return -OCF_ERR_NO_LOCK;
130131

131132
policy = cache->cleaner.policy;
@@ -138,7 +139,7 @@ static inline int ocf_cleaning_add_core(ocf_cache_t cache,
138139
result = cleaning_policy_ops[policy].add_core(cache, core_id);
139140

140141
unlock:
141-
ocf_refcnt_dec(&cache->cleaner.refcnt);
142+
env_refcnt_dec(&cache->cleaner.refcnt);
142143

143144
return result;
144145
}
@@ -148,7 +149,7 @@ static inline void ocf_cleaning_remove_core(ocf_cache_t cache,
148149
{
149150
ocf_cleaning_t policy;
150151

151-
if (unlikely(!ocf_refcnt_inc(&cache->cleaner.refcnt)))
152+
if (unlikely(!env_refcnt_inc(&cache->cleaner.refcnt)))
152153
return;
153154

154155
policy = cache->cleaner.policy;
@@ -161,15 +162,15 @@ static inline void ocf_cleaning_remove_core(ocf_cache_t cache,
161162
cleaning_policy_ops[policy].remove_core(cache, core_id);
162163

163164
unlock:
164-
ocf_refcnt_dec(&cache->cleaner.refcnt);
165+
env_refcnt_dec(&cache->cleaner.refcnt);
165166
}
166167

167168
static inline void ocf_cleaning_init_cache_block(ocf_cache_t cache,
168169
ocf_cache_line_t cache_line)
169170
{
170171
ocf_cleaning_t policy;
171172

172-
if (unlikely(!ocf_refcnt_inc(&cache->cleaner.refcnt)))
173+
if (unlikely(!env_refcnt_inc(&cache->cleaner.refcnt)))
173174
return;
174175

175176
policy = cache->cleaner.policy;
@@ -181,15 +182,15 @@ static inline void ocf_cleaning_init_cache_block(ocf_cache_t cache,
181182
cleaning_policy_ops[policy].init_cache_block(cache, cache_line);
182183

183184
unlock:
184-
ocf_refcnt_dec(&cache->cleaner.refcnt);
185+
env_refcnt_dec(&cache->cleaner.refcnt);
185186
}
186187

187188
static inline void ocf_cleaning_purge_cache_block(ocf_cache_t cache,
188189
ocf_cache_line_t cache_line)
189190
{
190191
ocf_cleaning_t policy;
191192

192-
if (unlikely(!ocf_refcnt_inc(&cache->cleaner.refcnt)))
193+
if (unlikely(!env_refcnt_inc(&cache->cleaner.refcnt)))
193194
return;
194195

195196
policy = cache->cleaner.policy;
@@ -201,15 +202,15 @@ static inline void ocf_cleaning_purge_cache_block(ocf_cache_t cache,
201202
cleaning_policy_ops[policy].purge_cache_block(cache, cache_line);
202203

203204
unlock:
204-
ocf_refcnt_dec(&cache->cleaner.refcnt);
205+
env_refcnt_dec(&cache->cleaner.refcnt);
205206
}
206207

207208
static inline void ocf_cleaning_purge_range(ocf_cache_t cache,
208209
ocf_core_id_t core_id, uint64_t start_byte, uint64_t end_byte)
209210
{
210211
ocf_cleaning_t policy;
211212

212-
if (unlikely(!ocf_refcnt_inc(&cache->cleaner.refcnt)))
213+
if (unlikely(!env_refcnt_inc(&cache->cleaner.refcnt)))
213214
return;
214215

215216
policy = cache->cleaner.policy;
@@ -222,15 +223,15 @@ static inline void ocf_cleaning_purge_range(ocf_cache_t cache,
222223
end_byte);
223224

224225
unlock:
225-
ocf_refcnt_dec(&cache->cleaner.refcnt);
226+
env_refcnt_dec(&cache->cleaner.refcnt);
226227
}
227228

228229
static inline void ocf_cleaning_set_hot_cache_line(ocf_cache_t cache,
229230
ocf_cache_line_t cache_line)
230231
{
231232
ocf_cleaning_t policy;
232233

233-
if (unlikely(!ocf_refcnt_inc(&cache->cleaner.refcnt)))
234+
if (unlikely(!env_refcnt_inc(&cache->cleaner.refcnt)))
234235
return;
235236

236237
policy = cache->cleaner.policy;
@@ -242,7 +243,7 @@ static inline void ocf_cleaning_set_hot_cache_line(ocf_cache_t cache,
242243
cleaning_policy_ops[policy].set_hot_cache_line(cache, cache_line);
243244

244245
unlock:
245-
ocf_refcnt_dec(&cache->cleaner.refcnt);
246+
env_refcnt_dec(&cache->cleaner.refcnt);
246247
}
247248

248249
static inline int ocf_cleaning_set_param(ocf_cache_t cache,
@@ -274,7 +275,7 @@ static inline void ocf_cleaning_perform_cleaning(ocf_cache_t cache,
274275
{
275276
ocf_cleaning_t policy;
276277

277-
if (unlikely(!ocf_refcnt_inc(&cache->cleaner.refcnt))) {
278+
if (unlikely(!env_refcnt_inc(&cache->cleaner.refcnt))) {
278279
cmpl(&cache->cleaner, 1000);
279280
return;
280281
}
@@ -283,14 +284,14 @@ static inline void ocf_cleaning_perform_cleaning(ocf_cache_t cache,
283284
ENV_BUG_ON(policy >= ocf_cleaning_max);
284285

285286
if (unlikely(!cleaning_policy_ops[policy].perform_cleaning)) {
286-
ocf_refcnt_dec(&cache->cleaner.refcnt);
287+
env_refcnt_dec(&cache->cleaner.refcnt);
287288
cmpl(&cache->cleaner, 1000);
288289
return;
289290
}
290291

291292
cleaning_policy_ops[policy].perform_cleaning(cache, cmpl);
292293

293-
ocf_refcnt_dec(&cache->cleaner.refcnt);
294+
env_refcnt_dec(&cache->cleaner.refcnt);
294295
}
295296

296297
static inline const char *ocf_cleaning_get_name(ocf_cleaning_t policy)

src/engine/cache_engine.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright(c) 2012-2022 Intel Corporation
3-
* Copyright(c) 2024 Huawei Technologies
3+
* Copyright(c) 2024-2025 Huawei Technologies
44
* SPDX-License-Identifier: BSD-3-Clause
55
*/
66

@@ -22,7 +22,6 @@
2222
#include "engine_flush.h"
2323
#include "engine_discard.h"
2424
#include "../utils/utils_user_part.h"
25-
#include "../utils/utils_refcnt.h"
2625
#include "../ocf_request.h"
2726
#include "../metadata/metadata.h"
2827
#include "../ocf_space.h"

0 commit comments

Comments
 (0)