Request X/Open 7 rather than declaring strdup ourselves.
[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 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
24 #endif
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200809L
27 #endif
28 #ifndef _XOPEN_SOURCE
29 # define _XOPEN_SOURCE 700
30 #endif
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <stdint.h>
35 #include <inttypes.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <pthread.h>
39
40 #include "pop_stats.h"
41
42 struct pop_stats_s /* {{{ */
43 {
44   pthread_mutex_t lock;
45
46   /* Options */
47   char *opt_file;
48   long opt_interval;
49
50   /* Data */
51   long count;
52   int64_t rating_sum;
53   int rating_worst;
54   int rating_best;
55 }; /* }}} struct pop_stats_s */
56
57 /*
58  * Private functions
59  */
60 static int ps_flush (pop_stats_t *ps) /* {{{ */
61 {
62   double average;
63
64   average = ((double) ps->rating_sum) / ((double) ps->count);
65
66   fprintf (stdout, "[STATS] worst:%i average:%g best:%i\n",
67       ps->rating_worst, average, ps->rating_best);
68
69   ps->count = 0;
70   ps->rating_sum = 0;
71   ps->rating_worst = 0;
72   ps->rating_best = 0;
73
74   return (0);
75 } /* }}} int ps_flush */
76
77 /*
78  * Public functions
79  */
80 pop_stats_t *pop_stats_create (void) /* {{{ */
81 {
82   pop_stats_t *ps;
83
84   ps = malloc (sizeof (*ps));
85   if (ps == NULL)
86     return (NULL);
87
88   memset (ps, 0, sizeof (*ps));
89   pthread_mutex_init (&ps->lock, /* attr = */ NULL);
90
91   return (ps);
92 } /* }}} pop_stats_t *pop_stats_create */
93
94 void pop_stats_destroy (pop_stats_t *ps) /* {{{ */
95 {
96   if (ps == NULL)
97     return;
98
99   if (ps->count > 0)
100     ps_flush (ps);
101
102   free (ps->opt_file);
103   free (ps);
104 } /* }}} void pop_stats_destroy */
105
106 int pop_stats_opt_file (pop_stats_t *ps, const char *file) /* {{{ */
107 {
108   char *file_copy;
109
110   if ((ps == NULL) || (file == NULL))
111     return (-EINVAL);
112
113   file_copy = strdup (file);
114   if (file_copy == NULL)
115     return (-ENOMEM);
116
117   if (ps->opt_file != NULL)
118     free (ps->opt_file);
119   ps->opt_file = file_copy;
120
121   return (0);
122 } /* }}} int pop_stats_opt_file */
123
124 int pop_stats_opt_interval (pop_stats_t *ps, long interval) /* {{{ */
125 {
126   if ((ps == NULL) || (interval <= 0))
127     return (-EINVAL);
128
129   ps->opt_interval = interval;
130
131   return (0);
132 } /* }}} int pop_stats_opt_interval */
133
134 int pop_stats_add_rating (pop_stats_t *ps, int rating) /* {{{ */
135 {
136   if (ps == NULL)
137     return (-EINVAL);
138
139   pthread_mutex_lock (&ps->lock);
140
141   if (ps->count == 0)
142   {
143     ps->rating_worst = rating;
144     ps->rating_best = rating;
145   }
146   else
147   {
148     if (ps->rating_worst < rating)
149       ps->rating_worst = rating;
150     if (ps->rating_best > rating)
151       ps->rating_best = rating;
152   }
153
154   ps->rating_sum += ((int64_t) rating);
155   ps->count++;
156
157   if (ps->count >= ps->opt_interval)
158     ps_flush (ps);
159
160   pthread_mutex_unlock (&ps->lock);
161   return (0);
162 } /* }}} int pop_stats_add_rating */
163
164 /* vim: set shiftwidth=2 softtabstop=2 et fdm=marker : */