Merge pull request #172 from robertbaldyga/utils-cleanup

Utils cleanup
This commit is contained in:
Michal Rakowski 2019-05-28 11:08:12 +02:00 committed by GitHub
commit b609164f78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
51 changed files with 360 additions and 678 deletions

View File

@ -16,7 +16,6 @@
#include "ocf_def.h"
#include "ocf_types.h"
#include "ocf_utilities.h"
#include "ocf_io.h"
#include "ocf_volume.h"
#include "ocf_cache.h"

View File

@ -1,74 +0,0 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef __OCF_UTILITIES_H__
#define __OCF_UTILITIES_H__
/**
* @file
* @brief OCF memory pool reference
*/
struct ocf_mpool;
/**
* @brief Create OCF memory pool
*
* @param cache OCF cache instance
* @param size Size of particular item
* @param hdr_size Header size before array of items
* @param flags Allocation flags
* @param mpool_max Maximal allocator size (power of two)
* @param fmt_name Format name of allocator
* @param ... Format parameters
*
* @return OCF memory pool reference
*/
struct ocf_mpool *ocf_mpool_create(struct ocf_cache *cache,
uint32_t hdr_size, uint32_t size, int flags, int mpool_max,
const char *name_perfix);
/**
* @brief Destroy existing memory pool
*
* @param mpool memory pool
*/
void ocf_mpool_destroy(struct ocf_mpool *mpool);
/**
* @brief Allocate new items of memory pool
*
* @note Allocation based on ATOMIC memory pool and this function can be called
* when IRQ disable
*
* @param mpool OCF memory pool reference
* @param count Count of elements to be allocated
*
* @return Pointer to the new items
*/
void *ocf_mpool_new(struct ocf_mpool *mpool, uint32_t count);
/**
* @brief Allocate new items of memory pool with specified allocation flag
*
* @param mpool OCF memory pool reference
* @param count Count of elements to be allocated
* @param flags Kernel allocation falgs
*
* @return Pointer to the new items
*/
void *ocf_mpool_new_f(struct ocf_mpool *mpool, uint32_t count, int flags);
/**
* @brief Free existing items of memory pool
*
* @param mpool OCF memory pool reference
* @param items Items to be freed
* @param count - Count of elements to be free
*/
void ocf_mpool_del(struct ocf_mpool *mpool, void *items, uint32_t count);
#endif /* __OCF_UTILITIES_H__ */

View File

@ -9,7 +9,7 @@
#include "../metadata/metadata.h"
#include "../utils/utils_cleaner.h"
#include "../utils/utils_cache_line.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../cleaning/acp.h"
#include "../engine/engine_common.h"
#include "../concurrency/ocf_cache_concurrency.h"

View File

@ -10,7 +10,7 @@
#include "../metadata/metadata.h"
#include "../utils/utils_cleaner.h"
#include "../utils/utils_part.h"
#include "../utils/utils_allocator.h"
#include "../utils/utils_realloc.h"
#include "../concurrency/ocf_cache_concurrency.h"
#include "../ocf_def_priv.h"
#include "cleaning_priv.h"

View File

@ -7,7 +7,7 @@
#include "../ocf_priv.h"
#include "../ocf_request.h"
#include "../utils/utils_cache_line.h"
#include "../utils/utils_allocator.h"
#include "../utils/utils_realloc.h"
#define OCF_CACHE_CONCURRENCY_DEBUG 0

View File

@ -21,7 +21,7 @@
#include "engine_ops.h"
#include "../utils/utils_part.h"
#include "../utils/utils_refcnt.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../metadata/metadata.h"
#include "../eviction/eviction.h"

View File

@ -10,7 +10,7 @@
#include "engine_inv.h"
#include "engine_common.h"
#include "cache_engine.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../utils/utils_io.h"
#include "../concurrency/ocf_concurrency.h"

View File

@ -11,7 +11,7 @@
#define OCF_ENGINE_DEBUG_IO_NAME "common"
#include "engine_debug.h"
#include "../utils/utils_cache_line.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../utils/utils_cleaner.h"
#include "../metadata/metadata.h"
#include "../eviction/eviction.h"

View File

@ -7,7 +7,7 @@
#include "engine_d2c.h"
#include "engine_common.h"
#include "cache_engine.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../utils/utils_io.h"
#include "../metadata/metadata.h"

View File

@ -8,7 +8,7 @@
#include "engine_common.h"
#include "engine_discard.h"
#include "../metadata/metadata.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../utils/utils_io.h"
#include "../utils/utils_cache_line.h"
#include "../concurrency/ocf_concurrency.h"

View File

@ -9,7 +9,7 @@
#include "engine_common.h"
#include "engine_pt.h"
#include "engine_wb.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../utils/utils_part.h"
#include "../utils/utils_io.h"
#include "../concurrency/ocf_concurrency.h"

View File

