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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <netinet/in.h>
20 #include <sys/types.h>
26 #include <vppinfra/clib.h>
27 #include <vppinfra/vec.h>
28 #include <vppinfra/hash.h>
29 #include <vppinfra/elog.h>
30 #include <vppinfra/mem.h>
35 #include "cpel_util.h"
37 static elog_main_t elog_main;
42 void convert_clib_file(char *clib_file)
44 clib_error_t *error = 0;
46 elog_main_t *em = &elog_main;
47 double starttime, delta;
49 error = elog_read_file (&elog_main, clib_file);
52 clib_warning("%U", format_clib_error, error);
58 starttime = em->events[0].time;
60 for (i = 0; i < vec_len (em->events); i++) {
61 elog_event_t *e; /* clib event */
62 evt_t *ep; /* xxx2cpel event */
70 e = vec_elt_at_index(em->events, i);
72 /* Seconds since start of log */
73 delta = e->time - starttime;
75 /* u64 nanoseconds since start of log */
76 timestamp = delta * 1e9;
78 s = format (0, "%U%c", format_elog_event, em, e, 0);
80 /* allocate an event instance */
81 vec_add2(the_events, ep, 1);
82 ep->timestamp = timestamp;
84 /* convert string event code to a real number */
85 t = vec_elt_at_index (em->event_types, e->event_type);
88 * Construct a reasonable event name.
89 * Truncate the format string at the first whitespace break
90 * or printf format character.
92 brief_event_name = format (0, "%s", t->format);
94 for (j = 0; j < vec_len (brief_event_name); j++) {
95 if (brief_event_name[j] == ' ' ||
96 brief_event_name[j] == '%' ||
97 brief_event_name[j] == '(') {
98 brief_event_name[j] = 0;
102 /* Throw away that much of the formatted event */
103 vec_delete (s, j+1, 0);
105 ep->event_id = find_or_add_event(brief_event_name, "%s");
107 track_name = format (0, "%U%c", format_elog_track_name, em, e, 0);
109 ep->track_id = find_or_add_track (track_name);
111 ep->datum = find_or_add_strtab(s);
113 vec_free (track_name);
114 vec_free(brief_event_name);
119 u8 *vec_basename (char *s)
129 while (cp > s && *cp != '/')
135 rv = format (0, "%s", cp);
140 int event_compare (const void *a0, const void *a1)
142 evt_t *e0 = (evt_t *)a0;
143 evt_t *e1 = (evt_t *)a1;
145 if (e0->timestamp < e1->timestamp)
147 else if (e0->timestamp > e1->timestamp)
152 int main (int argc, char **argv)
155 char **inputfiles = 0;
156 char *outputfile = 0;
159 clib_mem_init_thread_safe (0, 256 << 20);
164 while (curarg < argc) {
165 if (!strncmp(argv[curarg], "--input-file", 3)) {
168 vec_add1 (inputfiles, argv[curarg]);
172 clib_warning("Missing filename after --input-file\n");
176 if (!strncmp(argv[curarg], "--output-file", 3)) {
179 outputfile = argv[curarg];
183 clib_warning("Missing filename after --output-file\n");
186 vec_add1 (inputfiles, argv[curarg]);
192 "c2cpel [--input-file] <filename> --output-file <filename>\n");
196 if (vec_len(inputfiles) == 0 || outputfile == 0)
199 if (vec_len(inputfiles) > 1)
202 clib_mem_init (0, ((uword)3<<30));
206 convert_clib_file (inputfiles[0]);
208 ofp = fopen (outputfile, "w");
210 clib_unix_warning ("couldn't create %s", outputfile);
215 fixup_event_tracks();
218 * Four sections: string-table, event definitions, track defs, events.
220 if (!write_cpel_header(ofp, 4)) {
221 clib_warning ("Error writing cpel header to %s...\n", outputfile);
226 if (!write_string_table(ofp)) {
227 clib_warning ("Error writing string table to %s...\n", outputfile);
232 if (!write_event_defs(ofp)) {
233 clib_warning ("Error writing event defs to %s...\n", outputfile);
238 if (!write_track_defs(ofp)) {
239 clib_warning ("Error writing track defs to %s...\n", outputfile);
244 if (!write_events(ofp, (u64) 1e9)) {
245 clib_warning ("Error writing events to %s...\n", outputfile);