Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_PERFORMANCE_MONITOR_DATABASE_H_ | |
| 6 #define CHROME_BROWSER_PERFORMANCE_MONITOR_DATABASE_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <vector> | |
| 10 #include <map> | |
| 11 #include <string> | |
| 12 | |
| 13 #include "base/file_path.h" | |
| 14 #include "base/gtest_prod_util.h" | |
| 15 #include "base/memory/linked_ptr.h" | |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "base/memory/scoped_vector.h" | |
| 19 #include "base/time.h" | |
| 20 #include "third_party/leveldatabase/src/include/leveldb/db.h" | |
| 21 | |
| 22 namespace performance_monitor { | |
| 23 | |
| 24 struct TimeRange { | |
| 25 TimeRange(); | |
| 26 TimeRange(base::Time start_time, base::Time end_time); | |
| 27 ~TimeRange(); | |
| 28 | |
| 29 base::Time start; | |
| 30 base::Time end; | |
| 31 }; | |
| 32 | |
| 33 // The class supporting all performance monitor storage. This class wraps | |
| 34 // multiple leveldb::DB objects. Most methods must be called on a background | |
| 35 // thread and are named to reflect that. Callers should use | |
| 36 // BrowserThread::PostBlockingPoolSequencedTask using | |
| 37 // performance_monitor::kDBSequenceToken as the sequence token. | |
| 38 // | |
| 39 // Different schemas are used for the different leveldb::DB's based off of the | |
| 40 // structure of the data and the common ways that it will need to be accessed. | |
| 41 // The following specifies the schema of each type of leveldb::DB. Delimiters | |
| 42 // are denoted with a '-'. | |
| 43 // | |
| 44 // State DB: | |
| 45 // Stores information about the configuration or 'state' of the browser. Things | |
| 46 // like browser version go in here. | |
| 47 // Key: Unique Identifier | |
| 48 // Value: State Value | |
| 49 // | |
| 50 // Active Interval DB: | |
| 51 // Stores information about when there is data in the database. When the | |
| 52 // database is constructed, the time is noted as the start of the active | |
| 53 // interval. Then, every write operation the current time is marked as the end | |
| 54 // of the current active interval. If the database has no write operations for | |
| 55 // a certian amount of time, then the database is considered inactive for that | |
| 56 // time period and a new start time is noted. Having the key be the beginning | |
| 57 // of the active interval allows for efficient upserts to the current active | |
| 58 // interval. I the end of the active interval was in the key, then every update | |
|
Aaron Boodman
2012/06/11 21:08:02
s/I/If/
eaugusti
2012/06/11 22:05:07
Done.
| |
| 59 // to the active interval would have to remove a key and insert a new one. | |
| 60 // Key: Beginning of ActiveInterval | |
| 61 // Value: End of ActiveInterval | |
| 62 // | |
| 63 // Event DB: | |
| 64 // Stores all events. A time and type is enough to uniquely identify an event. | |
| 65 // Using the time that the event took place as the beginning of the key allows | |
| 66 // us to efficiently answer the question: "What are all the events that took | |
| 67 // place in this time range?". | |
| 68 // Key: Time - Type | |
| 69 // Value: Event in JSON | |
| 70 // | |
| 71 // Recent DB: | |
| 72 // Stores the most recent metric statistics to go into the database. This | |
| 73 // database becomes useful when it is necessary to find all the active metrics | |
| 74 // within a timerange. Without it, all the metric databases would need to be | |
| 75 // searched to see if that metric is active. | |
| 76 // Key: Time - Metric - Activity | |
| 77 // Value: Statistic | |
| 78 // | |
| 79 // Any Metric DB: | |
| 80 // Every metric gets its own database. The name of the database is just the name | |
| 81 // of the metric. The activity is first in the key so that all statistics for a | |
| 82 // specific acticity are spatially local. | |
| 83 // Key: Activity - Time | |
| 84 // Value: Statistic | |
| 85 class Database : public base::RefCountedThreadSafe<Database> { | |
| 86 public: | |
| 87 // The class that the database will use to infer time. Abstracting out the | |
| 88 // time mechanism allows for easy testing and mock data insetion. | |
| 89 class Clock { | |
| 90 public: | |
| 91 Clock() {} | |
| 92 virtual ~Clock() {} | |
| 93 virtual base::Time GetTime() = 0; | |
| 94 }; | |
| 95 | |
| 96 // A "state" value is anything that can only have one value at a time, and | |
| 97 // usually describes the state of the browser eg. version. | |
| 98 bool AddStateValueOnBackgroundThread(const std::string& key, | |
| 99 const std::string& value); | |
| 100 | |
| 101 std::string GetStateValueOnBackgroundThread(const std::string& key); | |
| 102 | |
| 103 // Erase everything in the database. Warning - No undo! | |
| 104 void ClearOnBackgroundThread(); | |
| 105 | |
| 106 // Returns the times for which there is data in the database. | |
| 107 std::vector<TimeRange> GetActiveIntervalOnBackgroundThread( | |
| 108 const base::Time& start, const base::Time& end); | |
| 109 | |
| 110 FilePath path() const { return path_; } | |
| 111 | |
| 112 void set_clock(linked_ptr<Clock> clock) { | |
| 113 clock_ = clock; | |
| 114 } | |
| 115 | |
| 116 private: | |
| 117 friend class base::RefCountedThreadSafe<Database>; | |
| 118 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorDatabaseSetupTest, | |
| 119 OpenCloseDefaultTest); | |
| 120 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorDatabaseSetupTest, | |
| 121 OpenCloseAlternatePathTest); | |
| 122 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorDatabaseSetupTest, | |
| 123 ActiveIntervalTest); | |
| 124 | |
| 125 typedef std::map<std::string, linked_ptr<leveldb::DB> > LevelDbMap; | |
| 126 | |
| 127 // By default, the database uses a clock that simply returns the current time. | |
| 128 class SystemClock : public Clock { | |
| 129 public: | |
| 130 SystemClock() {} | |
| 131 virtual ~SystemClock() {} | |
| 132 virtual base::Time GetTime() OVERRIDE; | |
| 133 }; | |
| 134 | |
| 135 Database(const FilePath& path, linked_ptr<Clock> clock); | |
| 136 virtual ~Database(); | |
| 137 | |
| 138 static scoped_refptr<Database> InitOnBackgroundThread( | |
| 139 FilePath path, linked_ptr<Clock> clock); | |
| 140 | |
| 141 static scoped_refptr<Database> InitOnBackgroundThread(FilePath path) { | |
| 142 return InitOnBackgroundThread(path, linked_ptr<Clock>(new SystemClock())); | |
| 143 } | |
| 144 | |
| 145 static scoped_refptr<Database> InitOnBackgroundThread() { | |
| 146 return InitOnBackgroundThread(FilePath(), | |
| 147 linked_ptr<Clock>(new SystemClock())); | |
| 148 } | |
| 149 | |
| 150 bool CloseOnBackgroundThread(); | |
| 151 | |
| 152 // Get the leveldb::DB object refered to by |id|. | |
| 153 // If one does not yet exist, it will be created. | |
| 154 linked_ptr<leveldb::DB> FetchDBOnBackgroundThread(const std::string& id); | |
| 155 | |
| 156 // Mark the database as being active for the current time. | |
| 157 void UpdateActiveIntervalOnBackgroundThread(); | |
| 158 | |
| 159 // The directory where all the databases will reside. | |
| 160 FilePath path_; | |
| 161 | |
| 162 // The key for the beginning of the active interval. | |
| 163 std::string start_time_key_; | |
| 164 | |
| 165 // The last time the database had a transaction. | |
| 166 base::Time last_update_time_; | |
| 167 | |
| 168 linked_ptr<Clock> clock_; | |
| 169 | |
| 170 // Mapping of names to LevelDB objects. | |
| 171 LevelDbMap databases_; | |
| 172 | |
| 173 leveldb::ReadOptions read_options_; | |
| 174 leveldb::WriteOptions write_options_; | |
| 175 | |
| 176 DISALLOW_COPY_AND_ASSIGN(Database); | |
| 177 }; | |
| 178 } // namespace performance_monitor | |
| 179 | |
| 180 #endif // CHROME_BROWSER_PERFORMANCE_MONITOR_DATABASE_H_ | |
| OLD | NEW |