aboutsummaryrefslogtreecommitdiff
path: root/src/profiling/memory/BUILD.gn
blob: f0e350f1117028ceaba2d279f6a29df8987b6b46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# Copyright (C) 2018 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("../../../gn/fuzzer.gni")
import("../../../gn/perfetto.gni")
import("../../../gn/test.gni")

assert(enable_perfetto_heapprofd)

# The Android heap profiling daemon.
executable("heapprofd") {
  deps = [
    "../../../gn:default_deps",
    "../../../protos/perfetto/trace:zero",
    "../../../src/base",
    "../../../src/base:unix_socket",
    "../../../src/profiling/memory:daemon",
    "../../../src/profiling/memory:wire_protocol",
    "../../../src/tracing/ipc/producer",
  ]
  sources = [ "main.cc" ]
}

# This library gets loaded into (and executes in) arbitrary android processes.
# Logging must be non-allocating. This is achieved by defining
# PERFETTO_ANDROID_ASYNC_SAFE_LOG, which needs to be set for all perfetto code
# being compiled for this library. When generating Android.bp, the |cflags|
# entry on this target is sufficient (as all sources are flattened into a
# single bp target). However this is not correctly reflected in the gn
# structure (which is a tree of targets) as the dependencies would not pick
# up the flag (and thus use the wrong logging macro).
#
# This builds only in the Android tree, when using the generated Android.bp.
if (perfetto_build_with_android) {
  shared_library("heapprofd_client") {
    configs -= [ "//gn/standalone:android_liblog" ]
    cflags = [ "-DPERFETTO_ANDROID_ASYNC_SAFE_LOG" ]
    deps = [
      ":heapprofd_client_api",
      ":malloc_interceptor_bionic_hooks",
    ]
  }

  # This will export publicly visible symbols for the
  # malloc_interceptor_bionic_hooks.
  source_set("malloc_interceptor_bionic_hooks") {
    deps = [
      ":bionic_libc_platform_headers_on_android",
      ":wrap_allocators",
      "../../../gn:default_deps",
      "../../base",
    ]
    cflags = [
      "-isystem",
      rebase_path("../../../buildtools/bionic/libc", root_build_dir),
    ]
    sources = [ "malloc_interceptor_bionic_hooks.cc" ]
  }
}  # if (perfetto_build_with_android)

executable("heapprofd_standalone_client_example") {
  deps = [
    ":heapprofd_standalone_client",
    "../../../gn:default_deps",
  ]
  sources = [ "heapprofd_standalone_client_example.cc" ]
}

source_set("client_api_standalone") {
  deps = [
    ":client",
    ":client_api",
    ":daemon",
    "../../../gn:default_deps",
    "../../../include/perfetto/ext/tracing/ipc",
    "../../base",
    "../common:proc_utils",
  ]
  sources = [ "client_api_factory_standalone.cc" ]
}

# This can be used to instrument custom allocators to report their allocations
# to Perfetto. This bundles a copy of heapprofd in the library, in contrast to
# heapprofd_client_api (see below), which expects one to be present in the
# Android platform.
shared_library("heapprofd_standalone_client") {
  deps = [
    ":client_api_standalone",
    "../../../gn:default_deps",
  ]
  ldflags = [
    "-Wl,--version-script",
    rebase_path("heapprofd_client_api.map.txt", root_build_dir),
  ]
}

shared_library("heapprofd_api_noop") {
  deps = [ "../../../gn:default_deps" ]
  ldflags = [
    "-Wl,--version-script",
    rebase_path("heapprofd_client_api.map.txt", root_build_dir),
  ]

  # This target does absolutely nothing, so we do not want to depend on
  # liblog.
  configs -= [ "//gn/standalone:android_liblog" ]
  sources = [ "client_api_noop.cc" ]
}

