summaryrefslogtreecommitdiff
path: root/src/stats.c
blob: 828c1ef8ac2dcef4535b428e049be80ffabd004e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2010  BMW Car IT GmbH. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <errno.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <sys/stat.h>

#include "connman.h"

#define MODE		(S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
			S_IXGRP | S_IROTH | S_IXOTH)

#ifdef TEMP_FAILURE_RETRY
#define TFR TEMP_FAILURE_RETRY
#else
#define TFR
#endif

#define MAGIC 0xFA00B916

/*
 * Statistics counters are stored into a ring buffer which is stored
 * into a file
 *
 * File properties:
 *   The ring buffer is mmap to a file
 *   Initialy only the smallest possible amount of disk space is allocated
 *   The files grow to the configured maximal size
 *   The grows by _SC_PAGESIZE step size
 *   For each service a file is created
 *   Each file has a header where the indexes are stored
 *
 * Entries properties:
 *   Each entry has a timestamp
 *   A flag to mark if the entry is either home (0) or roaming (1) entry
 *   The entries are fixed sized (stats_record)
 *
 * Ring buffer properties:
 *   There are to indexes 'begin', 'end', 'home' and 'roaming'
 *   'begin' points to the oldest entry
 *   'end' points to the newest/current entry
 *   'home' points to the current home entry
 *   'roaming' points to the current roaming entry
 *   If 'begin' == 'end' then the buffer is empty
 *   If 'end' + 1 == 'begin then it's full
 *   The ring buffer is valid in the range (begin, end]
 *   If 'home' has the value UINT_MAX', 'home' is invalid
 *   if 'roaming' has the value UINT_MAX', 'roaming' is invalid
 *   'first' points to the first entry in the ring buffer
 *   'last' points to the last entry in the ring buffer
 *
 * History file:
 *   Same format as the ring buffer file
 *   For a period of at least 2 months dayly records are keept
 *   If older, then only a monthly record is keept
 */


struct stats_file_header {
	unsigned int magic;
	unsigned int begin;
	unsigned int end;
	unsigned int home;
	unsigned int roaming;
};

struct stats_record {
	time_t ts;
	unsigned int roaming;
	struct connman_stats_data data;
};

struct stats_file {
	int fd;
	char *name;
	char *addr;
	size_t len;
	size_t max_len;

	/* cached values */
	struct stats_record *first;
	struct stats_record *last;
	struct stats_record *home;
	struct stats_record *roaming;

	/* history */
	char *history_name;
	int account_period_offset;
};

struct stats_iter {
	struct stats_file *file;
	struct stats_record *begin;
	struct stats_record *end;
	struct stats_record *it;
};

GHashTable *stats_hash = NULL;

static struct stats_file_header *get_hdr(struct stats_file *file)
{
	return (struct stats_file_header *)file->addr;
}

static struct stats_record *get_begin(struct stats_file *file)
{
	unsigned int off = get_hdr(file)->begin;

	return (struct stats_record *)(file->addr + off);
}

static struct stats_record *get_end(struct stats_file *file)
{
	unsigned int off = get_hdr(file)->end;

	return (struct stats_record *)(file->addr + off);
}

static struct stats_record *get_home(struct stats_file *file)
{
	struct stats_file_header *hdr;

	hdr = get_hdr(file);

	if (hdr->home == UINT_MAX)
		return NULL;

	return (struct stats_record *)(file->addr + hdr->home);
}

static struct stats_record *get_roaming(struct stats_file *file)
{
	struct stats_file_header *hdr;

	hdr = get_hdr(file);

	if (hdr->roaming == UINT_MAX)
		return NULL;

	return (struct stats_record *)(file->addr + hdr->roaming);
}

static void set_end(struct stats_file *file, struct stats_record *end)
{
	struct stats_file_header *hdr;

	hdr = get_hdr(file);
	hdr->end = (char *)end - file->addr;
}

static void set_home(struct stats_file *file, struct stats_record *home)
{
	struct stats_file_header *hdr;

	hdr = get_hdr(file);
	hdr->home = (char *)home - file->addr;
}

static void set_roaming(struct stats_file *file, struct stats_record *roaming)
{
	struct stats_file_header *hdr;

	hdr = get_hdr(file);
	hdr->roaming = (char *)roaming - file->addr;
}

static struct stats_record *get_next(struct stats_file *file,
					struct stats_record *cur)
{
	cur++;

	if (cur > file->last)
		cur = file->first;

	return cur;
}

static struct stats_record *get_iterator_begin(struct stats_file *file)
{
	return get_next(file, get_begin(file));
}

static struct stats_record *get_iterator_end(struct stats_file *file)
{
	return get_next(file, get_end(file));
}

