Initial commit

Signed-off-by: Robert Baldyga <robert.baldyga@intel.com>
This commit is contained in:
Robert Baldyga
2018-11-29 15:14:21 +01:00
commit a8e1ce8cc5
178 changed files with 35378 additions and 0 deletions

30
tests/ut/add_new_test_file.py Executable file
View File

@@ -0,0 +1,30 @@
#!/usr/bin/env python2
#
# Copyright(c) 2012-2018 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause-Clear
#
import commands
import sys
import os
args = ' '.join(sys.argv[1:])
script_path = os.path.dirname(os.path.realpath(__file__))
framework_script_path = script_path + os.sep + "../ut-framework/add_new_test_file.py"
framework_script_path = os.path.normpath(framework_script_path)
status, output = commands.getstatusoutput(framework_script_path + " " + args)
print output
if status == 0:
path = output.split(" ", 1)[0]
with open(script_path + os.sep + "header.c", "r") as header_file:
with open(path, "r+") as source_file:
source = source_file.readlines()
source_file.seek(0, os.SEEK_SET)
source_file.truncate()
source_file.writelines(header_file.readlines())
source_file.writelines(source)

View File

@@ -0,0 +1,118 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
/*
<tested_file_path>src/cleaning/alru.c</tested_file_path>
<tested_function>cleaning_policy_alru_initialize_part</tested_function>
<functions_to_leave>
</functions_to_leave>
*/
#undef static
#undef inline
/*
* This headers must be in test source file. It's important that cmocka.h is
* last.
*/
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "print_desc.h"
/*
* Headers from tested target.
*/
#include "ocf/ocf.h"
#include "../ocf_cache_priv.h"
#include "cleaning.h"
#include "alru.h"
#include "../metadata/metadata.h"
#include "../utils/utils_cleaner.h"
#include "../utils/utils_part.h"
#include "../utils/utils_allocator.h"
#include "../concurrency/ocf_cache_concurrency.h"
#include "../ocf_def_priv.h"
void _alru_rebuild(struct ocf_cache *cache)
{
}
static void cleaning_policy_alru_initialize_test01(void **state)
{
int result;
struct ocf_cache cache;
ocf_part_id_t part_id = 0;
int collision_table_entries = 900729;
cache.collision_table_entries = collision_table_entries;
print_test_description("Check if all variables are set correctly");
cache.user_parts[part_id].runtime = test_malloc(sizeof(struct ocf_user_part_runtime));
cache.runtime_meta = test_malloc(sizeof(struct ocf_superblock_runtime));
result = cleaning_policy_alru_initialize_part(&cache, &cache.user_parts[part_id], 1, 1);
assert_int_equal(result, 0);
assert_int_equal(cache.user_parts[part_id].runtime->cleaning.policy.alru.size, 0);
assert_int_equal(cache.user_parts[part_id].runtime->cleaning.policy.alru.lru_head, collision_table_entries);
assert_int_equal(cache.user_parts[part_id].runtime->cleaning.policy.alru.lru_tail, collision_table_entries);
assert_int_equal(cache.runtime_meta->cleaning_thread_access, 0);
test_free(cache.runtime_meta);
test_free(cache.user_parts[part_id].runtime);
}
static void cleaning_policy_alru_initialize_test02(void **state)
{
int result;
struct ocf_cache cache;
ocf_part_id_t part_id = 0;
uint32_t collision_table_entries = 900729;
cache.collision_table_entries = collision_table_entries;
print_test_description("Check if only appropirate variables are changed");
cache.user_parts[part_id].runtime = test_malloc(sizeof(struct ocf_user_part_runtime));
cache.runtime_meta = test_malloc(sizeof(struct ocf_superblock_runtime));
cache.user_parts[part_id].runtime->cleaning.policy.alru.size = 1;
cache.user_parts[part_id].runtime->cleaning.policy.alru.lru_head = -collision_table_entries;
cache.user_parts[part_id].runtime->cleaning.policy.alru.lru_tail = -collision_table_entries;
result = cleaning_policy_alru_initialize_part(&cache, &cache.user_parts[part_id], 0, 0);
assert_int_equal(result, 0);
assert_int_equal(cache.user_parts[part_id].runtime->cleaning.policy.alru.size, 1);
assert_int_equal(cache.user_parts[part_id].runtime->cleaning.policy.alru.lru_head, -collision_table_entries);
assert_int_equal(cache.user_parts[part_id].runtime->cleaning.policy.alru.lru_tail, -collision_table_entries);
assert_int_equal(cache.runtime_meta->cleaning_thread_access, 0);
test_free(cache.runtime_meta);
test_free(cache.user_parts[part_id].runtime);
}
/*
* Main function. It runs tests.
*/
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(cleaning_policy_alru_initialize_test01),
cmocka_unit_test(cleaning_policy_alru_initialize_test02)
};
print_message("Unit test of alru.c\n");
return cmocka_run_group_tests(tests, NULL, NULL);
}

View File

