Merge "Update settings when device information in device_list is changed" into tizen
[platform/upstream/connman.git] / tools / stats-tool.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2010-2014  BMW Car IT GmbH.
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 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <sys/mman.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <string.h>
32
33 #include <sys/time.h>
34 #include <time.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdbool.h>
38 #include <errno.h>
39
40 #include <glib.h>
41 #include <glib/gstdio.h>
42
43 #ifdef TEMP_FAILURE_RETRY
44 #define TFR TEMP_FAILURE_RETRY
45 #else
46 #define TFR
47 #endif
48
49 #define MAGIC 0xFA00B916
50
51 struct connman_stats_data {
52         unsigned int rx_packets;
53         unsigned int tx_packets;
54         unsigned int rx_bytes;
55         unsigned int tx_bytes;
56         unsigned int rx_errors;
57         unsigned int tx_errors;
58         unsigned int rx_dropped;
59         unsigned int tx_dropped;
60         unsigned int time;
61 };
62
63 struct stats_file_header {
64         unsigned int magic;
65         unsigned int begin;
66         unsigned int end;
67         unsigned int home;
68         unsigned int roaming;
69 };
70
71 struct stats_record {
72         time_t ts;
73         unsigned int roaming;
74         struct connman_stats_data data;
75 };
76
77 struct stats_file {
78         int fd;
79         char *name;
80         char *addr;
81         size_t len;
82         size_t max_len;
83
84         /* cached values */
85         int max_nr;
86         int nr;
87         struct stats_record *first;
88         struct stats_record *last;
89         struct stats_record *home_first;
90         struct stats_record *roaming_first;
91 };
92
93 struct stats_iter {
94         struct stats_file *file;
95         struct stats_record *begin;
96         struct stats_record *end;
97         struct stats_record *it;
98 };
99
100 static gint option_create = 0;
101 static gint option_interval = 3;
102 static bool option_dump = false;
103 static bool option_summary = false;
104 static char *option_info_file_name = NULL;
105 static time_t option_start_ts = -1;
106 static char *option_last_file_name = NULL;
107
108 static bool parse_start_ts(const char *key, const char *value,
109                                         gpointer user_data, GError **error)
110 {
111         struct tm tm;
112
113         strptime(value, "%FT%TZ", &tm);
114         option_start_ts = mktime(&tm);
115
116         return true;
117 }
118
119 static GOptionEntry options[] = {
120         { "create", 'c', 0, G_OPTION_ARG_INT, &option_create,
121                         "Create a .data file with NR faked entries", "NR" },
122         { "interval", 'i', 0, G_OPTION_ARG_INT, &option_interval,
123                         "Interval in seconds (used with create)", "INTERVAL" },
124         { "dump", 'd', 0, G_OPTION_ARG_NONE, &option_dump,
125                         "Dump contents of .data file" },
126         { "summary", 's', 0, G_OPTION_ARG_NONE, &option_summary,
127                         "Summary of .data file" },
128         { "info", 'f', 0, G_OPTION_ARG_FILENAME, &option_info_file_name,
129                         ".info file name" },
130         { "startts", 't', 0, G_OPTION_ARG_CALLBACK, parse_start_ts,
131                         "Set start time for creating .data file "
132                         "(example 2010-11-05T23:00:12Z)", "TS"},
133         { "last", 'l', 0, G_OPTION_ARG_FILENAME, &option_last_file_name,
134                           "Start values from last .data file" },
135         { NULL },
136 };
137
138 static struct stats_file_header *get_hdr(struct stats_file *file)
139 {
140         return (struct stats_file_header *)file->addr;
141 }
142
143 static struct stats_record *get_begin(struct stats_file *file)
144 {
145         unsigned int off = get_hdr(file)->begin;
146
147         return (struct stats_record *)(file->addr + off);
148 }
149
150 static struct stats_record *get_end(struct stats_file *file)
151 {
152         unsigned int off = get_hdr(file)->end;
153
154         return (struct stats_record *)(file->addr + off);
155 }
156
157 static struct stats_record *get_home(struct stats_file *file)
158 {
159         struct stats_file_header *hdr;
160
161         hdr = get_hdr(file);
162
163         if (hdr->home == UINT_MAX)
164                 return NULL;
165
166         return (struct stats_record *)(file->addr + hdr->home);
167 }
168
169 static struct stats_record *get_roaming(struct stats_file *file)
170 {
171         struct stats_file_header *hdr;
172
173         hdr = get_hdr(file);
174
175         if (hdr->roaming == UINT_MAX)
176                 return NULL;
177
178         return (struct stats_record *)(file->addr + hdr->roaming);
179 }
180
181 static void set_end(struct stats_file *file, struct stats_record *end)
182 {
183         struct stats_file_header *hdr;
184
185         hdr = get_hdr(file);
186         hdr->end = (char *)end - file->addr;
187 }
188
189 static int get_index(struct stats_file *file, struct stats_record *rec)
190 {
191         return rec - file->first;
192 }
193
194 static struct stats_record *get_next(struct stats_file *file,
195                                         struct stats_record *cur)
196 {
197         cur++;
198
199         if (cur > file->last)
200                 cur = file->first;
201
202         return cur;
203 }
204
205 static struct stats_record *get_iterator_begin(struct stats_file *file)
206 {
207         return get_next(file, get_begin(file));
208 }
209
210 static struct stats_record *get_iterator_end(struct stats_file *file)
211 {
212         return get_next(file, get_end(file));
213 }
214
215 static void stats_print_record(struct stats_record *rec)
216 {
217         char buffer[30];
218
219         strftime(buffer, 30, "%d-%m-%Y %T", localtime(&rec->ts));
220         printf("%p %lld %s %01d %d %d %d %d %d %d %d %d %d\n",
221                 rec, (long long int)rec->ts, buffer,
222                 rec->roaming,
223                 rec->data.rx_packets,
224                 rec->data.tx_packets,
225                 rec->data.rx_bytes,
226                 rec->data.tx_bytes,
227                 rec->data.rx_errors,
228                 rec->data.tx_errors,
229                 rec->data.rx_dropped,
230                 rec->data.tx_dropped,
231                 rec->data.time);
232 }
233
234 static void stats_hdr_info(struct stats_file *file)
235 {
236         struct stats_file_header *hdr;
237         struct stats_record *begin, *end, *home, *roaming;
238         unsigned int home_idx, roaming_idx;
239
240         hdr = get_hdr(file);
241         begin = get_begin(file);
242         end = get_end(file);
243
244         home = get_home(file);
245         if (!home)
246                 home_idx = UINT_MAX;
247         else
248                 home_idx = get_index(file, home);
249
250         roaming = get_roaming(file);
251         if (!roaming)
252                 roaming_idx = UINT_MAX;
253         else
254                 roaming_idx = get_index(file, roaming);
255
256         printf("Data Structure Sizes\n");
257         printf("  sizeof header   %zd/0x%02zx\n",
258                 sizeof(struct stats_file_header),
259                 sizeof(struct stats_file_header));
260         printf("  sizeof entry    %zd/0%02zx\n\n",
261                 sizeof(struct stats_record),
262                 sizeof(struct stats_record));
263
264         printf("File\n");
265         printf("  addr            %p\n",  file->addr);
266         printf("  len             %zd\n", file->len);
267
268         printf("  max nr entries  %d\n", file->max_nr);
269         printf("  nr entries      %d\n\n", file->nr);
270
271         printf("Header\n");
272         printf("  magic           0x%08x\n", hdr->magic);
273         printf("  begin           [%d] 0x%08x\n",
274                 get_index(file, begin), hdr->begin);
275         printf("  end             [%d] 0x%08x\n",
276                 get_index(file, end), hdr->end);
277         printf("  home            [%d] 0x%08x\n",
278                 home_idx, hdr->home);
279         printf("  roaming         [%d] 0x%08x\n\n",
280                 roaming_idx, hdr->roaming);
281
282
283         printf("Pointers\n");
284         printf("  hdr             %p\n", hdr);
285         printf("  begin           %p\n", begin);
286         printf("  end             %p\n", end);
287         printf("  home            %p\n", home);
288         printf("  romaing         %p\n", roaming);
289         printf("  first           %p\n", file->first);
290         printf("  last            %p\n\n", file->last);
291 }
292
293 static void stats_print_entries(struct stats_file *file)
294 {
295         struct stats_record *it;
296         int i;
297
298         printf("[ idx] ptr ts ts rx_packets tx_packets rx_bytes "
299                 "tx_bytes rx_errors tx_errors rx_dropped tx_dropped time\n\n");
300
301         for (i = 0, it = file->first; it <= file->last; it++, i++) {
302                 printf("[%04d] ", i);
303                 stats_print_record(it);
304         }
305 }
306
307 static void stats_print_rec_diff(struct stats_record *begin,
308                                         struct stats_record *end)
309 {
310         printf("\trx_packets: %d\n",
311                 end->data.rx_packets - begin->data.rx_packets);
312         printf("\ttx_packets: %d\n",
313                 end->data.tx_packets - begin->data.tx_packets);
314         printf("\trx_bytes:   %d\n",
315                 end->data.rx_bytes - begin->data.rx_bytes);
316         printf("\ttx_bytes:   %d\n",
317                 end->data.tx_bytes - begin->data.tx_bytes);
318         printf("\trx_errors:  %d\n",
319                 end->data.rx_errors - begin->data.rx_errors);
320         printf("\ttx_errors:  %d\n",
321                 end->data.tx_errors - begin->data.tx_errors);
322         printf("\trx_dropped: %d\n",
323                 end->data.rx_dropped - begin->data.rx_dropped);
324         printf("\ttx_dropped: %d\n",
325                 end->data.tx_dropped - begin->data.tx_dropped);
326         printf("\ttime:       %d\n",
327                 end->data.time - begin->data.time);
328 }
329
330 static void stats_print_diff(struct stats_file *file)
331 {
332         struct stats_record *begin, *end;
333
334         begin = get_begin(file);
335         begin = get_next(file, begin);
336         end = get_end(file);
337
338         printf("\n(begin + 1)\n");
339         printf("\t[%04d] ", get_index(file, begin));
340         stats_print_record(begin);
341         printf("end\n");
342         printf("\t[%04d] ", get_index(file, end));
343         stats_print_record(end);
344
345         if (file->home_first && get_home(file)) {
346                 printf("\nhome\n");
347                 stats_print_rec_diff(file->home_first, get_home(file));
348         }
349
350         if (file->roaming_first && get_roaming(file)) {
351                 printf("\roaming\n");
352                 stats_print_rec_diff(file->roaming_first, get_roaming(file));
353         }
354 }
355
356 static void update_max_nr_entries(struct stats_file *file)
357 {
358         file->max_nr = (file->len - sizeof(struct stats_file_header)) /
359                 sizeof(struct stats_record);
360 }
361
362 static void update_nr_entries(struct stats_file *file)
363 {
364         struct stats_record *begin, *end;
365         int nr;
366
367         begin = get_begin(file);
368         end = get_end(file);
369
370         nr = get_index(file, end) - get_index(file, begin);
371
372         if (nr < 0)
373                 nr += file->max_nr;
374
375         file->nr = nr;
376 }
377
378 static void update_first(struct stats_file *file)
379 {
380         file->first = (struct stats_record *)(file->addr +
381                                         sizeof(struct stats_file_header));
382 }
383
384 static void update_last(struct stats_file *file)
385 {
386         struct stats_record *last;
387
388         last = file->first;
389         last += file->max_nr - 1;
390
391         file->last = last;
392 }
393
394 static int stats_file_update_cache(struct stats_file *file)
395 {
396         struct stats_record *it, *end;
397
398         update_max_nr_entries(file);
399         update_nr_entries(file);
400         update_first(file);
401         update_last(file);
402         file->home_first = NULL;
403         file->roaming_first = NULL;
404
405         end = get_iterator_end(file);
406         for (it = get_iterator_begin(file);
407                         it != end;
408                         it = get_next(file, it)) {
409
410                 if (!file->home_first && it->roaming == 0)
411                         file->home_first = it;
412
413                 if (!file->roaming_first && it->roaming == 1)
414                         file->roaming_first = it;
415
416                 if (file->home_first && file->roaming_first)
417                         break;
418         }
419
420         return 0;
421 }
422
423 static int stats_file_remap(struct stats_file *file, size_t size)
424 {
425         size_t page_size, new_size;
426         void *addr;
427         int err;
428
429         page_size = sysconf(_SC_PAGESIZE);
430         new_size = (size + page_size - 1) & ~(page_size - 1);
431
432         err = ftruncate(file->fd, new_size);
433         if (err < 0) {
434                 fprintf(stderr, "ftrunctate error %s for %s",
435                                 strerror(errno), file->name);
436                 return -errno;
437         }
438
439         if (!file->addr) {
440                 addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE,
441                                 MAP_SHARED, file->fd, 0);
442         } else {
443                 addr = mremap(file->addr, file->len, new_size, MREMAP_MAYMOVE);
444         }
445
446         if (addr == MAP_FAILED) {
447                 fprintf(stderr, "mmap error %s for %s\n",
448                         strerror(errno), file->name);
449                 return -errno;
450         }
451
452         file->addr = addr;
453         file->len = new_size;
454         file->max_len = new_size;
455
456         return 0;
457 }
458
459 static int stats_open(struct stats_file *file, const char *name)
460 {
461         struct stats_file_header *hdr;
462         struct stat tm;
463         int err;
464         size_t size = 0;
465
466         bzero(file, sizeof(struct stats_file));
467
468         if (name) {
469                 file->name = g_strdup(name);
470
471                 file->fd = TFR(open(file->name,
472                                         O_RDWR | O_CREAT | O_CLOEXEC, 0644));
473                 if (file->fd == -1) {
474                         fprintf(stderr, "open error %s for %s\n",
475                                 strerror(errno), file->name);
476                         return -errno;
477                 }
478
479                 err = fstat(file->fd, &tm);
480                 if (err < 0) {
481                         fprintf(stderr, "fstat error %s for %s\n",
482                                 strerror(errno), file->name);
483                         return err;
484                 }
485
486                 size = (size_t)tm.st_size;
487         } else {
488                 file->name = g_strdup("stats.XXXXXX.tmp");
489                 file->fd = g_mkstemp_full(file->name, O_RDWR | O_CREAT, 0644);
490                 if (file->fd == -1) {
491                         fprintf(stderr, "creating tmp failed\n");
492                         return -1;
493                 }
494         }
495
496         if (size == 0)
497                 size = sysconf(_SC_PAGESIZE);
498
499         err = stats_file_remap(file, size);
500         if (err < 0) {
501                 fprintf(stderr, "remap failed\n");
502                 return err;
503         }
504
505         /* Initialize new file */
506         hdr = get_hdr(file);
507         if (hdr->magic != MAGIC ||
508                         hdr->begin < sizeof(struct stats_file_header) ||
509                         hdr->end < sizeof(struct stats_file_header) ||
510                         hdr->home < sizeof(struct stats_file_header) ||
511                         hdr->roaming < sizeof(struct stats_file_header) ||
512                         hdr->begin > file->len ||
513                         hdr->end > file->len) {
514                 hdr->magic = MAGIC;
515                 hdr->begin = sizeof(struct stats_file_header);
516                 hdr->end = sizeof(struct stats_file_header);
517                 hdr->home = UINT_MAX;
518                 hdr->roaming = UINT_MAX;
519
520         }
521         stats_file_update_cache(file);
522
523         return 0;
524 }
525
526 static void stats_close(struct stats_file *file)
527 {
528         munmap(file->addr, file->len);
529         close(file->fd);
530         g_free(file->name);
531 }
532
533 static int stats_create(struct stats_file *file, unsigned int nr,
534                         unsigned int interval, time_t start_ts,
535                         struct stats_record *start)
536 {
537         unsigned int i;
538         int err;
539         struct stats_record *cur, *next;
540         struct stats_file_header *hdr;
541         unsigned int pkt;
542         unsigned int step_ts;
543         unsigned int roaming = FALSE;
544
545         hdr = get_hdr(file);
546
547         hdr->magic = MAGIC;
548         hdr->begin = sizeof(struct stats_file_header);
549         hdr->end = sizeof(struct stats_file_header);
550         hdr->home = UINT_MAX;
551         hdr->roaming = UINT_MAX;
552
553         stats_file_update_cache(file);
554
555         if (start) {
556                 struct stats_record *rec;
557
558                 rec = get_end(file);
559                 memcpy(rec, start, sizeof(struct stats_record));
560         } else {
561                 get_end(file)->ts = start_ts;
562         }
563
564         for (i = 0; i < nr; i++) {
565                 if (file->last == get_end(file)) {
566                         err = stats_file_remap(file, file->len +
567                                                 sysconf(_SC_PAGESIZE));
568                         if (err < 0)
569                                 return err;
570
571                         stats_file_update_cache(file);
572                 }
573                 cur = get_end(file);
574                 next = get_next(file, cur);
575
576                 step_ts = (rand() % interval);
577                 if (step_ts == 0)
578                         step_ts = 1;
579
580                 next->ts = cur->ts + step_ts;
581                 next->roaming = roaming;
582                 next->data.time = cur->data.time + step_ts;
583
584                 next->data.rx_packets = cur->data.rx_packets;
585                 next->data.rx_bytes = cur->data.rx_bytes;
586
587                 if (rand() % 3 == 0) {
588                         pkt = rand() % 5;
589                         next->data.rx_packets += pkt;
590                         next->data.rx_bytes += pkt * (rand() % 1500);
591                 }
592
593                 next->data.tx_packets = cur->data.tx_packets;
594                 next->data.tx_bytes = cur->data.tx_bytes;
595
596                 if (rand() % 3 == 0) {
597                         pkt = rand() % 5;
598                         next->data.tx_packets += pkt;
599                         next->data.tx_bytes += pkt * (rand() % 1500);
600                 }
601
602                 set_end(file, next);
603
604                 if ((rand() % 50) == 0)
605                         roaming = roaming ? FALSE : TRUE;
606
607         }
608
609         return 0;
610 }
611
612 static struct stats_record *get_next_record(struct stats_iter *iter)
613 {
614         if (iter->it != iter->end) {
615                 struct stats_record *tmp;
616
617                 tmp = iter->it;
618                 iter->it = get_next(iter->file, iter->it);
619
620                 return tmp;
621         }
622
623         return NULL;
624 }
625
626 static int append_record(struct stats_file *file,
627                                 struct stats_record *rec)
628 {
629         struct stats_record *cur, *next;
630         int err;
631
632         if (file->last == get_end(file)) {
633                 err = stats_file_remap(file, file->len +
634                                         sysconf(_SC_PAGESIZE));
635                 if (err < 0)
636                         return err;
637
638                 stats_file_update_cache(file);
639         }
640
641         cur = get_end(file);
642         next = get_next(file, cur);
643
644         memcpy(next, rec, sizeof(struct stats_record));
645
646         set_end(file, next);
647
648         return 0;
649 }
650
651 static struct stats_record *process_file(struct stats_iter *iter,
652                                         struct stats_file *temp_file,
653                                         struct stats_record *cur,
654                                         GDate *date_change_step_size,
655                                         int account_period_offset)
656 {
657         struct stats_record *home, *roaming;
658         struct stats_record *next;
659
660         home = NULL;
661         roaming = NULL;
662
663         if (!cur)
664                 cur = get_next_record(iter);
665         next = get_next_record(iter);
666
667         while (next) {
668                 GDate date_cur;
669                 GDate date_next;
670                 int append;
671
672                 append = FALSE;
673
674                 if (cur->roaming)
675                         roaming = cur;
676                 else
677                         home = cur;
678
679                 g_date_set_time_t(&date_cur, cur->ts);
680                 g_date_set_time_t(&date_next, next->ts);
681
682                 if (g_date_compare(&date_cur, date_change_step_size) < 0) {
683                         /* month period size */
684                         GDateDay day_cur, day_next;
685                         GDateMonth month_cur, month_next;
686
687                         month_cur = g_date_get_month(&date_cur);
688                         month_next = g_date_get_month(&date_next);
689
690                         day_cur = g_date_get_day(&date_cur);
691                         day_next = g_date_get_day(&date_next);
692
693                         if (day_cur == day_next && month_cur != month_next)
694                                 append = TRUE;
695                         else if (day_cur < account_period_offset
696                                         && day_next >= account_period_offset)
697                                 append = TRUE;
698                 } else {
699                         /* day period size */
700                         if (g_date_days_between(&date_cur, &date_next) > 0)
701                                 append = TRUE;
702                 }
703
704                 if (append) {
705                         if (home) {
706                                 append_record(temp_file, home);
707                                 home = NULL;
708                         }
709
710                         if (roaming) {
711                                 append_record(temp_file, roaming);
712                                 roaming = NULL;
713                         }
714                 }
715
716                 cur = next;
717                 next = get_next_record(iter);
718         }
719
720         return cur;
721 }
722
723 static int summarize(struct stats_file *data_file,
724                         struct stats_file *history_file,
725                         struct stats_file *temp_file,
726                         int account_period_offset)
727 {
728         struct stats_iter data_iter;
729         struct stats_iter history_iter;
730         struct stats_record *cur, *next;
731
732         GDate today, date_change_step_size;
733
734         /*
735          * First calculate the date when switch from monthly
736          * accounting period size to daily size
737          */
738         g_date_set_time_t(&today, time(NULL));
739
740         date_change_step_size = today;
741         if (g_date_get_day(&today) - account_period_offset >= 0)
742                 g_date_subtract_months(&date_change_step_size, 2);
743         else
744                 g_date_subtract_months(&date_change_step_size, 3);
745
746         g_date_set_day(&date_change_step_size, account_period_offset);
747
748
749         /* Now process history file */
750         cur = NULL;
751
752         if (history_file) {
753                 history_iter.file = history_file;
754                 history_iter.begin = get_iterator_begin(history_iter.file);
755                 history_iter.end = get_iterator_end(history_iter.file);
756                 history_iter.it = history_iter.begin;
757
758                 cur = process_file(&history_iter, temp_file, NULL,
759                                         &date_change_step_size,
760                                         account_period_offset);
761         }
762
763         data_iter.file = data_file;
764         data_iter.begin = get_iterator_begin(data_iter.file);
765         data_iter.end = get_iterator_end(data_iter.file);
766         data_iter.it = data_iter.begin;
767
768         /*
769          * Ensure date_file records are newer than the history_file
770          * record
771          */
772         if (cur) {
773                 next = get_next_record(&data_iter);
774                 while (next && cur->ts > next->ts)
775                         next = get_next_record(&data_iter);
776         }
777
778         /* And finally process the new data records */
779         cur = process_file(&data_iter, temp_file, cur,
780                                 &date_change_step_size, account_period_offset);
781
782         if (cur)
783                 append_record(temp_file, cur);
784
785         return 0;
786 }
787
788 static void swap_and_close_files(struct stats_file *history_file,
789                         struct stats_file *temp_file)
790 {
791         munmap(history_file->addr, history_file->len);
792         munmap(temp_file->addr, temp_file->len);
793
794         close(temp_file->fd);
795
796         unlink(history_file->name);
797
798         if (link(temp_file->name, history_file->name) < 0)
799                 return;
800
801         unlink(temp_file->name);
802         close(history_file->fd);
803 }
804
805 static void history_file_update(struct stats_file *data_file,
806                                 const char *history_file_name)
807 {
808         struct stats_file _history_file;
809         struct stats_file tempory_file;
810
811         struct stats_file *history_file = NULL;
812
813         if (stats_open(&_history_file, history_file_name) == 0)
814                 history_file = &_history_file;
815
816         if (stats_open(&tempory_file, NULL) < 0) {
817                 if (history_file)
818                         stats_close(history_file);
819                 return;
820         }
821
822         summarize(data_file, history_file, &tempory_file, 13);
823
824         swap_and_close_files(history_file, &tempory_file);
825 }
826
827 int main(int argc, char *argv[])
828 {
829         GOptionContext *context;
830         GError *error = NULL;
831
832         struct stats_file_header *hdr;
833         struct stats_file data, *data_file;
834         struct stats_record *rec;
835         time_t start_ts;
836         int err;
837
838         rec = NULL;
839
840         data_file = &data;
841
842         putenv("TZ=GMT0");
843
844         context = g_option_context_new(NULL);
845         g_option_context_add_main_entries(context, options, NULL);
846
847         if (!g_option_context_parse(context, &argc, &argv, &error)) {
848                 if (error) {
849                         g_printerr("%s\n", error->message);
850                         g_error_free(error);
851                 } else
852                         g_printerr("An unknown error occurred\n");
853                 exit(1);
854         }
855
856         g_option_context_free(context);
857
858         if (argc < 2) {
859                 printf("Usage: %s [FILENAME]\n", argv[0]);
860                 exit(0);
861         }
862
863         err = stats_open(data_file, argv[1]);
864         if (err < 0) {
865                 fprintf(stderr, "failed open file %s\n", argv[1]);
866                 exit(1);
867         }
868
869         if (option_last_file_name) {
870                 struct stats_file last;
871                 if (stats_open(&last, option_last_file_name) < 0) {
872                         fprintf(stderr, "failed open file %s\n",
873                                 option_last_file_name);
874                         exit(1);
875                 }
876
877                 rec = get_end(&last);
878         }
879
880         if (option_start_ts == -1)
881                 start_ts = time(NULL);
882         else
883                 start_ts = option_start_ts;
884
885         if (option_interval == 0) {
886                 printf("interval cannot be zero, using the default value\n");
887                 option_interval = 3;
888         }
889
890         if (option_create > 0)
891                 stats_create(data_file, option_create, option_interval,
892                                 start_ts, rec);
893
894         hdr = get_hdr(data_file);
895         if (hdr->magic != MAGIC) {
896                 fprintf(stderr, "header file magic test failed\n");
897                 goto err;
898         }
899
900         stats_file_update_cache(data_file);
901
902         stats_hdr_info(data_file);
903
904         if (option_dump)
905                 stats_print_entries(data_file);
906
907         if (option_summary)
908                 stats_print_diff(data_file);
909
910         if (option_info_file_name)
911                 history_file_update(data_file, option_info_file_name);
912
913 err:
914         stats_close(data_file);
915
916         return 0;
917 }