Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors&fixes #847

Merged
merged 10 commits into from
Oct 14, 2024
65 changes: 36 additions & 29 deletions example/simple/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ struct cache_priv {
ocf_queue_t io_queue;
};

/*
* Helper function for error handling.
*/
void error(char *msg)
{
printf("ERROR: %s", msg);
exit(1);
}

/*
* Queue ops providing interface for running queue thread in asynchronous
* way. Optional synchronous kick callback is not provided. The stop()
Expand Down Expand Up @@ -318,8 +309,10 @@ void perform_workload(ocf_core_t core)

/* Allocate data buffer and fill it with example data */
data1 = ctx_data_alloc(1);
if (!data1)
error("Unable to allocate data1\n");
if (!data1) {
printf("Error: Unable to allocate data1\n");
return;
}
strcpy(data1->ptr, "This is some test data");
/* Prepare and submit write IO to the core */
submit_io(core, data1, 0, 512, OCF_WRITE, complete_write);
Expand All @@ -332,8 +325,10 @@ void perform_workload(ocf_core_t core)

/* Allocate data buffer for read */
data2 = ctx_data_alloc(1);
if (!data2)
error("Unable to allocate data2\n");
if (!data2) {
printf("Error: Unable to allocate data2\n");
return;
}
/* Prepare and submit read IO to the core */
submit_io(core, data2, 0, 512, OCF_READ, complete_read);
/* After read completes, complete_read() callback will be called,
Expand All @@ -360,49 +355,61 @@ int main(int argc, char *argv[])

/* Initialize completion semaphore */
ret = sem_init(&context.sem, 0, 0);
if (ret)
error("Unable to initialize completion semaphore\n");
if (ret) {
printf("Error: Unable to initialize completion semaphore\n");
goto sem_err;
}
context.error = &ret;

/* Initialize OCF context */
if (ctx_init(&ctx))
error("Unable to initialize context\n");
if (ctx_init(&ctx)) {
printf("Error: Unable to initialize context\n");
goto ctx_err;
}

/* Start cache */
if (initialize_cache(ctx, &cache1))
error("Unable to start cache\n");
if (initialize_cache(ctx, &cache1)) {
printf("Error: Unable to start cache\n");
goto cache_err;
}

/* Add core */
if (initialize_core(cache1, &core1))
error("Unable to add core\n");
if (initialize_core(cache1, &core1)) {
printf("Error: Unable to add core\n");
goto core_err;
}

/* Do some actual io operations */
perform_workload(core1);

/* Remove core from cache */
ocf_mngt_cache_remove_core(core1, remove_core_complete, &context);
sem_wait(&context.sem);
if (ret)
error("Unable to remove core\n");
if (ret) {
printf("Error: Unable to remove core\n");
goto core_err;
}

/* Stop cache */
ocf_mngt_cache_stop(cache1, simple_complete, &context);
sem_wait(&context.sem);
if (ret)
error("Unable to stop cache\n");
if (ret) {
mmichal10 marked this conversation as resolved.
Show resolved Hide resolved
printf("Error: Unable to stop cache\n");
}

core_err:
cache_priv = ocf_cache_get_priv(cache1);

/* Put the management queue */
ocf_queue_put(cache_priv->mngt_queue);

free(cache_priv);

cache_err:
/* Deinitialize context */
ctx_cleanup(ctx);

ctx_err:
/* Destroy completion semaphore */
sem_destroy(&context.sem);