@@ -0,0 +1,258 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
/*
* This headers must be in test source file. It's important that cmocka.h is
* last.
*/
#undef static
#undef inline
//<tested_file_path>src/cleaning/cleaning.c</tested_file_path>
//<tested_function>ocf_cleaner_run</tested_function>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "print_desc.h"
/*
* Headers from tested target.
*/
#include "cleaning.h"
#include "alru.h"
#include "acp.h"
#include "../ocf_cache_priv.h"
#include "../ocf_ctx_priv.h"
#include "../mngt/ocf_mngt_common.h"
#include "../metadata/metadata.h"
/*
* Mocked functions. Here we must deliver functions definitions which are not
* in tested source file.
*/
void __wrap_cleaning_policy_alru_setup(struct ocf_cache *cache)
{}
int __wrap_cleaning_policy_alru_set_cleaning_param(struct ocf_cache *cache,
uint32_t param_id, uint32_t param_value)
{
}
int __wrap_cleaning_policy_alru_get_cleaning_param(struct ocf_cache *cache,
uint32_t param_id, uint32_t *param_value)
{
}
int __wrap_cleaning_policy_acp_initialize(struct ocf_cache *cache,
int init_metadata, int init_params){}
void __wrap_cleaning_policy_acp_deinitialize(struct ocf_cache *cache){}
int __wrap_cleaning_policy_acp_perform_cleaning(struct ocf_cache *cache,
uint32_t io_queue){}
void __wrap_cleaning_policy_acp_init_cache_block(struct ocf_cache *cache,
uint32_t cache_line){}
void __wrap_cleaning_policy_acp_set_hot_cache_line(struct ocf_cache *cache,
uint32_t cache_line){}
void __wrap_cleaning_policy_acp_purge_block(struct ocf_cache *cache,
uint32_t cache_line){}
int __wrap_cleaning_policy_acp_purge_range(struct ocf_cache *cache,
int core_id, uint64_t start_byte, uint64_t end_byte){}
int __wrap_cleaning_policy_acp_add_core(ocf_cache_t cache, ocf_core_id_t core_id){}
int __wrap_cleaning_policy_acp_remove_core(ocf_cache_t cache,
ocf_core_id_t core_id){}
void __wrap_cleaning_policy_acp_request_pending(struct ocf_request *rq){
}
void cleaning_policy_acp_setup(struct ocf_cache *cache)
{
}
int cleaning_policy_acp_set_cleaning_param(struct ocf_cache *cache,
uint32_t param_id, uint32_t param_value)
{
}
int cleaning_policy_acp_get_cleaning_param(struct ocf_cache *cache,
uint32_t param_id, uint32_t *param_value)
{
}
int __wrap_cleaning_policy_acp_set_cleaning_parameters(
struct ocf_cache *cache, struct ocf_cleaning_params *params)
{
}
void __wrap_cleaning_policy_acp_get_cleaning_parameters(
struct ocf_cache *cache, struct ocf_cleaning_params *params)
{
}
void __wrap_cleaning_policy_alru_init_cache_block(struct ocf_cache *cache,
uint32_t cache_line)
{
}
void __wrap_cleaning_policy_alru_purge_cache_block(struct ocf_cache *cache,
uint32_t cache_line)
{
}
int __wrap_cleaning_policy_alru_purge_range(struct ocf_cache *cache,
int partition_id, int core_id, uint64_t start_byte,
uint64_t end_byte)
{
}
void __wrap_cleaning_policy_alru_set_hot_cache_line(struct ocf_cache *cache,
uint32_t cache_line)
{
}
int __wrap_cleaning_policy_alru_initialize(struct ocf_cache *cache, int partition_id,
int init_metadata)
{
}
int __wrap_cleaning_policy_alru_flush_block(struct ocf_cache *cache,
uint32_t io_queue, uint32_t count, uint32_t *cache_lines,
int partition_id, int core_id, uint8_t do_lock)
{
}
int __wrap_cleaning_policy_alru_set_cleaning_parameters(ocf_cache_t cache,
ocf_part_id_t part_id, struct ocf_cleaning_params *params)
{
}
void __wrap_cleaning_policy_alru_get_cleaning_parameters(ocf_cache_t cache,
ocf_part_id_t part_id, struct ocf_cleaning_params *params)
{
}
int __wrap_cleaning_alru_perform_cleaning(struct ocf_cache *cache, uint32_t io_queue)
{
function_called();
return mock();
}
ocf_cache_t __wrap_ocf_cleaner_get_cache(ocf_cleaner_t c)
{
function_called();
return mock_ptr_type(struct ocf_cache*);
}
bool __wrap_ocf_mngt_is_cache_locked(ocf_cache_t cache)
{
function_called();
return mock();
}
int __wrap__ocf_cleaner_run_check_dirty_inactive(struct ocf_cache *cache)
{
function_called();
return mock();
}
int __wrap_env_bit_test(int nr, const void *addr)
{
function_called();
return mock();
}
int __wrap_env_rmutex_trylock(env_rmutex *rmutex)
{
function_called();
return mock();
}
void __wrap_env_rmutex_unlock(env_rmutex *rmutex)
{
function_called();
}
/*
* Tests of functions. Every test name must be written to tests array in main().
* Declarations always look the same: static void test_name(void **state);
*/
static void ocf_cleaner_run_test01(void **state)
{
struct ocf_cache cache;
ocf_part_id_t part_id;
uint32_t io_queue;
int result;
//Initialize needed structures.
cache.conf_meta = test_malloc(sizeof(struct ocf_superblock_config));
cache.conf_meta->cleaning_policy_type = ocf_cleaning_alru;
print_test_description("Parts are ready for cleaning - should perform cleaning"
" for each part");
expect_function_call(__wrap_ocf_cleaner_get_cache);
will_return(__wrap_ocf_cleaner_get_cache, &cache);
expect_function_call(__wrap_env_bit_test);
will_return(__wrap_env_bit_test, 1);
expect_function_call(__wrap_ocf_mngt_is_cache_locked);
will_return(__wrap_ocf_mngt_is_cache_locked, 0);
expect_function_call(__wrap_env_rmutex_trylock);
will_return(__wrap_env_rmutex_trylock, 1);
expect_function_call(__wrap__ocf_cleaner_run_check_dirty_inactive);
will_return(__wrap__ocf_cleaner_run_check_dirty_inactive, 0);
expect_function_call(__wrap_cleaning_alru_perform_cleaning);
will_return(__wrap_cleaning_alru_perform_cleaning, 0);
expect_function_call(__wrap_env_rmutex_unlock);
result = ocf_cleaner_run(&cache.cleaner, io_queue);
assert_int_equal(result, 0);
/* Release allocated memory if allocated with test_* functions */
test_free(cache.conf_meta);
}
/*
* Main function. It runs tests.
*/
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(ocf_cleaner_run_test01)
};
print_message("Unit test of cleaning.c\n");
return cmocka_run_group_tests(tests, NULL, NULL);
}

5
tests/ut/header.c Normal file
View File

@@ -0,0 +1,5 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/

View File

