5 * Copyright (C) 2010 BMW Car IT GmbH. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/types.h>
39 #ifdef TEMP_FAILURE_RETRY
40 #define TFR TEMP_FAILURE_RETRY
45 #define MAGIC 0xFA00B916
48 * Statistics counters are stored into a ring buffer which is stored
52 * The ring buffer is mmap to a file
53 * Initialy only the smallest possible amount of disk space is allocated
54 * The files grow to the configured maximal size
55 * The grows by _SC_PAGESIZE step size
56 * For each service a file is created
57 * Each file has a header where the indexes are stored
60 * Each entry has a timestamp
61 * A flag to mark if the entry is either home (0) or roaming (1) entry
62 * The entries are fixed sized (stats_record)
64 * Ring buffer properties:
65 * There are to indexes 'begin', 'end', 'home' and 'roaming'
66 * 'begin' points to the oldest entry
67 * 'end' points to the newest/current entry
68 * 'home' points to the current home entry
69 * 'roaming' points to the current roaming entry
70 * If 'begin' == 'end' then the buffer is empty
71 * If 'end' + 1 == 'begin then it's full
72 * The ring buffer is valid in the range (begin, end]
73 * If 'home' has the value UINT_MAX', 'home' is invalid
74 * if 'roaming' has the value UINT_MAX', 'roaming' is invalid
75 * 'first' points to the first entry in the ring buffer
76 * 'last' points to the last entry in the ring buffer
79 * Same format as the ring buffer file
80 * For a period of at least 2 months dayly records are keept
81 * If older, then only a monthly record is keept
85 struct stats_file_header {
96 struct connman_stats_data data;
107 struct stats_record *first;
108 struct stats_record *last;
109 struct stats_record *home;
110 struct stats_record *roaming;
114 int account_period_offset;
118 struct stats_file *file;
119 struct stats_record *begin;
120 struct stats_record *end;
121 struct stats_record *it;
124 GHashTable *stats_hash = NULL;
126 static struct stats_file_header *get_hdr(struct stats_file *file)
128 return (struct stats_file_header *)file->addr;
131 static struct stats_record *get_begin(struct stats_file *file)
133 unsigned int off = get_hdr(file)->begin;
135 return (struct stats_record *)(file->addr + off);
138 static struct stats_record *get_end(struct stats_file *file)
140 unsigned int off = get_hdr(file)->end;
142 return (struct stats_record *)(file->addr + off);
145 static struct stats_record *get_home(struct stats_file *file)
147 struct stats_file_header *hdr;
151 if (hdr->home == UINT_MAX)
154 return (struct stats_record *)(file->addr + hdr->home);
157 static struct stats_record *get_roaming(struct stats_file *file)
159 struct stats_file_header *hdr;
163 if (hdr->roaming == UINT_MAX)
166 return (struct stats_record *)(file->addr + hdr->roaming);
169 static void set_end(struct stats_file *file, struct stats_record *end)
171 struct stats_file_header *hdr;
174 hdr->end = (char *)end - file->addr;
177 static void set_home(struct stats_file *file, struct stats_record *home)
179 struct stats_file_header *hdr;
182 hdr->home = (char *)home - file->addr;
185 static void set_roaming(struct stats_file *file, struct stats_record *roaming)
187 struct stats_file_header *hdr;
190 hdr->roaming = (char *)roaming - file->addr;
193 static struct stats_record *get_next(struct stats_file *file,
194 struct stats_record *cur)
198 if (cur > file->last)
204 static struct stats_record *get_iterator_begin(struct stats_file *file)
206 return get_next(file, get_begin(file));
209 static struct stats_record *get_iterator_end(struct stats_file *file)
211 return get_next(file, get_end(file));
214 static void stats_free(gpointer user_data)
216 struct stats_file *file = user_data;
221 msync(file->addr, file->len, MS_SYNC);
223 munmap(file->addr, file->len);
226 TFR(close(file->fd));
229 if (file->history_name != NULL) {
230 g_free(file->history_name);
231 file->history_name = NULL;
234 if (file->name != NULL) {
242 static void update_first(struct stats_file *file)
244 file->first = (struct stats_record *)
245 (file->addr + sizeof(struct stats_file_header));
248 static void update_last(struct stats_file *file)
250 unsigned int max_entries;
252 max_entries = (file->len - sizeof(struct stats_file_header)) /
253 sizeof(struct stats_record);
254 file->last = file->first + max_entries - 1;
257 static void update_home(struct stats_file *file)
259 file->home = get_home(file);
262 static void update_roaming(struct stats_file *file)
264 file->roaming = get_roaming(file);
267 static void stats_file_update_cache(struct stats_file *file)
272 update_roaming(file);
275 static int stats_file_remap(struct stats_file *file, size_t size)
277 size_t page_size, new_size;
281 DBG("file %p size %zu addr %p len %zu", file, size, file->addr,
284 page_size = sysconf(_SC_PAGESIZE);
285 new_size = (size + page_size - 1) & ~(page_size - 1);
287 err = ftruncate(file->fd, new_size);
289 connman_error("ftrunctate error %s for %s",
290 strerror(errno), file->name);
294 if (file->addr == NULL) {
296 * Though the buffer is not shared between processes, we still
297 * have to take MAP_SHARED because MAP_PRIVATE does not
298 * guarantee that writes will hit the file without an explicit
299 * call to munmap or msync. For more details please read the
302 addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE,
303 MAP_SHARED, file->fd, 0);
305 addr = mremap(file->addr, file->len, new_size, MREMAP_MAYMOVE);
308 if (addr == MAP_FAILED) {
309 connman_error("mmap error %s for %s",
310 strerror(errno), file->name);
311 if (errno == EINVAL) {
312 connman_error("%s might be on a file system, such as "
313 "JFFS2, that does not allow shared "
314 "writable mappings.", file->name);
320 file->len = new_size;
322 stats_file_update_cache(file);
327 static int stats_open(struct stats_file *file,
330 DBG("file %p name %s", file, name);
332 file->name = g_strdup(name);
334 file->fd = TFR(open(file->name, O_RDWR | O_CREAT, 0644));
336 connman_error("open error %s for %s",
337 strerror(errno), file->name);
346 static int stats_open_temp(struct stats_file *file)
348 file->name = g_strdup_printf("%s/stats/stats.XXXXXX.tmp",
350 file->fd = g_mkstemp_full(file->name, O_RDWR | O_CREAT, 0644);
352 connman_error("create tempory file error %s for %s",
353 strerror(errno), file->name);
362 static int stats_file_setup(struct stats_file *file)
364 struct stats_file_header *hdr;
369 DBG("file %p fd %d name %s", file, file->fd, file->name);
371 err = fstat(file->fd, &st);
373 connman_error("fstat error %s for %s\n",
374 strerror(errno), file->name);
376 TFR(close(file->fd));
383 size = (size_t)st.st_size;
384 file->max_len = STATS_MAX_FILE_SIZE;
386 if (size < (size_t)sysconf(_SC_PAGESIZE))
387 size = sysconf(_SC_PAGESIZE);
389 err = stats_file_remap(file, size);
391 TFR(close(file->fd));
400 if (hdr->magic != MAGIC ||
401 hdr->begin < sizeof(struct stats_file_header) ||
402 hdr->end < sizeof(struct stats_file_header) ||
403 hdr->home < sizeof(struct stats_file_header) ||
404 hdr->roaming < sizeof(struct stats_file_header) ||
405 hdr->begin > file->len ||
406 hdr->end > file->len) {
408 hdr->begin = sizeof(struct stats_file_header);
409 hdr->end = sizeof(struct stats_file_header);
410 hdr->home = UINT_MAX;
411 hdr->roaming = UINT_MAX;
413 stats_file_update_cache(file);
420 static struct stats_record *get_next_record(struct stats_iter *iter)
422 if (iter->it != iter->end) {
423 struct stats_record *tmp;
426 iter->it = get_next(iter->file, iter->it);
434 static int append_record(struct stats_file *file,
435 struct stats_record *rec)
437 struct stats_record *cur, *next;
440 if (file->last == get_end(file)) {
441 err = stats_file_remap(file, file->len +
442 sysconf(_SC_PAGESIZE));
446 stats_file_update_cache(file);
450 next = get_next(file, cur);
452 memcpy(next, rec, sizeof(struct stats_record));
459 static struct stats_record *process_file(struct stats_iter *iter,
460 struct stats_file *temp_file,
461 struct stats_record *cur,
462 GDate *date_change_step_size,
463 int account_period_offset)
465 struct stats_record *home, *roaming;
466 struct stats_record *next;
472 cur = get_next_record(iter);
473 next = get_next_record(iter);
475 while (next != NULL) {
482 if (cur->roaming == TRUE)
487 g_date_set_time_t(&date_cur, cur->ts);
488 g_date_set_time_t(&date_next, next->ts);
490 if (g_date_compare(&date_cur, date_change_step_size) < 0) {
491 /* month period size */
492 GDateDay day_cur, day_next;
493 GDateMonth month_cur, month_next;
495 month_cur = g_date_get_month(&date_cur);
496 month_next = g_date_get_month(&date_next);
498 day_cur = g_date_get_day(&date_cur);
499 day_next = g_date_get_day(&date_next);
501 if (day_cur == day_next && month_cur != month_next) {
503 } else if (day_cur < account_period_offset &&
504 day_next >= account_period_offset) {
508 /* day period size */
509 if (g_date_days_between(&date_cur, &date_next) > 0)
513 if (append == TRUE) {
515 append_record(temp_file, home);
519 if (roaming != NULL) {
520 append_record(temp_file, roaming);
526 next = get_next_record(iter);
532 static int summarize(struct stats_file *data_file,
533 struct stats_file *history_file,
534 struct stats_file *temp_file)
536 struct stats_iter data_iter;
537 struct stats_iter history_iter;
538 struct stats_record *cur, *next;
540 GDate today, date_change_step_size;
543 * First calculate the date when switch from monthly
544 * accounting period size to daily size
546 g_date_set_time_t(&today, time(NULL));
548 date_change_step_size = today;
549 if (g_date_get_day(&today) - data_file->account_period_offset >= 0)
550 g_date_subtract_months(&date_change_step_size, 2);
552 g_date_subtract_months(&date_change_step_size, 3);
554 g_date_set_day(&date_change_step_size,
555 data_file->account_period_offset);
558 /* Now process history file */
561 if (history_file != NULL) {
562 history_iter.file = history_file;
563 history_iter.begin = get_iterator_begin(history_iter.file);
564 history_iter.end = get_iterator_end(history_iter.file);
565 history_iter.it = history_iter.begin;
567 cur = process_file(&history_iter, temp_file, NULL,
568 &date_change_step_size,
569 data_file->account_period_offset);
572 data_iter.file = data_file;
573 data_iter.begin = get_iterator_begin(data_iter.file);
574 data_iter.end = get_iterator_end(data_iter.file);
575 data_iter.it = data_iter.begin;
578 * Ensure date_file records are newer than the history_file
582 next = get_next_record(&data_iter);
583 while (next != NULL && cur->ts > next->ts)
584 next = get_next_record(&data_iter);
587 /* And finally process the new data records */
588 cur = process_file(&data_iter, temp_file, cur,
589 &date_change_step_size,
590 data_file->account_period_offset);
593 append_record(temp_file, cur);
598 static void stats_file_unmap(struct stats_file *file)
600 msync(file->addr, file->len, MS_SYNC);
601 munmap(file->addr, file->len);
605 static void stats_file_cleanup(struct stats_file *file)
612 static int stats_file_close_swap(struct stats_file *history_file,
613 struct stats_file *temp_file)
617 stats_file_unmap(history_file);
618 stats_file_unmap(temp_file);
620 TFR(close(temp_file->fd));
622 unlink(history_file->name);
624 err = link(temp_file->name, history_file->name);
626 unlink(temp_file->name);
628 TFR(close(history_file->fd));
630 stats_file_cleanup(history_file);
631 stats_file_cleanup(temp_file);
636 static int stats_file_history_update(struct stats_file *data_file)
638 struct stats_file _history_file, *history_file;
639 struct stats_file _temp_file, *temp_file;
642 history_file = &_history_file;
643 temp_file = &_temp_file;
645 bzero(history_file, sizeof(struct stats_file));
646 bzero(temp_file, sizeof(struct stats_file));
648 err = stats_open(history_file, data_file->history_name);
651 stats_file_setup(history_file);
653 err = stats_open_temp(temp_file);
655 stats_free(history_file);
658 stats_file_setup(temp_file);
660 summarize(data_file, history_file, temp_file);
662 err = stats_file_close_swap(history_file, temp_file);
667 int __connman_stats_service_register(struct connman_service *service)
669 struct stats_file *file;
673 DBG("service %p", service);
675 file = g_hash_table_lookup(stats_hash, service);
677 file = g_try_new0(struct stats_file, 1);
681 g_hash_table_insert(stats_hash, service, file);
686 name = g_strdup_printf("%s/stats/%s.data", STORAGEDIR,
687 __connman_service_get_ident(service));
688 file->history_name = g_strdup_printf("%s/stats/%s.history", STORAGEDIR,
689 __connman_service_get_ident(service));
691 /* TODO: Use a global config file instead of hard coded value. */
692 file->account_period_offset = 1;
694 err = stats_open(file, name);
699 err = stats_file_setup(file);
706 g_hash_table_remove(stats_hash, service);
711 void __connman_stats_service_unregister(struct connman_service *service)
713 DBG("service %p", service);
715 g_hash_table_remove(stats_hash, service);
718 int __connman_stats_update(struct connman_service *service,
719 connman_bool_t roaming,
720 struct connman_stats_data *data)
722 struct stats_file *file;
723 struct stats_record *next;
726 file = g_hash_table_lookup(stats_hash, service);
730 if (file->len < file->max_len &&
731 file->last == get_end(file)) {
732 DBG("grow file %s", file->name);
734 err = stats_file_remap(file, file->len + sysconf(_SC_PAGESIZE));
739 next = get_next(file, get_end(file));
741 if (next == get_begin(file)) {
742 DBG("ring buffer is full, update history file");
744 if (stats_file_history_update(file) < 0) {
745 connman_warn("history file update failed %s",
750 next->ts = time(NULL);
751 next->roaming = roaming;
752 memcpy(&next->data, data, sizeof(struct connman_stats_data));
755 set_home(file, next);
757 set_roaming(file, next);
764 int __connman_stats_get(struct connman_service *service,
765 connman_bool_t roaming,
766 struct connman_stats_data *data)
768 struct stats_file *file;
769 struct stats_record *rec;
771 file = g_hash_table_lookup(stats_hash, service);
781 memcpy(data, &rec->data,
782 sizeof(struct connman_stats_data));
788 int __connman_stats_init(void)
792 stats_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
798 void __connman_stats_cleanup(void)
802 g_hash_table_destroy(stats_hash);