75c9f0da9f05b8926e1b09c80e6003528cfc3d7e
[dragonfly.git] / sys / kern / dsched / fq / fq.h
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 #ifndef _DSCHED_FQ_H_
35 #define _DSCHED_FQ_H_
36
37 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
38
39 #ifndef _SYS_QUEUE_H_
40 #include <sys/queue.h>
41 #endif
42 #ifndef _SYS_BIO_H_
43 #include <sys/bio.h>
44 #endif
45 #ifndef _SYS_BIOTRACK_H_
46 #include <sys/biotrack.h>
47 #endif
48 #ifndef _SYS_SPINLOCK_H_
49 #include <sys/spinlock.h>
50 #endif
51
52 #define FQ_PRIO_BIAS            5
53 #define FQ_PRIO_MAX             10
54 #define FQ_PRIO_MIN             1
55 #define FQ_PRIO_IDLE            -1
56 #define FQ_BUCKET_ACTIVE        0x01
57
58 #define FQ_DISPATCH_SML_ARRAY_SZ        128
59 #define FQ_DISPATCH_ARRAY_SZ    512
60
61 #define FQ_DRAIN_CANCEL 0x1
62 #define FQ_DRAIN_FLUSH  0x2
63
64 struct disk;
65 struct proc;
66
67 struct fq_disk_ctx {
68         struct dsched_disk_ctx head;
69
70         struct thread   *td;            /* dispatcher thread td */
71         struct thread   *td_balance;    /* balancer thread td */
72         int     avg_rq_time;            /* XXX: not yet used */
73         int32_t incomplete_tp;          /* IOs issued but not completed */
74         int     idle;                   /* disk idle ? */
75         struct timeval start_idle;      /* disk idleness start time */
76         int     idle_time;              /* aggregate idle time in interval */
77         int     die;                    /* flag to kill related threads */
78         struct timeval start_interval;  /* current interval start time */
79
80         int     prev_full;              /* disk >90% busy during prev. to last
81                                            interval? */
82         int     last_full;              /* disk >90% busy during last interval */
83         int     disk_busy;              /* disk >90% busy during cur. interval */
84         int64_t budgetpb[FQ_PRIO_MAX+1];/* next interval budget for each thread
85                                            in each prio */
86 };
87
88 struct fq_thread_io {
89         struct dsched_thread_io head;
90
91         int32_t transactions;   /* IOs completed so far during current interval */
92         int32_t avg_latency;    /* avg latency for current interval IOs */
93         int32_t interval_transactions;  /* IOs completed during last interval */
94         int32_t interval_avg_latency;   /* avg latency for last interval IOs */
95         int32_t max_tp;         /* rate limit of transactions per interval */
96         int32_t issued;         /* IOs issued to disk (but not completed) */
97
98         int     rebalance;      /* thread needs to rebalance w/ fq_balance_self */
99 };
100
101 struct dispatch_prep {
102         struct fq_thread_io     *tdio;
103         struct bio              *bio;
104 };
105
106
107 void    fq_balance_thread(struct fq_disk_ctx *diskctx);
108 void    fq_dispatcher(struct fq_disk_ctx *diskctx);
109 biodone_t       fq_completed;
110
111 void    fq_dispatch(struct fq_disk_ctx *diskctx, struct bio *bio,
112                         struct fq_thread_io *tdio);
113 void    fq_drain(struct fq_disk_ctx *diskctx, int mode);
114 void    fq_balance_self(struct fq_thread_io *tdio);
115 #endif /* _KERNEL || _KERNEL_STRUCTURES */
116
117
118 struct dsched_fq_stats {
119         int32_t procs_limited;
120
121         int32_t transactions;
122         int32_t transactions_completed;
123         int32_t cancelled;
124 };
125
126 #endif /* _DSCHED_FQ_H_ */