michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: // Tracked is the base class for all tracked objects. During construction, it michael@0: // registers the fact that an instance was created, and at destruction time, it michael@0: // records that event. The instance may be tagged with a name, which is refered michael@0: // to as its Location. The Location is a file and line number, most michael@0: // typically indicated where the object was constructed. In some cases, as the michael@0: // object's significance is refined (for example, a Task object is augmented to michael@0: // do additonal things), its Location may be redefined to that later location. michael@0: michael@0: // Tracking includes (for each instance) recording the birth thread, death michael@0: // thread, and duration of life (from construction to destruction). All this michael@0: // data is accumulated and filtered for review at about:objects. michael@0: michael@0: #ifndef BASE_TRACKED_H_ michael@0: #define BASE_TRACKED_H_ michael@0: michael@0: #include michael@0: michael@0: #include "base/time.h" michael@0: michael@0: #ifndef NDEBUG michael@0: #ifndef TRACK_ALL_TASK_OBJECTS michael@0: #define TRACK_ALL_TASK_OBJECTS michael@0: #endif // TRACK_ALL_TASK_OBJECTS michael@0: #endif // NDEBUG michael@0: michael@0: namespace tracked_objects { michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: // Location provides basic info where of an object was constructed, or was michael@0: // significantly brought to life. michael@0: michael@0: class Location { michael@0: public: michael@0: // Constructor should be called with a long-lived char*, such as __FILE__. michael@0: // It assumes the provided value will persist as a global constant, and it michael@0: // will not make a copy of it. michael@0: Location(const char* function_name, const char* file_name, int line_number) michael@0: : function_name_(function_name), michael@0: file_name_(file_name), michael@0: line_number_(line_number) { } michael@0: michael@0: // Provide a default constructor for easy of debugging. michael@0: Location() michael@0: : function_name_("Unknown"), michael@0: file_name_("Unknown"), michael@0: line_number_(-1) { } michael@0: michael@0: // Comparison operator for insertion into a std::map<> hash tables. michael@0: // All we need is *some* (any) hashing distinction. Strings should already michael@0: // be unique, so we don't bother with strcmp or such. michael@0: // Use line number as the primary key (because it is fast, and usually gets us michael@0: // a difference), and then pointers as secondary keys (just to get some michael@0: // distinctions). michael@0: bool operator < (const Location& other) const { michael@0: if (line_number_ != other.line_number_) michael@0: return line_number_ < other.line_number_; michael@0: if (file_name_ != other.file_name_) michael@0: return file_name_ < other.file_name_; michael@0: return function_name_ < other.function_name_; michael@0: } michael@0: michael@0: const char* function_name() const { return function_name_; } michael@0: const char* file_name() const { return file_name_; } michael@0: int line_number() const { return line_number_; } michael@0: michael@0: void Write(bool display_filename, bool display_function_name, michael@0: std::string* output) const; michael@0: michael@0: // Write function_name_ in HTML with '<' and '>' properly encoded. michael@0: void WriteFunctionName(std::string* output) const; michael@0: michael@0: private: michael@0: const char* const function_name_; michael@0: const char* const file_name_; michael@0: const int line_number_; michael@0: }; michael@0: michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: // Define a macro to record the current source location. michael@0: michael@0: #define FROM_HERE tracked_objects::Location(__FUNCTION__, __FILE__, __LINE__) michael@0: michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: michael@0: michael@0: class Births; michael@0: michael@0: class Tracked { michael@0: public: michael@0: Tracked(); michael@0: virtual ~Tracked(); michael@0: michael@0: // Used to record the FROM_HERE location of a caller. michael@0: void SetBirthPlace(const Location& from_here); michael@0: michael@0: // When a task sits around a long time, such as in a timer, or object watcher, michael@0: // this method should be called when the task becomes active, and its michael@0: // significant lifetime begins (and its waiting to be woken up has passed). michael@0: void ResetBirthTime(); michael@0: michael@0: bool MissingBirthplace() const; michael@0: michael@0: private: michael@0: #ifdef TRACK_ALL_TASK_OBJECTS michael@0: michael@0: // Pointer to instance were counts of objects with the same birth location michael@0: // (on the same thread) are stored. michael@0: Births* tracked_births_; michael@0: // The time this object was constructed. If its life consisted of a long michael@0: // waiting period, and then it became active, then this value is generally michael@0: // reset before the object begins it active life. michael@0: base::Time tracked_birth_time_; michael@0: michael@0: #endif // TRACK_ALL_TASK_OBJECTS michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(Tracked); michael@0: }; michael@0: michael@0: } // namespace tracked_objects michael@0: michael@0: #endif // BASE_TRACKED_H_