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

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include <stdio.h>
michael@0 8 #include "timecard.h"
michael@0 9 #include "mozilla/mozalloc.h"
michael@0 10
michael@0 11 Timecard *
michael@0 12 create_timecard()
michael@0 13 {
michael@0 14 Timecard *tc = moz_xcalloc(1,sizeof(Timecard));
michael@0 15 tc->entries_allocated = TIMECARD_INITIAL_TABLE_SIZE;
michael@0 16 tc->entries = moz_xcalloc(tc->entries_allocated, sizeof(TimecardEntry));
michael@0 17 tc->start_time = PR_Now();
michael@0 18 return tc;
michael@0 19 }
michael@0 20
michael@0 21 void
michael@0 22 destroy_timecard(Timecard *tc)
michael@0 23 {
michael@0 24 moz_free(tc->entries);
michael@0 25 moz_free(tc);
michael@0 26 }
michael@0 27
michael@0 28 void
michael@0 29 stamp_timecard(Timecard *tc,
michael@0 30 const char *event,
michael@0 31 const char *file,
michael@0 32 unsigned int line,
michael@0 33 const char *function)
michael@0 34 {
michael@0 35 TimecardEntry *entry = NULL;
michael@0 36
michael@0 37 /* Trim the path component from the filename */
michael@0 38 const char *last_slash = file;
michael@0 39 while (*file) {
michael@0 40 if (*file == '/' || *file == '\\') {
michael@0 41 last_slash = file;
michael@0 42 }
michael@0 43 file++;
michael@0 44 }
michael@0 45 file = last_slash;
michael@0 46 if (*file == '/' || *file == '\\') {
michael@0 47 file++;
michael@0 48 }
michael@0 49
michael@0 50 /* Ensure there is enough space left in the entries list */
michael@0 51 if (tc->curr_entry == tc->entries_allocated) {
michael@0 52 tc->entries_allocated *= 2;
michael@0 53 tc->entries = moz_xrealloc(tc->entries,
michael@0 54 tc->entries_allocated * sizeof(TimecardEntry));
michael@0 55 }
michael@0 56
michael@0 57 /* Record the data into the timecard entry */
michael@0 58 entry = &tc->entries[tc->curr_entry];
michael@0 59 entry->timestamp = PR_Now();
michael@0 60 entry->event = event;
michael@0 61 entry->file = file;
michael@0 62 entry->line = line;
michael@0 63 entry->function = function;
michael@0 64 tc->curr_entry++;
michael@0 65 }
michael@0 66
michael@0 67 void
michael@0 68 print_timecard(Timecard *tc)
michael@0 69 {
michael@0 70 size_t i;
michael@0 71 TimecardEntry *entry;
michael@0 72 size_t event_width = 5;
michael@0 73 size_t file_width = 4;
michael@0 74 size_t function_width = 8;
michael@0 75 size_t line_width;
michael@0 76 PRTime offset, delta;
michael@0 77
michael@0 78 for (i = 0; i < tc->curr_entry; i++) {
michael@0 79 entry = &tc->entries[i];
michael@0 80 if (strlen(entry->event) > event_width) {
michael@0 81 event_width = strlen(entry->event);
michael@0 82 }
michael@0 83 if (strlen(entry->file) > file_width) {
michael@0 84 file_width = strlen(entry->file);
michael@0 85 }
michael@0 86 if (strlen(entry->function) > function_width) {
michael@0 87 function_width = strlen(entry->function);
michael@0 88 }
michael@0 89 }
michael@0 90
michael@0 91 printf("\nTimecard created %4lld.%6.6lld\n\n",
michael@0 92 tc->start_time / PR_USEC_PER_SEC, tc->start_time % PR_USEC_PER_SEC);
michael@0 93
michael@0 94 line_width = 1 + 11 + 11 + event_width + file_width + 6 +
michael@0 95 function_width + (4 * 3);
michael@0 96
michael@0 97 printf(" %-11s | %-11s | %-*s | %-*s | %-*s\n",
michael@0 98 "Timestamp", "Delta",
michael@0 99 (int)event_width, "Event",
michael@0 100 (int)file_width + 6, "File",
michael@0 101 (int)function_width, "Function");
michael@0 102
michael@0 103 for (i = 0; i <= line_width; i++) {
michael@0 104 printf("=");
michael@0 105 }
michael@0 106 printf("\n");
michael@0 107
michael@0 108 for (i = 0; i < tc->curr_entry; i++) {
michael@0 109 entry = &tc->entries[i];
michael@0 110 offset = entry->timestamp - tc->start_time;
michael@0 111 if (i > 0) {
michael@0 112 delta = entry->timestamp - tc->entries[i-1].timestamp;
michael@0 113 } else {
michael@0 114 delta = entry->timestamp - tc->start_time;
michael@0 115 }
michael@0 116 printf(" %4lld.%6.6lld | %4lld.%6.6lld | %-*s | %*s:%-5d | %-*s\n",
michael@0 117 offset / PR_USEC_PER_SEC, offset % PR_USEC_PER_SEC,
michael@0 118 delta / PR_USEC_PER_SEC, delta % PR_USEC_PER_SEC,
michael@0 119 (int)event_width, entry->event,
michael@0 120 (int)file_width, entry->file, entry->line,
michael@0 121 (int)function_width, entry->function);
michael@0 122 }
michael@0 123 printf("\n");
michael@0 124 }

mercurial