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 void update_first(struct stats_file *file)
206 file->first = (struct stats_record *)
207 (file->addr + sizeof(struct stats_file_header));
210 static void update_last(struct stats_file *file)
212 unsigned int max_entries;
214 max_entries = (file->len - sizeof(struct stats_file_header)) /
215 sizeof(struct stats_record);
216 file->last = file->first + max_entries - 1;
219 static void update_home(struct stats_file *file)
221 file->home = get_home(file);
224 static void update_roaming(struct stats_file *file)
226 file->roaming = get_roaming(file);
229 static void stats_file_update_cache(struct stats_file *file)
234 update_roaming(file);
237 static int stats_file_remap(struct stats_file *file, size_t size)
239 size_t page_size, new_size;
243 page_size = sysconf(_SC_PAGESIZE);
244 new_size = (size + page_size - 1) & ~(page_size - 1);
246 err = ftruncate(file->fd, new_size);
248 connman_error("ftrunctate error %s for %s",
249 strerror(errno), file->name);
253 if (file->addr == NULL) {
254 addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE,
255 MAP_SHARED, file->fd, 0);
257 addr = mremap(file->addr, file->len, new_size, MREMAP_MAYMOVE);
260 if (addr == MAP_FAILED) {
261 connman_error("mmap error %s for %s",
262 strerror(errno), file->name);
267 file->len = new_size;
269 stats_file_update_cache(file);
274 static int stats_open(struct connman_service *service,
275 struct stats_file *file)
280 struct stats_file_header *hdr;
281 connman_bool_t new_file = FALSE;
283 file->name = g_strdup_printf("%s/stats/%s.data", STORAGEDIR,
284 __connman_service_get_ident(service));
286 err = stat(file->name, &st);
288 /* according documentation the only possible error is ENOENT */
293 file->fd = open(file->name, O_RDWR | O_CREAT, 0644);
296 connman_error("open error %s for %s",
297 strerror(errno), file->name);
301 file->max_len = STATS_MAX_FILE_SIZE;
303 if (st.st_size < sysconf(_SC_PAGESIZE))
304 size = sysconf(_SC_PAGESIZE);
308 err = stats_file_remap(file, size);
314 if (hdr->magic != MAGIC ||
315 hdr->begin < sizeof(struct stats_file_header) ||
316 hdr->end < sizeof(struct stats_file_header) ||
317 hdr->home < sizeof(struct stats_file_header) ||
318 hdr->roaming < sizeof(struct stats_file_header) ||
319 hdr->begin > file->len ||
320 hdr->end > file->len) {
321 if (new_file == FALSE) {
323 * A newly created file can't have a correct
324 * header so we only warn if the file already
325 * existed and doesn't have a proper
328 connman_warn("invalid file header for %s", file->name);
332 hdr->begin = sizeof(struct stats_file_header);
333 hdr->end = sizeof(struct stats_file_header);
334 hdr->home = UINT_MAX;
335 hdr->roaming = UINT_MAX;
337 stats_file_update_cache(file);
343 int __connman_stats_service_register(struct connman_service *service)
345 struct stats_file *file;
348 DBG("service %p", service);
350 file = g_hash_table_lookup(stats_hash, service);
352 file = g_try_new0(struct stats_file, 1);
356 g_hash_table_insert(stats_hash, service, file);
361 err = stats_open(service, file);
363 g_hash_table_remove(stats_hash, service);
368 void __connman_stats_service_unregister(struct connman_service *service)
370 DBG("service %p", service);
372 g_hash_table_remove(stats_hash, service);
375 int __connman_stats_update(struct connman_service *service,
376 connman_bool_t roaming,
377 struct connman_stats_data *data)
379 struct stats_file *file;
380 struct stats_record *next;
383 file = g_hash_table_lookup(stats_hash, service);
387 if (file->len < file->max_len &&
388 file->last == get_end(file)) {
389 DBG("grow file %s", file->name);
391 err = stats_file_remap(file, file->len + sysconf(_SC_PAGESIZE));
396 next = get_next(file, get_end(file));
398 if (next == get_begin(file))
399 set_begin(file, get_next(file, next));
401 next->ts = time(NULL);
402 next->roaming = roaming;
403 memcpy(&next->data, data, sizeof(struct connman_stats_data));
406 set_home(file, next);
408 set_roaming(file, next);
415 int __connman_stats_get(struct connman_service *service,
416 connman_bool_t roaming,
417 struct connman_stats_data *data)
419 struct stats_file *file;
420 struct stats_record *rec;
422 file = g_hash_table_lookup(stats_hash, service);
432 memcpy(data, &rec->data,
433 sizeof(struct connman_stats_data));
439 int __connman_stats_init(void)
443 stats_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
449 void __connman_stats_cleanup(void)
453 g_hash_table_destroy(stats_hash);