@ -8,7 +8,7 @@
#include "engine_inv.h"
#include "engine_common.h"
#include "cache_engine.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../utils/utils_cache_line.h"
#include "../metadata/metadata.h"
#include "../concurrency/ocf_concurrency.h"

View File

@ -7,7 +7,7 @@
#include "engine_common.h"
#include "cache_engine.h"
#include "engine_ops.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../utils/utils_io.h"
#define OCF_ENGINE_DEBUG_IO_NAME "ops"

View File

@ -7,7 +7,7 @@
#include "engine_pt.h"
#include "engine_common.h"
#include "cache_engine.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../utils/utils_io.h"
#include "../utils/utils_part.h"
#include "../metadata/metadata.h"

View File

@ -13,7 +13,7 @@
#include "cache_engine.h"
#include "../concurrency/ocf_concurrency.h"
#include "../utils/utils_io.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../utils/utils_cache_line.h"
#include "../utils/utils_part.h"
#include "../metadata/metadata.h"

View File

@ -7,7 +7,7 @@
#include "engine_wa.h"
#include "engine_common.h"
#include "cache_engine.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../utils/utils_io.h"
#include "../metadata/metadata.h"

View File

@ -9,7 +9,7 @@
#include "engine_common.h"
#include "engine_wb.h"
#include "../metadata/metadata.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../utils/utils_io.h"
#include "../utils/utils_cache_line.h"
#include "../utils/utils_part.h"

View File

@ -8,7 +8,7 @@
#include "engine_wi.h"
#include "engine_common.h"
#include "../concurrency/ocf_concurrency.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../utils/utils_cache_line.h"
#include "../utils/utils_io.h"
#include "../metadata/metadata.h"

View File

@ -8,7 +8,7 @@
#include "engine_wt.h"
#include "engine_inv.h"
#include "engine_common.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../utils/utils_io.h"
#include "../utils/utils_cache_line.h"
#include "../utils/utils_part.h"

View File

@ -8,7 +8,7 @@
#include "engine_zero.h"
#include "engine_common.h"
#include "../concurrency/ocf_concurrency.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../utils/utils_cache_line.h"
#include "../utils/utils_io.h"
#include "../metadata/metadata.h"

View File

@ -11,7 +11,7 @@
#include "../concurrency/ocf_concurrency.h"
#include "../mngt/ocf_mngt_common.h"
#include "../engine/engine_zero.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#define OCF_EVICTION_MAX_SCAN 1024

View File

@ -9,7 +9,7 @@
#include "../engine/engine_common.h"
#include "../engine/engine_bf.h"
#include "../utils/utils_cache_line.h"
#include "../utils/utils_allocator.h"
#include "../utils/utils_realloc.h"
#include "../utils/utils_io.h"
#include "../ocf_def_priv.h"

View File

@ -11,7 +11,7 @@
#include "../engine/cache_engine.h"
#include "../engine/engine_common.h"
#include "../utils/utils_io.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../ocf_def_priv.h"
#include "../ocf_priv.h"

View File