@@ -0,0 +1,105 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
//<tested_file_path>src/metadata/metadata_io.c</tested_file_path>
//<tested_function>metadata_io</tested_function>
#undef static
#undef inline
/*
* This headers must be in test source file. It's important that cmocka.h is
* last.
*/
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "print_desc.h"
/*
* Headers from tested target.
*/
#include "metadata.h"
#include "metadata_io.h"
#include "../engine/cache_engine.h"
#include "../engine/engine_common.h"
#include "../engine/engine_bf.h"
#include "../utils/utils_cache_line.h"
#include "../utils/utils_io.h"
#include "../utils/utils_allocator.h"
#include "../ocf_def_priv.h"
uint32_t __wrap_metadata_io_max_page(struct ocf_cache *cache)
{
function_called();
return mock();
}
void __wrap_env_cond_resched(void)
{
}
void __wrap_ocf_engine_push_rq_front(struct ocf_request *rq)
{
}
int __wrap_ocf_realloc(void **mem, size_t size, size_t count, size_t *limit)
{
}
int __wrap_ocf_realloc_cp(void **mem, size_t size, size_t count, size_t *limit)
{
}
ocf_ctx_t __wrap_ocf_cache_get_ctx(ocf_cache_t cache)
{
}
int __wrap_ocf_log_raw(const struct ocf_logger *logger, ocf_logger_lvl_t lvl,
const char *fmt, ...)
{
}
int __wrap_metadata_submit_io(
struct ocf_cache *cache,
struct metadata_io *mio,
uint32_t count,
uint32_t written)
{
}
int __wrap_ocf_restart_meta_io(struct ocf_request *req)
{
}
static void metadata_io_test01(void **state)
{
int result;
struct metadata_io mio;
struct ocf_cache cache;
print_test_description("Check error no. when invalid operation is given");
mio.dir = -1;
mio.cache = &cache;
expect_function_call(__wrap_metadata_io_max_page);
will_return(__wrap_metadata_io_max_page, 256);
result = metadata_io(&mio);
assert_int_equal(result, -EINVAL);
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(metadata_io_test01)
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

View File

@@ -0,0 +1,245 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
//<tested_file_path>src/metadata/metadata_io.c</tested_file_path>
//<tested_function>metadata_submit_io</tested_function>
#undef static
#undef inline
/*
* This headers must be in test source file. It's important that cmocka.h is
* last.
*/
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "print_desc.h"
/*
* Headers from tested target.
*/
#include "metadata.h"
#include "metadata_io.h"
#include "../engine/cache_engine.h"
#include "../engine/engine_common.h"
#include "../engine/engine_bf.h"
#include "../utils/utils_cache_line.h"
#include "../utils/utils_allocator.h"
#include "../ocf_def_priv.h"
struct ocf_io *__wrap_ocf_new_cache_io(struct ocf_cache *cache)
{
function_called();
return mock_ptr_type(struct ocf_io *);
}
int __wrap_metadata_io_write_fill(struct ocf_cache *cache,
ctx_data_t *data, uint32_t page, void *context)
{
function_called();
return mock();
}
void *__wrap_ctx_data_alloc(ocf_ctx_t ctx, uint32_t pages)
{
function_called();
return mock_ptr_type(void*);
}
void __wrap_ocf_io_configure(struct ocf_io *io, uint64_t addr,
uint32_t bytes, uint32_t dir, uint32_t class, uint64_t flags)
{
function_called();
}
void __wrap_metadata_io_end(struct ocf_io *io, int error)
{
}
void __wrap_ocf_io_set_cmpl(struct ocf_io *io, void *context,
void *context2, ocf_end_io_t fn)
{
function_called();
}
int __wrap_ocf_io_set_data(struct ocf_io *io, ctx_data_t *data,
uint32_t offset)
{
function_called();
return mock();
}
void __wrap_ocf_dobj_submit_io(struct ocf_io *io)
{
function_called();
}
void __wrap_ctx_data_free(ocf_ctx_t ctx, ctx_data_t *data)
{
function_called();
}
void __wrap_ocf_io_put(struct ocf_io *io)
{
function_called();
}
int __wrap_ocf_restart_meta_io(struct ocf_request *req)
{
}
void __wrap_env_atomic_inc(env_atomic *a)
{
function_called();
}
static void metadata_submit_io_test01(void **state)
{
int result;
struct metadata_io mio;
struct ocf_cache cache;
uint32_t count;
uint32_t written;
print_test_description("Couldn't allocate new IO");
expect_function_call(__wrap_ocf_new_cache_io);
will_return(__wrap_ocf_new_cache_io, 0);
result = metadata_submit_io(&cache, &mio, count, written);
assert_int_equal(result, -ENOMEM);
assert_int_equal(mio.error, -ENOMEM);
}
static void metadata_submit_io_test02(void **state)
{
int result;
struct metadata_io mio;
struct ocf_cache cache;
uint32_t count;
uint32_t written;
print_test_description("Couldn't allocate data buffer for IO");
expect_function_call(__wrap_ocf_new_cache_io);
will_return(__wrap_ocf_new_cache_io, 1);
expect_function_call(__wrap_ctx_data_alloc);
will_return(__wrap_ctx_data_alloc, 0);
expect_function_call(__wrap_ocf_io_put);
result = metadata_submit_io(&cache, &mio, count, written);
assert_int_equal(result, -ENOMEM);
assert_int_equal(mio.error, -ENOMEM);
}
static void metadata_submit_io_test03(void **state)
{
int result;
struct metadata_io mio;
struct ocf_cache cache;
uint32_t count;
uint32_t written;
int mio_err = 0;
print_test_description("Write operation is performed successfully");
mio.hndl_fn = __wrap_metadata_io_write_fill;
mio.dir = OCF_WRITE;
mio.error = mio_err;
count = 1;
expect_function_call(__wrap_ocf_new_cache_io);
will_return(__wrap_ocf_new_cache_io, 1);
expect_function_call(__wrap_ctx_data_alloc);
will_return(__wrap_ctx_data_alloc, 1);
expect_function_call(__wrap_metadata_io_write_fill);
will_return(__wrap_metadata_io_write_fill, 0);
expect_function_call(__wrap_ocf_io_configure);
expect_function_call(__wrap_ocf_io_set_cmpl);
expect_function_call(__wrap_ocf_io_set_data);
will_return(__wrap_ocf_io_set_data, 0);
expect_function_call(__wrap_env_atomic_inc);
expect_function_call(__wrap_ocf_dobj_submit_io);
result = metadata_submit_io(&cache, &mio, count, written);
assert_int_equal(result, 0);
assert_int_equal(mio.error, mio_err);
}
static void metadata_submit_io_test04(void **state)
{
int result;
int i;
int interations_before_fail;
struct metadata_io mio;
struct ocf_cache cache;
uint32_t count;
uint32_t written;
print_test_description("Write operation is performed, but if fails at 3rd iteration");
mio.hndl_fn = __wrap_metadata_io_write_fill;
mio.dir = OCF_WRITE;
count = 3;
interations_before_fail = 2;
expect_function_call(__wrap_ocf_new_cache_io);
will_return(__wrap_ocf_new_cache_io, 1);
expect_function_call(__wrap_ctx_data_alloc);
will_return(__wrap_ctx_data_alloc, 1);
for (i = 0; i < interations_before_fail; i++) {
expect_function_call(__wrap_metadata_io_write_fill);
will_return(__wrap_metadata_io_write_fill, 0);
}
expect_function_call(__wrap_metadata_io_write_fill);
will_return(__wrap_metadata_io_write_fill, 1);
expect_function_call(__wrap_ctx_data_free);
expect_function_call(__wrap_ocf_io_put);
result = metadata_submit_io(&cache, &mio, count, written);
assert_int_equal(result, 1);
assert_int_equal(mio.error, 1);
}
/*
* Main function. It runs tests.
*/
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(metadata_submit_io_test01),
cmocka_unit_test(metadata_submit_io_test02),
cmocka_unit_test(metadata_submit_io_test03),
cmocka_unit_test(metadata_submit_io_test04)
};
print_message("Example template for tests\n");
return cmocka_run_group_tests(tests, NULL, NULL);
}

View File

