stats: Sum stats ring buffer up info history file
[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         msync(file->addr, file->len, MS_SYNC);
218
219         munmap(file->addr, file->len);
220         file->addr = NULL;
221
222         TFR(close(file->fd));
223         file->fd = -1;
224
225         if (file->history_name != NULL)
226                 g_free(file->history_name);
227         g_free(file->name);
228         g_free(file);
229 }
230
231 static void update_first(struct stats_file *file)
232 {
233         file->first = (struct stats_record *)
234                         (file->addr + sizeof(struct stats_file_header));
235 }
236
237 static void update_last(struct stats_file *file)
238 {
239         unsigned int max_entries;
240
241         max_entries = (file->len - sizeof(struct stats_file_header)) /
242                         sizeof(struct stats_record);
243         file->last = file->first + max_entries - 1;
244 }
245
246 static void update_home(struct stats_file *file)
247 {
248         file->home = get_home(file);
249 }
250
251 static void update_roaming(struct stats_file *file)
252 {
253         file->roaming = get_roaming(file);
254 }
255
256 static void stats_file_update_cache(struct stats_file *file)
257 {
258         update_first(file);
259         update_last(file);
260         update_home(file);
261         update_roaming(file);
262 }
263
264 static int stats_file_remap(struct stats_file *file, size_t size)
265 {
266         size_t page_size, new_size;
267         void *addr;
268         int err;
269
270         page_size = sysconf(_SC_PAGESIZE);
271         new_size = (size + page_size - 1) & ~(page_size - 1);
272
273         err = ftruncate(file->fd, new_size);
274         if (err < 0) {
275                 connman_error("ftrunctate error %s for %s",
276                                 strerror(errno), file->name);
277                 return -errno;
278         }
279
280         if (file->addr == NULL) {
281                 addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE,
282                                 MAP_SHARED, file->fd, 0);
283         } else {
284                 addr = mremap(file->addr, file->len, new_size, MREMAP_MAYMOVE);
285         }
286
287         if (addr == MAP_FAILED) {
288                 connman_error("mmap error %s for %s",
289                                 strerror(errno), file->name);
290                 return -errno;
291         }
292
293         file->addr = addr;
294         file->len = new_size;
295
296         stats_file_update_cache(file);
297
298         return 0;
299 }
300
301 static int stats_open(struct stats_file *file,
302                         const char *name)
303 {
304         file->name = g_strdup(name);
305
306         file->fd = TFR(open(file->name, O_RDWR | O_CREAT, 0644));
307         if (file->fd < 0) {
308                 connman_error("open error %s for %s",
309                                 strerror(errno), file->name);
310                 g_free(file->name);
311                 return -errno;
312         }
313
314         return 0;
315 }
316
317 static int stats_open_temp(struct stats_file *file)
318 {
319         file->name = g_strdup_printf("%s/stats/stats.XXXXXX.tmp",
320                                         STORAGEDIR);
321         file->fd = g_mkstemp_full(file->name, O_RDWR | O_CREAT, 0644);
322         if (file->fd < 0) {
323                 connman_error("create tempory file error %s for %s",
324                                 strerror(errno), file->name);
325                 g_free(file->name);
326                 return -errno;
327         }
328
329         return 0;
330 }
331
332 static int stats_file_setup(struct stats_file *file)
333 {
334         struct stats_file_header *hdr;
335         struct stat st;
336         size_t size = 0;
337         int err;
338
339         err = fstat(file->fd, &st);
340         if (err < 0) {
341                 connman_error("fstat error %s for %s\n",
342                         strerror(errno), file->name);
343
344                 TFR(close(file->fd));
345                 g_free(file->name);
346
347                 return -errno;
348         }
349
350         size = (size_t)st.st_size;
351         file->max_len = STATS_MAX_FILE_SIZE;
352
353         if (size < (size_t)sysconf(_SC_PAGESIZE))
354                 size = sysconf(_SC_PAGESIZE);
355
356         err = stats_file_remap(file, size);
357         if (err < 0) {
358                 TFR(close(file->fd));
359                 g_free(file->name);
360
361                 return err;
362         }
363
364         hdr = get_hdr(file);
365
366         if (hdr->magic != MAGIC ||
367                         hdr->begin < sizeof(struct stats_file_header) ||
368                         hdr->end < sizeof(struct stats_file_header) ||
369                         hdr->home < sizeof(struct stats_file_header) ||
370                         hdr->roaming < sizeof(struct stats_file_header) ||
371                         hdr->begin > file->len ||
372                         hdr->end > file->len) {
373                 hdr->magic = MAGIC;
374                 hdr->begin = sizeof(struct stats_file_header);
375                 hdr->end = sizeof(struct stats_file_header);
376                 hdr->home = UINT_MAX;
377                 hdr->roaming = UINT_MAX;
378
379                 stats_file_update_cache(file);
380         }
381
382         return 0;
383 }
384
385
386 static struct stats_record *get_next_record(struct stats_iter *iter)
387 {
388         if (iter->it != iter->end) {
389                 struct stats_record *tmp;
390
391                 tmp = iter->it;
392                 iter->it = get_next(iter->file, iter->it);
393
394                 return tmp;
395         }
396
397         return NULL;
398 }
399
400 static int append_record(struct stats_file *file,
401                                 struct stats_record *rec)
402 {
403         struct stats_record *cur, *next;
404         int err;
405
406         if (file->last == get_end(file)) {
407                 err = stats_file_remap(file, file->len +
408                                         sysconf(_SC_PAGESIZE));
409                 if (err < 0)
410                         return err;
411
412                 stats_file_update_cache(file);
413         }
414
415         cur = get_end(file);
416         next = get_next(file, cur);
417
418         memcpy(next, rec, sizeof(struct stats_record));
419
420         set_end(file, next);
421
422         return 0;
423 }
424
425 static struct stats_record *process_file(struct stats_iter *iter,
426                                         struct stats_file *temp_file,
427                                         struct stats_record *cur,
428                                         GDate *date_change_step_size,
429                                         int account_period_offset)
430 {
431         struct stats_record *home, *roaming;
432         struct stats_record *next;
433
434         home = NULL;
435         roaming = NULL;
436
437         if (cur == NULL)
438                 cur = get_next_record(iter);
439         next = get_next_record(iter);
440
441         while (next != NULL) {
442                 GDate date_cur;
443                 GDate date_next;
444                 int append;
445
446                 append = FALSE;
447
448                 if (cur->roaming == TRUE)
449                         roaming = cur;
450                 else
451                         home = cur;
452
453                 g_date_set_time_t(&date_cur, cur->ts);
454                 g_date_set_time_t(&date_next, next->ts);
455
456                 if (g_date_compare(&date_cur, date_change_step_size) < 0) {
457                         /* month period size */
458                         GDateDay day_cur, day_next;
459                         GDateMonth month_cur, month_next;
460
461                         month_cur = g_date_get_month(&date_cur);
462                         month_next = g_date_get_month(&date_next);
463
464                         day_cur = g_date_get_day(&date_cur);
465                         day_next = g_date_get_day(&date_next);
466
467                         if (day_cur == day_next && month_cur != month_next) {
468                                 append = TRUE;
469                         } else if (day_cur < account_period_offset &&
470                                         day_next >= account_period_offset) {
471                                 append = TRUE;
472                         }
473                 } else {
474                         /* day period size */
475                         if (g_date_days_between(&date_cur, &date_next) > 0)
476                                 append = TRUE;
477                 }
478
479                 if (append == TRUE) {
480                         if (home != NULL) {
481                                 append_record(temp_file, home);
482                                 home = NULL;
483                         }
484
485                         if (roaming != NULL) {
486                                 append_record(temp_file, roaming);
487                                 roaming = NULL;
488                         }
489                 }
490
491                 cur = next;
492                 next = get_next_record(iter);
493         }
494
495         return cur;
496 }
497
498 static int summarize(struct stats_file *data_file,
499                         struct stats_file *history_file,
500                         struct stats_file *temp_file)
501 {
502         struct stats_iter data_iter;
503         struct stats_iter history_iter;
504         struct stats_record *cur, *next;
505
506         GDate today, date_change_step_size;
507
508         /*
509          * First calculate the date when switch from monthly
510          * accounting period size to daily size
511          */
512         g_date_set_time_t(&today, time(NULL));
513
514         date_change_step_size = today;
515         if (g_date_get_day(&today) - data_file->account_period_offset >= 0)
516                 g_date_subtract_months(&date_change_step_size, 2);
517         else
518                 g_date_subtract_months(&date_change_step_size, 3);
519
520         g_date_set_day(&date_change_step_size,
521                         data_file->account_period_offset);
522
523
524         /* Now process history file */
525         cur = NULL;
526
527         if (history_file != NULL) {
528                 history_iter.file = history_file;
529                 history_iter.begin = get_iterator_begin(history_iter.file);
530                 history_iter.end = get_iterator_end(history_iter.file);
531                 history_iter.it = history_iter.begin;
532
533                 cur = process_file(&history_iter, temp_file, NULL,
534                                         &date_change_step_size,
535                                         data_file->account_period_offset);
536         }
537
538         data_iter.file = data_file;
539         data_iter.begin = get_iterator_begin(data_iter.file);
540         data_iter.end = get_iterator_end(data_iter.file);
541         data_iter.it = data_iter.begin;
542
543         /*
544          * Ensure date_file records are newer than the history_file
545          * record
546          */
547         if (cur != NULL) {
548                 next = get_next_record(&data_iter);
549                 while (next != NULL && cur->ts > next->ts)
550                         next = get_next_record(&data_iter);
551         }
552
553         /* And finally process the new data records */
554         cur = process_file(&data_iter, temp_file, cur,
555                                 &date_change_step_size,
556                                 data_file->account_period_offset);
557
558         if (cur != NULL)
559                 append_record(temp_file, cur);
560
561         return 0;
562 }
563
564 static void stats_file_unmap(struct stats_file *file)
565 {
566         msync(file->addr, file->len, MS_SYNC);
567         munmap(file->addr, file->len);
568         file->addr = NULL;
569 }
570
571 static void stats_file_cleanup(struct stats_file *file)
572 {
573         file->fd = -1;
574         g_free(file->name);
575 }
576
577 static int stats_file_close_swap(struct stats_file *history_file,
578                                         struct stats_file *temp_file)
579 {
580         int err;
581
582         stats_file_unmap(history_file);
583         stats_file_unmap(temp_file);
584
585         TFR(close(temp_file->fd));
586
587         unlink(history_file->name);
588
589         err = link(temp_file->name, history_file->name);
590
591         unlink(temp_file->name);
592
593         TFR(close(history_file->fd));
594
595         stats_file_cleanup(history_file);
596         stats_file_cleanup(temp_file);
597
598         return err;
599 }
600
601 static int stats_file_history_update(struct stats_file *data_file)
602 {
603         struct stats_file _history_file, *history_file;
604         struct stats_file _temp_file, *temp_file;
605         int err;
606
607         history_file = &_history_file;
608         temp_file = &_temp_file;
609
610         bzero(history_file, sizeof(struct stats_file));
611         bzero(temp_file, sizeof(struct stats_file));
612
613         err = stats_open(history_file, data_file->history_name);
614         if (err < 0)
615                 return err;
616         stats_file_setup(history_file);
617
618         err = stats_open_temp(temp_file);
619         if (err < 0) {
620                 stats_free(history_file);
621                 return err;
622         }
623         stats_file_setup(temp_file);
624
625         summarize(data_file, history_file, temp_file);
626
627         err = stats_file_close_swap(history_file, temp_file);
628
629         return err;
630 }
631
632 int __connman_stats_service_register(struct connman_service *service)
633 {
634         struct stats_file *file;
635         char *name;
636         int err;
637
638         DBG("service %p", service);
639
640         file = g_hash_table_lookup(stats_hash, service);
641         if (file == NULL) {
642                 file = g_try_new0(struct stats_file, 1);
643                 if (file == NULL)
644                         return -ENOMEM;
645
646                 g_hash_table_insert(stats_hash, service, file);
647         } else {
648                 return -EALREADY;
649         }
650
651         name = g_strdup_printf("%s/stats/%s.data", STORAGEDIR,
652                                 __connman_service_get_ident(service));
653         file->history_name = g_strdup_printf("%s/stats/%s.history", STORAGEDIR,
654                                 __connman_service_get_ident(service));
655
656         /* TODO: Use a global config file instead of hard coded value. */
657         file->account_period_offset = 1;
658
659         err = stats_open(file, name);
660         g_free(name);
661         if (err < 0)
662                 goto err;
663
664         err = stats_file_setup(file);
665         if (err < 0)
666                 goto err;
667
668         return 0;
669
670 err:
671         g_hash_table_remove(stats_hash, service);
672
673         return err;
674 }
675
676 void __connman_stats_service_unregister(struct connman_service *service)
677 {
678         DBG("service %p", service);
679
680         g_hash_table_remove(stats_hash, service);
681 }
682
683 int  __connman_stats_update(struct connman_service *service,
684                                 connman_bool_t roaming,
685                                 struct connman_stats_data *data)
686 {
687         struct stats_file *file;
688         struct stats_record *next;
689         int err;
690
691         file = g_hash_table_lookup(stats_hash, service);
692         if (file == NULL)
693                 return -EEXIST;
694
695         if (file->len < file->max_len &&
696                         file->last == get_end(file)) {
697                 DBG("grow file %s", file->name);
698
699                 err = stats_file_remap(file, file->len + sysconf(_SC_PAGESIZE));
700                 if (err < 0)
701                         return err;
702         }
703
704         next = get_next(file, get_end(file));
705
706         if (next == get_begin(file)) {
707                 DBG("ring buffer is full, update history file");
708
709                 if (stats_file_history_update(file) < 0) {
710                         connman_warn("history file update failed %s",
711                                         file->history_name);
712                 }
713         }
714
715         next->ts = time(NULL);
716         next->roaming = roaming;
717         memcpy(&next->data, data, sizeof(struct connman_stats_data));
718
719         if (roaming != TRUE)
720                 set_home(file, next);
721         else
722                 set_roaming(file, next);
723
724         set_end(file, next);
725
726         return 0;
727 }
728
729 int __connman_stats_get(struct connman_service *service,
730                                 connman_bool_t roaming,
731                                 struct connman_stats_data *data)
732 {
733         struct stats_file *file;
734         struct stats_record *rec;
735
736         file = g_hash_table_lookup(stats_hash, service);
737         if (file == NULL)
738                 return -EEXIST;
739
740         if (roaming != TRUE)
741                 rec = file->home;
742         else
743                 rec = file->roaming;
744
745         if (rec != NULL) {
746                 memcpy(data, &rec->data,
747                         sizeof(struct connman_stats_data));
748         }
749
750         return 0;
751 }
752
753 int __connman_stats_init(void)
754 {
755         DBG("");
756
757         stats_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
758                                                         NULL, stats_free);
759
760         return 0;
761 }
762
763 void __connman_stats_cleanup(void)
764 {
765         DBG("");
766
767         g_hash_table_destroy(stats_hash);
768         stats_hash = NULL;
769 }