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

Side by Side Diff: base/metrics/persistent_metrics_file_util.cc

Issue 2308763002: Integrate Crashpad UMA (Closed)
Patch Set: pass through user data dir for metrics Created 4 years, 3 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/metrics/persistent_metrics_file_util.h"
6
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/metrics/persistent_histogram_allocator.h"
10 #include "base/metrics/persistent_memory_allocator.h"
11
12 namespace base {
13
14 void InitializeGlobalPersistentMetricsStorage(const base::FilePath& dir,
15 const char* metrics_name,
16 size_t size) {
17 // Metrics maps one file that we're going to use during this run (the
18 // "-active" one), and provides the previous one to the metrics system for for
19 // association with the previous run. On startup, we move the current -active
20 // to the base name, and then start this session on the -active one. Normally,
21 // when the browser starts it will read and delete the base name one, but in
22 // the case that something goes wrong, we will overwrite/delete any old one.
23 base::FilePath metrics_file =
24 dir.AppendASCII(metrics_name)
25 .AddExtension(PersistentMemoryAllocator::kFileExtension);
26 base::FilePath active_file =
27 dir.AppendASCII(metrics_name + std::string("-active"))
28 .AddExtension(PersistentMemoryAllocator::kFileExtension);
29
30 if (!base::ReplaceFile(active_file, metrics_file, nullptr))
31 base::DeleteFile(metrics_file, /*recursive=*/false);
32
33 base::GlobalHistogramAllocator::CreateWithFile(active_file, size, 0,
34 metrics_name);
35
36 // Get the allocator that was just created and report result. Exit if the
37 // allocator could not be created.
38 base::GlobalHistogramAllocator* allocator =
39 base::GlobalHistogramAllocator::Get();
40 if (!allocator)
41 return;
42
43 // Create tracking histograms for the allocator and record storage file.
44 allocator->CreateTrackingHistograms(metrics_name);
bcwhite 2016/09/14 13:16:12 This is the only line cannot be moved into CreateW
scottmg 2016/09/14 20:37:01 Done.
45 allocator->SetPersistentLocation(active_file);
bcwhite 2016/09/14 13:16:12 I think this could be part of CreateWithFile().
scottmg 2016/09/14 20:37:01 Done. I removed it from chrome/browser/chrome_brow
46 }
47
48 void CleanUpGlobalPersistentHistogramStorage() {
49 base::GlobalHistogramAllocator* allocator =
50 base::GlobalHistogramAllocator::Get();
51 if (!allocator)
52 return;
53
54 const base::FilePath& path = allocator->GetPersistentLocation();
55 if (path.empty())
56 return;
57
58 // Open (with delete) and then immediately close the file by going out of
59 // scope. This is the only cross-platform safe way to delete a file that may
60 // be open elsewhere. Open handles will continue to operate normally but
61 // new opens will not be possible.
62 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ |
63 base::File::FLAG_DELETE_ON_CLOSE);
64 }
65
66 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698