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 = 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);
82 void lu_qos_rr_init(struct lu_qos_rr *lqr)
84 spin_lock_init(&lqr->lqr_alloc);
87 EXPORT_SYMBOL(lu_qos_rr_init);
90 * Add a new target to Quality of Service (QoS) target table.
92 * Add a new MDT/OST target to the structure representing an OSS. Resort the
93 * list of known MDSs/OSSs by the number of MDTs/OSTs attached to each MDS/OSS.
94 * The MDS/OSS list is protected internally and no external locking is required.
96 * \param[in] qos lu_qos data
97 * \param[in] tgt target description
99 * \retval 0 on success
100 * \retval -ENOMEM on error
102 int lu_qos_add_tgt(struct lu_qos *qos, struct lu_tgt_desc *tgt)
104 struct lu_svr_qos *svr = NULL;
105 struct lu_svr_qos *tempsvr;
106 struct obd_export *exp = tgt->ltd_exp;
113 down_write(&qos->lq_rw_sem);
115 * a bit hacky approach to learn NID of corresponding connection
116 * but there is no official API to access information like this
119 list_for_each_entry(svr, &qos->lq_svr_list, lsq_svr_list) {
120 if (obd_uuid_equals(&svr->lsq_uuid,
121 &exp->exp_connection->c_remote_uuid)) {
125 if (svr->lsq_id > id)
132 GOTO(out, rc = -ENOMEM);
133 memcpy(&svr->lsq_uuid, &exp->exp_connection->c_remote_uuid,
134 sizeof(svr->lsq_uuid));
138 /* Assume we have to move this one */
139 list_del(&svr->lsq_svr_list);
142 svr->lsq_tgt_count++;
143 tgt->ltd_qos.ltq_svr = svr;
145 CDEBUG(D_OTHER, "add tgt %s to server %s (%d targets)\n",
146 obd_uuid2str(&tgt->ltd_uuid), obd_uuid2str(&svr->lsq_uuid),
150 * Add sorted by # of tgts. Find the first entry that we're
153 list_for_each_entry(tempsvr, &qos->lq_svr_list, lsq_svr_list) {
154 if (svr->lsq_tgt_count > tempsvr->lsq_tgt_count)
158 * ...and add before it. If we're the first or smallest, tempsvr
159 * points to the list head, and we add to the end.
161 list_add_tail(&svr->lsq_svr_list, &tempsvr->lsq_svr_list);
164 qos->lq_rr.lqr_dirty = 1;
167 up_write(&qos->lq_rw_sem);
170 EXPORT_SYMBOL(lu_qos_add_tgt);
173 * Remove MDT/OST target from QoS table.
175 * Removes given MDT/OST target from QoS table and releases related
176 * MDS/OSS structure if no target remain on the MDS/OSS.
178 * \param[in] qos lu_qos data
179 * \param[in] ltd target description
181 * \retval 0 on success
182 * \retval -ENOENT if no server was found
184 static int lu_qos_del_tgt(struct lu_qos *qos, struct lu_tgt_desc *ltd)
186 struct lu_svr_qos *svr;
191 down_write(&qos->lq_rw_sem);
192 svr = ltd->ltd_qos.ltq_svr;
194 GOTO(out, rc = -ENOENT);
196 svr->lsq_tgt_count--;
197 if (svr->lsq_tgt_count == 0) {
198 CDEBUG(D_OTHER, "removing server %s\n",
199 obd_uuid2str(&svr->lsq_uuid));
200 list_del(&svr->lsq_svr_list);
201 ltd->ltd_qos.ltq_svr = NULL;
206 qos->lq_rr.lqr_dirty = 1;
208 up_write(&qos->lq_rw_sem);
212 static inline __u64 tgt_statfs_bavail(struct lu_tgt_desc *tgt)
214 struct obd_statfs *statfs = &tgt->ltd_statfs;
216 return statfs->os_bavail * statfs->os_bsize;
219 static inline __u64 tgt_statfs_iavail(struct lu_tgt_desc *tgt)
221 return tgt->ltd_statfs.os_ffree;
225 * Calculate weight for a given tgt.
227 * The final tgt weight is bavail >> 16 * iavail >> 8 minus the tgt and server
228 * penalties. See ltd_qos_penalties_calc() for how penalties are calculated.
230 * \param[in] tgt target descriptor
232 void lu_tgt_qos_weight_calc(struct lu_tgt_desc *tgt)
234 struct lu_tgt_qos *ltq = &tgt->ltd_qos;
237 temp = (tgt_statfs_bavail(tgt) >> 16) * (tgt_statfs_iavail(tgt) >> 8);
238 temp2 = ltq->ltq_penalty + ltq->ltq_svr->lsq_penalty;
242 ltq->ltq_weight = temp - temp2;
244 EXPORT_SYMBOL(lu_tgt_qos_weight_calc);
247 * Allocate and initialize target table.
249 * A helper function to initialize the target table and allocate
250 * a bitmap of the available targets.
252 * \param[in] ltd target's table to initialize
253 * \param[in] is_mdt target table for MDTs
255 * \retval 0 on success
256 * \retval negative negated errno on error
258 int lu_tgt_descs_init(struct lu_tgt_descs *ltd, bool is_mdt)
260 mutex_init(<d->ltd_mutex);
261 init_rwsem(<d->ltd_rw_sem);
264 * the tgt array and bitmap are allocated/grown dynamically as tgts are
265 * added to the LOD/LMV, see lu_tgt_descs_add()
267 ltd->ltd_tgt_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
268 if (!ltd->ltd_tgt_bitmap)
271 ltd->ltd_tgts_size = BITS_PER_LONG;
272 ltd->ltd_death_row = 0;
273 ltd->ltd_refcount = 0;
275 /* Set up allocation policy (QoS and RR) */
276 INIT_LIST_HEAD(<d->ltd_qos.lq_svr_list);
277 init_rwsem(<d->ltd_qos.lq_rw_sem);
278 ltd->ltd_qos.lq_dirty = 1;
279 ltd->ltd_qos.lq_reset = 1;
280 /* Default priority is toward free space balance */
281 ltd->ltd_qos.lq_prio_free = 232;
282 /* Default threshold for rr (roughly 17%) */
283 ltd->ltd_qos.lq_threshold_rr = 43;
284 ltd->ltd_is_mdt = is_mdt;
286 lu_qos_rr_init(<d->ltd_qos.lq_rr);
290 EXPORT_SYMBOL(lu_tgt_descs_init);
293 * Free bitmap and target table pages.
295 * \param[in] ltd target table
297 void lu_tgt_descs_fini(struct lu_tgt_descs *ltd)
301 CFS_FREE_BITMAP(ltd->ltd_tgt_bitmap);
302 for (i = 0; i < TGT_PTRS; i++) {
303 if (ltd->ltd_tgt_idx[i])
304 OBD_FREE_PTR(ltd->ltd_tgt_idx[i]);
306 ltd->ltd_tgts_size = 0;
308 EXPORT_SYMBOL(lu_tgt_descs_fini);
311 * Expand size of target table.
313 * When the target table is full, we have to extend the table. To do so,
314 * we allocate new memory with some reserve, move data from the old table
315 * to the new one and release memory consumed by the old table.
317 * \param[in] ltd target table
318 * \param[in] newsize new size of the table
320 * \retval 0 on success
321 * \retval -ENOMEM if reallocation failed
323 static int lu_tgt_descs_resize(struct lu_tgt_descs *ltd, __u32 newsize)
325 struct cfs_bitmap *new_bitmap, *old_bitmap = NULL;
327 /* someone else has already resize the array */
328 if (newsize <= ltd->ltd_tgts_size)
331 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
335 if (ltd->ltd_tgts_size > 0) {
336 /* the bitmap already exists, copy data from old one */
337 cfs_bitmap_copy(new_bitmap, ltd->ltd_tgt_bitmap);
338 old_bitmap = ltd->ltd_tgt_bitmap;
341 ltd->ltd_tgts_size = newsize;
342 ltd->ltd_tgt_bitmap = new_bitmap;
345 CFS_FREE_BITMAP(old_bitmap);
347 CDEBUG(D_CONFIG, "tgt size: %d\n", ltd->ltd_tgts_size);
353 * Add new target to target table.
355 * Extend target table if it's full, update target table and bitmap.
356 * Notice we need to take ltd_rw_sem exclusively before entry to ensure
359 * \param[in] ltd target table
360 * \param[in] tgt new target desc
362 * \retval 0 on success
363 * \retval -ENOMEM if reallocation failed
364 * -EEXIST if target existed
366 int ltd_add_tgt(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt)
368 __u32 index = tgt->ltd_index;
373 if (index >= ltd->ltd_tgts_size) {
376 while (newsize < index + 1)
377 newsize = newsize << 1;
379 rc = lu_tgt_descs_resize(ltd, newsize);
382 } else if (cfs_bitmap_check(ltd->ltd_tgt_bitmap, index)) {
386 if (ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
387 OBD_ALLOC_PTR(ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK]);
388 if (ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
392 LTD_TGT(ltd, tgt->ltd_index) = tgt;
393 cfs_bitmap_set(ltd->ltd_tgt_bitmap, tgt->ltd_index);
395 ltd->ltd_lov_desc.ld_tgt_count++;
397 ltd->ltd_lov_desc.ld_active_tgt_count++;
401 EXPORT_SYMBOL(ltd_add_tgt);
404 * Delete target from target table
406 void ltd_del_tgt(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt)
408 lu_qos_del_tgt(<d->ltd_qos, tgt);
409 LTD_TGT(ltd, tgt->ltd_index) = NULL;
410 cfs_bitmap_clear(ltd->ltd_tgt_bitmap, tgt->ltd_index);
411 ltd->ltd_lov_desc.ld_tgt_count--;
413 ltd->ltd_lov_desc.ld_active_tgt_count--;
415 EXPORT_SYMBOL(ltd_del_tgt);
418 * Whether QoS data is up-to-date and QoS can be applied.
420 bool ltd_qos_is_usable(struct lu_tgt_descs *ltd)
422 if (!ltd->ltd_qos.lq_dirty && ltd->ltd_qos.lq_same_space)
425 if (ltd->ltd_lov_desc.ld_active_tgt_count < 2)
430 EXPORT_SYMBOL(ltd_qos_is_usable);
433 * Calculate penalties per-tgt and per-server
435 * Re-calculate penalties when the configuration changes, active targets
436 * change and after statfs refresh (all these are reflected by lq_dirty flag).
437 * On every tgt and server: decay the penalty by half for every 8x the update
438 * interval that the device has been idle. That gives lots of time for the
439 * statfs information to be updated (which the penalty is only a proxy for),
440 * and avoids penalizing server/tgt under light load.
441 * See lu_qos_tgt_weight_calc() for how penalties are factored into the weight.
443 * \param[in] ltd lu_tgt_descs
445 * \retval 0 on success
446 * \retval -EAGAIN the number of tgt isn't enough or all tgt spaces are
449 int ltd_qos_penalties_calc(struct lu_tgt_descs *ltd)
451 struct lu_qos *qos = <d->ltd_qos;
452 struct lov_desc *desc = <d->ltd_lov_desc;
453 struct lu_tgt_desc *tgt;
454 struct lu_svr_qos *svr;
455 __u64 ba_max, ba_min, ba;
456 __u64 ia_max, ia_min, ia = 1;
467 num_active = desc->ld_active_tgt_count - 1;
469 GOTO(out, rc = -EAGAIN);
471 /* find bavail on each server */
472 list_for_each_entry(svr, &qos->lq_svr_list, lsq_svr_list) {
474 /* if inode is not counted, set to 1 to ignore */
475 svr->lsq_iavail = ltd->ltd_is_mdt ? 0 : 1;
477 qos->lq_active_svr_count = 0;
480 * How badly user wants to select targets "widely" (not recently chosen
481 * and not on recent MDS's). As opposed to "freely" (free space avail.)
484 prio_wide = 256 - qos->lq_prio_free;
486 ba_min = (__u64)(-1);
488 ia_min = (__u64)(-1);
490 now = ktime_get_real_seconds();
492 /* Calculate server penalty per object */
493 ltd_foreach_tgt(ltd, tgt) {
494 if (!tgt->ltd_active)
497 /* when inode is counted, bavail >> 16 to avoid overflow */
498 ba = tgt_statfs_bavail(tgt);
506 ba_min = min(ba, ba_min);
507 ba_max = max(ba, ba_max);
509 /* Count the number of usable servers */
510 if (tgt->ltd_qos.ltq_svr->lsq_bavail == 0)
511 qos->lq_active_svr_count++;
512 tgt->ltd_qos.ltq_svr->lsq_bavail += ba;
514 if (ltd->ltd_is_mdt) {
515 /* iavail >> 8 to avoid overflow */
516 ia = tgt_statfs_iavail(tgt) >> 8;
520 ia_min = min(ia, ia_min);
521 ia_max = max(ia, ia_max);
523 tgt->ltd_qos.ltq_svr->lsq_iavail += ia;
528 * prio * bavail * iavail / (num_tgt - 1) / 2
530 tgt->ltd_qos.ltq_penalty_per_obj = prio_wide * ba * ia >> 8;
531 do_div(tgt->ltd_qos.ltq_penalty_per_obj, num_active);
532 tgt->ltd_qos.ltq_penalty_per_obj >>= 1;
534 age = (now - tgt->ltd_qos.ltq_used) >> 3;
535 if (qos->lq_reset || age > 32 * desc->ld_qos_maxage)
536 tgt->ltd_qos.ltq_penalty = 0;
537 else if (age > desc->ld_qos_maxage)
538 /* Decay tgt penalty. */
539 tgt->ltd_qos.ltq_penalty >>= age / desc->ld_qos_maxage;
542 num_active = qos->lq_active_svr_count - 1;
543 if (num_active < 1) {
545 * If there's only 1 server, we can't penalize it, so instead
546 * we have to double the tgt penalty
549 ltd_foreach_tgt(ltd, tgt) {
550 if (!tgt->ltd_active)
553 tgt->ltd_qos.ltq_penalty_per_obj <<= 1;
558 * Per-server penalty is
559 * prio * bavail * iavail / server_tgts / (num_svr - 1) / 2
561 list_for_each_entry(svr, &qos->lq_svr_list, lsq_svr_list) {
562 ba = svr->lsq_bavail;
563 ia = svr->lsq_iavail;
564 svr->lsq_penalty_per_obj = prio_wide * ba * ia >> 8;
565 do_div(svr->lsq_penalty_per_obj,
566 svr->lsq_tgt_count * num_active);
567 svr->lsq_penalty_per_obj >>= 1;
569 age = (now - svr->lsq_used) >> 3;
570 if (qos->lq_reset || age > 32 * desc->ld_qos_maxage)
571 svr->lsq_penalty = 0;
572 else if (age > desc->ld_qos_maxage)
573 /* Decay server penalty. */
574 svr->lsq_penalty >>= age / desc->ld_qos_maxage;
581 * If each tgt has almost same free space, do rr allocation for better
582 * creation performance
584 qos->lq_same_space = 0;
585 if ((ba_max * (256 - qos->lq_threshold_rr)) >> 8 < ba_min &&
586 (ia_max * (256 - qos->lq_threshold_rr)) >> 8 < ia_min) {
587 qos->lq_same_space = 1;
588 /* Reset weights for the next time we enter qos mode */
594 if (!rc && qos->lq_same_space)
599 EXPORT_SYMBOL(ltd_qos_penalties_calc);
602 * Re-calculate penalties and weights of all tgts.
604 * The function is called when some target was used for a new object. In
605 * this case we should re-calculate all the weights to keep new allocations
608 * \param[in] ltd lu_tgt_descs
609 * \param[in] tgt recently used tgt
610 * \param[out] total_wt new total weight for the pool
614 int ltd_qos_update(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt,
617 struct lu_qos *qos = <d->ltd_qos;
618 struct lu_tgt_qos *ltq;
619 struct lu_svr_qos *svr;
626 /* Don't allocate on this device anymore, until the next alloc_qos */
632 * Decay old penalty by half (we're adding max penalty, and don't
633 * want it to run away.)
635 ltq->ltq_penalty >>= 1;
636 svr->lsq_penalty >>= 1;
638 /* mark the server and tgt as recently used */
639 ltq->ltq_used = svr->lsq_used = ktime_get_real_seconds();
641 /* Set max penalties for this tgt and server */
642 ltq->ltq_penalty += ltq->ltq_penalty_per_obj *
643 ltd->ltd_lov_desc.ld_active_tgt_count;
644 svr->lsq_penalty += svr->lsq_penalty_per_obj *
645 ltd->ltd_lov_desc.ld_active_tgt_count;
647 /* Decrease all MDS penalties */
648 list_for_each_entry(svr, &qos->lq_svr_list, lsq_svr_list) {
649 if (svr->lsq_penalty < svr->lsq_penalty_per_obj)
650 svr->lsq_penalty = 0;
652 svr->lsq_penalty -= svr->lsq_penalty_per_obj;
656 /* Decrease all tgt penalties */
657 ltd_foreach_tgt(ltd, tgt) {
658 if (!tgt->ltd_active)
662 if (ltq->ltq_penalty < ltq->ltq_penalty_per_obj)
663 ltq->ltq_penalty = 0;
665 ltq->ltq_penalty -= ltq->ltq_penalty_per_obj;
667 lu_tgt_qos_weight_calc(tgt);
669 /* Recalc the total weight of usable osts */
671 *total_wt += ltq->ltq_weight;
673 CDEBUG(D_OTHER, "recalc tgt %d usable=%d bavail=%llu ffree=%llu tgtppo=%llu tgtp=%llu svrppo=%llu svrp=%llu wt=%llu\n",
674 tgt->ltd_index, ltq->ltq_usable,
675 tgt_statfs_bavail(tgt) >> 16,
676 tgt_statfs_iavail(tgt) >> 8,
677 ltq->ltq_penalty_per_obj >> 10,
678 ltq->ltq_penalty >> 10,
679 ltq->ltq_svr->lsq_penalty_per_obj >> 10,
680 ltq->ltq_svr->lsq_penalty >> 10,
681 ltq->ltq_weight >> 10);
686 EXPORT_SYMBOL(ltd_qos_update);