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