blob: 7ee9b6e2a002106c4ccc6d6e78b1b61d64599f4c [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/bitmap.h>
31#include <vppinfra/byte_order.h>
32#include <pwd.h>
33#include <stdarg.h>
34#include <time.h>
35#include "cpel.h"
36#include "cpel_util.h"
37
38evt_t *the_events;
39
40track_t *the_tracks;
41u32 *track_alpha_map;
42
43event_definition_t *the_event_definitions;
44i64 min_timestamp;
45
46/* Hash tables, used to find previous instances of the same items */
47uword *the_track_hash;
48uword *the_msg_event_hash;
49uword *the_strtab_hash;
50uword *the_pidtid_hash;
51uword *the_pid_to_name_hash;
52u8 *the_strtab;
53
54uword *the_event_id_bitmap;
55
56/*
57 * find_or_add_strtab
58 * Finds or adds a string to the string table
59 */
60u32 find_or_add_strtab(void *s_arg)
61{
62 uword *p;
63 int len;
64 u8 *this_string;
65 u8 *scopy=0;
66 char *s = s_arg;
67
68 p = hash_get_mem(the_strtab_hash, s);
69 if (p) {
70 return (p[0]);
71 }
72
73 /*
74 * Here's a CLIB bear-trap. We can't add the string-table
75 * strings to the to the hash table (directly), since it
76 * expands and moves periodically. All of the hash table
77 * entries turn into dangling references, yadda yadda.
78 */
79
80 len = strlen(s)+1;
81 vec_add2(the_strtab, this_string, len);
82 memcpy(this_string, s, len);
83
84 /* Make a copy which won't be moving around... */
85 vec_validate(scopy, len);
86 memcpy(scopy, s, len);
87
88 hash_set_mem(the_strtab_hash, scopy, this_string - the_strtab);
89
90 return(this_string - the_strtab);
91}
92
93/*
94 * find_or_add_track
95 * returns index in track table
96 */
97u32 find_or_add_track(void *s_arg)
98{
99 uword *p;
100 track_t *this_track;
101 u8 *copy_s;
102 char *s=s_arg;
103
104 p = hash_get_mem(the_track_hash, s);
105 if (p) {
106 return (p[0]);
107 }
108 vec_add2(the_tracks, this_track, 1);
109
110 this_track->original_index = this_track - the_tracks;
111 this_track->strtab_offset = find_or_add_strtab(s);
112
113 copy_s = (u8 *)vec_dup(s);
114
115 hash_set_mem(the_track_hash, copy_s, this_track - the_tracks);
116 return(this_track - the_tracks);
117}
118
119/*
120 * find_or_add_event
121 * Adds an event to the event definition vector and add it to
122 * the event hash table
123 */
124
125u32 find_or_add_event(void *s_arg, char *datum_format)
126{
127 uword *p;
128 u8 *copy_s;
129 event_definition_t *this_event_definition;
130 u32 event_id;
131 char *s=s_arg;
132
133 p = hash_get_mem(the_msg_event_hash, s);
134 if (p) {
135 return (p[0]);
136 }
137 vec_add2(the_event_definitions, this_event_definition, 1);
138
139 /* Allocate a new event-id */
140 event_id = clib_bitmap_first_clear (the_event_id_bitmap);
141 the_event_id_bitmap = clib_bitmap_set(the_event_id_bitmap, event_id, 1);
142 this_event_definition->event = event_id;
143 this_event_definition->event_format = find_or_add_strtab(s);
144 this_event_definition->datum_format = find_or_add_strtab(datum_format);
145
146 copy_s = (u8 *)vec_dup(s);
147
148 hash_set_mem(the_msg_event_hash, copy_s, event_id);
149
150 return(event_id);
151}
152
153/*
154 * write_string_table
155 */
156int write_string_table(FILE *ofp)
157{
158 cpel_section_header_t sh;
159
160 /* Round up string table size */
161 while (vec_len(the_strtab) & 0x7)
162 vec_add1(the_strtab, 0);
163
164 sh.section_type = ntohl(CPEL_SECTION_STRTAB);
165 sh.data_length = ntohl(vec_len(the_strtab));
166
167 if (fwrite(&sh, sizeof(sh), 1, ofp) != 1)
168 return(0);
169
170 if (fwrite(the_strtab, 1, vec_len(the_strtab), ofp) !=
171 vec_len(the_strtab))
172 return(0);
173
174 return(1);
175}
176
177/*
178 * write_cpel_header
179 */
180int write_cpel_header(FILE *ofp, u32 nsections)
181{
182 cpel_file_header_t h;
183
184 h.endian_version = CPEL_FILE_VERSION;
185 h.pad = 0;
186 h.nsections = ntohs(nsections);
187 h.file_date = ntohl(time(0));
188 if (fwrite(&h, sizeof(h), 1, ofp) != 1)
189 return (0);
190
191 return(1);
192}
193
194/*
195 * write_event_defs
196 */
197int write_event_defs(FILE *ofp)
198{
199 cpel_section_header_t sh;
200 event_definition_section_header_t edsh;
201 event_definition_t *this_event_definition;
202 int i;
203
204 /* Next, the event definitions */
205 sh.section_type = ntohl(CPEL_SECTION_EVTDEF);
206 sh.data_length = ntohl(vec_len(the_event_definitions)
207 *sizeof(the_event_definitions[0])
208 + sizeof(event_definition_section_header_t));
209
210 if (fwrite(&sh, sizeof(sh), 1, ofp) != 1)
211 return(0);
212
213 memset(&edsh, 0, sizeof(edsh));
214
215 strcpy(edsh.string_table_name, "FileStrtab");
216 edsh.number_of_event_definitions = ntohl(vec_len(the_event_definitions));
217
218 if (fwrite(&edsh, sizeof(edsh), 1, ofp) != 1)
219 return(0);
220
221 for (i = 0; i < vec_len(the_event_definitions); i++) {
222 this_event_definition = &the_event_definitions[i];
223 /* Endian fixup */
224 this_event_definition->event = ntohl(this_event_definition->event);
225 this_event_definition->event_format =
226 ntohl(this_event_definition->event_format);
227 this_event_definition->datum_format =
228 ntohl(this_event_definition->datum_format);
229
230 if (fwrite(this_event_definition, sizeof(the_event_definitions[0]),
231 1, ofp) != 1)
232 return(0);
233 }
234 return(1);
235}
236
237/*
238 * ntohll
239 */
240u64 ntohll (u64 x) {
241 if (clib_arch_is_little_endian)
242 x = ((((x >> 0) & 0xff) << 56)
243 | (((x >> 8) & 0xff) << 48)
244 | (((x >> 16) & 0xff) << 40)
245 | (((x >> 24) & 0xff) << 32)
246 | (((x >> 32) & 0xff) << 24)
247 | (((x >> 40) & 0xff) << 16)
248 | (((x >> 48) & 0xff) << 8)
249 | (((x >> 56) & 0xff) << 0));
250
251 return x;
252}
253
254/*
255 * write_events
256 */
257int write_events(FILE *ofp, u64 clock_ticks_per_second)
258{
259 cpel_section_header_t sh;
260 event_section_header_t eh;
261 u32 number_of_events;
262 int i;
263 event_entry_t e;
264 u64 net_timestamp;
265 evt_t *this_event;
266 u32 time0, time1;
267
268 number_of_events = vec_len(the_events);
269
270 sh.section_type = ntohl(CPEL_SECTION_EVENT);
271 sh.data_length = ntohl(number_of_events * sizeof(e) +
272 sizeof(event_section_header_t));
273
274 if (fwrite(&sh, sizeof(sh), 1, ofp) != 1)
275 return(0);
276
277 memset(&eh, 0, sizeof(eh));
278 strcpy(eh.string_table_name, "FileStrtab");
279 eh.number_of_events = ntohl(number_of_events);
280 eh.clock_ticks_per_second = ntohl(clock_ticks_per_second);
281
282 if (fwrite(&eh, sizeof(eh), 1, ofp) != 1)
283 return(0);
284
285 for (i = 0; i < number_of_events; i++) {
286 this_event = &the_events[i];
287 net_timestamp = ntohll(this_event->timestamp);
288
289 time1 = net_timestamp>>32;
290 time0 = net_timestamp & 0xFFFFFFFF;
291
292 e.time[0] = time0;
293 e.time[1] = time1;
294 e.track = ntohl(this_event->track_id);
295 e.event_code = ntohl(this_event->event_id);
296 e.event_datum = ntohl(this_event->datum);
297
298 if (fwrite(&e, sizeof(e), 1, ofp) != 1)
299 return(0);
300 }
301 return(1);
302}
303
304/*
305 * write_track_defs
306 */
307int write_track_defs(FILE *ofp)
308{
309 cpel_section_header_t sh;
310 track_definition_section_header_t tdsh;
311 track_definition_t record;
312 track_definition_t *this_track_definition = &record;
313 int i;
314 event_definition_section_header_t edsh;
315
316 /* Next, the event definitions */
317 sh.section_type = ntohl(CPEL_SECTION_TRACKDEF);
318 sh.data_length = ntohl(vec_len(the_tracks)
319 *sizeof(this_track_definition[0])
320 + sizeof(track_definition_section_header_t));
321
322 if (fwrite(&sh, sizeof(sh), 1, ofp) != 1)
323 return(0);
324
325 memset(&tdsh, 0, sizeof(tdsh));
326
327 strcpy(tdsh.string_table_name, "FileStrtab");
328 tdsh.number_of_track_definitions = ntohl(vec_len(the_tracks));
329
330 if (fwrite(&tdsh, sizeof(edsh), 1, ofp) != 1)
331 return(0);
332
333 for (i = 0; i < vec_len(the_tracks); i++) {
334 this_track_definition->track = ntohl(i);
335 this_track_definition->track_format =
336 ntohl(the_tracks[i].strtab_offset);
337
338 if (fwrite(this_track_definition, sizeof(this_track_definition[0]),
339 1, ofp) != 1)
340 return(0);
341 }
342 return(1);
343}
344
345void cpel_util_init (void)
346{
347 u8 *eventstr;
348
349 the_strtab_hash = hash_create_string (0, sizeof (uword));
350 the_msg_event_hash = hash_create_string (0, sizeof (uword));
351 the_track_hash = hash_create_string (0, sizeof (uword));
352 the_pidtid_hash = hash_create_string (0, sizeof(uword));
353 the_pid_to_name_hash = hash_create(0, sizeof(uword));
354
355 /* Must be first, or no supper... */
356 find_or_add_strtab("FileStrtab");
357
358 /* Historical canned events, no longer used. */
359 if (0) {
360 /* event 0 (not used) */
361 eventstr = format(0, "PlaceholderNotUsed");
362 vec_add1(eventstr, 0);
363 find_or_add_event(eventstr, "%s");
364 vec_free(eventstr);
365
366 /* event 1 (thread on CPU) */
367 eventstr = format(0, "THREAD/THRUNNING");
368 vec_add1(eventstr, 0);
369 find_or_add_event(eventstr, "%s");
370 vec_free(eventstr);
371
372 /* event 2 (thread ready) */
373 eventstr = format(0, "THREAD/THREADY");
374 vec_add1(eventstr, 0);
375 find_or_add_event(eventstr, "%s");
376 vec_free(eventstr);
377
378 /* event 3 (function enter) */
379 eventstr = format(0, "FUNC/ENTER");
380 vec_add1(eventstr, 0);
381 find_or_add_event(eventstr, "0x%x");
382 vec_free(eventstr);
383
384 /* event 4 (function enter) */
385 eventstr = format(0, "FUNC/EXIT");
386 vec_add1(eventstr, 0);
387 find_or_add_event(eventstr, "0x%x");
388 vec_free(eventstr);
389 }
390}
391
392/*
393 * alpha_compare_tracks
394 */
395static int alpha_compare_tracks(const void *a1, const void *a2)
396{
397 int i;
398 track_t *t1 = (track_t *)a1;
399 track_t *t2 = (track_t *)a2;
400 u8 *s1 = &the_strtab[t1->strtab_offset];
401 u8 *s2 = &the_strtab[t2->strtab_offset];
402
403 for (i = 0; s1[i] && s2[i]; i++) {
404 if (s1[i] < s2[i])
405 return(-1);
406 if (s1[i] > s2[i])
407 return(1);
408 }
409 return(0);
410}
411
412/*
413 * alpha_sort_tracks
414 * Alphabetically sort tracks, set up a mapping
415 * vector so we can quickly map the original track index to
416 * the new/improved/alpha-sorted index
417 */
418void alpha_sort_tracks(void)
419{
420 track_t *this_track;
421 int i;
422
423 qsort(the_tracks, vec_len(the_tracks), sizeof(track_t),
424 alpha_compare_tracks);
425
426 vec_validate(track_alpha_map, vec_len(the_tracks));
427 _vec_len(track_alpha_map) = vec_len(the_tracks);
428
429 for (i = 0; i < vec_len(the_tracks); i++) {
430 this_track = &the_tracks[i];
431 track_alpha_map[this_track->original_index] = i;
432 }
433}
434
435/*
436 * fixup_event_tracks
437 * Use the track alpha mapping to account for the alphabetic
438 * sort performed by the previous routine
439 */
440void fixup_event_tracks(void)
441{
442 int i;
443 u32 old_track;
444
445 for (i = 0; i < vec_len(the_events); i++) {
446 old_track = the_events[i].track_id;
447 the_events[i].track_id = track_alpha_map[old_track];
448 }
449}
450
451/* Indispensable for debugging in gdb... */
452
453u32 vl(void *x)
454{
455 return vec_len(x);
456}