| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_BASE_LOAD_LOG_H_ | 5 #ifndef NET_BASE_LOAD_LOG_H_ |
| 6 #define NET_BASE_LOAD_LOG_H_ | 6 #define NET_BASE_LOAD_LOG_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/ref_counted.h" | 10 #include "base/ref_counted.h" |
| 11 #include "base/time.h" | 11 #include "base/time.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 // LoadLog stores profiling information on where time was spent while servicing | 15 // LoadLog stores profiling information on where time was spent while servicing |
| 16 // a request (waiting in queues, resolving hosts, resolving proxy, etc...). | 16 // a request (waiting in queues, resolving hosts, resolving proxy, etc...). |
| 17 // | 17 // |
| 18 // Note that LoadLog is NOT THREADSAFE, however it is RefCountedThreadSafe so | 18 // Note that LoadLog is NOT THREADSAFE, however it is RefCountedThreadSafe so |
| 19 // that it can be AddRef() / Release() across threads. | 19 // that it can be AddRef() / Release() across threads. |
| 20 class LoadLog : public base::RefCountedThreadSafe<LoadLog> { | 20 class LoadLog : public base::RefCountedThreadSafe<LoadLog> { |
| 21 public: | 21 public: |
| 22 | |
| 23 enum EventType { | 22 enum EventType { |
| 24 #define EVENT_TYPE(label) TYPE_ ## label, | 23 #define EVENT_TYPE(label) TYPE_ ## label, |
| 25 #include "net/base/load_log_event_type_list.h" | 24 #include "net/base/load_log_event_type_list.h" |
| 26 #undef EVENT_TYPE | 25 #undef EVENT_TYPE |
| 27 }; | 26 }; |
| 28 | 27 |
| 29 // Whether this is the start/end of an event. Or in the case of EventTypes | 28 // Whether this is the start/end of an event. Or in the case of EventTypes |
| 30 // that are "instantaneous", kNone. | 29 // that are "instantaneous", kNone. |
| 31 enum EventPhase { | 30 enum EventPhase { |
| 32 PHASE_NONE, | 31 PHASE_NONE, |
| 33 PHASE_BEGIN, | 32 PHASE_BEGIN, |
| 34 PHASE_END, | 33 PHASE_END, |
| 35 }; | 34 }; |
| 36 | 35 |
| 37 // A logged event. Note that "phase" means if this is the start/end of a | 36 // A logged event. Note that "phase" means if this is the start/end of a |
| 38 // particular event type (in order to record a timestamp for both endpoints). | 37 // particular event type (in order to record a timestamp for both endpoints). |
| 39 struct Event { | 38 struct Event { |
| 40 Event(base::TimeTicks time, | 39 Event(base::TimeTicks time, |
| 41 EventType type, | 40 EventType type, |
| 42 EventPhase phase) | 41 EventPhase phase) |
| 43 : time(time), type(type), phase(phase) { | 42 : time(time), type(type), phase(phase) { |
| 44 } | 43 } |
| 45 | 44 |
| 46 base::TimeTicks time; | 45 base::TimeTicks time; |
| 47 EventType type; | 46 EventType type; |
| 48 EventPhase phase; | 47 EventPhase phase; |
| 49 }; | 48 }; |
| 50 | 49 |
| 51 // The maximum size of |events_|. | |
| 52 enum { kMaxNumEntries = 40 }; | |
| 53 | |
| 54 // Ordered set of events that were logged. | 50 // Ordered set of events that were logged. |
| 55 // TODO(eroman): use a StackVector or array to avoid allocations. | 51 // TODO(eroman): use a StackVector or array to avoid allocations. |
| 56 typedef std::vector<Event> EventList; | 52 typedef std::vector<Event> EventList; |
| 57 | 53 |
| 58 // Create a log, which can hold up to |kMaxNumEntries| Events. | 54 // Value for max_num_entries to indicate the LoadLog has no size limit. |
| 55 static const size_t kUnbounded = static_cast<size_t>(-1); |
| 56 |
| 57 // Creates a log, which can hold up to |max_num_entries| Events. |
| 58 // If |max_num_entries| is |kUnbounded|, then the log can grow arbitrarily |
| 59 // large. |
| 59 // | 60 // |
| 60 // If events are dropped because the log has grown too large, the final | 61 // If events are dropped because the log has grown too large, the final entry |
| 61 // entry will be of type kLogTruncated. | 62 // will be overwritten. |
| 62 LoadLog(); | 63 explicit LoadLog(size_t max_num_entries); |
| 63 | 64 |
| 64 // -------------------------------------------------------------------------- | 65 // -------------------------------------------------------------------------- |
| 65 | 66 |
| 66 // The public interface for adding events to the log are static methods. | 67 // The public interface for adding events to the log are static methods. |
| 67 // This makes it easier to deal with optionally NULL LoadLog. | 68 // This makes it easier to deal with optionally NULL LoadLog. |
| 68 | 69 |
| 69 // Adds an instantaneous event to the log. | 70 // Adds an instantaneous event to the log. |
| 70 static void AddEvent(LoadLog* log, EventType event) { | 71 static void AddEvent(LoadLog* log, EventType event) { |
| 71 if (log) | 72 if (log) |
| 72 log->Add(base::TimeTicks::Now(), event, PHASE_NONE); | 73 log->Add(base::TimeTicks::Now(), event, PHASE_NONE); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 86 log->Add(base::TimeTicks::Now(), event, PHASE_END); | 87 log->Add(base::TimeTicks::Now(), event, PHASE_END); |
| 87 } | 88 } |
| 88 | 89 |
| 89 // -------------------------------------------------------------------------- | 90 // -------------------------------------------------------------------------- |
| 90 | 91 |
| 91 // Returns the list of all events in the log. | 92 // Returns the list of all events in the log. |
| 92 const EventList& events() const { | 93 const EventList& events() const { |
| 93 return events_; | 94 return events_; |
| 94 } | 95 } |
| 95 | 96 |
| 97 // Returns the number of entries that were dropped from the log because the |
| 98 // maximum size had been reached. |
| 99 size_t num_entries_truncated() const { |
| 100 return num_entries_truncated_; |
| 101 } |
| 102 |
| 103 // Returns the bound on the size of the log. |
| 104 size_t max_num_entries() const { |
| 105 return max_num_entries_; |
| 106 } |
| 107 |
| 96 // Returns a C-String symbolic name for |event|. | 108 // Returns a C-String symbolic name for |event|. |
| 97 static const char* EventTypeToString(EventType event); | 109 static const char* EventTypeToString(EventType event); |
| 98 | 110 |
| 99 void Add(const Event& event); | 111 void Add(const Event& event); |
| 100 | 112 |
| 101 void Add(base::TimeTicks t, EventType event, EventPhase phase) { | 113 void Add(base::TimeTicks t, EventType event, EventPhase phase) { |
| 102 Add(Event(t, event, phase)); | 114 Add(Event(t, event, phase)); |
| 103 } | 115 } |
| 104 | 116 |
| 105 // Copies all events from |log|, appending it to the end of |this|. | 117 // Copies all events from |log|, appending it to the end of |this|. |
| 106 void Append(const LoadLog* log); | 118 void Append(const LoadLog* log); |
| 107 | 119 |
| 108 private: | 120 private: |
| 109 friend class base::RefCountedThreadSafe<LoadLog>; | 121 friend class base::RefCountedThreadSafe<LoadLog>; |
| 110 | 122 |
| 111 ~LoadLog() {} | 123 ~LoadLog() {} |
| 112 | 124 |
| 113 EventList events_; | 125 EventList events_; |
| 126 size_t num_entries_truncated_; |
| 127 size_t max_num_entries_;; |
| 114 }; | 128 }; |
| 115 | 129 |
| 116 } // namespace net | 130 } // namespace net |
| 117 | 131 |
| 118 #endif // NET_BASE_LOAD_LOG_H_ | 132 #endif // NET_BASE_LOAD_LOG_H_ |
| OLD | NEW |