media/webrtc/signaling/src/common/time_profiling/timecard.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/signaling/src/common/time_profiling/timecard.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,124 @@
     1.4 +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include <stdio.h>
    1.11 +#include "timecard.h"
    1.12 +#include "mozilla/mozalloc.h"
    1.13 +
    1.14 +Timecard *
    1.15 +create_timecard()
    1.16 +{
    1.17 +  Timecard *tc = moz_xcalloc(1,sizeof(Timecard));
    1.18 +  tc->entries_allocated = TIMECARD_INITIAL_TABLE_SIZE;
    1.19 +  tc->entries = moz_xcalloc(tc->entries_allocated, sizeof(TimecardEntry));
    1.20 +  tc->start_time = PR_Now();
    1.21 +  return tc;
    1.22 +}
    1.23 +
    1.24 +void
    1.25 +destroy_timecard(Timecard *tc)
    1.26 +{
    1.27 +  moz_free(tc->entries);
    1.28 +  moz_free(tc);
    1.29 +}
    1.30 +
    1.31 +void
    1.32 +stamp_timecard(Timecard *tc,
    1.33 +               const char *event,
    1.34 +               const char *file,
    1.35 +               unsigned int line,
    1.36 +               const char *function)
    1.37 +{
    1.38 +  TimecardEntry *entry = NULL;
    1.39 +
    1.40 +  /* Trim the path component from the filename */
    1.41 +  const char *last_slash = file;
    1.42 +  while (*file) {
    1.43 +    if (*file == '/' || *file == '\\') {
    1.44 +      last_slash = file;
    1.45 +    }
    1.46 +    file++;
    1.47 +  }
    1.48 +  file = last_slash;
    1.49 +  if (*file == '/' || *file == '\\') {
    1.50 +    file++;
    1.51 +  }
    1.52 +
    1.53 +  /* Ensure there is enough space left in the entries list */
    1.54 +  if (tc->curr_entry == tc->entries_allocated) {
    1.55 +    tc->entries_allocated *= 2;
    1.56 +    tc->entries = moz_xrealloc(tc->entries,
    1.57 +                               tc->entries_allocated * sizeof(TimecardEntry));
    1.58 +  }
    1.59 +
    1.60 +  /* Record the data into the timecard entry */
    1.61 +  entry = &tc->entries[tc->curr_entry];
    1.62 +  entry->timestamp = PR_Now();
    1.63 +  entry->event = event;
    1.64 +  entry->file = file;
    1.65 +  entry->line = line;
    1.66 +  entry->function = function;
    1.67 +  tc->curr_entry++;
    1.68 +}
    1.69 +
    1.70 +void
    1.71 +print_timecard(Timecard *tc)
    1.72 +{
    1.73 +  size_t i;
    1.74 +  TimecardEntry *entry;
    1.75 +  size_t event_width = 5;
    1.76 +  size_t file_width = 4;
    1.77 +  size_t function_width = 8;
    1.78 +  size_t line_width;
    1.79 +  PRTime offset, delta;
    1.80 +
    1.81 +  for (i = 0; i < tc->curr_entry; i++) {
    1.82 +    entry = &tc->entries[i];
    1.83 +    if (strlen(entry->event) > event_width) {
    1.84 +      event_width = strlen(entry->event);
    1.85 +    }
    1.86 +    if (strlen(entry->file) > file_width) {
    1.87 +      file_width = strlen(entry->file);
    1.88 +    }
    1.89 +    if (strlen(entry->function) > function_width) {
    1.90 +      function_width = strlen(entry->function);
    1.91 +    }
    1.92 +  }
    1.93 +
    1.94 +  printf("\nTimecard created %4lld.%6.6lld\n\n",
    1.95 +          tc->start_time / PR_USEC_PER_SEC, tc->start_time % PR_USEC_PER_SEC);
    1.96 +
    1.97 +  line_width = 1 + 11 + 11 + event_width + file_width + 6 +
    1.98 +                   function_width + (4 * 3);
    1.99 +
   1.100 +  printf(" %-11s | %-11s | %-*s | %-*s | %-*s\n",
   1.101 +          "Timestamp", "Delta",
   1.102 +          (int)event_width, "Event",
   1.103 +          (int)file_width + 6, "File",
   1.104 +          (int)function_width, "Function");
   1.105 +
   1.106 +  for (i = 0; i <= line_width; i++) {
   1.107 +    printf("=");
   1.108 +  }
   1.109 +  printf("\n");
   1.110 +
   1.111 +  for (i = 0; i < tc->curr_entry; i++) {
   1.112 +    entry = &tc->entries[i];
   1.113 +    offset = entry->timestamp - tc->start_time;
   1.114 +    if (i > 0) {
   1.115 +      delta = entry->timestamp - tc->entries[i-1].timestamp;
   1.116 +    } else {
   1.117 +      delta = entry->timestamp - tc->start_time;
   1.118 +    }
   1.119 +    printf(" %4lld.%6.6lld | %4lld.%6.6lld | %-*s | %*s:%-5d | %-*s\n",
   1.120 +           offset / PR_USEC_PER_SEC, offset % PR_USEC_PER_SEC,
   1.121 +           delta / PR_USEC_PER_SEC, delta % PR_USEC_PER_SEC,
   1.122 +           (int)event_width, entry->event,
   1.123 +           (int)file_width, entry->file, entry->line,
   1.124 +           (int)function_width, entry->function);
   1.125 +  }
   1.126 +  printf("\n");
   1.127 +}

mercurial