blob: 5d1c58a9db025ebceba5441cd7a814926f3a29f3 [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"
19 "strings"
20
21 "android/soong/android"
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070022 "android/soong/cc/config"
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070023)
24
25var (
26 // Add flags to ignore warnings that profiles are old or missing for
27 // some functions
Pirama Arumuga Nainarf4c0baf2017-09-28 14:35:15 -070028 profileUseOtherFlags = []string{"-Wno-backend-plugin"}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070029)
30
31const pgoProfileProject = "toolchain/pgo-profiles"
32
33const profileInstrumentFlag = "-fprofile-generate=/data/local/tmp"
34const profileSamplingFlag = "-gline-tables-only"
35const profileUseInstrumentFormat = "-fprofile-use=%s"
36const profileUseSamplingFormat = "-fprofile-sample-use=%s"
37
38type PgoProperties struct {
39 Pgo struct {
40 Instrumentation *bool
41 Sampling *bool
42 Profile_file *string `android:"arch_variant"`
43 Benchmarks []string
44 } `android:"arch_variant"`
45
46 PgoPresent bool `blueprint:"mutated"`
47 ShouldProfileModule bool `blueprint:"mutated"`
48}
49
50type pgo struct {
51 Properties PgoProperties
52}
53
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070054func (props *PgoProperties) isInstrumentation() bool {
55 return props.Pgo.Instrumentation != nil && *props.Pgo.Instrumentation == true
56}
57
58func (props *PgoProperties) isSampling() bool {
59 return props.Pgo.Sampling != nil && *props.Pgo.Sampling == true
60}
61
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070062func (pgo *pgo) props() []interface{} {
63 return []interface{}{&pgo.Properties}
64}
65
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070066func (pgo *pgo) addProfileGatherFlags(ctx ModuleContext, flags Flags) Flags {
67 if pgo.Properties.isInstrumentation() {
68 flags.CFlags = append(flags.CFlags, profileInstrumentFlag)
69 // The profile runtime is added below in deps(). Add the below
70 // flag, which is the only other link-time action performed by
71 // the Clang driver during link.
72 flags.LdFlags = append(flags.LdFlags, "-u__llvm_profile_runtime")
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070073 }
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070074 if pgo.Properties.isSampling() {
75 flags.CFlags = append(flags.CFlags, profileSamplingFlag)
76 flags.LdFlags = append(flags.LdFlags, profileSamplingFlag)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070077 }
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070078 return flags
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070079}
80
81func (pgo *pgo) profileUseFlag(ctx ModuleContext, file string) string {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070082 if pgo.Properties.isInstrumentation() {
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070083 return fmt.Sprintf(profileUseInstrumentFormat, file)
84 }
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070085 if pgo.Properties.isSampling() {
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070086 return fmt.Sprintf(profileUseSamplingFormat, file)
87 }
88 return ""
89}
90
91func (pgo *pgo) profileUseFlags(ctx ModuleContext, file string) []string {
92 flags := []string{pgo.profileUseFlag(ctx, file)}
93 flags = append(flags, profileUseOtherFlags...)
94 return flags
95}
96
97func (props *PgoProperties) isPGO(ctx BaseModuleContext) bool {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070098 isInstrumentation := props.isInstrumentation()
99 isSampling := props.isSampling()
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700100
101 profileKindPresent := isInstrumentation || isSampling
102 filePresent := props.Pgo.Profile_file != nil
103 benchmarksPresent := len(props.Pgo.Benchmarks) > 0
104
105 // If all three properties are absent, PGO is OFF for this module
106 if !profileKindPresent && !filePresent && !benchmarksPresent {
107 return false
108 }
109
110 // If at least one property exists, validate that all properties exist
111 if !profileKindPresent || !filePresent || !benchmarksPresent {
112 var missing []string
113 if !profileKindPresent {
114 missing = append(missing, "profile kind (either \"instrumentation\" or \"sampling\" property)")
115 }
116 if !filePresent {
117 missing = append(missing, "profile_file property")
118 }
119 if !benchmarksPresent {
120 missing = append(missing, "non-empty benchmarks property")
121 }
122 missingProps := strings.Join(missing, ", ")
123 ctx.ModuleErrorf("PGO specification is missing properties: " + missingProps)
124 }
125
126 // Sampling not supported yet
127 //
128 // TODO When sampling support is turned on, check that instrumentation and
129 // sampling are not simultaneously specified
130 if isSampling {
131 ctx.PropertyErrorf("pgo.sampling", "\"sampling\" is not supported yet)")
132 }
133
134 return true
135}
136
137func getPgoProfilesDir(ctx ModuleContext) android.OptionalPath {
138 return android.ExistentPathForSource(ctx, "", pgoProfileProject)
139}
140
141func (pgo *pgo) begin(ctx BaseModuleContext) {
142 // TODO Evaluate if we need to support PGO for host modules
143 if ctx.Host() {
144 return
145 }
146
147 // Check if PGO is needed for this module
148 pgo.Properties.PgoPresent = pgo.Properties.isPGO(ctx)
149
150 if !pgo.Properties.PgoPresent {
151 return
152 }
153
154 // This module should be instrumented if ANDROID_PGO_INSTRUMENT is set
155 // and includes a benchmark listed for this module
156 //
157 // TODO Validate that each benchmark instruments at least one module
158 pgo.Properties.ShouldProfileModule = false
159 pgoBenchmarks := ctx.AConfig().Getenv("ANDROID_PGO_INSTRUMENT")
160 pgoBenchmarksMap := make(map[string]bool)
161 for _, b := range strings.Split(pgoBenchmarks, ",") {
162 pgoBenchmarksMap[b] = true
163 }
164
165 for _, b := range pgo.Properties.Pgo.Benchmarks {
166 if pgoBenchmarksMap[b] == true {
167 pgo.Properties.ShouldProfileModule = true
168 break
169 }
170 }
171}
172
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700173func (pgo *pgo) deps(ctx BaseModuleContext, deps Deps) Deps {
174 if pgo.Properties.ShouldProfileModule {
175 runtimeLibrary := config.ProfileRuntimeLibrary(ctx.toolchain())
176 deps.LateStaticLibs = append(deps.LateStaticLibs, runtimeLibrary)
177 }
178 return deps
179}
180
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700181func (pgo *pgo) flags(ctx ModuleContext, flags Flags) Flags {
182 if ctx.Host() {
183 return flags
184 }
185
186 props := pgo.Properties
187
188 // Add flags to profile this module based on its profile_kind
189 if props.ShouldProfileModule {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700190 return pgo.addProfileGatherFlags(ctx, flags)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700191 }
192
193 // If the PGO profiles project is found, and this module has PGO
194 // enabled, add flags to use the profile
195 if profilesDir := getPgoProfilesDir(ctx); props.PgoPresent && profilesDir.Valid() {
196 profileFile := android.PathForSource(ctx, profilesDir.String(), *(props.Pgo.Profile_file))
197 profileUseFlags := pgo.profileUseFlags(ctx, profileFile.String())
198
199 flags.CFlags = append(flags.CFlags, profileUseFlags...)
200 flags.LdFlags = append(flags.LdFlags, profileUseFlags...)
201
202 // Update CFlagsDeps and LdFlagsDeps so the module is rebuilt
203 // if profileFile gets updated
204 flags.CFlagsDeps = append(flags.CFlagsDeps, profileFile)
205 flags.LdFlagsDeps = append(flags.LdFlagsDeps, profileFile)
206 }
207
208 return flags
209}