Skip to content
This repository
tree: c4b44759bf
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 365 lines (325 sloc) 10.818 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
/**
* Copyright (c) 2009 Ma Can <ml.macana@gmail.com>
* <macan@ncic.ac.cn>
*
* Armed with EMACS.
* Time-stamp: <2011-08-05 05:25:20 macan>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/

#include "hvfs.h"
#include "mds.h"
#include "xtable.h"
#include "tx.h"
#include "xnet.h"
#include "ring.h"
#include "lib.h"

struct spool_mgr
{
    struct list_head reqin;
    struct list_head modify_req; /* for suspending modify requests */
    struct list_head paused_req; /* for paused pending request */
    xlock_t rin_lock;
    xlock_t pmreq_lock;
    sem_t rin_sem;
};

struct spool_thread_arg
{
    int tid;
};

static struct spool_mgr spool_mgr;

int mds_spool_dispatch(struct xnet_msg *msg)
{
    xlock_lock(&spool_mgr.rin_lock);
    list_add_tail(&msg->list, &spool_mgr.reqin);
    xlock_unlock(&spool_mgr.rin_lock);
    atomic64_inc(&hmo.prof.misc.reqin_total);
    atomic64_inc(&hmo.prof.misc.reqin_qd);
    sem_post(&spool_mgr.rin_sem);

    return 0;
}

void mds_spool_redispatch(struct xnet_msg *msg, int sempost)
{
    xlock_lock(&spool_mgr.rin_lock);
    list_add_tail(&msg->list, &spool_mgr.reqin);
    xlock_unlock(&spool_mgr.rin_lock);
    atomic64_inc(&hmo.prof.misc.reqin_qd);
    if (sempost)
        sem_post(&spool_mgr.rin_sem);
}

int mds_spool_modify_pause(struct xnet_msg *msg)
{
    xlock_lock(&spool_mgr.pmreq_lock);
    list_add_tail(&msg->list, &spool_mgr.modify_req);
    xlock_unlock(&spool_mgr.pmreq_lock);
    atomic64_inc(&hmo.prof.mds.paused_mreq);
    atomic64_inc(&hmo.prof.misc.reqin_qd);
    
    return 0;
}

void mds_spool_provoke(void)
{
    sem_post(&spool_mgr.rin_sem);
}

static inline
void mds_spool_modify_resume(void)
{
    int i;

    for (i = 0; i < hmo.conf.spool_threads; i++) {
        sem_post(&spool_mgr.rin_sem);
    }
}

/* This function should be called BEFORE mp_check, we always check if there
* are too many ITBs in the cache. (Out Of Date)
*/
void mds_spool_itb_check(time_t t)
{
    static int memory_pressure = 0;
    
    if (!(hmo.conf.option & HVFS_MDS_MEMLIMIT))
        return;
    if (hmo.conf.memlimit < atomic64_read(&hmo.prof.cbht.aitb) *
        (sizeof(struct itb) + sizeof(struct ite) * ITB_SIZE)) {
        /* we want to evict some clean ITBs */
        if (++memory_pressure == hmo.conf.loadin_pressure) {
            if (!TXG_IS_DIRTY(hmo.txg[TXG_OPEN]))
                goto skip;
            if (hmo.txg[TXG_WB] != NULL)
                goto skip;
            if (!txg_switch(&hmi, &hmo)) {
                hvfs_info(mds, "Entering new txg %ld (loadin forced)\n",
                          hmo.txg[TXG_OPEN]->txg);
                sem_post(&hmo.commit_sem);
            }
        skip:
            memory_pressure = 0;
        }
        mds_scrub_trigger();
    }
}