static void stats_free(gpointer user_data)
{
	struct stats_file *file = user_data;

	if (file == NULL)
		return;

	msync(file->addr, file->len, MS_SYNC);

	munmap(file->addr, file->len);
	file->addr = NULL;

	TFR(close(file->fd));
	file->fd = -1;

	if (file->history_name != NULL) {
		g_free(file->history_name);
		file->history_name = NULL;
	}

	if (file->name != NULL) {
		g_free(file->name);
		file->name = NULL;
	}

	g_free(file);
}

static void update_first(struct stats_file *file)
{
	file->first = (struct stats_record *)
			(file->addr + sizeof(struct stats_file_header));
}

static void update_last(struct stats_file *file)
{
	unsigned int max_entries;

	max_entries = (file->len - sizeof(struct stats_file_header)) /
			sizeof(struct stats_record);
	file->last = file->first + max_entries - 1;
}

static void update_home(struct stats_file *file)
{
	file->home = get_home(file);
}

static void update_roaming(struct stats_file *file)
{
	file->roaming = get_roaming(file);
}

static void stats_file_update_cache(struct stats_file *file)
{
	update_first(file);
	update_last(file);
	update_home(file);
	update_roaming(file);
}

static int stats_file_remap(struct stats_file *file, size_t size)
{
	size_t page_size, new_size;
	void *addr;
	int err;

	DBG("file %p size %zu addr %p len %zu", file, size, file->addr,
		file->len);

	page_size = sysconf(_SC_PAGESIZE);
	new_size = (size + page_size - 1) & ~(page_size - 1);

	err = ftruncate(file->fd, new_size);
	if (err < 0) {
		connman_error("ftrunctate error %s for %s",
				strerror(errno), file->name);
		return -errno;
	}

	if (file->addr == NULL) {
		/*
		 * Though the buffer is not shared between processes, we still
		 * have to take MAP_SHARED because MAP_PRIVATE does not
		 * guarantee that writes will hit the file without an explicit
		 * call to munmap or msync. For more details please read the
		 * mmap man pages.
		 */
		addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE,
				MAP_SHARED, file->fd, 0);
	} else {
		addr = mremap(file->addr, file->len, new_size, MREMAP_MAYMOVE);
	}

	if (addr == MAP_FAILED) {
		connman_error("mmap error %s for %s",
				strerror(errno), file->name);
		if (errno == EINVAL) {
			connman_error("%s might be on a file system, such as "
					"JFFS2, that does not allow shared "
					"writable mappings.", file->name);
		}
		return -errno;
	}

	file->addr = addr;
	file->len = new_size;

	stats_file_update_cache(file);

	return 0;
}

static int stats_open(struct stats_file *file,
			const char *name)
{
	DBG("file %p name %s", file, name);

	file->name = g_strdup(name);

	file->fd = TFR(open(file->name, O_RDWR | O_CREAT | O_CLOEXEC, 0644));
	if (file->fd < 0) {
		connman_error("open error %s for %s",
				strerror(errno), file->name);
		g_free(file->name);
		file->name = NULL;
		return -errno;
	}

	return 0;
}

static int stats_open_temp(struct stats_file *file)
{
	file->name = g_strdup_printf("%s/stats.XXXXXX.tmp",
					STORAGEDIR);
	file->fd = g_mkstemp_full(file->name, O_RDWR | O_CREAT, 0644);
	if (file->fd < 0) {
		connman_error("create tempory file error %s for %s",
				strerror(errno), file->name);
		g_free(file->name);
		file->name = NULL;
		return -errno;
	}

	return 0;
}

static int stats_file_setup(struct stats_file *file)
{
	struct stats_file_header *hdr;
	struct stat st;
	size_t size = 0;
	int err;

	DBG("file %p fd %d name %s", file, file->fd, file->name);

	err = fstat(file->fd, &st);
	if (err < 0) {
		connman_error("fstat error %s for %s\n",
			strerror(errno), file->name);

		TFR(close(file->fd));
		g_free(file->name);
		file->name = NULL;

		return -errno;
	}

	size = (size_t)st.st_size;
	file->max_len = STATS_MAX_FILE_SIZE;

	if (size < (size_t)sysconf(_SC_PAGESIZE))
		size = sysconf(_SC_PAGESIZE);

	err = stats_file_remap(file, size);
	if (err < 0) {
		TFR(close(file->fd));
		g_free(file->name);
		file->name = NULL;

		return err;
	}

	hdr = get_hdr(file);

	if (hdr->magic != MAGIC ||
			hdr->begin < sizeof(struct stats_file_header) ||
			hdr->end < sizeof(struct stats_file_header) ||
			hdr->home < sizeof(struct stats_file_header) ||
			hdr->roaming < sizeof(struct stats_file_header) ||
			hdr->begin > file->len ||
			hdr->end > file->len) {
		hdr->magic = MAGIC;
		hdr->begin = sizeof(struct stats_file_header);
		hdr->end = sizeof(struct stats_file_header);
		hdr->home = UINT_MAX;
		hdr->roaming = UINT_MAX;

		stats_file_update_cache(file);
	}

	return 0;
}


