example: Introduce error handling

Signed-off-by: Michal Mielewczyk <michal.mielewczyk@huawei.com>
This commit is contained in:
Michal Mielewczyk 2024-10-07 12:41:45 +02:00
parent e8e7a1600c
commit d22885ef7d

View File

@ -22,15 +22,6 @@ struct cache_priv {
ocf_queue_t io_queue; 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 * Queue ops providing interface for running queue thread in asynchronous
* way. Optional synchronous kick callback is not provided. The stop() * way. Optional synchronous kick callback is not provided. The stop()
@ -318,8 +309,10 @@ void perform_workload(ocf_core_t core)
/* Allocate data buffer and fill it with example data */ /* Allocate data buffer and fill it with example data */
data1 = ctx_data_alloc(1); data1 = ctx_data_alloc(1);
if (!data1) if (!data1) {
error("Unable to allocate data1\n"); printf("Error: Unable to allocate data1\n");
return;
}
strcpy(data1->ptr, "This is some test data"); strcpy(data1->ptr, "This is some test data");
/* Prepare and submit write IO to the core */ /* Prepare and submit write IO to the core */
submit_io(core, data1, 0, 512, OCF_WRITE, complete_write); submit_io(core, data1, 0, 512, OCF_WRITE, complete_write);
@ -332,8 +325,10 @@ void perform_workload(ocf_core_t core)
/* Allocate data buffer for read */ /* Allocate data buffer for read */
data2 = ctx_data_alloc(1); data2 = ctx_data_alloc(1);
if (!data2) if (!data2) {
error("Unable to allocate data2\n"); printf("Error: Unable to allocate data2\n");
return;
}
/* Prepare and submit read IO to the core */ /* Prepare and submit read IO to the core */
submit_io(core, data2, 0, 512, OCF_READ, complete_read); submit_io(core, data2, 0, 512, OCF_READ, complete_read);
/* After read completes, complete_read() callback will be called, /* After read completes, complete_read() callback will be called,
@ -360,21 +355,29 @@ int main(int argc, char *argv[])
/* Initialize completion semaphore */ /* Initialize completion semaphore */
ret = sem_init(&context.sem, 0, 0); ret = sem_init(&context.sem, 0, 0);
if (ret) if (ret) {
error("Unable to initialize completion semaphore\n"); printf("Error: Unable to initialize completion semaphore\n");
goto sem_err;
}
context.error = &ret; context.error = &ret;
/* Initialize OCF context */ /* Initialize OCF context */
if (ctx_init(&ctx)) if (ctx_init(&ctx)) {
error("Unable to initialize context\n"); printf("Error: Unable to initialize context\n");
goto ctx_err;
}
/* Start cache */ /* Start cache */
if (initialize_cache(ctx, &cache1)) if (initialize_cache(ctx, &cache1)) {
error("Unable to start cache\n"); printf("Error: Unable to start cache\n");
goto cache_err;
}
/* Add core */ /* Add core */
if (initialize_core(cache1, &core1)) if (initialize_core(cache1, &core1)) {
error("Unable to add core\n"); printf("Error: Unable to add core\n");
goto core_err;
}
/* Do some actual io operations */ /* Do some actual io operations */
perform_workload(core1); perform_workload(core1);
@ -382,27 +385,31 @@ int main(int argc, char *argv[])
/* Remove core from cache */ /* Remove core from cache */
ocf_mngt_cache_remove_core(core1, remove_core_complete, &context); ocf_mngt_cache_remove_core(core1, remove_core_complete, &context);
sem_wait(&context.sem); sem_wait(&context.sem);
if (ret) if (ret) {
error("Unable to remove core\n"); printf("Error: Unable to remove core\n");
goto core_err;
}
/* Stop cache */ /* Stop cache */
ocf_mngt_cache_stop(cache1, simple_complete, &context); ocf_mngt_cache_stop(cache1, simple_complete, &context);
sem_wait(&context.sem); sem_wait(&context.sem);
if (ret) if (ret) {
error("Unable to stop cache\n"); printf("Error: Unable to stop cache\n");
}
core_err:
cache_priv = ocf_cache_get_priv(cache1); cache_priv = ocf_cache_get_priv(cache1);
/* Put the management queue */ /* Put the management queue */
ocf_queue_put(cache_priv->mngt_queue); ocf_queue_put(cache_priv->mngt_queue);
free(cache_priv); free(cache_priv);
cache_err:
/* Deinitialize context */ /* Deinitialize context */
ctx_cleanup(ctx); ctx_cleanup(ctx);
ctx_err:
/* Destroy completion semaphore */ /* Destroy completion semaphore */
sem_destroy(&context.sem); sem_destroy(&context.sem);
sem_err:
return 0; return ret;
} }