ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / sunrpc / timer.c
1 /*
2  * linux/net/sunrpc/timer.c
3  *
4  * Estimate RPC request round trip time.
5  *
6  * Based on packet round-trip and variance estimator algorithms described
7  * in appendix A of "Congestion Avoidance and Control" by Van Jacobson
8  * and Michael J. Karels (ACM Computer Communication Review; Proceedings
9  * of the Sigcomm '88 Symposium in Stanford, CA, August, 1988).
10  *
11  * This RTT estimator is used only for RPC over datagram protocols.
12  *
13  * Copyright (C) 2002 Trond Myklebust <trond.myklebust@fys.uio.no>
14  */
15
16 #include <asm/param.h>
17
18 #include <linux/types.h>
19 #include <linux/unistd.h>
20
21 #include <linux/sunrpc/clnt.h>
22 #include <linux/sunrpc/xprt.h>
23 #include <linux/sunrpc/timer.h>
24
25 #define RPC_RTO_MAX (60*HZ)
26 #define RPC_RTO_INIT (HZ/5)
27 #define RPC_RTO_MIN (HZ/10)
28
29 void
30 rpc_init_rtt(struct rpc_rtt *rt, unsigned long timeo)
31 {
32         unsigned long init = 0;
33         unsigned i;
34
35         rt->timeo = timeo;
36
37         if (timeo > RPC_RTO_INIT)
38                 init = (timeo - RPC_RTO_INIT) << 3;
39         for (i = 0; i < 5; i++) {
40                 rt->srtt[i] = init;
41                 rt->sdrtt[i] = RPC_RTO_INIT;
42         }
43 }
44
45 /*
46  * NB: When computing the smoothed RTT and standard deviation,
47  *     be careful not to produce negative intermediate results.
48  */
49 void
50 rpc_update_rtt(struct rpc_rtt *rt, unsigned timer, long m)
51 {
52         long *srtt, *sdrtt;
53
54         if (timer-- == 0)
55                 return;
56
57         /* jiffies wrapped; ignore this one */
58         if (m < 0)
59                 return;
60
61         if (m == 0)
62                 m = 1L;
63
64         srtt = (long *)&rt->srtt[timer];
65         m -= *srtt >> 3;
66         *srtt += m;
67
68         if (m < 0)
69                 m = -m;
70
71         sdrtt = (long *)&rt->sdrtt[timer];
72         m -= *sdrtt >> 2;
73         *sdrtt += m;
74
75         /* Set lower bound on the variance */
76         if (*sdrtt < RPC_RTO_MIN)
77                 *sdrtt = RPC_RTO_MIN;
78 }
79
80 /*
81  * Estimate rto for an nfs rpc sent via. an unreliable datagram.
82  * Use the mean and mean deviation of rtt for the appropriate type of rpc
83  * for the frequent rpcs and a default for the others.
84  * The justification for doing "other" this way is that these rpcs
85  * happen so infrequently that timer est. would probably be stale.
86  * Also, since many of these rpcs are
87  * non-idempotent, a conservative timeout is desired.
88  * getattr, lookup,
89  * read, write, commit     - A+4D
90  * other                   - timeo
91  */
92
93 unsigned long
94 rpc_calc_rto(struct rpc_rtt *rt, unsigned timer)
95 {
96         unsigned long res;
97
98         if (timer-- == 0)
99                 return rt->timeo;
100
101         res = ((rt->srtt[timer] + 7) >> 3) + rt->sdrtt[timer];
102         if (res > RPC_RTO_MAX)
103                 res = RPC_RTO_MAX;
104
105         return res;
106 }