4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * This file is part of Lustre, http://www.lustre.org/
25 * lustre/obdclass/lu_tgt_descs.c
27 * Lustre target descriptions
28 * These are the only exported functions, they provide some generic
29 * infrastructure for target description management used by LOD/LMV
33 #define DEBUG_SUBSYSTEM S_CLASS
35 #include <linux/module.h>
36 #include <linux/list.h>
37 #include <linux/random.h>
38 #include <libcfs/libcfs.h>
39 #include <libcfs/libcfs_hash.h> /* hash_long() */
40 #include <libcfs/linux/linux-mem.h>
41 #include <obd_class.h>
42 #include <obd_support.h>
43 #include <lustre_disk.h>
44 #include <lustre_fid.h>
45 #include <lu_object.h>
48 * lu_prandom_u64_max - returns a pseudo-random u64 number in interval
51 * \param[in] ep_ro right open interval endpoint
53 * \retval a pseudo-random 64-bit number that is in interval [0, ep_ro).
55 u64 lu_prandom_u64_max(u64 ep_ro)
60 #if BITS_PER_LONG == 32
62 * If ep_ro > 32-bit, first generate the high
63 * 32 bits of the random number, then add in the low
64 * 32 bits (truncated to the upper limit, if needed)
66 if (ep_ro > 0xffffffffULL)
67 rand = (u64)prandom_u32_max((u32)(ep_ro >> 32)) << 32;
69 if (rand == (ep_ro & 0xffffffff00000000ULL))
70 rand |= prandom_u32_max((u32)ep_ro);
72 rand |= prandom_u32();
74 rand = ((u64)prandom_u32() << 32 | prandom_u32()) % ep_ro;
80 EXPORT_SYMBOL(lu_prandom_u64_max);
83 * Add a new target to Quality of Service (QoS) target table.
85 * Add a new MDT/OST target to the structure representing an OSS. Resort the
86 * list of known MDSs/OSSs by the number of MDTs/OSTs attached to each MDS/OSS.
87 * The MDS/OSS list is protected internally and no external locking is required.
89 * \param[in] qos lu_qos data
90 * \param[in] tgt target description
92 * \retval 0 on success
93 * \retval -ENOMEM on error
95 int lu_qos_add_tgt(struct lu_qos *qos, struct lu_tgt_desc *tgt)
97 struct lu_svr_qos *svr = NULL;
98 struct lu_svr_qos *tempsvr;
99 struct obd_export *exp = tgt->ltd_exp;
106 down_write(&qos->lq_rw_sem);
108 * a bit hacky approach to learn NID of corresponding connection
109 * but there is no official API to access information like this
112 list_for_each_entry(svr, &qos->lq_svr_list, lsq_svr_list) {
113 if (obd_uuid_equals(&svr->lsq_uuid,
114 &exp->exp_connection->c_remote_uuid)) {
118 if (svr->lsq_id > id)
125 GOTO(out, rc = -ENOMEM);
126 memcpy(&svr->lsq_uuid, &exp->exp_connection->c_remote_uuid,
127 sizeof(svr->lsq_uuid));
131 /* Assume we have to move this one */
132 list_del(&svr->lsq_svr_list);
135 svr->lsq_tgt_count++;
136 tgt->ltd_qos.ltq_svr = svr;
138 CDEBUG(D_OTHER, "add tgt %s to server %s (%d targets)\n",
139 obd_uuid2str(&tgt->ltd_uuid), obd_uuid2str(&svr->lsq_uuid),
143 * Add sorted by # of tgts. Find the first entry that we're
146 list_for_each_entry(tempsvr, &qos->lq_svr_list, lsq_svr_list) {
147 if (svr->lsq_tgt_count > tempsvr->lsq_tgt_count)
151 * ...and add before it. If we're the first or smallest, tempsvr
152 * points to the list head, and we add to the end.
154 list_add_tail(&svr->lsq_svr_list, &tempsvr->lsq_svr_list);
156 set_bit(LQ_DIRTY, &qos->lq_flags);
157 #ifdef HAVE_SERVER_SUPPORT
158 set_bit(LQ_DIRTY, &qos->lq_rr.lqr_flags);
161 up_write(&qos->lq_rw_sem);
164 EXPORT_SYMBOL(lu_qos_add_tgt);
167 * Remove MDT/OST target from QoS table.
169 * Removes given MDT/OST target from QoS table and releases related
170 * MDS/OSS structure if no target remain on the MDS/OSS.
172 * \param[in] qos lu_qos data
173 * \param[in] ltd target description
175 * \retval 0 on success
176 * \retval -ENOENT if no server was found
178 int lu_qos_del_tgt(struct lu_qos *qos, struct lu_tgt_desc *ltd)
180 struct lu_svr_qos *svr;
185 down_write(&qos->lq_rw_sem);
186 svr = ltd->ltd_qos.ltq_svr;
188 GOTO(out, rc = -ENOENT);
190 ltd->ltd_qos.ltq_svr = NULL;
191 svr->lsq_tgt_count--;
192 if (svr->lsq_tgt_count == 0) {
193 CDEBUG(D_OTHER, "removing server %s\n",
194 obd_uuid2str(&svr->lsq_uuid));
195 list_del(&svr->lsq_svr_list);
199 set_bit(LQ_DIRTY, &qos->lq_flags);
200 #ifdef HAVE_SERVER_SUPPORT
201 set_bit(LQ_DIRTY, &qos->lq_rr.lqr_flags);
204 up_write(&qos->lq_rw_sem);
207 EXPORT_SYMBOL(lu_qos_del_tgt);
210 * Calculate weight for a given tgt.
212 * The final tgt weight uses only free space for OSTs, but combines
213 * both free space and inodes for MDTs, minus tgt and server penalties.
214 * See ltd_qos_penalties_calc() for how penalties are calculated.
216 * \param[in] tgt target descriptor
217 * \param[in] is_mdt target table is for MDT selection (use inodes)
219 void lu_tgt_qos_weight_calc(struct lu_tgt_desc *tgt, bool is_mdt)
221 struct lu_tgt_qos *ltq = &tgt->ltd_qos;
225 ltq->ltq_avail = (tgt_statfs_bavail(tgt) >> 16) *
226 (tgt_statfs_iavail(tgt) >> 8);
228 ltq->ltq_avail = tgt_statfs_bavail(tgt) >> 8;
229 penalty = ltq->ltq_penalty + ltq->ltq_svr->lsq_penalty;
230 if (ltq->ltq_avail < penalty)
233 ltq->ltq_weight = ltq->ltq_avail - penalty;
235 EXPORT_SYMBOL(lu_tgt_qos_weight_calc);
238 * Allocate and initialize target table.
240 * A helper function to initialize the target table and allocate
241 * a bitmap of the available targets.
243 * \param[in] ltd target's table to initialize
244 * \param[in] is_mdt target table for MDTs
246 * \retval 0 on success
247 * \retval negative negated errno on error
249 int lu_tgt_descs_init(struct lu_tgt_descs *ltd, bool is_mdt)
251 mutex_init(<d->ltd_mutex);
252 init_rwsem(<d->ltd_rw_sem);
255 * the tgt array and bitmap are allocated/grown dynamically as tgts are
256 * added to the LOD/LMV, see lu_tgt_descs_add()
258 ltd->ltd_tgt_bitmap = bitmap_zalloc(BITS_PER_LONG, GFP_NOFS);
259 if (!ltd->ltd_tgt_bitmap)
262 ltd->ltd_tgts_size = BITS_PER_LONG;
263 ltd->ltd_death_row = 0;
264 ltd->ltd_refcount = 0;
266 /* Set up allocation policy (QoS and RR) */
267 INIT_LIST_HEAD(<d->ltd_qos.lq_svr_list);
268 init_rwsem(<d->ltd_qos.lq_rw_sem);
269 set_bit(LQ_DIRTY, <d->ltd_qos.lq_flags);
270 set_bit(LQ_RESET, <d->ltd_qos.lq_flags);
271 ltd->ltd_is_mdt = is_mdt;
272 /* MDT imbalance threshold is low to balance across MDTs
273 * relatively quickly, because each directory may result
274 * in a large number of files/subdirs created therein.
277 ltd->ltd_lmv_desc.ld_pattern = LMV_HASH_TYPE_DEFAULT;
278 ltd->ltd_qos.lq_prio_free = LMV_QOS_DEF_PRIO_FREE * 256 / 100;
279 ltd->ltd_qos.lq_threshold_rr =
280 LMV_QOS_DEF_THRESHOLD_RR_PCT *
281 QOS_THRESHOLD_MAX / 100;
283 ltd->ltd_qos.lq_prio_free = LOV_QOS_DEF_PRIO_FREE * 256 / 100;
284 ltd->ltd_qos.lq_threshold_rr =
285 LOV_QOS_DEF_THRESHOLD_RR_PCT *
286 QOS_THRESHOLD_MAX / 100;
291 EXPORT_SYMBOL(lu_tgt_descs_init);
294 * Free bitmap and target table pages.
296 * \param[in] ltd target table
298 void lu_tgt_descs_fini(struct lu_tgt_descs *ltd)
302 bitmap_free(ltd->ltd_tgt_bitmap);
303 for (i = 0; i < ARRAY_SIZE(ltd->ltd_tgt_idx); i++) {
304 if (ltd->ltd_tgt_idx[i])
305 OBD_FREE_PTR(ltd->ltd_tgt_idx[i]);
307 ltd->ltd_tgts_size = 0;
309 EXPORT_SYMBOL(lu_tgt_descs_fini);
312 * Expand size of target table.
314 * When the target table is full, we have to extend the table. To do so,
315 * we allocate new memory with some reserve, move data from the old table
316 * to the new one and release memory consumed by the old table.
318 * \param[in] ltd target table
319 * \param[in] newsize new size of the table
321 * \retval 0 on success
322 * \retval -ENOMEM if reallocation failed
324 static int lu_tgt_descs_resize(struct lu_tgt_descs *ltd, __u32 newsize)
326 unsigned long *new_bitmap, *old_bitmap = NULL;
328 /* someone else has already resize the array */
329 if (newsize <= ltd->ltd_tgts_size)
332 new_bitmap = bitmap_zalloc(newsize, GFP_NOFS);
336 if (ltd->ltd_tgts_size > 0) {
337 /* the bitmap already exists, copy data from old one */
338 bitmap_copy(new_bitmap, ltd->ltd_tgt_bitmap,
340 old_bitmap = ltd->ltd_tgt_bitmap;
343 ltd->ltd_tgts_size = newsize;
344 ltd->ltd_tgt_bitmap = new_bitmap;
346 bitmap_free(old_bitmap);
348 CDEBUG(D_CONFIG, "tgt size: %d\n", ltd->ltd_tgts_size);
354 * Add new target to target table.
356 * Extend target table if it's full, update target table and bitmap.
357 * Notice we need to take ltd_rw_sem exclusively before entry to ensure
360 * \param[in] ltd target table
361 * \param[in] tgt new target desc
363 * \retval 0 on success
364 * \retval -ENOMEM if reallocation failed
365 * -EEXIST if target existed
367 int ltd_add_tgt(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt)
369 __u32 index = tgt->ltd_index;
374 if (index >= ltd->ltd_tgts_size) {
377 if (index > TGT_PTRS * TGT_PTRS_PER_BLOCK)
380 while (newsize < index + 1)
381 newsize = newsize << 1;
383 rc = lu_tgt_descs_resize(ltd, newsize);
386 } else if (test_bit(index, ltd->ltd_tgt_bitmap)) {
390 if (ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
391 OBD_ALLOC_PTR(ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK]);
392 if (ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
396 LTD_TGT(ltd, tgt->ltd_index) = tgt;
397 set_bit(tgt->ltd_index, ltd->ltd_tgt_bitmap);
399 ltd->ltd_lov_desc.ld_tgt_count++;
401 ltd->ltd_lov_desc.ld_active_tgt_count++;
405 EXPORT_SYMBOL(ltd_add_tgt);
408 * Delete target from target table
410 void ltd_del_tgt(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt)
412 lu_qos_del_tgt(<d->ltd_qos, tgt);
413 LTD_TGT(ltd, tgt->ltd_index) = NULL;
414 clear_bit(tgt->ltd_index, ltd->ltd_tgt_bitmap);
415 ltd->ltd_lov_desc.ld_tgt_count--;
417 ltd->ltd_lov_desc.ld_active_tgt_count--;
419 EXPORT_SYMBOL(ltd_del_tgt);
422 * Calculate penalties per-tgt and per-server
424 * Re-calculate penalties when the configuration changes, active targets
425 * change and after statfs refresh (all these are reflected by lq_dirty flag).
426 * On every tgt and server: decay the penalty by half for every 8x the update
427 * interval that the device has been idle. That gives lots of time for the
428 * statfs information to be updated (which the penalty is only a proxy for),
429 * and avoids penalizing server/tgt under light load.
430 * See lu_qos_tgt_weight_calc() for how penalties are factored into the weight.
432 * \param[in] ltd lu_tgt_descs
434 * \retval 0 on success
435 * \retval -EAGAIN the number of tgt isn't enough or all tgt spaces are
438 int ltd_qos_penalties_calc(struct lu_tgt_descs *ltd)
440 struct lu_qos *qos = <d->ltd_qos;
441 struct lov_desc *desc = <d->ltd_lov_desc;
442 struct lu_tgt_desc *tgt;
443 struct lu_svr_qos *svr;
444 __u64 ba_max, ba_min, ba;
445 __u64 ia_max, ia_min, ia = 1;
453 if (!test_bit(LQ_DIRTY, &qos->lq_flags))
456 num_active = desc->ld_active_tgt_count - 1;
458 GOTO(out, rc = -EAGAIN);
460 /* find bavail on each server */
461 list_for_each_entry(svr, &qos->lq_svr_list, lsq_svr_list) {
463 /* if inode is not counted, set to 1 to ignore */
464 svr->lsq_iavail = ltd->ltd_is_mdt ? 0 : 1;
466 qos->lq_active_svr_count = 0;
469 * How badly user wants to select targets "widely" (not recently chosen
470 * and not on recent MDS's). As opposed to "freely" (free space avail.)
473 prio_wide = 256 - qos->lq_prio_free;
475 ba_min = (__u64)(-1);
477 ia_min = (__u64)(-1);
479 now = ktime_get_real_seconds();
481 /* Calculate server penalty per object */
482 ltd_foreach_tgt(ltd, tgt) {
483 if (!tgt->ltd_active)
486 /* when inode is counted, bavail >> 16 to avoid overflow */
487 ba = tgt_statfs_bavail(tgt);
495 ba_min = min(ba, ba_min);
496 ba_max = max(ba, ba_max);
498 /* Count the number of usable servers */
499 if (tgt->ltd_qos.ltq_svr->lsq_bavail == 0)
500 qos->lq_active_svr_count++;
501 tgt->ltd_qos.ltq_svr->lsq_bavail += ba;
503 if (ltd->ltd_is_mdt) {
504 /* iavail >> 8 to avoid overflow */
505 ia = tgt_statfs_iavail(tgt) >> 8;
509 ia_min = min(ia, ia_min);
510 ia_max = max(ia, ia_max);
512 tgt->ltd_qos.ltq_svr->lsq_iavail += ia;
517 * prio * bavail * iavail / (num_tgt - 1) / prio_max / 2
519 tgt->ltd_qos.ltq_penalty_per_obj = prio_wide * ba * ia >> 9;
520 do_div(tgt->ltd_qos.ltq_penalty_per_obj, num_active);
522 age = (now - tgt->ltd_qos.ltq_used) >> 3;
523 if (test_bit(LQ_RESET, &qos->lq_flags) ||
524 age > 32 * desc->ld_qos_maxage)
525 tgt->ltd_qos.ltq_penalty = 0;
526 else if (age > desc->ld_qos_maxage)
527 /* Decay tgt penalty. */
528 tgt->ltd_qos.ltq_penalty >>= age / desc->ld_qos_maxage;
531 num_active = qos->lq_active_svr_count - 1;
532 if (num_active < 1) {
534 * If there's only 1 server, we can't penalize it, so instead
535 * we have to double the tgt penalty
538 ltd_foreach_tgt(ltd, tgt) {
539 if (!tgt->ltd_active)
542 tgt->ltd_qos.ltq_penalty_per_obj <<= 1;
547 * Per-server penalty is
548 * prio * bavail * iavail / server_tgts / (num_svr - 1) / 2
550 list_for_each_entry(svr, &qos->lq_svr_list, lsq_svr_list) {
551 ba = svr->lsq_bavail;
552 ia = svr->lsq_iavail;
553 svr->lsq_penalty_per_obj = prio_wide * ba * ia >> 8;
554 do_div(svr->lsq_penalty_per_obj,
555 svr->lsq_tgt_count * num_active);
556 svr->lsq_penalty_per_obj >>= 1;
558 age = (now - svr->lsq_used) >> 3;
559 if (test_bit(LQ_RESET, &qos->lq_flags) ||
560 age > 32 * desc->ld_qos_maxage)
561 svr->lsq_penalty = 0;
562 else if (age > desc->ld_qos_maxage)
563 /* Decay server penalty. */
564 svr->lsq_penalty >>= age / desc->ld_qos_maxage;
569 * If each tgt has almost same free space, do rr allocation for better
570 * creation performance
572 if (((ba_max * (QOS_THRESHOLD_MAX - qos->lq_threshold_rr)) /
573 QOS_THRESHOLD_MAX) < ba_min &&
574 ((ia_max * (QOS_THRESHOLD_MAX - qos->lq_threshold_rr)) /
575 QOS_THRESHOLD_MAX) < ia_min) {
576 set_bit(LQ_SAME_SPACE, &qos->lq_flags);
577 /* Reset weights for the next time we enter qos mode */
578 set_bit(LQ_RESET, &qos->lq_flags);
580 clear_bit(LQ_SAME_SPACE, &qos->lq_flags);
581 clear_bit(LQ_RESET, &qos->lq_flags);
583 clear_bit(LQ_DIRTY, &qos->lq_flags);
587 if (!rc && test_bit(LQ_SAME_SPACE, &qos->lq_flags))
592 EXPORT_SYMBOL(ltd_qos_penalties_calc);
595 * Re-calculate penalties and weights of all tgts.
597 * The function is called when some target was used for a new object. In
598 * this case we should re-calculate all the weights to keep new allocations
601 * \param[in] ltd lu_tgt_descs
602 * \param[in] tgt recently used tgt
603 * \param[out] total_wt new total weight for the pool
607 int ltd_qos_update(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt,
610 struct lu_qos *qos = <d->ltd_qos;
611 struct lu_tgt_qos *ltq;
612 struct lu_svr_qos *svr;
619 /* Don't allocate on this device anymore, until the next alloc_qos */
625 * Decay old penalty by half (we're adding max penalty, and don't
626 * want it to run away.)
628 ltq->ltq_penalty >>= 1;
629 svr->lsq_penalty >>= 1;
631 /* mark the server and tgt as recently used */
632 ltq->ltq_used = svr->lsq_used = ktime_get_real_seconds();
634 /* Set max penalties for this tgt and server */
635 ltq->ltq_penalty += ltq->ltq_penalty_per_obj *
636 ltd->ltd_lov_desc.ld_active_tgt_count;
637 svr->lsq_penalty += svr->lsq_penalty_per_obj *
638 qos->lq_active_svr_count;
640 /* Decrease all MDS penalties */
641 list_for_each_entry(svr, &qos->lq_svr_list, lsq_svr_list) {
642 if (svr->lsq_penalty < svr->lsq_penalty_per_obj)
643 svr->lsq_penalty = 0;
645 svr->lsq_penalty -= svr->lsq_penalty_per_obj;
649 /* Decrease all tgt penalties */
650 ltd_foreach_tgt(ltd, tgt) {
651 if (!tgt->ltd_active)
655 if (ltq->ltq_penalty < ltq->ltq_penalty_per_obj)
656 ltq->ltq_penalty = 0;
658 ltq->ltq_penalty -= ltq->ltq_penalty_per_obj;
660 lu_tgt_qos_weight_calc(tgt, ltd->ltd_is_mdt);
662 /* Recalc the total weight of usable osts */
664 *total_wt += ltq->ltq_weight;
666 CDEBUG(D_OTHER, "recalc tgt %d usable=%d bavail=%llu ffree=%llu tgtppo=%llu tgtp=%llu svrppo=%llu svrp=%llu wt=%llu\n",
667 tgt->ltd_index, ltq->ltq_usable,
668 tgt_statfs_bavail(tgt) >> 16,
669 tgt_statfs_iavail(tgt) >> 8,
670 ltq->ltq_penalty_per_obj >> 10,
671 ltq->ltq_penalty >> 10,
672 ltq->ltq_svr->lsq_penalty_per_obj >> 10,
673 ltq->ltq_svr->lsq_penalty >> 10,
674 ltq->ltq_weight >> 10);
679 EXPORT_SYMBOL(ltd_qos_update);