void mds_spool_mp_check(time_t t)
{
    if (!hmo.spool_modify_pause) {
        /* check if we are under memory pressure now */
        mds_spool_itb_check(t);
        return;
    }
    if (hmo.scrub_running)
        return;
    
    /* check to see if we should resume the modify requests' handling */
    if (hmo.conf.memlimit > atomic64_read(&hmo.prof.cbht.aitb) *
        (sizeof(struct itb) + sizeof(struct ite) * ITB_SIZE)) {
        /* ok, we disable the scrub thread now */
        hmo.conf.option |= HVFS_MDS_NOSCRUB;
        hmo.conf.scrub_interval = 600;
        mds_reset_itimer();

        hmo.spool_modify_pause = 0;
        mds_spool_modify_resume();

        hvfs_warning(mds, "Resume modify operations\n");
    } else {
        /* do commit? */
        {
            if (!TXG_IS_DIRTY(hmo.txg[TXG_OPEN]))
                goto skip;
            if (hmo.txg[TXG_WB] != NULL)
                goto skip;
            if (!txg_switch(&hmi, &hmo)) {
                hvfs_info(mds, "Entering new txg %ld (mp forced)\n",
                          hmo.txg[TXG_OPEN]->txg);
                sem_post(&hmo.commit_sem);
            }
        skip:;
        }
        
        /* we trigger the scrubbing thread to evict the clean ITBs */
        hmo.conf.option &= ~HVFS_MDS_NOSCRUB;
        if (hmo.conf.scrub_interval > 1) {
            hmo.conf.scrub_interval = 1;
            if (hmo.conf.mpcheck_sensitive &&
                hmo.conf.mpcheck_sensitive <= MPCHECK_SENSITIVE_MAX)
                mds_reset_itimer_us(SECOND_IN_US >>
                                    hmo.conf.mpcheck_sensitive);
            else
                mds_reset_itimer();
        }
        hvfs_debug(mds, "trigger scrub thread\n");
        mds_scrub_trigger();

        /* kick progressing in P=0.25 */
        if (lib_random(4) == 0) {
            hmo.spool_modify_pause = 0;
            mds_spool_modify_resume();
        }
        
        if (unlikely(t > hmo.mp_ts + hmo.conf.mp_to)) {
            hvfs_err(mds, "MDS pausing modification for a long time: %lds\n",
                     (u64)(t - hmo.mp_ts));
            hmo.scrub_op = HVFS_MDS_OP_EVICT_ALL;
            mds_scrub_trigger();
            hmo.scrub_op = HVFS_MDS_OP_EVICT;
        }
    }
}

static inline
int __serv_request(void)
{
    struct xnet_msg *msg = NULL, *pos, *n;

    if (likely(!hmo.spool_modify_pause)) {
        if (unlikely(!list_empty(&spool_mgr.modify_req))) {
            xlock_lock(&spool_mgr.pmreq_lock);
            list_for_each_entry_safe(pos, n, &spool_mgr.modify_req, list) {
                list_del_init(&pos->list);
                msg = pos;
                break;
            }
            xlock_unlock(&spool_mgr.pmreq_lock);
            if (msg) {
                atomic64_dec(&hmo.prof.misc.reqin_qd);
                return msg->xc->ops.dispatcher(msg);
            }
        }
    }
    if (likely(!hmo.reqin_pause)) {
        if (unlikely(!list_empty(&spool_mgr.paused_req))) {
            xlock_lock(&spool_mgr.rin_lock);
            list_splice_init(&spool_mgr.paused_req, &spool_mgr.reqin);
            xlock_unlock(&spool_mgr.rin_lock);
        }
    }
    
    xlock_lock(&spool_mgr.rin_lock);
    list_for_each_entry_safe(pos, n, &spool_mgr.reqin, list) {
        list_del_init(&pos->list);
        msg = pos;
        break;
    }
    xlock_unlock(&spool_mgr.rin_lock);

    if (!msg)
        return -EHSTOP;

    /* ok, deal with it, we just calling the secondary dispatcher */
    /* NOTE: important!
*
* reqin_pause will aggr many incoming request, if we have 256 or more
* requests pending, we begin dropping request. Thus, it is important to
* place hmo.reqin_drop branch BEFORE hmo.reqin_pause!
*/
    if (likely(!(hmo.reqin_drop | hmo.reqin_pause))) {
    dispatch:
        atomic64_dec(&hmo.prof.misc.reqin_qd);
        atomic64_inc(&hmo.prof.misc.reqin_handle);
        return msg->xc->ops.dispatcher(msg);
    } else if (hmo.reqin_drop) {
        if (HVFS_IS_CLIENT(msg->tx.ssite_id) ||
            HVFS_IS_AMC(msg->tx.ssite_id)) {
            atomic64_inc(&hmo.prof.misc.reqin_drop);
            xnet_free_msg(msg);
        } else {
            goto dispatch;
        }
    } else if (hmo.reqin_pause) {
        /* we should iterate on reqin list to drain R2 messages! */
        if (HVFS_IS_CLIENT(msg->tx.ssite_id) ||
            HVFS_IS_AMC(msg->tx.ssite_id)) {
            if (atomic64_read(&hmo.prof.misc.reqin_qd) > 256) {
                hmo.reqin_drop = 1;
            }
            /* re-insert this request to paused req list */
            xlock_lock(&spool_mgr.rin_lock);
            list_add_tail(&msg->list, &spool_mgr.paused_req);
            xlock_unlock(&spool_mgr.rin_lock);
        } else {
            goto dispatch;
        }
    }

    return 0;
}