@@ -0,0 +1,351 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
//<tested_file_path>src/mngt/ocf_mngt_cache.c</tested_file_path>
//<tested_function>_cache_mng_set_cache_mode</tested_function>
/*
<functions_to_leave>
</functions_to_leave>
*/
#undef static
#undef inline
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "print_desc.h"
/*
* Headers from tested target.
*/
#include "ocf/ocf.h"
#include "ocf_mngt_common.h"
#include "../ocf_core_priv.h"
#include "../ocf_queue_priv.h"
#include "../metadata/metadata.h"
#include "../engine/cache_engine.h"
#include "../utils/utils_part.h"
#include "../utils/utils_cache_line.h"
#include "../utils/utils_device.h"
#include "../utils/utils_io.h"
#include "../utils/utils_cache_line.h"
#include "../ocf_utils.h"
#include "../concurrency/ocf_concurrency.h"
#include "../eviction/ops.h"
#include "../ocf_ctx_priv.h"
#include "../cleaning/cleaning.h"
/*
* Mocked functions
*/
bool __wrap_ocf_cache_mode_is_valid(ocf_cache_mode_t mode)
{
function_called();
return mock();
}
const char *__wrap_ocf_get_io_iface_name(ocf_cache_mode_t cache_mode)
{
}
ocf_ctx_t __wrap_ocf_cache_get_ctx(ocf_cache_t cache)
{
}
int __wrap_ocf_log_raw(const struct ocf_logger *logger, ocf_logger_lvl_t lvl,
const char *fmt, ...)
{
function_called();
return mock();
}
int __wrap_ocf_mngt_cache_flush_nolock(ocf_cache_t cache, bool interruption)
{
function_called();
return mock();
}
int __wrap_ocf_metadata_flush_superblock(struct ocf_cache *cache)
{
function_called();
return mock();
}
bool __wrap_env_bit_test(int nr, const volatile unsigned long *addr)
{
function_called();
return mock();
}
void __wrap_env_atomic_set(env_atomic *a, int i)
{
function_called();
}
int __wrap_env_atomic_read(const env_atomic *a)
{
function_called();
return mock();
}
int __wrap_ocf_mngt_cache_reset_fallback_pt_error_counter(ocf_cache_t cache)
{
function_called();
return mock();
}
static void _cache_mng_set_cache_mode_test01(void **state)
{
int result;
struct ocf_cache cache;
ocf_cache_mode_t mode_old, mode_new;
uint8_t flush;
print_test_description("Invalid new mode produces appropirate error code");
cache.conf_meta = test_malloc(sizeof(struct ocf_superblock_config));
mode_old = -20;
cache.conf_meta->cache_mode = mode_old;
mode_new = ocf_cache_mode_none;
flush = 0;
expect_function_call(__wrap_ocf_cache_mode_is_valid);
will_return(__wrap_ocf_cache_mode_is_valid, 0);
result = _cache_mng_set_cache_mode(&cache, mode_new, flush);
assert_int_equal(result, -OCF_ERR_INVAL);
assert_int_equal(cache.conf_meta->cache_mode, mode_old);
test_free(cache.conf_meta);
}
static void _cache_mng_set_cache_mode_test02(void **state)
{
int result;
struct ocf_cache cache;
ocf_cache_mode_t mode_old, mode_new;
uint8_t flush;
print_test_description("Attempt to set mode the same as previous");
mode_old = mode_new = ocf_cache_mode_wt;
flush = 0;
cache.conf_meta = test_malloc(sizeof(struct ocf_superblock_config));
cache.conf_meta->cache_mode = mode_old;
expect_function_call(__wrap_ocf_cache_mode_is_valid);
will_return(__wrap_ocf_cache_mode_is_valid, 1);
expect_function_call(__wrap_ocf_log_raw);
will_return(__wrap_ocf_log_raw, 0);
result = _cache_mng_set_cache_mode(&cache, mode_new, flush);
assert_int_equal(result, 0);
assert_int_equal(cache.conf_meta->cache_mode, mode_old);
test_free(cache.conf_meta);
}
static void _cache_mng_set_cache_mode_test03(void **state)
{
int result;
struct ocf_cache cache;
ocf_cache_mode_t mode_old, mode_new;
uint8_t flush;
print_test_description("Flush flag is set, but operation failed -"
" check if error code is correct");
mode_old = ocf_cache_mode_wt;
mode_new = ocf_cache_mode_pt;
cache.conf_meta->cache_mode = mode_old;
flush = 1;
expect_function_call(__wrap_ocf_cache_mode_is_valid);
will_return(__wrap_ocf_cache_mode_is_valid, 1);
expect_function_call(__wrap_ocf_mngt_cache_flush_nolock);
will_return(__wrap_ocf_mngt_cache_flush_nolock, -OCF_ERR_NO_MEM);
result = _cache_mng_set_cache_mode(&cache, mode_new, flush);
assert_int_equal(result, -OCF_ERR_NO_MEM);
assert_int_equal(cache.conf_meta->cache_mode, mode_old);
}
static void _cache_mng_set_cache_mode_test04(void **state)
{
int result;
struct ocf_cache cache;
ocf_cache_mode_t mode_old, mode_new;
uint8_t flush;
int i;
print_test_description("Flush flag is not set, "
"old cache mode is write back. "
"Setting new cache mode is succesfull");
mode_old = ocf_cache_mode_wb;
mode_new = ocf_cache_mode_wa;
flush = 0;
cache.conf_meta = test_malloc(sizeof(struct ocf_superblock_config));
cache.conf_meta->cache_mode = mode_old;
expect_function_call(__wrap_ocf_cache_mode_is_valid);
will_return(__wrap_ocf_cache_mode_is_valid, 1);
for(i = 0; i != OCF_CORE_MAX; ++i) {
expect_function_call(__wrap_env_bit_test);
will_return(__wrap_env_bit_test, 1);
expect_function_call(__wrap_env_atomic_read);
will_return(__wrap_env_atomic_read, 1);
expect_function_call(__wrap_env_atomic_set);
}
expect_function_call(__wrap_ocf_metadata_flush_superblock);
will_return(__wrap_ocf_metadata_flush_superblock, 0);
expect_function_call(__wrap_ocf_log_raw);
will_return(__wrap_ocf_log_raw, 0);
result = _cache_mng_set_cache_mode(&cache, mode_new, flush);
assert_int_equal(result, 0);
assert_int_equal(cache.conf_meta->cache_mode, mode_new);
test_free(cache.conf_meta);
}
static void _cache_mng_set_cache_mode_test05(void **state)
{
int result;
struct ocf_cache cache;
ocf_cache_mode_t mode_old, mode_new;
uint8_t flush;
int i;
print_test_description("Flush flag is not set, "
"flushing metadata superblock fails");
mode_old = ocf_cache_mode_wt;
mode_new = ocf_cache_mode_wa;
flush = 0;
cache.conf_meta = test_malloc(sizeof(struct ocf_superblock_config));
cache.conf_meta->cache_mode = mode_old;
expect_function_call(__wrap_ocf_cache_mode_is_valid);
will_return(__wrap_ocf_cache_mode_is_valid, 1);
expect_function_call(__wrap_ocf_metadata_flush_superblock);
will_return(__wrap_ocf_metadata_flush_superblock, 1);
expect_function_call(__wrap_ocf_log_raw);
will_return(__wrap_ocf_log_raw, 0);
result = _cache_mng_set_cache_mode(&cache, mode_new, flush);
assert_int_equal(result, -OCF_ERR_WRITE_CACHE);
assert_int_equal(cache.conf_meta->cache_mode, mode_old);
test_free(cache.conf_meta);
}
static void _cache_mng_set_cache_mode_test06(void **state)
{
int result;
struct ocf_cache cache;
ocf_cache_mode_t mode_old, mode_new;
uint8_t flush;
int i;
print_test_description("No flush, mode changed successfully");
mode_old = ocf_cache_mode_wt;
mode_new = ocf_cache_mode_wa;
flush = 0;
cache.conf_meta = test_malloc(sizeof(struct ocf_superblock_config));
cache.conf_meta->cache_mode = mode_old;
expect_function_call(__wrap_ocf_cache_mode_is_valid);
will_return(__wrap_ocf_cache_mode_is_valid, 1);
expect_function_call(__wrap_ocf_metadata_flush_superblock);
will_return(__wrap_ocf_metadata_flush_superblock, 0);
expect_function_call(__wrap_ocf_log_raw);
will_return(__wrap_ocf_log_raw, 0);
result = _cache_mng_set_cache_mode(&cache, mode_new, flush);
assert_int_equal(result, 0);
assert_int_equal(cache.conf_meta->cache_mode, mode_new);
test_free(cache.conf_meta);
}
static void _cache_mng_set_cache_mode_test07(void **state)
{
int result;
struct ocf_cache cache;
ocf_cache_mode_t mode_old, mode_new;
uint8_t flush;
int i;
print_test_description("Flush performed, mode changed successfully");
mode_old = ocf_cache_mode_wt;
mode_new = ocf_cache_mode_wa;
flush = 1;
cache.conf_meta = test_malloc(sizeof(struct ocf_superblock_config));
cache.conf_meta->cache_mode = mode_old;
expect_function_call(__wrap_ocf_cache_mode_is_valid);
will_return(__wrap_ocf_cache_mode_is_valid, 1);
expect_function_call(__wrap_ocf_mngt_cache_flush_nolock);
will_return(__wrap_ocf_mngt_cache_flush_nolock, 0);
expect_function_call(__wrap_ocf_metadata_flush_superblock);
will_return(__wrap_ocf_metadata_flush_superblock, 0);
expect_function_call(__wrap_ocf_log_raw);
will_return(__wrap_ocf_log_raw, 0);
result = _cache_mng_set_cache_mode(&cache, mode_new, flush);
assert_int_equal(result, 0);
assert_int_equal(cache.conf_meta->cache_mode, mode_new);
test_free(cache.conf_meta);
}
/*
* Main function. It runs tests.
*/
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(_cache_mng_set_cache_mode_test01),
cmocka_unit_test(_cache_mng_set_cache_mode_test02),
cmocka_unit_test(_cache_mng_set_cache_mode_test03),
cmocka_unit_test(_cache_mng_set_cache_mode_test04),
cmocka_unit_test(_cache_mng_set_cache_mode_test05),
cmocka_unit_test(_cache_mng_set_cache_mode_test06),
cmocka_unit_test(_cache_mng_set_cache_mode_test07)
};
print_message("Unit test of _cache_mng_set_cache_mode\n");
return cmocka_run_group_tests(tests, NULL, NULL);
}

