Added WITH_PROXY to allow it to build without proxying
[freeradius.git] / src / main / stats.c
1 /*
2  * stats.c      Internal statistics handling.
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2008  The FreeRADIUS server project
21  * Copyright 2008  Alan DeKok <aland@deployingradius.com>
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/rad_assert.h>
29
30 #ifdef WITH_STATS
31
32 #define USEC (1000000)
33 #define EMA_SCALE (100)
34 #define PREC (USEC * EMA_SCALE)
35
36 #define F_EMA_SCALE (1000000)
37
38 static struct timeval   start_time;
39 static struct timeval   hup_time;
40
41 fr_stats_t radius_auth_stats = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
42 #ifdef WITH_ACCOUNTING
43 fr_stats_t radius_acct_stats = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
44 #endif
45
46 #ifdef WITH_PROXY
47 fr_stats_t proxy_auth_stats = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
48 #ifdef WITH_ACCOUNTING
49 fr_stats_t proxy_acct_stats = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
50 #endif
51 #endif
52
53 void request_stats_final(REQUEST *request)
54 {
55         if (request->master_state == REQUEST_COUNTED) return;
56
57         if ((request->listener->type != RAD_LISTEN_NONE) &&
58             (request->listener->type != RAD_LISTEN_AUTH) &&
59             (request->listener->type != RAD_LISTEN_ACCT)) return;
60
61 #undef INC_AUTH
62 #define INC_AUTH(_x) radius_auth_stats._x++;request->listener->stats._x++;if (request->client && request->client->auth) request->client->auth->_x++;
63
64
65 #undef INC_ACCT
66 #define INC_ACCT(_x) radius_acct_stats._x++;request->listener->stats._x++;if (request->client && request->client->acct) request->client->acct->_x++
67
68         /*
69          *      Update the statistics.
70          *
71          *      Note that we do NOT do this in a child thread.
72          *      Instead, we update the stats when a request is
73          *      deleted, because only the main server thread calls
74          *      this function, which makes it thread-safe.
75          */
76         if (request->reply) switch (request->reply->code) {
77         case PW_AUTHENTICATION_ACK:
78                 INC_AUTH(total_responses);
79                 INC_AUTH(total_access_accepts);
80                 break;
81
82         case PW_AUTHENTICATION_REJECT:
83                 INC_AUTH(total_responses);
84                 INC_AUTH(total_access_rejects);
85                 break;
86
87         case PW_ACCESS_CHALLENGE:
88                 INC_AUTH(total_responses);
89                 INC_AUTH(total_access_challenges);
90                 break;
91
92 #ifdef WITH_ACCOUNTING
93         case PW_ACCOUNTING_RESPONSE:
94                 INC_ACCT(total_responses);
95                 break;
96 #endif
97
98                 /*
99                  *      No response, it must have been a bad
100                  *      authenticator.
101                  */
102         case 0:
103                 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
104                         if (request->reply->offset == -2) {
105                                 INC_AUTH(total_bad_authenticators);
106                         } else {
107                                 INC_AUTH(total_packets_dropped);
108                         }
109                 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
110                         if (request->reply->offset == -2) {
111                                 INC_ACCT(total_bad_authenticators);
112                         } else {
113                                 INC_ACCT(total_packets_dropped);
114                         }
115                 }
116                 break;
117
118         default:
119                 break;
120         }
121
122 #ifdef WITH_PROXY
123         if (!request->proxy || !request->proxy_listener) goto done;     /* simplifies formatting */
124
125         switch (request->proxy->code) {
126         case PW_AUTHENTICATION_REQUEST:
127                 proxy_auth_stats.total_requests += request->num_proxied_requests;
128                 request->proxy_listener->stats.total_requests += request->num_proxied_requests;
129                 request->home_server->stats.total_requests += request->num_proxied_requests;
130                 break;
131
132 #ifdef WITH_ACCOUNTING
133         case PW_ACCOUNTING_REQUEST:
134                 proxy_acct_stats.total_requests++;
135                 request->proxy_listener->stats.total_requests += request->num_proxied_requests;
136                 request->home_server->stats.total_requests += request->num_proxied_requests;
137                 break;
138 #endif
139
140         default:
141                 break;
142         }
143
144         if (!request->proxy_reply) goto done;   /* simplifies formatting */
145
146 #undef INC
147 #define INC(_x) proxy_auth_stats._x += request->num_proxied_responses; request->proxy_listener->stats._x += request->num_proxied_responses; request->home_server->stats._x += request->num_proxied_responses;
148
149         switch (request->proxy_reply->code) {
150         case PW_AUTHENTICATION_ACK:
151                 INC(total_responses);
152                 INC(total_access_accepts);
153                 break;
154
155         case PW_AUTHENTICATION_REJECT:
156                 INC(total_responses);
157                 INC(total_access_rejects);
158                 break;
159
160         case PW_ACCESS_CHALLENGE:
161                 INC(total_responses);
162                 INC(total_access_challenges);
163                 break;
164
165 #ifdef WITH_ACCOUNTING
166         case PW_ACCOUNTING_RESPONSE:
167                 proxy_acct_stats.total_responses++;
168                 request->proxy_listener->stats.total_responses++;
169                 request->home_server->stats.total_responses++;
170                 break;
171 #endif
172
173         default:
174                 proxy_auth_stats.total_unknown_types++;
175                 request->proxy_listener->stats.total_unknown_types++;
176                 request->home_server->stats.total_unknown_types++;
177                 break;
178         }
179
180  done:
181 #endif /* WITH_PROXY */
182
183         request->master_state = REQUEST_COUNTED;
184 }
185
186 typedef struct fr_stats2vp {
187         int     attribute;
188         size_t  offset;
189 } fr_stats2vp;
190
191 /*
192  *      Authentication
193  */
194 static fr_stats2vp authvp[] = {
195         { 128, offsetof(fr_stats_t, total_requests) },
196         { 129, offsetof(fr_stats_t, total_access_accepts) },
197         { 130, offsetof(fr_stats_t, total_access_rejects) },
198         { 131, offsetof(fr_stats_t, total_access_challenges) },
199         { 132, offsetof(fr_stats_t, total_responses) },
200         { 133, offsetof(fr_stats_t, total_dup_requests) },
201         { 134, offsetof(fr_stats_t, total_malformed_requests) },
202         { 135, offsetof(fr_stats_t, total_bad_authenticators) },
203         { 136, offsetof(fr_stats_t, total_packets_dropped) },
204         { 137, offsetof(fr_stats_t, total_unknown_types) },
205         { 0, 0 }
206 };
207
208
209 #ifdef WITH_PROXY
210 /*
211  *      Proxied authentication requests.
212  */
213 static fr_stats2vp proxy_authvp[] = {
214         { 138, offsetof(fr_stats_t, total_requests) },
215         { 139, offsetof(fr_stats_t, total_access_accepts) },
216         { 140, offsetof(fr_stats_t, total_access_rejects) },
217         { 141, offsetof(fr_stats_t, total_access_challenges) },
218         { 142, offsetof(fr_stats_t, total_responses) },
219         { 143, offsetof(fr_stats_t, total_dup_requests) },
220         { 144, offsetof(fr_stats_t, total_malformed_requests) },
221         { 145, offsetof(fr_stats_t, total_bad_authenticators) },
222         { 146, offsetof(fr_stats_t, total_packets_dropped) },
223         { 147, offsetof(fr_stats_t, total_unknown_types) },
224         { 0, 0 }
225 };
226 #endif
227
228
229 #ifdef WITH_ACCOUNTING
230 /*
231  *      Accounting
232  */
233 static fr_stats2vp acctvp[] = {
234         { 148, offsetof(fr_stats_t, total_requests) },
235         { 149, offsetof(fr_stats_t, total_responses) },
236         { 150, offsetof(fr_stats_t, total_dup_requests) },
237         { 151, offsetof(fr_stats_t, total_malformed_requests) },
238         { 152, offsetof(fr_stats_t, total_bad_authenticators) },
239         { 153, offsetof(fr_stats_t, total_packets_dropped) },
240         { 154, offsetof(fr_stats_t, total_unknown_types) },
241         { 0, 0 }
242 };
243
244 #ifdef WITH_PROXY
245 static fr_stats2vp proxy_acctvp[] = {
246         { 155, offsetof(fr_stats_t, total_requests) },
247         { 156, offsetof(fr_stats_t, total_responses) },
248         { 157, offsetof(fr_stats_t, total_dup_requests) },
249         { 158, offsetof(fr_stats_t, total_malformed_requests) },
250         { 159, offsetof(fr_stats_t, total_bad_authenticators) },
251         { 160, offsetof(fr_stats_t, total_packets_dropped) },
252         { 161, offsetof(fr_stats_t, total_unknown_types) },
253         { 0, 0 }
254 };
255 #endif
256 #endif
257
258 static fr_stats2vp client_authvp[] = {
259         { 128, offsetof(fr_stats_t, total_requests) },
260         { 129, offsetof(fr_stats_t, total_access_accepts) },
261         { 130, offsetof(fr_stats_t, total_access_rejects) },
262         { 131, offsetof(fr_stats_t, total_access_challenges) },
263         { 132, offsetof(fr_stats_t, total_responses) },
264         { 133, offsetof(fr_stats_t, total_dup_requests) },
265         { 134, offsetof(fr_stats_t, total_malformed_requests) },
266         { 135, offsetof(fr_stats_t, total_bad_authenticators) },
267         { 136, offsetof(fr_stats_t, total_packets_dropped) },
268         { 137, offsetof(fr_stats_t, total_unknown_types) },
269         { 0, 0 }
270 };
271
272 #ifdef WITH_ACCOUNTING
273 static fr_stats2vp client_acctvp[] = {
274         { 148, offsetof(fr_stats_t, total_requests) },
275         { 149, offsetof(fr_stats_t, total_responses) },
276         { 150, offsetof(fr_stats_t, total_dup_requests) },
277         { 151, offsetof(fr_stats_t, total_malformed_requests) },
278         { 152, offsetof(fr_stats_t, total_bad_authenticators) },
279         { 153, offsetof(fr_stats_t, total_packets_dropped) },
280         { 154, offsetof(fr_stats_t, total_unknown_types) },
281         { 0, 0 }
282 };
283 #endif
284
285 #define FR2ATTR(x) ((11344 << 16) | (x))
286
287 static void request_stats_addvp(REQUEST *request,
288                                 fr_stats2vp *table, fr_stats_t *stats)
289 {
290         int i;
291         VALUE_PAIR *vp;
292
293         for (i = 0; table[i].attribute != 0; i++) {
294                 vp = radius_paircreate(request, &request->reply->vps,
295                                        FR2ATTR(table[i].attribute),
296                                        PW_TYPE_INTEGER);
297                 if (!vp) continue;
298
299                 vp->vp_integer = *(int *)(((char *) stats) + table[i].offset);
300         }
301 }
302
303
304 void request_stats_reply(REQUEST *request)
305 {
306         VALUE_PAIR *flag, *vp;
307
308         /*
309          *      Statistics are available ONLY on a "status" port.
310          */
311         rad_assert(request->packet->code == PW_STATUS_SERVER);
312         rad_assert(request->listener->type == RAD_LISTEN_NONE);
313                 
314         flag = pairfind(request->packet->vps, FR2ATTR(127));
315         if (!flag || (flag->vp_integer == 0)) return;
316
317         /*
318          *      Authentication.
319          */
320         if (((flag->vp_integer & 0x01) != 0) &&
321             ((flag->vp_integer & 0xc0) == 0)) {
322                 request_stats_addvp(request, authvp, &radius_auth_stats);
323         }
324                 
325 #ifdef WITH_ACCOUNTING
326         /*
327          *      Accounting
328          */
329         if (((flag->vp_integer & 0x02) != 0) &&
330             ((flag->vp_integer & 0xc0) == 0)) {
331                 request_stats_addvp(request, acctvp, &radius_acct_stats);
332         }
333 #endif
334
335 #ifdef WITH_PROXY
336         /*
337          *      Proxied authentication requests.
338          */
339         if (((flag->vp_integer & 0x04) != 0) &&
340             ((flag->vp_integer & 0x20) == 0)) {
341                 request_stats_addvp(request, proxy_authvp, &proxy_auth_stats);
342         }
343
344 #ifdef WITH_ACCOUNTING
345         /*
346          *      Proxied accounting requests.
347          */
348         if (((flag->vp_integer & 0x08) != 0) &&
349             ((flag->vp_integer & 0x20) == 0)) {
350                 request_stats_addvp(request, proxy_acctvp, &proxy_acct_stats);
351         }
352 #endif
353 #endif
354
355         /*
356          *      Internal server statistics
357          */
358         if ((flag->vp_integer & 0x10) != 0) {
359                 vp = radius_paircreate(request, &request->reply->vps,
360                                        FR2ATTR(176), PW_TYPE_DATE);
361                 if (vp) vp->vp_date = start_time.tv_sec;
362                 vp = radius_paircreate(request, &request->reply->vps,
363                                        FR2ATTR(177), PW_TYPE_DATE);
364                 if (vp) vp->vp_date = hup_time.tv_sec;
365                 
366 #ifdef HAVE_PTHREAD_H
367                 int i, array[RAD_LISTEN_MAX];
368
369                 thread_pool_queue_stats(array);
370
371                 for (i = 0; i <= RAD_LISTEN_DETAIL; i++) {
372                         vp = radius_paircreate(request, &request->reply->vps,
373                                                FR2ATTR(162 + i),
374                                                PW_TYPE_INTEGER);
375                         
376                         if (!vp) continue;
377                         vp->vp_integer = array[i];
378                 }
379 #endif
380         }
381
382         /*
383          *      For a particular client.
384          */
385         if ((flag->vp_integer & 0x20) != 0) {
386                 fr_ipaddr_t ipaddr;
387                 VALUE_PAIR *server_ip, *server_port = NULL;
388                 RADCLIENT *client = NULL;
389                 RADCLIENT_LIST *cl = NULL;
390
391                 /*
392                  *      See if we need to look up the client by server
393                  *      socket.
394                  */
395                 server_ip = pairfind(request->packet->vps, FR2ATTR(170));
396                 if (server_ip) {
397                         server_port = pairfind(request->packet->vps,
398                                                FR2ATTR(171));
399
400                         if (server_port) {
401                                 ipaddr.af = AF_INET;
402                                 ipaddr.ipaddr.ip4addr.s_addr = server_ip->vp_ipaddr;
403                                 cl = listener_find_client_list(&ipaddr, server_port->vp_integer);
404                                                                
405                                 /*
406                                  *      Not found: don't do anything
407                                  */
408                                 if (!cl) return;
409                         }
410                 }
411
412
413                 vp = pairfind(request->packet->vps, FR2ATTR(167));
414                 if (vp) {
415                         ipaddr.af = AF_INET;
416                         ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
417                         client = client_find(cl, &ipaddr, IPPROTO_UDP);
418 #ifdef WITH_TCP
419                         if (!client) {
420                                 client = client_find(cl, &ipaddr, IPPROTO_TCP);
421                         }
422 #endif
423
424                         /*
425                          *      Else look it up by number.
426                          */
427                 } else if ((vp = pairfind(request->packet->vps,
428                                            FR2ATTR(168))) != NULL) {
429                         client = client_findbynumber(cl, vp->vp_integer);
430                 }
431
432                 if (client) {
433                         /*
434                          *      If found, echo it back, along with
435                          *      the requested statistics.
436                          */
437                         pairadd(&request->reply->vps, paircopyvp(vp));
438
439                         /*
440                          *      When retrieving client by number, also
441                          *      echo back it's IP address.
442                          */
443                         if ((vp->type == PW_TYPE_INTEGER) &&
444                             (client->ipaddr.af == AF_INET)) {
445                                 vp = radius_paircreate(request,
446                                                        &request->reply->vps,
447                                                        FR2ATTR(167),
448                                                        PW_TYPE_IPADDR);
449                                 if (vp) {
450                                         vp->vp_ipaddr = client->ipaddr.ipaddr.ip4addr.s_addr;
451                                 }
452
453                                 if (client->prefix != 32) {
454                                         vp = radius_paircreate(request,
455                                                                &request->reply->vps,
456                                                                FR2ATTR(169),
457                                                                PW_TYPE_INTEGER);
458                                         if (vp) {
459                                                 vp->vp_integer = client->prefix;
460                                         }
461                                 }
462                         }
463                         
464                         if (server_ip) {
465                                 pairadd(&request->reply->vps,
466                                         paircopyvp(server_ip));
467                                 pairadd(&request->reply->vps,
468                                         paircopyvp(server_port));
469                         }
470
471                         if (client->auth &&
472                             ((flag->vp_integer & 0x01) != 0)) {
473                                 request_stats_addvp(request, client_authvp,
474                                                     client->auth);
475                         }
476 #ifdef WITH_ACCOUNTING
477                         if (client->acct &&
478                             ((flag->vp_integer & 0x01) != 0)) {
479                                 request_stats_addvp(request, client_acctvp,
480                                                     client->acct);
481                         }
482 #endif
483                 } /* else client wasn't found, don't echo it back */
484         }
485
486         /*
487          *      For a particular "listen" socket.
488          */
489         if (((flag->vp_integer & 0x40) != 0) &&
490             ((flag->vp_integer & 0x03) != 0)) {
491                 rad_listen_t *this;
492                 VALUE_PAIR *server_ip, *server_port;
493                 fr_ipaddr_t ipaddr;
494
495                 /*
496                  *      See if we need to look up the server by socket
497                  *      socket.
498                  */
499                 server_ip = pairfind(request->packet->vps, FR2ATTR(170));
500                 if (!server_ip) return;
501
502                 server_port = pairfind(request->packet->vps,
503                                        FR2ATTR(171));
504                 if (!server_port) return;
505                 
506                 ipaddr.af = AF_INET;
507                 ipaddr.ipaddr.ip4addr.s_addr = server_ip->vp_ipaddr;
508                 this = listener_find_byipaddr(&ipaddr,
509                                               server_port->vp_integer);
510                 
511                 /*
512                  *      Not found: don't do anything
513                  */
514                 if (!this) return;
515                 
516                 pairadd(&request->reply->vps,
517                         paircopyvp(server_ip));
518                 pairadd(&request->reply->vps,
519                         paircopyvp(server_port));
520
521                 if (((flag->vp_integer & 0x01) != 0) &&
522                     ((request->listener->type == RAD_LISTEN_AUTH) ||
523                      (request->listener->type == RAD_LISTEN_NONE))) {
524                         request_stats_addvp(request, authvp, &this->stats);
525                 }
526                 
527 #ifdef WITH_ACCOUNTING
528                 if (((flag->vp_integer & 0x02) != 0) &&
529                     ((request->listener->type == RAD_LISTEN_ACCT) ||
530                      (request->listener->type == RAD_LISTEN_NONE))) {
531                         request_stats_addvp(request, acctvp, &this->stats);
532                 }
533 #endif
534         }
535
536 #ifdef WITH_PROXY
537         /*
538          *      Home servers.
539          */
540         if (((flag->vp_integer & 0x80) != 0) &&
541             ((flag->vp_integer & 0x03) != 0)) {
542                 home_server *home;
543                 VALUE_PAIR *server_ip, *server_port;
544                 fr_ipaddr_t ipaddr;
545
546                 /*
547                  *      See if we need to look up the server by socket
548                  *      socket.
549                  */
550                 server_ip = pairfind(request->packet->vps, FR2ATTR(170));
551                 if (!server_ip) return;
552
553                 server_port = pairfind(request->packet->vps,
554                                        FR2ATTR(171));
555                 if (!server_port) return;
556                 
557                 ipaddr.af = AF_INET;
558                 ipaddr.ipaddr.ip4addr.s_addr = server_ip->vp_ipaddr;
559                 home = home_server_find(&ipaddr, server_port->vp_integer,
560                                         IPPROTO_UDP);
561
562                 /*
563                  *      Not found: don't do anything
564                  */
565                 if (!home) return;
566                 
567                 pairadd(&request->reply->vps,
568                         paircopyvp(server_ip));
569                 pairadd(&request->reply->vps,
570                         paircopyvp(server_port));
571
572                 vp = radius_paircreate(request, &request->reply->vps,
573                                        FR2ATTR(172), PW_TYPE_INTEGER);
574                 if (vp) vp->vp_integer = home->currently_outstanding;
575
576                 vp = radius_paircreate(request, &request->reply->vps,
577                                        FR2ATTR(173), PW_TYPE_INTEGER);
578                 if (vp) vp->vp_integer = home->state;
579
580                 if ((home->state == HOME_STATE_ALIVE) &&
581                     (home->revive_time.tv_sec != 0)) {
582                         vp = radius_paircreate(request, &request->reply->vps,
583                                                FR2ATTR(175), PW_TYPE_DATE);
584                         if (vp) vp->vp_date = home->revive_time.tv_sec;
585                 }
586
587                 if ((home->state == HOME_STATE_ALIVE) &&
588                     (home->ema.window > 0)) {
589                                 vp = radius_paircreate(request,
590                                                        &request->reply->vps,
591                                                        FR2ATTR(178),
592                                                        PW_TYPE_INTEGER);
593                                 if (vp) vp->vp_integer = home->ema.window;
594                                 vp = radius_paircreate(request,
595                                                        &request->reply->vps,
596                                                        FR2ATTR(179),
597                                                        PW_TYPE_INTEGER);
598                                 if (vp) vp->vp_integer = home->ema.ema1 / EMA_SCALE;
599                                 vp = radius_paircreate(request,
600                                                        &request->reply->vps,
601                                                        FR2ATTR(180),
602                                                        PW_TYPE_INTEGER);
603                                 if (vp) vp->vp_integer = home->ema.ema10 / EMA_SCALE;
604
605                 }
606
607                 if (home->state == HOME_STATE_IS_DEAD) {
608                         vp = radius_paircreate(request, &request->reply->vps,
609                                                FR2ATTR(174), PW_TYPE_DATE);
610                         if (vp) vp->vp_date = home->zombie_period_start.tv_sec + home->zombie_period;
611                 }
612
613                 if (((flag->vp_integer & 0x01) != 0) &&
614                     (home->type == HOME_TYPE_AUTH)) {
615                         request_stats_addvp(request, proxy_authvp,
616                                             &home->stats);
617                 }
618
619 #ifdef WITH_ACCOUNTING
620                 if (((flag->vp_integer & 0x02) != 0) &&
621                     (home->type == HOME_TYPE_ACCT)) {
622                         request_stats_addvp(request, proxy_acctvp,
623                                             &home->stats);
624                 }
625 #endif
626         }
627 #endif  /* WITH_PROXY */
628 }
629
630 void radius_stats_init(int flag)
631 {
632         if (!flag) {
633                 gettimeofday(&start_time, NULL);
634                 hup_time = start_time; /* it's just nicer this way */
635         } else {
636                 gettimeofday(&hup_time, NULL);
637         }
638 }
639
640 void radius_stats_ema(fr_stats_ema_t *ema,
641                       struct timeval *start, struct timeval *end)
642 {
643         int micro;
644         time_t tdiff;
645 #ifdef WITH_STATS_DEBUG
646         static int n = 0;
647 #endif
648         if (ema->window == 0) return;
649
650         rad_assert(start->tv_sec >= end->tv_sec);
651
652         /*
653          *      Initialize it.
654          */
655         if (ema->f1 == 0) {
656                 if (ema->window > 10000) ema->window = 10000;
657                 
658                 ema->f1 =  (2 * F_EMA_SCALE) / (ema->window + 1);
659                 ema->f10 = (2 * F_EMA_SCALE) / ((10 * ema->window) + 1);
660         }
661
662
663         tdiff = start->tv_sec;
664         tdiff -= end->tv_sec;
665         
666         micro = (int) tdiff;
667         if (micro > 40) micro = 40; /* don't overflow 32-bit ints */
668         micro *= USEC;
669         micro += start->tv_usec;
670         micro -= end->tv_usec;
671         
672         micro *= EMA_SCALE;
673
674         if (ema->ema1 == 0) {
675                 ema->ema1 = micro;
676                 ema->ema10 = micro;
677         } else {
678                 int diff;
679                 
680                 diff = ema->f1 * (micro - ema->ema1);
681                 ema->ema1 += (diff / 1000000);
682                 
683                 diff = ema->f10 * (micro - ema->ema10);
684                 ema->ema10 += (diff / 1000000);
685         }
686         
687         
688 #ifdef WITH_STATS_DEBUG
689         DEBUG("time %d %d.%06d\t%d.%06d\t%d.%06d\n",
690               n, micro / PREC, (micro / EMA_SCALE) % USEC,
691               ema->ema1 / PREC, (ema->ema1 / EMA_SCALE) % USEC,
692               ema->ema10 / PREC, (ema->ema10 / EMA_SCALE) % USEC);
693         n++;
694 #endif  
695 }
696
697 #endif /* WITH_STATS */