]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * ip_vs_est.c: simple rate estimator for IPVS | |
3 | * | |
1da177e4 LT |
4 | * Authors: Wensong Zhang <wensong@linuxvirtualserver.org> |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * Changes: | |
12 | * | |
13 | */ | |
9aada7ac HE |
14 | |
15 | #define KMSG_COMPONENT "IPVS" | |
16 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
17 | ||
1da177e4 | 18 | #include <linux/kernel.h> |
14c85021 | 19 | #include <linux/jiffies.h> |
1da177e4 | 20 | #include <linux/types.h> |
4ffd2e49 | 21 | #include <linux/interrupt.h> |
90754f8e | 22 | #include <linux/sysctl.h> |
3a14a313 | 23 | #include <linux/list.h> |
1da177e4 LT |
24 | |
25 | #include <net/ip_vs.h> | |
26 | ||
27 | /* | |
28 | This code is to estimate rate in a shorter interval (such as 8 | |
29 | seconds) for virtual services and real servers. For measure rate in a | |
30 | long interval, it is easy to implement a user level daemon which | |
31 | periodically reads those statistical counters and measure rate. | |
32 | ||
33 | Currently, the measurement is activated by slow timer handler. Hope | |
34 | this measurement will not introduce too much load. | |
35 | ||
36 | We measure rate during the last 8 seconds every 2 seconds: | |
37 | ||
38 | avgrate = avgrate*(1-W) + rate*W | |
39 | ||
40 | where W = 2^(-2) | |
41 | ||
42 | NOTES. | |
43 | ||
44 | * The stored value for average bps is scaled by 2^5, so that maximal | |
45 | rate is ~2.15Gbits/s, average pps and cps are scaled by 2^10. | |
46 | ||
47 | * A lot code is taken from net/sched/estimator.c | |
48 | */ | |
49 | ||
50 | ||
3a14a313 | 51 | static void estimation_timer(unsigned long arg); |
1da177e4 | 52 | |
3a14a313 SW |
53 | static LIST_HEAD(est_list); |
54 | static DEFINE_SPINLOCK(est_lock); | |
55 | static DEFINE_TIMER(est_timer, estimation_timer, 0, 0); | |
1da177e4 LT |
56 | |
57 | static void estimation_timer(unsigned long arg) | |
58 | { | |
59 | struct ip_vs_estimator *e; | |
60 | struct ip_vs_stats *s; | |
61 | u32 n_conns; | |
62 | u32 n_inpkts, n_outpkts; | |
63 | u64 n_inbytes, n_outbytes; | |
64 | u32 rate; | |
65 | ||
3a14a313 SW |
66 | spin_lock(&est_lock); |
67 | list_for_each_entry(e, &est_list, list) { | |
68 | s = container_of(e, struct ip_vs_stats, est); | |
1da177e4 LT |
69 | |
70 | spin_lock(&s->lock); | |
e9c0ce23 SW |
71 | n_conns = s->ustats.conns; |
72 | n_inpkts = s->ustats.inpkts; | |
73 | n_outpkts = s->ustats.outpkts; | |
74 | n_inbytes = s->ustats.inbytes; | |
75 | n_outbytes = s->ustats.outbytes; | |
1da177e4 LT |
76 | |
77 | /* scaled by 2^10, but divided 2 seconds */ | |
78 | rate = (n_conns - e->last_conns)<<9; | |
79 | e->last_conns = n_conns; | |
80 | e->cps += ((long)rate - (long)e->cps)>>2; | |
e9c0ce23 | 81 | s->ustats.cps = (e->cps+0x1FF)>>10; |
1da177e4 LT |
82 | |
83 | rate = (n_inpkts - e->last_inpkts)<<9; | |
84 | e->last_inpkts = n_inpkts; | |
85 | e->inpps += ((long)rate - (long)e->inpps)>>2; | |
e9c0ce23 | 86 | s->ustats.inpps = (e->inpps+0x1FF)>>10; |
1da177e4 LT |
87 | |
88 | rate = (n_outpkts - e->last_outpkts)<<9; | |
89 | e->last_outpkts = n_outpkts; | |
90 | e->outpps += ((long)rate - (long)e->outpps)>>2; | |
e9c0ce23 | 91 | s->ustats.outpps = (e->outpps+0x1FF)>>10; |
1da177e4 LT |
92 | |
93 | rate = (n_inbytes - e->last_inbytes)<<4; | |
94 | e->last_inbytes = n_inbytes; | |
95 | e->inbps += ((long)rate - (long)e->inbps)>>2; | |
e9c0ce23 | 96 | s->ustats.inbps = (e->inbps+0xF)>>5; |
1da177e4 LT |
97 | |
98 | rate = (n_outbytes - e->last_outbytes)<<4; | |
99 | e->last_outbytes = n_outbytes; | |
100 | e->outbps += ((long)rate - (long)e->outbps)>>2; | |
e9c0ce23 | 101 | s->ustats.outbps = (e->outbps+0xF)>>5; |
1da177e4 LT |
102 | spin_unlock(&s->lock); |
103 | } | |
3a14a313 | 104 | spin_unlock(&est_lock); |
1da177e4 LT |
105 | mod_timer(&est_timer, jiffies + 2*HZ); |
106 | } | |
107 | ||
3a14a313 | 108 | void ip_vs_new_estimator(struct ip_vs_stats *stats) |
1da177e4 | 109 | { |
3a14a313 | 110 | struct ip_vs_estimator *est = &stats->est; |
1da177e4 | 111 | |
3a14a313 | 112 | INIT_LIST_HEAD(&est->list); |
1da177e4 | 113 | |
e9c0ce23 SW |
114 | est->last_conns = stats->ustats.conns; |
115 | est->cps = stats->ustats.cps<<10; | |
1da177e4 | 116 | |
e9c0ce23 SW |
117 | est->last_inpkts = stats->ustats.inpkts; |
118 | est->inpps = stats->ustats.inpps<<10; | |
1da177e4 | 119 | |
e9c0ce23 SW |
120 | est->last_outpkts = stats->ustats.outpkts; |
121 | est->outpps = stats->ustats.outpps<<10; | |
1da177e4 | 122 | |
e9c0ce23 SW |
123 | est->last_inbytes = stats->ustats.inbytes; |
124 | est->inbps = stats->ustats.inbps<<5; | |
1da177e4 | 125 | |
e9c0ce23 SW |
126 | est->last_outbytes = stats->ustats.outbytes; |
127 | est->outbps = stats->ustats.outbps<<5; | |
1da177e4 | 128 | |
3a14a313 | 129 | spin_lock_bh(&est_lock); |
3a14a313 SW |
130 | list_add(&est->list, &est_list); |
131 | spin_unlock_bh(&est_lock); | |
1da177e4 LT |
132 | } |
133 | ||
134 | void ip_vs_kill_estimator(struct ip_vs_stats *stats) | |
135 | { | |
3a14a313 SW |
136 | struct ip_vs_estimator *est = &stats->est; |
137 | ||
138 | spin_lock_bh(&est_lock); | |
139 | list_del(&est->list); | |
3a14a313 | 140 | spin_unlock_bh(&est_lock); |
1da177e4 LT |
141 | } |
142 | ||
143 | void ip_vs_zero_estimator(struct ip_vs_stats *stats) | |
144 | { | |
3a14a313 SW |
145 | struct ip_vs_estimator *est = &stats->est; |
146 | ||
147 | /* set counters zero, caller must hold the stats->lock lock */ | |
148 | est->last_inbytes = 0; | |
149 | est->last_outbytes = 0; | |
150 | est->last_conns = 0; | |
151 | est->last_inpkts = 0; | |
152 | est->last_outpkts = 0; | |
153 | est->cps = 0; | |
154 | est->inpps = 0; | |
155 | est->outpps = 0; | |
156 | est->inbps = 0; | |
157 | est->outbps = 0; | |
1da177e4 | 158 | } |
a919cf4b SW |
159 | |
160 | int __init ip_vs_estimator_init(void) | |
161 | { | |
162 | mod_timer(&est_timer, jiffies + 2 * HZ); | |
163 | return 0; | |
164 | } | |
165 | ||
166 | void ip_vs_estimator_cleanup(void) | |
167 | { | |
168 | del_timer_sync(&est_timer); | |
169 | } |