743b795ae27916137de4b3e2a6ad79136e78c798
[platform/upstream/connman.git] / src / stats.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2010  BMW Car IT GmbH. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #define _GNU_SOURCE
27 #include <errno.h>
28 #include <sys/mman.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/time.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <limits.h>
36
37 #include "connman.h"
38
39 #ifdef TEMP_FAILURE_RETRY
40 #define TFR TEMP_FAILURE_RETRY
41 #else
42 #define TFR
43 #endif
44
45 #define MAGIC 0xFA00B916
46
47 /*
48  * Statistics counters are stored into a ring buffer which is stored
49  * into a file
50  *
51  * File properties:
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
58  *
59  * Entries properties:
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)
63  *
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
77  *
78  * History file:
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
82  */
83
84
85 struct stats_file_header {
86         unsigned int magic;
87         unsigned int begin;
88         unsigned int end;
89         unsigned int home;
90         unsigned int roaming;
91 };
92
93 struct stats_record {
94         time_t ts;
95         unsigned int roaming;
96         struct connman_stats_data data;
97 };
98
99 struct stats_file {
100         int fd;
101         char *name;
102         char *addr;
103         size_t len;
104         size_t max_len;
105
106         /* cached values */
107         struct stats_record *first;
108         struct stats_record *last;
109         struct stats_record *home;
110         struct stats_record *roaming;
111
112         /* history */
113         char *history_name;
114         int account_period_offset;
115 };
116
117 struct stats_iter {
118         struct stats_file *file;
119         struct stats_record *begin;
120         struct stats_record *end;
121         struct stats_record *it;
122 };
123
124 GHashTable *stats_hash = NULL;
125
126 static struct stats_file_header *get_hdr(struct stats_file *file)
127 {
128         return (struct stats_file_header *)file->addr;
129 }
130
131 static struct stats_record *get_begin(struct stats_file *file)
132 {
133         unsigned int off = get_hdr(file)->begin;
134
135         return (struct stats_record *)(file->addr + off);
136 }
137
138 static struct stats_record *get_end(struct stats_file *file)
139 {
140         unsigned int off = get_hdr(file)->end;
141
142         return (struct stats_record *)(file->addr + off);
143 }
144
145 static struct stats_record *get_home(struct stats_file *file)
146 {
147         struct stats_file_header *hdr;
148
149         hdr = get_hdr(file);
150
151         if (hdr->home == UINT_MAX)
152                 return NULL;
153
154         return (struct stats_record *)(file->addr + hdr->home);
155 }
156
157 static struct stats_record *get_roaming(struct stats_file *file)
158 {
159         struct stats_file_header *hdr;
160
161         hdr = get_hdr(file);
162
163         if (hdr->roaming == UINT_MAX)
164                 return NULL;
165
166         return (struct stats_record *)(file->addr + hdr->roaming);
167 }
168
169 static void set_end(struct stats_file *file, struct stats_record *end)
170 {
171         struct stats_file_header *hdr;
172
173         hdr = get_hdr(file);
174         hdr->end = (char *)end - file->addr;
175 }
176
177 static void set_home(struct stats_file *file, struct stats_record *home)
178 {
179         struct stats_file_header *hdr;
180
181         hdr = get_hdr(file);
182         hdr->home = (char *)home - file->addr;
183 }
184
185 static void set_roaming(struct stats_file *file, struct stats_record *roaming)
186 {
187         struct stats_file_header *hdr;
188
189         hdr = get_hdr(file);
190         hdr->roaming = (char *)roaming - file->addr;
191 }
192
193 static struct stats_record *get_next(struct stats_file *file,
194                                         struct stats_record *cur)
195 {
196         cur++;
197
198         if (cur > file->last)
199                 cur = file->first;
200
201         return cur;
202 }
203
204 static struct stats_record *get_iterator_begin(struct stats_file *file)
205 {
206         return get_next(file, get_begin(file));
207 }
208
209 static struct stats_record *get_iterator_end(struct stats_file *file)
210 {
211         return get_next(file, get_end(file));
212 }
213
214 static void stats_free(gpointer user_data)
215 {
216         struct stats_file *file = user_data;
217
218         if (file == NULL)
219                 return;
220
221         msync(file->addr, file->len, MS_SYNC);
222
223         munmap(file->addr, file->len);
224         file->addr = NULL;
225
226         TFR(close(file->fd));
227         file->fd = -1;
228
229         if (file->history_name != NULL) {
230                 g_free(file->history_name);
231                 file->history_name = NULL;
232         }
233
234         if (file->name != NULL) {
235                 g_free(file->name);
236                 file->name = NULL;
237         }
238
239         g_free(file);
240 }
241
242 static void update_first(struct stats_file *file)
243 {
244         file->first = (struct stats_record *)
245                         (file->addr + sizeof(struct stats_file_header));
246 }
247
248 static void update_last(struct stats_file *file)
249 {
250         unsigned int max_entries;
251
252         max_entries = (file->len - sizeof(struct stats_file_header)) /
253                         sizeof(struct stats_record);
254         file->last = file->first + max_entries - 1;
255 }
256
257 static void update_home(struct stats_file *file)
258 {
259         file->home = get_home(file);
260 }
261
262 static void update_roaming(struct stats_file *file)
263 {
264         file->roaming = get_roaming(file);
265 }
266
267 static void stats_file_update_cache(struct stats_file *file)
268 {
269         update_first(file);
270         update_last(file);
271         update_home(file);
272         update_roaming(file);
273 }
274
275 static int stats_file_remap(struct stats_file *file, size_t size)
276 {
277         size_t page_size, new_size;
278         void *addr;
279         int err;
280
281         DBG("file %p size %zu addr %p len %zu", file, size, file->addr,
282                 file->len);
283
284         page_size = sysconf(_SC_PAGESIZE);
285         new_size = (size + page_size - 1) & ~(page_size - 1);
286
287         err = ftruncate(file->fd, new_size);
288         if (err < 0) {
289                 connman_error("ftrunctate error %s for %s",
290                                 strerror(errno), file->name);
291                 return -errno;
292         }
293
294         if (file->addr == NULL) {
295                 /*
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
300                  * mmap man pages.
301                  */
302                 addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE,
303                                 MAP_SHARED, file->fd, 0);
304         } else {
305                 addr = mremap(file->addr, file->len, new_size, MREMAP_MAYMOVE);
306         }
307
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);
315                 }
316                 return -errno;
317         }
318
319         file->addr = addr;
320         file->len = new_size;
321
322         stats_file_update_cache(file);
323
324         return 0;
325 }
326
327 static int stats_open(struct stats_file *file,
328                         const char *name)
329 {
330         DBG("file %p name %s", file, name);
331
332         file->name = g_strdup(name);
333
334         file->fd = TFR(open(file->name, O_RDWR | O_CREAT, 0644));
335         if (file->fd < 0) {
336                 connman_error("open error %s for %s",
337                                 strerror(errno), file->name);
338                 g_free(file->name);
339                 file->name = NULL;
340                 return -errno;
341         }
342
343         return 0;
344 }
345
346 static int stats_open_temp(struct stats_file *file)
347 {
348         file->name = g_strdup_printf("%s/stats/stats.XXXXXX.tmp",
349                                         STORAGEDIR);
350         file->fd = g_mkstemp_full(file->name, O_RDWR | O_CREAT, 0644);
351         if (file->fd < 0) {
352                 connman_error("create tempory file error %s for %s",
353                                 strerror(errno), file->name);
354                 g_free(file->name);
355                 file->name = NULL;
356                 return -errno;
357         }
358
359         return 0;
360 }
361
362 static int stats_file_setup(struct stats_file *file)
363 {
364         struct stats_file_header *hdr;
365         struct stat st;
366         size_t size = 0;
367         int err;
368
369         DBG("file %p fd %d name %s", file, file->fd, file->name);
370
371         err = fstat(file->fd, &st);
372         if (err < 0) {
373                 connman_error("fstat error %s for %s\n",
374                         strerror(errno), file->name);
375
376                 TFR(close(file->fd));
377                 g_free(file->name);
378                 file->name = NULL;
379
380                 return -errno;
381         }
382
383         size = (size_t)st.st_size;
384         file->max_len = STATS_MAX_FILE_SIZE;
385
386         if (size < (size_t)sysconf(_SC_PAGESIZE))
387                 size = sysconf(_SC_PAGESIZE);
388
389         err = stats_file_remap(file, size);
390         if (err < 0) {
391                 TFR(close(file->fd));
392                 g_free(file->name);
393                 file->name = NULL;
394
395                 return err;
396         }
397
398         hdr = get_hdr(file);
399
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) {
407                 hdr->magic = MAGIC;
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;
412
413                 stats_file_update_cache(file);
414         }
415
416         return 0;
417 }
418
419
420 static struct stats_record *get_next_record(struct stats_iter *iter)
421 {
422         if (iter->it != iter->end) {
423                 struct stats_record *tmp;
424
425                 tmp = iter->it;
426                 iter->it = get_next(iter->file, iter->it);
427
428                 return tmp;
429         }
430
431         return NULL;
432 }
433
434 static int append_record(struct stats_file *file,
435                                 struct stats_record *rec)
436 {
437         struct stats_record *cur, *next;
438         int err;
439
440         if (file->last == get_end(file)) {
441                 err = stats_file_remap(file, file->len +
442                                         sysconf(_SC_PAGESIZE));
443                 if (err < 0)
444                         return err;
445
446                 stats_file_update_cache(file);
447         }
448
449         cur = get_end(file);
450         next = get_next(file, cur);
451
452         memcpy(next, rec, sizeof(struct stats_record));
453
454         set_end(file, next);
455
456         return 0;
457 }
458
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)
464 {
465         struct stats_record *home, *roaming;
466         struct stats_record *next;
467
468         home = NULL;
469         roaming = NULL;
470
471         if (cur == NULL)
472                 cur = get_next_record(iter);
473         next = get_next_record(iter);
474
475         while (next != NULL) {
476                 GDate date_cur;
477                 GDate date_next;
478                 int append;
479
480                 append = FALSE;
481
482                 if (cur->roaming == TRUE)
483                         roaming = cur;
484                 else
485                         home = cur;
486
487                 g_date_set_time_t(&date_cur, cur->ts);
488                 g_date_set_time_t(&date_next, next->ts);
489
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;
494
495                         month_cur = g_date_get_month(&date_cur);
496                         month_next = g_date_get_month(&date_next);
497
498                         day_cur = g_date_get_day(&date_cur);
499                         day_next = g_date_get_day(&date_next);
500
501                         if (day_cur == day_next && month_cur != month_next) {
502                                 append = TRUE;
503                         } else if (day_cur < account_period_offset &&
504                                         day_next >= account_period_offset) {
505                                 append = TRUE;
506                         }
507                 } else {
508                         /* day period size */
509                         if (g_date_days_between(&date_cur, &date_next) > 0)
510                                 append = TRUE;
511                 }
512
513                 if (append == TRUE) {
514                         if (home != NULL) {
515                                 append_record(temp_file, home);
516                                 home = NULL;
517                         }
518
519                         if (roaming != NULL) {
520                                 append_record(temp_file, roaming);
521                                 roaming = NULL;
522                         }
523                 }
524
525                 cur = next;
526                 next = get_next_record(iter);
527         }
528
529         return cur;
530 }
531
532 static int summarize(struct stats_file *data_file,
533                         struct stats_file *history_file,
534                         struct stats_file *temp_file)
535 {
536         struct stats_iter data_iter;
537         struct stats_iter history_iter;
538         struct stats_record *cur, *next;
539
540         GDate today, date_change_step_size;
541
542         /*
543          * First calculate the date when switch from monthly
544          * accounting period size to daily size
545          */
546         g_date_set_time_t(&today, time(NULL));
547
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);
551         else
552                 g_date_subtract_months(&date_change_step_size, 3);
553
554         g_date_set_day(&date_change_step_size,
555                         data_file->account_period_offset);
556
557
558         /* Now process history file */
559         cur = NULL;
560
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;
566
567                 cur = process_file(&history_iter, temp_file, NULL,
568                                         &date_change_step_size,
569                                         data_file->account_period_offset);
570         }
571
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;
576
577         /*
578          * Ensure date_file records are newer than the history_file
579          * record
580          */
581         if (cur != NULL) {
582                 next = get_next_record(&data_iter);
583                 while (next != NULL && cur->ts > next->ts)
584                         next = get_next_record(&data_iter);
585         }
586
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);
591
592         if (cur != NULL)
593                 append_record(temp_file, cur);
594
595         return 0;
596 }
597
598 static void stats_file_unmap(struct stats_file *file)
599 {
600         msync(file->addr, file->len, MS_SYNC);
601         munmap(file->addr, file->len);
602         file->addr = NULL;
603 }
604
605 static void stats_file_cleanup(struct stats_file *file)
606 {
607         file->fd = -1;
608         g_free(file->name);
609         file->name = NULL;
610 }
611
612 static int stats_file_close_swap(struct stats_file *history_file,
613                                         struct stats_file *temp_file)
614 {
615         int err;
616
617         stats_file_unmap(history_file);
618         stats_file_unmap(temp_file);
619
620         TFR(close(temp_file->fd));
621
622         unlink(history_file->name);
623
624         err = link(temp_file->name, history_file->name);
625
626         unlink(temp_file->name);
627
628         TFR(close(history_file->fd));
629
630         stats_file_cleanup(history_file);
631         stats_file_cleanup(temp_file);
632
633         return err;
634 }
635
636 static int stats_file_history_update(struct stats_file *data_file)
637 {
638         struct stats_file _history_file, *history_file;
639         struct stats_file _temp_file, *temp_file;
640         int err;
641
642         history_file = &_history_file;
643         temp_file = &_temp_file;
644
645         bzero(history_file, sizeof(struct stats_file));
646         bzero(temp_file, sizeof(struct stats_file));
647
648         err = stats_open(history_file, data_file->history_name);
649         if (err < 0)
650                 return err;
651         stats_file_setup(history_file);
652
653         err = stats_open_temp(temp_file);
654         if (err < 0) {
655                 stats_free(history_file);
656                 return err;
657         }
658         stats_file_setup(temp_file);
659
660         summarize(data_file, history_file, temp_file);
661
662         err = stats_file_close_swap(history_file, temp_file);
663
664         return err;
665 }
666
667 int __connman_stats_service_register(struct connman_service *service)
668 {
669         struct stats_file *file;
670         char *name;
671         int err;
672
673         DBG("service %p", service);
674
675         file = g_hash_table_lookup(stats_hash, service);
676         if (file == NULL) {
677                 file = g_try_new0(struct stats_file, 1);
678                 if (file == NULL)
679                         return -ENOMEM;
680
681                 g_hash_table_insert(stats_hash, service, file);
682         } else {
683                 return -EALREADY;
684         }
685
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));
690
691         /* TODO: Use a global config file instead of hard coded value. */
692         file->account_period_offset = 1;
693
694         err = stats_open(file, name);
695         g_free(name);
696         if (err < 0)
697                 goto err;
698
699         err = stats_file_setup(file);
700         if (err < 0)
701                 goto err;
702
703         return 0;
704
705 err:
706         g_hash_table_remove(stats_hash, service);
707
708         return err;
709 }
710
711 void __connman_stats_service_unregister(struct connman_service *service)
712 {
713         DBG("service %p", service);
714
715         g_hash_table_remove(stats_hash, service);
716 }
717
718 int  __connman_stats_update(struct connman_service *service,
719                                 connman_bool_t roaming,
720                                 struct connman_stats_data *data)
721 {
722         struct stats_file *file;
723         struct stats_record *next;
724         int err;
725
726         file = g_hash_table_lookup(stats_hash, service);
727         if (file == NULL)
728                 return -EEXIST;
729
730         if (file->len < file->max_len &&
731                         file->last == get_end(file)) {
732                 DBG("grow file %s", file->name);
733
734                 err = stats_file_remap(file, file->len + sysconf(_SC_PAGESIZE));
735                 if (err < 0)
736                         return err;
737         }
738
739         next = get_next(file, get_end(file));
740
741         if (next == get_begin(file)) {
742                 DBG("ring buffer is full, update history file");
743
744                 if (stats_file_history_update(file) < 0) {
745                         connman_warn("history file update failed %s",
746                                         file->history_name);
747                 }
748         }
749
750         next->ts = time(NULL);
751         next->roaming = roaming;
752         memcpy(&next->data, data, sizeof(struct connman_stats_data));
753
754         if (roaming != TRUE)
755                 set_home(file, next);
756         else
757                 set_roaming(file, next);
758
759         set_end(file, next);
760
761         return 0;
762 }
763
764 int __connman_stats_get(struct connman_service *service,
765                                 connman_bool_t roaming,
766                                 struct connman_stats_data *data)
767 {
768         struct stats_file *file;
769         struct stats_record *rec;
770
771         file = g_hash_table_lookup(stats_hash, service);
772         if (file == NULL)
773                 return -EEXIST;
774
775         if (roaming != TRUE)
776                 rec = file->home;
777         else
778                 rec = file->roaming;
779
780         if (rec != NULL) {
781                 memcpy(data, &rec->data,
782                         sizeof(struct connman_stats_data));
783         }
784
785         return 0;
786 }
787
788 int __connman_stats_init(void)
789 {
790         DBG("");
791
792         stats_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
793                                                         NULL, stats_free);
794
795         return 0;
796 }
797
798 void __connman_stats_cleanup(void)
799 {
800         DBG("");
801
802         g_hash_table_destroy(stats_hash);
803         stats_hash = NULL;
804 }