Reorganize source tree to use single autotools instance
[vpp.git] / src / tools / g2 / clib.c
1 /* 
2  *------------------------------------------------------------------
3  * Copyright (c) 2009-2016 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <netinet/in.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <sys/fcntl.h>
25 #include <sys/mman.h>
26 #include <unistd.h>
27 #include <ctype.h>
28 #include <vppinfra/clib.h>
29 #include <vppinfra/vec.h>
30 #include <vppinfra/hash.h>
31 #include <vppinfra/elog.h>
32 #include <pwd.h>
33 #include <stdarg.h>
34 #include <time.h>
35 #include "cpel.h"
36 #include "g2.h"
37
38 int widest_track_format;
39
40 typedef struct bound_track_ {
41     u32 track;
42     u8  *track_str;
43 } bound_track_t;
44
45 bound_track_t *bound_tracks;
46
47 uword *the_evtdef_hash; /* (event-id, event-definition) hash */
48 uword *the_trackdef_hash; /* (track-id, track-definition) hash */
49
50 elog_main_t elog_main;
51
52 void *get_clib_event (unsigned int datum)
53 {
54     elog_event_t *ep = vec_elt_at_index (elog_main.events, datum);
55     return (void *)ep;
56 }
57
58 /*
59  * read_clib_file
60  */
61 int read_clib_file(char *clib_file)
62 {
63     static FILE *ofp;
64     clib_error_t *error = 0;
65     int i;
66     elog_main_t *em = &elog_main;
67     double starttime, delta;
68
69     vec_free(em->events);
70     vec_free(em->event_types);
71     if (the_trackdef_hash)
72         hash_free(the_trackdef_hash);
73
74     the_trackdef_hash = hash_create (0, sizeof (uword));
75
76     error = elog_read_file (&elog_main, clib_file);
77
78     if (error) {
79         fformat(stderr, "%U", format_clib_error, error);
80         return (1);
81     }
82
83     if (ofp == NULL) {
84         ofp = fdopen(2, "w");
85         if (ofp == NULL) {
86             fprintf(stderr, "Couldn't fdopen(2)?\n");
87             exit(1);
88         }
89     }
90
91     em = &elog_main;
92
93     for (i = 0; i < vec_len (em->tracks); i++) {
94         u32 track_code;
95         bound_track_t * btp;
96         elog_track_t * t;
97         uword * p;
98         int track_strlen;
99
100         t = &em->tracks[i];
101         track_code = i;
102         p = hash_get(the_trackdef_hash, track_code);
103         if (p) {
104             fprintf(ofp, "track %d redefined, retain first definition\n",
105                     track_code);
106             continue;
107         }
108         vec_add2(bound_tracks, btp, 1);
109         btp->track = track_code;
110         btp->track_str = (u8 *) t->name;
111         hash_set(the_trackdef_hash, track_code, btp - bound_tracks);
112
113         track_strlen = strlen((char *)btp->track_str);
114         if (track_strlen > widest_track_format)
115             widest_track_format = track_strlen;
116     }
117
118     initialize_events();
119
120     for (i = 0; i < vec_len (em->event_types); i++) {
121         elog_event_type_t *ep;
122         u8 *tmp;
123
124         ep = vec_elt_at_index(em->event_types, i);
125         tmp = (u8 *) vec_dup(ep->format);
126         vec_add1(tmp,0);
127         add_event_from_clib_file (ep->type_index_plus_one, (char *) tmp, i);
128         vec_free(tmp);
129     }
130
131     finalize_events();
132
133     em->events = elog_get_events (em);
134
135     cpel_event_init(vec_len(em->events));
136
137     starttime = em->events[0].time;
138
139     for (i = 0; i < vec_len (em->events); i++) {
140         elog_event_t *ep;
141
142         ep = vec_elt_at_index(em->events, i);
143
144         delta = ep->time - starttime;
145
146         add_clib_event (delta, ep->track, ep->type + 1, i);
147     }
148
149     cpel_event_finalize();
150
151     set_pid_ax_width(8*widest_track_format);
152
153     return(0);
154 }