Skip to content

Commit 3986f9a

Browse files
committed
libceph: multiple workspaces for CRUSH computations
Replace a global map->crush_workspace (protected by a global mutex) with a list of workspaces, up to the number of CPUs + 1. This is based on a patch from Robin Geuze <robing@nl.team.blue>. Robin and his team have observed a 10-20% increase in IOPS on all queue depths and lower CPU usage as well on a high-end all-NVMe 100GbE cluster. Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
1 parent 1c30c90 commit 3986f9a

3 files changed

Lines changed: 166 additions & 17 deletions

File tree

‎include/linux/ceph/osdmap.h‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp,
137137
const char *fmt, ...);
138138
void ceph_oid_destroy(struct ceph_object_id *oid);
139139

140+
struct workspace_manager {
141+
struct list_head idle_ws;
142+
spinlock_t ws_lock;
143+
/* Number of free workspaces */
144+
int free_ws;
145+
/* Total number of allocated workspaces */
146+
atomic_t total_ws;
147+
/* Waiters for a free workspace */
148+
wait_queue_head_t ws_wait;
149+
};
150+
140151
struct ceph_pg_mapping {
141152
struct rb_node node;
142153
struct ceph_pg pgid;
@@ -184,8 +195,7 @@ struct ceph_osdmap {
184195
* the list of osds that store+replicate them. */
185196
struct crush_map *crush;
186197

187-
struct mutex crush_workspace_mutex;
188-
void *crush_workspace;
198+
struct workspace_manager crush_wsm;
189199
};
190200

191201
static inline bool ceph_osd_exists(struct ceph_osdmap *map, int osd)

‎include/linux/crush/crush.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ struct crush_work_bucket {
346346

347347
struct crush_work {
348348
struct crush_work_bucket **work; /* Per-bucket working store */
349+
#ifdef __KERNEL__
350+
struct list_head item;
351+
#endif
349352
};
350353

351354
#ifdef __KERNEL__

‎net/ceph/osdmap.c‎

Lines changed: 151 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,143 @@ static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
964964
return -EINVAL;
965965
}
966966

967+
/*
968+
* CRUSH workspaces
969+
*
970+
* workspace_manager framework borrowed from fs/btrfs/compression.c.
971+
* Two simplifications: there is only one type of workspace and there
972+
* is always at least one workspace.
973+
*/
974+
static struct crush_work *alloc_workspace(const struct crush_map *c)
975+
{
976+
struct crush_work *work;
977+
size_t work_size;
978+
979+
WARN_ON(!c->working_size);
980+
work_size = crush_work_size(c, CEPH_PG_MAX_SIZE);
981+
dout("%s work_size %zu bytes\n", __func__, work_size);
982+
983+
work = ceph_kvmalloc(work_size, GFP_NOIO);
984+
if (!work)
985+
return NULL;
986+
987+
INIT_LIST_HEAD(&work->item);
988+
crush_init_workspace(c, work);
989+
return work;
990+
}
991+
992+
static void free_workspace(struct crush_work *work)
993+
{
994+
WARN_ON(!list_empty(&work->item));
995+
kvfree(work);
996+
}
997+
998+
static void init_workspace_manager(struct workspace_manager *wsm)
999+
{
1000+
INIT_LIST_HEAD(&wsm->idle_ws);
1001+
spin_lock_init(&wsm->ws_lock);
1002+
atomic_set(&wsm->total_ws, 0);
1003+
wsm->free_ws = 0;
1004+
init_waitqueue_head(&wsm->ws_wait);
1005+
}
1006+
1007+
static void add_initial_workspace(struct workspace_manager *wsm,
1008+
struct crush_work *work)
1009+
{
1010+
WARN_ON(!list_empty(&wsm->idle_ws));
1011+
1012+
list_add(&work->item, &wsm->idle_ws);
1013+
atomic_set(&wsm->total_ws, 1);
1014+
wsm->free_ws = 1;
1015+
}
1016+
1017+
static void cleanup_workspace_manager(struct workspace_manager *wsm)
1018+
{
1019+
struct crush_work *work;
1020+
1021+
while (!list_empty(&wsm->idle_ws)) {
1022+
work = list_first_entry(&wsm->idle_ws, struct crush_work,
1023+
item);
1024+
list_del_init(&work->item);
1025+
free_workspace(work);
1026+
}
1027+
atomic_set(&wsm->total_ws, 0);
1028+
wsm->free_ws = 0;
1029+
}
1030+
1031+
/*
1032+
* Finds an available workspace or allocates a new one. If it's not
1033+
* possible to allocate a new one, waits until there is one.
1034+
*/
1035+
static struct crush_work *get_workspace(struct workspace_manager *wsm,
1036+
const struct crush_map *c)
1037+
{
1038+
struct crush_work *work;
1039+
int cpus = num_online_cpus();
1040+
1041+
again:
1042+
spin_lock(&wsm->ws_lock);
1043+
if (!list_empty(&wsm->idle_ws)) {
1044+
work = list_first_entry(&wsm->idle_ws, struct crush_work,
1045+
item);
1046+
list_del_init(&work->item);
1047+
wsm->free_ws--;
1048+
spin_unlock(&wsm->ws_lock);
1049+
return work;
1050+
1051+
}
1052+
if (atomic_read(&wsm->total_ws) > cpus) {
1053+
DEFINE_WAIT(wait);
1054+
1055+
spin_unlock(&wsm->ws_lock);
1056+
prepare_to_wait(&wsm->ws_wait, &wait, TASK_UNINTERRUPTIBLE);
1057+
if (atomic_read(&wsm->total_ws) > cpus && !wsm->free_ws)
1058+
schedule();
1059+
finish_wait(&wsm->ws_wait, &wait);
1060+
goto again;
1061+
}
1062+
atomic_inc(&wsm->total_ws);
1063+
spin_unlock(&wsm->ws_lock);
1064+
1065+
work = alloc_workspace(c);
1066+
if (!work) {
1067+
atomic_dec(&wsm->total_ws);
1068+
wake_up(&wsm->ws_wait);
1069+
1070+
/*
1071+
* Do not return the error but go back to waiting. We
1072+
* have the inital workspace and the CRUSH computation
1073+
* time is bounded so we will get it eventually.
1074+
*/
1075+
WARN_ON(atomic_read(&wsm->total_ws) < 1);
1076+
goto again;
1077+
}
1078+
return work;
1079+
}
1080+
1081+
/*
1082+
* Puts a workspace back on the list or frees it if we have enough
1083+
* idle ones sitting around.
1084+
*/
1085+
static void put_workspace(struct workspace_manager *wsm,
1086+
struct crush_work *work)
1087+
{
1088+
spin_lock(&wsm->ws_lock);
1089+
if (wsm->free_ws <= num_online_cpus()) {
1090+
list_add(&work->item, &wsm->idle_ws);
1091+
wsm->free_ws++;
1092+
spin_unlock(&wsm->ws_lock);
1093+
goto wake;
1094+
}
1095+
spin_unlock(&wsm->ws_lock);
1096+
1097+
free_workspace(work);
1098+
atomic_dec(&wsm->total_ws);
1099+
wake:
1100+
if (wq_has_sleeper(&wsm->ws_wait))
1101+
wake_up(&wsm->ws_wait);
1102+
}
1103+
9671104
/*
9681105
* osd map
9691106
*/
@@ -981,16 +1118,20 @@ struct ceph_osdmap *ceph_osdmap_alloc(void)
9811118
map->primary_temp = RB_ROOT;
9821119
map->pg_upmap = RB_ROOT;
9831120
map->pg_upmap_items = RB_ROOT;
984-
mutex_init(&map->crush_workspace_mutex);
1121+
1122+
init_workspace_manager(&map->crush_wsm);
9851123

9861124
return map;
9871125
}
9881126

