2 * ip_vs_est.c: simple rate estimator for IPVS
4 * Version: $Id: ip_vs_est.c,v 1.4 2002/11/30 01:50:35 wensong Exp $
6 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
16 #include <linux/config.h>
17 #include <linux/kernel.h>
18 #include <linux/jiffies.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
22 #include <net/ip_vs.h>
25 This code is to estimate rate in a shorter interval (such as 8
26 seconds) for virtual services and real servers. For measure rate in a
27 long interval, it is easy to implement a user level daemon which
28 periodically reads those statistical counters and measure rate.
30 Currently, the measurement is activated by slow timer handler. Hope
31 this measurement will not introduce too much load.
33 We measure rate during the last 8 seconds every 2 seconds:
35 avgrate = avgrate*(1-W) + rate*W
41 * The stored value for average bps is scaled by 2^5, so that maximal
42 rate is ~2.15Gbits/s, average pps and cps are scaled by 2^10.
44 * A lot code is taken from net/sched/estimator.c
48 struct ip_vs_estimator
50 struct ip_vs_estimator *next;
51 struct ip_vs_stats *stats;
67 static struct ip_vs_estimator *est_list = NULL;
68 static DEFINE_RWLOCK(est_lock);
69 static struct timer_list est_timer;
71 static void estimation_timer(unsigned long arg)
73 struct ip_vs_estimator *e;
74 struct ip_vs_stats *s;
76 u32 n_inpkts, n_outpkts;
77 u64 n_inbytes, n_outbytes;
81 for (e = est_list; e; e = e->next) {
87 n_outpkts = s->outpkts;
88 n_inbytes = s->inbytes;
89 n_outbytes = s->outbytes;
91 /* scaled by 2^10, but divided 2 seconds */
92 rate = (n_conns - e->last_conns)<<9;
93 e->last_conns = n_conns;
94 e->cps += ((long)rate - (long)e->cps)>>2;
95 s->cps = (e->cps+0x1FF)>>10;
97 rate = (n_inpkts - e->last_inpkts)<<9;
98 e->last_inpkts = n_inpkts;
99 e->inpps += ((long)rate - (long)e->inpps)>>2;
100 s->inpps = (e->inpps+0x1FF)>>10;
102 rate = (n_outpkts - e->last_outpkts)<<9;
103 e->last_outpkts = n_outpkts;
104 e->outpps += ((long)rate - (long)e->outpps)>>2;
105 s->outpps = (e->outpps+0x1FF)>>10;
107 rate = (n_inbytes - e->last_inbytes)<<4;
108 e->last_inbytes = n_inbytes;
109 e->inbps += ((long)rate - (long)e->inbps)>>2;
110 s->inbps = (e->inbps+0xF)>>5;
112 rate = (n_outbytes - e->last_outbytes)<<4;
113 e->last_outbytes = n_outbytes;
114 e->outbps += ((long)rate - (long)e->outbps)>>2;
115 s->outbps = (e->outbps+0xF)>>5;
116 spin_unlock(&s->lock);
118 read_unlock(&est_lock);
119 mod_timer(&est_timer, jiffies + 2*HZ);
122 int ip_vs_new_estimator(struct ip_vs_stats *stats)
124 struct ip_vs_estimator *est;
126 est = kmalloc(sizeof(*est), GFP_KERNEL);
130 memset(est, 0, sizeof(*est));
132 est->last_conns = stats->conns;
133 est->cps = stats->cps<<10;
135 est->last_inpkts = stats->inpkts;
136 est->inpps = stats->inpps<<10;
138 est->last_outpkts = stats->outpkts;
139 est->outpps = stats->outpps<<10;
141 est->last_inbytes = stats->inbytes;
142 est->inbps = stats->inbps<<5;
144 est->last_outbytes = stats->outbytes;
145 est->outbps = stats->outbps<<5;
147 write_lock_bh(&est_lock);
148 est->next = est_list;
149 if (est->next == NULL) {
150 init_timer(&est_timer);
151 est_timer.expires = jiffies + 2*HZ;
152 est_timer.function = estimation_timer;
153 add_timer(&est_timer);
156 write_unlock_bh(&est_lock);
160 void ip_vs_kill_estimator(struct ip_vs_stats *stats)
162 struct ip_vs_estimator *est, **pest;
165 write_lock_bh(&est_lock);
167 while ((est=*pest) != NULL) {
168 if (est->stats != stats) {
176 if (killed && est_list == NULL)
177 del_timer_sync(&est_timer);
178 write_unlock_bh(&est_lock);
181 void ip_vs_zero_estimator(struct ip_vs_stats *stats)
183 struct ip_vs_estimator *e;
185 write_lock_bh(&est_lock);
186 for (e = est_list; e; e = e->next) {
187 if (e->stats != stats)
190 /* set counters zero */
195 e->last_outbytes = 0;
202 write_unlock_bh(&est_lock);