dsched - Add debugging & fix rare problem conditions
[dragonfly.git] / sys / sys / dsched.h
CommitLineData
b80a9543
AH
1/*
2 * Copyright (c) 2009 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 _SYS_DSCHED_H_
35#define _SYS_DSCHED_H_
36
22befd90 37#if defined(_KERNEL)
b80a9543
AH
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_LOCK_H_
49#include <sys/lock.h>
50#endif
51#ifndef _SYS_CONF_H_
52#include <sys/conf.h>
53#endif
54#ifndef _SYS_MSGPORT_H_
55#include <sys/msgport.h>
56#endif
e02e815e
AH
57#ifndef _SYS_SYSCTL_H_
58#include <sys/sysctl.h>
59#endif
279e9fd5
AH
60#ifndef SYS_DISK_H_
61#include <sys/disk.h>
62#endif
b80a9543
AH
63
64#define DSCHED_POLICY_NAME_LENGTH 64
65
66#define dsched_set_disk_priv(dp, x) ((dp)->d_dsched_priv1 = (x))
67#define dsched_get_disk_priv(dp) ((dp)?((dp)->d_dsched_priv1):NULL)
68#define dsched_set_proc_priv(pp, x) ((pp)->p_dsched_priv1 = (x))
69#define dsched_get_proc_priv(pp) ((pp)?((pp)->p_dsched_priv1):NULL)
70
71#define dsched_set_thread_priv(td, x) ((td)->td_dsched_priv1 = (x))
72#define dsched_get_thread_priv(td) ((td)?((td)->td_dsched_priv1):NULL)
73
74#define dsched_set_buf_priv(bp, x) ((bp)->b_iosched = (x))
75#define dsched_get_buf_priv(bp) ((bp)?((bp)->b_iosched):NULL)
76#define dsched_clr_buf_priv(bp) ((bp)->b_iosched = NULL)
77#define dsched_is_clear_buf_priv(bp) ((bp)->b_iosched == NULL)
78
79
80#define dsched_set_bio_dp(bio, x) ((bio)->bio_caller_info1.ptr = (x))
81#define dsched_get_bio_dp(bio) ((bio)?((bio)->bio_caller_info1.ptr):NULL)
82#define dsched_set_bio_priv(bio, x) ((bio)->bio_caller_info2.ptr = (x))
83#define dsched_get_bio_priv(bio) ((bio)?((bio)->bio_caller_info2.ptr):NULL)
84#define dsched_set_bio_stime(bio, x) ((bio)->bio_caller_info3.lvalue = (x))
85#define dsched_get_bio_stime(bio) ((bio)?((bio)->bio_caller_info3.lvalue):0)
c7a0a046
AH
86#define dsched_set_bio_tdio(bio, x) ((bio)->bio_caller_info3.ptr = (x))
87#define dsched_get_bio_tdio(bio) ((bio)?((bio)->bio_caller_info3.ptr):0)
b80a9543
AH
88
89
e02e815e
AH
90struct dsched_thread_ctx {
91 TAILQ_ENTRY(dsched_thread_ctx) link;
92
93 TAILQ_HEAD(, dsched_thread_io) tdio_list; /* list of thread_io */
94 struct lock lock;
95
96 int32_t refcount;
97
98 struct proc *p;
99 struct thread *td;
100 int32_t dead;
101};
102
103struct dsched_disk_ctx {
104 TAILQ_ENTRY(dsched_disk_ctx) link;
105
106 TAILQ_HEAD(, dsched_thread_io) tdio_list; /* list of thread_io of disk */
107 struct lock lock;
108
109 int32_t refcount;
bc3c9325 110 int32_t flags;
e02e815e
AH
111
112 struct disk *dp; /* back pointer to disk struct */
bc3c9325
AH
113
114 struct sysctl_ctx_list sysctl_ctx;
e02e815e
AH
115};
116
c7a0a046
AH
117struct dsched_policy;
118
e02e815e
AH
119struct dsched_thread_io {
120 TAILQ_ENTRY(dsched_thread_io) link;
121 TAILQ_ENTRY(dsched_thread_io) dlink;
122
123 TAILQ_HEAD(, bio) queue; /* IO queue (bio) */
124 struct lock lock;
125 int32_t qlength;/* IO queue length */
126
127 int32_t refcount;
128
129 int32_t flags;
130
131 struct disk *dp;
132 struct dsched_disk_ctx *diskctx;
133 struct dsched_thread_ctx *tdctx;
134 struct proc *p;
c7a0a046
AH
135 struct dsched_policy *debug_policy;
136 int debug_inited;
137 int debug_priv;
e02e815e
AH
138};
139
140typedef int dsched_prepare_t(struct dsched_disk_ctx *diskctx);
141typedef void dsched_teardown_t(struct dsched_disk_ctx *diskctx);
142typedef void dsched_cancel_t(struct dsched_disk_ctx *diskctx);
143typedef int dsched_queue_t(struct dsched_disk_ctx *diskctx,
144 struct dsched_thread_io *tdio, struct bio *bio);
145
146typedef void dsched_new_tdio_t(struct dsched_thread_io *tdio);
147typedef void dsched_new_diskctx_t(struct dsched_disk_ctx *diskctx);
148typedef void dsched_destroy_tdio_t(struct dsched_thread_io *tdio);
149typedef void dsched_destroy_diskctx_t(struct dsched_disk_ctx *diskctx);
b80a9543 150
0160356d
AH
151
152struct dsched_policy {
153 char name[DSCHED_POLICY_NAME_LENGTH];
154 uint64_t uniq_id;
155 int ref_count;
156
157 TAILQ_ENTRY(dsched_policy) link;
b80a9543
AH
158
159 dsched_prepare_t *prepare;
160 dsched_teardown_t *teardown;
b80a9543
AH
161 dsched_cancel_t *cancel_all;
162 dsched_queue_t *bio_queue;
163
e02e815e
AH
164 dsched_new_tdio_t *new_tdio;
165 dsched_new_diskctx_t *new_diskctx;
166 dsched_destroy_tdio_t *destroy_tdio;
167 dsched_destroy_diskctx_t *destroy_diskctx;
b80a9543
AH
168};
169
b80a9543
AH
170TAILQ_HEAD(dsched_policy_head, dsched_policy);
171
e02e815e 172
fd87e9e4
MD
173#define DSCHED_THREAD_IO_LOCKINIT(x) \
174 lockinit(&(x)->lock, "tdiobioq", 0, LK_CANRECURSE)
175
176#define DSCHED_THREAD_IO_LOCK(x) do { \
177 dsched_thread_io_ref((x)); \
178 lockmgr(&(x)->lock, LK_EXCLUSIVE); \
179 } while(0)
180
181#define DSCHED_THREAD_IO_UNLOCK(x) do { \
182 lockmgr(&(x)->lock, LK_RELEASE); \
183 dsched_thread_io_unref((x)); \
184 } while(0)
185
186#define DSCHED_DISK_CTX_LOCKINIT(x) \
187 lockinit(&(x)->lock, "tdiodiskq", 0, LK_CANRECURSE)
188
189#define DSCHED_DISK_CTX_LOCK(x) do { \
190 dsched_disk_ctx_ref((x)); \
191 lockmgr(&(x)->lock, LK_EXCLUSIVE); \
192 } while(0)
193
194#define DSCHED_DISK_CTX_UNLOCK(x) do { \
195 lockmgr(&(x)->lock, LK_RELEASE); \
196 dsched_disk_ctx_unref((x)); \
197 } while(0)
198
199#define DSCHED_DISK_CTX_LOCK_ASSERT(x) \
200 KKASSERT(lockstatus(&(x)->lock, curthread) == LK_EXCLUSIVE)
201
202#define DSCHED_GLOBAL_THREAD_CTX_LOCKINIT(x) \
203 lockinit(&dsched_tdctx_lock, "tdctxglob", 0, LK_CANRECURSE)
204#define DSCHED_GLOBAL_THREAD_CTX_LOCK(x) \
205 lockmgr(&dsched_tdctx_lock, LK_EXCLUSIVE)
206#define DSCHED_GLOBAL_THREAD_CTX_UNLOCK(x) \
207 lockmgr(&dsched_tdctx_lock, LK_RELEASE)
208
209#define DSCHED_THREAD_CTX_LOCKINIT(x) \
210 lockinit(&(x)->lock, "tdctx", 0, LK_CANRECURSE)
211
212#define DSCHED_THREAD_CTX_LOCK(x) do { \
213 dsched_thread_ctx_ref((x)); \
214 lockmgr(&(x)->lock, LK_EXCLUSIVE); \
215 } while(0)
216
217#define DSCHED_THREAD_CTX_UNLOCK(x) do { \
218 lockmgr(&(x)->lock, LK_RELEASE); \
219 dsched_thread_ctx_unref((x)); \
220 } while(0)
e02e815e 221
bc3c9325 222/* flags for thread_io */
e02e815e
AH
223#define DSCHED_LINKED_DISK_CTX 0x01
224#define DSCHED_LINKED_THREAD_CTX 0x02
bc3c9325
AH
225/* flags for disk_ctx */
226#define DSCHED_SYSCTL_CTX_INITED 0x01
e02e815e
AH
227
228#define DSCHED_THREAD_CTX_MAX_SZ sizeof(struct dsched_thread_ctx)
229#define DSCHED_THREAD_IO_MAX_SZ 256
209d9536 230#define DSCHED_DISK_CTX_MAX_SZ 512
e02e815e
AH
231
232#define DSCHED_POLICY_MODULE(name, evh) \
233static moduledata_t name##_mod = { \
234 #name, \
235 evh, \
236 NULL \
237}; \
238DECLARE_MODULE(name, name##_mod, SI_SUB_PRE_DRIVERS, SI_ORDER_MIDDLE)
239
0160356d 240void dsched_disk_create_callback(struct disk *dp, const char *head_name, int unit);
279e9fd5 241void dsched_disk_update_callback(struct disk *dp, struct disk_info *info);
0160356d 242void dsched_disk_destroy_callback(struct disk *dp);
b80a9543 243void dsched_queue(struct disk *dp, struct bio *bio);
0160356d
AH
244int dsched_register(struct dsched_policy *d_policy);
245int dsched_unregister(struct dsched_policy *d_policy);
246int dsched_switch(struct disk *dp, struct dsched_policy *new_policy);
247void dsched_set_policy(struct disk *dp, struct dsched_policy *new_policy);
b80a9543
AH
248struct dsched_policy *dsched_find_policy(char *search);
249struct disk *dsched_find_disk(char *search);
250struct dsched_policy *dsched_policy_enumerate(struct dsched_policy *pol);
0160356d 251struct disk *dsched_disk_enumerate(struct disk *dp, struct dsched_policy *policy);
b80a9543
AH
252void dsched_cancel_bio(struct bio *bp);
253void dsched_strategy_raw(struct disk *dp, struct bio *bp);
254void dsched_strategy_sync(struct disk *dp, struct bio *bp);
255void dsched_strategy_async(struct disk *dp, struct bio *bp, biodone_t *done, void *priv);
fcefa6f2 256int dsched_debug(int level, char *fmt, ...) __printflike(2, 3);
e02e815e
AH
257
258void policy_new(struct disk *dp, struct dsched_policy *pol);
259void policy_destroy(struct disk *dp);
260
261void dsched_disk_ctx_ref(struct dsched_disk_ctx *diskctx);
262void dsched_thread_io_ref(struct dsched_thread_io *tdio);
263void dsched_thread_ctx_ref(struct dsched_thread_ctx *tdctx);
264void dsched_disk_ctx_unref(struct dsched_disk_ctx *diskctx);
265void dsched_thread_io_unref(struct dsched_thread_io *tdio);
266void dsched_thread_ctx_unref(struct dsched_thread_ctx *tdctx);
267
89dabacd
AH
268struct dsched_thread_io *dsched_new_policy_thread_tdio(struct dsched_disk_ctx *diskctx,
269 struct dsched_policy *pol);
e02e815e
AH
270struct dsched_thread_io *dsched_thread_io_alloc(struct disk *dp,
271 struct dsched_thread_ctx *tdctx, struct dsched_policy *pol);
272struct dsched_disk_ctx *dsched_disk_ctx_alloc(struct disk *dp,
273 struct dsched_policy *pol);
274struct dsched_thread_ctx *dsched_thread_ctx_alloc(struct proc *p);
275
276typedef void dsched_new_buf_t(struct buf *bp);
277typedef void dsched_new_proc_t(struct proc *p);
278typedef void dsched_new_thread_t(struct thread *td);
279typedef void dsched_exit_buf_t(struct buf *bp);
280typedef void dsched_exit_proc_t(struct proc *p);
281typedef void dsched_exit_thread_t(struct thread *td);
282
b80a9543
AH
283dsched_new_buf_t dsched_new_buf;
284dsched_new_proc_t dsched_new_proc;
285dsched_new_thread_t dsched_new_thread;
aa166ad1 286dsched_exit_buf_t dsched_exit_buf;
b80a9543
AH
287dsched_exit_proc_t dsched_exit_proc;
288dsched_exit_thread_t dsched_exit_thread;
289
22befd90 290#endif /* _KERNEL */
b80a9543
AH
291
292
293#define DSCHED_NAME_LENGTH 64
294#define DSCHED_SET_DEVICE_POLICY _IOWR('d', 1, struct dsched_ioctl)
295#define DSCHED_LIST_DISKS _IOWR('d', 2, struct dsched_ioctl)
296#define DSCHED_LIST_DISK _IOWR('d', 3, struct dsched_ioctl)
297#define DSCHED_LIST_POLICIES _IOWR('d', 4, struct dsched_ioctl)
298
299struct dsched_ioctl {
300 uint16_t num_elem;
301 char dev_name[DSCHED_NAME_LENGTH];
302 char pol_name[DSCHED_NAME_LENGTH];
303};
304
e02e815e
AH
305struct dsched_stats {
306 int32_t tdctx_allocations;
307 int32_t tdio_allocations;
308 int32_t diskctx_allocations;
309
310 int32_t no_tdctx;
311
312 int32_t nthreads;
313 int32_t nprocs;
314};
315
b80a9543 316#endif /* _SYS_DSCHED_H_ */