xref: /aosp_15_r20/external/bc/scripts/alloc.sh (revision 5a6e848804d15c18a0125914844ee4eb0bda4fcf)
1*5a6e8488SAndroid Build Coastguard Worker#!/bin/sh
2*5a6e8488SAndroid Build Coastguard Worker#
3*5a6e8488SAndroid Build Coastguard Worker# SPDX-License-Identifier: BSD-2-Clause
4*5a6e8488SAndroid Build Coastguard Worker#
5*5a6e8488SAndroid Build Coastguard Worker# Copyright (c) 2018-2024 Gavin D. Howard and contributors.
6*5a6e8488SAndroid Build Coastguard Worker#
7*5a6e8488SAndroid Build Coastguard Worker# Redistribution and use in source and binary forms, with or without
8*5a6e8488SAndroid Build Coastguard Worker# modification, are permitted provided that the following conditions are met:
9*5a6e8488SAndroid Build Coastguard Worker#
10*5a6e8488SAndroid Build Coastguard Worker# * Redistributions of source code must retain the above copyright notice, this
11*5a6e8488SAndroid Build Coastguard Worker#   list of conditions and the following disclaimer.
12*5a6e8488SAndroid Build Coastguard Worker#
13*5a6e8488SAndroid Build Coastguard Worker# * Redistributions in binary form must reproduce the above copyright notice,
14*5a6e8488SAndroid Build Coastguard Worker#   this list of conditions and the following disclaimer in the documentation
15*5a6e8488SAndroid Build Coastguard Worker#   and/or other materials provided with the distribution.
16*5a6e8488SAndroid Build Coastguard Worker#
17*5a6e8488SAndroid Build Coastguard Worker# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*5a6e8488SAndroid Build Coastguard Worker# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*5a6e8488SAndroid Build Coastguard Worker# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*5a6e8488SAndroid Build Coastguard Worker# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*5a6e8488SAndroid Build Coastguard Worker# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*5a6e8488SAndroid Build Coastguard Worker# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*5a6e8488SAndroid Build Coastguard Worker# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*5a6e8488SAndroid Build Coastguard Worker# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*5a6e8488SAndroid Build Coastguard Worker# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*5a6e8488SAndroid Build Coastguard Worker# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*5a6e8488SAndroid Build Coastguard Worker# POSSIBILITY OF SUCH DAMAGE.
28*5a6e8488SAndroid Build Coastguard Worker#
29*5a6e8488SAndroid Build Coastguard Worker
30*5a6e8488SAndroid Build Coastguard Worker# This script is only really useful for running on Linux. It tests the code to
31*5a6e8488SAndroid Build Coastguard Worker# free temps in order to make an allocation work. In order to see it work, I
32*5a6e8488SAndroid Build Coastguard Worker# suggest adding code after the following line in src/vm.c:
33*5a6e8488SAndroid Build Coastguard Worker#
34*5a6e8488SAndroid Build Coastguard Worker# if (BC_ERR(ptr == NULL)) bc_vm_fatalError(BC_ERR_FATAL_ALLOC_ERR);
35*5a6e8488SAndroid Build Coastguard Worker#
36*5a6e8488SAndroid Build Coastguard Worker# The code you should add is the following:
37*5a6e8488SAndroid Build Coastguard Worker#
38*5a6e8488SAndroid Build Coastguard Worker# bc_file_printf(&vm.ferr, "If you see this, the code worked.\n");
39*5a6e8488SAndroid Build Coastguard Worker# bc_file_flush(&vm.ferr, bc_flush_none);
40*5a6e8488SAndroid Build Coastguard Worker#
41*5a6e8488SAndroid Build Coastguard Worker# If you do not see the that message printed, the code did not work. Or, in the
42*5a6e8488SAndroid Build Coastguard Worker# case of some allocators, like jemalloc, the allocator just isn't great with
43*5a6e8488SAndroid Build Coastguard Worker# turning a bunch of small allocations into a bigger allocation,
44*5a6e8488SAndroid Build Coastguard Worker
45*5a6e8488SAndroid Build Coastguard Workerscript="$0"
46*5a6e8488SAndroid Build Coastguard Workerscriptdir=$(dirname "$script")
47*5a6e8488SAndroid Build Coastguard Worker
48*5a6e8488SAndroid Build Coastguard Worker. "$scriptdir/functions.sh"
49*5a6e8488SAndroid Build Coastguard Worker
50*5a6e8488SAndroid Build Coastguard Workerexport LANG=C
51*5a6e8488SAndroid Build Coastguard Worker
52*5a6e8488SAndroid Build Coastguard Workervirtlimit=1000000
53*5a6e8488SAndroid Build Coastguard Worker
54*5a6e8488SAndroid Build Coastguard Workerulimit -v $virtlimit
55*5a6e8488SAndroid Build Coastguard Worker
56*5a6e8488SAndroid Build Coastguard Worker# This script is designed to allocate lots of memory with a lot of caching of
57*5a6e8488SAndroid Build Coastguard Worker# numbers (the function f() specifically). Then, it's designed allocate one
58*5a6e8488SAndroid Build Coastguard Worker# large number and grow it until allocation failure (the function g()).
59*5a6e8488SAndroid Build Coastguard Worker"$scriptdir/../bin/bc" <<*EOF
60*5a6e8488SAndroid Build Coastguard Worker
61*5a6e8488SAndroid Build Coastguard Workerdefine f(i, n) {
62*5a6e8488SAndroid Build Coastguard Worker	if (n == 0) return i;
63*5a6e8488SAndroid Build Coastguard Worker	return f(i + 1, n - 1)
64*5a6e8488SAndroid Build Coastguard Worker}
65*5a6e8488SAndroid Build Coastguard Worker
66*5a6e8488SAndroid Build Coastguard Workerdefine g(n) {
67*5a6e8488SAndroid Build Coastguard Worker	t = (10^9)^(2^24)
68*5a6e8488SAndroid Build Coastguard Worker	while (n) {
69*5a6e8488SAndroid Build Coastguard Worker		n *= t
70*5a6e8488SAndroid Build Coastguard Worker		print "success\n"
71*5a6e8488SAndroid Build Coastguard Worker	}
72*5a6e8488SAndroid Build Coastguard Worker}
73*5a6e8488SAndroid Build Coastguard Worker
74*5a6e8488SAndroid Build Coastguard Workeriterations=2000000
75*5a6e8488SAndroid Build Coastguard Worker
76*5a6e8488SAndroid Build Coastguard Workerfor (l=0; l < 100; l++) {
77*5a6e8488SAndroid Build Coastguard Worker	iterations
78*5a6e8488SAndroid Build Coastguard Worker	j = f(0, iterations$)
79*5a6e8488SAndroid Build Coastguard Worker	iterations += 100000
80*5a6e8488SAndroid Build Coastguard Worker	print "here\n"
81*5a6e8488SAndroid Build Coastguard Worker	n=10^235929600
82*5a6e8488SAndroid Build Coastguard Worker	g(n)
83*5a6e8488SAndroid Build Coastguard Worker	print "success\n"
84*5a6e8488SAndroid Build Coastguard Worker	n=0
85*5a6e8488SAndroid Build Coastguard Worker}
86*5a6e8488SAndroid Build Coastguard Worker*EOF
87