blob: 439d2f78620d1b7a06683e7c3364ca00b1da487c [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
Pirama Arumuga Nainar3a254052019-06-14 09:54:23 -070030 // some functions.
Yi Kong69c1ed92019-03-21 14:28:13 -070031 profileUseOtherFlags = []string{
32 "-Wno-backend-plugin",
Yi Kong69c1ed92019-03-21 14:28:13 -070033 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070034
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -080035 globalPgoProfileProjects = []string{
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -080036 "toolchain/pgo-profiles",
Pirama Arumuga Nainar9d544a82020-06-29 09:25:51 -070037 "vendor/google_data/pgo_profile",
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -080038 }
39)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070040
Colin Cross571cccf2019-02-04 11:22:08 -080041var pgoProfileProjectsConfigKey = android.NewOnceKey("PgoProfileProjects")
42
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070043const profileInstrumentFlag = "-fprofile-generate=/data/local/tmp"
Yi Kong61a1b982020-01-30 21:15:12 +080044const profileSamplingFlag = "-gmlt -fdebug-info-for-profiling"
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070045const profileUseInstrumentFormat = "-fprofile-use=%s"
Yi Kongb6ec66a2020-01-31 12:36:12 +080046const profileUseSamplingFormat = "-fprofile-sample-accurate -fprofile-sample-use=%s"
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070047
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -080048func getPgoProfileProjects(config android.DeviceConfig) []string {
49 return config.OnceStringSlice(pgoProfileProjectsConfigKey, func() []string {
50 return append(globalPgoProfileProjects, config.PgoAdditionalProfileDirs()...)
51 })
52}
53
Yi Kong7e53c572018-02-14 18:16:12 +080054func recordMissingProfileFile(ctx BaseModuleContext, missing string) {
Colin Cross571cccf2019-02-04 11:22:08 -080055 getNamedMapForConfig(ctx.Config(), modulesMissingProfileFileKey).Store(missing, true)
Pirama Arumuga Nainar28316d42018-01-29 09:18:45 -080056}
57
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070058type PgoProperties struct {
59 Pgo struct {
Pirama Arumuga Nainar6aeed8b2017-10-16 13:31:40 -070060 Instrumentation *bool
61 Sampling *bool
62 Profile_file *string `android:"arch_variant"`
63 Benchmarks []string
64 Enable_profile_use *bool `android:"arch_variant"`
Pirama Arumuga Nainar690ed552017-12-13 16:48:20 -080065 // Additional compiler flags to use when building this module
66 // for profiling (either instrumentation or sampling).
67 Cflags []string `android:"arch_variant"`
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070068 } `android:"arch_variant"`
69
70 PgoPresent bool `blueprint:"mutated"`
71 ShouldProfileModule bool `blueprint:"mutated"`
Yi Kong7e53c572018-02-14 18:16:12 +080072 PgoCompile bool `blueprint:"mutated"`
Pirama Arumuga Nainar1150fd72020-09-21 22:04:25 -070073 PgoInstrLink 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
Yi Kongceb5b762020-03-20 15:22:27 +080092func (props *PgoProperties) addInstrumentationProfileGatherFlags(ctx ModuleContext, flags Flags) Flags {
Pirama Arumuga Nainar1150fd72020-09-21 22:04:25 -070093 // Add to C flags iff PGO is explicitly enabled for this module.
94 if props.ShouldProfileModule {
95 flags.Local.CFlags = append(flags.Local.CFlags, props.Pgo.Cflags...)
96 flags.Local.CFlags = append(flags.Local.CFlags, profileInstrumentFlag)
97 }
98 flags.Local.LdFlags = append(flags.Local.LdFlags, profileInstrumentFlag)
Yi Kongceb5b762020-03-20 15:22:27 +080099 return flags
100}
101func (props *PgoProperties) addSamplingProfileGatherFlags(ctx ModuleContext, flags Flags) Flags {
102 flags.Local.CFlags = append(flags.Local.CFlags, props.Pgo.Cflags...)
103
104 flags.Local.CFlags = append(flags.Local.CFlags, profileSamplingFlag)
105 flags.Local.LdFlags = append(flags.Local.LdFlags, profileSamplingFlag)
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
Colin Cross4af21ed2019-11-04 09:37:55 -0800174 flags.Local.CFlags = append(flags.Local.CFlags, profileUseFlags...)
175 flags.Local.LdFlags = append(flags.Local.LdFlags, profileUseFlags...)
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700176
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)
Yi Kong92474e52020-01-16 17:04:38 -0800181
182 if props.isSampling() {
183 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,-no-warn-sample-unused=true")
184 }
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700185 }
186 return flags
187}
188
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700189func (props *PgoProperties) isPGO(ctx BaseModuleContext) bool {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700190 isInstrumentation := props.isInstrumentation()
191 isSampling := props.isSampling()
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700192
193 profileKindPresent := isInstrumentation || isSampling
194 filePresent := props.Pgo.Profile_file != nil
195 benchmarksPresent := len(props.Pgo.Benchmarks) > 0
196
197 // If all three properties are absent, PGO is OFF for this module
198 if !profileKindPresent && !filePresent && !benchmarksPresent {
199 return false
200 }
201
Yi Kong84803c52020-07-21 15:38:23 +0800202 // profileKindPresent and filePresent are mandatory properties.
203 if !profileKindPresent || !filePresent {
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700204 var missing []string
205 if !profileKindPresent {
206 missing = append(missing, "profile kind (either \"instrumentation\" or \"sampling\" property)")
207 }
208 if !filePresent {
209 missing = append(missing, "profile_file property")
210 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700211 missingProps := strings.Join(missing, ", ")
212 ctx.ModuleErrorf("PGO specification is missing properties: " + missingProps)
213 }
214
Yi Kong84803c52020-07-21 15:38:23 +0800215 // Benchmark property is mandatory for instrumentation PGO.
216 if isInstrumentation && !benchmarksPresent {
217 ctx.ModuleErrorf("Instrumentation PGO specification is missing benchmark property")
218 }
219
Pirama Arumuga Nainar6fc8d912017-10-05 10:25:00 -0700220 if isSampling && isInstrumentation {
221 ctx.PropertyErrorf("pgo", "Exactly one of \"instrumentation\" and \"sampling\" properties must be set")
222 }
223
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700224 return true
225}
226
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700227func (pgo *pgo) begin(ctx BaseModuleContext) {
228 // TODO Evaluate if we need to support PGO for host modules
229 if ctx.Host() {
230 return
231 }
232
233 // Check if PGO is needed for this module
234 pgo.Properties.PgoPresent = pgo.Properties.isPGO(ctx)
235
236 if !pgo.Properties.PgoPresent {
237 return
238 }
239
240 // This module should be instrumented if ANDROID_PGO_INSTRUMENT is set
Pirama Arumuga Nainare236b5a2018-01-22 19:10:19 -0800241 // and includes 'all', 'ALL' or a benchmark listed for this module.
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700242 //
243 // TODO Validate that each benchmark instruments at least one module
244 pgo.Properties.ShouldProfileModule = false
Colin Cross6510f912017-11-29 00:27:14 -0800245 pgoBenchmarks := ctx.Config().Getenv("ANDROID_PGO_INSTRUMENT")
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700246 pgoBenchmarksMap := make(map[string]bool)
247 for _, b := range strings.Split(pgoBenchmarks, ",") {
248 pgoBenchmarksMap[b] = true
249 }
250
Pirama Arumuga Nainare236b5a2018-01-22 19:10:19 -0800251 if pgoBenchmarksMap["all"] == true || pgoBenchmarksMap["ALL"] == true {
252 pgo.Properties.ShouldProfileModule = true
Pirama Arumuga Nainar1150fd72020-09-21 22:04:25 -0700253 pgo.Properties.PgoInstrLink = pgo.Properties.isInstrumentation()
Pirama Arumuga Nainare236b5a2018-01-22 19:10:19 -0800254 } else {
255 for _, b := range pgo.Properties.Pgo.Benchmarks {
256 if pgoBenchmarksMap[b] == true {
257 pgo.Properties.ShouldProfileModule = true
Pirama Arumuga Nainar1150fd72020-09-21 22:04:25 -0700258 pgo.Properties.PgoInstrLink = pgo.Properties.isInstrumentation()
Pirama Arumuga Nainare236b5a2018-01-22 19:10:19 -0800259 break
260 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700261 }
262 }
Yi Kong7e53c572018-02-14 18:16:12 +0800263
Pirama Arumuga Nainar807d49b2020-02-12 13:57:37 -0800264 // PGO profile use is not feasible for a Clang coverage build because
265 // -fprofile-use and -fprofile-instr-generate are incompatible.
266 if ctx.DeviceConfig().ClangCoverageEnabled() {
267 return
268 }
269
Yi Kongca610d22018-04-24 10:42:02 -0700270 if !ctx.Config().IsEnvTrue("ANDROID_PGO_NO_PROFILE_USE") &&
271 proptools.BoolDefault(pgo.Properties.Pgo.Enable_profile_use, true) {
Yi Kong7e53c572018-02-14 18:16:12 +0800272 if profileFile := pgo.Properties.getPgoProfileFile(ctx); profileFile.Valid() {
273 pgo.Properties.PgoCompile = true
274 }
275 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700276}
277
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700278func (pgo *pgo) deps(ctx BaseModuleContext, deps Deps) Deps {
279 if pgo.Properties.ShouldProfileModule {
280 runtimeLibrary := config.ProfileRuntimeLibrary(ctx.toolchain())
281 deps.LateStaticLibs = append(deps.LateStaticLibs, runtimeLibrary)
282 }
283 return deps
284}
285
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700286func (pgo *pgo) flags(ctx ModuleContext, flags Flags) Flags {
287 if ctx.Host() {
288 return flags
289 }
290
Pirama Arumuga Nainar1150fd72020-09-21 22:04:25 -0700291 // Deduce PgoInstrLink property i.e. whether this module needs to be
292 // linked with profile-generation flags. Here, we're setting it if any
293 // dependency needs PGO instrumentation. It is initially set in
294 // begin() if PGO is directly enabled for this module.
295 if ctx.static() && !ctx.staticBinary() {
296 // For static libraries, check if any whole_static_libs are
297 // linked with profile generation
298 ctx.VisitDirectDeps(func(m android.Module) {
299 if depTag, ok := ctx.OtherModuleDependencyTag(m).(libraryDependencyTag); ok {
300 if depTag.static() && depTag.wholeStatic {
301 if cc, ok := m.(*Module); ok {
302 if cc.pgo.Properties.PgoInstrLink {
303 pgo.Properties.PgoInstrLink = true
304 }
305 }
306 }
307 }
308 })
309 } else {
310 // For executables and shared libraries, check all static dependencies.
311 ctx.VisitDirectDeps(func(m android.Module) {
312 if depTag, ok := ctx.OtherModuleDependencyTag(m).(libraryDependencyTag); ok {
313 if depTag.static() {
314 if cc, ok := m.(*Module); ok {
315 if cc.pgo.Properties.PgoInstrLink {
316 pgo.Properties.PgoInstrLink = true
317 }
318 }
319 }
320 }
321 })
322 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700323
Pirama Arumuga Nainar1150fd72020-09-21 22:04:25 -0700324 props := pgo.Properties
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700325 // Add flags to profile this module based on its profile_kind
Pirama Arumuga Nainar1150fd72020-09-21 22:04:25 -0700326 if (props.ShouldProfileModule && props.isInstrumentation()) || props.PgoInstrLink {
Yi Konga575ff32020-07-22 01:41:58 +0800327 // Instrumentation PGO use and gather flags cannot coexist.
Pirama Arumuga Nainarfe1da752020-09-02 17:44:06 +0000328 return props.addInstrumentationProfileGatherFlags(ctx, flags)
Yi Kongceb5b762020-03-20 15:22:27 +0800329 } else if props.ShouldProfileModule && props.isSampling() {
Pirama Arumuga Nainarfe1da752020-09-02 17:44:06 +0000330 flags = props.addSamplingProfileGatherFlags(ctx, flags)
Yi Kongceb5b762020-03-20 15:22:27 +0800331 } else if ctx.DeviceConfig().SamplingPGO() {
Pirama Arumuga Nainarfe1da752020-09-02 17:44:06 +0000332 flags = props.addSamplingProfileGatherFlags(ctx, flags)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700333 }
334
Colin Cross6510f912017-11-29 00:27:14 -0800335 if !ctx.Config().IsEnvTrue("ANDROID_PGO_NO_PROFILE_USE") {
Pirama Arumuga Nainarfe1da752020-09-02 17:44:06 +0000336 flags = props.addProfileUseFlags(ctx, flags)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700337 }
338
339 return flags
340}