blob: 379c2bc65f18daefc7b3af35aacb1257304aed4c [file] [log] [blame]
Dave Barach52642c32016-02-11 19:28:19 -05001/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2006-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#include <stdio.h>
17#include <stdlib.h>
18#include <netinet/in.h>
19#include <string.h>
20#include <sys/types.h>
21#include <sys/stat.h>
22#include <fcntl.h>
23#include <sys/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>
Dave Barachde9571f2017-03-25 08:20:22 -040031#include <vppinfra/mem.h>
Dave Barach52642c32016-02-11 19:28:19 -050032#include <pwd.h>
33#include <stdarg.h>
34#include <time.h>
35#include "cpel.h"
36#include "cpel_util.h"
37
38static elog_main_t elog_main;
39
40/*
41 * convert_clib_file
42 */
43void convert_clib_file(char *clib_file)
44{
45 clib_error_t *error = 0;
46 int i;
47 elog_main_t *em = &elog_main;
48 double starttime, delta;
49
50 error = elog_read_file (&elog_main, clib_file);
51
52 if (error) {
53 clib_warning("%U", format_clib_error, error);
54 exit (1);
55 }
56
57 em = &elog_main;
58
59 starttime = em->events[0].time;
60
61 for (i = 0; i < vec_len (em->events); i++) {
62 elog_event_t *e; /* clib event */
63 evt_t *ep; /* xxx2cpel event */
64 u8 *s;
65 u64 timestamp;
66 elog_event_type_t *t;
67 u8 *brief_event_name;
68 u8 *track_name;
69 int j;
70
71 e = vec_elt_at_index(em->events, i);
72
73 /* Seconds since start of log */
74 delta = e->time - starttime;
75
76 /* u64 nanoseconds since start of log */
77 timestamp = delta * 1e9;
78
79 s = format (0, "%U%c", format_elog_event, em, e, 0);
80
81 /* allocate an event instance */
82 vec_add2(the_events, ep, 1);
83 ep->timestamp = timestamp;
84
85 /* convert string event code to a real number */
86 t = vec_elt_at_index (em->event_types, e->type);
87
88 /*
89 * Construct a reasonable event name.
90 * Truncate the format string at the first whitespace break
91 * or printf format character.
92 */
93 brief_event_name = format (0, "%s", t->format);
94
95 for (j = 0; j < vec_len (brief_event_name); j++) {
96 if (brief_event_name[j] == ' ' ||
97 brief_event_name[j] == '%' ||
98 brief_event_name[j] == '(') {
99 brief_event_name[j] = 0;
100 break;
101 }
102 }
103 /* Throw away that much of the formatted event */
104 vec_delete (s, j+1, 0);
105
106 ep->event_id = find_or_add_event(brief_event_name, "%s");
107
108 track_name = format (0, "%U%c", format_elog_track, em, e, 0);
109
110 ep->track_id = find_or_add_track (track_name);
111
112 ep->datum = find_or_add_strtab(s);
113
114 vec_free (track_name);
115 vec_free(brief_event_name);
116 vec_free(s);
117 }
118}
119
120u8 *vec_basename (char *s)
121{
122 u8 * rv;
123 char *cp = s;
124
125 while (*cp)
126 cp++;
127
128 cp--;
129
130 while (cp > s && *cp != '/')
131 cp--;
132
133 if (cp > s)
134 cp++;
135
136 rv = format (0, "%s", cp);
137 return rv;
138}
139
140
141int event_compare (const void *a0, const void *a1)
142{
143 evt_t *e0 = (evt_t *)a0;
144 evt_t *e1 = (evt_t *)a1;
145
146 if (e0->timestamp < e1->timestamp)
147 return -1;
148 else if (e0->timestamp > e1->timestamp)
149 return 1;
150 return 0;
151}
152
153int main (int argc, char **argv)
154{
155 int curarg=1;
156 char **inputfiles = 0;
157 char *outputfile = 0;
158 FILE *ofp;
159
160 if (argc < 3)
161 goto usage;
162
163 while (curarg < argc) {
164 if (!strncmp(argv[curarg], "--input-file", 3)) {
165 curarg++;
166 if (curarg < argc) {
167 vec_add1 (inputfiles, argv[curarg]);
168 curarg++;
169 continue;
170 }
171 clib_warning("Missing filename after --input-file\n");
172 exit (1);
173 }
174
175 if (!strncmp(argv[curarg], "--output-file", 3)) {
176 curarg ++;
177 if (curarg < argc) {
178 outputfile = argv[curarg];
179 curarg ++;
180 continue;
181 }
182 clib_warning("Missing filename after --output-file\n");
183 exit(1);
184 }
185 vec_add1 (inputfiles, argv[curarg]);
186 curarg++;
187 continue;
188
189 usage:
190 fformat(stderr,
191 "c2cpel [--input-file] <filename> --output-file <filename>\n");
192 exit(1);
193 }
194
195 if (vec_len(inputfiles) == 0 || outputfile == 0)
196 goto usage;
197
198 if (vec_len(inputfiles) > 1)
199 goto usage;
200
Dave Barachde9571f2017-03-25 08:20:22 -0400201 clib_mem_init (0, ((uword)3<<30));
202
Dave Barach52642c32016-02-11 19:28:19 -0500203 cpel_util_init();
204
205 convert_clib_file (inputfiles[0]);
206
207 ofp = fopen (outputfile, "w");
208 if (ofp == NULL) {
209 clib_unix_warning ("couldn't create %s", outputfile);
210 exit (1);
211 }
212
213 alpha_sort_tracks();
214 fixup_event_tracks();
215
216 /*
217 * Four sections: string-table, event definitions, track defs, events.
218 */
219 if (!write_cpel_header(ofp, 4)) {
220 clib_warning ("Error writing cpel header to %s...\n", outputfile);
221 unlink(outputfile);
222 exit(1);
223 }
224
225 if (!write_string_table(ofp)) {
226 clib_warning ("Error writing string table to %s...\n", outputfile);
227 unlink(outputfile);
228 exit(1);
229 }
230
231 if (!write_event_defs(ofp)) {
232 clib_warning ("Error writing event defs to %s...\n", outputfile);
233 unlink(outputfile);
234 exit(1);
235 }
236
237 if (!write_track_defs(ofp)) {
238 clib_warning ("Error writing track defs to %s...\n", outputfile);
239 unlink(outputfile);
240 exit(1);
241 }
242
243 if (!write_events(ofp, (u64) 1e9)) {
244 clib_warning ("Error writing events to %s...\n", outputfile);
245 unlink(outputfile);
246 exit(1);
247
248 }
249 fclose(ofp);
250 exit (0);
251}