1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/tracked.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,128 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +//------------------------------------------------------------------------------ 1.9 +// Tracked is the base class for all tracked objects. During construction, it 1.10 +// registers the fact that an instance was created, and at destruction time, it 1.11 +// records that event. The instance may be tagged with a name, which is refered 1.12 +// to as its Location. The Location is a file and line number, most 1.13 +// typically indicated where the object was constructed. In some cases, as the 1.14 +// object's significance is refined (for example, a Task object is augmented to 1.15 +// do additonal things), its Location may be redefined to that later location. 1.16 + 1.17 +// Tracking includes (for each instance) recording the birth thread, death 1.18 +// thread, and duration of life (from construction to destruction). All this 1.19 +// data is accumulated and filtered for review at about:objects. 1.20 + 1.21 +#ifndef BASE_TRACKED_H_ 1.22 +#define BASE_TRACKED_H_ 1.23 + 1.24 +#include <string> 1.25 + 1.26 +#include "base/time.h" 1.27 + 1.28 +#ifndef NDEBUG 1.29 +#ifndef TRACK_ALL_TASK_OBJECTS 1.30 +#define TRACK_ALL_TASK_OBJECTS 1.31 +#endif // TRACK_ALL_TASK_OBJECTS 1.32 +#endif // NDEBUG 1.33 + 1.34 +namespace tracked_objects { 1.35 + 1.36 +//------------------------------------------------------------------------------ 1.37 +// Location provides basic info where of an object was constructed, or was 1.38 +// significantly brought to life. 1.39 + 1.40 +class Location { 1.41 + public: 1.42 + // Constructor should be called with a long-lived char*, such as __FILE__. 1.43 + // It assumes the provided value will persist as a global constant, and it 1.44 + // will not make a copy of it. 1.45 + Location(const char* function_name, const char* file_name, int line_number) 1.46 + : function_name_(function_name), 1.47 + file_name_(file_name), 1.48 + line_number_(line_number) { } 1.49 + 1.50 + // Provide a default constructor for easy of debugging. 1.51 + Location() 1.52 + : function_name_("Unknown"), 1.53 + file_name_("Unknown"), 1.54 + line_number_(-1) { } 1.55 + 1.56 + // Comparison operator for insertion into a std::map<> hash tables. 1.57 + // All we need is *some* (any) hashing distinction. Strings should already 1.58 + // be unique, so we don't bother with strcmp or such. 1.59 + // Use line number as the primary key (because it is fast, and usually gets us 1.60 + // a difference), and then pointers as secondary keys (just to get some 1.61 + // distinctions). 1.62 + bool operator < (const Location& other) const { 1.63 + if (line_number_ != other.line_number_) 1.64 + return line_number_ < other.line_number_; 1.65 + if (file_name_ != other.file_name_) 1.66 + return file_name_ < other.file_name_; 1.67 + return function_name_ < other.function_name_; 1.68 + } 1.69 + 1.70 + const char* function_name() const { return function_name_; } 1.71 + const char* file_name() const { return file_name_; } 1.72 + int line_number() const { return line_number_; } 1.73 + 1.74 + void Write(bool display_filename, bool display_function_name, 1.75 + std::string* output) const; 1.76 + 1.77 + // Write function_name_ in HTML with '<' and '>' properly encoded. 1.78 + void WriteFunctionName(std::string* output) const; 1.79 + 1.80 + private: 1.81 + const char* const function_name_; 1.82 + const char* const file_name_; 1.83 + const int line_number_; 1.84 +}; 1.85 + 1.86 + 1.87 +//------------------------------------------------------------------------------ 1.88 +// Define a macro to record the current source location. 1.89 + 1.90 +#define FROM_HERE tracked_objects::Location(__FUNCTION__, __FILE__, __LINE__) 1.91 + 1.92 + 1.93 +//------------------------------------------------------------------------------ 1.94 + 1.95 + 1.96 +class Births; 1.97 + 1.98 +class Tracked { 1.99 + public: 1.100 + Tracked(); 1.101 + virtual ~Tracked(); 1.102 + 1.103 + // Used to record the FROM_HERE location of a caller. 1.104 + void SetBirthPlace(const Location& from_here); 1.105 + 1.106 + // When a task sits around a long time, such as in a timer, or object watcher, 1.107 + // this method should be called when the task becomes active, and its 1.108 + // significant lifetime begins (and its waiting to be woken up has passed). 1.109 + void ResetBirthTime(); 1.110 + 1.111 + bool MissingBirthplace() const; 1.112 + 1.113 + private: 1.114 +#ifdef TRACK_ALL_TASK_OBJECTS 1.115 + 1.116 + // Pointer to instance were counts of objects with the same birth location 1.117 + // (on the same thread) are stored. 1.118 + Births* tracked_births_; 1.119 + // The time this object was constructed. If its life consisted of a long 1.120 + // waiting period, and then it became active, then this value is generally 1.121 + // reset before the object begins it active life. 1.122 + base::Time tracked_birth_time_; 1.123 + 1.124 +#endif // TRACK_ALL_TASK_OBJECTS 1.125 + 1.126 + DISALLOW_COPY_AND_ASSIGN(Tracked); 1.127 +}; 1.128 + 1.129 +} // namespace tracked_objects 1.130 + 1.131 +#endif // BASE_TRACKED_H_