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 <string> |
| 11 |
| 12 #include "base/file_path.h" |
| 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/scoped_vector.h" |
| 17 #include "base/time.h" |
| 18 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 19 |
| 20 namespace performance_monitor { |
| 21 |
| 22 struct TimeRange { |
| 23 TimeRange(); |
| 24 TimeRange(base::Time start_time, base::Time end_time); |
| 25 ~TimeRange(); |
| 26 |
| 27 base::Time start; |
| 28 base::Time end; |
| 29 }; |
| 30 |
| 31 // The class supporting all performance monitor storage. This class wraps |
| 32 // multiple leveldb::DB objects. All methods must be called from a background |
| 33 // thread. Callers should use BrowserThread::PostBlockingPoolSequencedTask using |
| 34 // performance_monitor::kDBSequenceToken as the sequence token. |
| 35 // |
| 36 // Different schemas are used for the different leveldb::DB's based off of the |
| 37 // structure of the data and the common ways that it will need to be accessed. |
| 38 // The following specifies the schema of each type of leveldb::DB. Delimiters |
| 39 // are denoted with a '-'. |
| 40 // |
| 41 // State DB: |
| 42 // Stores information about the configuration or 'state' of the browser. Things |
| 43 // like browser version go in here. |
| 44 // Key: Unique Identifier |
| 45 // Value: State Value |
| 46 // |
| 47 // Active Interval DB: |
| 48 // Stores information about when there is data in the database. When the |
| 49 // database is constructed, the time is noted as the start of the active |
| 50 // interval. Then, every write operation the current time is marked as the end |
| 51 // of the current active interval. If the database has no write operations for |
| 52 // a certian amount of time, then the database is considered inactive for that |
| 53 // time period and a new start time is noted. Having the key be the beginning |
| 54 // of the active interval allows for efficient upserts to the current active |
| 55 // interval. If the end of the active interval was in the key, then every update |
| 56 // to the active interval would have to remove a key and insert a new one. |
| 57 // Key: Beginning of ActiveInterval |
| 58 // Value: End of ActiveInterval |
| 59 // |
| 60 // Event DB: |
| 61 // Stores all events. A time and type is enough to uniquely identify an event. |
| 62 // Using the time that the event took place as the beginning of the key allows |
| 63 // us to efficiently answer the question: "What are all the events that took |
| 64 // place in this time range?". |
| 65 // Key: Time - Type |
| 66 // Value: Event in JSON |
| 67 // |
| 68 // Recent DB: |
| 69 // Stores the most recent metric statistics to go into the database. This |
| 70 // database becomes useful when it is necessary to find all the active metrics |
| 71 // within a timerange. Without it, all the metric databases would need to be |
| 72 // searched to see if that metric is active. |
| 73 // Key: Time - Metric - Activity |
| 74 // Value: Statistic |
| 75 // |
| 76 // Metric DB: |
| 77 // Stores the statistics for different metrics. Having the activity before the |
| 78 // time in the key esures that all statistics for a specific acticity are |
| 79 // spatially local. |
| 80 // Key: Metric - Activity - Time |
| 81 // Value: Statistic |
| 82 class Database : public base::RefCountedThreadSafe<Database> { |
| 83 public: |
| 84 // The class that the database will use to infer time. Abstracting out the |
| 85 // time mechanism allows for easy testing and mock data insetion. |
| 86 class Clock { |
| 87 public: |
| 88 Clock() {} |
| 89 virtual ~Clock() {} |
| 90 virtual base::Time GetTime() = 0; |
| 91 }; |
| 92 |
| 93 static scoped_refptr<Database> Create(FilePath path); |
| 94 |
| 95 // A "state" value is anything that can only have one value at a time, and |
| 96 // usually describes the state of the browser eg. version. |
| 97 bool AddStateValue(const std::string& key, const std::string& value); |
| 98 |
| 99 std::string GetStateValue(const std::string& key); |
| 100 |
| 101 // Erase everything in the database. Warning - No undo! |
| 102 void Clear(); |
| 103 |
| 104 // Returns the times for which there is data in the database. |
| 105 std::vector<TimeRange> GetActiveInterval(const base::Time& start, |
| 106 const base::Time& end); |
| 107 |
| 108 FilePath path() const { return path_; } |
| 109 |
| 110 void set_clock(scoped_ptr<Clock> clock) { |
| 111 clock_ = clock.Pass(); |
| 112 } |
| 113 |
| 114 private: |
| 115 friend class base::RefCountedThreadSafe<Database>; |
| 116 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorDatabaseSetupTest, OpenCloseTest); |
| 117 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorDatabaseSetupTest, |
| 118 ActiveIntervalTest); |
| 119 |
| 120 // By default, the database uses a clock that simply returns the current time. |
| 121 class SystemClock : public Clock { |
| 122 public: |
| 123 SystemClock() {} |
| 124 virtual ~SystemClock() {} |
| 125 virtual base::Time GetTime() OVERRIDE; |
| 126 }; |
| 127 |
| 128 explicit Database(const FilePath& path); |
| 129 virtual ~Database(); |
| 130 |
| 131 bool Close(); |
| 132 |
| 133 // Mark the database as being active for the current time. |
| 134 void UpdateActiveInterval(); |
| 135 |
| 136 // The directory where all the databases will reside. |
| 137 FilePath path_; |
| 138 |
| 139 // The key for the beginning of the active interval. |
| 140 std::string start_time_key_; |
| 141 |
| 142 // The last time the database had a transaction. |
| 143 base::Time last_update_time_; |
| 144 |
| 145 scoped_ptr<Clock> clock_; |
| 146 |
| 147 scoped_ptr<leveldb::DB> recent_db_; |
| 148 |
| 149 scoped_ptr<leveldb::DB> state_db_; |
| 150 |
| 151 scoped_ptr<leveldb::DB> active_interval_db_; |
| 152 |
| 153 scoped_ptr<leveldb::DB> metric_db_; |
| 154 |
| 155 leveldb::ReadOptions read_options_; |
| 156 leveldb::WriteOptions write_options_; |
| 157 |
| 158 DISALLOW_COPY_AND_ASSIGN(Database); |
| 159 }; |
| 160 } // namespace performance_monitor |
| 161 |
| 162 #endif // CHROME_BROWSER_PERFORMANCE_MONITOR_DATABASE_H_ |
OLD | NEW |