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