blob: 7334ea2663737645d5ea643386d543ddb56bc621 [file] [log] [blame]
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
18 "fmt"
Pirama Arumuga Nainar8aed42c2018-03-08 22:56:37 -080019 "path/filepath"
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070020 "strings"
21
Yi Kongca610d22018-04-24 10:42:02 -070022 "github.com/google/blueprint/proptools"
23
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070024 "android/soong/android"
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070025 "android/soong/cc/config"
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070026)
27
28var (
29 // Add flags to ignore warnings that profiles are old or missing for
Yi Kong69c1ed92019-03-21 14:28:13 -070030 // some functions, and turn on the experimental new pass manager.
31 profileUseOtherFlags = []string{
32 "-Wno-backend-plugin",
33 "-fexperimental-new-pass-manager",
34 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070035
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -080036 globalPgoProfileProjects = []string{
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -080037 "toolchain/pgo-profiles",
38 "vendor/google_data/pgo-profiles",
39 }
40)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070041
Colin Cross571cccf2019-02-04 11:22:08 -080042var pgoProfileProjectsConfigKey = android.NewOnceKey("PgoProfileProjects")
43
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070044const profileInstrumentFlag = "-fprofile-generate=/data/local/tmp"
45const profileSamplingFlag = "-gline-tables-only"
46const profileUseInstrumentFormat = "-fprofile-use=%s"
47const profileUseSamplingFormat = "-fprofile-sample-use=%s"
48
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -080049func getPgoProfileProjects(config android.DeviceConfig) []string {
50 return config.OnceStringSlice(pgoProfileProjectsConfigKey, func() []string {
51 return append(globalPgoProfileProjects, config.PgoAdditionalProfileDirs()...)
52 })
53}
54
Yi Kong7e53c572018-02-14 18:16:12 +080055func recordMissingProfileFile(ctx BaseModuleContext, missing string) {
Colin Cross571cccf2019-02-04 11:22:08 -080056 getNamedMapForConfig(ctx.Config(), modulesMissingProfileFileKey).Store(missing, true)
Pirama Arumuga Nainar28316d42018-01-29 09:18:45 -080057}
58
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070059type PgoProperties struct {
60 Pgo struct {
Pirama Arumuga Nainar6aeed8b2017-10-16 13:31:40 -070061 Instrumentation *bool
62 Sampling *bool
63 Profile_file *string `android:"arch_variant"`
64 Benchmarks []string
65 Enable_profile_use *bool `android:"arch_variant"`
Pirama Arumuga Nainar690ed552017-12-13 16:48:20 -080066 // Additional compiler flags to use when building this module
67 // for profiling (either instrumentation or sampling).
68 Cflags []string `android:"arch_variant"`
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070069 } `android:"arch_variant"`
70
71 PgoPresent bool `blueprint:"mutated"`
72 ShouldProfileModule bool `blueprint:"mutated"`
Yi Kong7e53c572018-02-14 18:16:12 +080073 PgoCompile bool `blueprint:"mutated"`
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070074}
75
76type pgo struct {
77 Properties PgoProperties
78}
79
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070080func (props *PgoProperties) isInstrumentation() bool {
81 return props.Pgo.Instrumentation != nil && *props.Pgo.Instrumentation == true
82}
83
84func (props *PgoProperties) isSampling() bool {
85 return props.Pgo.Sampling != nil && *props.Pgo.Sampling == true
86}
87
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070088func (pgo *pgo) props() []interface{} {
89 return []interface{}{&pgo.Properties}
90}
91
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -070092func (props *PgoProperties) addProfileGatherFlags(ctx ModuleContext, flags Flags) Flags {
Pirama Arumuga Nainar690ed552017-12-13 16:48:20 -080093 flags.CFlags = append(flags.CFlags, props.Pgo.Cflags...)
94
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -070095 if props.isInstrumentation() {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070096 flags.CFlags = append(flags.CFlags, profileInstrumentFlag)
97 // The profile runtime is added below in deps(). Add the below
98 // flag, which is the only other link-time action performed by
99 // the Clang driver during link.
100 flags.LdFlags = append(flags.LdFlags, "-u__llvm_profile_runtime")
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700101 }
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -0700102 if props.isSampling() {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700103 flags.CFlags = append(flags.CFlags, profileSamplingFlag)
104 flags.LdFlags = append(flags.LdFlags, profileSamplingFlag)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700105 }
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700106 return flags
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700107}
108
Yi Kong7e53c572018-02-14 18:16:12 +0800109func (props *PgoProperties) getPgoProfileFile(ctx BaseModuleContext) android.OptionalPath {
Pirama Arumuga Nainar8aed42c2018-03-08 22:56:37 -0800110 profile_file := *props.Pgo.Profile_file
111
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -0800112 // Test if the profile_file is present in any of the PGO profile projects
113 for _, profileProject := range getPgoProfileProjects(ctx.DeviceConfig()) {
Pirama Arumuga Nainar8aed42c2018-03-08 22:56:37 -0800114 // Bug: http://b/74395273 If the profile_file is unavailable,
115 // use a versioned file named
116 // <profile_file>.<arbitrary-version> when available. This
117 // works around an issue where ccache serves stale cache
118 // entries when the profile file has changed.
119 globPattern := filepath.Join(profileProject, profile_file+".*")
120 versioned_profiles, err := ctx.GlobWithDeps(globPattern, nil)
121 if err != nil {
122 ctx.ModuleErrorf("glob: %s", err.Error())
123 }
124
125 path := android.ExistentPathForSource(ctx, profileProject, profile_file)
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800126 if path.Valid() {
Pirama Arumuga Nainar8aed42c2018-03-08 22:56:37 -0800127 if len(versioned_profiles) != 0 {
128 ctx.PropertyErrorf("pgo.profile_file", "Profile_file has multiple versions: "+filepath.Join(profileProject, profile_file)+", "+strings.Join(versioned_profiles, ", "))
129 }
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800130 return path
131 }
Pirama Arumuga Nainar8aed42c2018-03-08 22:56:37 -0800132
133 if len(versioned_profiles) > 1 {
134 ctx.PropertyErrorf("pgo.profile_file", "Profile_file has multiple versions: "+strings.Join(versioned_profiles, ", "))
135 } else if len(versioned_profiles) == 1 {
136 return android.OptionalPathForPath(android.PathForSource(ctx, versioned_profiles[0]))
137 }
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800138 }
139
Pirama Arumuga Nainar28316d42018-01-29 09:18:45 -0800140 // Record that this module's profile file is absent
141 missing := *props.Pgo.Profile_file + ":" + ctx.ModuleDir() + "/Android.bp:" + ctx.ModuleName()
142 recordMissingProfileFile(ctx, missing)
143
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800144 return android.OptionalPathForPath(nil)
145}
146
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -0700147func (props *PgoProperties) profileUseFlag(ctx ModuleContext, file string) string {
148 if props.isInstrumentation() {
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700149 return fmt.Sprintf(profileUseInstrumentFormat, file)
150 }
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -0700151 if props.isSampling() {
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700152 return fmt.Sprintf(profileUseSamplingFormat, file)
153 }
154 return ""
155}
156
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -0700157func (props *PgoProperties) profileUseFlags(ctx ModuleContext, file string) []string {
158 flags := []string{props.profileUseFlag(ctx, file)}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700159 flags = append(flags, profileUseOtherFlags...)
160 return flags
161}
162
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700163func (props *PgoProperties) addProfileUseFlags(ctx ModuleContext, flags Flags) Flags {
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800164 // Return if 'pgo' property is not present in this module.
165 if !props.PgoPresent {
166 return flags
167 }
168
Yi Kongca610d22018-04-24 10:42:02 -0700169 if props.PgoCompile {
170 profileFile := props.getPgoProfileFile(ctx)
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800171 profileFilePath := profileFile.Path()
172 profileUseFlags := props.profileUseFlags(ctx, profileFilePath.String())
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700173
174 flags.CFlags = append(flags.CFlags, profileUseFlags...)
175 flags.LdFlags = append(flags.LdFlags, profileUseFlags...)
176
177 // Update CFlagsDeps and LdFlagsDeps so the module is rebuilt
178 // if profileFile gets updated
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800179 flags.CFlagsDeps = append(flags.CFlagsDeps, profileFilePath)
180 flags.LdFlagsDeps = append(flags.LdFlagsDeps, profileFilePath)
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700181 }
182 return flags
183}
184
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700185func (props *PgoProperties) isPGO(ctx BaseModuleContext) bool {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700186 isInstrumentation := props.isInstrumentation()
187 isSampling := props.isSampling()
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700188
189 profileKindPresent := isInstrumentation || isSampling
190 filePresent := props.Pgo.Profile_file != nil
191 benchmarksPresent := len(props.Pgo.Benchmarks) > 0
192
193 // If all three properties are absent, PGO is OFF for this module
194 if !profileKindPresent && !filePresent && !benchmarksPresent {
195 return false
196 }
197
198 // If at least one property exists, validate that all properties exist
199 if !profileKindPresent || !filePresent || !benchmarksPresent {
200 var missing []string
201 if !profileKindPresent {
202 missing = append(missing, "profile kind (either \"instrumentation\" or \"sampling\" property)")
203 }
204 if !filePresent {
205 missing = append(missing, "profile_file property")
206 }
207 if !benchmarksPresent {
208 missing = append(missing, "non-empty benchmarks property")
209 }
210 missingProps := strings.Join(missing, ", ")
211 ctx.ModuleErrorf("PGO specification is missing properties: " + missingProps)
212 }
213
214 // Sampling not supported yet
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700215 if isSampling {
216 ctx.PropertyErrorf("pgo.sampling", "\"sampling\" is not supported yet)")
217 }
218
Pirama Arumuga Nainar6fc8d912017-10-05 10:25:00 -0700219 if isSampling && isInstrumentation {
220 ctx.PropertyErrorf("pgo", "Exactly one of \"instrumentation\" and \"sampling\" properties must be set")
221 }
222
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700223 return true
224}
225
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700226func (pgo *pgo) begin(ctx BaseModuleContext) {
227 // TODO Evaluate if we need to support PGO for host modules
228 if ctx.Host() {
229 return
230 }
231
232 // Check if PGO is needed for this module
233 pgo.Properties.PgoPresent = pgo.Properties.isPGO(ctx)
234
235 if !pgo.Properties.PgoPresent {
236 return
237 }
238
239 // This module should be instrumented if ANDROID_PGO_INSTRUMENT is set
Pirama Arumuga Nainare236b5a2018-01-22 19:10:19 -0800240 // and includes 'all', 'ALL' or a benchmark listed for this module.
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700241 //
242 // TODO Validate that each benchmark instruments at least one module
243 pgo.Properties.ShouldProfileModule = false
Colin Cross6510f912017-11-29 00:27:14 -0800244 pgoBenchmarks := ctx.Config().Getenv("ANDROID_PGO_INSTRUMENT")
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700245 pgoBenchmarksMap := make(map[string]bool)
246 for _, b := range strings.Split(pgoBenchmarks, ",") {
247 pgoBenchmarksMap[b] = true
248 }
249
Pirama Arumuga Nainare236b5a2018-01-22 19:10:19 -0800250 if pgoBenchmarksMap["all"] == true || pgoBenchmarksMap["ALL"] == true {
251 pgo.Properties.ShouldProfileModule = true
252 } else {
253 for _, b := range pgo.Properties.Pgo.Benchmarks {
254 if pgoBenchmarksMap[b] == true {
255 pgo.Properties.ShouldProfileModule = true
256 break
257 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700258 }
259 }
Yi Kong7e53c572018-02-14 18:16:12 +0800260
Yi Kongca610d22018-04-24 10:42:02 -0700261 if !ctx.Config().IsEnvTrue("ANDROID_PGO_NO_PROFILE_USE") &&
262 proptools.BoolDefault(pgo.Properties.Pgo.Enable_profile_use, true) {
Yi Kong7e53c572018-02-14 18:16:12 +0800263 if profileFile := pgo.Properties.getPgoProfileFile(ctx); profileFile.Valid() {
264 pgo.Properties.PgoCompile = true
265 }
266 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700267}
268
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700269func (pgo *pgo) deps(ctx BaseModuleContext, deps Deps) Deps {
270 if pgo.Properties.ShouldProfileModule {
271 runtimeLibrary := config.ProfileRuntimeLibrary(ctx.toolchain())
272 deps.LateStaticLibs = append(deps.LateStaticLibs, runtimeLibrary)
273 }
274 return deps
275}
276
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700277func (pgo *pgo) flags(ctx ModuleContext, flags Flags) Flags {
278 if ctx.Host() {
279 return flags
280 }
281
282 props := pgo.Properties
283
284 // Add flags to profile this module based on its profile_kind
285 if props.ShouldProfileModule {
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -0700286 return props.addProfileGatherFlags(ctx, flags)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700287 }
288
Colin Cross6510f912017-11-29 00:27:14 -0800289 if !ctx.Config().IsEnvTrue("ANDROID_PGO_NO_PROFILE_USE") {
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700290 return props.addProfileUseFlags(ctx, flags)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700291 }
292
293 return flags
294}