44edcfe992c0aca430cb362f774af45fdfa58203
[dragonfly.git] / sys / dsched / fq / dsched_fq_diskops.c
1 /*
2  * Copyright (c) 2009, 2010 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Alex Hornung <ahornung@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/sysctl.h>
39 #include <sys/buf.h>
40 #include <sys/conf.h>
41 #include <sys/diskslice.h>
42 #include <sys/disk.h>
43 #include <machine/atomic.h>
44 #include <sys/malloc.h>
45 #include <sys/thread.h>
46 #include <sys/thread2.h>
47 #include <sys/sysctl.h>
48 #include <sys/spinlock2.h>
49 #include <machine/md_var.h>
50 #include <sys/ctype.h>
51 #include <sys/syslog.h>
52 #include <sys/device.h>
53 #include <sys/msgport.h>
54 #include <sys/msgport2.h>
55 #include <sys/buf2.h>
56 #include <sys/dsched.h>
57 #include <machine/varargs.h>
58 #include <machine/param.h>
59
60 #include <dsched/fq/dsched_fq.h>
61
62
63 MALLOC_DEFINE(M_DSCHEDFQ, "dschedfq", "fq dsched allocs");
64
65 static dsched_prepare_t         fq_prepare;
66 static dsched_teardown_t        fq_teardown;
67 static dsched_flush_t           fq_flush;
68 static dsched_cancel_t          fq_cancel;
69 static dsched_queue_t           fq_queue;
70
71 /* These are in _procops */
72 dsched_new_buf_t        fq_new_buf;
73 dsched_new_proc_t       fq_new_proc;
74 dsched_new_thread_t     fq_new_thread;
75 dsched_exit_buf_t       fq_exit_buf;
76 dsched_exit_proc_t      fq_exit_proc;
77 dsched_exit_thread_t    fq_exit_thread;
78
79 extern struct dsched_fq_stats   fq_stats;
80 extern struct spinlock  fq_fqmp_lock;
81 extern TAILQ_HEAD(, dsched_fq_mpriv)    dsched_fqmp_list;
82 extern struct callout   fq_callout;
83
84 struct dsched_ops dsched_fq_ops = {
85         .head = {
86                 .name = "fq"
87         },
88         .prepare = fq_prepare,
89         .teardown = fq_teardown,
90         .flush = fq_flush,
91         .cancel_all = fq_cancel,
92         .bio_queue = fq_queue,
93
94         .new_buf = fq_new_buf,
95         .new_proc = fq_new_proc,
96         .new_thread = fq_new_thread,
97         .exit_buf = fq_exit_buf,
98         .exit_proc = fq_exit_proc,
99         .exit_thread = fq_exit_thread,
100 };
101
102
103
104 static int
105 fq_prepare(struct disk *dp)
106 {
107         struct  dsched_fq_dpriv *dpriv;
108         struct dsched_fq_mpriv  *fqmp;
109         struct dsched_fq_priv   *fqp;
110         struct thread *td_core;
111
112         dpriv = fq_alloc_dpriv(dp);
113         fq_reference_dpriv(dpriv);
114         dsched_set_disk_priv(dp, dpriv);
115
116         FQ_GLOBAL_FQMP_LOCK();
117         TAILQ_FOREACH(fqmp, &dsched_fqmp_list, link) {
118                 fqp = fq_alloc_priv(dp);
119
120                 FQ_FQMP_LOCK(fqmp);
121 #if 0
122                 fq_reference_priv(fqp);
123 #endif
124                 TAILQ_INSERT_TAIL(&fqmp->fq_priv_list, fqp, link);
125                 FQ_FQMP_UNLOCK(fqmp);
126         }
127
128         FQ_GLOBAL_FQMP_UNLOCK();
129         lwkt_create((void (*)(void *))fq_dispatcher, dpriv, &td_core, NULL,
130             0, 0, "fq_dispatcher_%s", dp->d_cdev->si_name);
131         fq_balance_thread(dpriv);
132
133         return 0;
134 }
135
136
137
138 static void
139 fq_teardown(struct disk *dp)
140 {
141         struct dsched_fq_dpriv *dpriv;
142
143         dpriv = dsched_get_disk_priv(dp);
144         KKASSERT(dpriv != NULL);
145
146         /* Basically kill the dispatcher thread */
147         callout_stop(&fq_callout);
148         dpriv->die = 1;
149         wakeup(dpriv);
150         tsleep(dpriv, 0, "fq_dispatcher", hz/5); /* wait 200 ms */
151         callout_stop(&fq_callout);
152         wakeup(dpriv);
153         tsleep(dpriv, 0, "fq_dispatcher", hz/5); /* wait 200 ms */
154         callout_stop(&fq_callout);
155         /* XXX: we really need callout_drain, this REALLY sucks */
156
157
158         fq_dereference_dpriv(dpriv); /* from prepare */
159         fq_dereference_dpriv(dpriv); /* from alloc */
160
161         dsched_set_disk_priv(dp, NULL);
162         /* XXX: get rid of dpriv, cancel all queued requests...
163          *      but how do we get rid of all loose fqps?
164          *    --> possibly same solution as devfs; tracking a list of
165          *        orphans.
166          *    but for now we don't care much about this yet
167          */
168 }
169
170
171 static void
172 fq_flush(struct disk *dp, struct bio *biin)
173 {
174         /* Our flushes are handled by queue, this should never be called */
175         return;
176 }
177
178
179 static void
180 fq_cancel(struct disk *dp)
181 {
182         struct dsched_fq_dpriv  *dpriv;
183         struct dsched_fq_priv   *fqp, *fqp2;
184         struct bio *bio, *bio2;
185
186         dpriv = dsched_get_disk_priv(dp);
187         KKASSERT(dpriv != NULL);
188
189         /*
190          * all bios not in flight are queued in their respective fqps.
191          * good thing we have a list of fqps per disk dpriv.
192          */
193         FQ_DPRIV_LOCK(dpriv);
194         TAILQ_FOREACH_MUTABLE(fqp, &dpriv->fq_priv_list, dlink, fqp2) {
195                 if (fqp->qlength > 0) {
196                         FQ_FQP_LOCK(fqp);
197                         TAILQ_FOREACH_MUTABLE(bio, &fqp->queue, link, bio2) {
198                                 TAILQ_REMOVE(&fqp->queue, bio, link);
199                                 --fqp->qlength;
200                                 dsched_cancel_bio(bio);
201                                 atomic_add_int(&fq_stats.cancelled, 1);
202                         }
203                         FQ_FQP_UNLOCK(fqp);
204                 }
205         }
206         FQ_DPRIV_UNLOCK(dpriv);
207 }
208
209
210 static int
211 fq_queue(struct disk *dp, struct bio *obio)
212 {
213         struct bio *bio, *bio2;
214         struct dsched_fq_mpriv  *fqmp;
215         struct dsched_fq_priv   *fqp;
216         struct dsched_fq_dpriv  *dpriv;
217         int found = 0;
218         int count;
219         int max_tp, transactions;
220
221         /* get fqmp and fqp */
222         fqmp = dsched_get_buf_priv(obio->bio_buf);
223
224         /*
225          * XXX: hack. we don't want the assert because some null-fqmps are
226          * leaking through; just dispatch them. These come from the
227          * mi_startup() mess, which does the initial root mount.
228          */
229 #if 0
230         KKASSERT(fqmp != NULL);
231 #endif
232         if (fqmp == NULL) {
233                 atomic_add_int(&fq_stats.no_fqmp, 1);
234                 dsched_strategy_raw(dp, obio);
235                 return 0;
236         }
237
238
239         FQ_FQMP_LOCK(fqmp);
240 #if 0
241         kprintf("fq_queue, fqmp = %p\n", fqmp);
242 #endif
243         KKASSERT(!TAILQ_EMPTY(&fqmp->fq_priv_list));
244         TAILQ_FOREACH(fqp, &fqmp->fq_priv_list, link) {
245                 if (fqp->dp == dp) {
246                         fq_reference_priv(fqp);
247                         found = 1;
248                         break;
249                 }
250         }
251         FQ_FQMP_UNLOCK(fqmp);
252         dsched_clr_buf_priv(obio->bio_buf);
253         fq_dereference_mpriv(fqmp); /* acquired on new_buf */
254         atomic_subtract_int(&fq_stats.nbufs, 1);
255
256         KKASSERT(found == 1);
257         dpriv = dsched_get_disk_priv(dp);
258
259         /* XXX: probably rather pointless doing this atomically */
260         max_tp = atomic_fetchadd_int(&fqp->max_tp, 0);
261 #if 0
262         transactions = atomic_fetchadd_int(&fqp->transactions, 0);
263 #endif
264         transactions = atomic_fetchadd_int(&fqp->issued, 0);
265
266         /* | No rate limiting || Hasn't reached limit rate |     */
267         if ((max_tp == 0) || (transactions < max_tp)) {
268                 /*
269                  * Process pending bios from previous _queue() actions that
270                  * have been rate-limited and hence queued in the fqp.
271                  */
272                 KKASSERT(fqp->qlength >= 0);
273
274                 if (fqp->qlength > 0) {
275                         FQ_FQP_LOCK(fqp);
276                         count = 0;
277
278                         TAILQ_FOREACH_MUTABLE(bio, &fqp->queue, link, bio2) {
279                                 if ((fqp->max_tp > 0) && (fqp->issued >= fqp->max_tp))
280                                         break;
281                                 TAILQ_REMOVE(&fqp->queue, bio, link);
282                                 --fqp->qlength;
283
284                                 /*
285                                  * beware that we do have an fqp reference from the
286                                  * queueing
287                                  */
288                                 fq_dispatch(dpriv, bio, fqp);
289                         }
290                         FQ_FQP_UNLOCK(fqp);
291                 }
292
293                 /* Nothing is pending from previous IO, so just pass it down */
294                 fq_reference_priv(fqp);
295
296                 fq_dispatch(dpriv, obio, fqp);
297         } else {
298                 /*
299                  * This thread has exceeeded its fair share,
300                  * the transactions are now rate limited. At
301                  * this point, the rate would be exceeded, so
302                  * we just queue requests instead of
303                  * despatching them.
304                  */
305                 FQ_FQP_LOCK(fqp);
306                 fq_reference_priv(fqp);
307
308                 /*
309                  * Prioritize reads by inserting them at the front of the
310                  * queue.
311                  *
312                  * XXX: this might cause issues with data that should
313                  *      have been written and is being read, but hasn't
314                  *      actually been written yet.
315                  */
316                 if (obio->bio_buf->b_cmd == BUF_CMD_READ)
317                         TAILQ_INSERT_HEAD(&fqp->queue, obio, link);
318                 else
319                         TAILQ_INSERT_TAIL(&fqp->queue, obio, link);
320
321                 ++fqp->qlength;
322                 FQ_FQP_UNLOCK(fqp);
323         }
324
325         fq_dereference_priv(fqp);
326         return 0;
327 }
328
329
330 void
331 fq_completed(struct bio *bp)
332 {
333         struct bio *obio;
334         int     delta;
335         struct dsched_fq_priv   *fqp;
336         struct dsched_fq_dpriv  *dpriv;
337         struct disk     *dp;
338         int transactions, latency;
339
340         struct timeval tv;
341
342         getmicrotime(&tv);
343
344         dp = dsched_get_bio_dp(bp);
345         dpriv = dsched_get_disk_priv(dp);
346         fqp = dsched_get_bio_priv(bp);
347         KKASSERT(fqp != NULL);
348         KKASSERT(dpriv != NULL);
349
350         fq_reference_dpriv(dpriv);
351
352         if (!(bp->bio_buf->b_flags & B_ERROR)) {
353                 /*
354                  * Get the start ticks from when the bio was dispatched and calculate
355                  * how long it took until completion.
356                  */
357                 delta = (int)(1000000*((tv.tv_sec - bp->bio_caller_info3.tv.tv_sec)) +
358                     (tv.tv_usec - bp->bio_caller_info3.tv.tv_usec));
359                 if (delta <= 0)
360                         delta = 10000; /* default assume 10 ms */
361
362                 /* This is the last in-flight request and the disk is not idle yet */
363                 if ((dpriv->incomplete_tp <= 1) && (!dpriv->idle)) {
364                         dpriv->idle = 1;        /* Mark disk as idle */
365                         dpriv->start_idle = tv; /* Save start idle time */
366                         wakeup(dpriv);          /* Wake up fq_dispatcher */
367                 }
368                 atomic_subtract_int(&dpriv->incomplete_tp, 1);
369                 transactions = atomic_fetchadd_int(&fqp->transactions, 1);
370                 latency = atomic_fetchadd_int(&fqp->avg_latency, 0);
371
372                 if (latency != 0) {
373                         /* Moving averager, ((n-1)*avg_{n-1} + x) / n */
374                         latency = ((transactions) *
375                             latency + delta) / (transactions + 1);
376                 } else {
377                         latency = delta;
378                 }
379
380                 fqp->avg_latency = latency;
381                 atomic_add_int(&fq_stats.transactions_completed, 1);
382         }
383
384         fq_dereference_dpriv(dpriv);
385         /* decrease the ref count that was bumped for us on dispatch */
386         fq_dereference_priv(fqp);
387
388         obio = pop_bio(bp);
389         biodone(obio);
390 }
391
392 void
393 fq_dispatch(struct dsched_fq_dpriv *dpriv, struct bio *bio,
394     struct dsched_fq_priv *fqp)
395 {
396         struct timeval tv;
397
398         if (dpriv->idle) {
399                 getmicrotime(&tv);              
400                 atomic_add_int(&dpriv->idle_time,
401                     (int)(1000000*((tv.tv_sec - dpriv->start_idle.tv_sec)) +
402                     (tv.tv_usec - dpriv->start_idle.tv_usec)));
403                 dpriv->idle = 0;
404         }
405         dsched_strategy_async(dpriv->dp, bio, fq_completed, fqp);
406
407         atomic_add_int(&fqp->issued, 1);
408         atomic_add_int(&dpriv->incomplete_tp, 1);
409         atomic_add_int(&fq_stats.transactions, 1);
410 }