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>
23 #include <sys/fcntl.h>
27 #include <vppinfra/clib.h>
28 #include <vppinfra/vec.h>
29 #include <vppinfra/hash.h>
30 #include <vppinfra/elog.h>
31 #include <vppinfra/mem.h>
36 #include "cpel_util.h"
38 static elog_main_t elog_main;
43 void convert_clib_file(char *clib_file)
45 clib_error_t *error = 0;
47 elog_main_t *em = &elog_main;
48 double starttime, delta;
50 error = elog_read_file (&elog_main, clib_file);
53 clib_warning("%U", format_clib_error, error);
59 starttime = em->events[0].time;
61 for (i = 0; i < vec_len (em->events); i++) {
62 elog_event_t *e; /* clib event */
63 evt_t *ep; /* xxx2cpel event */
71 e = vec_elt_at_index(em->events, i);
73 /* Seconds since start of log */
74 delta = e->time - starttime;
76 /* u64 nanoseconds since start of log */
77 timestamp = delta * 1e9;
79 s = format (0, "%U%c", format_elog_event, em, e, 0);
81 /* allocate an event instance */
82 vec_add2(the_events, ep, 1);
83 ep->timestamp = timestamp;
85 /* convert string event code to a real number */
86 t = vec_elt_at_index (em->event_types, e->type);
89 * Construct a reasonable event name.
90 * Truncate the format string at the first whitespace break
91 * or printf format character.
93 brief_event_name = format (0, "%s", t->format);
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;
103 /* Throw away that much of the formatted event */
104 vec_delete (s, j+1, 0);
106 ep->event_id = find_or_add_event(brief_event_name, "%s");
108 track_name = format (0, "%U%c", format_elog_track, em, e, 0);
110 ep->track_id = find_or_add_track (track_name);
112 ep->datum = find_or_add_strtab(s);
114 vec_free (track_name);
115 vec_free(brief_event_name);
120 u8 *vec_basename (char *s)
130 while (cp > s && *cp != '/')
136 rv = format (0, "%s", cp);
141 int event_compare (const void *a0, const void *a1)
143 evt_t *e0 = (evt_t *)a0;
144 evt_t *e1 = (evt_t *)a1;
146 if (e0->timestamp < e1->timestamp)
148 else if (e0->timestamp > e1->timestamp)
153 int main (int argc, char **argv)
156 char **inputfiles = 0;
157 char *outputfile = 0;
160 clib_mem_init_thread_safe (0, 256 << 20);
165 while (curarg < argc) {
166 if (!strncmp(argv[curarg], "--input-file", 3)) {
169 vec_add1 (inputfiles, argv[curarg]);
173 clib_warning("Missing filename after --input-file\n");
177 if (!strncmp(argv[curarg], "--output-file", 3)) {
180 outputfile = argv[curarg];
184 clib_warning("Missing filename after --output-file\n");
187 vec_add1 (inputfiles, argv[curarg]);
193 "c2cpel [--input-file] <filename> --output-file <filename>\n");
197 if (vec_len(inputfiles) == 0 || outputfile == 0)
200 if (vec_len(inputfiles) > 1)
203 clib_mem_init (0, ((uword)3<<30));
207 convert_clib_file (inputfiles[0]);
209 ofp = fopen (outputfile, "w");
211 clib_unix_warning ("couldn't create %s", outputfile);
216 fixup_event_tracks();
219 * Four sections: string-table, event definitions, track defs, events.
221 if (!write_cpel_header(ofp, 4)) {
222 clib_warning ("Error writing cpel header to %s...\n", outputfile);
227 if (!write_string_table(ofp)) {
228 clib_warning ("Error writing string table to %s...\n", outputfile);
233 if (!write_event_defs(ofp)) {
234 clib_warning ("Error writing event defs to %s...\n", outputfile);
239 if (!write_track_defs(ofp)) {
240 clib_warning ("Error writing track defs to %s...\n", outputfile);
245 if (!write_events(ofp, (u64) 1e9)) {
246 clib_warning ("Error writing events to %s...\n", outputfile);