Skip to content

Commit 29cc667

Browse files
amirvdavem330
authored andcommitted
net/mlx5: Store counters in rbtree instead of list
In order to use bulk counters, we need to have counters sorted by id. Signed-off-by: Amir Vadai <amir@vadai.me> Reviewed-by: Or Gerlitz <ogerlitz@mellanox.com> Signed-off-by: Saeed Mahameed <saeedm@mellanox.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 1801772 commit 29cc667

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

drivers/net/ethernet/mellanox/mlx5/core/fs_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ struct mlx5_fc_cache {
111111
};
112112

113113
struct mlx5_fc {
114+
struct rb_node node;
114115
struct list_head list;
115116

116117
/* last{packets,bytes} members are used when calculating the delta since

drivers/net/ethernet/mellanox/mlx5/core/fs_counters.c

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include <linux/mlx5/driver.h>
3434
#include <linux/mlx5/fs.h>
35+
#include <linux/rbtree.h>
3536
#include "mlx5_core.h"
3637
#include "fs_core.h"
3738
#include "fs_cmd.h"
@@ -68,32 +69,63 @@
6869
* elapsed, the thread will actually query the hardware.
6970
*/
7071

72+
static void mlx5_fc_stats_insert(struct rb_root *root, struct mlx5_fc *counter)
73+
{
74+
struct rb_node **new = &root->rb_node;
75+
struct rb_node *parent = NULL;
76+
77+
while (*new) {
78+
struct mlx5_fc *this = container_of(*new, struct mlx5_fc, node);
79+
int result = counter->id - this->id;
80+
81+
parent = *new;
82+
if (result < 0)
83+
new = &((*new)->rb_left);
84+
else
85+
new = &((*new)->rb_right);
86+
}
87+
88+
/* Add new node and rebalance tree. */
89+
rb_link_node(&counter->node, parent, new);
90+
rb_insert_color(&counter->node, root);
91+
}
92+
7193
static void mlx5_fc_stats_work(struct work_struct *work)
7294
{
7395
struct mlx5_core_dev *dev = container_of(work, struct mlx5_core_dev,
7496
priv.fc_stats.work.work);
7597
struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
7698
unsigned long now = jiffies;
7799
struct mlx5_fc *counter;
78-
struct mlx5_fc *tmp;
100+
struct rb_node *node;
101+
LIST_HEAD(tmplist);
79102
int err = 0;
80103

81104
spin_lock(&fc_stats->addlist_lock);
82105

83-
list_splice_tail_init(&fc_stats->addlist, &fc_stats->list);
106+
list_splice_tail_init(&fc_stats->addlist, &tmplist);
84107

85-
if (!list_empty(&fc_stats->list))
108+
if (!list_empty(&tmplist) || !RB_EMPTY_ROOT(&fc_stats->counters))
86109
queue_delayed_work(fc_stats->wq, &fc_stats->work, MLX5_FC_STATS_PERIOD);
87110

88111
spin_unlock(&fc_stats->addlist_lock);
89112

90-
list_for_each_entry_safe(counter, tmp, &fc_stats->list, list) {
91-
struct mlx5_fc_cache *c = &counter->cache;
113+
list_for_each_entry(counter, &tmplist, list)
114+
mlx5_fc_stats_insert(&fc_stats->counters, counter);
115+
116+
node = rb_first(&fc_stats->counters);
117+
while (node) {
118+
struct mlx5_fc_cache *c;
92119
u64 packets;
93120
u64 bytes;
94121

122+
counter = rb_entry(node, struct mlx5_fc, node);
123+
c = &counter->cache;
124+
125+
node = rb_next(node);
126+
95127
if (counter->deleted) {
96-
list_del(&counter->list);
128+
rb_erase(&counter->node, &fc_stats->counters);
97129

98130
mlx5_cmd_fc_free(dev, counter->id);
99131

@@ -176,7 +208,7 @@ int mlx5_init_fc_stats(struct mlx5_core_dev *dev)
176208
{
177209
struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
178210

179-
INIT_LIST_HEAD(&fc_stats->list);
211+
fc_stats->counters = RB_ROOT;
180212
INIT_LIST_HEAD(&fc_stats->addlist);
181213
spin_lock_init(&fc_stats->addlist_lock);
182214

@@ -194,20 +226,32 @@ void mlx5_cleanup_fc_stats(struct mlx5_core_dev *dev)
194226
struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
195227
struct mlx5_fc *counter;
196228
struct mlx5_fc *tmp;
229+
struct rb_node *node;
197230

198231
cancel_delayed_work_sync(&dev->priv.fc_stats.work);
199232
destroy_workqueue(dev->priv.fc_stats.wq);
200233
dev->priv.fc_stats.wq = NULL;
201234

202-
list_splice_tail_init(&fc_stats->addlist, &fc_stats->list);
203-
204-
list_for_each_entry_safe(counter, tmp, &fc_stats->list, list) {
235+
list_for_each_entry_safe(counter, tmp, &fc_stats->addlist, list) {
205236
list_del(&counter->list);
206237

207238
mlx5_cmd_fc_free(dev, counter->id);
208239

209240
kfree(counter);
210241
}
242+
243+
node = rb_first(&fc_stats->counters);
244+
while (node) {
245+
counter = rb_entry(node, struct mlx5_fc, node);
246+
247+
node = rb_next(node);
248+
249+
rb_erase(&counter->node, &fc_stats->counters);
250+
251+
mlx5_cmd_fc_free(dev, counter->id);
252+
253+
kfree(counter);
254+
}
211255
}
212256

213257
void mlx5_fc_query_cached(struct mlx5_fc *counter,

include/linux/mlx5/driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ struct mlx5_irq_info {
469469
};
470470

471471
struct mlx5_fc_stats {
472-
struct list_head list;
472+
struct rb_root counters;
473473
struct list_head addlist;
474474
/* protect addlist add/splice operations */
475475
spinlock_t addlist_lock;

0 commit comments

Comments
 (0)