-/**
- * Calculate per-OST and per-OSS penalties
- *
- * Re-calculate penalties when the configuration changes, active targets
- * change and after statfs refresh (all these are reflected by lq_dirty flag).
- * On every OST and OSS: decay the penalty by half for every 8x the update
- * interval that the device has been idle. That gives lots of time for the
- * statfs information to be updated (which the penalty is only a proxy for),
- * and avoids penalizing OSS/OSTs under light load.
- * See lod_qos_calc_weight() for how penalties are factored into the weight.
- *
- * \param[in] lod LOD device
- *
- * \retval 0 on success
- * \retval -EAGAIN the number of OSTs isn't enough
- */
-static int lod_qos_calc_ppo(struct lod_device *lod)
-{
- struct lod_qos_oss *oss;
- __u64 ba_max, ba_min, temp;
- __u32 num_active;
- unsigned int i;
- int rc, prio_wide;
- time_t now, age;
- ENTRY;
-
- if (!lod->lod_qos.lq_dirty)
- GOTO(out, rc = 0);
-
- num_active = lod->lod_desc.ld_active_tgt_count - 1;
- if (num_active < 1)
- GOTO(out, rc = -EAGAIN);
-
- /* find bavail on each OSS */
- list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list)
- oss->lqo_bavail = 0;
- lod->lod_qos.lq_active_oss_count = 0;
-
- /*
- * How badly user wants to select OSTs "widely" (not recently chosen
- * and not on recent OSS's). As opposed to "freely" (free space
- * avail.) 0-256
- */
- prio_wide = 256 - lod->lod_qos.lq_prio_free;
-
- ba_min = (__u64)(-1);
- ba_max = 0;
- now = cfs_time_current_sec();
- /* Calculate OST penalty per object
- * (lod ref taken in lod_qos_prep_create()) */
- cfs_foreach_bit(lod->lod_ost_bitmap, i) {
- LASSERT(OST_TGT(lod,i));
- temp = TGT_BAVAIL(i);
- if (!temp)
- continue;
- ba_min = min(temp, ba_min);
- ba_max = max(temp, ba_max);
-
- /* Count the number of usable OSS's */
- if (OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail == 0)
- lod->lod_qos.lq_active_oss_count++;
- OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail += temp;
-
- /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
- temp >>= 1;
- do_div(temp, num_active);
- OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj =
- (temp * prio_wide) >> 8;
-
- age = (now - OST_TGT(lod,i)->ltd_qos.ltq_used) >> 3;
- if (lod->lod_qos.lq_reset ||
- age > 32 * lod->lod_desc.ld_qos_maxage)
- OST_TGT(lod,i)->ltd_qos.ltq_penalty = 0;
- else if (age > lod->lod_desc.ld_qos_maxage)
- /* Decay OST penalty. */
- OST_TGT(lod,i)->ltd_qos.ltq_penalty >>=
- (age / lod->lod_desc.ld_qos_maxage);
- }
-
- num_active = lod->lod_qos.lq_active_oss_count - 1;
- if (num_active < 1) {
- /* If there's only 1 OSS, we can't penalize it, so instead
- we have to double the OST penalty */
- num_active = 1;
- cfs_foreach_bit(lod->lod_ost_bitmap, i)
- OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj <<= 1;
- }
-
- /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
- list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
- temp = oss->lqo_bavail >> 1;
- do_div(temp, oss->lqo_ost_count * num_active);
- oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
-
- age = (now - oss->lqo_used) >> 3;
- if (lod->lod_qos.lq_reset ||
- age > 32 * lod->lod_desc.ld_qos_maxage)
- oss->lqo_penalty = 0;
- else if (age > lod->lod_desc.ld_qos_maxage)
- /* Decay OSS penalty. */
- oss->lqo_penalty >>= age / lod->lod_desc.ld_qos_maxage;
- }
-
- lod->lod_qos.lq_dirty = 0;
- lod->lod_qos.lq_reset = 0;
-
- /* If each ost has almost same free space,
- * do rr allocation for better creation performance */
- lod->lod_qos.lq_same_space = 0;
- if ((ba_max * (256 - lod->lod_qos.lq_threshold_rr)) >> 8 < ba_min) {
- lod->lod_qos.lq_same_space = 1;
- /* Reset weights for the next time we enter qos mode */
- lod->lod_qos.lq_reset = 1;
- }
- rc = 0;
-
-out:
-#ifndef FORCE_QOS
- if (!rc && lod->lod_qos.lq_same_space)
- RETURN(-EAGAIN);
-#endif
- RETURN(rc);
-}
-
-/**
- * Calculate weight for a given OST target.
- *
- * The final OST weight is the number of bytes available minus the OST and
- * OSS penalties. See lod_qos_calc_ppo() for how penalties are calculated.
- *
- * \param[in] lod LOD device, where OST targets are listed
- * \param[in] i OST target index
- *
- * \retval 0
- */
-static int lod_qos_calc_weight(struct lod_device *lod, int i)
-{
- __u64 temp, temp2;
-
- temp = TGT_BAVAIL(i);
- temp2 = OST_TGT(lod,i)->ltd_qos.ltq_penalty +
- OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_penalty;
- if (temp < temp2)
- OST_TGT(lod,i)->ltd_qos.ltq_weight = 0;
- else
- OST_TGT(lod,i)->ltd_qos.ltq_weight = temp - temp2;
- return 0;
-}
-
-/**
- * Re-calculate weights.
- *
- * The function is called when some OST target was used for a new object. In
- * this case we should re-calculate all the weights to keep new allocations
- * balanced well.
- *
- * \param[in] lod LOD device
- * \param[in] osts OST pool where a new object was placed
- * \param[in] index OST target where a new object was placed
- * \param[out] total_wt new total weight for the pool
- *
- * \retval 0
- */
-static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
- __u32 index, __u64 *total_wt)
-{
- struct lod_tgt_desc *ost;
- struct lod_qos_oss *oss;
- unsigned int j;
- ENTRY;
-
- ost = OST_TGT(lod,index);
- LASSERT(ost);
-
- /* Don't allocate on this devuce anymore, until the next alloc_qos */
- ost->ltd_qos.ltq_usable = 0;
-
- oss = ost->ltd_qos.ltq_oss;
-
- /* Decay old penalty by half (we're adding max penalty, and don't
- want it to run away.) */
- ost->ltd_qos.ltq_penalty >>= 1;
- oss->lqo_penalty >>= 1;
-
- /* mark the OSS and OST as recently used */
- ost->ltd_qos.ltq_used = oss->lqo_used = cfs_time_current_sec();
-
- /* Set max penalties for this OST and OSS */
- ost->ltd_qos.ltq_penalty +=
- ost->ltd_qos.ltq_penalty_per_obj * lod->lod_ostnr;
- oss->lqo_penalty += oss->lqo_penalty_per_obj *
- lod->lod_qos.lq_active_oss_count;
-
- /* Decrease all OSS penalties */
- list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
- if (oss->lqo_penalty < oss->lqo_penalty_per_obj)
- oss->lqo_penalty = 0;
- else
- oss->lqo_penalty -= oss->lqo_penalty_per_obj;
- }
-
- *total_wt = 0;
- /* Decrease all OST penalties */
- for (j = 0; j < osts->op_count; j++) {
- int i;
-
- i = osts->op_array[j];
- if (!cfs_bitmap_check(lod->lod_ost_bitmap, i))
- continue;
-
- ost = OST_TGT(lod,i);
- LASSERT(ost);
-
- if (ost->ltd_qos.ltq_penalty <
- ost->ltd_qos.ltq_penalty_per_obj)
- ost->ltd_qos.ltq_penalty = 0;
- else
- ost->ltd_qos.ltq_penalty -=
- ost->ltd_qos.ltq_penalty_per_obj;
-
- lod_qos_calc_weight(lod, i);
-
- /* Recalc the total weight of usable osts */
- if (ost->ltd_qos.ltq_usable)
- *total_wt += ost->ltd_qos.ltq_weight;
-
- QOS_DEBUG("recalc tgt %d usable=%d avail="LPU64
- " ostppo="LPU64" ostp="LPU64" ossppo="LPU64
- " ossp="LPU64" wt="LPU64"\n",
- i, ost->ltd_qos.ltq_usable, TGT_BAVAIL(i) >> 10,
- ost->ltd_qos.ltq_penalty_per_obj >> 10,
- ost->ltd_qos.ltq_penalty >> 10,
- ost->ltd_qos.ltq_oss->lqo_penalty_per_obj >> 10,
- ost->ltd_qos.ltq_oss->lqo_penalty >> 10,
- ost->ltd_qos.ltq_weight >> 10);
- }
-
- RETURN(0);
-}
-
-void lod_qos_rr_init(struct lod_qos_rr *lqr)
-{
- spin_lock_init(&lqr->lqr_alloc);
- lqr->lqr_dirty = 1;
-}
-
-