dsched_fq - Refactor and clean; handle flushes
[games.git] / sys / dsched / fq / dsched_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_IOQ_INIT(x)          lockinit(&(x)->fq_lock, "fqioq", 0, LK_CANRECURSE)
53 #define FQ_IOQ_LOCK(x)          lockmgr(&(x)->fq_lock, LK_EXCLUSIVE)
54 #define FQ_IOQ_UNLOCK(x)        lockmgr(&(x)->fq_lock, LK_RELEASE)
55 */
56
57 #define FQ_FQMP_LOCKINIT(x)     spin_init(&(x)->lock)
58 #define FQ_FQP_LOCKINIT(x)      spin_init(&(x)->lock)
59 #define FQ_DPRIV_LOCKINIT(x)    spin_init(&(x)->lock)
60 #define FQ_GLOBAL_FQMP_LOCKINIT(x)      spin_init(&fq_fqmp_lock)
61
62
63 #define FQ_FQMP_LOCK(x)         fq_reference_mpriv((x)); \
64                                 spin_lock_wr(&(x)->lock)
65
66 #define FQ_FQP_LOCK(x)          fq_reference_priv((x)); \
67                                 spin_lock_wr(&(x)->lock)
68
69 #define FQ_DPRIV_LOCK(x)        fq_reference_dpriv((x)); \
70                                 spin_lock_wr(&(x)->lock)
71
72 #define FQ_GLOBAL_FQMP_LOCK(x)  spin_lock_wr(&fq_fqmp_lock)
73
74
75 #define FQ_FQMP_UNLOCK(x)       spin_unlock_wr(&(x)->lock); \
76                                 fq_dereference_mpriv((x))
77
78 #define FQ_FQP_UNLOCK(x)        spin_unlock_wr(&(x)->lock); \
79                                 fq_dereference_priv((x))
80
81 #define FQ_DPRIV_UNLOCK(x)      spin_unlock_wr(&(x)->lock); \
82                                 fq_dereference_dpriv((x))
83
84 #define FQ_GLOBAL_FQMP_UNLOCK(x) spin_unlock_wr(&fq_fqmp_lock)
85
86 #define FQ_REBALANCE_TIMEOUT    1       /* in seconds */
87 #define FQ_TOTAL_DISK_TIME      1000000*FQ_REBALANCE_TIMEOUT    /* in useconds */
88
89
90 #define FQ_DRAIN_CANCEL 0x1
91 #define FQ_DRAIN_FLUSH  0x2
92
93 struct disk;
94 struct proc;
95
96 #define FQP_LINKED_DPRIV        0x01
97 #define FQP_LINKED_FQMP         0x02
98
99 struct dsched_fq_priv {
100         TAILQ_ENTRY(dsched_fq_priv)     link;
101         TAILQ_ENTRY(dsched_fq_priv)     dlink;
102         TAILQ_HEAD(, bio)       queue;
103
104         struct  spinlock        lock;
105         struct  disk            *dp;
106         struct dsched_fq_dpriv  *dpriv;
107         struct dsched_fq_mpriv  *fqmp;
108         struct proc             *p;
109
110         int32_t qlength;
111         int32_t flags;
112
113         int     refcount;
114         int32_t transactions;
115         int32_t avg_latency;
116         int32_t max_tp;
117         int32_t issued;
118 };
119
120 struct dsched_fq_dpriv {
121         struct thread   *td;
122         struct disk     *dp;
123         struct spinlock lock;
124         int     refcount;
125
126         int     avg_rq_time;    /* XXX: unused */
127         int32_t incomplete_tp;
128         int64_t max_budget;
129         int     idle;
130         struct timeval start_idle;
131         int     idle_time;
132         int     die;
133
134         /* list contains all fq_priv for this disk */
135         TAILQ_HEAD(, dsched_fq_priv)    fq_priv_list;
136         TAILQ_ENTRY(dsched_fq_dpriv)    link;
137 };
138
139 struct dsched_fq_mpriv {
140         struct proc *p;
141         struct thread *td;
142         int dead;
143         struct spinlock lock;
144         int     refcount;
145         TAILQ_HEAD(, dsched_fq_priv)    fq_priv_list;
146         TAILQ_ENTRY(dsched_fq_mpriv)    link;
147 };
148
149
150 #define FQ_PRIO_BIAS            5
151 #define FQ_PRIO_MAX             10
152 #define FQ_PRIO_MIN             1
153 #define FQ_PRIO_IDLE            -1
154 #define FQ_BUCKET_ACTIVE        0x01
155
156
157
158 struct dsched_fq_priv   *fq_alloc_priv(struct disk *dp);
159 struct dsched_fq_dpriv  *fq_alloc_dpriv(struct disk *dp);
160 struct dsched_fq_mpriv  *fq_alloc_mpriv(struct proc *p);
161 void    fq_balance_thread(struct dsched_fq_dpriv *dpriv);
162 void    fq_dispatcher(struct dsched_fq_dpriv *dpriv);
163 biodone_t       fq_completed;
164
165 void    fq_reference_dpriv(struct dsched_fq_dpriv *dpriv);
166 void    fq_reference_priv(struct dsched_fq_priv *fqp);
167 void    fq_reference_mpriv(struct dsched_fq_mpriv *fqmp);
168 void    fq_dereference_dpriv(struct dsched_fq_dpriv *dpriv);
169 void    fq_dereference_priv(struct dsched_fq_priv *fqp);
170 void    fq_dereference_mpriv(struct dsched_fq_mpriv *fqmp);
171 void    fq_dispatch(struct dsched_fq_dpriv *dpriv, struct bio *bio,
172                         struct dsched_fq_priv *fqp);
173 void    fq_drain(struct dsched_fq_dpriv *dpriv, int mode);
174 #endif /* _KERNEL || _KERNEL_STRUCTURES */
175
176
177 struct dsched_fq_stats {
178         int32_t fqmp_allocations;
179         int32_t fqp_allocations;
180         int32_t dpriv_allocations;
181
182         int32_t procs_limited;
183
184         int32_t transactions;
185         int32_t transactions_completed;
186         int32_t cancelled;
187
188         int32_t no_fqmp;
189
190         int32_t nthreads;
191         int32_t nprocs;
192
193         int32_t nbufs;
194 };
195
196 #endif /* _DSCHED_FQ_H_ */