shared_library("heapprofd_client_api") {
  configs -= [ "//gn/standalone:android_liblog" ]
  if (perfetto_build_with_android) {
    cflags = [ "-DPERFETTO_ANDROID_ASYNC_SAFE_LOG" ]
  } else {
    # We don't have async safe logging for non-Android, so disable all
    # logging in the client.
    # TODO(fmayer): Add async-safe logging for non-Android.
    cflags = [ "-DPERFETTO_DISABLE_LOG" ]
  }
  deps = [ ":client_api" ]
  if (perfetto_build_with_android) {
    sources = [ "client_api_factory_android.cc" ]
  }
}

# On GLibc Linux, this can be used to override the allocators using
# LD_PRELOAD. On non-GLibc, this will probably fail to link.
shared_library("heapprofd_glibc_preload") {
  deps = [
    ":client_api_standalone",
    ":wrap_allocators",
    "../../../gn:default_deps",
    "../../base",
  ]
  ldflags = [
    "-Wl,--version-script",
    rebase_path("heapprofd_preload.map.txt", root_build_dir),
  ]
  sources = [ "malloc_interceptor_glibc_preload.cc" ]
}

# On Android builds, this is converted to
# header_libs: ["bionic_libc_platform_headers"].
source_set("bionic_libc_platform_headers_on_android") {
}

source_set("client_api") {
  if (perfetto_build_with_android) {
    cflags = [ "-DPERFETTO_ANDROID_ASYNC_SAFE_LOG" ]
  } else {
    # We don't have async safe logging for non-Android, so disable all
    # logging in the client.
    # TODO(fmayer): Add async-safe logging for non-Android.
    cflags = [ "-DPERFETTO_DISABLE_LOG" ]
  }
  deps = [
    ":client",
    ":scoped_spinlock",
    ":wire_protocol",
    "../../../gn:default_deps",
    "../../base",
    "../common:proc_utils",
  ]
  sources = [ "client_api.cc" ]
}

source_set("wrap_allocators") {
  deps = [
    "../../../gn:default_deps",
    "../../base",
  ]
  sources = [ "wrap_allocators.cc" ]
}

source_set("wire_protocol") {
  public_deps = [ "../../../gn:libunwindstack" ]
  deps = [
    ":bionic_libc_platform_headers_on_android",
    ":ring_buffer",
    "../../../gn:default_deps",
    "../../base",
    "../../base:unix_socket",
  ]
  sources = [
    "wire_protocol.cc",
    "wire_protocol.h",
  ]
}

source_set("scoped_spinlock") {
  deps = [
    "../../../gn:default_deps",
    "../../base",
  ]
  sources = [
    "scoped_spinlock.cc",
    "scoped_spinlock.h",
  ]
}

source_set("ring_buffer") {
  deps = [
    ":scoped_spinlock",
    "../../../gn:default_deps",
    "../../base",
  ]
  sources = [
    "shared_ring_buffer.cc",
    "shared_ring_buffer.h",
  ]
}

source_set("ring_buffer_unittests") {
  testonly = true
  deps = [
    ":ring_buffer",
    "../../../gn:default_deps",
    "../../../gn:gtest_and_gmock",
    "../../base",
  ]
  sources = [ "shared_ring_buffer_unittest.cc" ]
}

source_set("daemon") {
  deps = [
    ":ring_buffer",
    ":scoped_spinlock",
    ":wire_protocol",
    "../../../gn:default_deps",
    "../../../protos/perfetto/config/profiling:cpp",
    "../../base",
    "../../base:unix_socket",
    "../../tracing/core",
    "../../tracing/ipc/producer",
    "../common:callstack_trie",
    "../common:interner",
    "../common:interning_output",
    "../common:proc_utils",
    "../common:producer_support",
    "../common:profiler_guardrails",
    "../common:unwind_support",
  ]
  public_deps = [
    "../../../gn:libunwindstack",
    "../../../protos/perfetto/config/profiling:cpp",
    "../../../protos/perfetto/trace:zero",
    "../../../protos/perfetto/trace/interned_data:zero",
    "../../../protos/perfetto/trace/profiling:zero",
  ]
  sources = [
    "bookkeeping.cc",
    "bookkeeping.h",
    "bookkeeping_dump.cc",
    "bookkeeping_dump.h",
    "heapprofd_producer.cc",
    "heapprofd_producer.h",
    "java_hprof_producer.cc",
    "java_hprof_producer.h",
    "log_histogram.cc",
    "log_histogram.h",
    "system_property.cc",
    "system_property.h",
    "unwinding.cc",
    "unwinding.h",
    "unwound_messages.h",
  ]
}

