OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "components/startup_metric_utils/browser/startup_metric_utils.h" | 5 #include "components/startup_metric_utils/browser/startup_metric_utils.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/containers/hash_tables.h" | 9 #include "base/containers/hash_tables.h" |
10 #include "base/environment.h" | 10 #include "base/environment.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/prefs/pref_registry_simple.h" |
| 15 #include "base/prefs/pref_service.h" |
| 16 #include "base/process/process_info.h" |
14 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
15 #include "base/sys_info.h" | 18 #include "base/sys_info.h" |
16 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
17 #include "base/trace_event/trace_event.h" | 20 #include "base/trace_event/trace_event.h" |
18 #include "build/build_config.h" | 21 #include "build/build_config.h" |
19 | 22 |
20 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
21 #include <winternl.h> | 24 #include <winternl.h> |
22 #include "base/win/win_util.h" | 25 #include "base/win/win_util.h" |
23 #include "base/win/windows_version.h" | 26 #include "base/win/windows_version.h" |
24 #endif | 27 #endif |
25 | 28 |
26 namespace startup_metric_utils { | 29 namespace startup_metric_utils { |
27 | 30 |
28 namespace { | 31 namespace { |
29 | 32 |
| 33 const char kLastStartupTimestampPref[] = |
| 34 "startup_metric.last_startup_timestamp"; |
| 35 |
30 // Mark as volatile to defensively make sure usage is thread-safe. | 36 // Mark as volatile to defensively make sure usage is thread-safe. |
31 // Note that at the time of this writing, access is only on the UI thread. | 37 // Note that at the time of this writing, access is only on the UI thread. |
32 volatile bool g_non_browser_ui_displayed = false; | 38 volatile bool g_non_browser_ui_displayed = false; |
33 | 39 |
34 base::LazyInstance<base::TimeTicks>::Leaky g_process_creation_ticks = | 40 base::LazyInstance<base::TimeTicks>::Leaky g_process_creation_ticks = |
35 LAZY_INSTANCE_INITIALIZER; | 41 LAZY_INSTANCE_INITIALIZER; |
36 | 42 |
37 base::LazyInstance<base::TimeTicks>::Leaky g_browser_main_entry_point_ticks = | 43 base::LazyInstance<base::TimeTicks>::Leaky g_browser_main_entry_point_ticks = |
38 LAZY_INSTANCE_INITIALIZER; | 44 LAZY_INSTANCE_INITIALIZER; |
39 | 45 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 SIZE_T PrivatePageCount; | 95 SIZE_T PrivatePageCount; |
90 LARGE_INTEGER Reserved6[6]; | 96 LARGE_INTEGER Reserved6[6]; |
91 // Array of SYSTEM_THREAD_INFORMATION structs follows. | 97 // Array of SYSTEM_THREAD_INFORMATION structs follows. |
92 }; | 98 }; |
93 | 99 |
94 // The signature of the NtQuerySystemInformation function. | 100 // The signature of the NtQuerySystemInformation function. |
95 typedef NTSTATUS (WINAPI *NtQuerySystemInformationPtr)( | 101 typedef NTSTATUS (WINAPI *NtQuerySystemInformationPtr)( |
96 SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG); | 102 SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG); |
97 #endif // defined(OS_WIN) | 103 #endif // defined(OS_WIN) |
98 | 104 |
| 105 #define UMA_HISTOGRAM_TIME_IN_MINUTES_MONTH_RANGE(name, sample) \ |
| 106 UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, \ |
| 107 base::TimeDelta::FromDays(30).InMinutes(), 50) |
99 | 108 |
100 // Helper macro for splitting out an UMA histogram based on cold or warm start. | 109 // Helper macro for splitting out an UMA histogram based on cold or warm start. |
101 // |type| is the histogram type, and corresponds to an UMA macro like | 110 // |type| is the histogram type, and corresponds to an UMA macro like |
102 // UMA_HISTOGRAM_LONG_TIMES. It must be itself be a macro that only takes two | 111 // UMA_HISTOGRAM_LONG_TIMES. It must be itself be a macro that only takes two |
103 // parameters. | 112 // parameters. |
104 // |basename| is the basename of the histogram. A histogram of this name will | 113 // |basename| is the basename of the histogram. A histogram of this name will |
105 // always be recorded to. If the startup is either cold or warm then a value | 114 // always be recorded to. If the startup is either cold or warm then a value |
106 // will also be recorded to the histogram with name |basename| and suffix | 115 // will also be recorded to the histogram with name |basename| and suffix |
107 // ".ColdStart" or ".WarmStart", as appropriate. | 116 // ".ColdStart" or ".WarmStart", as appropriate. |
108 // |value_expr| is an expression evaluating to the value to be recorded. This | 117 // |value_expr| is an expression evaluating to the value to be recorded. This |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 // looping if the data is in fact invalid. | 376 // looping if the data is in fact invalid. |
368 if (proc_info->NextEntryOffset <= 0) | 377 if (proc_info->NextEntryOffset <= 0) |
369 return false; | 378 return false; |
370 index += proc_info->NextEntryOffset; | 379 index += proc_info->NextEntryOffset; |
371 } | 380 } |
372 | 381 |
373 return false; | 382 return false; |
374 } | 383 } |
375 #endif // defined(OS_WIN) | 384 #endif // defined(OS_WIN) |
376 | 385 |
| 386 void RegisterPrefs(PrefRegistrySimple* registry) { |
| 387 DCHECK(registry); |
| 388 registry->RegisterInt64Pref(kLastStartupTimestampPref, 0); |
| 389 } |
| 390 |
377 bool WasNonBrowserUIDisplayed() { | 391 bool WasNonBrowserUIDisplayed() { |
378 return g_non_browser_ui_displayed; | 392 return g_non_browser_ui_displayed; |
379 } | 393 } |
380 | 394 |
381 void SetNonBrowserUIDisplayed() { | 395 void SetNonBrowserUIDisplayed() { |
382 g_non_browser_ui_displayed = true; | 396 g_non_browser_ui_displayed = true; |
383 } | 397 } |
384 | 398 |
385 void RecordStartupProcessCreationTime(const base::Time& time) { | 399 void RecordStartupProcessCreationTime(const base::Time& time) { |
386 DCHECK(g_process_creation_ticks.Get().is_null()); | 400 DCHECK(g_process_creation_ticks.Get().is_null()); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 | 476 |
463 // Process create to chrome.dll:main(). Reported as a histogram only as | 477 // Process create to chrome.dll:main(). Reported as a histogram only as |
464 // the other two events above are sufficient for tracing purposes. | 478 // the other two events above are sufficient for tracing purposes. |
465 UMA_HISTOGRAM_WITH_STARTUP_TEMPERATURE( | 479 UMA_HISTOGRAM_WITH_STARTUP_TEMPERATURE( |
466 UMA_HISTOGRAM_LONG_TIMES, "Startup.LoadTime.ProcessCreateToDllMain", | 480 UMA_HISTOGRAM_LONG_TIMES, "Startup.LoadTime.ProcessCreateToDllMain", |
467 g_browser_main_entry_point_ticks.Get() - process_creation_ticks); | 481 g_browser_main_entry_point_ticks.Get() - process_creation_ticks); |
468 } | 482 } |
469 } | 483 } |
470 } | 484 } |
471 | 485 |
| 486 void RecordTimeSinceLastStartup(PrefService* pref_service) { |
| 487 DCHECK(pref_service); |
| 488 |
| 489 // Get the timestamp of the current startup. |
| 490 const base::Time process_start_time = |
| 491 base::CurrentProcessInfo::CreationTime(); |
| 492 |
| 493 // Get the timestamp of the last startup from |pref_service|. |
| 494 const int64_t last_startup_timestamp_internal = |
| 495 pref_service->GetInt64(kLastStartupTimestampPref); |
| 496 if (last_startup_timestamp_internal != 0) { |
| 497 // Log the Startup.TimeSinceLastStartup histogram. |
| 498 const base::Time last_startup_timestamp = |
| 499 base::Time::FromInternalValue(last_startup_timestamp_internal); |
| 500 const base::TimeDelta time_since_last_startup = |
| 501 process_start_time - last_startup_timestamp; |
| 502 const int minutes_since_last_startup = time_since_last_startup.InMinutes(); |
| 503 |
| 504 // Ignore negative values, which can be caused by system clock changes. |
| 505 if (minutes_since_last_startup >= 0) { |
| 506 UMA_HISTOGRAM_WITH_STARTUP_TEMPERATURE( |
| 507 UMA_HISTOGRAM_TIME_IN_MINUTES_MONTH_RANGE, |
| 508 "Startup.TimeSinceLastStartup", minutes_since_last_startup); |
| 509 } |
| 510 } |
| 511 |
| 512 // Write the timestamp of the current startup in |pref_service|. |
| 513 pref_service->SetInt64(kLastStartupTimestampPref, |
| 514 process_start_time.ToInternalValue()); |
| 515 } |
| 516 |
472 void RecordBrowserWindowDisplay(const base::TimeTicks& ticks) { | 517 void RecordBrowserWindowDisplay(const base::TimeTicks& ticks) { |
473 static bool is_first_call = true; | 518 static bool is_first_call = true; |
474 if (!is_first_call || ticks.is_null()) | 519 if (!is_first_call || ticks.is_null()) |
475 return; | 520 return; |
476 is_first_call = false; | 521 is_first_call = false; |
477 if (WasNonBrowserUIDisplayed() || g_process_creation_ticks.Get().is_null()) | 522 if (WasNonBrowserUIDisplayed() || g_process_creation_ticks.Get().is_null()) |
478 return; | 523 return; |
479 | 524 |
480 UMA_HISTOGRAM_AND_TRACE_WITH_STARTUP_TEMPERATURE( | 525 UMA_HISTOGRAM_AND_TRACE_WITH_STARTUP_TEMPERATURE( |
481 UMA_HISTOGRAM_LONG_TIMES, "Startup.BrowserWindowDisplay", | 526 UMA_HISTOGRAM_LONG_TIMES, "Startup.BrowserWindowDisplay", |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 | 606 |
562 base::TimeTicks MainEntryPointTicks() { | 607 base::TimeTicks MainEntryPointTicks() { |
563 return g_browser_main_entry_point_ticks.Get(); | 608 return g_browser_main_entry_point_ticks.Get(); |
564 } | 609 } |
565 | 610 |
566 StartupTemperature GetStartupTemperature() { | 611 StartupTemperature GetStartupTemperature() { |
567 return g_startup_temperature; | 612 return g_startup_temperature; |
568 } | 613 } |
569 | 614 |
570 } // namespace startup_metric_utils | 615 } // namespace startup_metric_utils |
OLD | NEW |