static struct stats_record *get_next_record(struct stats_iter *iter)
{
	if (iter->it != iter->end) {
		struct stats_record *tmp;

		tmp = iter->it;
		iter->it = get_next(iter->file, iter->it);

		return tmp;
	}

	return NULL;
}

static int append_record(struct stats_file *file,
				struct stats_record *rec)
{
	struct stats_record *cur, *next;
	int err;

	if (file->last == get_end(file)) {
		err = stats_file_remap(file, file->len +
					sysconf(_SC_PAGESIZE));
		if (err < 0)
			return err;

		stats_file_update_cache(file);
	}

	cur = get_end(file);
	next = get_next(file, cur);

	memcpy(next, rec, sizeof(struct stats_record));

	set_end(file, next);

	return 0;
}

static struct stats_record *process_file(struct stats_iter *iter,
					struct stats_file *temp_file,
					struct stats_record *cur,
					GDate *date_change_step_size,
					int account_period_offset)
{
	struct stats_record *home, *roaming;
	struct stats_record *next;

	home = NULL;
	roaming = NULL;

	if (cur == NULL)
		cur = get_next_record(iter);
	next = get_next_record(iter);

	while (next != NULL) {
		GDate date_cur;
		GDate date_next;
		int append;

		append = FALSE;

		if (cur->roaming == TRUE)
			roaming = cur;
		else
			home = cur;

		g_date_set_time_t(&date_cur, cur->ts);
		g_date_set_time_t(&date_next, next->ts);

		if (g_date_compare(&date_cur, date_change_step_size) < 0) {
			/* month period size */
			GDateDay day_cur, day_next;
			GDateMonth month_cur, month_next;

			month_cur = g_date_get_month(&date_cur);
			month_next = g_date_get_month(&date_next);

			day_cur = g_date_get_day(&date_cur);
			day_next = g_date_get_day(&date_next);

			if (day_cur == day_next && month_cur != month_next) {
				append = TRUE;
			} else if (day_cur < account_period_offset &&
					day_next >= account_period_offset) {
				append = TRUE;
			}
		} else {
			/* day period size */
			if (g_date_days_between(&date_cur, &date_next) > 0)
				append = TRUE;
		}

		if (append == TRUE) {
			if (home != NULL) {
				append_record(temp_file, home);
				home = NULL;
			}

			if (roaming != NULL) {
				append_record(temp_file, roaming);
				roaming = NULL;
			}
		}

		cur = next;
		next = get_next_record(iter);
	}

	return cur;
}

static int summarize(struct stats_file *data_file,
			struct stats_file *history_file,
			struct stats_file *temp_file)
{
	struct stats_iter data_iter;
	struct stats_iter history_iter;
	struct stats_record *cur, *next;

	GDate today, date_change_step_size;

	/*
	 * First calculate the date when switch from monthly
	 * accounting period size to daily size
	 */
	g_date_set_time_t(&today, time(NULL));

	date_change_step_size = today;
	if (g_date_get_day(&today) - data_file->account_period_offset >= 0)
		g_date_subtract_months(&date_change_step_size, 2);
	else
		g_date_subtract_months(&date_change_step_size, 3);

	g_date_set_day(&date_change_step_size,
			data_file->account_period_offset);


	/* Now process history file */
	cur = NULL;

	if (history_file != NULL) {
		history_iter.file = history_file;
		history_iter.begin = get_iterator_begin(history_iter.file);
		history_iter.end = get_iterator_end(history_iter.file);
		history_iter.it = history_iter.begin;

		cur = process_file(&history_iter, temp_file, NULL,
					&date_change_step_size,
					data_file->account_period_offset);
	}

	data_iter.file = data_file;
	data_iter.begin = get_iterator_begin(data_iter.file);
	data_iter.end = get_iterator_end(data_iter.file);
	data_iter.it = data_iter.begin;

	/*
	 * Ensure date_file records are newer than the history_file
	 * record
	 */
	if (cur != NULL) {
		next = get_next_record(&data_iter);
		while (next != NULL && cur->ts > next->ts)
			next = get_next_record(&data_iter);
	}

	/* And finally process the new data records */
	cur = process_file(&data_iter, temp_file, cur,
				&date_change_step_size,
				data_file->account_period_offset);

	if (cur != NULL)
		append_record(temp_file, cur);

	return 0;
}

static void stats_file_unmap(struct stats_file *file)
{
	msync(file->addr, file->len, MS_SYNC);
	munmap(file->addr, file->len);
	file->addr = NULL;
}