9891127
void ceph_osdmap_destroy(struct ceph_osdmap *map)
9901128
{
9911129
dout("osdmap_destroy %p\n", map);
1130+
9921131
if (map->crush)
9931132
crush_destroy(map->crush);
1133+
cleanup_workspace_manager(&map->crush_wsm);
1134+
9941135
while (!RB_EMPTY_ROOT(&map->pg_temp)) {
9951136
struct ceph_pg_mapping *pg =
9961137
rb_entry(rb_first(&map->pg_temp),
@@ -1029,7 +1170,6 @@ void ceph_osdmap_destroy(struct ceph_osdmap *map)
10291170
kvfree(map->osd_weight);
10301171
kvfree(map->osd_addr);
10311172
kvfree(map->osd_primary_affinity);
1032-
kvfree(map->crush_workspace);
10331173
kfree(map);
10341174
}
10351175

@@ -1104,26 +1244,22 @@ static int osdmap_set_max_osd(struct ceph_osdmap *map, u32 max)
11041244

11051245
static int osdmap_set_crush(struct ceph_osdmap *map, struct crush_map *crush)
11061246
{
1107-
void *workspace;
1108-
size_t work_size;
1247+
struct crush_work *work;
11091248

11101249
if (IS_ERR(crush))
11111250
return PTR_ERR(crush);
11121251

1113-
work_size = crush_work_size(crush, CEPH_PG_MAX_SIZE);
1114-
dout("%s work_size %zu bytes\n", __func__, work_size);
1115-
workspace = ceph_kvmalloc(work_size, GFP_NOIO);
1116-
if (!workspace) {
1252+
work = alloc_workspace(crush);
1253+
if (!work) {
11171254
crush_destroy(crush);
11181255
return -ENOMEM;
11191256
}
1120-
crush_init_workspace(crush, workspace);
11211257

11221258
if (map->crush)
11231259
crush_destroy(map->crush);
1124-
kvfree(map->crush_workspace);
1260+
cleanup_workspace_manager(&map->crush_wsm);
11251261
map->crush = crush;
1126-
map->crush_workspace = workspace;
1262+
add_initial_workspace(&map->crush_wsm, work);
11271263
return 0;
11281264
}
11291265

@@ -2322,6 +2458,7 @@ static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
23222458
s64 choose_args_index)
23232459
{
23242460
struct crush_choose_arg_map *arg_map;
2461+
struct crush_work *work;
23252462
int r;
23262463

23272464
BUG_ON(result_max > CEPH_PG_MAX_SIZE);
@@ -2332,12 +2469,11 @@ static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
23322469
arg_map = lookup_choose_arg_map(&map->crush->choose_args,
23332470
CEPH_DEFAULT_CHOOSE_ARGS);
23342471

2335-
mutex_lock(&map->crush_workspace_mutex);
2472+
work = get_workspace(&map->crush_wsm, map->crush);
23362473
r = crush_do_rule(map->crush, ruleno, x, result, result_max,
2337-
weight, weight_max, map->crush_workspace,
2474+
weight, weight_max, work,
23382475
arg_map ? arg_map->args : NULL);
2339-
mutex_unlock(&map->crush_workspace_mutex);
2340-
2476+
put_workspace(&map->crush_wsm, work);
23412477
return r;
23422478
}
23432479

0 commit comments

Comments
 (0)