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
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 static 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);
233 g_free(file->history_name);
234 file->history_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);
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 | O_CLOEXEC, 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.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);
384 size = (size_t)st.st_size;
385 file->max_len = STATS_MAX_FILE_SIZE;
387 if (size < (size_t)sysconf(_SC_PAGESIZE))
388 size = sysconf(_SC_PAGESIZE);
390 err = stats_file_remap(file, size);
402 if (hdr->magic != MAGIC ||
403 hdr->begin < sizeof(struct stats_file_header) ||
404 hdr->end < sizeof(struct stats_file_header) ||
405 hdr->home < sizeof(struct stats_file_header) ||
406 hdr->roaming < sizeof(struct stats_file_header) ||
407 hdr->begin > file->len ||
408 hdr->end > file->len) {
410 hdr->begin = sizeof(struct stats_file_header);
411 hdr->end = sizeof(struct stats_file_header);
412 hdr->home = UINT_MAX;
413 hdr->roaming = UINT_MAX;
415 stats_file_update_cache(file);
422 static struct stats_record *get_next_record(struct stats_iter *iter)
424 if (iter->it != iter->end) {
425 struct stats_record *tmp;
428 iter->it = get_next(iter->file, iter->it);
436 static int append_record(struct stats_file *file,
437 struct stats_record *rec)
439 struct stats_record *cur, *next;
442 if (file->last == get_end(file)) {
443 err = stats_file_remap(file, file->len +
444 sysconf(_SC_PAGESIZE));
448 stats_file_update_cache(file);
452 next = get_next(file, cur);
454 memcpy(next, rec, sizeof(struct stats_record));
461 static struct stats_record *process_file(struct stats_iter *iter,
462 struct stats_file *temp_file,
463 struct stats_record *cur,
464 GDate *date_change_step_size,
465 int account_period_offset)
467 struct stats_record *home, *roaming;
468 struct stats_record *next;
474 cur = get_next_record(iter);
475 next = get_next_record(iter);
489 g_date_set_time_t(&date_cur, cur->ts);
490 g_date_set_time_t(&date_next, next->ts);
492 if (g_date_compare(&date_cur, date_change_step_size) < 0) {
493 /* month period size */
494 GDateDay day_cur, day_next;
495 GDateMonth month_cur, month_next;
497 month_cur = g_date_get_month(&date_cur);
498 month_next = g_date_get_month(&date_next);
500 day_cur = g_date_get_day(&date_cur);
501 day_next = g_date_get_day(&date_next);
503 if (day_cur == day_next && month_cur != month_next) {
505 } else if (day_cur < account_period_offset &&
506 day_next >= account_period_offset) {
510 /* day period size */
511 if (g_date_days_between(&date_cur, &date_next) > 0)
517 append_record(temp_file, home);
522 append_record(temp_file, roaming);
528 next = get_next_record(iter);
534 static int summarize(struct stats_file *data_file,
535 struct stats_file *history_file,
536 struct stats_file *temp_file)
538 struct stats_iter data_iter;
539 struct stats_iter history_iter;
540 struct stats_record *cur, *next;
542 GDate today, date_change_step_size;
545 * First calculate the date when switch from monthly
546 * accounting period size to daily size
548 g_date_set_time_t(&today, time(NULL));
550 date_change_step_size = today;
551 if (g_date_get_day(&today) - data_file->account_period_offset >= 0)
552 g_date_subtract_months(&date_change_step_size, 2);
554 g_date_subtract_months(&date_change_step_size, 3);
556 g_date_set_day(&date_change_step_size,
557 data_file->account_period_offset);
560 /* Now process history file */
564 history_iter.file = history_file;
565 history_iter.begin = get_iterator_begin(history_iter.file);
566 history_iter.end = get_iterator_end(history_iter.file);
567 history_iter.it = history_iter.begin;
569 cur = process_file(&history_iter, temp_file, NULL,
570 &date_change_step_size,
571 data_file->account_period_offset);
574 data_iter.file = data_file;
575 data_iter.begin = get_iterator_begin(data_iter.file);
576 data_iter.end = get_iterator_end(data_iter.file);
577 data_iter.it = data_iter.begin;
580 * Ensure date_file records are newer than the history_file
584 next = get_next_record(&data_iter);
585 while (next && cur->ts > next->ts)
586 next = get_next_record(&data_iter);
589 /* And finally process the new data records */
590 cur = process_file(&data_iter, temp_file, cur,
591 &date_change_step_size,
592 data_file->account_period_offset);
595 append_record(temp_file, cur);
600 static void stats_file_unmap(struct stats_file *file)
602 msync(file->addr, file->len, MS_SYNC);
603 munmap(file->addr, file->len);
607 static void stats_file_cleanup(struct stats_file *file)
614 static int stats_file_close_swap(struct stats_file *history_file,
615 struct stats_file *temp_file)
619 stats_file_unmap(history_file);
620 stats_file_unmap(temp_file);
622 close(temp_file->fd);
624 unlink(history_file->name);
626 err = link(temp_file->name, history_file->name);
628 unlink(temp_file->name);
630 close(history_file->fd);
632 stats_file_cleanup(history_file);
633 stats_file_cleanup(temp_file);
638 static int stats_file_history_update(struct stats_file *data_file)
640 struct stats_file _history_file, *history_file;
641 struct stats_file _temp_file, *temp_file;
644 history_file = &_history_file;
645 temp_file = &_temp_file;
647 bzero(history_file, sizeof(struct stats_file));
648 bzero(temp_file, sizeof(struct stats_file));
650 history_file->fd = -1;
653 err = stats_open(history_file, data_file->history_name);
656 stats_file_setup(history_file);
658 err = stats_open_temp(temp_file);
660 stats_free(history_file);
663 stats_file_setup(temp_file);
665 summarize(data_file, history_file, temp_file);
667 err = stats_file_close_swap(history_file, temp_file);
672 int __connman_stats_service_register(struct connman_service *service)
674 struct stats_file *file;
678 DBG("service %p", service);
680 dir = g_strdup_printf("%s/%s", STORAGEDIR,
681 __connman_service_get_ident(service));
683 /* If the dir doesn't exist, create it */
684 if (!g_file_test(dir, G_FILE_TEST_IS_DIR)) {
685 if (mkdir(dir, MODE) < 0) {
686 if (errno != EEXIST) {
696 file = g_hash_table_lookup(stats_hash, service);
698 file = g_try_new0(struct stats_file, 1);
704 g_hash_table_insert(stats_hash, service, file);
709 name = g_strdup_printf("%s/%s/data", STORAGEDIR,
710 __connman_service_get_ident(service));
711 file->history_name = g_strdup_printf("%s/%s/history", STORAGEDIR,
712 __connman_service_get_ident(service));
714 /* TODO: Use a global config file instead of hard coded value. */
715 file->account_period_offset = 1;
717 err = stats_open(file, name);
722 err = stats_file_setup(file);
729 g_hash_table_remove(stats_hash, service);
734 void __connman_stats_service_unregister(struct connman_service *service)
736 DBG("service %p", service);
738 g_hash_table_remove(stats_hash, service);
741 int __connman_stats_update(struct connman_service *service,
743 struct connman_stats_data *data)
745 struct stats_file *file;
746 struct stats_record *next;
749 file = g_hash_table_lookup(stats_hash, service);
753 if (file->len < file->max_len &&
754 file->last == get_end(file)) {
755 DBG("grow file %s", file->name);
757 err = stats_file_remap(file, file->len + sysconf(_SC_PAGESIZE));
762 next = get_next(file, get_end(file));
764 if (next == get_begin(file)) {
765 DBG("ring buffer is full, update history file");
767 if (stats_file_history_update(file) < 0) {
768 connman_warn("history file update failed %s",
773 next->ts = time(NULL);
774 next->roaming = roaming;
775 memcpy(&next->data, data, sizeof(struct connman_stats_data));
778 set_home(file, next);
780 set_roaming(file, next);
787 int __connman_stats_get(struct connman_service *service,
789 struct connman_stats_data *data)
791 struct stats_file *file;
792 struct stats_record *rec;
794 file = g_hash_table_lookup(stats_hash, service);
804 memcpy(data, &rec->data,
805 sizeof(struct connman_stats_data));
811 int __connman_stats_init(void)
815 stats_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
821 void __connman_stats_cleanup(void)
825 g_hash_table_destroy(stats_hash);