blob: a341ab9f938be43fb7d9a5f0a91c5e29a6cb67c5 [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
30 // some functions
Pirama Arumuga Nainarf4c0baf2017-09-28 14:35:15 -070031 profileUseOtherFlags = []string{"-Wno-backend-plugin"}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070032
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -080033 globalPgoProfileProjects = []string{
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -080034 "toolchain/pgo-profiles",
35 "vendor/google_data/pgo-profiles",
36 }
37)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070038
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -080039const pgoProfileProjectsConfigKey = "PgoProfileProjects"
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070040const profileInstrumentFlag = "-fprofile-generate=/data/local/tmp"
41const profileSamplingFlag = "-gline-tables-only"
42const profileUseInstrumentFormat = "-fprofile-use=%s"
43const profileUseSamplingFormat = "-fprofile-sample-use=%s"
44
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -080045func getPgoProfileProjects(config android.DeviceConfig) []string {
46 return config.OnceStringSlice(pgoProfileProjectsConfigKey, func() []string {
47 return append(globalPgoProfileProjects, config.PgoAdditionalProfileDirs()...)
48 })
49}
50
Yi Kong7e53c572018-02-14 18:16:12 +080051func recordMissingProfileFile(ctx BaseModuleContext, missing string) {
Pirama Arumuga Nainar28316d42018-01-29 09:18:45 -080052 getNamedMapForConfig(ctx.Config(), modulesMissingProfileFile).Store(missing, true)
53}
54
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070055type PgoProperties struct {
56 Pgo struct {
Pirama Arumuga Nainar6aeed8b2017-10-16 13:31:40 -070057 Instrumentation *bool
58 Sampling *bool
59 Profile_file *string `android:"arch_variant"`
60 Benchmarks []string
61 Enable_profile_use *bool `android:"arch_variant"`
Pirama Arumuga Nainar690ed552017-12-13 16:48:20 -080062 // Additional compiler flags to use when building this module
63 // for profiling (either instrumentation or sampling).
64 Cflags []string `android:"arch_variant"`
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070065 } `android:"arch_variant"`
66
67 PgoPresent bool `blueprint:"mutated"`
68 ShouldProfileModule bool `blueprint:"mutated"`
Yi Kong7e53c572018-02-14 18:16:12 +080069 PgoCompile bool `blueprint:"mutated"`
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070070}
71
72type pgo struct {
73 Properties PgoProperties
74}
75
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070076func (props *PgoProperties) isInstrumentation() bool {
77 return props.Pgo.Instrumentation != nil && *props.Pgo.Instrumentation == true
78}
79
80func (props *PgoProperties) isSampling() bool {
81 return props.Pgo.Sampling != nil && *props.Pgo.Sampling == true
82}
83
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070084func (pgo *pgo) props() []interface{} {
85 return []interface{}{&pgo.Properties}
86}
87
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -070088func (props *PgoProperties) addProfileGatherFlags(ctx ModuleContext, flags Flags) Flags {
Pirama Arumuga Nainar690ed552017-12-13 16:48:20 -080089 flags.CFlags = append(flags.CFlags, props.Pgo.Cflags...)
90
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -070091 if props.isInstrumentation() {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070092 flags.CFlags = append(flags.CFlags, profileInstrumentFlag)
93 // The profile runtime is added below in deps(). Add the below
94 // flag, which is the only other link-time action performed by
95 // the Clang driver during link.
96 flags.LdFlags = append(flags.LdFlags, "-u__llvm_profile_runtime")
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070097 }
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -070098 if props.isSampling() {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070099 flags.CFlags = append(flags.CFlags, profileSamplingFlag)
100 flags.LdFlags = append(flags.LdFlags, profileSamplingFlag)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700101 }
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700102 return flags
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700103}
104
Yi Kong7e53c572018-02-14 18:16:12 +0800105func (props *PgoProperties) getPgoProfileFile(ctx BaseModuleContext) android.OptionalPath {
Pirama Arumuga Nainar8aed42c2018-03-08 22:56:37 -0800106 profile_file := *props.Pgo.Profile_file
107
Pirama Arumuga Nainar49540802018-01-29 23:11:42 -0800108 // Test if the profile_file is present in any of the PGO profile projects
109 for _, profileProject := range getPgoProfileProjects(ctx.DeviceConfig()) {
Pirama Arumuga Nainar8aed42c2018-03-08 22:56:37 -0800110 // Bug: http://b/74395273 If the profile_file is unavailable,
111 // use a versioned file named
112 // <profile_file>.<arbitrary-version> when available. This
113 // works around an issue where ccache serves stale cache
114 // entries when the profile file has changed.
115 globPattern := filepath.Join(profileProject, profile_file+".*")
116 versioned_profiles, err := ctx.GlobWithDeps(globPattern, nil)
117 if err != nil {
118 ctx.ModuleErrorf("glob: %s", err.Error())
119 }
120
121 path := android.ExistentPathForSource(ctx, profileProject, profile_file)
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800122 if path.Valid() {
Pirama Arumuga Nainar8aed42c2018-03-08 22:56:37 -0800123 if len(versioned_profiles) != 0 {
124 ctx.PropertyErrorf("pgo.profile_file", "Profile_file has multiple versions: "+filepath.Join(profileProject, profile_file)+", "+strings.Join(versioned_profiles, ", "))
125 }
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800126 return path
127 }
Pirama Arumuga Nainar8aed42c2018-03-08 22:56:37 -0800128
129 if len(versioned_profiles) > 1 {
130 ctx.PropertyErrorf("pgo.profile_file", "Profile_file has multiple versions: "+strings.Join(versioned_profiles, ", "))
131 } else if len(versioned_profiles) == 1 {
132 return android.OptionalPathForPath(android.PathForSource(ctx, versioned_profiles[0]))
133 }
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800134 }
135
Pirama Arumuga Nainar28316d42018-01-29 09:18:45 -0800136 // Record that this module's profile file is absent
137 missing := *props.Pgo.Profile_file + ":" + ctx.ModuleDir() + "/Android.bp:" + ctx.ModuleName()
138 recordMissingProfileFile(ctx, missing)
139
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800140 return android.OptionalPathForPath(nil)
141}
142
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -0700143func (props *PgoProperties) profileUseFlag(ctx ModuleContext, file string) string {
144 if props.isInstrumentation() {
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700145 return fmt.Sprintf(profileUseInstrumentFormat, file)
146 }
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -0700147 if props.isSampling() {
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700148 return fmt.Sprintf(profileUseSamplingFormat, file)
149 }
150 return ""
151}
152
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -0700153func (props *PgoProperties) profileUseFlags(ctx ModuleContext, file string) []string {
154 flags := []string{props.profileUseFlag(ctx, file)}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700155 flags = append(flags, profileUseOtherFlags...)
156 return flags
157}
158
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700159func (props *PgoProperties) addProfileUseFlags(ctx ModuleContext, flags Flags) Flags {
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800160 // Return if 'pgo' property is not present in this module.
161 if !props.PgoPresent {
162 return flags
163 }
164
Yi Kongca610d22018-04-24 10:42:02 -0700165 if props.PgoCompile {
166 profileFile := props.getPgoProfileFile(ctx)
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800167 profileFilePath := profileFile.Path()
168 profileUseFlags := props.profileUseFlags(ctx, profileFilePath.String())
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700169
170 flags.CFlags = append(flags.CFlags, profileUseFlags...)
171 flags.LdFlags = append(flags.LdFlags, profileUseFlags...)
172
173 // Update CFlagsDeps and LdFlagsDeps so the module is rebuilt
174 // if profileFile gets updated
Pirama Arumuga Nainar64946fe2018-01-17 14:00:53 -0800175 flags.CFlagsDeps = append(flags.CFlagsDeps, profileFilePath)
176 flags.LdFlagsDeps = append(flags.LdFlagsDeps, profileFilePath)
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700177 }
178 return flags
179}
180
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700181func (props *PgoProperties) isPGO(ctx BaseModuleContext) bool {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700182 isInstrumentation := props.isInstrumentation()
183 isSampling := props.isSampling()
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700184
185 profileKindPresent := isInstrumentation || isSampling
186 filePresent := props.Pgo.Profile_file != nil
187 benchmarksPresent := len(props.Pgo.Benchmarks) > 0
188
189 // If all three properties are absent, PGO is OFF for this module
190 if !profileKindPresent && !filePresent && !benchmarksPresent {
191 return false
192 }
193
194 // If at least one property exists, validate that all properties exist
195 if !profileKindPresent || !filePresent || !benchmarksPresent {
196 var missing []string
197 if !profileKindPresent {
198 missing = append(missing, "profile kind (either \"instrumentation\" or \"sampling\" property)")
199 }
200 if !filePresent {
201 missing = append(missing, "profile_file property")
202 }
203 if !benchmarksPresent {
204 missing = append(missing, "non-empty benchmarks property")
205 }
206 missingProps := strings.Join(missing, ", ")
207 ctx.ModuleErrorf("PGO specification is missing properties: " + missingProps)
208 }
209
210 // Sampling not supported yet
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700211 if isSampling {
212 ctx.PropertyErrorf("pgo.sampling", "\"sampling\" is not supported yet)")
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
248 } else {
249 for _, b := range pgo.Properties.Pgo.Benchmarks {
250 if pgoBenchmarksMap[b] == true {
251 pgo.Properties.ShouldProfileModule = true
252 break
253 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700254 }
255 }
Yi Kong7e53c572018-02-14 18:16:12 +0800256
Yi Kongca610d22018-04-24 10:42:02 -0700257 if !ctx.Config().IsEnvTrue("ANDROID_PGO_NO_PROFILE_USE") &&
258 proptools.BoolDefault(pgo.Properties.Pgo.Enable_profile_use, true) {
Yi Kong7e53c572018-02-14 18:16:12 +0800259 if profileFile := pgo.Properties.getPgoProfileFile(ctx); profileFile.Valid() {
260 pgo.Properties.PgoCompile = true
261 }
262 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700263}
264
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700265func (pgo *pgo) deps(ctx BaseModuleContext, deps Deps) Deps {
266 if pgo.Properties.ShouldProfileModule {
267 runtimeLibrary := config.ProfileRuntimeLibrary(ctx.toolchain())
268 deps.LateStaticLibs = append(deps.LateStaticLibs, runtimeLibrary)
269 }
270 return deps
271}
272
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700273func (pgo *pgo) flags(ctx ModuleContext, flags Flags) Flags {
274 if ctx.Host() {
275 return flags
276 }
277
278 props := pgo.Properties
279
280 // Add flags to profile this module based on its profile_kind
281 if props.ShouldProfileModule {
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -0700282 return props.addProfileGatherFlags(ctx, flags)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700283 }
284
Colin Cross6510f912017-11-29 00:27:14 -0800285 if !ctx.Config().IsEnvTrue("ANDROID_PGO_NO_PROFILE_USE") {
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700286 return props.addProfileUseFlags(ctx, flags)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700287 }
288
289 return flags
290}