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