blob: e78549e7b22f45efa62d7746e3c0ff09eb6802a5 [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"
25)
26
27var (
28 // Add flags to ignore warnings that profiles are old or missing for
Pirama Arumuga Nainar3a254052019-06-14 09:54:23 -070029 // some functions.
Yi Kong69c1ed92019-03-21 14:28:13 -070030 profileUseOtherFlags = []string{
31 "-Wno-backend-plugin",
Yi Kong69c1ed92019-03-21 14:28:13 -070032 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070033
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -080034 globalPgoProfileProjects = []string{
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -080035 "toolchain/pgo-profiles",
Pirama Arumuga Nainar9d544a82020-06-29 09:25:51 -070036 "vendor/google_data/pgo_profile",
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -080037 }
38)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070039
Colin Cross571cccf2019-02-04 11:22:08 -080040var pgoProfileProjectsConfigKey = android.NewOnceKey("PgoProfileProjects")
41
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070042const profileInstrumentFlag = "-fprofile-generate=/data/local/tmp"
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070043const profileUseInstrumentFormat = "-fprofile-use=%s"
Yi Kongb6ec66a2020-01-31 12:36:12 +080044const profileUseSamplingFormat = "-fprofile-sample-accurate -fprofile-sample-use=%s"
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070045
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -080046func getPgoProfileProjects(config android.DeviceConfig) []string {
47 return config.OnceStringSlice(pgoProfileProjectsConfigKey, func() []string {
48 return append(globalPgoProfileProjects, config.PgoAdditionalProfileDirs()...)
49 })
50}
51
Yi Kong7e53c572018-02-14 18:16:12 +080052func recordMissingProfileFile(ctx BaseModuleContext, missing string) {
Colin Cross571cccf2019-02-04 11:22:08 -080053 getNamedMapForConfig(ctx.Config(), modulesMissingProfileFileKey).Store(missing, true)
Pirama Arumuga Nainar28316d42018-01-29 09:18:45 -080054}
55
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070056type PgoProperties struct {
57 Pgo struct {
Pirama Arumuga Nainar6aeed8b2017-10-16 13:31:40 -070058 Instrumentation *bool
Pirama Arumuga Nainar19a5aae2021-06-16 14:31:19 -070059 Sampling *bool `android:"arch_variant"`
Pirama Arumuga Nainar6aeed8b2017-10-16 13:31:40 -070060 Profile_file *string `android:"arch_variant"`
61 Benchmarks []string
62 Enable_profile_use *bool `android:"arch_variant"`
Pirama Arumuga Nainar690ed552017-12-13 16:48:20 -080063 // Additional compiler flags to use when building this module
64 // for profiling (either instrumentation or sampling).
65 Cflags []string `android:"arch_variant"`
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070066 } `android:"arch_variant"`
67
68 PgoPresent bool `blueprint:"mutated"`
69 ShouldProfileModule bool `blueprint:"mutated"`
Yi Kong7e53c572018-02-14 18:16:12 +080070 PgoCompile bool `blueprint:"mutated"`
Pirama Arumuga Nainar1150fd72020-09-21 22:04:25 -070071 PgoInstrLink bool `blueprint:"mutated"`
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070072}
73
74type pgo struct {
75 Properties PgoProperties
76}
77
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070078func (props *PgoProperties) isInstrumentation() bool {
79 return props.Pgo.Instrumentation != nil && *props.Pgo.Instrumentation == true
80}
81
82func (props *PgoProperties) isSampling() bool {
83 return props.Pgo.Sampling != nil && *props.Pgo.Sampling == true
84}
85
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070086func (pgo *pgo) props() []interface{} {
87 return []interface{}{&pgo.Properties}
88}
89
Yi Kongceb5b762020-03-20 15:22:27 +080090func (props *PgoProperties) addInstrumentationProfileGatherFlags(ctx ModuleContext, flags Flags) Flags {
Pirama Arumuga Nainar1150fd72020-09-21 22:04:25 -070091 // Add to C flags iff PGO is explicitly enabled for this module.
92 if props.ShouldProfileModule {
93 flags.Local.CFlags = append(flags.Local.CFlags, props.Pgo.Cflags...)
94 flags.Local.CFlags = append(flags.Local.CFlags, profileInstrumentFlag)
95 }
96 flags.Local.LdFlags = append(flags.Local.LdFlags, profileInstrumentFlag)
Yi Kongceb5b762020-03-20 15:22:27 +080097 return flags
98}
99func (props *PgoProperties) addSamplingProfileGatherFlags(ctx ModuleContext, flags Flags) Flags {
100 flags.Local.CFlags = append(flags.Local.CFlags, props.Pgo.Cflags...)
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700101 return flags
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700102}
103
Yi Kong7e53c572018-02-14 18:16:12 +0800104func (props *PgoProperties) getPgoProfileFile(ctx BaseModuleContext) android.OptionalPath {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800105 profileFile := *props.Pgo.Profile_file
Pirama Arumuga Nainar8aed42c2018-03-08 22:56:37 -0800106
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -0800107 // Test if the profile_file is present in any of the PGO profile projects
108 for _, profileProject := range getPgoProfileProjects(ctx.DeviceConfig()) {
Pirama Arumuga Nainar8aed42c2018-03-08 22:56:37 -0800109 // Bug: http://b/74395273 If the profile_file is unavailable,
110 // use a versioned file named
111 // <profile_file>.<arbitrary-version> when available. This
112 // works around an issue where ccache serves stale cache
113 // entries when the profile file has changed.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800114 globPattern := filepath.Join(profileProject, profileFile+".*")
115 versionedProfiles, err := ctx.GlobWithDeps(globPattern, nil)
Pirama Arumuga Nainar8aed42c2018-03-08 22:56:37 -0800116 if err != nil {
117 ctx.ModuleErrorf("glob: %s", err.Error())
118 }
119
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800120 path := android.ExistentPathForSource(ctx, profileProject, profileFile)
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800121 if path.Valid() {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800122 if len(versionedProfiles) != 0 {
123 ctx.PropertyErrorf("pgo.profile_file", "Profile_file has multiple versions: "+filepath.Join(profileProject, profileFile)+", "+strings.Join(versionedProfiles, ", "))
Pirama Arumuga Nainar8aed42c2018-03-08 22:56:37 -0800124 }
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800125 return path
126 }
Pirama Arumuga Nainar8aed42c2018-03-08 22:56:37 -0800127
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800128 if len(versionedProfiles) > 1 {
129 ctx.PropertyErrorf("pgo.profile_file", "Profile_file has multiple versions: "+strings.Join(versionedProfiles, ", "))
130 } else if len(versionedProfiles) == 1 {
131 return android.OptionalPathForPath(android.PathForSource(ctx, versionedProfiles[0]))
Pirama Arumuga Nainar8aed42c2018-03-08 22:56:37 -0800132 }
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800133 }
134
Pirama Arumuga Nainar28316d42018-01-29 09:18:45 -0800135 // Record that this module's profile file is absent
136 missing := *props.Pgo.Profile_file + ":" + ctx.ModuleDir() + "/Android.bp:" + ctx.ModuleName()
137 recordMissingProfileFile(ctx, missing)
138
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800139 return android.OptionalPathForPath(nil)
140}
141
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -0700142func (props *PgoProperties) profileUseFlag(ctx ModuleContext, file string) string {
143 if props.isInstrumentation() {
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700144 return fmt.Sprintf(profileUseInstrumentFormat, file)
145 }
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -0700146 if props.isSampling() {
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700147 return fmt.Sprintf(profileUseSamplingFormat, file)
148 }
149 return ""
150}
151
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -0700152func (props *PgoProperties) profileUseFlags(ctx ModuleContext, file string) []string {
153 flags := []string{props.profileUseFlag(ctx, file)}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700154 flags = append(flags, profileUseOtherFlags...)
155 return flags
156}
157
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700158func (props *PgoProperties) addProfileUseFlags(ctx ModuleContext, flags Flags) Flags {
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800159 // Return if 'pgo' property is not present in this module.
160 if !props.PgoPresent {
161 return flags
162 }
163
Yi Kongca610d22018-04-24 10:42:02 -0700164 if props.PgoCompile {
165 profileFile := props.getPgoProfileFile(ctx)
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800166 profileFilePath := profileFile.Path()
167 profileUseFlags := props.profileUseFlags(ctx, profileFilePath.String())
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700168
Colin Cross4af21ed2019-11-04 09:37:55 -0800169 flags.Local.CFlags = append(flags.Local.CFlags, profileUseFlags...)
170 flags.Local.LdFlags = append(flags.Local.LdFlags, profileUseFlags...)
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700171
172 // Update CFlagsDeps and LdFlagsDeps so the module is rebuilt
173 // if profileFile gets updated
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800174 flags.CFlagsDeps = append(flags.CFlagsDeps, profileFilePath)
175 flags.LdFlagsDeps = append(flags.LdFlagsDeps, profileFilePath)
Yi Kong92474e52020-01-16 17:04:38 -0800176
177 if props.isSampling() {
178 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,-no-warn-sample-unused=true")
179 }
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700180 }
181 return flags
182}
183
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700184func (props *PgoProperties) isPGO(ctx BaseModuleContext) bool {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700185 isInstrumentation := props.isInstrumentation()
186 isSampling := props.isSampling()
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700187
188 profileKindPresent := isInstrumentation || isSampling
189 filePresent := props.Pgo.Profile_file != nil
190 benchmarksPresent := len(props.Pgo.Benchmarks) > 0
191
192 // If all three properties are absent, PGO is OFF for this module
193 if !profileKindPresent && !filePresent && !benchmarksPresent {
194 return false
195 }
196
Yi Kong84803c52020-07-21 15:38:23 +0800197 // profileKindPresent and filePresent are mandatory properties.
198 if !profileKindPresent || !filePresent {
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700199 var missing []string
200 if !profileKindPresent {
201 missing = append(missing, "profile kind (either \"instrumentation\" or \"sampling\" property)")
202 }
203 if !filePresent {
204 missing = append(missing, "profile_file property")
205 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700206 missingProps := strings.Join(missing, ", ")
207 ctx.ModuleErrorf("PGO specification is missing properties: " + missingProps)
208 }
209
Yi Kong84803c52020-07-21 15:38:23 +0800210 // Benchmark property is mandatory for instrumentation PGO.
211 if isInstrumentation && !benchmarksPresent {
212 ctx.ModuleErrorf("Instrumentation PGO specification is missing benchmark property")
213 }
214
Pirama Arumuga Nainar6fc8d912017-10-05 10:25:00 -0700215 if isSampling && isInstrumentation {
216 ctx.PropertyErrorf("pgo", "Exactly one of \"instrumentation\" and \"sampling\" properties must be set")
217 }
218
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700219 return true
220}
221
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700222func (pgo *pgo) begin(ctx BaseModuleContext) {
223 // TODO Evaluate if we need to support PGO for host modules
224 if ctx.Host() {
225 return
226 }
227
228 // Check if PGO is needed for this module
229 pgo.Properties.PgoPresent = pgo.Properties.isPGO(ctx)
230
231 if !pgo.Properties.PgoPresent {
232 return
233 }
234
235 // This module should be instrumented if ANDROID_PGO_INSTRUMENT is set
Pirama Arumuga Nainare236b5a2018-01-22 19:10:19 -0800236 // and includes 'all', 'ALL' or a benchmark listed for this module.
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700237 //
238 // TODO Validate that each benchmark instruments at least one module
239 pgo.Properties.ShouldProfileModule = false
Colin Cross6510f912017-11-29 00:27:14 -0800240 pgoBenchmarks := ctx.Config().Getenv("ANDROID_PGO_INSTRUMENT")
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700241 pgoBenchmarksMap := make(map[string]bool)
242 for _, b := range strings.Split(pgoBenchmarks, ",") {
243 pgoBenchmarksMap[b] = true
244 }
245
Pirama Arumuga Nainare236b5a2018-01-22 19:10:19 -0800246 if pgoBenchmarksMap["all"] == true || pgoBenchmarksMap["ALL"] == true {
247 pgo.Properties.ShouldProfileModule = true
Pirama Arumuga Nainar1150fd72020-09-21 22:04:25 -0700248 pgo.Properties.PgoInstrLink = pgo.Properties.isInstrumentation()
Pirama Arumuga Nainare236b5a2018-01-22 19:10:19 -0800249 } else {
250 for _, b := range pgo.Properties.Pgo.Benchmarks {
251 if pgoBenchmarksMap[b] == 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 break
255 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700256 }
257 }
Yi Kong7e53c572018-02-14 18:16:12 +0800258
Pirama Arumuga Nainar807d49b2020-02-12 13:57:37 -0800259 // PGO profile use is not feasible for a Clang coverage build because
260 // -fprofile-use and -fprofile-instr-generate are incompatible.
261 if ctx.DeviceConfig().ClangCoverageEnabled() {
262 return
263 }
264
Yi Kongca610d22018-04-24 10:42:02 -0700265 if !ctx.Config().IsEnvTrue("ANDROID_PGO_NO_PROFILE_USE") &&
266 proptools.BoolDefault(pgo.Properties.Pgo.Enable_profile_use, true) {
Yi Kong7e53c572018-02-14 18:16:12 +0800267 if profileFile := pgo.Properties.getPgoProfileFile(ctx); profileFile.Valid() {
268 pgo.Properties.PgoCompile = true
269 }
270 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700271}
272
273func (pgo *pgo) flags(ctx ModuleContext, flags Flags) Flags {
274 if ctx.Host() {
275 return flags
276 }
277
Pirama Arumuga Nainar1150fd72020-09-21 22:04:25 -0700278 // Deduce PgoInstrLink property i.e. whether this module needs to be
279 // linked with profile-generation flags. Here, we're setting it if any
280 // dependency needs PGO instrumentation. It is initially set in
281 // begin() if PGO is directly enabled for this module.
282 if ctx.static() && !ctx.staticBinary() {
283 // For static libraries, check if any whole_static_libs are
284 // linked with profile generation
285 ctx.VisitDirectDeps(func(m android.Module) {
286 if depTag, ok := ctx.OtherModuleDependencyTag(m).(libraryDependencyTag); ok {
287 if depTag.static() && depTag.wholeStatic {
288 if cc, ok := m.(*Module); ok {
289 if cc.pgo.Properties.PgoInstrLink {
290 pgo.Properties.PgoInstrLink = true
291 }
292 }
293 }
294 }
295 })
296 } else {
297 // For executables and shared libraries, check all static dependencies.
298 ctx.VisitDirectDeps(func(m android.Module) {
299 if depTag, ok := ctx.OtherModuleDependencyTag(m).(libraryDependencyTag); ok {
300 if depTag.static() {
301 if cc, ok := m.(*Module); ok {
302 if cc.pgo.Properties.PgoInstrLink {
303 pgo.Properties.PgoInstrLink = true
304 }
305 }
306 }
307 }
308 })
309 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700310
Pirama Arumuga Nainar1150fd72020-09-21 22:04:25 -0700311 props := pgo.Properties
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700312 // Add flags to profile this module based on its profile_kind
Pirama Arumuga Nainar1150fd72020-09-21 22:04:25 -0700313 if (props.ShouldProfileModule && props.isInstrumentation()) || props.PgoInstrLink {
Yi Konga575ff32020-07-22 01:41:58 +0800314 // Instrumentation PGO use and gather flags cannot coexist.
Pirama Arumuga Nainarfe1da752020-09-02 17:44:06 +0000315 return props.addInstrumentationProfileGatherFlags(ctx, flags)
Yi Kongceb5b762020-03-20 15:22:27 +0800316 } else if props.ShouldProfileModule && props.isSampling() {
Pirama Arumuga Nainarfe1da752020-09-02 17:44:06 +0000317 flags = props.addSamplingProfileGatherFlags(ctx, flags)
Yi Kongceb5b762020-03-20 15:22:27 +0800318 } else if ctx.DeviceConfig().SamplingPGO() {
Pirama Arumuga Nainarfe1da752020-09-02 17:44:06 +0000319 flags = props.addSamplingProfileGatherFlags(ctx, flags)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700320 }
321
Colin Cross6510f912017-11-29 00:27:14 -0800322 if !ctx.Config().IsEnvTrue("ANDROID_PGO_NO_PROFILE_USE") {
Pirama Arumuga Nainarfe1da752020-09-02 17:44:06 +0000323 flags = props.addProfileUseFlags(ctx, flags)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700324 }
325
326 return flags
327}