@ -11,7 +11,7 @@
#include "../ocf_cache_priv.h"
#include "../ocf_ctx_priv.h"
#include "../utils/utils_io.h"
#include "../utils/utils_allocator.h"
#include "../utils/utils_realloc.h"
int ocf_metadata_updater_init(ocf_cache_t cache)
{

View File

@ -13,12 +13,10 @@
#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 "../utils/utils_pipeline.h"
#include "../utils/utils_refcnt.h"
#include "../ocf_utils.h"
#include "../concurrency/ocf_concurrency.h"
#include "../eviction/ops.h"
#include "../ocf_ctx_priv.h"

View File

@ -5,12 +5,12 @@
#include "ocf/ocf.h"
#include "ocf_mngt_common.h"
#include "ocf_mngt_core_priv.h"
#include "../ocf_priv.h"
#include "../ocf_ctx_priv.h"
#include "../metadata/metadata.h"
#include "../engine/cache_engine.h"
#include "../utils/utils_req.h"
#include "../utils/utils_device.h"
#include "../ocf_request.h"
#include "../eviction/ops.h"
#include "../ocf_logger_priv.h"
#include "../ocf_queue_priv.h"
@ -96,7 +96,7 @@ void cache_mng_core_remove_from_meta(struct ocf_cache *cache, int core_id)
cache->core_conf_meta[core_id].added = false;
/* Clear UUID of core */
ocf_metadata_clear_core_uuid(&cache->core[core_id]);
ocf_mngt_core_clear_uuid_metadata(&cache->core[core_id]);
cache->core_conf_meta[core_id].seq_no = OCF_SEQ_NO_INVALID;
OCF_METADATA_UNLOCK_WR();

View File

@ -9,7 +9,6 @@
#include "../ocf_priv.h"
#include "../metadata/metadata.h"
#include "../engine/cache_engine.h"
#include "../utils/utils_device.h"
#include "../utils/utils_pipeline.h"
#include "../ocf_stats_priv.h"
#include "../ocf_def_priv.h"
@ -22,6 +21,59 @@ static ocf_seq_no_t _ocf_mngt_get_core_seq_no(ocf_cache_t cache)
return ++cache->conf_meta->curr_core_seq_no;
}
static int _ocf_uuid_set(const struct ocf_volume_uuid *uuid,
struct ocf_metadata_uuid *muuid)
{
int result;
if (!uuid->data || !muuid->data)
return -EINVAL;
if (uuid->size > sizeof(muuid->data))
return -ENOBUFS;
result = env_memcpy(muuid->data, sizeof(muuid->data),
uuid->data, uuid->size);
if (result)
return result;
result = env_memset(muuid->data + uuid->size,
sizeof(muuid->data) - uuid->size, 0);
if (result)
return result;
muuid->size = uuid->size;
return 0;
}
static int ocf_mngt_core_set_uuid_metadata(ocf_core_t core,
const struct ocf_volume_uuid *uuid,
struct ocf_volume_uuid *new_uuid)
{
ocf_cache_t cache = ocf_core_get_cache(core);
struct ocf_metadata_uuid *muuid = ocf_metadata_get_core_uuid(cache,
ocf_core_get_id(core));
if (_ocf_uuid_set(uuid, muuid))
return -ENOBUFS;
if (new_uuid) {
new_uuid->data = muuid->data;
new_uuid->size = muuid->size;
}
return 0;
}
void ocf_mngt_core_clear_uuid_metadata(ocf_core_t core)
{
struct ocf_volume_uuid uuid = { .size = 0, };
ocf_mngt_core_set_uuid_metadata(core, &uuid, NULL);
}
static int _ocf_mngt_cache_try_add_core(ocf_cache_t cache, ocf_core_t *core,
struct ocf_mngt_core_config *cfg)
{
@ -118,7 +170,7 @@ static void _ocf_mngt_cache_add_core_handle_error(
ocf_volume_deinit(volume);
if (context->flags.uuid_set)
ocf_metadata_clear_core_uuid(core);
ocf_mngt_core_clear_uuid_metadata(core);
}
static void _ocf_mngt_cache_add_core_flush_sb_complete(void *priv, int error)
@ -154,7 +206,7 @@ static void _ocf_mngt_cache_add_core(ocf_cache_t cache,
volume->cache = cache;
/* Set uuid */
result = ocf_metadata_set_core_uuid(core, &cfg->uuid, &new_uuid);
result = ocf_mngt_core_set_uuid_metadata(core, &cfg->uuid, &new_uuid);
if (result)
OCF_PL_FINISH_RET(context->pipeline, result);
@ -830,7 +882,7 @@ int ocf_mngt_core_set_uuid(ocf_core_t core, const struct ocf_volume_uuid *uuid)
return 0;
}
result = ocf_metadata_set_core_uuid(core, uuid, NULL);
result = ocf_mngt_core_set_uuid_metadata(core, uuid, NULL);
if (result)
return result;

View File

@ -10,4 +10,6 @@
int ocf_mngt_core_init_front_volume(ocf_core_t core);
void ocf_mngt_core_clear_uuid_metadata(ocf_core_t core);
#endif /* __OCF_MNGT_CORE_PRIV_H__ */

View File

@ -15,7 +15,7 @@
#include "../utils/utils_part.h"
#include "../utils/utils_pipeline.h"
#include "../utils/utils_refcnt.h"
#include "../utils/utils_req.h"
#include "../ocf_request.h"
#include "../ocf_def_priv.h"
struct ocf_mngt_cache_flush_context;

View File

@ -7,7 +7,7 @@
#include "metadata/metadata.h"
#include "engine/cache_engine.h"
#include "utils/utils_cache_line.h"
#include "utils/utils_req.h"
#include "ocf_request.h"
#include "utils/utils_part.h"
#include "ocf_priv.h"
#include "ocf_cache_priv.h"

View File

@ -9,9 +9,8 @@
#include "ocf_io_priv.h"
#include "metadata/metadata.h"
#include "engine/cache_engine.h"
#include "utils/utils_req.h"
#include "ocf_request.h"
#include "utils/utils_part.h"
#include "utils/utils_device.h"
#include "ocf_request.h"
#include "ocf_trace_priv.h"

View File

@ -7,7 +7,7 @@
#include "ocf_ctx_priv.h"
#include "ocf_priv.h"
#include "ocf_volume_priv.h"
#include "ocf_utils.h"
#include "ocf_request.h"
#include "ocf_logger_priv.h"
#include "ocf_core_priv.h"
#include "mngt/ocf_mngt_core_pool_priv.h"
@ -141,7 +141,7 @@ int ocf_ctx_create(ocf_ctx_t *ctx, const struct ocf_ctx_config *cfg)
if (ret)
goto err_ctx;
ret = ocf_utils_init(ocf_ctx);
ret = ocf_req_allocator_init(ocf_ctx);
if (ret)
goto err_logger;
@ -156,7 +156,7 @@ int ocf_ctx_create(ocf_ctx_t *ctx, const struct ocf_ctx_config *cfg)
return 0;
err_utils:
ocf_utils_deinit(ocf_ctx);
ocf_req_allocator_deinit(ocf_ctx);
err_logger:
ocf_logger_close(&ocf_ctx->logger);
err_ctx:
@ -190,7 +190,7 @@ void ocf_ctx_put(ocf_ctx_t ctx)
ocf_mngt_core_pool_deinit(ctx);
ocf_core_volume_type_deinit(ctx);
ocf_utils_deinit(ctx);
ocf_req_allocator_deinit(ctx);
ocf_logger_close(&ctx->logger);
env_free(ctx);
}

