From: Florian Forster Date: Fri, 4 Sep 2009 08:15:31 +0000 (+0200) Subject: pop_stats: Add module for population statistics. X-Git-Tag: v1.0.0~71 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=b120c6c380911d59608e5e639e05e7e6bb8c9de2;p=sort-networks.git pop_stats: Add module for population statistics. Actually more offspring statistics, though. --- diff --git a/src/Makefile b/src/Makefile index ea72c21..efa38c9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -29,6 +29,7 @@ sn_random.o: sn_random.c sn_random.h sn_stage.o: sn_stage.c sn_stage.h sn_comparator.h +pop_stats.o: pop_stats.c pop_stats.h sn-apply: sn-apply.c sn_network.o sn_stage.o sn_comparator.o sn_random.o @@ -38,7 +39,7 @@ sn-cut: sn-cut.c sn_network.o sn_stage.o sn_comparator.o sn_random.o sn-evolution: CFLAGS += $(POPULATION_CFLAGS) sn-evolution: LDFLAGS += $(POPULATION_LDFLAGS) -sn-evolution: sn-evolution.c sn_network.o sn_stage.o sn_comparator.o sn_population.o sn_random.o +sn-evolution: sn-evolution.c sn_network.o sn_stage.o sn_comparator.o sn_population.o sn_random.o pop_stats.o sn-evolution2: CFLAGS += $(POPULATION_CFLAGS) sn-evolution2: LDFLAGS += $(POPULATION_LDFLAGS) -lm diff --git a/src/pop_stats.c b/src/pop_stats.c new file mode 100644 index 0000000..bdad39b --- /dev/null +++ b/src/pop_stats.c @@ -0,0 +1,158 @@ +/** + * collectd - src/pop_stats.c + * Copyright (C) 2009 Florian octo Forster + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; only version 2 of the License is applicable. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * Florian octo Forster + **/ + +#include +#include +#include +#include +#include +#include +#include + +#include "pop_stats.h" + +/* Yes, this is ugly, but the GNU libc doesn't export it with the above flags. + * */ +char *strdup (const char *s); + +struct pop_stats_s /* {{{ */ +{ + pthread_mutex_t lock; + + /* Options */ + char *opt_file; + long opt_interval; + + /* Data */ + long count; + int64_t rating_sum; + int rating_worst; + int rating_best; +}; /* }}} struct pop_stats_s */ + +/* + * Private functions + */ +static int ps_flush (pop_stats_t *ps) /* {{{ */ +{ + double average; + + average = ((double) ps->rating_sum) / ((double) ps->count); + + fprintf (stdout, "[STATS] worst:%i average:%g best:%i\n", + ps->rating_worst, average, ps->rating_best); + + ps->count = 0; + ps->rating_sum = 0; + ps->rating_worst = 0; + ps->rating_best = 0; + + return (0); +} /* }}} int ps_flush */ + +/* + * Public functions + */ +pop_stats_t *pop_stats_create (void) /* {{{ */ +{ + pop_stats_t *ps; + + ps = malloc (sizeof (*ps)); + if (ps == NULL) + return (NULL); + + memset (ps, 0, sizeof (*ps)); + pthread_mutex_init (&ps->lock, /* attr = */ NULL); + + return (ps); +} /* }}} pop_stats_t *pop_stats_create */ + +void pop_stats_destroy (pop_stats_t *ps) /* {{{ */ +{ + if (ps == NULL) + return; + + if (ps->count > 0) + ps_flush (ps); + + free (ps->opt_file); + free (ps); +} /* }}} void pop_stats_destroy */ + +int pop_stats_opt_file (pop_stats_t *ps, const char *file) /* {{{ */ +{ + char *file_copy; + + if ((ps == NULL) || (file == NULL)) + return (-EINVAL); + + file_copy = strdup (file); + if (file_copy == NULL) + return (-ENOMEM); + + if (ps->opt_file != NULL) + free (ps->opt_file); + ps->opt_file = file_copy; + + return (0); +} /* }}} int pop_stats_opt_file */ + +int pop_stats_opt_interval (pop_stats_t *ps, long interval) /* {{{ */ +{ + if ((ps == NULL) || (interval <= 0)) + return (-EINVAL); + + ps->opt_interval = interval; + + return (0); +} /* }}} int pop_stats_opt_interval */ + +int pop_stats_add_rating (pop_stats_t *ps, int rating) /* {{{ */ +{ + if (ps == NULL) + return (-EINVAL); + + pthread_mutex_lock (&ps->lock); + + if (ps->count == 0) + { + ps->rating_worst = rating; + ps->rating_best = rating; + } + else + { + if (ps->rating_worst < rating) + ps->rating_worst = rating; + if (ps->rating_best > rating) + ps->rating_best = rating; + } + + ps->rating_sum += ((int64_t) rating); + ps->count++; + + if (ps->count >= ps->opt_interval) + ps_flush (ps); + + pthread_mutex_unlock (&ps->lock); + return (0); +} /* }}} int pop_stats_add_rating */ + +/* vim: set shiftwidth=2 softtabstop=2 et fdm=marker : */ diff --git a/src/pop_stats.h b/src/pop_stats.h new file mode 100644 index 0000000..3108ab6 --- /dev/null +++ b/src/pop_stats.h @@ -0,0 +1,38 @@ +/** + * collectd - src/pop_stats.h + * Copyright (C) 2009 Florian octo Forster + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; only version 2 of the License is applicable. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * Florian octo Forster + **/ + +#ifndef POP_STATS_H +#define POP_STATS_H 1 + +struct pop_stats_s; +typedef struct pop_stats_s pop_stats_t; + +pop_stats_t *pop_stats_create (void); +void pop_stats_destroy (pop_stats_t *); + +int pop_stats_opt_file (pop_stats_t *, const char *file); +int pop_stats_opt_interval (pop_stats_t *, long interval); + +int pop_stats_add_rating (pop_stats_t *, int); + +#endif /* POP_STATS_H */ + +/* vim: set shiftwidth=2 softtabstop=2 et : */