Merge pull request #51 from robertbaldyga/add-logger-priv

Enable passing priv to logger during context initialization
This commit is contained in:
Michał Mielewczyk 2019-01-24 11:13:39 +01:00 committed by GitHub
commit ec37b2ea9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 100 additions and 69 deletions

View File

@ -254,17 +254,19 @@ static int ctx_logger_dump_stack(ocf_logger_t logger)
} }
/* /*
* This structure describes context ops. They are splitted into few categories: * This structure describes context config, containing simple context info
* and pointers to ops callbacks. Ops are splitted into few categories:
* - data ops, providing context specific data handing interface, * - data ops, providing context specific data handing interface,
* - queue ops, providing interface for starting, stoping and kicking * - queue ops, providing interface for starting, stoping and kicking
* queue thread in both synchronous and asynchronous way, * queue thread in both synchronous and asynchronous way,
* - cleaner ops, providing interface to start and stop clener thread, * - cleaner ops, providing interface to start and stop clener thread,
* - metadata updater ops, providing interface for starting, stoping * - metadata updater ops, providing interface for starting, stoping
* and kicking metadata updater thread. * and kicking metadata updater thread.
* - logger ops, providing interface for text message logging
*/ */
static const struct ocf_ctx_ops ctx_ops = { static const struct ocf_ctx_config ctx_cfg = {
.name = "OCF Example", .name = "OCF Example",
.ops = {
.data = { .data = {
.alloc = ctx_data_alloc, .alloc = ctx_data_alloc,
.free = ctx_data_free, .free = ctx_data_free,
@ -300,8 +302,10 @@ static const struct ocf_ctx_ops ctx_ops = {
.printf = ctx_logger_printf, .printf = ctx_logger_printf,
.dump_stack = ctx_logger_dump_stack, .dump_stack = ctx_logger_dump_stack,
}, },
},
}; };
/* /*
* Function initializing context. Prepares context, sets logger and * Function initializing context. Prepares context, sets logger and
* registers data object type. * registers data object type.
@ -310,7 +314,7 @@ int ctx_init(ocf_ctx_t *ctx)
{ {
int ret; int ret;
ret = ocf_ctx_init(ctx, &ctx_ops); ret = ocf_ctx_init(ctx, &ctx_cfg);
if (ret) if (ret)
return ret; return ret;

View File

@ -240,17 +240,31 @@ struct ocf_metadata_updater_ops {
* @brief OCF context specific operation * @brief OCF context specific operation
*/ */
struct ocf_ctx_ops { struct ocf_ctx_ops {
/** /* Context data operations */
* @brief The name of the environment which provides platform struct ocf_data_ops data;
* interface for cache engine
*/ /* Queue operations */
struct ocf_queue_ops queue;
/* Cleaner operations */
struct ocf_cleaner_ops cleaner;
/* Metadata updater operations */
struct ocf_metadata_updater_ops metadata_updater;
/* Logger operations */
struct ocf_logger_ops logger;
};
struct ocf_ctx_config {
/* Context name */
const char *name; const char *name;
struct ocf_data_ops data; /* Context operations */
struct ocf_queue_ops queue; const struct ocf_ctx_ops ops;
struct ocf_cleaner_ops cleaner;
struct ocf_metadata_updater_ops metadata_updater; /* Context logger priv */
struct ocf_logger_ops logger; void *logger_priv;
}; };
/** /**
@ -322,7 +336,7 @@ int ocf_ctx_data_obj_create(ocf_ctx_t ctx, ocf_data_obj_t *obj,
* *
* @return Zero when success, otherwise an error * @return Zero when success, otherwise an error
*/ */
int ocf_ctx_init(ocf_ctx_t *ctx, const struct ocf_ctx_ops *ops); int ocf_ctx_init(ocf_ctx_t *ctx, const struct ocf_ctx_config *cfg);
/** /**
* @brief De-Initialize OCF context * @brief De-Initialize OCF context

View File

@ -113,13 +113,13 @@ int ocf_ctx_data_obj_create(ocf_ctx_t ctx, ocf_data_obj_t *obj,
/* /*
* *
*/ */
int ocf_ctx_init(ocf_ctx_t *ctx, const struct ocf_ctx_ops *ops) int ocf_ctx_init(ocf_ctx_t *ctx, const struct ocf_ctx_config *cfg)
{ {
ocf_ctx_t ocf_ctx; ocf_ctx_t ocf_ctx;
int ret; int ret;
OCF_CHECK_NULL(ctx); OCF_CHECK_NULL(ctx);
OCF_CHECK_NULL(ops); OCF_CHECK_NULL(cfg);
ocf_ctx = env_zalloc(sizeof(*ocf_ctx), ENV_MEM_NORMAL); ocf_ctx = env_zalloc(sizeof(*ocf_ctx), ENV_MEM_NORMAL);
if (!ocf_ctx) if (!ocf_ctx)
@ -130,8 +130,10 @@ int ocf_ctx_init(ocf_ctx_t *ctx, const struct ocf_ctx_ops *ops)
if (ret) if (ret)
goto err_ctx; goto err_ctx;
ocf_ctx->ctx_ops = ops; ocf_ctx->ops = &cfg->ops;
ocf_ctx->logger.ops = &ops->logger; ocf_ctx->cfg = cfg;
ocf_logger_init(&ocf_ctx->logger, &cfg->ops.logger, cfg->logger_priv);
ret = ocf_logger_open(&ocf_ctx->logger); ret = ocf_logger_open(&ocf_ctx->logger);
if (ret) if (ret)

View File

@ -16,7 +16,8 @@
* @brief OCF main control structure * @brief OCF main control structure
*/ */
struct ocf_ctx { struct ocf_ctx {
const struct ocf_ctx_ops *ctx_ops; const struct ocf_ctx_ops *ops;
const struct ocf_ctx_config *cfg;
struct ocf_logger logger; struct ocf_logger logger;
struct ocf_data_obj_type *data_obj_type[OCF_DATA_OBJ_TYPE_MAX]; struct ocf_data_obj_type *data_obj_type[OCF_DATA_OBJ_TYPE_MAX];
env_mutex lock; env_mutex lock;
@ -50,34 +51,34 @@ struct ocf_ctx {
*/ */
static inline void *ctx_data_alloc(ocf_ctx_t ctx, uint32_t pages) static inline void *ctx_data_alloc(ocf_ctx_t ctx, uint32_t pages)
{ {
return ctx->ctx_ops->data.alloc(pages); return ctx->ops->data.alloc(pages);
} }
static inline void ctx_data_free(ocf_ctx_t ctx, ctx_data_t *data) static inline void ctx_data_free(ocf_ctx_t ctx, ctx_data_t *data)
{ {
ctx->ctx_ops->data.free(data); ctx->ops->data.free(data);
} }
static inline int ctx_data_mlock(ocf_ctx_t ctx, ctx_data_t *data) static inline int ctx_data_mlock(ocf_ctx_t ctx, ctx_data_t *data)
{ {
return ctx->ctx_ops->data.mlock(data); return ctx->ops->data.mlock(data);
} }
static inline void ctx_data_munlock(ocf_ctx_t ctx, ctx_data_t *data) static inline void ctx_data_munlock(ocf_ctx_t ctx, ctx_data_t *data)
{ {
ctx->ctx_ops->data.munlock(data); ctx->ops->data.munlock(data);
} }
static inline uint32_t ctx_data_rd(ocf_ctx_t ctx, void *dst, static inline uint32_t ctx_data_rd(ocf_ctx_t ctx, void *dst,
ctx_data_t *src, uint32_t size) ctx_data_t *src, uint32_t size)
{ {
return ctx->ctx_ops->data.read(dst, src, size); return ctx->ops->data.read(dst, src, size);
} }
static inline uint32_t ctx_data_wr(ocf_ctx_t ctx, ctx_data_t *dst, static inline uint32_t ctx_data_wr(ocf_ctx_t ctx, ctx_data_t *dst,
const void *src, uint32_t size) const void *src, uint32_t size)
{ {
return ctx->ctx_ops->data.write(dst, src, size); return ctx->ops->data.write(dst, src, size);
} }
static inline void ctx_data_rd_check(ocf_ctx_t ctx, void *dst, static inline void ctx_data_rd_check(ocf_ctx_t ctx, void *dst,
@ -99,7 +100,7 @@ static inline void ctx_data_wr_check(ocf_ctx_t ctx, ctx_data_t *dst,
static inline uint32_t ctx_data_zero(ocf_ctx_t ctx, ctx_data_t *dst, static inline uint32_t ctx_data_zero(ocf_ctx_t ctx, ctx_data_t *dst,
uint32_t size) uint32_t size)
{ {
return ctx->ctx_ops->data.zero(dst, size); return ctx->ops->data.zero(dst, size);
} }
static inline void ctx_data_zero_check(ocf_ctx_t ctx, ctx_data_t *dst, static inline void ctx_data_zero_check(ocf_ctx_t ctx, ctx_data_t *dst,
@ -113,7 +114,7 @@ static inline void ctx_data_zero_check(ocf_ctx_t ctx, ctx_data_t *dst,
static inline uint32_t ctx_data_seek(ocf_ctx_t ctx, ctx_data_t *dst, static inline uint32_t ctx_data_seek(ocf_ctx_t ctx, ctx_data_t *dst,
ctx_data_seek_t seek, uint32_t size) ctx_data_seek_t seek, uint32_t size)
{ {
return ctx->ctx_ops->data.seek(dst, seek, size); return ctx->ops->data.seek(dst, seek, size);
} }
static inline void ctx_data_seek_check(ocf_ctx_t ctx, ctx_data_t *dst, static inline void ctx_data_seek_check(ocf_ctx_t ctx, ctx_data_t *dst,
@ -127,59 +128,59 @@ static inline void ctx_data_seek_check(ocf_ctx_t ctx, ctx_data_t *dst,
static inline uint64_t ctx_data_cpy(ocf_ctx_t ctx, ctx_data_t *dst, ctx_data_t *src, static inline uint64_t ctx_data_cpy(ocf_ctx_t ctx, ctx_data_t *dst, ctx_data_t *src,
uint64_t to, uint64_t from, uint64_t bytes) uint64_t to, uint64_t from, uint64_t bytes)
{ {
return ctx->ctx_ops->data.copy(dst, src, to, from, bytes); return ctx->ops->data.copy(dst, src, to, from, bytes);
} }
static inline void ctx_data_secure_erase(ocf_ctx_t ctx, ctx_data_t *dst) static inline void ctx_data_secure_erase(ocf_ctx_t ctx, ctx_data_t *dst)
{ {
return ctx->ctx_ops->data.secure_erase(dst); return ctx->ops->data.secure_erase(dst);
} }
static inline int ctx_queue_init(ocf_ctx_t ctx, ocf_queue_t queue) static inline int ctx_queue_init(ocf_ctx_t ctx, ocf_queue_t queue)
{ {
return ctx->ctx_ops->queue.init(queue); return ctx->ops->queue.init(queue);
} }
static inline void ctx_queue_kick(ocf_ctx_t ctx, ocf_queue_t queue, static inline void ctx_queue_kick(ocf_ctx_t ctx, ocf_queue_t queue,
bool allow_sync) bool allow_sync)
{ {
if (allow_sync && ctx->ctx_ops->queue.kick_sync) if (allow_sync && ctx->ops->queue.kick_sync)
ctx->ctx_ops->queue.kick_sync(queue); ctx->ops->queue.kick_sync(queue);
else else
ctx->ctx_ops->queue.kick(queue); ctx->ops->queue.kick(queue);
} }
static inline void ctx_queue_stop(ocf_ctx_t ctx, ocf_queue_t queue) static inline void ctx_queue_stop(ocf_ctx_t ctx, ocf_queue_t queue)
{ {
ctx->ctx_ops->queue.stop(queue); ctx->ops->queue.stop(queue);
} }
static inline int ctx_cleaner_init(ocf_ctx_t ctx, ocf_cleaner_t cleaner) static inline int ctx_cleaner_init(ocf_ctx_t ctx, ocf_cleaner_t cleaner)
{ {
return ctx->ctx_ops->cleaner.init(cleaner); return ctx->ops->cleaner.init(cleaner);
} }
static inline void ctx_cleaner_stop(ocf_ctx_t ctx, ocf_cleaner_t cleaner) static inline void ctx_cleaner_stop(ocf_ctx_t ctx, ocf_cleaner_t cleaner)
{ {
ctx->ctx_ops->cleaner.stop(cleaner); ctx->ops->cleaner.stop(cleaner);
} }
static inline int ctx_metadata_updater_init(ocf_ctx_t ctx, static inline int ctx_metadata_updater_init(ocf_ctx_t ctx,
ocf_metadata_updater_t mu) ocf_metadata_updater_t mu)
{ {
return ctx->ctx_ops->metadata_updater.init(mu); return ctx->ops->metadata_updater.init(mu);
} }
static inline void ctx_metadata_updater_kick(ocf_ctx_t ctx, static inline void ctx_metadata_updater_kick(ocf_ctx_t ctx,
ocf_metadata_updater_t mu) ocf_metadata_updater_t mu)
{ {
ctx->ctx_ops->metadata_updater.kick(mu); ctx->ops->metadata_updater.kick(mu);
} }
static inline void ctx_metadata_updater_stop(ocf_ctx_t ctx, static inline void ctx_metadata_updater_stop(ocf_ctx_t ctx,
ocf_metadata_updater_t mu) ocf_metadata_updater_t mu)
{ {
ctx->ctx_ops->metadata_updater.stop(mu); ctx->ops->metadata_updater.stop(mu);
} }
/** /**

View File

@ -47,6 +47,13 @@ int ocf_log_stack_trace_raw(ocf_logger_t logger)
return logger->ops->dump_stack(logger); return logger->ops->dump_stack(logger);
} }
void ocf_logger_init(ocf_logger_t logger,
const struct ocf_logger_ops *ops, void *priv)
{
logger->ops = ops;
logger->priv = priv;
}
int ocf_logger_open(ocf_logger_t logger) int ocf_logger_open(ocf_logger_t logger)
{ {
if (!logger->ops->open) if (!logger->ops->open)

View File

@ -21,6 +21,9 @@ int ocf_log_raw_rl(ocf_logger_t logger, const char *func_name);
int ocf_log_stack_trace_raw(ocf_logger_t logger); int ocf_log_stack_trace_raw(ocf_logger_t logger);
void ocf_logger_init(ocf_logger_t logger,
const struct ocf_logger_ops *ops, void *priv);
int ocf_logger_open(ocf_logger_t logger); int ocf_logger_open(ocf_logger_t logger);
void ocf_logger_close(ocf_logger_t logger); void ocf_logger_close(ocf_logger_t logger);