• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//
2// Copyright (C) 2018 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17package {
18    default_applicable_licenses: ["external_jemalloc_new_license"],
19}
20
21// Added automatically by a large-scale-change that took the approach of
22// 'apply every license found to every target'. While this makes sure we respect
23// every license restriction, it may not be entirely correct.
24//
25// e.g. GPL in an MIT project might only apply to the contrib/ directory.
26//
27// Please consider splitting the single license below into multiple licenses,
28// taking care not to lose any license_kind information, and overriding the
29// default license using the 'licenses: [...]' property on targets as needed.
30//
31// For unused files, consider creating a 'fileGroup' with "//visibility:private"
32// to attach the license to, and including a comment whether the files may be
33// used in the current project.
34// See: http://go/android-license-faq
35license {
36    name: "external_jemalloc_new_license",
37    visibility: [":__subpackages__"],
38    license_kinds: [
39        "SPDX-license-identifier-Apache-2.0",
40        "SPDX-license-identifier-BSD",
41        "SPDX-license-identifier-ISC",
42        "SPDX-license-identifier-MIT",
43        "legacy_unencumbered",
44    ],
45    license_text: [
46        "COPYING",
47    ],
48}
49
50common_cflags = [
51    "-D_GNU_SOURCE",
52    "-D_REENTRANT",
53    "-Wall",
54    "-Wshorten-64-to-32",
55    "-Wsign-compare",
56    "-Wundef",
57    "-Wno-format-zero-length",
58    "-pipe",
59    "-g3",
60    "-fvisibility=hidden",
61    "-O3",
62    "-funroll-loops",
63
64    // The following flags are for avoiding errors when compiling.
65    "-Wno-unused-parameter",
66    "-Wno-unused-function",
67    "-Wno-missing-field-initializers",
68
69    "-U_FORTIFY_SOURCE",
70]
71
72common_c_local_includes = [
73    "src",
74    "include",
75]
76
77// These parameters change the way jemalloc works.
78//   ANDROID_NUM_ARENAS=XX
79//     The total number of arenas to create.
80//   ANDROID_TCACHE_NSLOTS_SMALL_MIN=XX
81//     The minimum number of small slots held in the tcache. This must be
82//     at least 1.
83//   ANDROID_TCACHE_NSLOTS_SMALL_MAX=XX
84//     The number of small slots held in the tcache. The higher this number
85//     is, the higher amount of PSS consumed. If this number is set too low
86//     then small allocations will take longer to complete.
87//   ANDROID_TCACHE_NSLOTS_LARGE=XX
88//     The number of large slots held in the tcache. The higher this number
89//     is, the higher amount of PSS consumed. If this number is set too low
90//     then large allocations will take longer to complete.
91//   ANDROID_LG_TCACHE_MAXCLASS_DEFAULT=XX
92//     1 << XX is the maximum sized allocation that will be in the tcache.
93
94android_common_cflags = [
95    // Default parameters for jemalloc config.
96    "-DANDROID_ENABLE_TCACHE",
97    "-DANDROID_LG_TCACHE_MAXCLASS_DEFAULT=16",
98    "-DANDROID_NUM_ARENAS=2",
99    "-DANDROID_TCACHE_NSLOTS_SMALL_MAX=8",
100    "-DANDROID_TCACHE_NSLOTS_LARGE=16",
101]
102
103android_product_variables = {
104    malloc_low_memory: {
105        // Parameters to minimize RSS.
106        cflags: [
107            // Disable the tcache on non-svelte configurations, to save PSS.
108            "-UANDROID_ENABLE_TCACHE",
109
110            "-UANDROID_NUM_ARENAS",
111            "-DANDROID_NUM_ARENAS=1",
112
113            // This value cannot go below 2.
114            "-UANDROID_TCACHE_NSLOTS_SMALL_MAX",
115            "-DANDROID_TCACHE_NSLOTS_SMALL_MAX=2",
116
117            "-UANDROID_TCACHE_NSLOTS_LARGE",
118            "-DANDROID_TCACHE_NSLOTS_LARGE=1",
119        ],
120    },
121}
122
123cc_defaults {
124    name: "jemalloc5_defaults",
125    host_supported: true,
126    native_bridge_supported: true,
127    cflags: common_cflags,
128
129    target: {
130        android: {
131            cflags: android_common_cflags,
132            product_variables: android_product_variables,
133        },
134        linux_bionic: {
135            enabled: true,
136        },
137    },
138
139    local_include_dirs: common_c_local_includes,
140    stl: "none",
141}
142
143lib_src_files = [
144    "src/jemalloc.c",
145    "src/arena.c",
146    "src/background_thread.c",
147    "src/base.c",
148    "src/bin.c",
149    "src/bitmap.c",
150    "src/ckh.c",
151    "src/ctl.c",
152    "src/div.c",
153    "src/extent.c",
154    "src/extent_dss.c",
155    "src/extent_mmap.c",
156    "src/hash.c",
157    "src/hooks.c",
158    "src/large.c",
159    "src/log.c",
160    "src/malloc_io.c",
161    "src/mutex.c",
162    "src/mutex_pool.c",
163    "src/nstime.c",
164    "src/pages.c",
165    "src/prng.c",
166    "src/prof.c",
167    "src/rtree.c",
168    "src/stats.c",
169    "src/sz.c",
170    "src/tcache.c",
171    "src/ticker.c",
172    "src/tsd.c",
173    "src/witness.c",
174]
175
176//-----------------------------------------------------------------------
177// jemalloc static library
178//-----------------------------------------------------------------------
179cc_library {
180    name: "libjemalloc5",
181    ramdisk_available: true,
182    vendor_ramdisk_available: true,
183    recovery_available: true,
184
185    defaults: ["jemalloc5_defaults"],
186
187    srcs: lib_src_files,
188
189    export_include_dirs: ["include"],
190
191    target: {
192        android: {
193            shared: {
194                enabled: false,
195            },
196            system_shared_libs: [],
197            header_libs: ["libc_headers"],
198        },
199        linux_bionic: {
200            system_shared_libs: [],
201            header_libs: ["libc_headers"],
202        },
203        musl: {
204            // Linking against musl uses libjemalloc5 by default, list only
205            // libc_musl here to avoid a circular dependency.
206            system_shared_libs: ["libc_musl"],
207        },
208    },
209    apex_available: [
210        "com.android.runtime",
211    ],
212}
213
214//-----------------------------------------------------------------------
215// jemalloc "je"-prefixed static library
216//
217// This is the same as "libjemalloc5", except:
218//  - It only supports host (just because we don't need it for device)
219//  - all the functions (malloc, calloc, free, etc.) have a "je_" prefix.
220//    The -DJEMALLOC_NO_RENAME flag causes the prefix to be added.
221//
222// We need this because when building rust binaries with jemalloc and
223// the tikv-jemallocator wrappers, code in libc gets a segfault before
224// reaching main(). I'm not sure why that is, but if we prefix the jemalloc
225// methods and have the tikv-jemallocator crate use the prefixed ones,
226// all rust code will use jemalloc successfully while libc will continue
227// using the system allocator.
228//-----------------------------------------------------------------------
229cc_library_host_static {
230    name: "libjemalloc5_je_prefixed",
231
232    defaults: ["jemalloc5_defaults"],
233
234    cflags: ["-DJEMALLOC_NO_RENAME"],
235
236    srcs: lib_src_files,
237
238    export_include_dirs: ["include"],
239
240    target: {
241        linux_bionic: {
242            system_shared_libs: [],
243            header_libs: ["libc_headers"],
244        },
245        musl: {
246            // Linking against musl uses libjemalloc5 by default, list only
247            // libc_musl here to avoid a circular dependency.
248            system_shared_libs: ["libc_musl"],
249        },
250    },
251
252    visibility: [
253        "//external/rust/crates/tikv-jemalloc-sys:__subpackages__",
254    ],
255}
256
257//-----------------------------------------------------------------------
258// jemalloc static jet library
259//-----------------------------------------------------------------------
260cc_library_static {
261    name: "libjemalloc5_jet",
262
263    defaults: ["jemalloc5_defaults"],
264
265    cflags: [
266        "-DJEMALLOC_JET",
267    ],
268
269    srcs: lib_src_files,
270}
271
272jemalloc5_testlib_srcs = [
273    "test/src/btalloc.c",
274    "test/src/btalloc_0.c",
275    "test/src/btalloc_1.c",
276    "test/src/math.c",
277    "test/src/mtx.c",
278    "test/src/mq.c",
279    "test/src/SFMT.c",
280    "test/src/test.c",
281    "test/src/thd.c",
282    "test/src/timer.c",
283]
284
285//-----------------------------------------------------------------------
286// jemalloc unit test library
287//-----------------------------------------------------------------------
288cc_library_static {
289    name: "libjemalloc5_unittest",
290
291    defaults: ["jemalloc5_defaults"],
292
293    cflags: [
294        "-DJEMALLOC_UNIT_TEST",
295    ],
296
297    local_include_dirs: [
298        "test/include",
299    ],
300
301    srcs: jemalloc5_testlib_srcs,
302
303    whole_static_libs: ["libjemalloc5_jet"],
304}
305
306//-----------------------------------------------------------------------
307// jemalloc unit tests
308//-----------------------------------------------------------------------
309unit_tests = [
310    "test/unit/a0.c",
311    "test/unit/arena_reset.c",
312    "test/unit/atomic.c",
313    "test/unit/background_thread.c",
314    "test/unit/background_thread_enable.c",
315    "test/unit/base.c",
316    "test/unit/bitmap.c",
317    "test/unit/ckh.c",
318    "test/unit/decay.c",
319    "test/unit/div.c",
320    "test/unit/emitter.c",
321    "test/unit/extent_quantize.c",
322    "test/unit/fork.c",
323    "test/unit/hash.c",
324    "test/unit/hooks.c",
325    "test/unit/junk.c",
326    "test/unit/junk_alloc.c",
327    "test/unit/junk_free.c",
328    "test/unit/log.c",
329    "test/unit/mallctl.c",
330    "test/unit/malloc_io.c",
331    "test/unit/math.c",
332    "test/unit/mq.c",
333    "test/unit/mtx.c",
334    "test/unit/pack.c",
335    "test/unit/pages.c",
336    "test/unit/ph.c",
337    "test/unit/prng.c",
338    "test/unit/prof_accum.c",
339    "test/unit/prof_active.c",
340    "test/unit/prof_gdump.c",
341    "test/unit/prof_idump.c",
342    "test/unit/prof_reset.c",
343    "test/unit/prof_tctx.c",
344    "test/unit/prof_thread_name.c",
345    "test/unit/ql.c",
346    "test/unit/qr.c",
347    "test/unit/rb.c",
348    "test/unit/retained.c",
349    "test/unit/rtree.c",
350    "test/unit/SFMT.c",
351    "test/unit/size_classes.c",
352    "test/unit/slab.c",
353    "test/unit/smoothstep.c",
354    "test/unit/spin.c",
355    "test/unit/stats.c",
356    "test/unit/stats_print.c",
357    "test/unit/ticker.c",
358    "test/unit/nstime.c",
359    "test/unit/tsd.c",
360    "test/unit/witness.c",
361    "test/unit/zero.c",
362]
363
364cc_test {
365    name: "jemalloc5_unittests",
366
367    defaults: ["jemalloc5_defaults"],
368
369    gtest: false,
370
371    cflags: common_cflags + [
372        "-DJEMALLOC_UNIT_TEST",
373    ],
374
375    local_include_dirs: common_c_local_includes + [
376        "test/include",
377    ],
378
379    srcs: unit_tests,
380
381    static_libs: ["libjemalloc5_unittest"],
382
383    test_per_src: true,
384
385    target: {
386        linux_bionic: {
387            enabled: true,
388        },
389        linux_glibc: {
390            // The sanitizer does not work for these tests on the host.
391            sanitize: {
392                never: true,
393            },
394        },
395    },
396}
397
398//-----------------------------------------------------------------------
399// jemalloc integration test library
400//-----------------------------------------------------------------------
401cc_library_static {
402    name: "libjemalloc5_integrationtest",
403
404    defaults: ["jemalloc5_defaults"],
405
406    cflags: [
407        "-U_FORTIFY_SOURCE",
408        "-DJEMALLOC_INTEGRATION_TEST",
409        "-DJEMALLOC_NO_RENAME",
410    ],
411
412    local_include_dirs: [
413        "test/include",
414    ],
415
416    srcs: jemalloc5_testlib_srcs + lib_src_files,
417}
418
419//-----------------------------------------------------------------------
420// jemalloc integration tests
421//-----------------------------------------------------------------------
422integration_tests = [
423    "test/integration/aligned_alloc.c",
424    "test/integration/allocated.c",
425    "test/integration/extent.c",
426    "test/integration/mallocx.c",
427    "test/integration/MALLOCX_ARENA.c",
428    "test/integration/overflow.c",
429    "test/integration/posix_memalign.c",
430    "test/integration/rallocx.c",
431    "test/integration/sdallocx.c",
432    "test/integration/thread_arena.c",
433    "test/integration/xallocx.c",
434    "test/integration/cpp/basic.cpp",
435]
436
437cc_test {
438    name: "jemalloc5_integrationtests",
439
440    defaults: ["jemalloc5_defaults"],
441
442    gtest: false,
443
444    cflags: common_cflags + [
445        "-DJEMALLOC_INTEGRATION_TEST",
446        "-DJEMALLOC_NO_RENAME",
447    ],
448
449    local_include_dirs: common_c_local_includes + [
450        "test/include",
451    ],
452
453    srcs: integration_tests,
454
455    static_libs: ["libjemalloc5_integrationtest"],
456
457    test_per_src: true,
458
459    target: {
460        linux_glibc: {
461            // The sanitizer does not work for these tests on the host.
462            sanitize: {
463                never: true,
464            },
465        },
466    },
467
468    // Needed for basic.cpp test.
469    stl: "libc++_static",
470}
471
472//-----------------------------------------------------------------------
473// jemalloc stress test library
474//-----------------------------------------------------------------------
475cc_library_static {
476    name: "libjemalloc5_stresstestlib",
477
478    defaults: ["jemalloc5_defaults"],
479
480    cflags: [
481        "-DJEMALLOC_STRESS_TEST",
482        "-DJEMALLOC_STRESS_TESTLIB",
483    ],
484
485    local_include_dirs: [
486        "test/include",
487    ],
488
489    srcs: jemalloc5_testlib_srcs,
490}
491
492//-----------------------------------------------------------------------
493// jemalloc stress tests
494//-----------------------------------------------------------------------
495// All tests are in the same order as in the original jemalloc Makefile
496// to make it easier to track changes.
497stress_tests = [
498    "test/stress/microbench.c",
499]
500
501cc_test {
502    name: "jemalloc5_stresstests",
503
504    defaults: ["jemalloc5_defaults"],
505
506    gtest: false,
507
508    cflags: common_cflags + [
509        "-DJEMALLOC_STRESS_TEST",
510    ],
511
512    local_include_dirs: common_c_local_includes + [
513        "test/include",
514    ],
515
516    srcs: stress_tests,
517
518    static_libs: [
519        "libjemalloc5",
520        "libjemalloc5_stresstestlib",
521        "libjemalloc5_jet",
522    ],
523
524    test_per_src: true,
525
526    target: {
527        linux_glibc: {
528            // The sanitizer does not work for these tests on the host.
529            sanitize: {
530                never: true,
531            },
532        },
533    },
534}
535