Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: sql/connection.h

Issue 1145833002: [sql] Stats gathering for sql/ APIs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add mockable time source. Then mock it mercilessly. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef SQL_CONNECTION_H_ 5 #ifndef SQL_CONNECTION_H_
6 #define SQL_CONNECTION_H_ 6 #define SQL_CONNECTION_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <map> 9 #include <map>
10 #include <set> 10 #include <set>
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/callback.h" 14 #include "base/callback.h"
15 #include "base/compiler_specific.h" 15 #include "base/compiler_specific.h"
16 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "base/memory/ref_counted.h" 17 #include "base/memory/ref_counted.h"
18 #include "base/memory/scoped_ptr.h" 18 #include "base/memory/scoped_ptr.h"
19 #include "base/threading/thread_restrictions.h" 19 #include "base/threading/thread_restrictions.h"
20 #include "base/time/time.h" 20 #include "base/time/time.h"
21 #include "sql/sql_export.h" 21 #include "sql/sql_export.h"
22 22
23 struct sqlite3; 23 struct sqlite3;
24 struct sqlite3_stmt; 24 struct sqlite3_stmt;
25 25
26 namespace base { 26 namespace base {
27 class FilePath; 27 class FilePath;
28 class HistogramBase;
28 } 29 }
29 30
30 namespace sql { 31 namespace sql {
31 32
32 class Recovery; 33 class Recovery;
33 class Statement; 34 class Statement;
34 35
36 // To allow some test classes to be friended.
37 namespace test {
38 class ScopedCommitHook;
39 class ScopedScalarFunction;
40 class ScopedMockTimeSource;
41 }
42
35 // Uniquely identifies a statement. There are two modes of operation: 43 // Uniquely identifies a statement. There are two modes of operation:
36 // 44 //
37 // - In the most common mode, you will use the source file and line number to 45 // - In the most common mode, you will use the source file and line number to
38 // identify your statement. This is a convienient way to get uniqueness for 46 // identify your statement. This is a convienient way to get uniqueness for
39 // a statement that is only used in one place. Use the SQL_FROM_HERE macro 47 // a statement that is only used in one place. Use the SQL_FROM_HERE macro
40 // to generate a StatementID. 48 // to generate a StatementID.
41 // 49 //
42 // - In the "custom" mode you may use the statement from different places or 50 // - In the "custom" mode you may use the statement from different places or
43 // need to manage it yourself for whatever reason. In this case, you should 51 // need to manage it yourself for whatever reason. In this case, you should
44 // make up your own unique name and pass it to the StatementID. This name 52 // make up your own unique name and pass it to the StatementID. This name
(...skipping 28 matching lines...) Expand all
73 81
74 private: 82 private:
75 int number_; 83 int number_;
76 const char* str_; 84 const char* str_;
77 }; 85 };
78 86
79 #define SQL_FROM_HERE sql::StatementID(__FILE__, __LINE__) 87 #define SQL_FROM_HERE sql::StatementID(__FILE__, __LINE__)
80 88
81 class Connection; 89 class Connection;
82 90
91 // Abstract the source of timing information for metrics (RecordCommitTime, etc)
92 // to allow testing control.
93 class SQL_EXPORT TimeSource {
94 public:
95 TimeSource() {}
96 virtual ~TimeSource() {}
97
98 // Return the current time (by default base::TimeTicks::Now()).
99 virtual base::TimeTicks Now();
100
101 private:
102 DISALLOW_COPY_AND_ASSIGN(TimeSource);
103 };
104
83 class SQL_EXPORT Connection { 105 class SQL_EXPORT Connection {
84 private: 106 private:
85 class StatementRef; // Forward declaration, see real one below. 107 class StatementRef; // Forward declaration, see real one below.
86 108
87 public: 109 public:
88 // The database is opened by calling Open[InMemory](). Any uncommitted 110 // The database is opened by calling Open[InMemory](). Any uncommitted
89 // transactions will be rolled back when this object is deleted. 111 // transactions will be rolled back when this object is deleted.
90 Connection(); 112 Connection();
91 ~Connection(); 113 ~Connection();
92 114
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 void set_error_callback(const ErrorCallback& callback) { 155 void set_error_callback(const ErrorCallback& callback) {
134 error_callback_ = callback; 156 error_callback_ = callback;
135 } 157 }
136 bool has_error_callback() const { 158 bool has_error_callback() const {
137 return !error_callback_.is_null(); 159 return !error_callback_.is_null();
138 } 160 }
139 void reset_error_callback() { 161 void reset_error_callback() {
140 error_callback_.Reset(); 162 error_callback_.Reset();
141 } 163 }
142 164
143 // Set this tag to enable additional connection-type histogramming 165 // Set this to enable additional per-connection histogramming. Must be called
144 // for SQLite error codes and database version numbers. 166 // before Open().
145 void set_histogram_tag(const std::string& tag) { 167 void set_histogram_tag(const std::string& tag);
146 histogram_tag_ = tag;
147 }
148 168
149 // Record a sparse UMA histogram sample under 169 // Record a sparse UMA histogram sample under
150 // |name|+"."+|histogram_tag_|. If |histogram_tag_| is empty, no 170 // |name|+"."+|histogram_tag_|. If |histogram_tag_| is empty, no
151 // histogram is recorded. 171 // histogram is recorded.
152 void AddTaggedHistogram(const std::string& name, size_t sample) const; 172 void AddTaggedHistogram(const std::string& name, size_t sample) const;
153 173
174 // Track various API calls and results.
175 enum Events {
176 // Number of statements run, either with sql::Statement or Execute*().
177 EVENT_STATEMENT_RUN = 0,
178
179 // Number of rows returned by statements run.
180 EVENT_STATEMENT_ROWS,
181
182 // Number of statements successfully run (all steps returned SQLITE_DONE or
183 // SQLITE_ROW).
184 EVENT_STATEMENT_SUCCESS,
185
186 // Number of statements run by Execute() or ExecuteAndReturnErrorCode().
187 EVENT_EXECUTE,
188
189 // Number of rows changed by autocommit statements.
190 EVENT_CHANGES_AUTOCOMMIT,
191
192 // Number of rows changed by statements in transactions.
193 EVENT_CHANGES,
194
195 // Count actual SQLite transaction statements (not including nesting).
196 EVENT_BEGIN,
197 EVENT_COMMIT,
198 EVENT_ROLLBACK,
199
200 // Leave this at the end.
201 // TODO(shess): |EVENT_MAX| causes compile fail on Windows.
202 EVENT_MX
203 };
204 void RecordEvent(Events event, size_t count);
205 void RecordOneEvent(Events event) {
206 RecordEvent(event, 1);
207 }
208
154 // Run "PRAGMA integrity_check" and post each line of 209 // Run "PRAGMA integrity_check" and post each line of
155 // results into |messages|. Returns the success of running the 210 // results into |messages|. Returns the success of running the
156 // statement - per the SQLite documentation, if no errors are found the 211 // statement - per the SQLite documentation, if no errors are found the
157 // call should succeed, and a single value "ok" should be in messages. 212 // call should succeed, and a single value "ok" should be in messages.
158 bool FullIntegrityCheck(std::vector<std::string>* messages); 213 bool FullIntegrityCheck(std::vector<std::string>* messages);
159 214
160 // Runs "PRAGMA quick_check" and, unlike the FullIntegrityCheck method, 215 // Runs "PRAGMA quick_check" and, unlike the FullIntegrityCheck method,
161 // interprets the results returning true if the the statement executes 216 // interprets the results returning true if the the statement executes
162 // without error and results in a single "ok" value. 217 // without error and results in a single "ok" value.
163 bool QuickIntegrityCheck() WARN_UNUSED_RESULT; 218 bool QuickIntegrityCheck() WARN_UNUSED_RESULT;
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 // For recovery module. 463 // For recovery module.
409 friend class Recovery; 464 friend class Recovery;
410 465
411 // Allow test-support code to set/reset error ignorer. 466 // Allow test-support code to set/reset error ignorer.
412 friend class ScopedErrorIgnorer; 467 friend class ScopedErrorIgnorer;
413 468
414 // Statement accesses StatementRef which we don't want to expose to everybody 469 // Statement accesses StatementRef which we don't want to expose to everybody
415 // (they should go through Statement). 470 // (they should go through Statement).
416 friend class Statement; 471 friend class Statement;
417 472
473 friend class test::ScopedCommitHook;
474 friend class test::ScopedScalarFunction;
475 friend class test::ScopedMockTimeSource;
476
418 // Internal initialize function used by both Init and InitInMemory. The file 477 // Internal initialize function used by both Init and InitInMemory. The file
419 // name is always 8 bits since we want to use the 8-bit version of 478 // name is always 8 bits since we want to use the 8-bit version of
420 // sqlite3_open. The string can also be sqlite's special ":memory:" string. 479 // sqlite3_open. The string can also be sqlite's special ":memory:" string.
421 // 480 //
422 // |retry_flag| controls retrying the open if the error callback 481 // |retry_flag| controls retrying the open if the error callback
423 // addressed errors using RazeAndClose(). 482 // addressed errors using RazeAndClose().
424 enum Retry { 483 enum Retry {
425 NO_RETRY = 0, 484 NO_RETRY = 0,
426 RETRY_ON_POISON 485 RETRY_ON_POISON
427 }; 486 };
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 // allowing this function to be const. Open statements can block 600 // allowing this function to be const. Open statements can block
542 // closing the database, so only use in cases where the last ref is 601 // closing the database, so only use in cases where the last ref is
543 // released before close could be called (which should always be the 602 // released before close could be called (which should always be the
544 // case for const functions). 603 // case for const functions).
545 scoped_refptr<StatementRef> GetUntrackedStatement(const char* sql) const; 604 scoped_refptr<StatementRef> GetUntrackedStatement(const char* sql) const;
546 605
547 bool IntegrityCheckHelper( 606 bool IntegrityCheckHelper(
548 const char* pragma_sql, 607 const char* pragma_sql,
549 std::vector<std::string>* messages) WARN_UNUSED_RESULT; 608 std::vector<std::string>* messages) WARN_UNUSED_RESULT;
550 609
610 // Record time spent executing explicit COMMIT statements.
611 void RecordCommitTime(const base::TimeDelta& delta);
612
613 // Record time in DML (Data Manipulation Language) statements such as INSERT
614 // or UPDATE outside of an explicit transaction. Due to implementation
615 // limitations time spent on DDL (Data Definition Language) statements such as
616 // ALTER and CREATE is not included.
617 void RecordAutoCommitTime(const base::TimeDelta& delta);
618
619 // Record all time spent on updating the database. This includes CommitTime()
620 // and AutoCommitTime(), plus any time spent spilling to the journal if
621 // transactions do not fit in cache.
622 void RecordUpdateTime(const base::TimeDelta& delta);
623
624 // Record all time spent running statements, including time spent doing
625 // updates and time spent on read-only queries.
626 void RecordQueryTime(const base::TimeDelta& delta);
627
628 // Record |delta| as query time if |read_only| (from sqlite3_stmt_readonly) is
629 // true, autocommit time if the database is not in a transaction, or update
630 // time if the database is in a transaction. Also records change count to
631 // EVENT_CHANGES_AUTOCOMMIT or EVENT_CHANGES_COMMIT.
632 void RecordTimeAndChanges(const base::TimeDelta& delta, bool read_only);
633
634 // Helper to return the current time from the time source.
635 base::TimeTicks Now() {
636 return clock_->Now();
637 }
638
551 // The actual sqlite database. Will be NULL before Init has been called or if 639 // The actual sqlite database. Will be NULL before Init has been called or if
552 // Init resulted in an error. 640 // Init resulted in an error.
553 sqlite3* db_; 641 sqlite3* db_;
554 642
555 // Parameters we'll configure in sqlite before doing anything else. Zero means 643 // Parameters we'll configure in sqlite before doing anything else. Zero means
556 // use the default value. 644 // use the default value.
557 int page_size_; 645 int page_size_;
558 int cache_size_; 646 int cache_size_;
559 bool exclusive_locking_; 647 bool exclusive_locking_;
560 bool restrict_to_user_; 648 bool restrict_to_user_;
(...skipping 26 matching lines...) Expand all
587 // to enable diagnostics to distinguish calls to never-opened 675 // to enable diagnostics to distinguish calls to never-opened
588 // databases (incorrect use of the API) from calls to once-valid 676 // databases (incorrect use of the API) from calls to once-valid
589 // databases. 677 // databases.
590 bool poisoned_; 678 bool poisoned_;
591 679
592 ErrorCallback error_callback_; 680 ErrorCallback error_callback_;
593 681
594 // Tag for auxiliary histograms. 682 // Tag for auxiliary histograms.
595 std::string histogram_tag_; 683 std::string histogram_tag_;
596 684
685 // Linear histogram for RecordEvent().
686 base::HistogramBase* stats_histogram_;
687
688 // Histogram for tracking time taken in commit.
689 base::HistogramBase* commit_time_histogram_;
690
691 // Histogram for tracking time taken in autocommit updates.
692 base::HistogramBase* autocommit_time_histogram_;
693
694 // Histogram for tracking time taken in updates (including commit and
695 // autocommit).
696 base::HistogramBase* update_time_histogram_;
697
698 // Histogram for tracking time taken in all queries.
699 base::HistogramBase* query_time_histogram_;
700
701 // Source for timing information, provided to allow tests to inject time
702 // changes.
703 scoped_ptr<TimeSource> clock_;
704
597 DISALLOW_COPY_AND_ASSIGN(Connection); 705 DISALLOW_COPY_AND_ASSIGN(Connection);
598 }; 706 };
599 707
600 } // namespace sql 708 } // namespace sql
601 709
602 #endif // SQL_CONNECTION_H_ 710 #endif // SQL_CONNECTION_H_
OLDNEW
« no previous file with comments | « sql/BUILD.gn ('k') | sql/connection.cc » ('j') | sql/connection_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698