File: common.bpf

package info (click to toggle)
pcp 7.1.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 252,748 kB
  • sloc: ansic: 1,483,656; sh: 182,366; xml: 160,462; cpp: 83,813; python: 24,980; perl: 18,327; yacc: 6,877; lex: 2,864; makefile: 2,738; awk: 165; fortran: 60; java: 52
file content (219 lines) | stat: -rw-r--r-- 5,816 bytes parent folder | download
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
#
# Common shell routines for testing pmdabpf
#
# Copyright (c) 2021 Netflix.
# Author jkoch@netflix.com
#

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_pmdabpf_check()
{
    [ $PCP_PLATFORM = linux ] || _notrun "BPF test, only works with Linux"
    [ -f /sys/kernel/btf/vmlinux ] || _notrun "BTF not available"
    [ -f $PCP_PMDAS_DIR/bpf/pmdabpf ] || _notrun "bpf PMDA not installed"
}

_pmdabpf_require_kernel_version()
{
    uname -r | awk -F. -v __major=$1 -v __minor=$2 '$1 < __major || ($1 == __major && $2 < __minor) {exit 1}' || \
    _notrun "this test requires kernel $1.$2+"
}

_pmdabpf_require_libbpf_version()
{
    LIBBPF_VERSION=false
    eval `sed -n <$PCP_INC_DIR/builddefs -e '/LIBBPF_VERSION/s/ //gp'`

    echo $LIBBPF_VERSION \
    | awk -F. -v __major=$1 -v __minor=$2 '$1 < __major || ($1 == __major && $2 < __minor) {exit 1}' || \
    _notrun "this test requires libbpf $1.$2+"
}

# try and load the modules as per the config file name passed in as $1
#
_pmdabpf_tryload()
{
    echo "--- start pmdabpf config ---" >>$seq_full
    cat $1 >>$seq_full
    echo "--- end pmdabpf config ---" >>$seq_full

    echo "=== start module load test ===" >>$seq_full
    $sudo $PCP_PMDAS_DIR/bpf/pmdabpf --tryload $1 >$tmp.out 2>&1
    __tryload=$?
    cat $tmp.out >>$seq_full
    if [ $__tryload -eq 0 ]
    then
	echo "=== end module load test (passed) ===" >>$seq_full
    else
	echo "=== end module load test (failed) ===" >>$seq_full
	_notrun "cannot load BPF module(s) for this test, see $seq_full for details"
    fi
}

_pmdabpf_install_filter()
{
    # ignore warnings because PMDA might not be ready yet
    # and therefore it has no values
    sed \
    -e "s/.*pmdabpf.* Info/Info/g" \
    -e "s/[0-9]\+ warnings, //" \
    -e "s/[0-9]\+ metrics and [0-9]\+ values/X metrics and X values/g" \
    #end
}

# install the bpf PMDA using the config file name passed in as $1
#
_pmdabpf_install()
{
    # start from known starting points
    cd $PCP_PMDAS_DIR/bpf
    $sudo ./Remove >/dev/null 2>&1

    # set aside any pre-existing cluster and indom caches
    __indomcache="$PCP_VAR_DIR/config/pmda"
    [ -f $__indomcache/157.0 ] && \
    $sudo mv $__indomcache/157.0 $__indomcache/157.0.$seq
    [ -f $__indomcache/157.1 ] && \
    $sudo mv $__indomcache/157.1 $__indomcache/157.1.$seq

    [ -f $PCP_PMDAS_DIR/bpf/bpf.conf ] && \
    $sudo cp $PCP_PMDAS_DIR/bpf/bpf.conf $PCP_PMDAS_DIR/bpf/bpf.conf.$seq
    $sudo cp $1 $PCP_PMDAS_DIR/bpf/bpf.conf

    echo
    echo "=== bpf agent installation ==="
    $sudo ./Install </dev/null >$tmp.out 2>&1
    cat $tmp.out | _filter_pmda_install | _pmdabpf_install_filter

    echo
}

