X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fstats.c;h=1df41f1476062aac82ce2d55c1a0c898a90beef5;hb=f2a9f93bbb2df62e3b3246ea253f8aaae29234d2;hp=7d05b893290efe21c066ed140579f94390c47530;hpb=cf0919476b0a5c6bb779fdfdc62b037d774dfdd2;p=platform%2Fupstream%2Fconnman.git diff --git a/src/stats.c b/src/stats.c old mode 100644 new mode 100755 index 7d05b89..1df41f1 --- a/src/stats.c +++ b/src/stats.c @@ -2,7 +2,7 @@ * * Connection Manager * - * Copyright (C) 2010 BMW Car IT GmbH. All rights reserved. + * Copyright (C) 2010-2014 BMW Car IT GmbH. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as @@ -23,7 +23,7 @@ #include #endif -#define _GNU_SOURCE +#include #include #include #include @@ -32,9 +32,19 @@ #include #include #include +#include #include "connman.h" +#define MODE (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \ + S_IXGRP | S_IROTH | S_IXOTH) + +#ifdef TEMP_FAILURE_RETRY +#define TFR TEMP_FAILURE_RETRY +#else +#define TFR +#endif + #define MAGIC 0xFA00B916 /* @@ -43,7 +53,7 @@ * * File properties: * The ring buffer is mmap to a file - * Initialy only the smallest possible amount of disk space is allocated + * Initially only the smallest possible amount of disk space is allocated * The files grow to the configured maximal size * The grows by _SC_PAGESIZE step size * For each service a file is created @@ -67,8 +77,14 @@ * if 'roaming' has the value UINT_MAX', 'roaming' is invalid * 'first' points to the first entry in the ring buffer * 'last' points to the last entry in the ring buffer + * + * History file: + * Same format as the ring buffer file + * For a period of at least 2 months daily records are kept + * If older, then only a monthly record is kept */ + struct stats_file_header { unsigned int magic; unsigned int begin; @@ -95,9 +111,20 @@ struct stats_file { struct stats_record *last; struct stats_record *home; struct stats_record *roaming; + + /* history */ + char *history_name; + int account_period_offset; +}; + +struct stats_iter { + struct stats_file *file; + struct stats_record *begin; + struct stats_record *end; + struct stats_record *it; }; -GHashTable *stats_hash = NULL; +static GHashTable *stats_hash = NULL; static struct stats_file_header *get_hdr(struct stats_file *file) { @@ -142,14 +169,6 @@ static struct stats_record *get_roaming(struct stats_file *file) return (struct stats_record *)(file->addr + hdr->roaming); } -static void set_begin(struct stats_file *file, struct stats_record *begin) -{ - struct stats_file_header *hdr; - - hdr = get_hdr(file); - hdr->begin = (char *)begin - file->addr; -} - static void set_end(struct stats_file *file, struct stats_record *end) { struct stats_file_header *hdr; @@ -185,10 +204,23 @@ static struct stats_record *get_next(struct stats_file *file, return cur; } +static struct stats_record *get_iterator_begin(struct stats_file *file) +{ + return get_next(file, get_begin(file)); +} + +static struct stats_record *get_iterator_end(struct stats_file *file) +{ + return get_next(file, get_end(file)); +} + static void stats_free(gpointer user_data) { struct stats_file *file = user_data; + if (!file) + return; + msync(file->addr, file->len, MS_SYNC); munmap(file->addr, file->len); @@ -197,21 +229,13 @@ static void stats_free(gpointer user_data) close(file->fd); file->fd = -1; - g_free(file->name); - g_free(file); -} - -static int stats_create(struct connman_service *service) -{ - struct stats_file *file; - - file = g_try_new0(struct stats_file, 1); - if (file == NULL) - return -ENOMEM; + g_free(file->history_name); + file->history_name = NULL; - g_hash_table_insert(stats_hash, service, file); + g_free(file->name); + file->name = NULL; - return 0; + g_free(file); } static void update_first(struct stats_file *file) @@ -253,6 +277,9 @@ static int stats_file_remap(struct stats_file *file, size_t size) void *addr; int err; + DBG("file %p size %zu addr %p len %zu", file, size, file->addr, + file->len); + page_size = sysconf(_SC_PAGESIZE); new_size = (size + page_size - 1) & ~(page_size - 1); @@ -263,7 +290,14 @@ static int stats_file_remap(struct stats_file *file, size_t size) return -errno; } - if (file->addr == NULL) { + if (!file->addr) { + /* + * Though the buffer is not shared between processes, we still + * have to take MAP_SHARED because MAP_PRIVATE does not + * guarantee that writes will hit the file without an explicit + * call to munmap or msync. For more details please read the + * mmap man pages. + */ addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE, MAP_SHARED, file->fd, 0); } else { @@ -273,6 +307,11 @@ static int stats_file_remap(struct stats_file *file, size_t size) if (addr == MAP_FAILED) { connman_error("mmap error %s for %s", strerror(errno), file->name); + if (errno == EINVAL) { + connman_error("%s might be on a file system, such as " + "JFFS2, that does not allow shared " + "writable mappings.", file->name); + } return -errno; } @@ -284,43 +323,78 @@ static int stats_file_remap(struct stats_file *file, size_t size) return 0; } -static int stats_open(struct connman_service *service, - struct stats_file *file) +static int stats_open(struct stats_file *file, + const char *name) { - struct stat st; - int err; - size_t size; - struct stats_file_header *hdr; - connman_bool_t new_file = FALSE; + DBG("file %p name %s", file, name); - file->name = g_strdup_printf("%s/stats/%s.data", STORAGEDIR, - __connman_service_get_ident(service)); + file->name = g_strdup(name); - err = stat(file->name, &st); - if (err < 0) { - /* according documentation the only possible error is ENOENT */ - st.st_size = 0; - new_file = TRUE; + file->fd = TFR(open(file->name, O_RDWR | O_CREAT | O_CLOEXEC, 0644)); + if (file->fd < 0) { + connman_error("open error %s for %s", + strerror(errno), file->name); + g_free(file->name); + file->name = NULL; + return -errno; } - file->fd = open(file->name, O_RDWR | O_CREAT, 0644); + return 0; +} +static int stats_open_temp(struct stats_file *file) +{ + file->name = g_strdup_printf("%s/stats.XXXXXX.tmp", + STORAGEDIR); + file->fd = g_mkstemp_full(file->name, O_RDWR | O_CREAT, 0644); if (file->fd < 0) { - connman_error("open error %s for %s", + connman_error("create temporary file error %s for %s", strerror(errno), file->name); + g_free(file->name); + file->name = NULL; return -errno; } + return 0; +} + +static int stats_file_setup(struct stats_file *file) +{ + struct stats_file_header *hdr; + struct stat st; + size_t size = 0; + int err; + + DBG("file %p fd %d name %s", file, file->fd, file->name); + + err = fstat(file->fd, &st); + if (err < 0) { + connman_error("fstat error %s for %s\n", + strerror(errno), file->name); + + close(file->fd); + file->fd = -1; + g_free(file->name); + file->name = NULL; + + return -errno; + } + + size = (size_t)st.st_size; file->max_len = STATS_MAX_FILE_SIZE; - if (st.st_size < sysconf(_SC_PAGESIZE)) + if (size < (size_t)sysconf(_SC_PAGESIZE)) size = sysconf(_SC_PAGESIZE); - else - size = st.st_size; err = stats_file_remap(file, size); - if (err < 0) + if (err < 0) { + close(file->fd); + file->fd = -1; + g_free(file->name); + file->name = NULL; + return err; + } hdr = get_hdr(file); @@ -331,16 +405,6 @@ static int stats_open(struct connman_service *service, hdr->roaming < sizeof(struct stats_file_header) || hdr->begin > file->len || hdr->end > file->len) { - if (new_file == FALSE) { - /* - * A newly created file can't have a correct - * header so we only warn if the file already - * existed and doesn't have a proper - * header. - */ - connman_warn("invalid file header for %s", file->name); - } - hdr->magic = MAGIC; hdr->begin = sizeof(struct stats_file_header); hdr->end = sizeof(struct stats_file_header); @@ -353,28 +417,315 @@ static int stats_open(struct connman_service *service, return 0; } + +static struct stats_record *get_next_record(struct stats_iter *iter) +{ + if (iter->it != iter->end) { + struct stats_record *tmp; + + tmp = iter->it; + iter->it = get_next(iter->file, iter->it); + + return tmp; + } + + return NULL; +} + +static int append_record(struct stats_file *file, + struct stats_record *rec) +{ + struct stats_record *cur, *next; + int err; + + if (file->last == get_end(file)) { + err = stats_file_remap(file, file->len + + sysconf(_SC_PAGESIZE)); + if (err < 0) + return err; + + stats_file_update_cache(file); + } + + cur = get_end(file); + next = get_next(file, cur); + + memcpy(next, rec, sizeof(struct stats_record)); + + set_end(file, next); + + return 0; +} + +static struct stats_record *process_file(struct stats_iter *iter, + struct stats_file *temp_file, + struct stats_record *cur, + GDate *date_change_step_size, + int account_period_offset) +{ + struct stats_record *home, *roaming; + struct stats_record *next; + + home = NULL; + roaming = NULL; + + if (!cur) + cur = get_next_record(iter); + next = get_next_record(iter); + + while (next) { + GDate date_cur; + GDate date_next; + bool append; + + append = false; + + if (cur->roaming) + roaming = cur; + else + home = cur; + + g_date_set_time_t(&date_cur, cur->ts); + g_date_set_time_t(&date_next, next->ts); + + if (g_date_compare(&date_cur, date_change_step_size) < 0) { + /* month period size */ + GDateDay day_cur, day_next; + GDateMonth month_cur, month_next; + + month_cur = g_date_get_month(&date_cur); + month_next = g_date_get_month(&date_next); + + day_cur = g_date_get_day(&date_cur); + day_next = g_date_get_day(&date_next); + + if (day_cur == day_next && month_cur != month_next) { + append = true; + } else if (day_cur < account_period_offset && + day_next >= account_period_offset) { + append = true; + } + } else { + /* day period size */ + if (g_date_days_between(&date_cur, &date_next) > 0) + append = true; + } + + if (append) { + if (home) { + append_record(temp_file, home); + home = NULL; + } + + if (roaming) { + append_record(temp_file, roaming); + roaming = NULL; + } + } + + cur = next; + next = get_next_record(iter); + } + + return cur; +} + +static int summarize(struct stats_file *data_file, + struct stats_file *history_file, + struct stats_file *temp_file) +{ + struct stats_iter data_iter; + struct stats_iter history_iter; + struct stats_record *cur, *next; + + GDate today, date_change_step_size; + + /* + * First calculate the date when switch from monthly + * accounting period size to daily size + */ + g_date_set_time_t(&today, time(NULL)); + + date_change_step_size = today; + if (g_date_get_day(&today) - data_file->account_period_offset >= 0) + g_date_subtract_months(&date_change_step_size, 2); + else + g_date_subtract_months(&date_change_step_size, 3); + + g_date_set_day(&date_change_step_size, + data_file->account_period_offset); + + + /* Now process history file */ + cur = NULL; + + if (history_file) { + history_iter.file = history_file; + history_iter.begin = get_iterator_begin(history_iter.file); + history_iter.end = get_iterator_end(history_iter.file); + history_iter.it = history_iter.begin; + + cur = process_file(&history_iter, temp_file, NULL, + &date_change_step_size, + data_file->account_period_offset); + } + + data_iter.file = data_file; + data_iter.begin = get_iterator_begin(data_iter.file); + data_iter.end = get_iterator_end(data_iter.file); + data_iter.it = data_iter.begin; + + /* + * Ensure date_file records are newer than the history_file + * record + */ + if (cur) { + next = get_next_record(&data_iter); + while (next && cur->ts > next->ts) + next = get_next_record(&data_iter); + } + + /* And finally process the new data records */ + cur = process_file(&data_iter, temp_file, cur, + &date_change_step_size, + data_file->account_period_offset); + + if (cur) + append_record(temp_file, cur); + + return 0; +} + +static void stats_file_unmap(struct stats_file *file) +{ + msync(file->addr, file->len, MS_SYNC); + munmap(file->addr, file->len); + file->addr = NULL; +} + +static void stats_file_cleanup(struct stats_file *file) +{ + file->fd = -1; + g_free(file->name); + file->name = NULL; +} + +static int stats_file_close_swap(struct stats_file *history_file, + struct stats_file *temp_file) +{ + int err; + + stats_file_unmap(history_file); + stats_file_unmap(temp_file); + + close(temp_file->fd); + + unlink(history_file->name); + + err = link(temp_file->name, history_file->name); + + unlink(temp_file->name); + + close(history_file->fd); + + stats_file_cleanup(history_file); + stats_file_cleanup(temp_file); + + return err; +} + +static int stats_file_history_update(struct stats_file *data_file) +{ + struct stats_file _history_file, *history_file; + struct stats_file _temp_file, *temp_file; + int err; + + history_file = &_history_file; + temp_file = &_temp_file; + + bzero(history_file, sizeof(struct stats_file)); + bzero(temp_file, sizeof(struct stats_file)); + + history_file->fd = -1; + temp_file->fd = -1; + + err = stats_open(history_file, data_file->history_name); + if (err < 0) + return err; + stats_file_setup(history_file); + + err = stats_open_temp(temp_file); + if (err < 0) { + stats_free(history_file); + return err; + } + stats_file_setup(temp_file); + + summarize(data_file, history_file, temp_file); + + err = stats_file_close_swap(history_file, temp_file); + + return err; +} + int __connman_stats_service_register(struct connman_service *service) { struct stats_file *file; + char *name, *dir; int err; DBG("service %p", service); + dir = g_strdup_printf("%s/%s", STORAGEDIR, + connman_service_get_identifier(service)); + + /* If the dir doesn't exist, create it */ + if (!g_file_test(dir, G_FILE_TEST_IS_DIR)) { + if (mkdir(dir, MODE) < 0) { + if (errno != EEXIST) { + g_free(dir); + + err = -errno; + goto err; + } + } + } + + g_free(dir); file = g_hash_table_lookup(stats_hash, service); - if (file == NULL) { - err = stats_create(service); + if (!file) { + file = g_try_new0(struct stats_file, 1); + if (!file) + return -ENOMEM; - if (err < 0) - return err; + file->fd = -1; - file = g_hash_table_lookup(stats_hash, service); + g_hash_table_insert(stats_hash, service, file); } else { return -EALREADY; } - err = stats_open(service, file); + name = g_strdup_printf("%s/%s/data", STORAGEDIR, + connman_service_get_identifier(service)); + file->history_name = g_strdup_printf("%s/%s/history", STORAGEDIR, + connman_service_get_identifier(service)); + + /* TODO: Use a global config file instead of hard coded value. */ + file->account_period_offset = 1; + + err = stats_open(file, name); + g_free(name); + if (err < 0) + goto err; + + err = stats_file_setup(file); if (err < 0) - g_hash_table_remove(stats_hash, service); + goto err; + + return 0; + +err: + g_hash_table_remove(stats_hash, service); return err; } @@ -387,7 +738,7 @@ void __connman_stats_service_unregister(struct connman_service *service) } int __connman_stats_update(struct connman_service *service, - connman_bool_t roaming, + bool roaming, struct connman_stats_data *data) { struct stats_file *file; @@ -395,7 +746,7 @@ int __connman_stats_update(struct connman_service *service, int err; file = g_hash_table_lookup(stats_hash, service); - if (file == NULL) + if (!file) return -EEXIST; if (file->len < file->max_len && @@ -409,14 +760,20 @@ int __connman_stats_update(struct connman_service *service, next = get_next(file, get_end(file)); - if (next == get_begin(file)) - set_begin(file, get_next(file, next)); + if (next == get_begin(file)) { + DBG("ring buffer is full, update history file"); + + if (stats_file_history_update(file) < 0) { + connman_warn("history file update failed %s", + file->history_name); + } + } next->ts = time(NULL); next->roaming = roaming; memcpy(&next->data, data, sizeof(struct connman_stats_data)); - if (roaming != TRUE) + if (!roaming) set_home(file, next); else set_roaming(file, next); @@ -427,22 +784,22 @@ int __connman_stats_update(struct connman_service *service, } int __connman_stats_get(struct connman_service *service, - connman_bool_t roaming, + bool roaming, struct connman_stats_data *data) { struct stats_file *file; struct stats_record *rec; file = g_hash_table_lookup(stats_hash, service); - if (file == NULL) + if (!file) return -EEXIST; - if (roaming != TRUE) + if (!roaming) rec = file->home; else rec = file->roaming; - if (rec != NULL) { + if (rec) { memcpy(data, &rec->data, sizeof(struct connman_stats_data)); }