aboutsummaryrefslogtreecommitdiffstats
path: root/vespabase/src/start-cbinaries.sh
blob: 38c081481a61dd0ec4a752b5c9c90576276a18ea (plain) (blame)
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
#!/bin/bash
# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

# BEGIN environment bootstrap section
# Do not edit between here and END as this section should stay identical in all scripts

findpath () {
    myname=${0}
    mypath=${myname%/*}
    myname=${myname##*/}
    empty_if_start_slash=${mypath%%/*}
    if [ "${empty_if_start_slash}" ]; then
        mypath=$(pwd)/${mypath}
    fi
    if [ "$mypath" ] && [ -d "$mypath" ]; then
        return
    fi
    mypath=$(pwd)
    if [ -f "${mypath}/${myname}" ]; then
        return
    fi
    echo "FATAL: Could not figure out the path where $myname lives from $0"
    exit 1
}

COMMON_ENV=libexec/vespa/common-env.sh

source_common_env () {
    if [ "$VESPA_HOME" ] && [ -d "$VESPA_HOME" ]; then
        export VESPA_HOME
        common_env=$VESPA_HOME/$COMMON_ENV
        if [ -f "$common_env" ]; then
            . $common_env
            return
        fi
    fi
    return 1
}

findroot () {
    source_common_env && return
    if [ "$VESPA_HOME" ]; then
        echo "FATAL: bad VESPA_HOME value '$VESPA_HOME'"
        exit 1
    fi
    if [ "$ROOT" ] && [ -d "$ROOT" ]; then
        VESPA_HOME="$ROOT"
        source_common_env && return
    fi
    findpath
    while [ "$mypath" ]; do
        VESPA_HOME=${mypath}
        source_common_env && return
        mypath=${mypath%/*}
    done
    echo "FATAL: missing VESPA_HOME environment variable"
    echo "Could not locate $COMMON_ENV anywhere"
    exit 1
}

findhost () {
    if [ "${VESPA_HOSTNAME}" = "" ]; then
        VESPA_HOSTNAME=$(vespa-detect-hostname || hostname -f || hostname || echo "localhost") || exit 1
    fi
    validate="${VESPA_HOME}/bin/vespa-validate-hostname"
    if [ -f "$validate" ]; then
        "$validate" "${VESPA_HOSTNAME}" || exit 1
    fi
    export VESPA_HOSTNAME
}

findroot
findhost

ROOT=${VESPA_HOME%/}
export ROOT

# END environment bootstrap section

bname=`basename $0`
no_valgrind=true
use_callgrind=false

export STD_THREAD_PREVENT_TRY_CATCH=true
export GLIBCXX_FORCE_NEW=1
unset LD_PRELOAD

check_bname_in_value () {
    if [ "$1" = "all" ]; then
       return 0
    fi
    for f in $1; do
       if [ "$f" = "$bname" ]; then
           return 0
       fi
    done
    return 1
}

configure_valgrind () {
    no_valgrind=true
    if which valgrind >/dev/null 2>&1; then
        if check_bname_in_value "$VESPA_USE_VALGRIND"; then
            no_valgrind=false
            valgrind_log=${VESPA_HOME}/tmp/valgrind.$bname.log.$$
            case $VESPA_VALGRIND_OPT in
                *callgrind*) use_callgrind=true;;
            esac
        fi
    fi
}

configure_huge_pages () {
    if check_bname_in_value "$VESPA_USE_HUGEPAGES_LIST"; then
        log_debug_message "Want huge pages for '$bname' since VESPA_USE_HUGEPAGES_LIST=${VESPA_USE_HUGEPAGES_LIST}"
        export VESPA_USE_HUGEPAGES="yes"
    fi
}

configure_use_madvise () {
    for f in $VESPA_USE_MADVISE_LIST
    do
        # log_debug_message "Testing '$f'"
        app=`echo $f | cut -d '=' -f1`
        limit=`echo $f | cut -d '=' -f2`
        if [ "$app" = "$bname" ]; then
            log_debug_message "I ($bname) shall use madvise with limit $limit, as set in VESPA_USE_MADVISE_LIST"
            export VESPA_MALLOC_MADVISE_LIMIT="$limit"
            break
        fi

        if [ "$app" = "all" ]; then
            log_debug_message "I shall use madvise with limit $limit, as VESPA_USE_MADVISE_LIST is 'all'"
            export VESPA_MALLOC_MADVISE_LIMIT="$limit"
        fi
    done
}

configure_vespa_malloc () {
    if check_bname_in_value "$VESPA_USE_NO_VESPAMALLOC"; then
        # log_debug_message "Not using vespamalloc for '$bname' since VESPA_USE_NO_VESPAMALLOC=${VESPA_USE_NO_VESPAMALLOC}"
        return
    fi
    suf=vespa/malloc/libvespamalloc.so
    if check_bname_in_value "$VESPA_USE_VESPAMALLOC_D"; then
        suf=vespa/malloc/libvespamallocd.so
    fi
    if check_bname_in_value "$VESPA_USE_VESPAMALLOC_DST"; then
        suf=vespa/malloc/libvespamallocdst16.so
    fi

    if $no_valgrind || $use_callgrind; then
        # should really check that format is same as binary below
        for pre in lib64 lib; do
            tryfile="${VESPA_HOME}/${pre}/${suf}"
            if [ -f "$tryfile" ]; then
                LD_PRELOAD="$tryfile"
                if [ "$VESPA_LOAD_CODE_AS_HUGEPAGES" ]; then
                    LD_PRELOAD="$LD_PRELOAD:${VESPA_HOME}/${pre}/vespa/malloc/libvespa_load_as_huge.so"
                fi
                if [ "$VESPA_USE_HUGEPAGES" ]; then
                    export VESPA_MALLOC_HUGEPAGES="$VESPA_USE_HUGEPAGES"
                    log_debug_message "enabling hugepages for '$0-bin'."
                fi
                break
            fi
        done
    fi
}

configure_valgrind
configure_huge_pages
configure_use_madvise
configure_vespa_malloc

log_debug_message "Using LD_PRELOAD='$LD_PRELOAD'"

if $no_valgrind ; then
    numactl=$(get_numa_ctl_cmd)
    ulimit -c unlimited
    log_debug_message "Starting $0 with : " \
         $numactl env LD_PRELOAD=$LD_PRELOAD $0-bin "$@"
    exec $numactl env LD_PRELOAD=$LD_PRELOAD $0-bin "$@"
else
    if $use_callgrind ; then
        export LD_PRELOAD
    else
        export LD_PRELOAD=""
    fi
    # ignore signal until shared libraries have loaded, etc:
    trap "" SIGTERM
    log_debug_message "Starting $0 with : " \
         valgrind $VESPA_VALGRIND_OPT --log-file="$valgrind_log" $0-bin "$@"
    exec valgrind $VESPA_VALGRIND_OPT --log-file="$valgrind_log" $0-bin "$@"
fi