2 * Copyright(c) 2012-2018 Intel Corporation
3 * SPDX-License-Identifier: BSD-3-Clause-Clear
6 //<tested_file_path>src/mngt/ocf_mngt_cache.c</tested_file_path>
7 //<tested_function>_cache_mng_set_cache_mode</tested_function>
21 #include "print_desc.h"
24 * Headers from tested target.
27 #include "ocf_mngt_common.h"
28 #include "../ocf_core_priv.h"
29 #include "../ocf_queue_priv.h"
30 #include "../metadata/metadata.h"
31 #include "../engine/cache_engine.h"
32 #include "../utils/utils_part.h"
33 #include "../utils/utils_cache_line.h"
34 #include "../utils/utils_device.h"
35 #include "../utils/utils_io.h"
36 #include "../utils/utils_cache_line.h"
37 #include "../utils/utils_pipeline.h"
38 #include "../ocf_utils.h"
39 #include "../concurrency/ocf_concurrency.h"
40 #include "../eviction/ops.h"
41 #include "../ocf_ctx_priv.h"
42 #include "../cleaning/cleaning.h"
47 bool __wrap_ocf_cache_mode_is_valid(ocf_cache_mode_t mode
)
53 const char *__wrap_ocf_get_io_iface_name(ocf_cache_mode_t cache_mode
)
57 ocf_ctx_t
__wrap_ocf_cache_get_ctx(ocf_cache_t cache
)
62 int __wrap_ocf_log_raw(ocf_logger_t logger
, ocf_logger_lvl_t lvl
,
69 int __wrap_ocf_mngt_cache_flush(ocf_cache_t cache
, bool interruption
)
75 int __wrap_ocf_metadata_flush_superblock(struct ocf_cache
*cache
)
79 bool __wrap_env_bit_test(int nr
, const volatile unsigned long *addr
)
85 void __wrap_env_atomic_set(env_atomic
*a
, int i
)
90 int __wrap_env_atomic_read(const env_atomic
*a
)
96 int __wrap_ocf_mngt_cache_reset_fallback_pt_error_counter(ocf_cache_t cache
)
102 char *__wrap_ocf_cache_get_name(ocf_cache_t cache
)
106 void __wrap__ocf_mngt_test_volume_initial_write(
107 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
111 void __wrap_ocf_mngt_test_volume_first_read(
112 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
116 void __wrap__ocf_mngt_test_volume_discard(
117 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
121 void __wrap__ocf_mngt_test_volume_second_read(
122 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
126 void __wrap__ocf_mngt_attach_cache_device(
127 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
131 void __wrap__ocf_mngt_attach_check_ram(
132 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
136 void __wrap__ocf_mngt_attach_load_properties(
137 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
141 void __wrap__ocf_mngt_attach_prepare_metadata(
142 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
146 void __wrap__ocf_mngt_test_volume(
147 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
151 void __wrap__ocf_mngt_attach_load_superblock(
152 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
156 void __wrap__ocf_mngt_attach_init_instance(
157 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
161 void __wrap__ocf_mngt_attach_clean_pol(
162 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
166 void __wrap__ocf_mngt_attach_flush_metadata(
167 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
171 void __wrap__ocf_mngt_attach_discard(
172 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
176 void __wrap__ocf_mngt_attach_flush(
177 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
181 void __wrap__ocf_mngt_attach_shutdown_status(
182 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
186 void __wrap__ocf_mngt_attach_post_init(
187 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
191 void __wrap_ocf_mngt_cache_stop_wait_io(
192 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
196 void __wrap_ocf_mngt_cache_stop_remove_cores(
197 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
201 void __wrap_ocf_mngt_cache_stop_unplug(
202 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
206 void __wrap_ocf_mngt_cache_stop_put_io_queues(
207 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
211 void __wrap_ocf_mngt_cache_detach_flush(
212 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
216 void __wrap_ocf_mngt_cache_detach_wait_pending(
217 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
221 void __wrap_ocf_mngt_cache_detach_update_metadata(
222 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
226 void __wrap_ocf_mngt_cache_detach_unplug(
227 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
231 void __wrap__ocf_mngt_test_volume_first_read(
232 ocf_pipeline_t pipeline
, void *priv
, ocf_pipeline_arg_t arg
)
236 void __wrap__ocf_mngt_test_volume_finish(
237 ocf_pipeline_t pipeline
, void *priv
, int error
)
241 void __wrap__ocf_mngt_cache_attach_finish(
242 ocf_pipeline_t pipeline
, void *priv
, int error
)
246 void __wrap_ocf_mngt_cache_stop_finish(
247 ocf_pipeline_t pipeline
, void *priv
, int error
)
251 void __wrap_ocf_mngt_cache_detach_finish(
252 ocf_pipeline_t pipeline
, void *priv
, int error
)
256 void __wrap_ocf_mngt_cache_save_finish(
257 ocf_pipeline_t pipeline
, void *priv
, int error
)
261 static void _cache_mng_set_cache_mode_test01(void **state
)
263 ocf_cache_mode_t mode_old
= -20;
264 ocf_cache_mode_t mode_new
= ocf_cache_mode_none
;
265 struct ocf_ctx ctx
= {
266 .logger
= 0x1, /* Just not NULL, we don't care. */
268 struct ocf_superblock_config sb_config
= {
269 .cache_mode
= mode_old
,
271 struct ocf_cache cache
= {
273 .conf_meta
= &sb_config
,
277 print_test_description("Invalid new mode produces appropirate error code");
279 expect_function_call(__wrap_ocf_cache_mode_is_valid
);
280 will_return(__wrap_ocf_cache_mode_is_valid
, 0);
282 result
= _cache_mng_set_cache_mode(&cache
, mode_new
);
284 assert_int_equal(result
, -OCF_ERR_INVAL
);
285 assert_int_equal(cache
.conf_meta
->cache_mode
, mode_old
);
288 static void _cache_mng_set_cache_mode_test02(void **state
)
290 ocf_cache_mode_t mode_old
= ocf_cache_mode_wt
;
291 ocf_cache_mode_t mode_new
= ocf_cache_mode_wt
;
292 struct ocf_ctx ctx
= {
293 .logger
= 0x1, /* Just not NULL, we don't care. */
295 struct ocf_superblock_config sb_config
= {
296 .cache_mode
= mode_old
,
298 struct ocf_cache cache
= {
300 .conf_meta
= &sb_config
,
305 print_test_description("Attempt to set mode the same as previous");
307 expect_function_call(__wrap_ocf_cache_mode_is_valid
);
308 will_return(__wrap_ocf_cache_mode_is_valid
, 1);
310 expect_function_call(__wrap_ocf_log_raw
);
311 will_return(__wrap_ocf_log_raw
, 0);
313 result
= _cache_mng_set_cache_mode(&cache
, mode_new
);
315 assert_int_equal(result
, 0);
316 assert_int_equal(cache
.conf_meta
->cache_mode
, mode_old
);
319 static void _cache_mng_set_cache_mode_test03(void **state
)
321 ocf_cache_mode_t mode_old
= ocf_cache_mode_wb
;
322 ocf_cache_mode_t mode_new
= ocf_cache_mode_wa
;
323 struct ocf_ctx ctx
= {
324 .logger
= 0x1, /* Just not NULL, we don't care. */
326 struct ocf_superblock_config sb_config
= {
327 .cache_mode
= mode_old
,
329 struct ocf_cache cache
= {
331 .conf_meta
= &sb_config
,
336 print_test_description("Old cache mode is write back. "
337 "Setting new cache mode is succesfull");
339 expect_function_call(__wrap_ocf_cache_mode_is_valid
);
340 will_return(__wrap_ocf_cache_mode_is_valid
, 1);
342 for(i
= 0; i
!= OCF_CORE_MAX
; ++i
) {
343 expect_function_call(__wrap_env_bit_test
);
344 will_return(__wrap_env_bit_test
, 1);
346 expect_function_call(__wrap_env_atomic_read
);
347 will_return(__wrap_env_atomic_read
, 1);
348 expect_function_call(__wrap_env_atomic_set
);
351 expect_function_call(__wrap_ocf_log_raw
);
352 will_return(__wrap_ocf_log_raw
, 0);
354 result
= _cache_mng_set_cache_mode(&cache
, mode_new
);
356 assert_int_equal(result
, 0);
357 assert_int_equal(cache
.conf_meta
->cache_mode
, mode_new
);
360 static void _cache_mng_set_cache_mode_test04(void **state
)
362 ocf_cache_mode_t mode_old
= ocf_cache_mode_wt
;
363 ocf_cache_mode_t mode_new
= ocf_cache_mode_wa
;
364 struct ocf_ctx ctx
= {
365 .logger
= 0x1, /* Just not NULL, we don't care. */
367 struct ocf_superblock_config sb_config
= {
368 .cache_mode
= mode_old
,
370 struct ocf_cache cache
= {
372 .conf_meta
= &sb_config
,
377 print_test_description("Mode changed successfully");
379 expect_function_call(__wrap_ocf_cache_mode_is_valid
);
380 will_return(__wrap_ocf_cache_mode_is_valid
, 1);
382 expect_function_call(__wrap_ocf_log_raw
);
383 will_return(__wrap_ocf_log_raw
, 0);
385 result
= _cache_mng_set_cache_mode(&cache
, mode_new
);
387 assert_int_equal(result
, 0);
388 assert_int_equal(cache
.conf_meta
->cache_mode
, mode_new
);
392 * Main function. It runs tests.
396 const struct CMUnitTest tests
[] = {
397 cmocka_unit_test(_cache_mng_set_cache_mode_test01
),
398 cmocka_unit_test(_cache_mng_set_cache_mode_test02
),
399 cmocka_unit_test(_cache_mng_set_cache_mode_test03
),
400 cmocka_unit_test(_cache_mng_set_cache_mode_test04
),
403 print_message("Unit test of _cache_mng_set_cache_mode\n");
405 return cmocka_run_group_tests(tests
, NULL
, NULL
);