Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 1 | // Copyright 2018 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 | #include "sql/initialization.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/metrics/histogram_macros.h" |
| 9 | #include "base/no_destructor.h" |
Will Harris | b869359 | 2018-08-28 22:58:44 | [diff] [blame] | 10 | #include "base/numerics/safe_conversions.h" |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 11 | #include "base/threading/sequenced_task_runner_handle.h" |
Etienne Bergeron | d4888db | 2021-04-30 01:18:49 | [diff] [blame^] | 12 | #include "base/trace_event/trace_event.h" |
Justin Cohen | 19ba543 | 2018-07-10 16:47:30 | [diff] [blame] | 13 | #include "build/build_config.h" |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 14 | #include "third_party/sqlite/sqlite3.h" |
| 15 | |
| 16 | namespace sql { |
| 17 | |
| 18 | namespace { |
| 19 | |
Justin Cohen | 19ba543 | 2018-07-10 16:47:30 | [diff] [blame] | 20 | #if !defined(OS_IOS) |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 21 | void RecordSqliteMemory10Min() { |
Will Harris | b869359 | 2018-08-28 22:58:44 | [diff] [blame] | 22 | const int32_t used = |
| 23 | base::saturated_cast<int32_t>(sqlite3_memory_used() / 1024); |
Steven Holte | 9592222 | 2018-09-14 20:06:23 | [diff] [blame] | 24 | UMA_HISTOGRAM_COUNTS_1M("Sqlite.MemoryKB.TenMinutes", used); |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | void RecordSqliteMemoryHour() { |
Will Harris | b869359 | 2018-08-28 22:58:44 | [diff] [blame] | 28 | const int32_t used = |
| 29 | base::saturated_cast<int32_t>(sqlite3_memory_used() / 1024); |
Steven Holte | 9592222 | 2018-09-14 20:06:23 | [diff] [blame] | 30 | UMA_HISTOGRAM_COUNTS_1M("Sqlite.MemoryKB.OneHour", used); |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void RecordSqliteMemoryDay() { |
Will Harris | b869359 | 2018-08-28 22:58:44 | [diff] [blame] | 34 | const int32_t used = |
| 35 | base::saturated_cast<int32_t>(sqlite3_memory_used() / 1024); |
Steven Holte | 9592222 | 2018-09-14 20:06:23 | [diff] [blame] | 36 | UMA_HISTOGRAM_COUNTS_1M("Sqlite.MemoryKB.OneDay", used); |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void RecordSqliteMemoryWeek() { |
Will Harris | b869359 | 2018-08-28 22:58:44 | [diff] [blame] | 40 | const int32_t used = |
| 41 | base::saturated_cast<int32_t>(sqlite3_memory_used() / 1024); |
Steven Holte | 9592222 | 2018-09-14 20:06:23 | [diff] [blame] | 42 | UMA_HISTOGRAM_COUNTS_1M("Sqlite.MemoryKB.OneWeek", used); |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 43 | } |
Justin Cohen | 19ba543 | 2018-07-10 16:47:30 | [diff] [blame] | 44 | #endif // !defined(OS_IOS) |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 45 | |
| 46 | } // anonymous namespace |
| 47 | |
| 48 | void EnsureSqliteInitialized() { |
| 49 | // sqlite3_initialize() uses double-checked locking and thus can have |
| 50 | // data races. |
| 51 | static base::NoDestructor<base::Lock> sqlite_init_lock; |
| 52 | base::AutoLock auto_lock(*sqlite_init_lock); |
| 53 | |
| 54 | static bool first_call = true; |
| 55 | if (first_call) { |
Etienne Bergeron | d4888db | 2021-04-30 01:18:49 | [diff] [blame^] | 56 | TRACE_EVENT0("sql", "EnsureSqliteInitialized"); |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 57 | sqlite3_initialize(); |
| 58 | |
Justin Cohen | 19ba543 | 2018-07-10 16:47:30 | [diff] [blame] | 59 | #if !defined(OS_IOS) |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 60 | // Schedule callback to record memory footprint histograms at 10m, 1h, and |
| 61 | // 1d. There may not be a registered task runner in tests. |
Justin Cohen | 19ba543 | 2018-07-10 16:47:30 | [diff] [blame] | 62 | // TODO(crbug.com/861889): Disable very long critical tasks on iOS until |
| 63 | // 861889 is fixed. |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 64 | if (base::SequencedTaskRunnerHandle::IsSet()) { |
| 65 | base::SequencedTaskRunnerHandle::Get()->PostDelayedTask( |
| 66 | FROM_HERE, base::BindOnce(&RecordSqliteMemory10Min), |
| 67 | base::TimeDelta::FromMinutes(10)); |
| 68 | base::SequencedTaskRunnerHandle::Get()->PostDelayedTask( |
| 69 | FROM_HERE, base::BindOnce(&RecordSqliteMemoryHour), |
| 70 | base::TimeDelta::FromHours(1)); |
| 71 | base::SequencedTaskRunnerHandle::Get()->PostDelayedTask( |
| 72 | FROM_HERE, base::BindOnce(&RecordSqliteMemoryDay), |
| 73 | base::TimeDelta::FromDays(1)); |
| 74 | base::SequencedTaskRunnerHandle::Get()->PostDelayedTask( |
| 75 | FROM_HERE, base::BindOnce(&RecordSqliteMemoryWeek), |
| 76 | base::TimeDelta::FromDays(7)); |
| 77 | } |
Justin Cohen | 19ba543 | 2018-07-10 16:47:30 | [diff] [blame] | 78 | #endif // !defined(OS_IOS) |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 79 | first_call = false; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | } // namespace sql |