regmap: rbtree: Reduce number of nodes, take 2
Support for reducing the number of nodes and memory consumption of the rbtree cache by allowing for small unused holes in the node's register cache block was initially added in commit0c7ed856
("regmap: Cut down on the average # of nodes in the rbtree cache"). But the commit had problems and so its effect was reverted again in commit4e67fb5
("regmap: rbtree: Fix overlapping rbnodes."). This patch brings the feature back of reducing the average number of nodes, which will speedup node look-up, while at the same time also reducing the memory usage of the rbtree cache. This patch takes a slightly different approach than the original patch though. It modifies the adjacent node look-up to not only consider nodes that are just one to the left or the right of the register but any node that falls in a certain range around the register. The range is calculated based on how much memory it would take to allocate a new node compared to how much memory it takes adding a set of unused registers to an existing node. E.g. if a node takes up 24 bytes and each register in a block uses 1 byte the range will be from the register address - 24 to the register address + 24. If we find a node that falls within this range it is cheaper or as expensive to add the register to the existing node and have a couple of unused registers in the node's cache compared to allocating a new node. Signed-off-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Mark Brown <broonie@linaro.org>
This commit is contained in:
Родитель
194c753a21
Коммит
472fdec738
|
@ -278,27 +278,34 @@ static int regcache_rbtree_read(struct regmap *map,
|
||||||
|
|
||||||
static int regcache_rbtree_insert_to_block(struct regmap *map,
|
static int regcache_rbtree_insert_to_block(struct regmap *map,
|
||||||
struct regcache_rbtree_node *rbnode,
|
struct regcache_rbtree_node *rbnode,
|
||||||
unsigned int pos, unsigned int reg,
|
unsigned int base_reg,
|
||||||
|
unsigned int top_reg,
|
||||||
|
unsigned int reg,
|
||||||
unsigned int value)
|
unsigned int value)
|
||||||
{
|
{
|
||||||
|
unsigned int blklen;
|
||||||
|
unsigned int pos, offset;
|
||||||
u8 *blk;
|
u8 *blk;
|
||||||
|
|
||||||
|
blklen = (top_reg - base_reg) / map->reg_stride + 1;
|
||||||
|
pos = (reg - base_reg) / map->reg_stride;
|
||||||
|
offset = (rbnode->base_reg - base_reg) / map->reg_stride;
|
||||||
|
|
||||||
blk = krealloc(rbnode->block,
|
blk = krealloc(rbnode->block,
|
||||||
(rbnode->blklen + 1) * map->cache_word_size,
|
blklen * map->cache_word_size,
|
||||||
GFP_KERNEL);
|
GFP_KERNEL);
|
||||||
if (!blk)
|
if (!blk)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
/* insert the register value in the correct place in the rbnode block */
|
/* insert the register value in the correct place in the rbnode block */
|
||||||
memmove(blk + (pos + 1) * map->cache_word_size,
|
if (pos == 0)
|
||||||
blk + pos * map->cache_word_size,
|
memmove(blk + offset * map->cache_word_size,
|
||||||
(rbnode->blklen - pos) * map->cache_word_size);
|
blk, rbnode->blklen * map->cache_word_size);
|
||||||
|
|
||||||
/* update the rbnode block, its size and the base register */
|
/* update the rbnode block, its size and the base register */
|
||||||
rbnode->block = blk;
|
rbnode->block = blk;
|
||||||
rbnode->blklen++;
|
rbnode->blklen = blklen;
|
||||||
if (!pos)
|
rbnode->base_reg = base_reg;
|
||||||
rbnode->base_reg = reg;
|
|
||||||
|
|
||||||
regcache_rbtree_set_register(map, rbnode, pos, value);
|
regcache_rbtree_set_register(map, rbnode, pos, value);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -352,9 +359,7 @@ static int regcache_rbtree_write(struct regmap *map, unsigned int reg,
|
||||||
struct regcache_rbtree_ctx *rbtree_ctx;
|
struct regcache_rbtree_ctx *rbtree_ctx;
|
||||||
struct regcache_rbtree_node *rbnode, *rbnode_tmp;
|
struct regcache_rbtree_node *rbnode, *rbnode_tmp;
|
||||||
struct rb_node *node;
|
struct rb_node *node;
|
||||||
unsigned int base_reg, top_reg;
|
|
||||||
unsigned int reg_tmp;
|
unsigned int reg_tmp;
|
||||||
unsigned int pos;
|
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
rbtree_ctx = map->cache;
|
rbtree_ctx = map->cache;
|
||||||
|
@ -371,6 +376,19 @@ static int regcache_rbtree_write(struct regmap *map, unsigned int reg,
|
||||||
reg_tmp = (reg - rbnode->base_reg) / map->reg_stride;
|
reg_tmp = (reg - rbnode->base_reg) / map->reg_stride;
|
||||||
regcache_rbtree_set_register(map, rbnode, reg_tmp, value);
|
regcache_rbtree_set_register(map, rbnode, reg_tmp, value);
|
||||||
} else {
|
} else {
|
||||||
|
unsigned int base_reg, top_reg;
|
||||||
|
unsigned int new_base_reg, new_top_reg;
|
||||||
|
unsigned int min, max;
|
||||||
|
unsigned int max_dist;
|
||||||
|
|
||||||
|
max_dist = map->reg_stride * sizeof(*rbnode_tmp) /
|
||||||
|
map->cache_word_size;
|
||||||
|
if (reg < max_dist)
|
||||||
|
min = 0;
|
||||||
|
else
|
||||||
|
min = reg - max_dist;
|
||||||
|
max = reg + max_dist;
|
||||||
|
|
||||||
/* look for an adjacent register to the one we are about to add */
|
/* look for an adjacent register to the one we are about to add */
|
||||||
for (node = rb_first(&rbtree_ctx->root); node;
|
for (node = rb_first(&rbtree_ctx->root); node;
|
||||||
node = rb_next(node)) {
|
node = rb_next(node)) {
|
||||||
|
@ -380,16 +398,17 @@ static int regcache_rbtree_write(struct regmap *map, unsigned int reg,
|
||||||
regcache_rbtree_get_base_top_reg(map, rbnode_tmp,
|
regcache_rbtree_get_base_top_reg(map, rbnode_tmp,
|
||||||
&base_reg, &top_reg);
|
&base_reg, &top_reg);
|
||||||
|
|
||||||
/* decide where in the block to place our register */
|
if (base_reg <= max && top_reg >= min) {
|
||||||
if (base_reg > 0 && reg == base_reg - map->reg_stride)
|
new_base_reg = min(reg, base_reg);
|
||||||
pos = 0;
|
new_top_reg = max(reg, top_reg);
|
||||||
else if (reg > 0 && reg - map->reg_stride == top_reg)
|
} else {
|
||||||
pos = rbnode_tmp->blklen;
|
|
||||||
else
|
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
ret = regcache_rbtree_insert_to_block(map, rbnode_tmp,
|
ret = regcache_rbtree_insert_to_block(map, rbnode_tmp,
|
||||||
pos, reg, value);
|
new_base_reg,
|
||||||
|
new_top_reg, reg,
|
||||||
|
value);
|
||||||
if (ret)
|
if (ret)
|
||||||
return ret;
|
return ret;
|
||||||
rbtree_ctx->cached_rbnode = rbnode_tmp;
|
rbtree_ctx->cached_rbnode = rbnode_tmp;
|
||||||
|
|
Загрузка…
Ссылка в новой задаче