a77bd484a4800d5c6beae8d0f12b01cf149adbfa
[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_DRAIN_CANCEL 0x1
59 #define FQ_DRAIN_FLUSH  0x2
60
61 struct disk;
62 struct proc;
63
64 struct fq_disk_ctx {
65         struct dsched_disk_ctx head;
66
67         struct thread   *td;            /* dispatcher thread td */
68         struct thread   *td_balance;    /* balancer thread td */
69         int     avg_rq_time;            /* XXX: not yet used */
70         int32_t incomplete_tp;          /* IOs issued but not completed */
71         int     idle;                   /* disk idle ? */
72         struct timeval start_idle;      /* disk idleness start time */
73         int     idle_time;              /* aggregate idle time in interval */
74         int     die;                    /* flag to kill related threads */
75         struct timeval start_interval;  /* current interval start time */
76
77         int     prev_full;              /* disk >90% busy during prev. to last
78                                            interval? */
79         int     last_full;              /* disk >90% busy during last interval */
80         int     disk_busy;              /* disk >90% busy during cur. interval */
81         int64_t budgetpb[FQ_PRIO_MAX+1];/* next interval budget for each thread
82                                            in each prio */
83 };
84
85 struct fq_thread_io {
86         struct dsched_thread_io head;
87
88         int32_t transactions;   /* IOs completed so far during current interval */
89         int32_t avg_latency;    /* avg latency for current interval IOs */
90         int32_t interval_transactions;  /* IOs completed during last interval */
91         int32_t interval_avg_latency;   /* avg latency for last interval IOs */
92         int32_t max_tp;         /* rate limit of transactions per interval */
93         int32_t issued;         /* IOs issued to disk (but not completed) */
94
95         int     rebalance;      /* thread needs to rebalance w/ fq_balance_self */
96 };
97
98
99
100 void    fq_balance_thread(struct fq_disk_ctx *diskctx);
101 void    fq_dispatcher(struct fq_disk_ctx *diskctx);
102 biodone_t       fq_completed;
103
104 void    fq_dispatch(struct fq_disk_ctx *diskctx, struct bio *bio,
105                         struct fq_thread_io *tdio);
106 void    fq_drain(struct fq_disk_ctx *diskctx, int mode);
107 void    fq_balance_self(struct fq_thread_io *tdio);
108 #endif /* _KERNEL || _KERNEL_STRUCTURES */
109
110
111 struct dsched_fq_stats {
112         int32_t procs_limited;
113
114         int32_t transactions;
115         int32_t transactions_completed;
116         int32_t cancelled;
117 };
118
119 #endif /* _DSCHED_FQ_H_ */