_pmdabpf_wait_for_metric()
{
    # Wait for the PMDA to load the bpf modules and become ready, 10s max
    __canwait=10
    __i=0
    while [ $__i -lt $__canwait ]
    do
	grep -q 'Info: bpf pmda init complete' $PCP_LOG_DIR/pmcd/bpf.log && break
	sleep 1
	__i=`expr $__i + 1`
    done
    sed -n '/Error:/,$p' $PCP_LOG_DIR/pmcd/bpf.log
    if [ $__i -eq $__canwait ]; then
        echo PMDA was not ready in $__canwait seconds, test failed
        exit
    fi
    echo "=== _pmdabpf_wait_for_metric: after $__i secs, metrics should be available ===" >>$seq_full
    # "should" be available, but now wait up to 5 seconds for values
    # to be seen ...
    #
    __canwait=5
    __i=1
    while [ $__i -le $__canwait ]
    do
	touch $tmp.ok
	pmprobe bpf \
	| while read __metric __numval __foo
	do
	    if [ "$__numval" -eq -12389 ]
	    then
		# PM_ERR_AGAIN ... this is expected (apparently)
		#
		:
	    elif [ "$__numval" -lt 0 ]
	    then
		rm -f $tmp.ok
		if [ $__i -eq $__canwait ]
		then
		    # still bad on last iteration ... report
		    #
		    echo "_pmdabpf_wait_for_metric: Botch: bpf PMDA ready but metric $__metric returns numval $__numval (`pmerr $__numval`)"
		fi
	    fi
	done
	if [ -f $tmp.ok ]
	then
	    # goodness
	    #
	    echo "=== _pmdabpf_wait_for_metric: after another $__i secs, values are available ===" >>$seq_full
	    pminfo -f bpf >>$seq_full
	    return 0
	fi
	sleep 1
	__i=`expr $__i + 1`
    done

    # badness ...
    #
    pminfo -f bpf
    echo "Here's the PMDA log ..."
    cat $PCP_LOG_DIR/pmcd/bpf.log

    return 0
}

_pmdabpf_wait_for_value()
{
    __value_regex=${2:-'.*'}

    sed -n '/Error:/,$p' $PCP_LOG_DIR/pmcd/bpf.log
    for __i in `seq 1 30`; do pminfo -f $1 | grep -q 'inst .* value '$__value_regex && break; sleep 1; done
    if [ $__i -ge 30 ]; then
        echo Could not get a single value, test failed
        exit
    fi
}

_pmdabpf_remove()
{
    cd $PCP_PMDAS_DIR/bpf

    echo
    echo "=== remove bpf agent ===" | tee -a $seq_full
    $sudo ./Remove >$tmp.out 2>&1
    cat $tmp.out >>$seq_full
    _filter_pmda_remove <$tmp.out \
    | sed \
	-e '/ Info: /d' \
    # end
}

_pmdabpf_cleanup()
{
    cat $PCP_LOG_DIR/pmcd/bpf.log >> $seq_full
    # restore any pre-existing BPF configuration file
    if [ -f $PCP_PMDAS_DIR/bpf/bpf.conf.$seq ]; then
        $sudo cp $PCP_PMDAS_DIR/bpf/bpf.conf.$seq $PCP_PMDAS_DIR/bpf/bpf.conf
        $sudo rm $PCP_PMDAS_DIR/bpf/bpf.conf.$seq
    else
        $sudo rm -f $PCP_PMDAS_DIR/bpf/bpf.conf
    fi
    # restore any pre-existing cluster and indom caches
    __indomcache="$PCP_VAR_DIR/config/pmda"
    [ -f $__indomcache/157.0.$seq ] && \
    $sudo mv $__indomcache/157.0.$seq $__indomcache/157.0
    [ -f $__indomcache/157.1.$seq ] && \
    $sudo mv $__indomcache/157.1.$seq $__indomcache/157.1
    # note: _restore_auto_restart pmcd done in _cleanup_pmda()
    _cleanup_pmda bpf
}

_value_filter_any()
{
    awk '/value .+/ {print "OK"; exit}'
}

_value_filter_nonzero()
{
    awk '/value [1-9][0-9]*/ {print "OK"; exit}'
}

_value_filter_exact()
{
    grep "value $1" > /dev/null && echo OK
}