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/bitmap.h>
31 #include <vppinfra/byte_order.h>
36 #include "cpel_util.h"
43 event_definition_t *the_event_definitions;
46 /* Hash tables, used to find previous instances of the same items */
47 uword *the_track_hash;
48 uword *the_msg_event_hash;
49 uword *the_strtab_hash;
50 uword *the_pidtid_hash;
51 uword *the_pid_to_name_hash;
54 uword *the_event_id_bitmap;
58 * Finds or adds a string to the string table
60 u32 find_or_add_strtab(void *s_arg)
68 p = hash_get_mem(the_strtab_hash, s);
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.
81 vec_add2(the_strtab, this_string, len);
82 memcpy(this_string, s, len);
84 /* Make a copy which won't be moving around... */
85 vec_validate(scopy, len);
86 memcpy(scopy, s, len);
88 hash_set_mem(the_strtab_hash, scopy, this_string - the_strtab);
90 return(this_string - the_strtab);
95 * returns index in track table
97 u32 find_or_add_track(void *s_arg)
104 p = hash_get_mem(the_track_hash, s);
108 vec_add2(the_tracks, this_track, 1);
110 this_track->original_index = this_track - the_tracks;
111 this_track->strtab_offset = find_or_add_strtab(s);
113 copy_s = (u8 *)vec_dup(s);
115 hash_set_mem(the_track_hash, copy_s, this_track - the_tracks);
116 return(this_track - the_tracks);
121 * Adds an event to the event definition vector and add it to
122 * the event hash table
125 u32 find_or_add_event(void *s_arg, char *datum_format)
129 event_definition_t *this_event_definition;
133 p = hash_get_mem(the_msg_event_hash, s);
137 vec_add2(the_event_definitions, this_event_definition, 1);
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);
146 copy_s = (u8 *)vec_dup(s);
148 hash_set_mem(the_msg_event_hash, copy_s, event_id);
156 int write_string_table(FILE *ofp)
158 cpel_section_header_t sh;
160 /* Round up string table size */
161 while (vec_len(the_strtab) & 0x7)
162 vec_add1(the_strtab, 0);
164 sh.section_type = ntohl(CPEL_SECTION_STRTAB);
165 sh.data_length = ntohl(vec_len(the_strtab));
167 if (fwrite(&sh, sizeof(sh), 1, ofp) != 1)
170 if (fwrite(the_strtab, 1, vec_len(the_strtab), ofp) !=
180 int write_cpel_header(FILE *ofp, u32 nsections)
182 cpel_file_header_t h;
184 h.endian_version = CPEL_FILE_VERSION;
186 h.nsections = ntohs(nsections);
187 h.file_date = ntohl(time(0));
188 if (fwrite(&h, sizeof(h), 1, ofp) != 1)
197 int write_event_defs(FILE *ofp)
199 cpel_section_header_t sh;
200 event_definition_section_header_t edsh;
201 event_definition_t *this_event_definition;
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));
210 if (fwrite(&sh, sizeof(sh), 1, ofp) != 1)
213 clib_memset(&edsh, 0, sizeof(edsh));
215 strcpy(edsh.string_table_name, "FileStrtab");
216 edsh.number_of_event_definitions = ntohl(vec_len(the_event_definitions));
218 if (fwrite(&edsh, sizeof(edsh), 1, ofp) != 1)
221 for (i = 0; i < vec_len(the_event_definitions); i++) {
222 this_event_definition = &the_event_definitions[i];
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);
230 if (fwrite(this_event_definition, sizeof(the_event_definitions[0]),
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));
257 int write_events(FILE *ofp, u64 clock_ticks_per_second)
259 cpel_section_header_t sh;
260 event_section_header_t eh;
261 u32 number_of_events;
268 number_of_events = vec_len(the_events);
270 sh.section_type = ntohl(CPEL_SECTION_EVENT);
271 sh.data_length = ntohl(number_of_events * sizeof(e) +
272 sizeof(event_section_header_t));
274 if (fwrite(&sh, sizeof(sh), 1, ofp) != 1)
277 clib_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);
282 if (fwrite(&eh, sizeof(eh), 1, ofp) != 1)
285 for (i = 0; i < number_of_events; i++) {
286 this_event = &the_events[i];
287 net_timestamp = ntohll(this_event->timestamp);
289 time1 = net_timestamp>>32;
290 time0 = net_timestamp & 0xFFFFFFFF;
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);
298 if (fwrite(&e, sizeof(e), 1, ofp) != 1)
307 int write_track_defs(FILE *ofp)
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;
314 event_definition_section_header_t edsh;
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));
322 if (fwrite(&sh, sizeof(sh), 1, ofp) != 1)
325 clib_memset(&tdsh, 0, sizeof(tdsh));
327 strcpy(tdsh.string_table_name, "FileStrtab");
328 tdsh.number_of_track_definitions = ntohl(vec_len(the_tracks));
330 if (fwrite(&tdsh, sizeof(edsh), 1, ofp) != 1)
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);
338 if (fwrite(this_track_definition, sizeof(this_track_definition[0]),
345 void cpel_util_init (void)
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));
355 /* Must be first, or no supper... */
356 find_or_add_strtab("FileStrtab");
358 /* Historical canned events, no longer used. */
360 /* event 0 (not used) */
361 eventstr = format(0, "PlaceholderNotUsed");
362 vec_add1(eventstr, 0);
363 find_or_add_event(eventstr, "%s");
366 /* event 1 (thread on CPU) */
367 eventstr = format(0, "THREAD/THRUNNING");
368 vec_add1(eventstr, 0);
369 find_or_add_event(eventstr, "%s");
372 /* event 2 (thread ready) */
373 eventstr = format(0, "THREAD/THREADY");
374 vec_add1(eventstr, 0);
375 find_or_add_event(eventstr, "%s");
378 /* event 3 (function enter) */
379 eventstr = format(0, "FUNC/ENTER");
380 vec_add1(eventstr, 0);
381 find_or_add_event(eventstr, "0x%x");
384 /* event 4 (function enter) */
385 eventstr = format(0, "FUNC/EXIT");
386 vec_add1(eventstr, 0);
387 find_or_add_event(eventstr, "0x%x");
393 * alpha_compare_tracks
395 static int alpha_compare_tracks(const void *a1, const void *a2)
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];
403 for (i = 0; s1[i] && s2[i]; i++) {
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
418 void alpha_sort_tracks(void)
423 qsort(the_tracks, vec_len(the_tracks), sizeof(track_t),
424 alpha_compare_tracks);
426 vec_validate(track_alpha_map, vec_len(the_tracks));
427 _vec_len(track_alpha_map) = vec_len(the_tracks);
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;
437 * Use the track alpha mapping to account for the alphabetic
438 * sort performed by the previous routine
440 void fixup_event_tracks(void)
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];
451 /* Indispensable for debugging in gdb... */