2 * libsortnetwork - src/pop_stats.c
3 * Copyright (C) 2009-2010 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <ff at octo.it>
30 #include "pop_stats.h"
32 /* Yes, this is ugly, but the GNU libc doesn't export it with the above flags.
34 char *strdup (const char *s);
36 struct pop_stats_s /* {{{ */
49 }; /* }}} struct pop_stats_s */
54 static int ps_flush (pop_stats_t *ps) /* {{{ */
58 average = ((double) ps->rating_sum) / ((double) ps->count);
60 fprintf (stdout, "[STATS] worst:%i average:%g best:%i\n",
61 ps->rating_worst, average, ps->rating_best);
69 } /* }}} int ps_flush */
74 pop_stats_t *pop_stats_create (void) /* {{{ */
78 ps = malloc (sizeof (*ps));
82 memset (ps, 0, sizeof (*ps));
83 pthread_mutex_init (&ps->lock, /* attr = */ NULL);
86 } /* }}} pop_stats_t *pop_stats_create */
88 void pop_stats_destroy (pop_stats_t *ps) /* {{{ */
98 } /* }}} void pop_stats_destroy */
100 int pop_stats_opt_file (pop_stats_t *ps, const char *file) /* {{{ */
104 if ((ps == NULL) || (file == NULL))
107 file_copy = strdup (file);
108 if (file_copy == NULL)
111 if (ps->opt_file != NULL)
113 ps->opt_file = file_copy;
116 } /* }}} int pop_stats_opt_file */
118 int pop_stats_opt_interval (pop_stats_t *ps, long interval) /* {{{ */
120 if ((ps == NULL) || (interval <= 0))
123 ps->opt_interval = interval;
126 } /* }}} int pop_stats_opt_interval */
128 int pop_stats_add_rating (pop_stats_t *ps, int rating) /* {{{ */
133 pthread_mutex_lock (&ps->lock);
137 ps->rating_worst = rating;
138 ps->rating_best = rating;
142 if (ps->rating_worst < rating)
143 ps->rating_worst = rating;
144 if (ps->rating_best > rating)
145 ps->rating_best = rating;
148 ps->rating_sum += ((int64_t) rating);
151 if (ps->count >= ps->opt_interval)
154 pthread_mutex_unlock (&ps->lock);
156 } /* }}} int pop_stats_add_rating */
158 /* vim: set shiftwidth=2 softtabstop=2 et fdm=marker : */