misc: use right include for fctnl.h and poll.h
[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/mman.h>
25 #include <unistd.h>
26 #include <ctype.h>
27 #include <vppinfra/clib.h>
28 #include <vppinfra/vec.h>
29 #include <vppinfra/hash.h>
30 #include <vppinfra/elog.h>
31 #include <pwd.h>
32 #include <stdarg.h>
33 #include <time.h>
34 #include "cpel.h"
35 #include "g2.h"
36
37 extern int widest_track_format;
38
39 typedef struct bound_track_ {
40     u32 track;
41     u8  *track_str;
42 } bound_track_t;
43
44 extern bound_track_t *bound_tracks;
45
46 extern uword *the_evtdef_hash; /* (event-id, event-definition) hash */
47 extern uword *the_trackdef_hash; /* (track-id, track-definition) hash */
48
49 extern elog_main_t elog_main;
50
51 void *get_clib_event (unsigned int datum)
52 {
53     elog_event_t *ep = vec_elt_at_index (elog_main.events, datum);
54     return (void *)ep;
55 }
56
57 /*
58  * read_clib_file
59  */
60 int read_clib_file(char *clib_file)
61 {
62     static FILE *ofp;
63     clib_error_t *error = 0;
64     int i;
65     elog_main_t *em = &elog_main;
66     double starttime, delta;
67
68     vec_free(em->events);
69     vec_free(em->event_types);
70     if (the_trackdef_hash)
71         hash_free(the_trackdef_hash);
72
73     the_trackdef_hash = hash_create (0, sizeof (uword));
74
75     error = elog_read_file (&elog_main, clib_file);
76
77     if (error) {
78         fformat(stderr, "%U", format_clib_error, error);
79         return (1);
80     }
81
82     if (ofp == NULL) {
83         ofp = fdopen(2, "w");
84         if (ofp == NULL) {
85             fprintf(stderr, "Couldn't fdopen(2)?\n");
86             exit(1);
87         }
88     }
89
90     em = &elog_main;
91
92     for (i = 0; i < vec_len (em->tracks); i++) {
93         u32 track_code;
94         bound_track_t * btp;
95         elog_track_t * t;
96         uword * p;
97         int track_strlen;
98
99         t = &em->tracks[i];
100         track_code = i;
101         p = hash_get(the_trackdef_hash, track_code);
102         if (p) {
103             fprintf(ofp, "track %d redefined, retain first definition\n",
104                     track_code);
105             continue;
106         }
107         vec_add2(bound_tracks, btp, 1);
108         btp->track = track_code;
109         btp->track_str = (u8 *) t->name;
110         hash_set(the_trackdef_hash, track_code, btp - bound_tracks);
111
112         track_strlen = strlen((char *)btp->track_str);
113         if (track_strlen > widest_track_format)
114             widest_track_format = track_strlen;
115     }
116
117     initialize_events();
118
119     for (i = 0; i < vec_len (em->event_types); i++) {
120         elog_event_type_t *ep;
121         u8 *tmp;
122
123         ep = vec_elt_at_index(em->event_types, i);
124         tmp = (u8 *) vec_dup(ep->format);
125         vec_add1(tmp,0);
126         add_event_from_clib_file (ep->type_index_plus_one, (char *) tmp, i);
127         vec_free(tmp);
128     }
129
130     finalize_events();
131
132     cpel_event_init(vec_len(em->events));
133
134     starttime = em->events[0].time;
135
136     for (i = 0; i < vec_len (em->events); i++) {
137         elog_event_t *ep;
138
139         ep = vec_elt_at_index(em->events, i);
140
141         delta = ep->time - starttime;
142
143         add_clib_event (delta, ep->track, ep->event_type + 1, i);
144     }
145
146     cpel_event_finalize();
147
148     set_pid_ax_width(8*widest_track_format);
149
150     return(0);
151 }
152
153 unsigned int vl(void *a)
154 {
155     return vec_len (a);
156 }