static void stats_file_cleanup(struct stats_file *file)
{
	file->fd = -1;
	g_free(file->name);
	file->name = NULL;
}

static int stats_file_close_swap(struct stats_file *history_file,
					struct stats_file *temp_file)
{
	int err;

	stats_file_unmap(history_file);
	stats_file_unmap(temp_file);

	TFR(close(temp_file->fd));

	unlink(history_file->name);

	err = link(temp_file->name, history_file->name);

	unlink(temp_file->name);

	TFR(close(history_file->fd));

	stats_file_cleanup(history_file);
	stats_file_cleanup(temp_file);

	return err;
}

static int stats_file_history_update(struct stats_file *data_file)
{
	struct stats_file _history_file, *history_file;
	struct stats_file _temp_file, *temp_file;
	int err;

	history_file = &_history_file;
	temp_file = &_temp_file;

	bzero(history_file, sizeof(struct stats_file));
	bzero(temp_file, sizeof(struct stats_file));

	err = stats_open(history_file, data_file->history_name);
	if (err < 0)
		return err;
	stats_file_setup(history_file);

	err = stats_open_temp(temp_file);
	if (err < 0) {
		stats_free(history_file);
		return err;
	}
	stats_file_setup(temp_file);

	summarize(data_file, history_file, temp_file);

	err = stats_file_close_swap(history_file, temp_file);

	return err;
}

int __connman_stats_service_register(struct connman_service *service)
{
	struct stats_file *file;
	char *name, *dir;
	int err;

	DBG("service %p", service);

	file = g_hash_table_lookup(stats_hash, service);
	if (file == NULL) {
		file = g_try_new0(struct stats_file, 1);
		if (file == NULL)
			return -ENOMEM;

		g_hash_table_insert(stats_hash, service, file);
	} else {
		return -EALREADY;
	}

	dir = g_strdup_printf("%s/%s", STORAGEDIR,
				__connman_service_get_ident(service));

	/* If the dir doesn't exist, create it */
	if (!g_file_test(dir, G_FILE_TEST_IS_DIR)) {
		if(mkdir(dir, MODE) < 0) {
			if (errno != EEXIST) {
				g_free(dir);

				err = -errno;
				goto err;
			}
		}
	}

	g_free(dir);

	name = g_strdup_printf("%s/%s/data", STORAGEDIR,
				__connman_service_get_ident(service));
	file->history_name = g_strdup_printf("%s/%s/history", STORAGEDIR,
				__connman_service_get_ident(service));

	/* TODO: Use a global config file instead of hard coded value. */
	file->account_period_offset = 1;

	err = stats_open(file, name);
	g_free(name);
	if (err < 0)
		goto err;

	err = stats_file_setup(file);
	if (err < 0)
		goto err;

	return 0;

err:
	g_hash_table_remove(stats_hash, service);

	return err;
}

void __connman_stats_service_unregister(struct connman_service *service)
{
	DBG("service %p", service);

	g_hash_table_remove(stats_hash, service);
}

int  __connman_stats_update(struct connman_service *service,
				connman_bool_t roaming,
				struct connman_stats_data *data)
{
	struct stats_file *file;
	struct stats_record *next;
	int err;

	file = g_hash_table_lookup(stats_hash, service);
	if (file == NULL)
		return -EEXIST;

	if (file->len < file->max_len &&
			file->last == get_end(file)) {
		DBG("grow file %s", file->name);

		err = stats_file_remap(file, file->len + sysconf(_SC_PAGESIZE));
		if (err < 0)
			return err;
	}

	next = get_next(file, get_end(file));

	if (next == get_begin(file)) {
		DBG("ring buffer is full, update history file");

		if (stats_file_history_update(file) < 0) {
			connman_warn("history file update failed %s",
					file->history_name);
		}
	}

	next->ts = time(NULL);
	next->roaming = roaming;
	memcpy(&next->data, data, sizeof(struct connman_stats_data));

	if (roaming != TRUE)
		set_home(file, next);
	else
		set_roaming(file, next);

	set_end(file, next);

	return 0;
}

int __connman_stats_get(struct connman_service *service,
				connman_bool_t roaming,
				struct connman_stats_data *data)
{
	struct stats_file *file;
	struct stats_record *rec;

	file = g_hash_table_lookup(stats_hash, service);
	if (file == NULL)
		return -EEXIST;

	if (roaming != TRUE)
		rec = file->home;
	else
		rec = file->roaming;

	if (rec != NULL) {
		memcpy(data, &rec->data,
			sizeof(struct connman_stats_data));
	}

	return 0;
}

int __connman_stats_init(void)
{
	DBG("");

	stats_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
							NULL, stats_free);

	return 0;
}

void __connman_stats_cleanup(void)
{
	DBG("");

	g_hash_table_destroy(stats_hash);
	stats_hash = NULL;
}