View File

@@ -0,0 +1,176 @@
/*
*<tested_file_path>src/mngt/ocf_mngt_cache.c</tested_file_path>
* <tested_function>ocf_mngt_cache_set_fallback_pt_error_threshold</tested_function>
* <functions_to_leave>
* INSERT HERE LIST OF FUNCTIONS YOU WANT TO LEAVE
* ONE FUNCTION PER LINE
*</functions_to_leave>
*/
#undef static
#undef inline
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "print_desc.h"
#include "ocf/ocf.h"
#include "ocf_mngt_common.h"
#include "../ocf_core_priv.h"
#include "../ocf_queue_priv.h"
#include "../metadata/metadata.h"
#include "../engine/cache_engine.h"
#include "../utils/utils_part.h"
#include "../utils/utils_cache_line.h"
#include "../utils/utils_device.h"
#include "../utils/utils_io.h"
#include "../utils/utils_cache_line.h"
#include "../ocf_utils.h"
#include "../concurrency/ocf_concurrency.h"
#include "../eviction/ops.h"
#include "../ocf_ctx_priv.h"
#include "../cleaning/cleaning.h"
int __wrap_ocf_log_raw(const struct ocf_logger *logger, ocf_logger_lvl_t lvl,
const char *fmt, ...)
{
function_called();
}
ocf_ctx_t __wrap_ocf_cache_get_ctx(ocf_cache_t cache)
{
function_called();
}
int __wrap_ocf_mng_cache_set_fallback_pt(ocf_cache_t cache)
{
function_called();
}
bool __wrap_ocf_fallback_pt_is_on(ocf_cache_t cache)
{
}
static void ocf_mngt_cache_set_fallback_pt_error_threshold_test01(void **state)
{
struct ocf_cache cache;
int new_threshold;
int result;
print_test_description("Appropriate error code on invalid threshold value");
new_threshold = -1;
result = ocf_mngt_cache_set_fallback_pt_error_threshold(&cache, new_threshold);
assert_int_equal(result, -OCF_ERR_INVAL);
new_threshold = 10000001;
result = ocf_mngt_cache_set_fallback_pt_error_threshold(&cache, new_threshold);
assert_int_equal(result, -OCF_ERR_INVAL);
}
static void ocf_mngt_cache_set_fallback_pt_error_threshold_test02(void **state)
{
struct ocf_cache cache;
int new_threshold;
int old_threshold;
print_test_description("Invalid new threshold value doesn't change current threshold");
new_threshold = -1;
old_threshold = cache.fallback_pt_error_threshold = 1000;
ocf_mngt_cache_set_fallback_pt_error_threshold(&cache, new_threshold);
assert_int_equal(cache.fallback_pt_error_threshold, old_threshold);
new_threshold = 10000001;
old_threshold = cache.fallback_pt_error_threshold = 1000;
ocf_mngt_cache_set_fallback_pt_error_threshold(&cache, new_threshold);
assert_int_equal(cache.fallback_pt_error_threshold, old_threshold);
}
static void ocf_mngt_cache_set_fallback_pt_error_threshold_test03(void **state)
{
struct ocf_cache cache;
int new_threshold, old_threshold;
print_test_description("Setting new threshold value");
new_threshold = 5000;
old_threshold = cache.fallback_pt_error_threshold = 1000;
ocf_mngt_cache_set_fallback_pt_error_threshold(&cache, new_threshold);
assert_int_equal(cache.fallback_pt_error_threshold, new_threshold);
new_threshold = 1000000;
old_threshold = cache.fallback_pt_error_threshold = 1000;
ocf_mngt_cache_set_fallback_pt_error_threshold(&cache, new_threshold);
assert_int_equal(cache.fallback_pt_error_threshold, new_threshold);
new_threshold = 0;
old_threshold = cache.fallback_pt_error_threshold = 1000;
ocf_mngt_cache_set_fallback_pt_error_threshold(&cache, new_threshold);
assert_int_equal(cache.fallback_pt_error_threshold, new_threshold);
}
static void ocf_mngt_cache_set_fallback_pt_error_threshold_test04(void **state)
{
struct ocf_cache cache;
int new_threshold;
int result;
print_test_description("Return appropriate value on success");
new_threshold = 5000;
result = ocf_mngt_cache_set_fallback_pt_error_threshold(&cache, new_threshold);
assert_int_equal(result, 0);
new_threshold = 1000000;
result = ocf_mngt_cache_set_fallback_pt_error_threshold(&cache, new_threshold);
assert_int_equal(cache.fallback_pt_error_threshold, new_threshold);
new_threshold = 0;
result = ocf_mngt_cache_set_fallback_pt_error_threshold(&cache, new_threshold);
assert_int_equal(result, 0);
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(ocf_mngt_cache_set_fallback_pt_error_threshold_test01),
cmocka_unit_test(ocf_mngt_cache_set_fallback_pt_error_threshold_test02),
cmocka_unit_test(ocf_mngt_cache_set_fallback_pt_error_threshold_test03),
cmocka_unit_test(ocf_mngt_cache_set_fallback_pt_error_threshold_test04)
};
print_message("Unit test of src/mngt/ocf_mngt_cache.c");
return cmocka_run_group_tests(tests, NULL, NULL);
}

View File

@@ -0,0 +1,2 @@
add_library(ocf_env ocf_env.c)
target_link_libraries(ocf_env pthread)

View File

611
tests/ut/ocf_env/ocf_env.c Normal file
View File