return 0;
sem_err:
return ret;
}
15 changes: 10 additions & 5 deletions src/engine/cache_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,17 @@ static ocf_req_cb ocf_cache_mode_to_engine_cb(

bool ocf_fallback_pt_is_on(ocf_cache_t cache)
{
ENV_BUG_ON(env_atomic_read(&cache->fallback_pt_error_counter) < 0);
int threshold = cache->fallback_pt_error_threshold;
int counter;

return (cache->fallback_pt_error_threshold !=
OCF_CACHE_FALLBACK_PT_INACTIVE &&
env_atomic_read(&cache->fallback_pt_error_counter) >=
cache->fallback_pt_error_threshold);
if (threshold == OCF_CACHE_FALLBACK_PT_INACTIVE)
return false;

counter = env_atomic_read(&cache->fallback_pt_error_counter);

ENV_BUG_ON(counter < 0);

return counter >= threshold;
}

void ocf_resolve_effective_cache_mode(ocf_cache_t cache,
Expand Down
4 changes: 0 additions & 4 deletions src/metadata/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,6 @@ static inline void ocf_metadata_config_init(ocf_cache_t cache, size_t size)

static void ocf_metadata_deinit_fixed_size(struct ocf_cache *cache)
{
int result = 0;
uint32_t i;

struct ocf_metadata_ctrl *ctrl = (struct ocf_metadata_ctrl *)
Expand All @@ -481,9 +480,6 @@ static void ocf_metadata_deinit_fixed_size(struct ocf_cache *cache)

env_vfree(ctrl);
cache->metadata.priv = NULL;

if (result)
ENV_BUG();
}

static struct ocf_metadata_ctrl *ocf_metadata_ctrl_init(
Expand Down
67 changes: 47 additions & 20 deletions src/mngt/ocf_mngt_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ struct ocf_cache_mngt_init_params {
bool metadata_inited : 1;
/*!< Metadata is inited to valid state */

bool added_to_list : 1;
/*!< Cache is added to context list */

bool cache_locked : 1;
/*!< Cache has been locked */
} flags;
Expand Down Expand Up @@ -826,33 +823,59 @@ static void _ocf_mngt_load_metadata(ocf_pipeline_t pipeline,
* @brief allocate memory for new cache, add it to cache queue, set initial
* values and running state
*/
static int _ocf_mngt_init_new_cache(struct ocf_cache_mngt_init_params *params)
static int _ocf_mngt_init_new_cache(struct ocf_cache_mngt_init_params *params,
char *new_cache_name)
{
ocf_cache_t cache = env_vzalloc(sizeof(*cache));
int result;

if (!cache)
if (!cache) {
ocf_log(params->ctx, log_err, "Failed to allocate cache %s\n",
new_cache_name);
return -OCF_ERR_NO_MEM;
}

if (ocf_mngt_cache_lock_init(cache)) {
ocf_log(params->ctx, log_err,
"Failed to allocate cache %s lock\n",
new_cache_name);
result = -OCF_ERR_NO_MEM;
goto alloc_err;
}

/* Lock cache during setup - this trylock should always succeed */
ENV_BUG_ON(ocf_mngt_cache_trylock(cache));
result = ocf_mngt_cache_trylock(cache);
if (result) {
ocf_log(params->ctx, log_crit,
"Failed to lock the newly created cache %s\n",
new_cache_name);
goto lock_init_err;
}

if (env_mutex_init(&cache->flush_mutex)) {
ocf_log(params->ctx, log_err,
"Failed to allocate cache %s flush lock\n",
new_cache_name);
result = -OCF_ERR_NO_MEM;
goto lock_err;
}

INIT_LIST_HEAD(&cache->io_queues);
result = env_spinlock_init(&cache->io_queues_lock);
if (result)
if (result) {
ocf_log(params->ctx, log_err,
"Failed to allocate cache %s queue lock\n",
new_cache_name);
goto mutex_err;
}

ENV_BUG_ON(!ocf_refcnt_inc(&cache->refcnt.cache));
result = !ocf_refcnt_inc(&cache->refcnt.cache);
if (result) {
ocf_log(params->ctx, log_crit,
"Failed to increment %s refcnt\n",
new_cache_name);
goto cache_refcnt_inc_err;
}

/* start with freezed metadata ref counter to indicate detached device*/
ocf_refcnt_freeze(&cache->refcnt.metadata);
Expand All @@ -869,10 +892,13 @@ static int _ocf_mngt_init_new_cache(struct ocf_cache_mngt_init_params *params)

return 0;

cache_refcnt_inc_err:
env_spinlock_destroy(&cache->io_queues_lock);
mutex_err:
env_mutex_destroy(&cache->flush_mutex);
lock_err:
ocf_mngt_cache_unlock(cache);
lock_init_err:
ocf_mngt_cache_lock_deinit(cache);
alloc_err:
env_vfree(cache);
Expand Down Expand Up @@ -950,7 +976,7 @@ static int _ocf_mngt_init_prepare_cache(struct ocf_cache_mngt_init_params *param

ocf_log(param->ctx, log_info, "Inserting cache %s\n", cfg->name);

ret = _ocf_mngt_init_new_cache(param);
ret = _ocf_mngt_init_new_cache(param, cfg->name);
if (ret)
goto out;

Expand Down Expand Up @@ -1442,20 +1468,15 @@ static void _ocf_mngt_init_handle_error(ocf_ctx_t ctx,

env_mutex_destroy(&cache->flush_mutex);

if (params->flags.cache_locked)
ocf_mngt_cache_unlock(cache);

ocf_mngt_cache_lock_deinit(cache);

if (params->flags.metadata_inited)
ocf_metadata_deinit(cache);

if (!params->flags.added_to_list)
return;

env_rmutex_lock(&ctx->lock);

list_del(&cache->list);
env_vfree(cache);

env_rmutex_unlock(&ctx->lock);
}

static void _ocf_mngt_cache_init(ocf_cache_t cache,
Expand Down Expand Up @@ -1493,6 +1514,8 @@ static int _ocf_mngt_cache_start(ocf_ctx_t ctx, ocf_cache_t *cache,
params.metadata.promotion_policy = cfg->promotion_policy;
params.locked = cfg->locked;

ocf_ctx_get(ctx);

result = env_rmutex_lock_interruptible(&ctx->lock);
if (result)
goto _cache_mngt_init_instance_ERROR;
Expand All @@ -1501,6 +1524,8 @@ static int _ocf_mngt_cache_start(ocf_ctx_t ctx, ocf_cache_t *cache,
result = _ocf_mngt_init_prepare_cache(&params, cfg);
if (result) {
env_rmutex_unlock(&ctx->lock);
ocf_log(ctx, log_err, "Failed to prepare cache %s\n",
cfg->name);
goto _cache_mngt_init_instance_ERROR;
}

Expand All @@ -1514,27 +1539,28 @@ static int _ocf_mngt_cache_start(ocf_ctx_t ctx, ocf_cache_t *cache,
result = ocf_metadata_init(tmp_cache, params.metadata.line_size);
if (result) {
env_rmutex_unlock(&ctx->lock);
ocf_log(ctx, log_err, "Failed to initialize cache %s "
"metadata\n", cfg->name);
result = -OCF_ERR_NO_MEM;
goto _cache_mngt_init_instance_ERROR;
}
params.flags.metadata_inited = true;

result = ocf_cache_set_name(tmp_cache, cfg->name, OCF_CACHE_NAME_SIZE);
if (result) {
ocf_log(ctx, log_err, "Failed to set cache %s name\n",
cfg->name);
env_rmutex_unlock(&ctx->lock);
goto _cache_mngt_init_instance_ERROR;
}

list_add_tail(&tmp_cache->list, &ctx->caches);
params.flags.added_to_list = true;
env_rmutex_unlock(&ctx->lock);

ocf_cache_log(tmp_cache, log_debug, "Metadata initialized\n");

_ocf_mngt_cache_init(tmp_cache, &params);

ocf_ctx_get(ctx);

if (!params.locked) {
/* User did not request to lock cache instance after creation -
unlock it here since we have acquired the lock to
Expand All @@ -1550,6 +1576,7 @@ static int _ocf_mngt_cache_start(ocf_ctx_t ctx, ocf_cache_t *cache,
_cache_mngt_init_instance_ERROR:
_ocf_mngt_init_handle_error(ctx, &params);
*cache = NULL;
ocf_ctx_put(ctx);
return result;
}

Expand Down
4 changes: 3 additions & 1 deletion src/mngt/ocf_mngt_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,10 @@ static int _ocf_mngt_cache_trylock(ocf_cache_t cache,
return -OCF_ERR_CACHE_NOT_EXIST;

result = trylock_fn(&cache->lock);
if (result)
if (result) {
ocf_mngt_cache_put(cache);
return result;
}

if (env_bit_test(ocf_cache_state_stopping, &cache->cache_state)) {
/* Cache already stopping, do not allow any operation */
Expand Down
1 change: 1 addition & 0 deletions src/utils/utils_pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static int _ocf_pipeline_run_step(struct ocf_request *req)

while (true) {
step = &pipeline->properties->steps[pipeline->next_step];
ocf_cache_log(req->cache, log_debug, "PL STEP: %s\n", step->name);
switch (step->type) {
case ocf_pipeline_step_single:
pipeline->next_step++;
Expand Down
Loading