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

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: asvitkine nits. Created 5 years, 6 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
« no previous file with comments | « sql/BUILD.gn ('k') | sql/connection.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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. Values corrospond to UMA
175 // histograms, do not modify, or add or delete other than directly
176 // before EVENT_MAX_VALUE.
177 enum Events {
178 // Number of statements run, either with sql::Statement or Execute*().
179 EVENT_STATEMENT_RUN = 0,
180
181 // Number of rows returned by statements run.
182 EVENT_STATEMENT_ROWS,
183
184 // Number of statements successfully run (all steps returned SQLITE_DONE or
185 // SQLITE_ROW).
186 EVENT_STATEMENT_SUCCESS,
187
188 // Number of statements run by Execute() or ExecuteAndReturnErrorCode().
189 EVENT_EXECUTE,
190
191 // Number of rows changed by autocommit statements.
192 EVENT_CHANGES_AUTOCOMMIT,
193
194 // Number of rows changed by statements in transactions.
195 EVENT_CHANGES,
196
197 // Count actual SQLite transaction statements (not including nesting).
198 EVENT_BEGIN,
199 EVENT_COMMIT,
200 EVENT_ROLLBACK,
201
202 // Leave this at the end.
203 // TODO(shess): |EVENT_MAX| causes compile fail on Windows.
204 EVENT_MAX_VALUE
205 };
206 void RecordEvent(Events event, size_t count);
207 void RecordOneEvent(Events event) {
208 RecordEvent(event, 1);
209 }
210
154 // Run "PRAGMA integrity_check" and post each line of 211 // Run "PRAGMA integrity_check" and post each line of
155 // results into |messages|. Returns the success of running the 212 // results into |messages|. Returns the success of running the
156 // statement - per the SQLite documentation, if no errors are found the 213 // statement - per the SQLite documentation, if no errors are found the
157 // call should succeed, and a single value "ok" should be in messages. 214 // call should succeed, and a single value "ok" should be in messages.
158 bool FullIntegrityCheck(std::vector<std::string>* messages); 215 bool FullIntegrityCheck(std::vector<std::string>* messages);
159 216
160 // Runs "PRAGMA quick_check" and, unlike the FullIntegrityCheck method, 217 // Runs "PRAGMA quick_check" and, unlike the FullIntegrityCheck method,
161 // interprets the results returning true if the the statement executes 218 // interprets the results returning true if the the statement executes
162 // without error and results in a single "ok" value. 219 // without error and results in a single "ok" value.
163 bool QuickIntegrityCheck() WARN_UNUSED_RESULT; 220 bool QuickIntegrityCheck() WARN_UNUSED_RESULT;
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 // For recovery module. 465 // For recovery module.
409 friend class Recovery; 466 friend class Recovery;
410 467
411 // Allow test-support code to set/reset error ignorer. 468 // Allow test-support code to set/reset error ignorer.
412 friend class ScopedErrorIgnorer; 469 friend class ScopedErrorIgnorer;
413 470
414 // Statement accesses StatementRef which we don't want to expose to everybody 471 // Statement accesses StatementRef which we don't want to expose to everybody
415 // (they should go through Statement). 472 // (they should go through Statement).
416 friend class Statement; 473 friend class Statement;
417 474
475 friend class test::ScopedCommitHook;
476 friend class test::ScopedScalarFunction;
477 friend class test::ScopedMockTimeSource;
478
418 // Internal initialize function used by both Init and InitInMemory. The file 479 // 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 480 // 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. 481 // sqlite3_open. The string can also be sqlite's special ":memory:" string.
421 // 482 //
422 // |retry_flag| controls retrying the open if the error callback 483 // |retry_flag| controls retrying the open if the error callback
423 // addressed errors using RazeAndClose(). 484 // addressed errors using RazeAndClose().
424 enum Retry { 485 enum Retry {
425 NO_RETRY = 0, 486 NO_RETRY = 0,
426 RETRY_ON_POISON 487 RETRY_ON_POISON
427 }; 488 };
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 // allowing this function to be const. Open statements can block 602 // allowing this function to be const. Open statements can block
542 // closing the database, so only use in cases where the last ref is 603 // 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 604 // released before close could be called (which should always be the
544 // case for const functions). 605 // case for const functions).
545 scoped_refptr<StatementRef> GetUntrackedStatement(const char* sql) const; 606 scoped_refptr<StatementRef> GetUntrackedStatement(const char* sql) const;
546 607
547 bool IntegrityCheckHelper( 608 bool IntegrityCheckHelper(
548 const char* pragma_sql, 609 const char* pragma_sql,
549 std::vector<std::string>* messages) WARN_UNUSED_RESULT; 610 std::vector<std::string>* messages) WARN_UNUSED_RESULT;
550 611
612 // Record time spent executing explicit COMMIT statements.
613 void RecordCommitTime(const base::TimeDelta& delta);
614
615 // Record time in DML (Data Manipulation Language) statements such as INSERT
616 // or UPDATE outside of an explicit transaction. Due to implementation
617 // limitations time spent on DDL (Data Definition Language) statements such as
618 // ALTER and CREATE is not included.
619 void RecordAutoCommitTime(const base::TimeDelta& delta);
620
621 // Record all time spent on updating the database. This includes CommitTime()
622 // and AutoCommitTime(), plus any time spent spilling to the journal if
623 // transactions do not fit in cache.
624 void RecordUpdateTime(const base::TimeDelta& delta);
625
626 // Record all time spent running statements, including time spent doing
627 // updates and time spent on read-only queries.
628 void RecordQueryTime(const base::TimeDelta& delta);
629
630 // Record |delta| as query time if |read_only| (from sqlite3_stmt_readonly) is
631 // true, autocommit time if the database is not in a transaction, or update
632 // time if the database is in a transaction. Also records change count to
633 // EVENT_CHANGES_AUTOCOMMIT or EVENT_CHANGES_COMMIT.
634 void RecordTimeAndChanges(const base::TimeDelta& delta, bool read_only);
635
636 // Helper to return the current time from the time source.
637 base::TimeTicks Now() {
638 return clock_->Now();
639 }
640
551 // The actual sqlite database. Will be NULL before Init has been called or if 641 // The actual sqlite database. Will be NULL before Init has been called or if
552 // Init resulted in an error. 642 // Init resulted in an error.
553 sqlite3* db_; 643 sqlite3* db_;
554 644
555 // Parameters we'll configure in sqlite before doing anything else. Zero means 645 // Parameters we'll configure in sqlite before doing anything else. Zero means
556 // use the default value. 646 // use the default value.
557 int page_size_; 647 int page_size_;
558 int cache_size_; 648 int cache_size_;
559 bool exclusive_locking_; 649 bool exclusive_locking_;
560 bool restrict_to_user_; 650 bool restrict_to_user_;
(...skipping 26 matching lines...) Expand all
587 // to enable diagnostics to distinguish calls to never-opened 677 // to enable diagnostics to distinguish calls to never-opened
588 // databases (incorrect use of the API) from calls to once-valid 678 // databases (incorrect use of the API) from calls to once-valid
589 // databases. 679 // databases.
590 bool poisoned_; 680 bool poisoned_;
591 681
592 ErrorCallback error_callback_; 682 ErrorCallback error_callback_;
593 683
594 // Tag for auxiliary histograms. 684 // Tag for auxiliary histograms.
595 std::string histogram_tag_; 685 std::string histogram_tag_;
596 686
687 // Linear histogram for RecordEvent().
688 base::HistogramBase* stats_histogram_;
689
690 // Histogram for tracking time taken in commit.
691 base::HistogramBase* commit_time_histogram_;
692
693 // Histogram for tracking time taken in autocommit updates.
694 base::HistogramBase* autocommit_time_histogram_;
695
696 // Histogram for tracking time taken in updates (including commit and
697 // autocommit).
698 base::HistogramBase* update_time_histogram_;
699
700 // Histogram for tracking time taken in all queries.
701 base::HistogramBase* query_time_histogram_;
702
703 // Source for timing information, provided to allow tests to inject time
704 // changes.
705 scoped_ptr<TimeSource> clock_;
706
597 DISALLOW_COPY_AND_ASSIGN(Connection); 707 DISALLOW_COPY_AND_ASSIGN(Connection);
598 }; 708 };
599 709
600 } // namespace sql 710 } // namespace sql
601 711
602 #endif // SQL_CONNECTION_H_ 712 #endif // SQL_CONNECTION_H_
OLDNEW
« no previous file with comments | « sql/BUILD.gn ('k') | sql/connection.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698