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