1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tradefed.testtype.coverage;
18 
19 import com.android.tradefed.config.Option;
20 
21 import com.google.common.collect.ImmutableList;
22 
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.List;
26 
27 /** Tradefed object to hold coverage options. */
28 public final class CoverageOptions {
29 
30     @Option(
31         name = "coverage",
32         description =
33                 "Collect code coverage for this test run. Note that the build under test must be a "
34                         + "coverage build or else this will fail."
35     )
36     private boolean mCoverage = false;
37 
38     @Option(
39         name = "coverage-toolchain",
40         description =
41                 "The coverage toolchains that were used to compile the coverage build to "
42                         + "collect coverage from."
43     )
44     private List<Toolchain> mToolchains = new ArrayList<>();
45 
46     public enum Toolchain {
47         CLANG,
48         GCOV,
49         JACOCO,
50         GCOV_KERNEL;
51     }
52 
53     @Option(
54         name = "coverage-flush",
55         description = "Forces coverage data to be flushed at the end of the test."
56     )
57     private boolean mCoverageFlush = false;
58 
59     @Option(
60         name = "coverage-processes",
61         description = "Name of processes to collect coverage data from."
62     )
63     private List<String> mCoverageProcesses = new ArrayList<>();
64 
65     @Option(name = "merge-coverage", description = "Merge coverage measurements before logging.")
66     private boolean mMergeCoverage = false;
67 
68     @Option(
69             name = "reset-coverage-before-test",
70             description = "Reset coverage before running each test.")
71     private boolean mResetCoverageBeforeTest = true;
72 
73     @Option(name = "llvm-profdata-path", description = "Path to llvm-profdata tool.")
74     private File mLlvmProfdataPath = null;
75 
76     @Option(
77             name = "profraw-filter",
78             description =
79                     "The regex of profraw files to merge for coverage measurements.  E.g."
80                             + " \"foo.*\\.profraw\".  Default: \".*\\.profraw\"")
81     private String mProfrawFilter = ".*\\.profraw";
82 
83     @Option(
84             name = "pull-timeout",
85             isTimeVal = true,
86             description = "Timeout in milliseconds to pull coverage metrics from the device.")
87     private long mPullTimeout = 20 * 60 * 1000;
88 
89     @Option(name = "jacocoagent-path", description = "Path to jacocoagent.jar.")
90     private File mJaCoCoAgentPath = null;
91 
92     @Option(
93             name = "device-coverage-path",
94             description = "Path to coverage measurements on devices.")
95     private List<String> mDeviceCoveragePaths =
96             new ArrayList<>(ImmutableList.of("/data/misc/trace", "/data/local/tmp"));
97 
98     /**
99      * Returns whether coverage measurements should be collected from this run.
100      *
101      * @return whether to collect coverage measurements
102      */
isCoverageEnabled()103     public boolean isCoverageEnabled() {
104         return mCoverage;
105     }
106 
107     /**
108      * Returns the coverage toolchains to collect coverage from.
109      *
110      * @return the toolchains to collect coverage from
111      */
getCoverageToolchains()112     public List<Toolchain> getCoverageToolchains() {
113         return ImmutableList.copyOf(mToolchains);
114     }
115 
116     /**
117      * Returns whether coverage measurements should be flushed from running processes after the test
118      * has completed.
119      *
120      * @return whether to flush processes for coverage measurements after the test
121      */
isCoverageFlushEnabled()122     public boolean isCoverageFlushEnabled() {
123         return mCoverageFlush;
124     }
125 
126     /**
127      * Returns the name of processes to flush coverage from after the test has completed.
128      *
129      * @return a {@link List} of process names to flush coverage from after the test
130      */
getCoverageProcesses()131     public List<String> getCoverageProcesses() {
132         return ImmutableList.copyOf(mCoverageProcesses);
133     }
134 
135     /** Returns whether to merge coverage measurements together before logging. */
shouldMergeCoverage()136     public boolean shouldMergeCoverage() {
137         return mMergeCoverage;
138     }
139 
140     /**
141      * Returns whether coverage measurements should be reset before each test.
142      *
143      * <p>Enabling this allows the coverage to be more targeted to the test.
144      *
145      * @return whether to reset coverage before the test
146      */
shouldResetCoverageBeforeTest()147     public boolean shouldResetCoverageBeforeTest() {
148         return mResetCoverageBeforeTest;
149     }
150 
151     /**
152      * Returns the directory containing the llvm-profdata tool.
153      *
154      * @return a {@link File} containing the llvm-profdata tool and its dependencies
155      */
getLlvmProfdataPath()156     public File getLlvmProfdataPath() {
157         return mLlvmProfdataPath;
158     }
159 
160     /**
161      * Returns the prefix of profdata filenames used for coverage measurements.
162      *
163      * @return a {@link String} containing the prefix of profdata filenames
164      */
getProfrawFilter()165     public String getProfrawFilter() {
166         return mProfrawFilter;
167     }
168 
169     /**
170      * Returns the timeout in milliseconds for pulling coverage metrics from the device.
171      *
172      * @return a {@link long} as timeout in milliseconds.
173      */
getPullTimeout()174     public long getPullTimeout() {
175         return mPullTimeout;
176     }
177 
178     /**
179      * Returns jacocoagent.jar.
180      *
181      * @return a {@link File} pointing to jacocoagent.jar.
182      */
getJaCoCoAgentPath()183     public File getJaCoCoAgentPath() {
184         return mJaCoCoAgentPath;
185     }
186 
187     /**
188      * Returns the locations on the device where coverage measurements are stored.
189      *
190      * @return a {link List} containing the device coverage paths
191      */
getDeviceCoveragePaths()192     public List<String> getDeviceCoveragePaths() {
193         return ImmutableList.copyOf(mDeviceCoveragePaths);
194     }
195 }
196