xref: /aosp_15_r20/external/jemalloc_new/Android.bp (revision 1208bc7e437ced7eb82efac44ba17e3beba411da)
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    // Default enable the tcache.
72    "-DANDROID_ENABLE_TCACHE",
73]
74
75common_c_local_includes = [
76    "src",
77    "include",
78]
79
80// These parameters change the way jemalloc works.
81//   ANDROID_NUM_ARENAS=XX
82//     The total number of arenas to create.
83//   ANDROID_TCACHE_NSLOTS_SMALL_MIN=XX
84//     The minimum number of small slots held in the tcache. This must be
85//     at least 1.
86//   ANDROID_TCACHE_NSLOTS_SMALL_MAX=XX
87//     The number of small slots held in the tcache. The higher this number
88//     is, the higher amount of PSS consumed. If this number is set too low
89//     then small allocations will take longer to complete.
90//   ANDROID_TCACHE_NSLOTS_LARGE=XX
91//     The number of large slots held in the tcache. The higher this number
92//     is, the higher amount of PSS consumed. If this number is set too low
93//     then large allocations will take longer to complete.
94//   ANDROID_LG_TCACHE_MAXCLASS_DEFAULT=XX
95//     1 << XX is the maximum sized allocation that will be in the tcache.
96
97android_common_cflags = [
98    // Default parameters for jemalloc config.
99    "-DANDROID_LG_TCACHE_MAXCLASS_DEFAULT=16",
100    "-DANDROID_NUM_ARENAS=2",
101    "-DANDROID_TCACHE_NSLOTS_SMALL_MAX=8",
102    "-DANDROID_TCACHE_NSLOTS_LARGE=16",
103]
104
105android_product_variables = {
106    malloc_low_memory: {
107        // Parameters to minimize RSS.
108        cflags: [
109            // Disable the tcache on non-svelte configurations, to save PSS.
110            "-UANDROID_ENABLE_TCACHE",
111
112            "-UANDROID_NUM_ARENAS",
113            "-DANDROID_NUM_ARENAS=1",
114
115            // This value cannot go below 2.
116            "-UANDROID_TCACHE_NSLOTS_SMALL_MAX",
117            "-DANDROID_TCACHE_NSLOTS_SMALL_MAX=2",
118
119            "-UANDROID_TCACHE_NSLOTS_LARGE",
120            "-DANDROID_TCACHE_NSLOTS_LARGE=1",
121
122            // Minimize the size of the internal data structures by removing
123            // unused stats and other data not used on Android.
124            "-DANDROID_MINIMIZE_STRUCTS",
125        ],
126    },
127}
128
129cc_defaults {
130    name: "jemalloc5_defaults",
131    host_supported: true,
132    native_bridge_supported: true,
133    cflags: common_cflags,
134
135    target: {
136        android: {
137            cflags: android_common_cflags,
138            product_variables: android_product_variables,
139        },
140        linux_bionic: {
141            enabled: true,
142        },
143    },
144
145    local_include_dirs: common_c_local_includes,
146    stl: "none",
147}
148
149lib_src_files = [
150    "src/jemalloc.c",
151    "src/arena.c",
152    "src/background_thread.c",
153    "src/base.c",
154    "src/bin.c",
155    "src/bitmap.c",
156    "src/ckh.c",
157    "src/ctl.c",
158    "src/div.c",
159    "src/extent.c",
160    "src/extent_dss.c",
161    "src/extent_mmap.c",
162    "src/hash.c",
163    "src/hooks.c",
164    "src/large.c",
165    "src/log.c",
166    "src/malloc_io.c",
167    "src/mutex.c",
168    "src/mutex_pool.c",
169    "src/nstime.c",
170    "src/pages.c",
171    "src/prng.c",
172    "src/prof.c",
173    "src/rtree.c",
174    "src/stats.c",
175    "src/sz.c",
176    "src/tcache.c",
177    "src/ticker.c",
178    "src/tsd.c",
179    "src/witness.c",
180]
181
182//-----------------------------------------------------------------------
183// jemalloc static library
184//-----------------------------------------------------------------------
185cc_library {
186    name: "libjemalloc5",
187    ramdisk_available: true,
188    vendor_ramdisk_available: true,
189    recovery_available: true,
190
191    defaults: ["jemalloc5_defaults"],
192
193    srcs: lib_src_files,
194
195    export_include_dirs: ["include"],
196
197    target: {
198        android: {
199            shared: {
200                enabled: false,
201            },
202            system_shared_libs: [],
203            header_libs: ["libc_headers"],
204        },
205        linux_bionic: {
206            system_shared_libs: [],
207            header_libs: ["libc_headers"],
208        },
209        musl: {
210            // Linking against musl uses libjemalloc5 by default, list only
211            // libc_musl here to avoid a circular dependency.
212            system_shared_libs: ["libc_musl"],
213        },
214    },
215    apex_available: [
216        "com.android.runtime",
217    ],
218}
219
220//-----------------------------------------------------------------------
221// jemalloc "je"-prefixed static library
222//
223// This is the same as "libjemalloc5", except:
224//  - It only supports host (just because we don't need it for device)
225//  - all the functions (malloc, calloc, free, etc.) have a "je_" prefix.
226//    The -DJEMALLOC_NO_RENAME flag causes the prefix to be added.
227//
228// We need this because when building rust binaries with jemalloc and
229// the tikv-jemallocator wrappers, code in libc gets a segfault before
230// reaching main(). I'm not sure why that is, but if we prefix the jemalloc
231// methods and have the tikv-jemallocator crate use the prefixed ones,
232// all rust code will use jemalloc successfully while libc will continue
233// using the system allocator.
234//-----------------------------------------------------------------------
235cc_library_host_static {
236    name: "libjemalloc5_je_prefixed",
237
238    defaults: ["jemalloc5_defaults"],
239
240    cflags: ["-DJEMALLOC_NO_RENAME"],
241
242    srcs: lib_src_files,
243
244    export_include_dirs: ["include"],
245
246    target: {
247        linux_bionic: {
248            system_shared_libs: [],
249            header_libs: ["libc_headers"],
250        },
251        musl: {
252            // Linking against musl uses libjemalloc5 by default, list only
253            // libc_musl here to avoid a circular dependency.
254            system_shared_libs: ["libc_musl"],
255        },
256    },
257
258    visibility: [
259        "//external/rust/android-crates-io/crates/tikv-jemalloc-sys:__subpackages__",
260    ],
261}
262
263//-----------------------------------------------------------------------
264// jemalloc static jet library
265//-----------------------------------------------------------------------
266cc_library_static {
267    name: "libjemalloc5_jet",
268
269    defaults: ["jemalloc5_defaults"],
270
271    cflags: [
272        "-DJEMALLOC_JET",
273    ],
274
275    srcs: lib_src_files,
276}
277
278jemalloc5_testlib_srcs = [
279    "test/src/btalloc.c",
280    "test/src/btalloc_0.c",
281    "test/src/btalloc_1.c",
282    "test/src/math.c",
283    "test/src/mtx.c",
284    "test/src/mq.c",
285    "test/src/SFMT.c",
286    "test/src/test.c",
287    "test/src/thd.c",
288    "test/src/timer.c",
289]
290
291//-----------------------------------------------------------------------
292// jemalloc unit test library
293//-----------------------------------------------------------------------
294cc_library_static {
295    name: "libjemalloc5_unittest",
296
297    defaults: ["jemalloc5_defaults"],
298
299    cflags: [
300        "-DJEMALLOC_UNIT_TEST",
301    ],
302
303    local_include_dirs: [
304        "test/include",
305    ],
306
307    srcs: jemalloc5_testlib_srcs,
308
309    whole_static_libs: ["libjemalloc5_jet"],
310}
311
312//-----------------------------------------------------------------------
313// jemalloc unit tests
314//-----------------------------------------------------------------------
315unit_tests = [
316    "test/unit/a0.c",
317    "test/unit/arena_reset.c",
318    "test/unit/atomic.c",
319    "test/unit/background_thread.c",
320    "test/unit/background_thread_enable.c",
321    "test/unit/base.c",
322    "test/unit/bitmap.c",
323    "test/unit/ckh.c",
324    "test/unit/decay.c",
325    "test/unit/div.c",
326    "test/unit/emitter.c",
327    "test/unit/extent_quantize.c",
328    "test/unit/fork.c",
329    "test/unit/hash.c",
330    "test/unit/hooks.c",
331    "test/unit/junk.c",
332    "test/unit/junk_alloc.c",
333    "test/unit/junk_free.c",
334    "test/unit/log.c",
335    "test/unit/mallctl.c",
336    "test/unit/malloc_io.c",
337    "test/unit/math.c",
338    "test/unit/mq.c",
339    "test/unit/mtx.c",
340    "test/unit/pack.c",
341    "test/unit/pages.c",
342    "test/unit/ph.c",
343    "test/unit/prng.c",
344    "test/unit/prof_accum.c",
345    "test/unit/prof_active.c",
346    "test/unit/prof_gdump.c",
347    "test/unit/prof_idump.c",
348    "test/unit/prof_reset.c",
349    "test/unit/prof_tctx.c",
350    "test/unit/prof_thread_name.c",
351    "test/unit/ql.c",
352    "test/unit/qr.c",
353    "test/unit/rb.c",
354    "test/unit/retained.c",
355    "test/unit/rtree.c",
356    "test/unit/SFMT.c",
357    "test/unit/size_classes.c",
358    "test/unit/slab.c",
359    "test/unit/smoothstep.c",
360    "test/unit/spin.c",
361    "test/unit/stats.c",
362    "test/unit/stats_print.c",
363    "test/unit/ticker.c",
364    "test/unit/nstime.c",
365    "test/unit/tsd.c",
366    "test/unit/witness.c",
367    "test/unit/zero.c",
368]
369
370genrule_defaults {
371    name: "jemalloc5_gtestifier_defaults",
372    tools: ["gtestifier"],
373    cmd: "$(location gtestifier)" +
374        " --in $(in)" +
375        " --out $(out)" +
376        " --suite jemalloc5" +
377        " --test_name_prefix test_ " +
378        " --main_no_arguments " +
379        " --predicate testResultPredicate",
380}
381
382gensrcs {
383    name: "jemalloc5_unittests_gtestified_srcs",
384    defaults: ["jemalloc5_gtestifier_defaults"],
385    srcs: unit_tests,
386    output_extension: "c",
387}
388
389cc_test {
390    name: "jemalloc5_unittests",
391
392    defaults: ["jemalloc5_defaults"],
393
394    cflags: common_cflags + [
395        "-DJEMALLOC_UNIT_TEST",
396        "-include jemalloc_gtest.h",
397    ],
398
399    stl: "libc++",
400
401    local_include_dirs: common_c_local_includes + [
402        "test/include",
403        "android/test",
404    ],
405
406    srcs: [":jemalloc5_unittests_gtestified_srcs"],
407
408    static_libs: [
409        "libjemalloc5_unittest",
410        "libgtestifier",
411    ],
412
413    isolated: true,
414
415    target: {
416        linux_bionic: {
417            enabled: true,
418        },
419        linux_glibc: {
420            // The sanitizer does not work for these tests on the host.
421            sanitize: {
422                never: true,
423            },
424        },
425    },
426}
427
428//-----------------------------------------------------------------------
429// jemalloc integration test library
430//-----------------------------------------------------------------------
431cc_library_static {
432    name: "libjemalloc5_integrationtest",
433
434    defaults: ["jemalloc5_defaults"],
435
436    cflags: [
437        "-U_FORTIFY_SOURCE",
438        "-DJEMALLOC_INTEGRATION_TEST",
439        "-DJEMALLOC_NO_RENAME",
440    ],
441
442    local_include_dirs: [
443        "test/include",
444    ],
445
446    srcs: jemalloc5_testlib_srcs + lib_src_files,
447}
448
449//-----------------------------------------------------------------------
450// jemalloc integration tests
451//-----------------------------------------------------------------------
452integration_tests = [
453    "test/integration/aligned_alloc.c",
454    "test/integration/allocated.c",
455    "test/integration/extent.c",
456    "test/integration/mallocx.c",
457    "test/integration/MALLOCX_ARENA.c",
458    "test/integration/overflow.c",
459    "test/integration/posix_memalign.c",
460    "test/integration/rallocx.c",
461    "test/integration/sdallocx.c",
462    "test/integration/thread_arena.c",
463    "test/integration/xallocx.c",
464]
465
466integration_cpp_tests = [
467    "test/integration/cpp/basic.cpp",
468]
469
470gensrcs {
471    name: "jemalloc5_integrationtests_gtestified_srcs",
472    defaults: ["jemalloc5_gtestifier_defaults"],
473    srcs: integration_tests,
474    output_extension: "c",
475}
476
477gensrcs {
478    name: "jemalloc5_cpp_integrationtests_gtestified_srcs",
479    defaults: ["jemalloc5_gtestifier_defaults"],
480    srcs: integration_cpp_tests,
481    output_extension: "cpp",
482}
483
484cc_test {
485    name: "jemalloc5_integrationtests",
486
487    defaults: ["jemalloc5_defaults"],
488
489    cflags: common_cflags + [
490        "-DJEMALLOC_INTEGRATION_TEST",
491        "-DJEMALLOC_NO_RENAME",
492        "-include jemalloc_gtest.h",
493    ],
494
495    local_include_dirs: common_c_local_includes + [
496        "test/include",
497        "android/test",
498    ],
499
500    srcs: [
501        ":jemalloc5_integrationtests_gtestified_srcs",
502        ":jemalloc5_cpp_integrationtests_gtestified_srcs",
503    ],
504
505    static_libs: [
506        "libjemalloc5_integrationtest",
507        "libgtestifier",
508    ],
509
510    target: {
511        linux_glibc: {
512            // The sanitizer does not work for these tests on the host.
513            sanitize: {
514                never: true,
515            },
516        },
517    },
518
519    // Needed for basic.cpp test.
520    stl: "libc++_static",
521}
522
523//-----------------------------------------------------------------------
524// jemalloc stress test library
525//-----------------------------------------------------------------------
526cc_library_static {
527    name: "libjemalloc5_stresstestlib",
528
529    defaults: ["jemalloc5_defaults"],
530
531    cflags: [
532        "-DJEMALLOC_STRESS_TEST",
533        "-DJEMALLOC_STRESS_TESTLIB",
534    ],
535
536    local_include_dirs: [
537        "test/include",
538    ],
539
540    srcs: jemalloc5_testlib_srcs,
541}
542
543//-----------------------------------------------------------------------
544// jemalloc stress tests
545//-----------------------------------------------------------------------
546// All tests are in the same order as in the original jemalloc Makefile
547// to make it easier to track changes.
548stress_tests = [
549    "test/stress/microbench.c",
550]
551
552gensrcs {
553    name: "jemalloc5_stresstests_gtestified_srcs",
554    defaults: ["jemalloc5_gtestifier_defaults"],
555    srcs: stress_tests,
556    output_extension: "c",
557}
558
559cc_test {
560    name: "jemalloc5_stresstests",
561
562    defaults: ["jemalloc5_defaults"],
563
564    cflags: common_cflags + [
565        "-DJEMALLOC_STRESS_TEST",
566        "-include jemalloc_gtest.h",
567    ],
568
569    local_include_dirs: common_c_local_includes + [
570        "test/include",
571        "android/test",
572    ],
573
574    srcs: [":jemalloc5_stresstests_gtestified_srcs"],
575
576    static_libs: [
577        "libjemalloc5",
578        "libjemalloc5_stresstestlib",
579        "libjemalloc5_jet",
580        "libgtestifier",
581    ],
582
583    target: {
584        linux_glibc: {
585            // The sanitizer does not work for these tests on the host.
586            sanitize: {
587                never: true,
588            },
589        },
590    },
591
592    stl: "libc++",
593}
594