Mercurial > dovecot > core-2.2
view src/stats/mail-domain.c @ 13294:c51fbe64eae1
Initial implementation of statistics gathering daemon and plugins to feed it.
Some statistics are still missing, some of the code is a bit ugly and the
internal protocols will probably still change.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Fri, 26 Aug 2011 05:15:12 +0300 |
parents | |
children | fd68963a7add |
line wrap: on
line source
/* Copyright (c) 2011 Dovecot authors, see the included COPYING file */ #include "lib.h" #include "ioloop.h" #include "hash.h" #include "llist.h" #include "global-memory.h" #include "stats-settings.h" #include "mail-stats.h" #include "mail-domain.h" static struct hash_table *mail_domains_hash; /* domains are sorted by their last_update timestamp, oldest first */ static struct mail_domain *mail_domains_head, *mail_domains_tail; struct mail_domain *stable_mail_domains; static size_t mail_domain_memsize(const struct mail_domain *domain) { return sizeof(*domain) + strlen(domain->name) + 1; } struct mail_domain *mail_domain_login(const char *name) { struct mail_domain *domain; domain = hash_table_lookup(mail_domains_hash, name); if (domain != NULL) { domain->num_logins++; mail_domain_refresh(domain, NULL); return domain; } domain = i_new(struct mail_domain, 1); domain->name = i_strdup(name); domain->reset_timestamp = ioloop_time; hash_table_insert(mail_domains_hash, domain->name, domain); DLLIST_PREPEND_FULL(&stable_mail_domains, domain, stable_prev, stable_next); DLLIST2_APPEND_FULL(&mail_domains_head, &mail_domains_tail, domain, sorted_prev, sorted_next); domain->num_logins++; domain->last_update = ioloop_time; global_memory_alloc(mail_domain_memsize(domain)); return domain; } struct mail_domain *mail_domain_lookup(const char *name) { return hash_table_lookup(mail_domains_hash, name); } void mail_domain_ref(struct mail_domain *domain) { domain->refcount++; } void mail_domain_unref(struct mail_domain **_domain) { struct mail_domain *domain = *_domain; i_assert(domain->refcount > 0); domain->refcount--; *_domain = NULL; } static void mail_domain_free(struct mail_domain *domain) { i_assert(domain->refcount == 0); i_assert(domain->users == NULL); global_memory_free(mail_domain_memsize(domain)); hash_table_remove(mail_domains_hash, domain->name); DLLIST_REMOVE_FULL(&stable_mail_domains, domain, stable_prev, stable_next); DLLIST2_REMOVE_FULL(&mail_domains_head, &mail_domains_tail, domain, sorted_prev, sorted_next); i_free(domain->name); i_free(domain); } void mail_domain_refresh(struct mail_domain *domain, const struct mail_stats *diff_stats) { if (diff_stats != NULL) mail_stats_add(&domain->stats, diff_stats); domain->last_update = ioloop_time; DLLIST2_REMOVE_FULL(&mail_domains_head, &mail_domains_tail, domain, sorted_prev, sorted_next); DLLIST2_APPEND_FULL(&mail_domains_head, &mail_domains_tail, domain, sorted_prev, sorted_next); } void mail_domains_free_memory(void) { while (mail_domains_head != NULL && mail_domains_head->refcount == 0) { mail_domain_free(mail_domains_head); if (global_used_memory < stats_settings->memory_limit) break; if (ioloop_time - mail_domains_head->last_update < stats_settings->domain_min_time) break; } } void mail_domains_init(void) { mail_domains_hash = hash_table_create(default_pool, default_pool, 0, str_hash, (hash_cmp_callback_t *)strcmp); } void mail_domains_deinit(void) { while (mail_domains_head != NULL) mail_domain_free(mail_domains_head); hash_table_destroy(&mail_domains_hash); }