View File

@ -30,7 +30,6 @@ struct ocf_ctx {
struct {
struct ocf_req_allocator *req;
env_allocator *core_io_allocator;
} resources;
};

View File

@ -4,11 +4,10 @@
*/
#include "ocf/ocf.h"
#include "utils_req.h"
#include "utils_cache_line.h"
#include "../ocf_request.h"
#include "../ocf_cache_priv.h"
#include "../ocf_queue_priv.h"
#include "ocf_request.h"
#include "ocf_cache_priv.h"
#include "ocf_queue_priv.h"
#include "utils/utils_cache_line.h"
#define OCF_UTILS_RQ_DEBUG 0
@ -76,20 +75,20 @@ int ocf_req_allocator_init(struct ocf_ctx *ocf_ctx)
req = ocf_ctx->resources.req;
if (!req)
goto ocf_utils_req_init_ERROR;
goto err;
for (i = 0; i < ARRAY_SIZE(req->allocator); i++) {
req->size[i] = ocf_req_sizeof(1 << i);
if (snprintf(name, sizeof(name), ALLOCATOR_NAME_FMT,
(1 << i)) < 0) {
goto ocf_utils_req_init_ERROR;
goto err;
}
req->allocator[i] = env_allocator_create(req->size[i], name);
if (!req->allocator[i])
goto ocf_utils_req_init_ERROR;
goto err;
OCF_DEBUG_PARAM(cache, "New request allocator, lines = %u, "
"size = %lu", 1 << i, req->size[i]);
@ -97,10 +96,8 @@ int ocf_req_allocator_init(struct ocf_ctx *ocf_ctx)
return 0;
ocf_utils_req_init_ERROR:
err:
ocf_req_allocator_deinit(ocf_ctx);
return -1;
}

View File