source_set("client") {
  deps = [
    ":ring_buffer",
    ":scoped_spinlock",
    ":wire_protocol",
    "../../../gn:default_deps",
    "../../base",
    "../../base:unix_socket",
    "../common:proc_utils",
  ]
  public_deps = [ "../../../gn:libunwindstack" ]
  sources = [
    "client.cc",
    "client.h",
    "sampler.cc",
    "sampler.h",
  ]
}

perfetto_unittest_source_set("unittests") {
  testonly = true
  deps = [
    ":client",
    ":daemon",
    ":ring_buffer",
    ":wire_protocol",
    "../../../gn:default_deps",
    "../../../gn:gtest_and_gmock",
    "../../../gn:libunwindstack",
    "../../base",
    "../../base:test_support",
    "../../tracing/core",
    "../common:proc_utils",
    "../common:unwind_support",
  ]
  sources = [
    "bookkeeping_unittest.cc",
    "client_unittest.cc",
    "heapprofd_producer_unittest.cc",
    "parse_smaps_unittest.cc",
    "sampler_unittest.cc",
    "system_property_unittest.cc",
    "unwinding_unittest.cc",
    "wire_protocol_unittest.cc",
  ]

  # Do not build with Android to avoid applying PERFETTO_ANDROID_ASYNC_SAFE_LOG
  # to the whole perfetto_unittests target.
  if (!perfetto_build_with_android) {
    sources += [ "client_api_unittest.cc" ]
    deps += [ ":client_api" ]
  }
}

source_set("end_to_end_tests") {
  testonly = true
  deps = [
    ":client",
    ":daemon",
    ":wire_protocol",
    "../../../gn:default_deps",
    "../../../gn:gtest_and_gmock",
    "../../../gn:libunwindstack",
    "../../../protos/perfetto/config/profiling:cpp",
    "../../../protos/perfetto/trace/interned_data:cpp",
    "../../../protos/perfetto/trace/profiling:cpp",
    "../../../test:test_helper",
    "../../base",
    "../../base:test_support",
    "../../trace_processor:lib",
  ]
  sources = [ "heapprofd_end_to_end_test.cc" ]
  if (perfetto_build_with_android) {
    deps += [ ":heapprofd_client_api" ]
  } else {
    deps += [ ":heapprofd_standalone_client" ]
  }
  if (start_daemons_for_testing) {
    defines = [ "PERFETTO_START_DAEMONS_FOR_TESTING" ]
  }
}

perfetto_fuzzer_test("unwinding_fuzzer") {
  testonly = true
  sources = [ "unwinding_fuzzer.cc" ]
  deps = [
    ":daemon",
    ":ring_buffer",
    ":wire_protocol",
    "../../../gn:default_deps",
    "../../base",
    "../../tracing/core",
    "../common:unwind_support",
  ]
}

perfetto_fuzzer_test("shared_ring_buffer_fuzzer") {
  testonly = true
  sources = [ "shared_ring_buffer_fuzzer.cc" ]
  deps = [
    ":ring_buffer",
    "../../../gn:default_deps",
    "../../base",
  ]
}

perfetto_fuzzer_test("shared_ring_buffer_write_fuzzer") {
  testonly = true
  sources = [ "shared_ring_buffer_write_fuzzer.cc" ]
  deps = [
    ":ring_buffer",
    "../../../gn:default_deps",
    "../../base",
  ]
}

if (enable_perfetto_benchmarks) {
  source_set("benchmarks") {
    testonly = true
    deps = [
      ":client",
      ":client_api",
      "../../../gn:benchmark",
      "../../../gn:default_deps",
      "../../base",
      "../../base:test_support",
    ]
    sources = [ "client_api_benchmark.cc" ]
  }
}