@@ -0,0 +1,611 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#include "ocf_env.h"
#include <sys/types.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
void bug_on(int cond)
{
/* Wrap this to use your implementation */
assert_false(cond);
}
void *env_malloc(size_t size, int flags)
{
return malloc(size);
}
void *env_zalloc(size_t size, int flags)
{
return calloc(1, size);
}
void env_free(const void *ptr)
{
return free((void *) ptr);
}
void *env_vmalloc(size_t size)
{
return malloc(size);
}
void *env_vzalloc(size_t size)
{
return calloc(1, size);
}
void env_vfree(const void *ptr)
{
return free((void *) ptr);
}
uint64_t env_get_free_memory(void)
{
return sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES);
}
/* *** ALLOCATOR *** */
struct _env_allocator {
/*!< Memory pool ID unique name */
char *name;
/*!< Size of specific item of memory pool */
uint32_t item_size;
/*!< Number of currently allocated items in pool */
env_atomic count;
};
size_t env_allocator_align(size_t size)
{
if (size <= 2)
return size;
return (1ULL << 32) >> __builtin_clz(size - 1);
}
struct _env_allocator_item {
uint32_t flags;
uint32_t cpu;
char data[];
};
void *env_allocator_new(env_allocator *allocator)
{
struct _env_allocator_item *item = NULL;
item = calloc(1, allocator->item_size);
if (item) {
item->cpu = 0;
env_atomic_inc(&allocator->count);
}
return &item->data;
}
env_allocator *env_allocator_create(uint32_t size, const char *name)
{
env_allocator *allocator = calloc(1, sizeof(*allocator));
allocator->item_size = size + sizeof(struct _env_allocator_item);
allocator->name = strdup(name);
return allocator;
}
void env_allocator_del(env_allocator *allocator, void *obj)
{
struct _env_allocator_item *item;
item = container_of(obj, struct _env_allocator_item, data);
env_atomic_dec(&allocator->count);
free(item);
}
void env_allocator_destroy(env_allocator *allocator)
{
if (allocator) {
if (env_atomic_read(&allocator->count)) {
fprintf(stderr, "Not all object deallocated\n");
ENV_WARN(true, OCF_PREFIX_SHORT" Cleanup problem\n");
}
free(allocator->name);
free(allocator);
}
}
uint32_t env_allocator_item_count(env_allocator *allocator)
{
return env_atomic_read(&allocator->count);
}
/* *** COMPLETION *** */
void env_completion_init(env_completion *completion)
{
function_called();
check_expected_ptr(completion);
}
void env_completion_wait(env_completion *completion)
{
function_called();
check_expected_ptr(completion);
}
void env_completion_complete(env_completion *completion)
{
function_called();
check_expected_ptr(completion);
}
int env_mutex_init(env_mutex *mutex)
{
function_called();
check_expected_ptr(mutex);
return mock();
}
void env_mutex_lock(env_mutex *mutex)
{
function_called();
check_expected_ptr(mutex);
}
int env_mutex_lock_interruptible(env_mutex *mutex)
{
function_called();
check_expected_ptr(mutex);
return mock();
}
int env_mutex_trylock(env_mutex *mutex)
{
function_called();
check_expected_ptr(mutex);
return mock();
}
void env_mutex_unlock(env_mutex *mutex)
{
function_called();
check_expected_ptr(mutex);
}
int env_mutex_is_locked(env_mutex *mutex)
{
function_called();
check_expected_ptr(mutex);
return mock();
}
int env_rmutex_init(env_rmutex *rmutex)
{
function_called();
check_expected_ptr(rmutex);
return mock();
}
void env_rmutex_lock(env_rmutex *rmutex)
{
function_called();
check_expected_ptr(rmutex);
}
int env_rmutex_lock_interruptible(env_rmutex *rmutex)
{
function_called();
check_expected_ptr(rmutex);
return mock();
}
int env_rmutex_trylock(env_rmutex *rmutex)
{
function_called();
check_expected_ptr(rmutex);
return mock();
}
void env_rmutex_unlock(env_rmutex *rmutex)
{
function_called();
check_expected_ptr(rmutex);
}
int env_rmutex_is_locked(env_rmutex *rmutex)
{
function_called();
check_expected_ptr(rmutex);
return mock();
}
int env_rwsem_init(env_rwsem *s)
{
function_called();
check_expected_ptr(s);
return mock();
}
void env_rwsem_up_read(env_rwsem *s)
{
function_called();
check_expected_ptr(s);
}
void env_rwsem_down_read(env_rwsem *s)
{
function_called();
check_expected_ptr(s);
}
int env_rwsem_down_read_trylock(env_rwsem *s)
{
function_called();
check_expected_ptr(s);
return mock();
}
void env_rwsem_up_write(env_rwsem *s)
{
function_called();
check_expected_ptr(s);
}
void env_rwsem_down_write(env_rwsem *s)
{
function_called();
check_expected_ptr(s);
}
int env_rwsem_down_write_trylock(env_rwsem *s)
{
function_called();
check_expected_ptr(s);
return mock();
}
int env_atomic_read(const env_atomic *a)
{
return *a;
}
void env_atomic_set(env_atomic *a, int i)
{
*a = i;
}
void env_atomic_add(int i, env_atomic *a)
{
*a += i;
}
void env_atomic_sub(int i, env_atomic *a)
{
*a -= i;
}
bool env_atomic_sub_and_test(int i, env_atomic *a)
{
return *a-=i == 0;
}
void env_atomic_inc(env_atomic *a)
{
++*a;
}
void env_atomic_dec(env_atomic *a)
{
--*a;
}
bool env_atomic_dec_and_test(env_atomic *a)
{
return --*a == 0;
}
bool env_atomic_inc_and_test(env_atomic *a)
{
return ++*a == 0;
}
int env_atomic_add_return(int i, env_atomic *a)
{
return *a+=i;
}
int env_atomic_sub_return(int i, env_atomic *a)
{
return *a-=i;
}
int env_atomic_inc_return(env_atomic *a)
{
return ++*a;
}
int env_atomic_dec_return(env_atomic *a)
{
return --*a;
}
int env_atomic_cmpxchg(env_atomic *a, int old, int new_value)
{
int oldval = *a;
if (oldval == old)
*a = new_value;
return oldval;
}
int env_atomic_add_unless(env_atomic *a, int i, int u)
{
int c, old;
c = *a;
for (;;) {
if (c == (u))
break;
old = env_atomic_cmpxchg((a), c, c + (i));
if (old == c)
break;
c = old;
}
return c != (u);
}
long env_atomic64_read(const env_atomic64 *a)
{
return *a;
}
void env_atomic64_set(env_atomic64 *a, long i)
{
*a=i;
}
void env_atomic64_add(long i, env_atomic64 *a)
{
*a += i;
}
void env_atomic64_sub(long i, env_atomic64 *a)
{
*a -= i;
}
void env_atomic64_inc(env_atomic64 *a)
{
++*a;
}
void env_atomic64_dec(env_atomic64 *a)
{
--*a;
}
long env_atomic64_cmpxchg(env_atomic64 *a, long old, long new)
{
long oldval = *a;
if (oldval == old)
*a = new;
return oldval;
}
void env_spinlock_init(env_spinlock *l)
{
function_called();
check_expected_ptr(l);
}
void env_spinlock_lock(env_spinlock *l)
{
function_called();
check_expected_ptr(l);
}
void env_spinlock_unlock(env_spinlock *l)
{
function_called();
check_expected_ptr(l);
}
void env_spinlock_lock_irq(env_spinlock *l)
{
function_called();
check_expected_ptr(l);
}
void env_spinlock_unlock_irq(env_spinlock *l)
{
function_called();
check_expected_ptr(l);
}
void env_rwlock_init(env_rwlock *l)
{
function_called();
check_expected_ptr(l);
}
void env_rwlock_read_lock(env_rwlock *l)
{
function_called();
check_expected_ptr(l);
}
void env_rwlock_read_unlock(env_rwlock *l)
{
function_called();
check_expected_ptr(l);
}
void env_rwlock_write_lock(env_rwlock *l)
{
function_called();
check_expected_ptr(l);
}
void env_rwlock_write_unlock(env_rwlock *l)
{
function_called();
check_expected_ptr(l);
}
void env_waitqueue_init(env_waitqueue *w)
{
w->completed = false;
w->waiting = false;
w->co = NULL;
}
void env_waitqueue_wake_up(env_waitqueue *w)
{
w->completed = true;
if (!w->waiting || !w->co)
return;
}
void env_bit_set(int nr, volatile void *addr)
{
char *byte = (char *) addr + (nr >> 3);
char mask = 1 << (nr & 7);
__sync_or_and_fetch(byte, mask);
}
void env_bit_clear(int nr, volatile void *addr)
{
char *byte = (char *) addr + (nr >> 3);
char mask = 1 << (nr & 7);
mask = ~mask;
__sync_and_and_fetch(byte, mask);
}
bool env_bit_test(int nr, const volatile unsigned long *addr)
{
const char *byte = (char *) addr + (nr >> 3);
char mask = 1 << (nr & 7);
return !!(*byte & mask);
}
/* *** SCHEDULING *** */
void env_touch_softlockup_wd(void)
{
function_called();
}
void env_schedule(void)
{
function_called();
}
int env_in_interrupt(void)
{
function_called();
return mock();
}
uint64_t env_get_tick_count(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
uint64_t env_ticks_to_msecs(uint64_t j)
{
return j;
}
uint64_t env_ticks_to_secs(uint64_t j)
{
return j / 1000;
}
uint64_t env_secs_to_ticks(uint64_t j)
{
return j * 1000;
}
int env_memset(void *dest, size_t count, int ch)
{
memset(dest, ch, count);
return 0;
}
int env_memcpy(void *dest, size_t destsz, const void * src, size_t count)
{
if (destsz < count)
memcpy(dest, src, destsz);
else
memcpy(dest, src, count);
return 0;
}
int env_memcmp(const void *str1, size_t n1, const void *str2, size_t n2,
int *diff)
{
size_t n = n1 > n2 ? n2 : n1;
*diff = memcmp(str1, str2, n);
return 0;
}
int env_strncpy(char * dest, size_t destsz, const char *src, size_t count)
{
if (destsz < count)
strncpy(dest, src, destsz);
else
strncpy(dest, src, count);
return 0;
}
size_t env_strnlen(const char *str, size_t strsz)
{
return strlen(str);
}
void env_sort(void *base, size_t num, size_t size,
int (*cmp_fn)(const void *, const void *),
void (*swap_fn)(void *, void *, int size))
{
qsort(base, num, size, cmp_fn);
}
int env_strncmp(const char * str1, const char * str2, size_t num)
{
return strncmp(str1, str2, num);
}
void env_msleep(uint64_t n)
{
}
/* *** CRC *** */
uint32_t env_crc32(uint32_t crc, uint8_t const *data, size_t len)
{
function_called();
check_expected(crc);
check_expected(len);
check_expected_ptr(data);
return mock();
}

358
tests/ut/ocf_env/ocf_env.h Normal file
View File

@@ -0,0 +1,358 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef __LIBOCF_ENV_H__
#define __LIBOCF_ENV_H__
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef __USE_GNU
#define __USE_GNU
#endif
#include <linux/limits.h>
#include <linux/stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <pthread.h>
#include <assert.h>
#include <semaphore.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <sys/time.h>
#include "ocf_env_list.h"
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef uint64_t sector_t;
#define ENV_PRIu64 "lu"
#define __packed __attribute__((packed))
#define __aligned(x) __attribute__((aligned(x)))
/* linux sector 512-bytes */
#define ENV_SECTOR_SHIFT 9
#define PAGE_SIZE 4096
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
/* *** MEMORY MANAGEMENT *** */
#define ENV_MEM_NORMAL 0
#define ENV_MEM_NOIO 1
#define ENV_MEM_ATOMIC 2
#define min(x, y) MIN(x, y)
#define ENV_WARN(cond, fmt, args...) ({})
#define ENV_WARN_ON(cond) ({ \
if (unlikely(cond)) \
fprintf(stderr, "WARNING (%s:%d)\n", \
__FILE__, __LINE__); \
})
#define ENV_BUG() ({ \
fprintf(stderr, "BUG (%s:%d)\n", \
__FILE__, __LINE__); \
assert(0); \
abort(); \
})
#define ENV_BUG_ON(cond) bug_on((int)cond);
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type, member) );})
/* ATOMICS */
#ifndef atomic_read
#define atomic_read(ptr) (*(__typeof__(*ptr) *volatile) (ptr))
#endif
#ifndef atomic_set
#define atomic_set(ptr, i) ((*(__typeof__(*ptr) *volatile) (ptr)) = (i))
#endif
#define likely(x) (x)
#define unlikely(x) (x)
/*
* Bug on for testing
*/
void bug_on(int cond);
void *env_malloc(size_t size, int flags);
void *env_zalloc(size_t size, int flags);
void env_free(const void *ptr);
void *env_vmalloc(size_t size);
void *env_vzalloc(size_t size);
void env_vfree(const void *ptr);
uint64_t env_get_free_memory(void);
/* *** ALLOCATOR *** */
typedef struct _env_allocator env_allocator;
env_allocator *env_allocator_create(uint32_t size, const char *name);
void env_allocator_destroy(env_allocator *allocator);
void *env_allocator_new(env_allocator *allocator);
void env_allocator_del(env_allocator *allocator, void *item);
uint32_t env_allocator_item_count(env_allocator *allocator);
/* *** MUTEX *** */
typedef struct {
pthread_mutex_t m;
} env_mutex;
int env_mutex_init(env_mutex *mutex);
void env_mutex_lock(env_mutex *mutex);
int env_mutex_lock_interruptible(env_mutex *mutex);
int env_mutex_trylock(env_mutex *mutex);
void env_mutex_unlock(env_mutex *mutex);
int env_mutex_is_locked(env_mutex *mutex);
/* *** RECURSIVE MUTEX *** */
typedef env_mutex env_rmutex;
int env_rmutex_init(env_rmutex *rmutex);
void env_rmutex_lock(env_rmutex *rmutex);
int env_rmutex_lock_interruptible(env_rmutex *rmutex);
int env_rmutex_trylock(env_rmutex *rmutex);
void env_rmutex_unlock(env_rmutex *rmutex);
int env_rmutex_is_locked(env_rmutex *rmutex);
/* *** RW SEMAPHORE *** */
typedef struct {
pthread_rwlock_t lock;
} env_rwsem;
int env_rwsem_init(env_rwsem *s);
void env_rwsem_up_read(env_rwsem *s);
void env_rwsem_down_read(env_rwsem *s);
int env_rwsem_down_read_trylock(env_rwsem *s);
void env_rwsem_up_write(env_rwsem *s);
void env_rwsem_down_write(env_rwsem *s);
int env_rwsem_down_write_trylock(env_rwsem *s);
int env_rwsem_is_locked(env_rwsem *s);
/* *** ATOMIC VARIABLES *** */
typedef int env_atomic;
typedef long env_atomic64;
int env_atomic_read(const env_atomic *a);
void env_atomic_set(env_atomic *a, int i);
void env_atomic_add(int i, env_atomic *a);
void env_atomic_sub(int i, env_atomic *a);
bool env_atomic_sub_and_test(int i, env_atomic *a);
void env_atomic_inc(env_atomic *a);
void env_atomic_dec(env_atomic *a);
bool env_atomic_dec_and_test(env_atomic *a);
bool env_atomic_inc_and_test(env_atomic *a);
int env_atomic_add_return(int i, env_atomic *a);
int env_atomic_sub_return(int i, env_atomic *a);
int env_atomic_inc_return(env_atomic *a);
int env_atomic_dec_return(env_atomic *a);
int env_atomic_cmpxchg(env_atomic *a, int old, int new_value);
int env_atomic_add_unless(env_atomic *a, int i, int u);
long env_atomic64_read(const env_atomic64 *a);
void env_atomic64_set(env_atomic64 *a, long i);
void env_atomic64_add(long i, env_atomic64 *a);
void env_atomic64_sub(long i, env_atomic64 *a);
void env_atomic64_inc(env_atomic64 *a);
void env_atomic64_dec(env_atomic64 *a);
long env_atomic64_cmpxchg(env_atomic64 *a, long old, long new);
typedef int Coroutine;
/* *** COMPLETION *** */
struct completion {
bool completed;
bool waiting;
Coroutine *co;
};
typedef struct completion env_completion;
void env_completion_init(env_completion *completion);
void env_completion_wait(env_completion *completion);
void env_completion_complete(env_completion *completion);
/* *** SPIN LOCKS *** */
typedef struct {
} env_spinlock;
void env_spinlock_init(env_spinlock *l);
void env_spinlock_lock(env_spinlock *l);
void env_spinlock_unlock(env_spinlock *l);
void env_spinlock_lock_irq(env_spinlock *l);
void env_spinlock_unlock_irq(env_spinlock *l);
#define env_spinlock_lock_irqsave(l, flags) \
env_spinlock_lock(l); (void)flags;
#define env_spinlock_unlock_irqrestore(l, flags) \
env_spinlock_unlock(l); (void)flags;
/* *** RW LOCKS *** */
typedef struct {
} env_rwlock;
void env_rwlock_init(env_rwlock *l);
void env_rwlock_read_lock(env_rwlock *l);
void env_rwlock_read_unlock(env_rwlock *l);
void env_rwlock_write_lock(env_rwlock *l);
void env_rwlock_write_unlock(env_rwlock *l);
/* *** WAITQUEUE *** */
typedef struct {
bool waiting;
bool completed;
Coroutine *co;
} env_waitqueue;
void env_waitqueue_init(env_waitqueue *w);
void env_waitqueue_wake_up(env_waitqueue *w);
#define env_waitqueue_wait(w, condition) \
({ \
int __ret = 0; \
if (!(condition) && !w.completed) { \
w.waiting = true; \
} \
w.co = NULL; \
w.waiting = false; \
w.completed = false; \
__ret = __ret; \
})
/* *** BIT OPERATIONS *** */
void env_bit_set(int nr, volatile void *addr);
void env_bit_clear(int nr, volatile void *addr);
bool env_bit_test(int nr, const volatile unsigned long *addr);
/* *** SCHEDULING *** */
void env_touch_softlockup_wd(void);
void env_schedule(void);
int env_in_interrupt(void);
uint64_t env_get_tick_count(void);
uint64_t env_ticks_to_msecs(uint64_t j);
uint64_t env_ticks_to_secs(uint64_t j);
uint64_t env_secs_to_ticks(uint64_t j);
/* *** STRING OPERATIONS *** */
int env_memset(void *dest, size_t count, int ch);
int env_memcpy(void *dest, size_t destsz, const void * src, size_t count);
int env_memcmp(const void *str1, size_t n1, const void *str2, size_t n2,
int *diff);
int env_strncpy(char * dest, size_t destsz, const char *src, size_t srcsz);
size_t env_strnlen(const char *str, size_t strsz);
int env_strncmp(const char * str1, const char * str2, size_t num);
/* *** SORTING *** */
void env_sort(void *base, size_t num, size_t size,
int (*cmp_fn)(const void *, const void *),
void (*swap_fn)(void *, void *, int size));
void env_msleep(uint64_t n);
/* *** CRC *** */
uint32_t env_crc32(uint32_t crc, uint8_t const *data, size_t len);
#endif /* __OCF_ENV_H__ */

