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/ref_counted.h" | |
16 #include "base/memory/scoped_ptr.h" | |
17 #include "base/memory/scoped_vector.h" | |
18 #include "base/time.h" | |
19 #include "third_party/leveldatabase/src/include/leveldb/db.h" | |
20 | |
21 namespace performance_monitor { | |
22 | |
23 // The class that the database will use to infer time. Abstracting out the time | |
24 // mechanism allows for easy testing and mock data insetion. | |
25 class DBClock : public base::RefCountedThreadSafe<DBClock> { | |
Aaron Boodman
2012/06/08 19:33:56
These should be nested classes of Database and los
Aaron Boodman
2012/06/08 19:33:56
Why is it necessary for such a simple class to be
eaugusti
2012/06/08 22:52:08
We had it like this so that tests and the db could
| |
26 public: | |
27 DBClock() {} | |
28 virtual base::Time GetTime() = 0; | |
29 protected: | |
30 friend class base::RefCountedThreadSafe<DBClock>; | |
31 virtual ~DBClock() {} | |
32 }; | |
33 | |
34 // By default, the database uses a clock that simply returns the current time. | |
35 class SystemClock : public DBClock { | |
36 public: | |
37 SystemClock() {} | |
38 virtual base::Time GetTime() OVERRIDE; | |
39 private: | |
40 virtual ~SystemClock() {} | |
41 }; | |
42 | |
43 // The class supporting all performance monitor storage. This class wraps | |
44 // multiple leveldb::DB objects. Most methods must be called on a background | |
45 // thread and are named to reflect that. Callers should use | |
46 // BrowserThread::PostBlockingPoolSequencedTask using | |
47 // performance_monitor::kDBSequenceToken as the sequence token. | |
48 class Database : public base::RefCountedThreadSafe<Database> { | |
49 public: | |
50 typedef std::vector<std::pair<base::Time, base::Time> > TimePairs; | |
Aaron Boodman
2012/06/08 19:33:56
std::pair is almost always a mistake. "first" and
eaugusti
2012/06/08 22:52:08
Done.
| |
51 | |
52 // A "state" value is anything that can only have one value at a time, and | |
53 // usually describes the state of the browser eg. version. | |
54 bool AddStateValueOnBackgroundThread(const std::string& key, | |
55 const std::string& value); | |
56 | |
57 std::string GetStateValueOnBackgroundThread(const std::string& key); | |
58 | |
59 // Erase everything in the database. Warning - No undo! | |
60 void ClearOnBackgroundThread(); | |
61 | |
62 // Returns a list of pairs of (start time, end time). | |
63 TimePairs GetUptimeOnBackgroundThread(const base::Time& start, | |
64 const base::Time& end); | |
65 | |
66 FilePath path() const { return path_; } | |
67 | |
68 void set_clock(scoped_refptr<DBClock> clock) { | |
69 clock_ = clock; | |
70 } | |
71 | |
72 private: | |
73 friend class base::RefCountedThreadSafe<Database>; | |
74 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorDatabaseSetupTest, | |
Aaron Boodman
2012/06/08 19:33:56
friends are often a smell. Can the functionality t
eaugusti
2012/06/08 22:52:08
We were going to have only the performance monitor
| |
75 OpenCloseDefaultTest); | |
76 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorDatabaseSetupTest, | |
77 OpenCloseAlternatePathTest); | |
78 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorDatabaseSetupTest, UptimeTest); | |
79 | |
80 typedef std::map<std::string, leveldb::DB*> LevelDbMap; | |
81 | |
82 Database(const FilePath& path, scoped_refptr<DBClock> clock); | |
83 virtual ~Database(); | |
84 | |
85 static scoped_refptr<Database> InitOnBackgroundThread( | |
Aaron Boodman
2012/06/08 21:40:05
This class seems to have no public way to get an i
eaugusti
2012/06/08 22:52:08
Through the main performance monitor class.
| |
86 FilePath path, scoped_refptr<DBClock> clock); | |
87 | |
88 static scoped_refptr<Database> InitOnBackgroundThread(FilePath path) { | |
89 return InitOnBackgroundThread(path, new SystemClock()); | |
90 } | |
91 | |
92 static scoped_refptr<Database> InitOnBackgroundThread() { | |
93 return InitOnBackgroundThread(FilePath(), new SystemClock()); | |
94 } | |
95 | |
96 bool CloseOnBackgroundThread(); | |
97 | |
98 // Get the leveldb::DB object refered to by |id|. | |
99 // If one does not yet exist, it will be created. | |
100 leveldb::DB* FetchDBOnBackgroundThread(const std::string& id); | |
101 | |
102 // Mark the database as bring active for the current time. | |
Aaron Boodman
2012/06/08 21:40:05
s/bring/being/?
eaugusti
2012/06/08 22:52:08
Done.
| |
103 void UpdateUptimeOnBackgroundThread(); | |
104 | |
105 // The directory where all the databases will reside. | |
106 FilePath path_; | |
107 | |
108 // The key for the beginning of the uptime. | |
109 std::string start_time_key_; | |
110 | |
111 // The last time the database had a transaction. | |
112 base::Time last_update_time_; | |
113 | |
114 // The amount of time that the db has to be inactive to be considered "down". | |
115 base::TimeDelta uptime_threshold_; | |
116 | |
117 scoped_refptr<DBClock> clock_; | |
118 | |
119 // Mapping of names to LevelDB objects. | |
120 LevelDbMap databases_; | |
Aaron Boodman
2012/06/08 19:33:56
It appears that there are a fixed set of known lev
Aaron Boodman
2012/06/08 21:40:05
Also, I think that it would be good to briefly des
eaugusti
2012/06/08 22:52:08
Done.
| |
121 | |
122 leveldb::ReadOptions read_options_; | |
123 leveldb::WriteOptions write_options_; | |
124 | |
125 DISALLOW_COPY_AND_ASSIGN(Database); | |
126 }; | |
127 } // namespace performance_monitor | |
128 | |
129 #endif // CHROME_BROWSER_PERFORMANCE_MONITOR_DATABASE_H_ | |
OLD | NEW |