Motr  M0
cache.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2014-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include "stob/cache.h" /* m0_stob_cache */
24 
25 #include "lib/memory.h" /* M0_ALLOC_PTR */
26 #include "lib/thread.h" /* M0_THREAD_INIT */
27 #include "lib/arith.h" /* m0_rnd64 */
28 
29 #include "ut/ut.h" /* M0_UT_ASSERT */
30 #include "ut/threads.h" /* M0_UT_THREADS_DEFINE */
31 
32 #include "stob/stob.h" /* m0_stob */
33 #include "stob/stob_internal.h" /* m0_stob__key_set */
34 
35 enum {
40 };
41 
43  int suc_index;
44  size_t suc_idle_size;
45  size_t suc_iter_nr;
46 };
47 
50 
52 {
54  struct m0_stob *stob;
55  struct m0_stob *found;
56  struct m0_stob *found2;
57  uint64_t state = ctx->suc_index;
58  int i;
59  long j;
60 
61  for (i = 0; i < STOB_UT_CACHE_ITER_NR; ++i) {
62  /* select random stob */
65  /* add to cache if it hasn't been added yet */
66  /* delete if it has already been added */
69  if (found == NULL) {
71  } else {
73  }
75  /*
76  * If stob was in the cache before and idle_size > 0
77  * then second lookup will bring stob back to busy cache.
78  * It is not what we need, so push it back to the idle cache
79  * in this case.
80  */
81  if (found != NULL && found2 != NULL)
84  M0_UT_ASSERT(ergo(found == NULL, found2 != NULL));
85  M0_UT_ASSERT(M0_IN(stob, (found, found2)));
86  }
87 }
88 
90  struct m0_stob *stob)
91 {
92  /* XXX check that it is called */
93 }
94 
96 
97 static void stob_ut_cache_test(size_t thread_nr,
98  size_t iter_nr,
99  size_t idle_size)
100 {
101  struct stob_ut_cache_ctx *ctxs;
102  struct m0_stob *stob;
103  const struct m0_fid *stob_fid;
104  size_t i;
105  int rc;
106  uint64_t state = 0;
107 
108  M0_ALLOC_ARR(ctxs, thread_nr);
109  M0_UT_ASSERT(ctxs != NULL);
110 
113 
114  rc = m0_stob_cache_init(&stob_ut_cache, idle_size,
116  M0_UT_ASSERT(rc == 0);
117 
118  for (i = 0; i < ARRAY_SIZE(stob_ut_cache_stobs); ++i)
119  stob_ut_cache_stobs[i].so_id.si_fid.f_key = m0_rnd64(&state);
120 
121  for (i = 0; i < thread_nr; ++i) {
122  ctxs[i] = (struct stob_ut_cache_ctx){
123  .suc_index = i,
124  .suc_idle_size = idle_size,
125  .suc_iter_nr = iter_nr,
126  };
127  }
128  M0_UT_THREADS_START(stob_cache, thread_nr, ctxs);
129  M0_UT_THREADS_STOP(stob_cache);
130 
131  /* clear stob cache */
133  for (i = 0; i < ARRAY_SIZE(stob_ut_cache_stobs); ++i) {
134  stob_fid = m0_stob_fid_get(&stob_ut_cache_stobs[i]);
136  if (stob != NULL)
138  }
140 
142  m0_free(ctxs);
143 }
144 
146 {
149 }
150 
152 {
154 }
155 
156 /*
157  * Local variables:
158  * c-indentation-style: "K&R"
159  * c-basic-offset: 8
160  * tab-width: 8
161  * fill-column: 80
162  * scroll-step: 1
163  * End:
164  */
165 /*
166  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
167  */
Definition: beck.c:235
#define M0_ALLOC_ARR(arr, nr)
Definition: memory.h:84
size_t suc_idle_size
Definition: cache.c:44
#define NULL
Definition: misc.h:38
M0_INTERNAL void m0_stob_cache_add(struct m0_stob_cache *cache, struct m0_stob *stob)
Definition: cache.c:120
#define ergo(a, b)
Definition: misc.h:293
void m0_stob_ut_cache_idle_size0(void)
Definition: cache.c:151
static void stob_ut_cache_evict_cb(struct m0_stob_cache *cache, struct m0_stob *stob)
Definition: cache.c:89
M0_INTERNAL const struct m0_fid * m0_stob_fid_get(struct m0_stob *stob)
Definition: stob.c:255
#define M0_SET0(obj)
Definition: misc.h:64
size_t suc_iter_nr
Definition: cache.c:45
M0_INTERNAL int m0_stob_cache_init(struct m0_stob_cache *cache, uint64_t idle_size, m0_stob_cache_eviction_cb_t eviction_cb)
Definition: cache.c:42
int i
Definition: dir.c:1033
#define M0_SET_ARR0(arr)
Definition: misc.h:72
Definition: stob.h:163
static struct m0_stob * stob
Definition: storage.c:39
static struct m0_stob stob_ut_cache_stobs[STOB_UT_CACHE_STOB_NR]
Definition: cache.c:48
M0_INTERNAL void m0_stob_cache_lock(struct m0_stob_cache *cache)
Definition: cache.c:185
M0_INTERNAL void m0_stob_cache_fini(struct m0_stob_cache *cache)
Definition: cache.c:61
M0_INTERNAL void m0_stob_cache_unlock(struct m0_stob_cache *cache)
Definition: cache.c:190
static struct m0_stob_cache stob_ut_cache
Definition: cache.c:49
static void stob_ut_cache_thread(struct stob_ut_cache_ctx *ctx)
Definition: cache.c:51
M0_INTERNAL uint64_t m0_rnd64(uint64_t *seed)
Definition: misc.c:100
M0_UT_THREADS_DEFINE(stob_cache, stob_ut_cache_thread)
static void stob_ut_cache_test(size_t thread_nr, size_t iter_nr, size_t idle_size)
Definition: cache.c:97
Definition: fid.h:38
void m0_stob_ut_cache(void)
Definition: cache.c:145
#define M0_UT_THREADS_STOP(name)
Definition: threads.h:55
static uint64_t found
Definition: base.c:376
Definition: nucleus.c:42
#define M0_UT_THREADS_START(name, thread_nr, param_array)
Definition: threads.h:51
M0_INTERNAL struct m0_stob * m0_stob_cache_lookup(struct m0_stob_cache *cache, const struct m0_fid *stob_fid)
Definition: cache.c:137
void m0_free(void *data)
Definition: memory.c:146
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_UT_ASSERT(a)
Definition: ut.h:46
M0_INTERNAL void m0_stob_cache_idle(struct m0_stob_cache *cache, struct m0_stob *stob)
Definition: cache.c:129