blob: 00794b2253d0a828a71fdeba3f29dc38a988246f [file] [log] [blame]
Sajjad Mirza94c68bc2019-08-15 01:02:351// Copyright 2019 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 "content/common/coverage_utils.h"
6
7#include <memory>
8
9#include "base/base_paths.h"
10#include "base/environment.h"
11#include "base/files/file.h"
12#include "base/files/file_path.h"
13#include "base/files/file_util.h"
14#include "base/logging.h"
15#include "base/path_service.h"
16#include "base/rand_util.h"
17#include "base/strings/strcat.h"
18#include "base/strings/string_number_conversions.h"
19#include "base/threading/thread_restrictions.h"
20
21namespace content {
22
23base::File OpenCoverageFile() {
24 base::ScopedAllowBlockingForTesting allows_blocking;
25 std::unique_ptr<base::Environment> env(base::Environment::Create());
26 std::string prof_template;
27 base::FilePath path;
28 if (env->GetVar("LLVM_PROFILE_FILE", &prof_template)) {
29 path = base::FilePath(prof_template).DirName();
30 base::CreateDirectory(path);
31 } else {
32 base::PathService::Get(base::DIR_CURRENT, &path);
33 }
34
35 // sajjadm@ and liaoyuke@ experimentally determined that a size 4 pool works
36 // well for the coverage builder.
37 int pool_index = base::RandInt(0, 3);
38 std::string filename = base::StrCat(
39 {"child_pool-", base::NumberToString(pool_index), ".profraw"});
40 path = path.Append(filename);
41 uint32_t flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ |
42 base::File::FLAG_WRITE;
43
44 base::File file(path, flags);
45 if (!file.IsValid()) {
46 LOG(ERROR) << "Opening file: " << path << " failed with "
47 << file.error_details();
48 }
49
50 return file;
51}
52
53} // namespace content