OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 //------------------------------------------------------------------------------ | 5 //------------------------------------------------------------------------------ |
6 // Tracked is the base class for all tracked objects. During construction, it | 6 // Tracked is the base class for all tracked objects. During construction, it |
7 // registers the fact that an instance was created, and at destruction time, it | 7 // registers the fact that an instance was created, and at destruction time, it |
8 // records that event. The instance may be tagged with a name, which is refered | 8 // records that event. The instance may be tagged with a name, which is refered |
9 // to as its Location. The Location is a file and line number, most | 9 // to as its Location. The Location is a file and line number, most |
10 // typically indicated where the object was constructed. In some cases, as the | 10 // typically indicated where the object was constructed. In some cases, as the |
11 // object's significance is refined (for example, a Task object is augmented to | 11 // object's significance is refined (for example, a Task object is augmented to |
12 // do additonal things), its Location may be redefined to that later location. | 12 // do additonal things), its Location may be redefined to that later location. |
13 | 13 |
14 // Tracking includes (for each instance) recording the birth thread, death | 14 // Tracking includes (for each instance) recording the birth thread, death |
15 // thread, and duration of life (from construction to destruction). All this | 15 // thread, and duration of life (from construction to destruction). All this |
16 // data is accumulated and filtered for review at about:objects. | 16 // data is accumulated and filtered for review at about:objects. |
17 | 17 |
18 #ifndef BASE_TRACKED_H_ | 18 #ifndef BASE_TRACKED_H_ |
19 #define BASE_TRACKED_H_ | 19 #define BASE_TRACKED_H_ |
20 #pragma once | 20 #pragma once |
21 | 21 |
22 #include <string> | 22 #include <string> |
23 | 23 |
24 #include "base/base_export.h" | 24 #include "base/base_export.h" |
25 #include "base/string_piece.h" | |
26 #include "base/stringize_macros.h" | |
25 #include "base/time.h" | 27 #include "base/time.h" |
26 | 28 |
27 #ifndef NDEBUG | 29 #ifndef NDEBUG |
28 #ifndef TRACK_ALL_TASK_OBJECTS | 30 #ifndef TRACK_ALL_TASK_OBJECTS |
29 #define TRACK_ALL_TASK_OBJECTS | 31 #define TRACK_ALL_TASK_OBJECTS |
30 #endif // TRACK_ALL_TASK_OBJECTS | 32 #endif // TRACK_ALL_TASK_OBJECTS |
31 #endif // NDEBUG | 33 #endif // NDEBUG |
32 | 34 |
33 namespace tracked_objects { | 35 namespace tracked_objects { |
34 | 36 |
35 //------------------------------------------------------------------------------ | 37 //------------------------------------------------------------------------------ |
36 // Location provides basic info where of an object was constructed, or was | 38 // Location provides basic info where of an object was constructed, or was |
37 // significantly brought to life. | 39 // significantly brought to life. |
38 | 40 |
39 class BASE_EXPORT Location { | 41 class BASE_EXPORT Location { |
40 public: | 42 public: |
41 // Constructor should be called with a long-lived char*, such as __FILE__. | 43 // Constructor should be called with a long-lived char*. It assumes the |
42 // It assumes the provided value will persist as a global constant, and it | 44 // provided value will persist as a global constant, and it will not make a |
43 // will not make a copy of it. | 45 // copy of it. |
44 Location(const char* function_name, | 46 // |file_line| is formatted as "FILE:LINE". |
45 const char* file_name, | 47 Location(const char* file_line, |
46 int line_number, | 48 const char* function_name, |
jbates
2011/08/31 00:40:48
I really wanted this to be "FILE:LINE:FUNCTION" to
jar (doing other things)
2011/09/01 18:17:51
There is a memory cost of having FILE:LINE, as it
jbates
2011/09/01 23:01:52
The only purpose I had in mind of merging these is
| |
47 const void* program_counter); | 49 const void* program_counter); |
48 | 50 |
49 // Provide a default constructor for easy of debugging. | 51 // Default constructor initializes to "Unknown:-1", "Unknown", NULL. |
50 Location(); | 52 Location(); |
51 | 53 |
52 // Comparison operator for insertion into a std::map<> hash tables. | 54 // Comparison operator for insertion into a std::map<> hash tables. |
53 // All we need is *some* (any) hashing distinction. Strings should already | 55 // All we need is *some* (any) hashing distinction. Strings should already |
54 // be unique, so we don't bother with strcmp or such. | 56 // be unique, so we don't bother with strcmp or such. |
55 // Use line number as the primary key (because it is fast, and usually gets us | 57 // Use line number as the primary key (because it is fast, and usually gets us |
56 // a difference), and then pointers as secondary keys (just to get some | 58 // a difference), and then pointers as secondary keys (just to get some |
57 // distinctions). | 59 // distinctions). |
58 bool operator < (const Location& other) const { | 60 bool operator < (const Location& other) const { |
59 if (line_number_ != other.line_number_) | 61 return file_line_ < other.file_line_; |
jbates
2011/08/31 00:40:48
If file_line is the same, function is the same, so
jar (doing other things)
2011/09/01 18:17:51
I think this is not true when templates are invole
jbates
2011/09/01 23:01:52
OK, I'll add it back in. In linux/gcc, what I see
| |
60 return line_number_ < other.line_number_; | |
61 if (file_name_ != other.file_name_) | |
62 return file_name_ < other.file_name_; | |
63 return function_name_ < other.function_name_; | |
64 } | 62 } |
65 | 63 |
66 const char* function_name() const { return function_name_; } | 64 // Get long-lived file_line string "FILE:LINE". |
67 const char* file_name() const { return file_name_; } | 65 const char* file_line() const { return file_line_; } |
68 int line_number() const { return line_number_; } | 66 const base::StringPiece file_name() const; |
67 int line_number() const; | |
68 const base::StringPiece function_name() const; | |
69 const void* program_counter() const { return program_counter_; } | 69 const void* program_counter() const { return program_counter_; } |
70 | 70 |
71 void Write(bool display_filename, bool display_function_name, | 71 void Write(bool display_filename, bool display_function_name, |
72 std::string* output) const; | 72 std::string* output) const; |
73 | 73 |
74 // Write function_name_ in HTML with '<' and '>' properly encoded. | 74 // Write function_name_ in HTML with '<' and '>' properly encoded. |
75 void WriteFunctionName(std::string* output) const; | 75 void WriteFunctionName(std::string* output) const; |
76 | 76 |
77 private: | 77 private: |
78 const char* const function_name_; | 78 const char* file_line_; |
79 const char* const file_name_; | 79 const char* function_name_; |
80 const int line_number_; | 80 const void* program_counter_; |
jar (doing other things)
2011/09/01 18:17:51
Why did you decide these didn't need to be constan
jbates
2011/09/01 23:01:52
operator= doesn't work if these pointers can't be
jar (doing other things)
2011/09/03 00:26:33
I think it made it clear that the object was not g
jbates
2011/09/03 01:11:41
I see. I find it useful to be able to copy Locatio
| |
81 const void* const program_counter_; | |
82 }; | 81 }; |
83 | 82 |
84 BASE_EXPORT const void* GetProgramCounter(); | 83 BASE_EXPORT const void* GetProgramCounter(); |
85 | 84 |
86 //------------------------------------------------------------------------------ | 85 //------------------------------------------------------------------------------ |
87 // Define a macro to record the current source location. | 86 // Define a macro to record the current source location. |
88 | 87 |
89 #define FROM_HERE tracked_objects::Location( \ | 88 #define FROM_HERE tracked_objects::Location( \ |
jar (doing other things)
2011/09/03 00:26:33
I'm not sure how this happened... but the goal is
jbates
2011/09/03 01:11:41
I don't know the history of this code. The use cas
jar (doing other things)
2011/09/15 16:26:39
Can you clarify how this is used? Is it sent up as
jbates
2011/09/15 16:48:33
Now that we have about:tracing in the official bui
| |
90 __FUNCTION__, \ | 89 __FILE__ ":" STRINGIZE(__LINE__), __FUNCTION__, \ |
jar (doing other things)
2011/09/01 18:17:51
As mentioned, this will notably increase memory ut
jbates
2011/09/01 23:01:52
I compared release builds with and without this CL
jar (doing other things)
2011/09/03 00:26:33
hmmm... the "cost" will appear in each file where
jbates
2011/09/03 01:11:41
code/text size is separate, and it does go up by 8
| |
91 __FILE__, \ | 90 tracked_objects::GetProgramCounter()) |
92 __LINE__, \ | |
93 tracked_objects::GetProgramCounter()) \ | |
94 | 91 |
95 | 92 |
96 //------------------------------------------------------------------------------ | 93 //------------------------------------------------------------------------------ |
97 | 94 |
98 | 95 |
99 class Births; | 96 class Births; |
100 | 97 |
101 class BASE_EXPORT Tracked { | 98 class BASE_EXPORT Tracked { |
102 public: | 99 public: |
103 Tracked(); | 100 Tracked(); |
104 virtual ~Tracked(); | 101 virtual ~Tracked(); |
105 | 102 |
106 // Used to record the FROM_HERE location of a caller. | 103 // Used to record the FROM_HERE location of a caller. |
107 void SetBirthPlace(const Location& from_here); | 104 void SetBirthPlace(const Location& from_here); |
108 const Location GetBirthPlace() const; | 105 const Location GetBirthPlace() const; |
109 | 106 |
110 // When a task sits around a long time, such as in a timer, or object watcher, | 107 // When a task sits around a long time, such as in a timer, or object watcher, |
111 // this method should be called when the task becomes active, and its | 108 // this method should be called when the task becomes active, and its |
112 // significant lifetime begins (and its waiting to be woken up has passed). | 109 // significant lifetime begins (and its waiting to be woken up has passed). |
113 void ResetBirthTime(); | 110 void ResetBirthTime(); |
114 | 111 |
115 bool MissingBirthPlace() const; | 112 bool MissingBirthPlace() const; |
116 | 113 |
117 #if defined(TRACK_ALL_TASK_OBJECTS) | 114 #if defined(TRACK_ALL_TASK_OBJECTS) |
118 base::TimeTicks tracked_birth_time() const { return tracked_birth_time_; } | 115 base::TimeTicks tracked_birth_time() const { return tracked_birth_time_; } |
119 #else | 116 #else |
120 base::TimeTicks tracked_birth_time() const { return base::TimeTicks::Now(); } | 117 base::TimeTicks tracked_birth_time() const { return base::TimeTicks::Now(); } |
jar (doing other things)
2011/09/03 00:26:33
You didn't just make this change... but....
My ge
jbates
2011/09/03 01:11:41
I believe ajwong is considering removing this clas
| |
121 #endif // defined(TRACK_ALL_TASK_OBJECTS) | 118 #endif // defined(TRACK_ALL_TASK_OBJECTS) |
122 | 119 |
123 // Returns null if SetBirthPlace has not been called. | 120 // Returns null if SetBirthPlace has not been called. |
124 const void* get_birth_program_counter() const { | 121 const void* get_birth_program_counter() const { |
125 return birth_program_counter_; | 122 return birth_program_counter_; |
126 } | 123 } |
127 | 124 |
128 private: | 125 private: |
129 #if defined(TRACK_ALL_TASK_OBJECTS) | 126 #if defined(TRACK_ALL_TASK_OBJECTS) |
130 | 127 |
131 // Pointer to instance were counts of objects with the same birth location | 128 // Pointer to instance were counts of objects with the same birth location |
132 // (on the same thread) are stored. | 129 // (on the same thread) are stored. |
133 Births* tracked_births_; | 130 Births* tracked_births_; |
134 // The time this object was constructed. If its life consisted of a long | 131 // The time this object was constructed. If its life consisted of a long |
135 // waiting period, and then it became active, then this value is generally | 132 // waiting period, and then it became active, then this value is generally |
136 // reset before the object begins it active life. | 133 // reset before the object begins it active life. |
137 base::TimeTicks tracked_birth_time_; | 134 base::TimeTicks tracked_birth_time_; |
138 | 135 |
139 #endif // defined(TRACK_ALL_TASK_OBJECTS) | 136 #endif // defined(TRACK_ALL_TASK_OBJECTS) |
140 | 137 |
141 const void* birth_program_counter_; | 138 const void* birth_program_counter_; |
142 | 139 |
143 DISALLOW_COPY_AND_ASSIGN(Tracked); | 140 DISALLOW_COPY_AND_ASSIGN(Tracked); |
144 }; | 141 }; |
145 | 142 |
146 } // namespace tracked_objects | 143 } // namespace tracked_objects |
147 | 144 |
148 #endif // BASE_TRACKED_H_ | 145 #endif // BASE_TRACKED_H_ |
OLD | NEW |