5 * Copyright (C) 2010-2014 BMW Car IT GmbH.
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>
39 #define MODE (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
40 S_IXGRP | S_IROTH | S_IXOTH)
42 #ifdef TEMP_FAILURE_RETRY
43 #define TFR TEMP_FAILURE_RETRY
48 #define MAGIC 0xFA00B916
51 * Statistics counters are stored into a ring buffer which is stored
55 * The ring buffer is mmap to a file
56 * Initially only the smallest possible amount of disk space is allocated
57 * The files grow to the configured maximal size
58 * The grows by _SC_PAGESIZE step size
59 * For each service a file is created
60 * Each file has a header where the indexes are stored
63 * Each entry has a timestamp
64 * A flag to mark if the entry is either home (0) or roaming (1) entry
65 * The entries are fixed sized (stats_record)
67 * Ring buffer properties:
68 * There are to indexes 'begin', 'end', 'home' and 'roaming'
69 * 'begin' points to the oldest entry
70 * 'end' points to the newest/current entry
71 * 'home' points to the current home entry
72 * 'roaming' points to the current roaming entry
73 * If 'begin' == 'end' then the buffer is empty
74 * If 'end' + 1 == 'begin then it's full
75 * The ring buffer is valid in the range (begin, end]
76 * If 'home' has the value UINT_MAX', 'home' is invalid
77 * if 'roaming' has the value UINT_MAX', 'roaming' is invalid
78 * 'first' points to the first entry in the ring buffer
79 * 'last' points to the last entry in the ring buffer
82 * Same format as the ring buffer file
83 * For a period of at least 2 months daily records are kept
84 * If older, then only a monthly record is kept
88 struct stats_file_header {
99 struct connman_stats_data data;
110 struct stats_record *first;
111 struct stats_record *last;
112 struct stats_record *home;
113 struct stats_record *roaming;
117 int account_period_offset;
121 struct stats_file *file;
122 struct stats_record *begin;
123 struct stats_record *end;
124 struct stats_record *it;
127 static GHashTable *stats_hash = NULL;
129 static struct stats_file_header *get_hdr(struct stats_file *file)
131 return (struct stats_file_header *)file->addr;
134 static struct stats_record *get_begin(struct stats_file *file)
136 unsigned int off = get_hdr(file)->begin;
138 return (struct stats_record *)(file->addr + off);
141 static struct stats_record *get_end(struct stats_file *file)
143 unsigned int off = get_hdr(file)->end;
145 return (struct stats_record *)(file->addr + off);
148 static struct stats_record *get_home(struct stats_file *file)
150 struct stats_file_header *hdr;
154 if (hdr->home == UINT_MAX)
157 return (struct stats_record *)(file->addr + hdr->home);
160 static struct stats_record *get_roaming(struct stats_file *file)
162 struct stats_file_header *hdr;
166 if (hdr->roaming == UINT_MAX)
169 return (struct stats_record *)(file->addr + hdr->roaming);
172 static void set_end(struct stats_file *file, struct stats_record *end)
174 struct stats_file_header *hdr;
177 hdr->end = (char *)end - file->addr;
180 static void set_home(struct stats_file *file, struct stats_record *home)
182 struct stats_file_header *hdr;
185 hdr->home = (char *)home - file->addr;
188 static void set_roaming(struct stats_file *file, struct stats_record *roaming)
190 struct stats_file_header *hdr;
193 hdr->roaming = (char *)roaming - file->addr;
196 static struct stats_record *get_next(struct stats_file *file,
197 struct stats_record *cur)
201 if (cur > file->last)
207 static struct stats_record *get_iterator_begin(struct stats_file *file)
209 return get_next(file, get_begin(file));
212 static struct stats_record *get_iterator_end(struct stats_file *file)
214 return get_next(file, get_end(file));
217 static void stats_free(gpointer user_data)
219 struct stats_file *file = user_data;
224 msync(file->addr, file->len, MS_SYNC);
226 munmap(file->addr, file->len);
232 g_free(file->history_name);
233 file->history_name = NULL;
241 static void update_first(struct stats_file *file)
243 file->first = (struct stats_record *)
244 (file->addr + sizeof(struct stats_file_header));
247 static void update_last(struct stats_file *file)
249 unsigned int max_entries;
251 max_entries = (file->len - sizeof(struct stats_file_header)) /
252 sizeof(struct stats_record);
253 file->last = file->first + max_entries - 1;
256 static void update_home(struct stats_file *file)
258 file->home = get_home(file);
261 static void update_roaming(struct stats_file *file)
263 file->roaming = get_roaming(file);
266 static void stats_file_update_cache(struct stats_file *file)
271 update_roaming(file);
274 static int stats_file_remap(struct stats_file *file, size_t size)
276 size_t page_size, new_size;
280 DBG("file %p size %zu addr %p len %zu", file, size, file->addr,
283 page_size = sysconf(_SC_PAGESIZE);
284 new_size = (size + page_size - 1) & ~(page_size - 1);
286 err = ftruncate(file->fd, new_size);
288 connman_error("ftrunctate error %s for %s",
289 strerror(errno), file->name);
295 * Though the buffer is not shared between processes, we still
296 * have to take MAP_SHARED because MAP_PRIVATE does not
297 * guarantee that writes will hit the file without an explicit
298 * call to munmap or msync. For more details please read the
301 addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE,
302 MAP_SHARED, file->fd, 0);
304 addr = mremap(file->addr, file->len, new_size, MREMAP_MAYMOVE);
307 if (addr == MAP_FAILED) {
308 connman_error("mmap error %s for %s",
309 strerror(errno), file->name);
310 if (errno == EINVAL) {
311 connman_error("%s might be on a file system, such as "
312 "JFFS2, that does not allow shared "
313 "writable mappings.", file->name);
319 file->len = new_size;
321 stats_file_update_cache(file);
326 static int stats_open(struct stats_file *file,
329 DBG("file %p name %s", file, name);
331 file->name = g_strdup(name);
333 file->fd = TFR(open(file->name, O_RDWR | O_CREAT | O_CLOEXEC, 0644));
335 connman_error("open error %s for %s",
336 strerror(errno), file->name);
345 static int stats_open_temp(struct stats_file *file)
347 file->name = g_strdup_printf("%s/stats.XXXXXX.tmp",
349 file->fd = g_mkstemp_full(file->name, O_RDWR | O_CREAT, 0644);
351 connman_error("create temporary file error %s for %s",
352 strerror(errno), file->name);
361 static int stats_file_setup(struct stats_file *file)
363 struct stats_file_header *hdr;
368 DBG("file %p fd %d name %s", file, file->fd, file->name);
370 err = fstat(file->fd, &st);
372 connman_error("fstat error %s for %s\n",
373 strerror(errno), file->name);
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);
401 if (hdr->magic != MAGIC ||
402 hdr->begin < sizeof(struct stats_file_header) ||
403 hdr->end < sizeof(struct stats_file_header) ||
404 hdr->home < sizeof(struct stats_file_header) ||
405 hdr->roaming < sizeof(struct stats_file_header) ||
406 hdr->begin > file->len ||
407 hdr->end > file->len) {
409 hdr->begin = sizeof(struct stats_file_header);
410 hdr->end = sizeof(struct stats_file_header);
411 hdr->home = UINT_MAX;
412 hdr->roaming = UINT_MAX;
414 stats_file_update_cache(file);
421 static struct stats_record *get_next_record(struct stats_iter *iter)
423 if (iter->it != iter->end) {
424 struct stats_record *tmp;
427 iter->it = get_next(iter->file, iter->it);
435 static int append_record(struct stats_file *file,
436 struct stats_record *rec)
438 struct stats_record *cur, *next;
441 if (file->last == get_end(file)) {
442 err = stats_file_remap(file, file->len +
443 sysconf(_SC_PAGESIZE));
447 stats_file_update_cache(file);
451 next = get_next(file, cur);
453 memcpy(next, rec, sizeof(struct stats_record));
460 static struct stats_record *process_file(struct stats_iter *iter,
461 struct stats_file *temp_file,
462 struct stats_record *cur,
463 GDate *date_change_step_size,
464 int account_period_offset)
466 struct stats_record *home, *roaming;
467 struct stats_record *next;
473 cur = get_next_record(iter);
474 next = get_next_record(iter);
488 g_date_set_time_t(&date_cur, cur->ts);
489 g_date_set_time_t(&date_next, next->ts);
491 if (g_date_compare(&date_cur, date_change_step_size) < 0) {
492 /* month period size */
493 GDateDay day_cur, day_next;
494 GDateMonth month_cur, month_next;
496 month_cur = g_date_get_month(&date_cur);
497 month_next = g_date_get_month(&date_next);
499 day_cur = g_date_get_day(&date_cur);
500 day_next = g_date_get_day(&date_next);
502 if (day_cur == day_next && month_cur != month_next) {
504 } else if (day_cur < account_period_offset &&
505 day_next >= account_period_offset) {
509 /* day period size */
510 if (g_date_days_between(&date_cur, &date_next) > 0)
516 append_record(temp_file, home);
521 append_record(temp_file, roaming);
527 next = get_next_record(iter);
533 static int summarize(struct stats_file *data_file,
534 struct stats_file *history_file,
535 struct stats_file *temp_file)
537 struct stats_iter data_iter;
538 struct stats_iter history_iter;
539 struct stats_record *cur, *next;
541 GDate today, date_change_step_size;
544 * First calculate the date when switch from monthly
545 * accounting period size to daily size
547 g_date_set_time_t(&today, time(NULL));
549 date_change_step_size = today;
550 if (g_date_get_day(&today) - data_file->account_period_offset >= 0)
551 g_date_subtract_months(&date_change_step_size, 2);
553 g_date_subtract_months(&date_change_step_size, 3);
555 g_date_set_day(&date_change_step_size,
556 data_file->account_period_offset);
559 /* Now process history file */
563 history_iter.file = history_file;
564 history_iter.begin = get_iterator_begin(history_iter.file);
565 history_iter.end = get_iterator_end(history_iter.file);
566 history_iter.it = history_iter.begin;
568 cur = process_file(&history_iter, temp_file, NULL,
569 &date_change_step_size,
570 data_file->account_period_offset);
573 data_iter.file = data_file;
574 data_iter.begin = get_iterator_begin(data_iter.file);
575 data_iter.end = get_iterator_end(data_iter.file);
576 data_iter.it = data_iter.begin;
579 * Ensure date_file records are newer than the history_file
583 next = get_next_record(&data_iter);
584 while (next && cur->ts > next->ts)
585 next = get_next_record(&data_iter);
588 /* And finally process the new data records */
589 cur = process_file(&data_iter, temp_file, cur,
590 &date_change_step_size,
591 data_file->account_period_offset);
594 append_record(temp_file, cur);
599 static void stats_file_unmap(struct stats_file *file)
601 msync(file->addr, file->len, MS_SYNC);
602 munmap(file->addr, file->len);
606 static void stats_file_cleanup(struct stats_file *file)
613 static int stats_file_close_swap(struct stats_file *history_file,
614 struct stats_file *temp_file)
618 stats_file_unmap(history_file);
619 stats_file_unmap(temp_file);
621 close(temp_file->fd);
623 unlink(history_file->name);
625 err = link(temp_file->name, history_file->name);
627 unlink(temp_file->name);
629 close(history_file->fd);
631 stats_file_cleanup(history_file);
632 stats_file_cleanup(temp_file);
637 static int stats_file_history_update(struct stats_file *data_file)
639 struct stats_file _history_file, *history_file;
640 struct stats_file _temp_file, *temp_file;
643 history_file = &_history_file;
644 temp_file = &_temp_file;
646 bzero(history_file, sizeof(struct stats_file));
647 bzero(temp_file, sizeof(struct stats_file));
649 history_file->fd = -1;
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 dir = g_strdup_printf("%s/%s", STORAGEDIR,
680 connman_service_get_identifier(service));
682 /* If the dir doesn't exist, create it */
683 if (!g_file_test(dir, G_FILE_TEST_IS_DIR)) {
684 if (mkdir(dir, MODE) < 0) {
685 if (errno != EEXIST) {
695 file = g_hash_table_lookup(stats_hash, service);
697 file = g_try_new0(struct stats_file, 1);
703 g_hash_table_insert(stats_hash, service, file);
708 name = g_strdup_printf("%s/%s/data", STORAGEDIR,
709 connman_service_get_identifier(service));
710 file->history_name = g_strdup_printf("%s/%s/history", STORAGEDIR,
711 connman_service_get_identifier(service));
713 /* TODO: Use a global config file instead of hard coded value. */
714 file->account_period_offset = 1;
716 err = stats_open(file, name);
721 err = stats_file_setup(file);
728 g_hash_table_remove(stats_hash, service);
733 void __connman_stats_service_unregister(struct connman_service *service)
735 DBG("service %p", service);
737 g_hash_table_remove(stats_hash, service);
740 int __connman_stats_update(struct connman_service *service,
742 struct connman_stats_data *data)
744 struct stats_file *file;
745 struct stats_record *next;
748 file = g_hash_table_lookup(stats_hash, service);
752 if (file->len < file->max_len &&
753 file->last == get_end(file)) {
754 DBG("grow file %s", file->name);
756 err = stats_file_remap(file, file->len + sysconf(_SC_PAGESIZE));
761 next = get_next(file, get_end(file));
763 if (next == get_begin(file)) {
764 DBG("ring buffer is full, update history file");
766 if (stats_file_history_update(file) < 0) {
767 connman_warn("history file update failed %s",
772 next->ts = time(NULL);
773 next->roaming = roaming;
774 memcpy(&next->data, data, sizeof(struct connman_stats_data));
777 set_home(file, next);
779 set_roaming(file, next);
786 int __connman_stats_get(struct connman_service *service,
788 struct connman_stats_data *data)
790 struct stats_file *file;
791 struct stats_record *rec;
793 file = g_hash_table_lookup(stats_hash, service);
803 memcpy(data, &rec->data,
804 sizeof(struct connman_stats_data));
810 int __connman_stats_init(void)
814 stats_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
820 void __connman_stats_cleanup(void)
824 g_hash_table_destroy(stats_hash);