5 * Copyright (C) 2010 BMW Car IT GmbH. All rights reserved.
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
28 #include <sys/types.h>
38 #define MAGIC 0xFA00B916
41 * Statistics counters are stored into a ring buffer which is stored
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
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)
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
72 struct stats_file_header {
83 struct connman_stats_data data;
94 struct stats_record *first;
95 struct stats_record *last;
96 struct stats_record *home;
97 struct stats_record *roaming;
100 GHashTable *stats_hash = NULL;
102 static struct stats_file_header *get_hdr(struct stats_file *file)
104 return (struct stats_file_header *)file->addr;
107 static struct stats_record *get_begin(struct stats_file *file)
109 unsigned int off = get_hdr(file)->begin;
111 return (struct stats_record *)(file->addr + off);
114 static struct stats_record *get_end(struct stats_file *file)
116 unsigned int off = get_hdr(file)->end;
118 return (struct stats_record *)(file->addr + off);
121 static struct stats_record *get_home(struct stats_file *file)
123 struct stats_file_header *hdr;
127 if (hdr->home == UINT_MAX)
130 return (struct stats_record *)(file->addr + hdr->home);
133 static struct stats_record *get_roaming(struct stats_file *file)
135 struct stats_file_header *hdr;
139 if (hdr->roaming == UINT_MAX)
142 return (struct stats_record *)(file->addr + hdr->roaming);
145 static void set_begin(struct stats_file *file, struct stats_record *begin)
147 struct stats_file_header *hdr;
150 hdr->begin = (char *)begin - file->addr;
153 static void set_end(struct stats_file *file, struct stats_record *end)
155 struct stats_file_header *hdr;
158 hdr->end = (char *)end - file->addr;
161 static void set_home(struct stats_file *file, struct stats_record *home)
163 struct stats_file_header *hdr;
166 hdr->home = (char *)home - file->addr;
169 static void set_roaming(struct stats_file *file, struct stats_record *roaming)
171 struct stats_file_header *hdr;
174 hdr->roaming = (char *)roaming - file->addr;
177 static struct stats_record *get_next(struct stats_file *file,
178 struct stats_record *cur)
182 if (cur > file->last)
188 static void stats_free(gpointer user_data)
190 struct stats_file *file = user_data;
192 msync(file->addr, file->len, MS_SYNC);
194 munmap(file->addr, file->len);
204 static int stats_create(struct connman_service *service)
206 struct stats_file *file;
208 file = g_try_new0(struct stats_file, 1);
212 g_hash_table_insert(stats_hash, service, file);
217 static void update_first(struct stats_file *file)
219 file->first = (struct stats_record *)
220 (file->addr + sizeof(struct stats_file_header));
223 static void update_last(struct stats_file *file)
225 unsigned int max_entries;
227 max_entries = (file->len - sizeof(struct stats_file_header)) /
228 sizeof(struct stats_record);
229 file->last = file->first + max_entries - 1;
232 static void update_home(struct stats_file *file)
234 file->home = get_home(file);
237 static void update_roaming(struct stats_file *file)
239 file->roaming = get_roaming(file);
242 static void stats_file_update_cache(struct stats_file *file)
247 update_roaming(file);
250 static int stats_file_remap(struct stats_file *file, size_t size)
252 size_t page_size, new_size;
256 page_size = sysconf(_SC_PAGESIZE);
257 new_size = (size + page_size - 1) & ~(page_size - 1);
259 err = ftruncate(file->fd, new_size);
261 connman_error("ftrunctate error %s for %s",
262 strerror(errno), file->name);
266 if (file->addr == NULL) {
267 addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE,
268 MAP_SHARED, file->fd, 0);
270 addr = mremap(file->addr, file->len, new_size, MREMAP_MAYMOVE);
273 if (addr == MAP_FAILED) {
274 connman_error("mmap error %s for %s",
275 strerror(errno), file->name);
280 file->len = new_size;
282 stats_file_update_cache(file);
287 static int stats_open(struct connman_service *service,
288 struct stats_file *file)
293 struct stats_file_header *hdr;
294 connman_bool_t new_file = FALSE;
296 file->name = g_strdup_printf("%s/stats/%s.data", STORAGEDIR,
297 __connman_service_get_ident(service));
299 err = stat(file->name, &st);
301 /* according documentation the only possible error is ENOENT */
306 file->fd = open(file->name, O_RDWR | O_CREAT, 0644);
309 connman_error("open error %s for %s",
310 strerror(errno), file->name);
314 file->max_len = STATS_MAX_FILE_SIZE;
316 if (st.st_size < sysconf(_SC_PAGESIZE))
317 size = sysconf(_SC_PAGESIZE);
321 err = stats_file_remap(file, size);
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) {
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
341 connman_warn("invalid file header for %s", file->name);
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;
350 stats_file_update_cache(file);
356 int __connman_stats_service_register(struct connman_service *service)
358 struct stats_file *file;
361 DBG("service %p", service);
363 file = g_hash_table_lookup(stats_hash, service);
365 err = stats_create(service);
370 file = g_hash_table_lookup(stats_hash, service);
375 err = stats_open(service, file);
377 g_hash_table_remove(stats_hash, service);
382 void __connman_stats_service_unregister(struct connman_service *service)
384 DBG("service %p", service);
386 g_hash_table_remove(stats_hash, service);
389 int __connman_stats_update(struct connman_service *service,
390 connman_bool_t roaming,
391 struct connman_stats_data *data)
393 struct stats_file *file;
394 struct stats_record *next;
397 file = g_hash_table_lookup(stats_hash, service);
401 if (file->len < file->max_len &&
402 file->last == get_end(file)) {
403 DBG("grow file %s", file->name);
405 err = stats_file_remap(file, file->len + sysconf(_SC_PAGESIZE));
410 next = get_next(file, get_end(file));
412 if (next == get_begin(file))
413 set_begin(file, get_next(file, next));
415 next->ts = time(NULL);
416 next->roaming = roaming;
417 memcpy(&next->data, data, sizeof(struct connman_stats_data));
420 set_home(file, next);
422 set_roaming(file, next);
429 int __connman_stats_get(struct connman_service *service,
430 connman_bool_t roaming,
431 struct connman_stats_data *data)
433 struct stats_file *file;
434 struct stats_record *rec;
436 file = g_hash_table_lookup(stats_hash, service);
446 memcpy(data, &rec->data,
447 sizeof(struct connman_stats_data));
453 int __connman_stats_init(void)
457 stats_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
463 void __connman_stats_cleanup(void)
467 g_hash_table_destroy(stats_hash);