View File

@@ -0,0 +1,13 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef __OCF_ENV_HEADERS_H__
#define __OCF_ENV_HEADERS_H__
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#endif /* __OCF_ENV_HEADERS_H__ */

View File

@@ -0,0 +1,146 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef __OCF_LIST_H__
#define __OCF_LIST_H__
#define LIST_POISON1 ((void *)0x101)
#define LIST_POISON2 ((void *)0x202)
/**
* List entry structure mimicking linux kernel based one.
*/
struct list_head {
struct list_head *next;
struct list_head *prev;
};
/**
* start an empty list
*/
#define INIT_LIST_HEAD(l) { (l)->prev = l; (l)->next = l; }
/**
* Add item to list head.
* @param it list entry to be added
* @param l1 list main node (head)
*/
static inline void list_add(struct list_head *it, struct list_head *l1)
{
it->prev = l1;
it->next = l1->next;
l1->next->prev = it;
l1->next = it;
}
/**
* Add item it to tail.
* @param it list entry to be added
* @param l1 list main node (head)
*/
static inline void list_add_tail(struct list_head *it, struct list_head *l1)
{
it->prev = l1->prev;
it->next = l1;
l1->prev->next = it;
l1->prev = it;
}
/**
* check if a list is empty (return true)
* @param l1 list main node (head)
*/
static inline int list_empty(struct list_head *l1)
{
return l1->next == l1;
}
/**
* delete an entry from a list
* @param it list entry to be deleted
*/
static inline void list_del(struct list_head *it)
{
it->next->prev = it->prev;
it->prev->next = it->next;
}
/**
* Extract an entry.
* @param list_head_i list head item, from which entry is extracted
* @param item_type type (struct) of list entry
* @param field_name name of list_head field within item_type
*/
#define list_entry(list_head_i, item_type, field_name) \
(item_type *)(((void*)(list_head_i)) - offsetof(item_type, field_name))
#define list_first_entry(list_head_i, item_type, field_name) \
list_entry((list_head_i)->next, item_type, field_name)
/**
* @param iterator uninitialized list_head pointer, to be used as iterator
* @param plist list head (main node)
*/
#define list_for_each(iterator, plist) \
for (iterator = (plist)->next; \
(iterator)->next != (plist)->next; \
iterator = (iterator)->next)
/**
* Safe version of list_for_each which works even if entries are deleted during
* loop.
* @param iterator uninitialized list_head pointer, to be used as iterator
* @param q another uninitialized list_head, used as helper
* @param plist list head (main node)
*/
/*
* Algorithm handles situation, where q is deleted.
* consider in example 3 element list with header h:
*
* h -> 1 -> 2 -> 3 ->
*1. i q
*
*2. i q
*
*3. q i
*/
#define list_for_each_safe(iterator, q, plist) \
for (iterator = (q = (plist)->next->next)->prev; \
(q) != (plist)->next; \
iterator = (q = (q)->next)->prev)
#define _list_entry_helper(item, head, field_name) \
list_entry(head, typeof(*item), field_name)
/**
* Iterate over list entries.
* @param list pointer to list item (iterator)
* @param plist pointer to list_head item
* @param field_name name of list_head field in list entry
*/
#define list_for_each_entry(item, plist, field_name) \
for (item = _list_entry_helper(item, (plist)->next, field_name); \
_list_entry_helper(item, (item)->field_name.next, field_name) !=\
_list_entry_helper(item, (plist)->next, field_name); \
item = _list_entry_helper(item, (item)->field_name.next, field_name))
/**
* Safe version of list_for_each_entry which works even if entries are deleted
* during loop.
* @param list pointer to list item (iterator)
* @param q another pointer to list item, used as helper
* @param plist pointer to list_head item
* @param field_name name of list_head field in list entry
*/
#define list_for_each_entry_safe(item, q, plist, field_name) \
for (item = _list_entry_helper(item, (plist)->next, field_name), \
q = _list_entry_helper(item, (item)->field_name.next, field_name); \
_list_entry_helper(item, (item)->field_name.next, field_name) != \
_list_entry_helper(item, (plist)->next, field_name); \
item = q, q = _list_entry_helper(q, (q)->field_name.next, field_name))
#endif

6
tests/ut/print_desc.h Normal file
View File

@@ -0,0 +1,6 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#define print_test_description(description) print_message("[ DESC ] %s\n", description);