xref: /openbmc/linux/net/sunrpc/timer.c (revision 64c70b1c)
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 
23 #define RPC_RTO_MAX (60*HZ)
24 #define RPC_RTO_INIT (HZ/5)
25 #define RPC_RTO_MIN (HZ/10)
26 
27 void
28 rpc_init_rtt(struct rpc_rtt *rt, unsigned long timeo)
29 {
30 	unsigned long init = 0;
31 	unsigned i;
32 
33 	rt->timeo = timeo;
34 
35 	if (timeo > RPC_RTO_INIT)
36 		init = (timeo - RPC_RTO_INIT) << 3;
37 	for (i = 0; i < 5; i++) {
38 		rt->srtt[i] = init;
39 		rt->sdrtt[i] = RPC_RTO_INIT;
40 		rt->ntimeouts[i] = 0;
41 	}
42 }
43 
44 /*
45  * NB: When computing the smoothed RTT and standard deviation,
46  *     be careful not to produce negative intermediate results.
47  */
48 void
49 rpc_update_rtt(struct rpc_rtt *rt, unsigned timer, long m)
50 {
51 	long *srtt, *sdrtt;
52 
53 	if (timer-- == 0)
54 		return;
55 
56 	/* jiffies wrapped; ignore this one */
57 	if (m < 0)
58 		return;
59 
60 	if (m == 0)
61 		m = 1L;
62 
63 	srtt = (long *)&rt->srtt[timer];
64 	m -= *srtt >> 3;
65 	*srtt += m;
66 
67 	if (m < 0)
68 		m = -m;
69 
70 	sdrtt = (long *)&rt->sdrtt[timer];
71 	m -= *sdrtt >> 2;
72 	*sdrtt += m;
73 
74 	/* Set lower bound on the variance */
75 	if (*sdrtt < RPC_RTO_MIN)
76 		*sdrtt = RPC_RTO_MIN;
77 }
78 
79 /*
80  * Estimate rto for an nfs rpc sent via. an unreliable datagram.
81  * Use the mean and mean deviation of rtt for the appropriate type of rpc
82  * for the frequent rpcs and a default for the others.
83  * The justification for doing "other" this way is that these rpcs
84  * happen so infrequently that timer est. would probably be stale.
85  * Also, since many of these rpcs are
86  * non-idempotent, a conservative timeout is desired.
87  * getattr, lookup,
88  * read, write, commit     - A+4D
89  * other                   - timeo
90  */
91 
92 unsigned long
93 rpc_calc_rto(struct rpc_rtt *rt, unsigned timer)
94 {
95 	unsigned long res;
96 
97 	if (timer-- == 0)
98 		return rt->timeo;
99 
100 	res = ((rt->srtt[timer] + 7) >> 3) + rt->sdrtt[timer];
101 	if (res > RPC_RTO_MAX)
102 		res = RPC_RTO_MAX;
103 
104 	return res;
105 }
106