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>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200809L
29 # define _XOPEN_SOURCE 700
40 #include "pop_stats.h"
42 struct pop_stats_s /* {{{ */
55 }; /* }}} struct pop_stats_s */
60 static int ps_flush (pop_stats_t *ps) /* {{{ */
64 average = ((double) ps->rating_sum) / ((double) ps->count);
66 fprintf (stdout, "[STATS] worst:%i average:%g best:%i\n",
67 ps->rating_worst, average, ps->rating_best);
75 } /* }}} int ps_flush */
80 pop_stats_t *pop_stats_create (void) /* {{{ */
84 ps = malloc (sizeof (*ps));
88 memset (ps, 0, sizeof (*ps));
89 pthread_mutex_init (&ps->lock, /* attr = */ NULL);
92 } /* }}} pop_stats_t *pop_stats_create */
94 void pop_stats_destroy (pop_stats_t *ps) /* {{{ */
104 } /* }}} void pop_stats_destroy */
106 int pop_stats_opt_file (pop_stats_t *ps, const char *file) /* {{{ */
110 if ((ps == NULL) || (file == NULL))
113 file_copy = strdup (file);
114 if (file_copy == NULL)
117 if (ps->opt_file != NULL)
119 ps->opt_file = file_copy;
122 } /* }}} int pop_stats_opt_file */
124 int pop_stats_opt_interval (pop_stats_t *ps, long interval) /* {{{ */
126 if ((ps == NULL) || (interval <= 0))
129 ps->opt_interval = interval;
132 } /* }}} int pop_stats_opt_interval */
134 int pop_stats_add_rating (pop_stats_t *ps, int rating) /* {{{ */
139 pthread_mutex_lock (&ps->lock);
143 ps->rating_worst = rating;
144 ps->rating_best = rating;
148 if (ps->rating_worst < rating)
149 ps->rating_worst = rating;
150 if (ps->rating_best > rating)
151 ps->rating_best = rating;
154 ps->rating_sum += ((int64_t) rating);
157 if (ps->count >= ps->opt_interval)
160 pthread_mutex_unlock (&ps->lock);
162 } /* }}} int pop_stats_add_rating */
164 /* vim: set shiftwidth=2 softtabstop=2 et fdm=marker : */