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>
40 #define MODE (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
41 S_IXGRP | S_IROTH | S_IXOTH)
43 #ifdef TEMP_FAILURE_RETRY
44 #define TFR TEMP_FAILURE_RETRY
49 #define MAGIC 0xFA00B916
52 * Statistics counters are stored into a ring buffer which is stored
56 * The ring buffer is mmap to a file
57 * Initialy only the smallest possible amount of disk space is allocated
58 * The files grow to the configured maximal size
59 * The grows by _SC_PAGESIZE step size
60 * For each service a file is created
61 * Each file has a header where the indexes are stored
64 * Each entry has a timestamp
65 * A flag to mark if the entry is either home (0) or roaming (1) entry
66 * The entries are fixed sized (stats_record)
68 * Ring buffer properties:
69 * There are to indexes 'begin', 'end', 'home' and 'roaming'
70 * 'begin' points to the oldest entry
71 * 'end' points to the newest/current entry
72 * 'home' points to the current home entry
73 * 'roaming' points to the current roaming entry
74 * If 'begin' == 'end' then the buffer is empty
75 * If 'end' + 1 == 'begin then it's full
76 * The ring buffer is valid in the range (begin, end]
77 * If 'home' has the value UINT_MAX', 'home' is invalid
78 * if 'roaming' has the value UINT_MAX', 'roaming' is invalid
79 * 'first' points to the first entry in the ring buffer
80 * 'last' points to the last entry in the ring buffer
83 * Same format as the ring buffer file
84 * For a period of at least 2 months dayly records are keept
85 * If older, then only a monthly record is keept
89 struct stats_file_header {
100 struct connman_stats_data data;
111 struct stats_record *first;
112 struct stats_record *last;
113 struct stats_record *home;
114 struct stats_record *roaming;
118 int account_period_offset;
122 struct stats_file *file;
123 struct stats_record *begin;
124 struct stats_record *end;
125 struct stats_record *it;
128 GHashTable *stats_hash = NULL;
130 static struct stats_file_header *get_hdr(struct stats_file *file)
132 return (struct stats_file_header *)file->addr;
135 static struct stats_record *get_begin(struct stats_file *file)
137 unsigned int off = get_hdr(file)->begin;
139 return (struct stats_record *)(file->addr + off);
142 static struct stats_record *get_end(struct stats_file *file)
144 unsigned int off = get_hdr(file)->end;
146 return (struct stats_record *)(file->addr + off);
149 static struct stats_record *get_home(struct stats_file *file)
151 struct stats_file_header *hdr;
155 if (hdr->home == UINT_MAX)
158 return (struct stats_record *)(file->addr + hdr->home);
161 static struct stats_record *get_roaming(struct stats_file *file)
163 struct stats_file_header *hdr;
167 if (hdr->roaming == UINT_MAX)
170 return (struct stats_record *)(file->addr + hdr->roaming);
173 static void set_end(struct stats_file *file, struct stats_record *end)
175 struct stats_file_header *hdr;
178 hdr->end = (char *)end - file->addr;
181 static void set_home(struct stats_file *file, struct stats_record *home)
183 struct stats_file_header *hdr;
186 hdr->home = (char *)home - file->addr;
189 static void set_roaming(struct stats_file *file, struct stats_record *roaming)
191 struct stats_file_header *hdr;
194 hdr->roaming = (char *)roaming - file->addr;
197 static struct stats_record *get_next(struct stats_file *file,
198 struct stats_record *cur)
202 if (cur > file->last)
208 static struct stats_record *get_iterator_begin(struct stats_file *file)
210 return get_next(file, get_begin(file));
213 static struct stats_record *get_iterator_end(struct stats_file *file)
215 return get_next(file, get_end(file));
218 static void stats_free(gpointer user_data)
220 struct stats_file *file = user_data;
225 msync(file->addr, file->len, MS_SYNC);
227 munmap(file->addr, file->len);
230 TFR(close(file->fd));
233 if (file->history_name != NULL) {
234 g_free(file->history_name);
235 file->history_name = NULL;
238 if (file->name != NULL) {
246 static void update_first(struct stats_file *file)
248 file->first = (struct stats_record *)
249 (file->addr + sizeof(struct stats_file_header));
252 static void update_last(struct stats_file *file)
254 unsigned int max_entries;
256 max_entries = (file->len - sizeof(struct stats_file_header)) /
257 sizeof(struct stats_record);
258 file->last = file->first + max_entries - 1;
261 static void update_home(struct stats_file *file)
263 file->home = get_home(file);
266 static void update_roaming(struct stats_file *file)
268 file->roaming = get_roaming(file);
271 static void stats_file_update_cache(struct stats_file *file)
276 update_roaming(file);
279 static int stats_file_remap(struct stats_file *file, size_t size)
281 size_t page_size, new_size;
285 DBG("file %p size %zu addr %p len %zu", file, size, file->addr,
288 page_size = sysconf(_SC_PAGESIZE);
289 new_size = (size + page_size - 1) & ~(page_size - 1);
291 err = ftruncate(file->fd, new_size);
293 connman_error("ftrunctate error %s for %s",
294 strerror(errno), file->name);
298 if (file->addr == NULL) {
300 * Though the buffer is not shared between processes, we still
301 * have to take MAP_SHARED because MAP_PRIVATE does not
302 * guarantee that writes will hit the file without an explicit
303 * call to munmap or msync. For more details please read the
306 addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE,
307 MAP_SHARED, file->fd, 0);
309 addr = mremap(file->addr, file->len, new_size, MREMAP_MAYMOVE);
312 if (addr == MAP_FAILED) {
313 connman_error("mmap error %s for %s",
314 strerror(errno), file->name);
315 if (errno == EINVAL) {
316 connman_error("%s might be on a file system, such as "
317 "JFFS2, that does not allow shared "
318 "writable mappings.", file->name);
324 file->len = new_size;
326 stats_file_update_cache(file);
331 static int stats_open(struct stats_file *file,
334 DBG("file %p name %s", file, name);
336 file->name = g_strdup(name);
338 file->fd = TFR(open(file->name, O_RDWR | O_CREAT, 0644));
340 connman_error("open error %s for %s",
341 strerror(errno), file->name);
350 static int stats_open_temp(struct stats_file *file)
352 file->name = g_strdup_printf("%s/stats.XXXXXX.tmp",
354 file->fd = g_mkstemp_full(file->name, O_RDWR | O_CREAT, 0644);
356 connman_error("create tempory file error %s for %s",
357 strerror(errno), file->name);
366 static int stats_file_setup(struct stats_file *file)
368 struct stats_file_header *hdr;
373 DBG("file %p fd %d name %s", file, file->fd, file->name);
375 err = fstat(file->fd, &st);
377 connman_error("fstat error %s for %s\n",
378 strerror(errno), file->name);
380 TFR(close(file->fd));
387 size = (size_t)st.st_size;
388 file->max_len = STATS_MAX_FILE_SIZE;
390 if (size < (size_t)sysconf(_SC_PAGESIZE))
391 size = sysconf(_SC_PAGESIZE);
393 err = stats_file_remap(file, size);
395 TFR(close(file->fd));
404 if (hdr->magic != MAGIC ||
405 hdr->begin < sizeof(struct stats_file_header) ||
406 hdr->end < sizeof(struct stats_file_header) ||
407 hdr->home < sizeof(struct stats_file_header) ||
408 hdr->roaming < sizeof(struct stats_file_header) ||
409 hdr->begin > file->len ||
410 hdr->end > file->len) {
412 hdr->begin = sizeof(struct stats_file_header);
413 hdr->end = sizeof(struct stats_file_header);
414 hdr->home = UINT_MAX;
415 hdr->roaming = UINT_MAX;
417 stats_file_update_cache(file);
424 static struct stats_record *get_next_record(struct stats_iter *iter)
426 if (iter->it != iter->end) {
427 struct stats_record *tmp;
430 iter->it = get_next(iter->file, iter->it);
438 static int append_record(struct stats_file *file,
439 struct stats_record *rec)
441 struct stats_record *cur, *next;
444 if (file->last == get_end(file)) {
445 err = stats_file_remap(file, file->len +
446 sysconf(_SC_PAGESIZE));
450 stats_file_update_cache(file);
454 next = get_next(file, cur);
456 memcpy(next, rec, sizeof(struct stats_record));
463 static struct stats_record *process_file(struct stats_iter *iter,
464 struct stats_file *temp_file,
465 struct stats_record *cur,
466 GDate *date_change_step_size,
467 int account_period_offset)
469 struct stats_record *home, *roaming;
470 struct stats_record *next;
476 cur = get_next_record(iter);
477 next = get_next_record(iter);
479 while (next != NULL) {
486 if (cur->roaming == TRUE)
491 g_date_set_time_t(&date_cur, cur->ts);
492 g_date_set_time_t(&date_next, next->ts);
494 if (g_date_compare(&date_cur, date_change_step_size) < 0) {
495 /* month period size */
496 GDateDay day_cur, day_next;
497 GDateMonth month_cur, month_next;
499 month_cur = g_date_get_month(&date_cur);
500 month_next = g_date_get_month(&date_next);
502 day_cur = g_date_get_day(&date_cur);
503 day_next = g_date_get_day(&date_next);
505 if (day_cur == day_next && month_cur != month_next) {
507 } else if (day_cur < account_period_offset &&
508 day_next >= account_period_offset) {
512 /* day period size */
513 if (g_date_days_between(&date_cur, &date_next) > 0)
517 if (append == TRUE) {
519 append_record(temp_file, home);
523 if (roaming != NULL) {
524 append_record(temp_file, roaming);
530 next = get_next_record(iter);
536 static int summarize(struct stats_file *data_file,
537 struct stats_file *history_file,
538 struct stats_file *temp_file)
540 struct stats_iter data_iter;
541 struct stats_iter history_iter;
542 struct stats_record *cur, *next;
544 GDate today, date_change_step_size;
547 * First calculate the date when switch from monthly
548 * accounting period size to daily size
550 g_date_set_time_t(&today, time(NULL));
552 date_change_step_size = today;
553 if (g_date_get_day(&today) - data_file->account_period_offset >= 0)
554 g_date_subtract_months(&date_change_step_size, 2);
556 g_date_subtract_months(&date_change_step_size, 3);
558 g_date_set_day(&date_change_step_size,
559 data_file->account_period_offset);
562 /* Now process history file */
565 if (history_file != NULL) {
566 history_iter.file = history_file;
567 history_iter.begin = get_iterator_begin(history_iter.file);
568 history_iter.end = get_iterator_end(history_iter.file);
569 history_iter.it = history_iter.begin;
571 cur = process_file(&history_iter, temp_file, NULL,
572 &date_change_step_size,
573 data_file->account_period_offset);
576 data_iter.file = data_file;
577 data_iter.begin = get_iterator_begin(data_iter.file);
578 data_iter.end = get_iterator_end(data_iter.file);
579 data_iter.it = data_iter.begin;
582 * Ensure date_file records are newer than the history_file
586 next = get_next_record(&data_iter);
587 while (next != NULL && cur->ts > next->ts)
588 next = get_next_record(&data_iter);
591 /* And finally process the new data records */
592 cur = process_file(&data_iter, temp_file, cur,
593 &date_change_step_size,
594 data_file->account_period_offset);
597 append_record(temp_file, cur);
602 static void stats_file_unmap(struct stats_file *file)
604 msync(file->addr, file->len, MS_SYNC);
605 munmap(file->addr, file->len);
609 static void stats_file_cleanup(struct stats_file *file)
616 static int stats_file_close_swap(struct stats_file *history_file,
617 struct stats_file *temp_file)
621 stats_file_unmap(history_file);
622 stats_file_unmap(temp_file);
624 TFR(close(temp_file->fd));
626 unlink(history_file->name);
628 err = link(temp_file->name, history_file->name);
630 unlink(temp_file->name);
632 TFR(close(history_file->fd));
634 stats_file_cleanup(history_file);
635 stats_file_cleanup(temp_file);
640 static int stats_file_history_update(struct stats_file *data_file)
642 struct stats_file _history_file, *history_file;
643 struct stats_file _temp_file, *temp_file;
646 history_file = &_history_file;
647 temp_file = &_temp_file;
649 bzero(history_file, sizeof(struct stats_file));
650 bzero(temp_file, sizeof(struct stats_file));
652 err = stats_open(history_file, data_file->history_name);
655 stats_file_setup(history_file);
657 err = stats_open_temp(temp_file);
659 stats_free(history_file);
662 stats_file_setup(temp_file);
664 summarize(data_file, history_file, temp_file);
666 err = stats_file_close_swap(history_file, temp_file);
671 int __connman_stats_service_register(struct connman_service *service)
673 struct stats_file *file;
677 DBG("service %p", service);
679 file = g_hash_table_lookup(stats_hash, service);
681 file = g_try_new0(struct stats_file, 1);
685 g_hash_table_insert(stats_hash, service, file);
690 dir = g_strdup_printf("%s/%s", STORAGEDIR,
691 __connman_service_get_ident(service));
693 /* If the dir doesn't exist, create it */
694 if (!g_file_test(dir, G_FILE_TEST_IS_DIR)) {
695 if(mkdir(dir, MODE) < 0) {
696 if (errno != EEXIST) {
707 name = g_strdup_printf("%s/%s/data", STORAGEDIR,
708 __connman_service_get_ident(service));
709 file->history_name = g_strdup_printf("%s/%s/history", STORAGEDIR,
710 __connman_service_get_ident(service));
712 /* TODO: Use a global config file instead of hard coded value. */
713 file->account_period_offset = 1;
715 err = stats_open(file, name);
720 err = stats_file_setup(file);
727 g_hash_table_remove(stats_hash, service);
732 void __connman_stats_service_unregister(struct connman_service *service)
734 DBG("service %p", service);
736 g_hash_table_remove(stats_hash, service);
739 int __connman_stats_update(struct connman_service *service,
740 connman_bool_t roaming,
741 struct connman_stats_data *data)
743 struct stats_file *file;
744 struct stats_record *next;
747 file = g_hash_table_lookup(stats_hash, service);
751 if (file->len < file->max_len &&
752 file->last == get_end(file)) {
753 DBG("grow file %s", file->name);
755 err = stats_file_remap(file, file->len + sysconf(_SC_PAGESIZE));
760 next = get_next(file, get_end(file));
762 if (next == get_begin(file)) {
763 DBG("ring buffer is full, update history file");
765 if (stats_file_history_update(file) < 0) {
766 connman_warn("history file update failed %s",
771 next->ts = time(NULL);
772 next->roaming = roaming;
773 memcpy(&next->data, data, sizeof(struct connman_stats_data));
776 set_home(file, next);
778 set_roaming(file, next);
785 int __connman_stats_get(struct connman_service *service,
786 connman_bool_t roaming,
787 struct connman_stats_data *data)
789 struct stats_file *file;
790 struct stats_record *rec;
792 file = g_hash_table_lookup(stats_hash, service);
802 memcpy(data, &rec->data,
803 sizeof(struct connman_stats_data));
809 int __connman_stats_init(void)
813 stats_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
819 void __connman_stats_cleanup(void)
823 g_hash_table_destroy(stats_hash);