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
28 #include <sys/types.h>
38 #ifdef TEMP_FAILURE_RETRY
39 #define TFR TEMP_FAILURE_RETRY
44 #define MAGIC 0xFA00B916
47 * Statistics counters are stored into a ring buffer which is stored
51 * The ring buffer is mmap to a file
52 * Initialy only the smallest possible amount of disk space is allocated
53 * The files grow to the configured maximal size
54 * The grows by _SC_PAGESIZE step size
55 * For each service a file is created
56 * Each file has a header where the indexes are stored
59 * Each entry has a timestamp
60 * A flag to mark if the entry is either home (0) or roaming (1) entry
61 * The entries are fixed sized (stats_record)
63 * Ring buffer properties:
64 * There are to indexes 'begin', 'end', 'home' and 'roaming'
65 * 'begin' points to the oldest entry
66 * 'end' points to the newest/current entry
67 * 'home' points to the current home entry
68 * 'roaming' points to the current roaming entry
69 * If 'begin' == 'end' then the buffer is empty
70 * If 'end' + 1 == 'begin then it's full
71 * The ring buffer is valid in the range (begin, end]
72 * If 'home' has the value UINT_MAX', 'home' is invalid
73 * if 'roaming' has the value UINT_MAX', 'roaming' is invalid
74 * 'first' points to the first entry in the ring buffer
75 * 'last' points to the last entry in the ring buffer
78 * Same format as the ring buffer file
79 * For a period of at least 2 months dayly records are keept
80 * If older, then only a monthly record is keept
84 struct stats_file_header {
95 struct connman_stats_data data;
106 struct stats_record *first;
107 struct stats_record *last;
108 struct stats_record *home;
109 struct stats_record *roaming;
113 int account_period_offset;
117 struct stats_file *file;
118 struct stats_record *begin;
119 struct stats_record *end;
120 struct stats_record *it;
123 GHashTable *stats_hash = NULL;
125 static struct stats_file_header *get_hdr(struct stats_file *file)
127 return (struct stats_file_header *)file->addr;
130 static struct stats_record *get_begin(struct stats_file *file)
132 unsigned int off = get_hdr(file)->begin;
134 return (struct stats_record *)(file->addr + off);
137 static struct stats_record *get_end(struct stats_file *file)
139 unsigned int off = get_hdr(file)->end;
141 return (struct stats_record *)(file->addr + off);
144 static struct stats_record *get_home(struct stats_file *file)
146 struct stats_file_header *hdr;
150 if (hdr->home == UINT_MAX)
153 return (struct stats_record *)(file->addr + hdr->home);
156 static struct stats_record *get_roaming(struct stats_file *file)
158 struct stats_file_header *hdr;
162 if (hdr->roaming == UINT_MAX)
165 return (struct stats_record *)(file->addr + hdr->roaming);
168 static void set_end(struct stats_file *file, struct stats_record *end)
170 struct stats_file_header *hdr;
173 hdr->end = (char *)end - file->addr;
176 static void set_home(struct stats_file *file, struct stats_record *home)
178 struct stats_file_header *hdr;
181 hdr->home = (char *)home - file->addr;
184 static void set_roaming(struct stats_file *file, struct stats_record *roaming)
186 struct stats_file_header *hdr;
189 hdr->roaming = (char *)roaming - file->addr;
192 static struct stats_record *get_next(struct stats_file *file,
193 struct stats_record *cur)
197 if (cur > file->last)
203 static struct stats_record *get_iterator_begin(struct stats_file *file)
205 return get_next(file, get_begin(file));
208 static struct stats_record *get_iterator_end(struct stats_file *file)
210 return get_next(file, get_end(file));
213 static void stats_free(gpointer user_data)
215 struct stats_file *file = user_data;
217 msync(file->addr, file->len, MS_SYNC);
219 munmap(file->addr, file->len);
222 TFR(close(file->fd));
225 if (file->history_name != NULL) {
226 g_free(file->history_name);
227 file->history_name = NULL;
230 if (file->name != NULL) {
239 static void update_first(struct stats_file *file)
241 file->first = (struct stats_record *)
242 (file->addr + sizeof(struct stats_file_header));
245 static void update_last(struct stats_file *file)
247 unsigned int max_entries;
249 max_entries = (file->len - sizeof(struct stats_file_header)) /
250 sizeof(struct stats_record);
251 file->last = file->first + max_entries - 1;
254 static void update_home(struct stats_file *file)
256 file->home = get_home(file);
259 static void update_roaming(struct stats_file *file)
261 file->roaming = get_roaming(file);
264 static void stats_file_update_cache(struct stats_file *file)
269 update_roaming(file);
272 static int stats_file_remap(struct stats_file *file, size_t size)
274 size_t page_size, new_size;
278 page_size = sysconf(_SC_PAGESIZE);
279 new_size = (size + page_size - 1) & ~(page_size - 1);
281 err = ftruncate(file->fd, new_size);
283 connman_error("ftrunctate error %s for %s",
284 strerror(errno), file->name);
288 if (file->addr == NULL) {
289 addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE,
290 MAP_SHARED, file->fd, 0);
292 addr = mremap(file->addr, file->len, new_size, MREMAP_MAYMOVE);
295 if (addr == MAP_FAILED) {
296 connman_error("mmap error %s for %s",
297 strerror(errno), file->name);
302 file->len = new_size;
304 stats_file_update_cache(file);
309 static int stats_open(struct stats_file *file,
312 file->name = g_strdup(name);
314 file->fd = TFR(open(file->name, O_RDWR | O_CREAT, 0644));
316 connman_error("open error %s for %s",
317 strerror(errno), file->name);
325 static int stats_open_temp(struct stats_file *file)
327 file->name = g_strdup_printf("%s/stats/stats.XXXXXX.tmp",
329 file->fd = g_mkstemp_full(file->name, O_RDWR | O_CREAT, 0644);
331 connman_error("create tempory file error %s for %s",
332 strerror(errno), file->name);
340 static int stats_file_setup(struct stats_file *file)
342 struct stats_file_header *hdr;
347 err = fstat(file->fd, &st);
349 connman_error("fstat error %s for %s\n",
350 strerror(errno), file->name);
352 TFR(close(file->fd));
358 size = (size_t)st.st_size;
359 file->max_len = STATS_MAX_FILE_SIZE;
361 if (size < (size_t)sysconf(_SC_PAGESIZE))
362 size = sysconf(_SC_PAGESIZE);
364 err = stats_file_remap(file, size);
366 TFR(close(file->fd));
374 if (hdr->magic != MAGIC ||
375 hdr->begin < sizeof(struct stats_file_header) ||
376 hdr->end < sizeof(struct stats_file_header) ||
377 hdr->home < sizeof(struct stats_file_header) ||
378 hdr->roaming < sizeof(struct stats_file_header) ||
379 hdr->begin > file->len ||
380 hdr->end > file->len) {
382 hdr->begin = sizeof(struct stats_file_header);
383 hdr->end = sizeof(struct stats_file_header);
384 hdr->home = UINT_MAX;
385 hdr->roaming = UINT_MAX;
387 stats_file_update_cache(file);
394 static struct stats_record *get_next_record(struct stats_iter *iter)
396 if (iter->it != iter->end) {
397 struct stats_record *tmp;
400 iter->it = get_next(iter->file, iter->it);
408 static int append_record(struct stats_file *file,
409 struct stats_record *rec)
411 struct stats_record *cur, *next;
414 if (file->last == get_end(file)) {
415 err = stats_file_remap(file, file->len +
416 sysconf(_SC_PAGESIZE));
420 stats_file_update_cache(file);
424 next = get_next(file, cur);
426 memcpy(next, rec, sizeof(struct stats_record));
433 static struct stats_record *process_file(struct stats_iter *iter,
434 struct stats_file *temp_file,
435 struct stats_record *cur,
436 GDate *date_change_step_size,
437 int account_period_offset)
439 struct stats_record *home, *roaming;
440 struct stats_record *next;
446 cur = get_next_record(iter);
447 next = get_next_record(iter);
449 while (next != NULL) {
456 if (cur->roaming == TRUE)
461 g_date_set_time_t(&date_cur, cur->ts);
462 g_date_set_time_t(&date_next, next->ts);
464 if (g_date_compare(&date_cur, date_change_step_size) < 0) {
465 /* month period size */
466 GDateDay day_cur, day_next;
467 GDateMonth month_cur, month_next;
469 month_cur = g_date_get_month(&date_cur);
470 month_next = g_date_get_month(&date_next);
472 day_cur = g_date_get_day(&date_cur);
473 day_next = g_date_get_day(&date_next);
475 if (day_cur == day_next && month_cur != month_next) {
477 } else if (day_cur < account_period_offset &&
478 day_next >= account_period_offset) {
482 /* day period size */
483 if (g_date_days_between(&date_cur, &date_next) > 0)
487 if (append == TRUE) {
489 append_record(temp_file, home);
493 if (roaming != NULL) {
494 append_record(temp_file, roaming);
500 next = get_next_record(iter);
506 static int summarize(struct stats_file *data_file,
507 struct stats_file *history_file,
508 struct stats_file *temp_file)
510 struct stats_iter data_iter;
511 struct stats_iter history_iter;
512 struct stats_record *cur, *next;
514 GDate today, date_change_step_size;
517 * First calculate the date when switch from monthly
518 * accounting period size to daily size
520 g_date_set_time_t(&today, time(NULL));
522 date_change_step_size = today;
523 if (g_date_get_day(&today) - data_file->account_period_offset >= 0)
524 g_date_subtract_months(&date_change_step_size, 2);
526 g_date_subtract_months(&date_change_step_size, 3);
528 g_date_set_day(&date_change_step_size,
529 data_file->account_period_offset);
532 /* Now process history file */
535 if (history_file != NULL) {
536 history_iter.file = history_file;
537 history_iter.begin = get_iterator_begin(history_iter.file);
538 history_iter.end = get_iterator_end(history_iter.file);
539 history_iter.it = history_iter.begin;
541 cur = process_file(&history_iter, temp_file, NULL,
542 &date_change_step_size,
543 data_file->account_period_offset);
546 data_iter.file = data_file;
547 data_iter.begin = get_iterator_begin(data_iter.file);
548 data_iter.end = get_iterator_end(data_iter.file);
549 data_iter.it = data_iter.begin;
552 * Ensure date_file records are newer than the history_file
556 next = get_next_record(&data_iter);
557 while (next != NULL && cur->ts > next->ts)
558 next = get_next_record(&data_iter);
561 /* And finally process the new data records */
562 cur = process_file(&data_iter, temp_file, cur,
563 &date_change_step_size,
564 data_file->account_period_offset);
567 append_record(temp_file, cur);
572 static void stats_file_unmap(struct stats_file *file)
574 msync(file->addr, file->len, MS_SYNC);
575 munmap(file->addr, file->len);
579 static void stats_file_cleanup(struct stats_file *file)
585 static int stats_file_close_swap(struct stats_file *history_file,
586 struct stats_file *temp_file)
590 stats_file_unmap(history_file);
591 stats_file_unmap(temp_file);
593 TFR(close(temp_file->fd));
595 unlink(history_file->name);
597 err = link(temp_file->name, history_file->name);
599 unlink(temp_file->name);
601 TFR(close(history_file->fd));
603 stats_file_cleanup(history_file);
604 stats_file_cleanup(temp_file);
609 static int stats_file_history_update(struct stats_file *data_file)
611 struct stats_file _history_file, *history_file;
612 struct stats_file _temp_file, *temp_file;
615 history_file = &_history_file;
616 temp_file = &_temp_file;
618 bzero(history_file, sizeof(struct stats_file));
619 bzero(temp_file, sizeof(struct stats_file));
621 err = stats_open(history_file, data_file->history_name);
624 stats_file_setup(history_file);
626 err = stats_open_temp(temp_file);
628 stats_free(history_file);
631 stats_file_setup(temp_file);
633 summarize(data_file, history_file, temp_file);
635 err = stats_file_close_swap(history_file, temp_file);
640 int __connman_stats_service_register(struct connman_service *service)
642 struct stats_file *file;
646 DBG("service %p", service);
648 file = g_hash_table_lookup(stats_hash, service);
650 file = g_try_new0(struct stats_file, 1);
654 g_hash_table_insert(stats_hash, service, file);
659 name = g_strdup_printf("%s/stats/%s.data", STORAGEDIR,
660 __connman_service_get_ident(service));
661 file->history_name = g_strdup_printf("%s/stats/%s.history", STORAGEDIR,
662 __connman_service_get_ident(service));
664 /* TODO: Use a global config file instead of hard coded value. */
665 file->account_period_offset = 1;
667 err = stats_open(file, name);
672 err = stats_file_setup(file);
679 g_hash_table_remove(stats_hash, service);
684 void __connman_stats_service_unregister(struct connman_service *service)
686 DBG("service %p", service);
688 g_hash_table_remove(stats_hash, service);
691 int __connman_stats_update(struct connman_service *service,
692 connman_bool_t roaming,
693 struct connman_stats_data *data)
695 struct stats_file *file;
696 struct stats_record *next;
699 file = g_hash_table_lookup(stats_hash, service);
703 if (file->len < file->max_len &&
704 file->last == get_end(file)) {
705 DBG("grow file %s", file->name);
707 err = stats_file_remap(file, file->len + sysconf(_SC_PAGESIZE));
712 next = get_next(file, get_end(file));
714 if (next == get_begin(file)) {
715 DBG("ring buffer is full, update history file");
717 if (stats_file_history_update(file) < 0) {
718 connman_warn("history file update failed %s",
723 next->ts = time(NULL);
724 next->roaming = roaming;
725 memcpy(&next->data, data, sizeof(struct connman_stats_data));
728 set_home(file, next);
730 set_roaming(file, next);
737 int __connman_stats_get(struct connman_service *service,
738 connman_bool_t roaming,
739 struct connman_stats_data *data)
741 struct stats_file *file;
742 struct stats_record *rec;
744 file = g_hash_table_lookup(stats_hash, service);
754 memcpy(data, &rec->data,
755 sizeof(struct connman_stats_data));
761 int __connman_stats_init(void)
765 stats_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
771 void __connman_stats_cleanup(void)
775 g_hash_table_destroy(stats_hash);