7a13c826452e71d2e01acd12923e53f0612219cd
[sort-networks.git] / src / pop_stats.c
1 /**
2  * libsortnetwork - src/pop_stats.c
3  * Copyright (C) 2009-2010  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <ff at octo.it>
20  **/
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <inttypes.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <pthread.h>
29
30 #include "pop_stats.h"
31
32 /* Yes, this is ugly, but the GNU libc doesn't export it with the above flags.
33  * */
34 char *strdup (const char *s);
35
36 struct pop_stats_s /* {{{ */
37 {
38   pthread_mutex_t lock;
39
40   /* Options */
41   char *opt_file;
42   long opt_interval;
43
44   /* Data */
45   long count;
46   int64_t rating_sum;
47   int rating_worst;
48   int rating_best;
49 }; /* }}} struct pop_stats_s */
50
51 /*
52  * Private functions
53  */
54 static int ps_flush (pop_stats_t *ps) /* {{{ */
55 {
56   double average;
57
58   average = ((double) ps->rating_sum) / ((double) ps->count);
59
60   fprintf (stdout, "[STATS] worst:%i average:%g best:%i\n",
61       ps->rating_worst, average, ps->rating_best);
62
63   ps->count = 0;
64   ps->rating_sum = 0;
65   ps->rating_worst = 0;
66   ps->rating_best = 0;
67
68   return (0);
69 } /* }}} int ps_flush */
70
71 /*
72  * Public functions
73  */
74 pop_stats_t *pop_stats_create (void) /* {{{ */
75 {
76   pop_stats_t *ps;
77
78   ps = malloc (sizeof (*ps));
79   if (ps == NULL)
80     return (NULL);
81
82   memset (ps, 0, sizeof (*ps));
83   pthread_mutex_init (&ps->lock, /* attr = */ NULL);
84
85   return (ps);
86 } /* }}} pop_stats_t *pop_stats_create */
87
88 void pop_stats_destroy (pop_stats_t *ps) /* {{{ */
89 {
90   if (ps == NULL)
91     return;
92
93   if (ps->count > 0)
94     ps_flush (ps);
95
96   free (ps->opt_file);
97   free (ps);
98 } /* }}} void pop_stats_destroy */
99
100 int pop_stats_opt_file (pop_stats_t *ps, const char *file) /* {{{ */
101 {
102   char *file_copy;
103
104   if ((ps == NULL) || (file == NULL))
105     return (-EINVAL);
106
107   file_copy = strdup (file);
108   if (file_copy == NULL)
109     return (-ENOMEM);
110
111   if (ps->opt_file != NULL)
112     free (ps->opt_file);
113   ps->opt_file = file_copy;
114
115   return (0);
116 } /* }}} int pop_stats_opt_file */
117
118 int pop_stats_opt_interval (pop_stats_t *ps, long interval) /* {{{ */
119 {
120   if ((ps == NULL) || (interval <= 0))
121     return (-EINVAL);
122
123   ps->opt_interval = interval;
124
125   return (0);
126 } /* }}} int pop_stats_opt_interval */
127
128 int pop_stats_add_rating (pop_stats_t *ps, int rating) /* {{{ */
129 {
130   if (ps == NULL)
131     return (-EINVAL);
132
133   pthread_mutex_lock (&ps->lock);
134
135   if (ps->count == 0)
136   {
137     ps->rating_worst = rating;
138     ps->rating_best = rating;
139   }
140   else
141   {
142     if (ps->rating_worst < rating)
143       ps->rating_worst = rating;
144     if (ps->rating_best > rating)
145       ps->rating_best = rating;
146   }
147
148   ps->rating_sum += ((int64_t) rating);
149   ps->count++;
150
151   if (ps->count >= ps->opt_interval)
152     ps_flush (ps);
153
154   pthread_mutex_unlock (&ps->lock);
155   return (0);
156 } /* }}} int pop_stats_add_rating */
157
158 /* vim: set shiftwidth=2 softtabstop=2 et fdm=marker : */