static
void *spool_main(void *arg)
{
    struct spool_thread_arg *sta = (struct spool_thread_arg *)arg;
    sigset_t set;
    int err = 0;

    /* first, let us block the SIGALRM and SIGCHLD */
    sigemptyset(&set);
    sigaddset(&set, SIGALRM);
    sigaddset(&set, SIGCHLD);
    pthread_sigmask(SIG_BLOCK, &set, NULL); /* oh, we do not care about the
* errs */
    
    while (!hmo.spool_thread_stop) {
        err = sem_wait(&spool_mgr.rin_sem);
        if (err == EINTR)
            continue;
        hvfs_debug(mds, "Service thread %d wakeup to handle the requests.\n",
                   sta->tid);
        /* trying to handle more and more requsts. */
        while (1) {
            err = __serv_request();
            if (err == -EHSTOP)
                break;
            else if (err) {
                hvfs_err(mds, "Service thread handle request w/ error %d\n",
                         err);
                break;
            }
        }
    }
    pthread_exit(0);
}

int mds_spool_create(void)
{
    struct spool_thread_arg *sta;
    int i, err = 0;
    
    /* init the mgr struct */
    INIT_LIST_HEAD(&spool_mgr.reqin);
    INIT_LIST_HEAD(&spool_mgr.modify_req);
    INIT_LIST_HEAD(&spool_mgr.paused_req);
    xlock_init(&spool_mgr.rin_lock);
    xlock_init(&spool_mgr.pmreq_lock);
    sem_init(&spool_mgr.rin_sem, 0, 0);
    hmo.spool_modify_pause = 0;

    /* init service threads' pool */
    if (!hmo.conf.spool_threads)
        hmo.conf.spool_threads = 4;

    hmo.spool_thread = xzalloc(hmo.conf.spool_threads * sizeof(pthread_t));
    if (!hmo.spool_thread) {
        hvfs_err(mds, "xzalloc() pthread_t failed\n");
        return -ENOMEM;
    }

    sta = xzalloc(hmo.conf.spool_threads * sizeof(struct spool_thread_arg));
    if (!sta) {
        hvfs_err(mds, "xzalloc() struct spool_thread_arg failed\n");
        err = -ENOMEM;
        goto out_free;
    }

    for (i = 0; i < hmo.conf.spool_threads; i++) {
        (sta + i)->tid = i;
        err = pthread_create(hmo.spool_thread + i, NULL, &spool_main,
                             sta + i);
        if (err)
            goto out;
    }

out:
    return err;
out_free:
    xfree(hmo.spool_thread);
    goto out;
}

void mds_spool_destroy(void)
{
    int i;

    hmo.spool_thread_stop = 1;
    for (i = 0; i < hmo.conf.spool_threads; i++) {
        sem_post(&spool_mgr.rin_sem);
    }
    for (i = 0; i < hmo.conf.spool_threads; i++) {
        pthread_join(*(hmo.spool_thread + i), NULL);
    }
    sem_destroy(&spool_mgr.rin_sem);
}
Something went wrong with that request. Please try again.