OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/browser_watcher/stability_debugging.h" | 5 #include "components/browser_watcher/stability_debugging.h" |
6 | 6 |
7 #include <string> | |
8 | |
9 #include "base/debug/activity_tracker.h" | 7 #include "base/debug/activity_tracker.h" |
10 #include "base/feature_list.h" | |
11 #include "base/files/file.h" | |
12 #include "base/metrics/persistent_memory_allocator.h" | |
13 #include "base/path_service.h" | |
14 #include "base/process/process.h" | |
15 #include "base/strings/stringprintf.h" | |
16 #include "base/time/time.h" | |
17 #include "components/browser_watcher/features.h" | |
18 | 8 |
19 namespace browser_watcher { | 9 namespace browser_watcher { |
20 | 10 |
21 namespace { | |
22 | |
23 #if defined(OS_WIN) | |
24 bool GetCreationTime(const base::Process& process, base::Time* time) { | |
25 DCHECK(time); | |
26 | |
27 FILETIME creation_time = {}; | |
28 FILETIME ignore1 = {}; | |
29 FILETIME ignore2 = {}; | |
30 FILETIME ignore3 = {}; | |
31 if (!::GetProcessTimes(process.Handle(), &creation_time, &ignore1, &ignore2, | |
32 &ignore3)) { | |
33 return false; | |
34 } | |
35 *time = base::Time::FromFileTime(creation_time); | |
36 return true; | |
37 } | |
38 #endif // defined(OS_WIN) | |
39 | |
40 } // namespace | |
41 | |
42 #if defined(OS_WIN) | |
43 | |
44 base::FilePath GetStabilityDir(const base::FilePath& user_data_dir) { | |
45 return user_data_dir.AppendASCII("Stability"); | |
46 } | |
47 | |
48 bool GetStabilityFileForProcess(const base::Process& process, | |
49 const base::FilePath& user_data_dir, | |
50 base::FilePath* file_path) { | |
51 DCHECK(file_path); | |
52 base::FilePath stability_dir = GetStabilityDir(user_data_dir); | |
53 | |
54 // Build the name using the pid and creation time. On windows, this is unique | |
55 // even after the process exits. | |
56 base::Time creation_time; | |
57 if (!GetCreationTime(process, &creation_time)) | |
58 return false; | |
59 | |
60 std::string file_name = | |
61 base::StringPrintf("%u-%llu", process.Pid(), creation_time.ToJavaTime()); | |
62 *file_path = stability_dir.AppendASCII(file_name).AddExtension( | |
63 base::PersistentMemoryAllocator::kFileExtension); | |
64 return true; | |
65 } | |
66 | |
67 base::FilePath::StringType GetStabilityFilePattern() { | |
68 return base::FilePath::StringType(FILE_PATH_LITERAL("*-*")) + | |
69 base::PersistentMemoryAllocator::kFileExtension; | |
70 } | |
71 | |
72 void MarkStabilityFileForDeletion(const base::FilePath& user_data_dir) { | |
73 if (!base::FeatureList::IsEnabled( | |
74 browser_watcher::kStabilityDebuggingFeature)) { | |
75 return; | |
76 } | |
77 | |
78 base::FilePath stability_file; | |
79 if (!GetStabilityFileForProcess(base::Process::Current(), user_data_dir, | |
80 &stability_file)) { | |
81 // TODO(manzagop): add a metric for this. | |
82 return; | |
83 } | |
84 | |
85 // Open (with delete) and then immediately close the file by going out of | |
86 // scope. This should cause the stability debugging file to be deleted prior | |
87 // to the next execution. | |
88 base::File file(stability_file, base::File::FLAG_OPEN | | |
89 base::File::FLAG_READ | | |
90 base::File::FLAG_DELETE_ON_CLOSE); | |
91 } | |
92 #endif // defined(OS_WIN) | |
93 | |
94 void SetStabilityDataInt(base::StringPiece name, int64_t value) { | 11 void SetStabilityDataInt(base::StringPiece name, int64_t value) { |
95 base::debug::GlobalActivityTracker* global_tracker = | 12 base::debug::GlobalActivityTracker* global_tracker = |
96 base::debug::GlobalActivityTracker::Get(); | 13 base::debug::GlobalActivityTracker::Get(); |
97 if (!global_tracker) | 14 if (!global_tracker) |
98 return; // Activity tracking isn't enabled. | 15 return; // Activity tracking isn't enabled. |
99 | 16 |
100 global_tracker->process_data().SetInt(name, value); | 17 global_tracker->process_data().SetInt(name, value); |
101 } | 18 } |
102 | 19 |
103 } // namespace browser_watcher | 20 } // namespace browser_watcher |
OLD | NEW |