cleaner: Rework request allocation

Signed-off-by: Robert Baldyga <robert.baldyga@huawei.com>
This commit is contained in:
Robert Baldyga 2024-06-17 21:51:21 +02:00
parent 3248c85828
commit 3300dbd4e7

View File

@ -73,13 +73,31 @@ enum {
ocf_cleaner_req_type_slave = 2 ocf_cleaner_req_type_slave = 2
}; };
static inline uint32_t _ocf_cleaner_get_req_max_count(uint32_t count,
bool low_mem)
{
if (low_mem || count <= 4096)
return count < 128 ? count : 128;
return 1024;
}
static struct ocf_request *_ocf_cleaner_alloc_master_req( static struct ocf_request *_ocf_cleaner_alloc_master_req(
struct ocf_cache *cache, uint32_t count, struct ocf_cache *cache, uint32_t count,
const struct ocf_cleaner_attribs *attribs) const struct ocf_cleaner_attribs *attribs)
{ {
struct ocf_request *req = _ocf_cleaner_alloc_req(cache, count, attribs); struct ocf_request *req;
req =_ocf_cleaner_alloc_req(cache, count, attribs);
if (unlikely(!req)) {
/* Some memory allocation error, try re-allocate request */
count = _ocf_cleaner_get_req_max_count(count, true);
req = _ocf_cleaner_alloc_req(cache, count, attribs);
}
if (unlikely(!req))
return NULL;
if (req) {
/* Set type of cleaning request */ /* Set type of cleaning request */
req->master_io_req_type = ocf_cleaner_req_type_master; req->master_io_req_type = ocf_cleaner_req_type_master;
@ -91,20 +109,27 @@ static struct ocf_request *_ocf_cleaner_alloc_master_req(
/* The count of all requests */ /* The count of all requests */
env_atomic_set(&req->master_remaining, 1); env_atomic_set(&req->master_remaining, 1);
OCF_DEBUG_PARAM(cache, "New master request, count = %u", OCF_DEBUG_PARAM(cache, "New master request, count = %u", count);
count);
}
return req; return req;
} }
static struct ocf_request *_ocf_cleaner_alloc_slave_req( static struct ocf_request *_ocf_cleaner_alloc_slave_req(
struct ocf_request *master, struct ocf_request *master, uint32_t count,
uint32_t count, const struct ocf_cleaner_attribs *attribs) const struct ocf_cleaner_attribs *attribs)
{ {
struct ocf_request *req = _ocf_cleaner_alloc_req( struct ocf_request *req;
master->cache, count, attribs);
req = _ocf_cleaner_alloc_req(master->cache, count, attribs);
if (unlikely(!req)) {
/* Some memory allocation error, try re-allocate request */
count = _ocf_cleaner_get_req_max_count(count, true);
req = _ocf_cleaner_alloc_req(master->cache, count, attribs);
}
if (unlikely(!req))
return NULL;
if (req) {
/* Set type of cleaning request */ /* Set type of cleaning request */
req->master_io_req_type = ocf_cleaner_req_type_slave; req->master_io_req_type = ocf_cleaner_req_type_slave;
@ -122,7 +147,7 @@ static struct ocf_request *_ocf_cleaner_alloc_slave_req(
OCF_DEBUG_PARAM(req->cache, OCF_DEBUG_PARAM(req->cache,
"New slave request, count = %u,all requests count = %d", "New slave request, count = %u,all requests count = %d",
count, env_atomic_read(&master->master_remaining)); count, env_atomic_read(&master->master_remaining));
}
return req; return req;
} }
@ -777,15 +802,6 @@ static int _ocf_cleaner_do_fire(struct ocf_request *req, uint32_t count)
return result; return result;
} }
static inline uint32_t _ocf_cleaner_get_req_max_count(uint32_t count,
bool low_mem)
{
if (low_mem || count <= 4096)
return count < 128 ? count : 128;
return 1024;
}
static void _ocf_cleaner_fire_error(struct ocf_request *master, static void _ocf_cleaner_fire_error(struct ocf_request *master,
struct ocf_request *req, int err) struct ocf_request *req, int err)
{ {
@ -823,14 +839,7 @@ void ocf_cleaner_fire(struct ocf_cache *cache,
/* Allocate master request */ /* Allocate master request */
master = _ocf_cleaner_alloc_master_req(cache, max, attribs); master = _ocf_cleaner_alloc_master_req(cache, max, attribs);
if (unlikely(!master)) {
if (!master) {
/* Some memory allocation error, try re-allocate request */
max = _ocf_cleaner_get_req_max_count(count, true);
master = _ocf_cleaner_alloc_master_req(cache, max, attribs);
}
if (!master) {
attribs->cmpl_fn(attribs->cmpl_context, -OCF_ERR_NO_MEM); attribs->cmpl_fn(attribs->cmpl_context, -OCF_ERR_NO_MEM);
return; return;
} }
@ -843,28 +852,18 @@ void ocf_cleaner_fire(struct ocf_cache *cache,
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
/* when request hasn't yet been allocated or is just issued */ /* when request hasn't yet been allocated or is just issued */
if (!req) { if (unlikely(!req)) {
if (max > count - i) { if (max > count - i) {
/* less than max left */ /* less than max left */
max = count - i; max = count - i;
} }
req = _ocf_cleaner_alloc_slave_req(master, max, attribs); req = _ocf_cleaner_alloc_slave_req(master, max, attribs);
} if (unlikely(!req)) {
if (!req) {
/* Some memory allocation error,
* try re-allocate request
*/
max = _ocf_cleaner_get_req_max_count(max, true);
req = _ocf_cleaner_alloc_slave_req(master, max, attribs);
}
/* when request allocation failed stop processing */
if (!req) {
master->error = -OCF_ERR_NO_MEM; master->error = -OCF_ERR_NO_MEM;
break; break;
} }
}
if (attribs->getter(cache, attribs->getter_context, if (attribs->getter(cache, attribs->getter_context,
i, &cache_line)) { i, &cache_line)) {