Merge home and roaming stats 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 #define MAGIC 0xFA00B916
39
40 /*
41  * Statistics counters are stored into a ring buffer which is stored
42  * into a file
43  *
44  * File properties:
45  *   The ring buffer is mmap to a file
46  *   Initialy only the smallest possible amount of disk space is allocated
47  *   The files grow to the configured maximal size
48  *   The grows by _SC_PAGESIZE step size
49  *   For each service a file is created
50  *   Each file has a header where the indexes are stored
51  *
52  * Entries properties:
53  *   Each entry has a timestamp
54  *   A flag to mark if the entry is either home (0) or roaming (1) entry
55  *   The entries are fixed sized (stats_record)
56  *
57  * Ring buffer properties:
58  *   There are to indexes 'begin', 'end', 'home' and 'roaming'
59  *   'begin' points to the oldest entry
60  *   'end' points to the newest/current entry
61  *   'home' points to the current home entry
62  *   'roaming' points to the current roaming entry
63  *   If 'begin' == 'end' then the buffer is empty
64  *   If 'end' + 1 == 'begin then it's full
65  *   The ring buffer is valid in the range (begin, end]
66  *   If 'home' has the value UINT_MAX', 'home' is invalid
67  *   if 'roaming' has the value UINT_MAX', 'roaming' is invalid
68  *   'first' points to the first entry in the ring buffer
69  *   'last' points to the last entry in the ring buffer
70  */
71
72 struct stats_file_header {
73         unsigned int magic;
74         unsigned int begin;
75         unsigned int end;
76         unsigned int home;
77         unsigned int roaming;
78 };
79
80 struct stats_record {
81         time_t ts;
82         unsigned int roaming;
83         struct connman_stats_data data;
84 };
85
86 struct stats_file {
87         int fd;
88         char *name;
89         char *addr;
90         size_t len;
91         size_t max_len;
92
93         /* cached values */
94         struct stats_record *first;
95         struct stats_record *last;
96         struct stats_record *home;
97         struct stats_record *roaming;
98 };
99
100 GHashTable *stats_hash = NULL;
101
102 static struct stats_file_header *get_hdr(struct stats_file *file)
103 {
104         return (struct stats_file_header *)file->addr;
105 }
106
107 static struct stats_record *get_begin(struct stats_file *file)
108 {
109         unsigned int off = get_hdr(file)->begin;
110
111         return (struct stats_record *)(file->addr + off);
112 }
113
114 static struct stats_record *get_end(struct stats_file *file)
115 {
116         unsigned int off = get_hdr(file)->end;
117
118         return (struct stats_record *)(file->addr + off);
119 }
120
121 static struct stats_record *get_home(struct stats_file *file)
122 {
123         struct stats_file_header *hdr;
124
125         hdr = get_hdr(file);
126
127         if (hdr->home == UINT_MAX)
128                 return NULL;
129
130         return (struct stats_record *)(file->addr + hdr->home);
131 }
132
133 static struct stats_record *get_roaming(struct stats_file *file)
134 {
135         struct stats_file_header *hdr;
136
137         hdr = get_hdr(file);
138
139         if (hdr->roaming == UINT_MAX)
140                 return NULL;
141
142         return (struct stats_record *)(file->addr + hdr->roaming);
143 }
144
145 static void set_begin(struct stats_file *file, struct stats_record *begin)
146 {
147         struct stats_file_header *hdr;
148
149         hdr = get_hdr(file);
150         hdr->begin = (char *)begin - file->addr;
151 }
152
153 static void set_end(struct stats_file *file, struct stats_record *end)
154 {
155         struct stats_file_header *hdr;
156
157         hdr = get_hdr(file);
158         hdr->end = (char *)end - file->addr;
159 }
160
161 static void set_home(struct stats_file *file, struct stats_record *home)
162 {
163         struct stats_file_header *hdr;
164
165         hdr = get_hdr(file);
166         hdr->home = (char *)home - file->addr;
167 }
168
169 static void set_roaming(struct stats_file *file, struct stats_record *roaming)
170 {
171         struct stats_file_header *hdr;
172
173         hdr = get_hdr(file);
174         hdr->roaming = (char *)roaming - file->addr;
175 }
176
177 static struct stats_record *get_next(struct stats_file *file,
178                                         struct stats_record *cur)
179 {
180         cur++;
181
182         if (cur > file->last)
183                 cur = file->first;
184
185         return cur;
186 }
187
188 static void stats_free(gpointer user_data)
189 {
190         struct stats_file *file = user_data;
191
192         msync(file->addr, file->len, MS_SYNC);
193
194         munmap(file->addr, file->len);
195         file->addr = NULL;
196
197         close(file->fd);
198         file->fd = -1;
199
200         g_free(file->name);
201         g_free(file);
202 }
203
204 static int stats_create(struct connman_service *service)
205 {
206         struct stats_file *file;
207
208         file = g_try_new0(struct stats_file, 1);
209         if (file == NULL)
210                 return -ENOMEM;
211
212         g_hash_table_insert(stats_hash, service, file);
213
214         return 0;
215 }
216
217 static void update_first(struct stats_file *file)
218 {
219         file->first = (struct stats_record *)
220                         (file->addr + sizeof(struct stats_file_header));
221 }
222
223 static void update_last(struct stats_file *file)
224 {
225         unsigned int max_entries;
226
227         max_entries = (file->len - sizeof(struct stats_file_header)) /
228                         sizeof(struct stats_record);
229         file->last = file->first + max_entries - 1;
230 }
231
232 static void update_home(struct stats_file *file)
233 {
234         file->home = get_home(file);
235 }
236
237 static void update_roaming(struct stats_file *file)
238 {
239         file->roaming = get_roaming(file);
240 }
241
242 static void stats_file_update_cache(struct stats_file *file)
243 {
244         update_first(file);
245         update_last(file);
246         update_home(file);
247         update_roaming(file);
248 }
249
250 static int stats_file_remap(struct stats_file *file, size_t size)
251 {
252         size_t page_size, new_size;
253         void *addr;
254         int err;
255
256         page_size = sysconf(_SC_PAGESIZE);
257         new_size = (size + page_size - 1) & ~(page_size - 1);
258
259         err = ftruncate(file->fd, new_size);
260         if (err < 0) {
261                 connman_error("ftrunctate error %s for %s",
262                                 strerror(errno), file->name);
263                 return -errno;
264         }
265
266         if (file->addr == NULL) {
267                 addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE,
268                                 MAP_SHARED, file->fd, 0);
269         } else {
270                 addr = mremap(file->addr, file->len, new_size, MREMAP_MAYMOVE);
271         }
272
273         if (addr == MAP_FAILED) {
274                 connman_error("mmap error %s for %s",
275                                 strerror(errno), file->name);
276                 return -errno;
277         }
278
279         file->addr = addr;
280         file->len = new_size;
281
282         stats_file_update_cache(file);
283
284         return 0;
285 }
286
287 static int stats_open(struct connman_service *service,
288                         struct stats_file *file)
289 {
290         struct stat st;
291         int err;
292         size_t size;
293         struct stats_file_header *hdr;
294         connman_bool_t new_file = FALSE;
295
296         file->name = g_strdup_printf("%s/stats/%s.data", STORAGEDIR,
297                         __connman_service_get_ident(service));
298
299         err = stat(file->name, &st);
300         if (err < 0) {
301                 /* according documentation the only possible error is ENOENT */
302                 st.st_size = 0;
303                 new_file = TRUE;
304         }
305
306         file->fd = open(file->name, O_RDWR | O_CREAT, 0644);
307
308         if (file->fd < 0) {
309                 connman_error("open error %s for %s",
310                                 strerror(errno), file->name);
311                 return -errno;
312         }
313
314         file->max_len = STATS_MAX_FILE_SIZE;
315
316         if (st.st_size < sysconf(_SC_PAGESIZE))
317                 size = sysconf(_SC_PAGESIZE);
318         else
319                 size = st.st_size;
320
321         err = stats_file_remap(file, size);
322         if (err < 0)
323                 return err;
324
325         hdr = get_hdr(file);
326
327         if (hdr->magic != MAGIC ||
328                         hdr->begin < sizeof(struct stats_file_header) ||
329                         hdr->end < sizeof(struct stats_file_header) ||
330                         hdr->home < sizeof(struct stats_file_header) ||
331                         hdr->roaming < sizeof(struct stats_file_header) ||
332                         hdr->begin > file->len ||
333                         hdr->end > file->len) {
334                 if (new_file == FALSE) {
335                         /*
336                          * A newly created file can't have a correct
337                          * header so we only warn if the file already
338                          * existed and doesn't have a proper
339                          * header.
340                          */
341                         connman_warn("invalid file header for %s", file->name);
342                 }
343
344                 hdr->magic = MAGIC;
345                 hdr->begin = sizeof(struct stats_file_header);
346                 hdr->end = sizeof(struct stats_file_header);
347                 hdr->home = UINT_MAX;
348                 hdr->roaming = UINT_MAX;
349
350                 stats_file_update_cache(file);
351         }
352
353         return 0;
354 }
355
356 int __connman_stats_service_register(struct connman_service *service)
357 {
358         struct stats_file *file;
359         int err;
360
361         DBG("service %p", service);
362
363         file = g_hash_table_lookup(stats_hash, service);
364         if (file == NULL) {
365                 err = stats_create(service);
366
367                 if (err < 0)
368                         return err;
369
370                 file = g_hash_table_lookup(stats_hash, service);
371         } else {
372                 return -EALREADY;
373         }
374
375         err = stats_open(service, file);
376         if (err < 0)
377                 g_hash_table_remove(stats_hash, service);
378
379         return err;
380 }
381
382 void __connman_stats_service_unregister(struct connman_service *service)
383 {
384         DBG("service %p", service);
385
386         g_hash_table_remove(stats_hash, service);
387 }
388
389 int  __connman_stats_update(struct connman_service *service,
390                                 connman_bool_t roaming,
391                                 struct connman_stats_data *data)
392 {
393         struct stats_file *file;
394         struct stats_record *next;
395         int err;
396
397         file = g_hash_table_lookup(stats_hash, service);
398         if (file == NULL)
399                 return -EEXIST;
400
401         if (file->len < file->max_len &&
402                         file->last == get_end(file)) {
403                 DBG("grow file %s", file->name);
404
405                 err = stats_file_remap(file, file->len + sysconf(_SC_PAGESIZE));
406                 if (err < 0)
407                         return err;
408         }
409
410         next = get_next(file, get_end(file));
411
412         if (next == get_begin(file))
413                 set_begin(file, get_next(file, next));
414
415         next->ts = time(NULL);
416         next->roaming = roaming;
417         memcpy(&next->data, data, sizeof(struct connman_stats_data));
418
419         if (roaming != TRUE)
420                 set_home(file, next);
421         else
422                 set_roaming(file, next);
423
424         set_end(file, next);
425
426         return 0;
427 }
428
429 int __connman_stats_get(struct connman_service *service,
430                                 connman_bool_t roaming,
431                                 struct connman_stats_data *data)
432 {
433         struct stats_file *file;
434         struct stats_record *rec;
435
436         file = g_hash_table_lookup(stats_hash, service);
437         if (file == NULL)
438                 return -EEXIST;
439
440         if (roaming != TRUE)
441                 rec = file->home;
442         else
443                 rec = file->roaming;
444
445         if (rec != NULL) {
446                 memcpy(data, &rec->data,
447                         sizeof(struct connman_stats_data));
448         }
449
450         return 0;
451 }
452
453 int __connman_stats_init(void)
454 {
455         DBG("");
456
457         stats_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
458                                                         NULL, stats_free);
459
460         return 0;
461 }
462
463 void __connman_stats_cleanup(void)
464 {
465         DBG("");
466
467         g_hash_table_destroy(stats_hash);
468         stats_hash = NULL;
469 }