@ -8,6 +8,8 @@
#include "ocf_env.h"
struct ocf_req_allocator;
struct ocf_req_info {
/* Number of hits, invalid, misses. */
unsigned int hit_no;
@ -201,4 +203,131 @@ struct ocf_request {
typedef void (*ocf_req_end_t)(struct ocf_request *req, int error);
#endif
/**
* @brief Initialize OCF request allocation utility
*
* @param cache - OCF cache instance
* @return Operation status 0 - successful, non-zero failure
*/
int ocf_req_allocator_init(struct ocf_ctx *ocf_ctx);
/**
* @brief De-initialize OCF request allocation utility
*
* @param cache - OCF cache instance
*/
void ocf_req_allocator_deinit(struct ocf_ctx *ocf_ctx);
/**
* @brief Allocate new OCF request
*
* @param queue - I/O queue handle
* @param core - OCF core instance
* @param addr - LBA of request
* @param bytes - number of bytes of request
* @param rw - Read or Write
*
* @return new OCF request
*/
struct ocf_request *ocf_req_new(ocf_queue_t queue, ocf_core_t core,
uint64_t addr, uint32_t bytes, int rw);
/**
* @brief Allocate OCF request map
*
* @param req OCF request
*
* @retval 0 Allocation succeed
* @retval non-zero Allocation failed
*/
int ocf_req_alloc_map(struct ocf_request *req);
/**
* @brief Allocate new OCF request with NOIO map allocation for huge request
*
* @param queue - I/O queue handle
* @param core - OCF core instance
* @param addr - LBA of request
* @param bytes - number of bytes of request
* @param rw - Read or Write
*
* @return new OCF request
*/
struct ocf_request *ocf_req_new_extended(ocf_queue_t queue, ocf_core_t core,
uint64_t addr, uint32_t bytes, int rw);
/**
* @brief Allocate new OCF request for DISCARD operation
*
* @param queue - I/O queue handle
* @param core - OCF core instance
* @param addr - LBA of request
* @param bytes - number of bytes of request
* @param rw - Read or Write
*
* @return new OCF request
*/
struct ocf_request *ocf_req_new_discard(ocf_queue_t queue, ocf_core_t core,
uint64_t addr, uint32_t bytes, int rw);
/**
* @brief Increment OCF request reference count
*
* @param req - OCF request
*/
void ocf_req_get(struct ocf_request *req);
/**
* @brief Decrement OCF request reference. If reference is 0 then request will
* be deallocated
*
* @param req - OCF request
*/
void ocf_req_put(struct ocf_request *req);
/**
* @brief Clear OCF request info
*
* @param req - OCF request
*/
void ocf_req_clear_info(struct ocf_request *req);
/**
* @brief Clear OCF request map
*
* @param req - OCF request
*/
void ocf_req_clear_map(struct ocf_request *req);
/**
* @brief Clear OCF request
*
* @param req - OCF request
*/
static inline void ocf_req_clear(struct ocf_request *req)
{
ocf_req_clear_info(req);
ocf_req_clear_map(req);
env_atomic_set(&req->lock_remaining, 0);
env_atomic_set(&req->req_remaining, 0);
}
/**
* @brief Return OCF request reference count
*
* @param req - OCF request
* @return OCF request reference count
*/
static inline int ocf_req_ref_count(struct ocf_request *req)
{
return env_atomic_read(&req->ref_count);
}
static inline bool ocf_req_is_4k(uint64_t addr, uint32_t bytes)
{
return !((addr % PAGE_SIZE) || (bytes % PAGE_SIZE));
}
#endif /* __OCF_REQUEST_H__ */

View File

@ -1,43 +0,0 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#include "ocf/ocf.h"
#include "ocf_cache_priv.h"
#include "utils/utils_req.h"
#include "ocf_utils.h"
#include "ocf_ctx_priv.h"
int ocf_utils_init(struct ocf_ctx *ocf_ctx)
{
int result;
result = ocf_req_allocator_init(ocf_ctx);
if (result)
goto ocf_utils_init_ERROR;
ocf_ctx->resources.core_io_allocator =
env_allocator_create(sizeof(struct ocf_core_io),
"ocf_io");
if (!ocf_ctx->resources.core_io_allocator)
goto ocf_utils_init_ERROR;
return 0;
ocf_utils_init_ERROR:
ocf_utils_deinit(ocf_ctx);
return -1;
}
void ocf_utils_deinit(struct ocf_ctx *ocf_ctx)
{
ocf_req_allocator_deinit(ocf_ctx);
if (ocf_ctx->resources.core_io_allocator) {
env_allocator_destroy(ocf_ctx->resources.core_io_allocator);
ocf_ctx->resources.core_io_allocator = NULL;
}
}

View File

@ -1,13 +0,0 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef OCF_UTILS_H_
#define OCF_UTILS_H_
int ocf_utils_init(struct ocf_ctx *ocf_ctx);
void ocf_utils_deinit(struct ocf_ctx *ocf_ctx);
#endif /* OCF_UTILS_H_ */

View File

@ -1,267 +0,0 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#include "ocf/ocf.h"
#include "utils_allocator.h"
#include "../ocf_priv.h"
#include "../ocf_cache_priv.h"
#include "ocf_env.h"
#define OCF_ALLOCATOR_K_MAX (128 * KiB)
static int _ocf_realloc_with_cp(void **mem, size_t size, size_t count,
size_t *limit, bool cp)
{
size_t alloc_size = size * count;
ENV_BUG_ON(!mem);
ENV_BUG_ON(!limit);
if (size && count) {
/* Memory reallocation request */
if (alloc_size > *limit) {
/* The space is not enough, we need allocate new one */
void *new_mem;
if (alloc_size > OCF_ALLOCATOR_K_MAX)
new_mem = env_vzalloc(alloc_size);
else
new_mem = env_zalloc(alloc_size, ENV_MEM_NOIO);
if (!new_mem) {
/* Allocation error */
return -1;
}
/* Free previous memory */
if (*mem) {
if (cp) {
/* copy previous content into new allocated
* memory
*/
ENV_BUG_ON(env_memcpy(new_mem, alloc_size, *mem, *limit));
}
if (*limit > OCF_ALLOCATOR_K_MAX)
env_vfree(*mem);
else
env_free(*mem);
}
/* Update limit */
*limit = alloc_size;
/* Update memory pointer */
*mem = new_mem;
return 0;
}
/*
* The memory space is enough, no action required.
* Space after allocation set to '0'
*/
if (cp)
ENV_BUG_ON(env_memset(*mem + alloc_size, *limit - alloc_size, 0));
return 0;
}
if ((size == 0) && (count == 0)) {
if ((*mem) && (*limit)) {
/* Need to free memory */
if (*limit > OCF_ALLOCATOR_K_MAX)
env_vfree(*mem);
else
env_free(*mem);
/* Update limit */
*((size_t *)limit) = 0;
*mem = NULL;
return 0;
}
if ((!*mem) && (*limit == 0)) {
/* No allocation before do nothing */
return 0;
}
}
ENV_BUG();
return -1;
}
int ocf_realloc(void **mem, size_t size, size_t count, size_t *limit)
{
return _ocf_realloc_with_cp(mem, size, count, limit, false);
}
int ocf_realloc_cp(void **mem, size_t size, size_t count, size_t *limit)
{
return _ocf_realloc_with_cp(mem, size, count, limit, true);
}
void ocf_realloc_init(void **mem, size_t *limit)
{
ENV_BUG_ON(!mem);
ENV_BUG_ON(!limit);
*mem = NULL;
*((size_t *)limit) = 0;
}
enum {
ocf_mpool_1,
ocf_mpool_2,
ocf_mpool_4,
ocf_mpool_8,
ocf_mpool_16,
ocf_mpool_32,
ocf_mpool_64,
ocf_mpool_128,
ocf_mpool_max
};
struct ocf_mpool {
struct ocf_cache *cache;
/*!< Cache instance */
uint32_t item_size;
/*!< Size of specific item of memory pool */
uint32_t hdr_size;
/*!< Header size before items */
env_allocator *allocator[ocf_mpool_max];
/*!< OS handle to memory pool */
int flags;
/*!< Allocation flags */
};
#define ALLOCATOR_NAME_MAX 128
struct ocf_mpool *ocf_mpool_create(struct ocf_cache *cache,
uint32_t hdr_size, uint32_t size, int flags, int mpool_max,
const char *name_perfix)
{
uint32_t i;
char name[ALLOCATOR_NAME_MAX] = { '\0' };
int result;
struct ocf_mpool *mpool;
OCF_CHECK_NULL(name_perfix);
mpool = env_zalloc(sizeof(*mpool), ENV_MEM_NORMAL);
if (!mpool)
goto ocf_multi_allocator_create_ERROR;
mpool->item_size = size;
mpool->hdr_size = hdr_size;
mpool->cache = cache;
mpool->flags = flags;
for (i = 0; i < OCF_MIN(ocf_mpool_max, mpool_max + 1); i++) {
result = snprintf(name, sizeof(name), "%s_%u", name_perfix,
(1 << i));
if (result < 0 || result >= sizeof(name))
goto ocf_multi_allocator_create_ERROR;
mpool->allocator[i] = env_allocator_create(
hdr_size + (size * (1 << i)), name);
if (!mpool->allocator[i])
goto ocf_multi_allocator_create_ERROR;
}
return mpool;
ocf_multi_allocator_create_ERROR:
ocf_mpool_destroy(mpool);
return NULL;
}
void ocf_mpool_destroy(struct ocf_mpool *mallocator)
{
if (mallocator) {
uint32_t i;
for (i = 0; i < ocf_mpool_max; i++)
if (mallocator->allocator[i])
env_allocator_destroy(mallocator->allocator[i]);
env_free(mallocator);
}
}
static env_allocator *ocf_mpool_get_allocator(
struct ocf_mpool *mallocator, uint32_t count)
{
unsigned int idx;
if (unlikely(count == 0))
return ocf_mpool_1;
idx = 31 - __builtin_clz(count);
if (__builtin_ffs(count) <= idx)
idx++;
if (idx >= ocf_mpool_max)
return NULL;
return mallocator->allocator[idx];
}
void *ocf_mpool_new_f(struct ocf_mpool *mpool, uint32_t count, int flags)
{
void *items = NULL;
env_allocator *allocator;
OCF_CHECK_NULL(mpool);
allocator = ocf_mpool_get_allocator(mpool, count);
if (allocator)
items = env_allocator_new(allocator);
else
items = env_zalloc(mpool->hdr_size + (mpool->item_size * count), flags);
#ifdef ZERO_OR_NULL_PTR
if (ZERO_OR_NULL_PTR(items))
return NULL;
#endif
return items;
}
void *ocf_mpool_new(struct ocf_mpool *mpool, uint32_t count)
{
return ocf_mpool_new_f(mpool, count, mpool->flags);
}
void ocf_mpool_del(struct ocf_mpool *mpool,
void *items, uint32_t count)
{
env_allocator *allocator;
OCF_CHECK_NULL(mpool);
allocator = ocf_mpool_get_allocator(mpool, count);
if (allocator)
env_allocator_del(allocator, items);
else
env_free(items);
}

View File

@ -7,9 +7,9 @@
#include "../engine/cache_engine.h"
#include "../engine/engine_common.h"
#include "../concurrency/ocf_concurrency.h"
#include "../ocf_request.h"
#include "utils_cleaner.h"
#include "utils_part.h"
#include "utils_req.h"
#include "utils_io.h"
#include "utils_cache_line.h"

View File

@ -1,64 +0,0 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef UTILS_DEVICE_H_
#define UTILS_DEVICE_H_
static inline int _ocf_uuid_set(const struct ocf_volume_uuid *uuid,
struct ocf_metadata_uuid *muuid)
{
int result;
if (!uuid || !muuid) {
return -EINVAL;
}
if (!uuid->data || !muuid->data) {
return -EINVAL;
}
if (uuid->size > sizeof(muuid->data)) {
return -ENOBUFS;
}
result = env_memcpy(muuid->data, sizeof(muuid->data), uuid->data, uuid->size);
if (result)
return result;
result = env_memset(muuid->data + uuid->size,
sizeof(muuid->data) - uuid->size, 0);
if (result)
return result;
muuid->size = uuid->size;
return 0;
}
static inline int ocf_metadata_set_core_uuid(ocf_core_t core,
const struct ocf_volume_uuid *uuid,
struct ocf_volume_uuid *new_uuid)
{
ocf_cache_t cache = ocf_core_get_cache(core);
struct ocf_metadata_uuid *muuid = ocf_metadata_get_core_uuid(cache,
ocf_core_get_id(core));
if (_ocf_uuid_set(uuid, muuid))
return -ENOBUFS;
if (new_uuid) {
new_uuid->data = muuid->data;
new_uuid->size = muuid->size;
}
return 0;
}
static inline void ocf_metadata_clear_core_uuid(ocf_core_t core)
{
struct ocf_volume_uuid uuid = { .size = 0, };
ocf_metadata_set_core_uuid(core, &uuid, NULL);
}
#endif /* UTILS_MEM_H_ */

View File

@ -1,8 +1,8 @@
#include "ocf/ocf.h"
#include "../engine/cache_engine.h"
#include "../engine/engine_common.h"
#include "../ocf_request.h"
#include "utils_pipeline.h"
#include "utils_req.h"
struct ocf_pipeline {
struct ocf_pipeline_properties *properties;

117
src/utils/utils_realloc.c Normal file
View File

@ -0,0 +1,117 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#include "ocf/ocf.h"
#include "utils_realloc.h"
#include "ocf_env.h"
#define OCF_REALLOC_K_MAX (128 * KiB)
static int _ocf_realloc_with_cp(void **mem, size_t size, size_t count,
size_t *limit, bool cp)
{
size_t alloc_size = size * count;
ENV_BUG_ON(!mem);
ENV_BUG_ON(!limit);
if (size && count) {
/* Memory reallocation request */
if (alloc_size > *limit) {
/* The space is not enough, we need allocate new one */
void *new_mem;
if (alloc_size > OCF_REALLOC_K_MAX)
new_mem = env_vzalloc(alloc_size);
else
new_mem = env_zalloc(alloc_size, ENV_MEM_NOIO);
if (!new_mem) {
/* Allocation error */
return -1;
}
/* Free previous memory */
if (*mem) {
if (cp) {
/* copy previous content into new allocated
* memory
*/
ENV_BUG_ON(env_memcpy(new_mem, alloc_size, *mem, *limit));
}
if (*limit > OCF_REALLOC_K_MAX)
env_vfree(*mem);
else
env_free(*mem);
}
/* Update limit */
*limit = alloc_size;
/* Update memory pointer */
*mem = new_mem;
return 0;
}
/*
* The memory space is enough, no action required.
* Space after allocation set to '0'
*/
if (cp)
ENV_BUG_ON(env_memset(*mem + alloc_size, *limit - alloc_size, 0));
return 0;
}
if ((size == 0) && (count == 0)) {
if ((*mem) && (*limit)) {
/* Need to free memory */
if (*limit > OCF_REALLOC_K_MAX)
env_vfree(*mem);
else
env_free(*mem);
/* Update limit */
*((size_t *)limit) = 0;
*mem = NULL;
return 0;
}
if ((!*mem) && (*limit == 0)) {
/* No allocation before do nothing */
return 0;
}
}
ENV_BUG();
return -1;
}
int ocf_realloc(void **mem, size_t size, size_t count, size_t *limit)
{
return _ocf_realloc_with_cp(mem, size, count, limit, false);
}
int ocf_realloc_cp(void **mem, size_t size, size_t count, size_t *limit)
{
return _ocf_realloc_with_cp(mem, size, count, limit, true);
}
void ocf_realloc_init(void **mem, size_t *limit)
{
ENV_BUG_ON(!mem);
ENV_BUG_ON(!limit);
*mem = NULL;
*((size_t *)limit) = 0;
}

View File

@ -3,12 +3,12 @@
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef UTILS_ALLOCATOR_H_
#define UTILS_ALLOCATOR_H_
#ifndef UTILS_REALLOC_H_
#define UTILS_REALLOC_H_
/**
* @file utils_allocator.h
* @brief OCF memory reallocator
* @file utils_realloc.h
* @brief OCF realloc
*/
void ocf_realloc_init(void **mem, size_t *limit);
@ -66,4 +66,4 @@ int ocf_realloc_cp(void **mem, size_t size, size_t count, size_t *limit);
#define OCF_REALLOC_CP(mem, size, count, limit) \
ocf_realloc_cp((void **)mem, size, count, limit)
#endif /* UTILS_ALLOCATOR_H_ */
#endif /* UTILS_REALLOC_H_ */

View File

@ -1,145 +0,0 @@
/*
* Copyright(c) 2012-2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef UTILS_RQ_H_
#define UTILS_RQ_H_
#include "../ocf_request.h"
/**
* @file utils_req.h
* @brief OCF request allocation utilities
*/
struct ocf_req_allocator;
/**
* @brief Initialize OCF request allocation utility
*
* @param cache - OCF cache instance
* @return Operation status 0 - successful, non-zero failure
*/
int ocf_req_allocator_init(struct ocf_ctx *ocf_ctx);
/**
* @brief De-initialize OCF request allocation utility
*
* @param cache - OCF cache instance
*/
void ocf_req_allocator_deinit(struct ocf_ctx *ocf_ctx);
/**
* @brief Allocate new OCF request
*
* @param queue - I/O queue handle
* @param core - OCF core instance
* @param addr - LBA of request
* @param bytes - number of bytes of request
* @param rw - Read or Write
*
* @return new OCF request
*/
struct ocf_request *ocf_req_new(ocf_queue_t queue, ocf_core_t core,
uint64_t addr, uint32_t bytes, int rw);
/**
* @brief Allocate OCF request map
*
* @param req OCF request
*
* @retval 0 Allocation succeed
* @retval non-zero Allocation failed
*/
int ocf_req_alloc_map(struct ocf_request *req);
/**
* @brief Allocate new OCF request with NOIO map allocation for huge request
*
* @param queue - I/O queue handle
* @param core - OCF core instance
* @param addr - LBA of request
* @param bytes - number of bytes of request
* @param rw - Read or Write
*
* @return new OCF request
*/
struct ocf_request *ocf_req_new_extended(ocf_queue_t queue, ocf_core_t core,
uint64_t addr, uint32_t bytes, int rw);
/**
* @brief Allocate new OCF request for DISCARD operation
*
* @param queue - I/O queue handle
* @param core - OCF core instance
* @param addr - LBA of request
* @param bytes - number of bytes of request
* @param rw - Read or Write
*
* @return new OCF request
*/
struct ocf_request *ocf_req_new_discard(ocf_queue_t queue, ocf_core_t core,
uint64_t addr, uint32_t bytes, int rw);
/**
* @brief Increment OCF request reference count
*
* @param req - OCF request
*/
void ocf_req_get(struct ocf_request *req);
/**
* @brief Decrement OCF request reference. If reference is 0 then request will
* be deallocated
*
* @param req - OCF request
*/
void ocf_req_put(struct ocf_request *req);
/**
* @brief Clear OCF request info
*
* @param req - OCF request
*/
void ocf_req_clear_info(struct ocf_request *req);
/**
* @brief Clear OCF request map
*
* @param req - OCF request
*/
void ocf_req_clear_map(struct ocf_request *req);
/**
* @brief Clear OCF request
*
* @param req - OCF request
*/
static inline void ocf_req_clear(struct ocf_request *req)
{
ocf_req_clear_info(req);
ocf_req_clear_map(req);
env_atomic_set(&req->lock_remaining, 0);
env_atomic_set(&req->req_remaining, 0);
}
/**
* @brief Return OCF request reference count
*
* @param req - OCF request
* @return OCF request reference count
*/
static inline int ocf_req_ref_count(struct ocf_request *req)
{
return env_atomic_read(&req->ref_count);
}
static inline bool ocf_req_is_4k(uint64_t addr, uint32_t bytes)
{
return !((addr % PAGE_SIZE) || (bytes % PAGE_SIZE));
}
#endif /* UTILS_RQ_H_ */

View File

@ -31,7 +31,7 @@
#include "../metadata/metadata.h"
#include "../utils/utils_cleaner.h"
#include "../utils/utils_part.h"
#include "../utils/utils_allocator.h"
#include "../utils/utils_realloc.h"
#include "../concurrency/ocf_cache_concurrency.h"
#include "../ocf_def_priv.h"

View File

@ -31,11 +31,9 @@
#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 "../utils/utils_pipeline.h"
#include "../ocf_utils.h"
#include "../concurrency/ocf_concurrency.h"
#include "../eviction/ops.h"
#include "../ocf_ctx_priv.h"

View File

@ -26,11 +26,9 @@
#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 "../utils/utils_pipeline.h"
#include "../ocf_utils.h"
#include "../concurrency/ocf_concurrency.h"
#include "../eviction/ops.h"
#include "../ocf_ctx_priv.h"