Skip to content

Commit 5b9665e

Browse files
authored
Perfcollect Support for Alpine Linux (microsoft#1227)
1 parent 0b23e58 commit 5b9665e

File tree

1 file changed

+135
-70
lines changed

1 file changed

+135
-70
lines changed

src/perfcollect/perfcollect

Lines changed: 135 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,14 @@ InitializeLog()
698698

699699
# The system information.
700700
LogAppend 'Machine info: ' `uname -a`
701-
LogAppend 'perf version:' `$perfcmd --version`
702-
LogAppend 'LTTng version: ' `lttng --version`
701+
if [ "$perfcmd" != "" ]
702+
then
703+
LogAppend 'perf version:' `$perfcmd --version 2>&1`
704+
fi
705+
if [ "$lttngcmd" != "" ]
706+
then
707+
LogAppend 'LTTng version: ' `$lttngcmd --version`
708+
fi
703709
LogAppend
704710
}
705711

@@ -723,32 +729,52 @@ CloseLog()
723729
##
724730
RedText()
725731
{
726-
tput setaf 1
732+
tput=`GetCommandFullPath "tput"`
733+
if [ "$tput" != "" ]
734+
then
735+
$tput setaf 1
736+
fi
727737
}
728738

729739
GreenText()
730740
{
731-
tput setaf 2
741+
tput=`GetCommandFullPath "tput"`
742+
if [ "$tput" != "" ]
743+
then
744+
$tput setaf 2
745+
fi
732746
}
733747

734748
BlueText()
735749
{
736-
tput setaf 6
750+
tput=`GetCommandFullPath "tput"`
751+
if [ "$tput" != "" ]
752+
then
753+
$tput setaf 6
754+
fi
737755
}
738756
YellowText()
739757
{
740-
tput setaf 3
758+
tput=`GetCommandFullPath "tput"`
759+
if [ "$tput" != "" ]
760+
then
761+
$tput setaf 3
762+
fi
741763
}
742764

743765
ResetText()
744766
{
745-
tput sgr0
767+
tput=`GetCommandFullPath "tput"`
768+
if [ "$tput" != "" ]
769+
then
770+
$tput sgr0
771+
fi
746772
}
747773

748774
# $1 == Status message
749775
WriteStatus()
750776
{
751-
LogAppend $*
777+
LogAppend $*
752778
BlueText
753779
echo $1
754780
ResetText
@@ -822,6 +848,7 @@ DiscoverCommands()
822848
lttngcmd=`GetCommandFullPath "lttng"`
823849
zipcmd=`GetCommandFullPath "zip"`
824850
unzipcmd=`GetCommandFullPath "unzip"`
851+
objdumpcmd=`GetCommandFullPath "objdump"`
825852
}
826853

827854
GetCommandFullPath()
@@ -832,6 +859,30 @@ GetCommandFullPath()
832859
######################################
833860
# Prerequisite Installation
834861
######################################
862+
IsAlpine()
863+
{
864+
local alpine=0
865+
local apk=`GetCommandFullPath "apk"`
866+
if [ "$apk" != "" ]
867+
then
868+
alpine=1
869+
fi
870+
871+
echo $alpine
872+
}
873+
874+
InstallPerf_Alpine()
875+
{
876+
# Disallow non-root users.
877+
EnsureRoot
878+
879+
# Install perf
880+
apk add perf --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community
881+
882+
# Install zip and unzip
883+
apk add zip unzip
884+
}
885+
835886
IsRHEL()
836887
{
837888
local rhel=0
@@ -959,6 +1010,9 @@ InstallPerf()
9591010
elif [ "$(IsRHEL)" == "1" ]
9601011
then
9611012
InstallPerf_RHEL
1013+
elif [ "$(IsAlpine)" == "1" ]
1014+
then
1015+
InstallPerf_Alpine
9621016
else
9631017
FatalError "Auto install unsupported for this distribution. Install perf manually to continue."
9641018
fi
@@ -1094,6 +1148,9 @@ InstallLTTng()
10941148
elif [ "$(IsRHEL)" == "1" ]
10951149
then
10961150
InstallLTTng_RHEL
1151+
elif [ "$(IsAlpine)" == "1" ]
1152+
then
1153+
echo "lttng-tools is not available on Alpine Linux, so no lttng-ust events will be collected."
10971154
else
10981155
FatalError "Auto install unsupported for this distribution. Install lttng and lttng-ust packages manually."
10991156
fi
@@ -1112,9 +1169,6 @@ SupportsAutoInstall()
11121169

11131170
EnsurePrereqsInstalled()
11141171
{
1115-
# Discover commands and then determine if they're all present.
1116-
DiscoverCommands
1117-
11181172
# If perf is not installed, then bail, as it is currently required.
11191173
if [ "$perfcmd" == "" ]
11201174
then
@@ -1131,6 +1185,12 @@ EnsurePrereqsInstalled()
11311185
exit 1
11321186
fi
11331187

1188+
# Disable LTTng use if running on Alpine.
1189+
if [ "$(IsAlpine)" == "1" ]
1190+
then
1191+
useLTTng=0
1192+
fi
1193+
11341194
# If LTTng is installed, consider using it.
11351195
if [ "$lttngcmd" == "" ] && [ "$useLTTng" == "1" ]
11361196
then
@@ -1314,9 +1374,9 @@ CreateLTTngSession()
13141374
{
13151375
if [ "$action" == "livetrace" ]
13161376
then
1317-
output=`lttng create --live`
1377+
output=`$lttngcmd create --live`
13181378
else
1319-
output=`lttng create`
1379+
output=`$lttngcmd create`
13201380
fi
13211381

13221382
lttngSessionName=`echo $output | grep -o "Session.*created." | sed 's/\(Session \| created.\)//g'`
@@ -1327,14 +1387,14 @@ SetupLTTngSession()
13271387
{
13281388

13291389
# Setup per-event context information.
1330-
RunSilent "lttng add-context --userspace --type vpid"
1331-
RunSilent "lttng add-context --userspace --type vtid"
1332-
RunSilent "lttng add-context --userspace --type procname"
1333-
RunSilent "lttng add-context --kernel -t pid -t procname"
1390+
RunSilent "$lttngcmd add-context --userspace --type vpid"
1391+
RunSilent "$lttngcmd add-context --userspace --type vtid"
1392+
RunSilent "$lttngcmd add-context --userspace --type procname"
1393+
RunSilent "$lttngcmd add-context --kernel -t pid -t procname"
13341394

13351395
if [ "$action" == "livetrace" ]
13361396
then
1337-
RunSilent "lttng enable-event --userspace --tracepoint DotNETRuntime:EventSource"
1397+
RunSilent "$lttngcmd enable-event --userspace --tracepoint DotNETRuntime:EventSource"
13381398
elif [ "$gcCollectOnly" == "1" ]
13391399
then
13401400
usePerf=0
@@ -1398,20 +1458,20 @@ SetupLTTngSession()
13981458

13991459
DestroyLTTngSession()
14001460
{
1401-
RunSilent "lttng destroy $lttngSessionName"
1461+
RunSilent "$lttngcmd destroy $lttngSessionName"
14021462
}
14031463

14041464
StartLTTngCollection()
14051465
{
14061466
CreateLTTngSession
14071467
SetupLTTngSession
14081468

1409-
RunSilent "lttng start $lttngSessionName"
1469+
RunSilent "$lttngcmd start $lttngSessionName"
14101470
}
14111471

14121472
StopLTTngCollection()
14131473
{
1414-
RunSilent "lttng stop $lttngSessionName"
1474+
RunSilent "$lttngcmd stop $lttngSessionName"
14151475
DestroyLTTngSession
14161476
}
14171477

@@ -1421,7 +1481,7 @@ EnableLTTngEvents()
14211481
args=( "$@" )
14221482
for (( i=0; i<${#args[@]}; i++ ))
14231483
do
1424-
RunSilent "lttng enable-event -s $lttngSessionName -u --tracepoint ${args[$i]}"
1484+
RunSilent "$lttngcmd enable-event -s $lttngSessionName -u --tracepoint ${args[$i]}"
14251485
done
14261486
}
14271487

@@ -1430,7 +1490,7 @@ EnableLTTngKernelEvents()
14301490
args=( "$@" )
14311491
for (( i=0; i<${#args[@]}; i++ ))
14321492
do
1433-
RunSilent "lttng enable-event -s $lttngSessionName -k ${args[$i]}"
1493+
RunSilent "$lttngcmd enable-event -s $lttngSessionName -k ${args[$i]}"
14341494
done
14351495
}
14361496

@@ -1565,9 +1625,6 @@ ProcessCollectedData()
15651625
LogAppend "Skipping ${path}"
15661626
fi
15671627
done
1568-
1569-
WriteStatus "...FINISHED"
1570-
15711628
else
15721629
if [ "$buildidList" != "" ] && [ $writeCrossgenWarning -eq 1 ]
15731630
then
@@ -1580,57 +1637,62 @@ ProcessCollectedData()
15801637

15811638
IFS=$OLDIFS
15821639

1583-
# Create debuginfo files (separate symbols) for all modules in the trace.
1584-
WriteStatus "Saving native symbols"
1640+
WriteStatus "...FINISHED"
15851641

1586-
# Get the list of DSOs with hits in the trace file (those that are actually used).
1587-
# Filter out /tmp/perf-$pid.map files and files that end in .dll.
1588-
local dsosWithHits=`$perfcmd buildid-list --with-hits | grep -v /tmp/perf- | grep -v .dll$`
1589-
for dso in $dsosWithHits
1590-
do
1591-
# Build up tuples of buildid and binary path.
1592-
local processEntry=0
1593-
if [ -f $dso ]
1594-
then
1595-
local pathToBinary=$dso
1596-
processEntry=1
1597-
else
1598-
local buildid=$dso
1599-
pathToBinary=''
1600-
fi
1642+
if [ "$objdumpcmd" != "" ]
1643+
then
1644+
# Create debuginfo files (separate symbols) for all modules in the trace.
1645+
WriteStatus "Saving native symbols"
16011646

1602-
# Once we have a tuple for a binary path that exists, process it.
1603-
if [ "$processEntry" == "1" ]
1604-
then
1605-
# Get the binary name without path.
1606-
local binaryName=`basename $pathToBinary`
1647+
# Get the list of DSOs with hits in the trace file (those that are actually used).
1648+
# Filter out /tmp/perf-$pid.map files and files that end in .dll.
1649+
local dsosWithHits=`$perfcmd buildid-list --with-hits | grep -v /tmp/perf- | grep -v .dll$`
1650+
for dso in $dsosWithHits
1651+
do
1652+
# Build up tuples of buildid and binary path.
1653+
local processEntry=0
1654+
if [ -f $dso ]
1655+
then
1656+
local pathToBinary=$dso
1657+
processEntry=1
1658+
else
1659+
local buildid=$dso
1660+
pathToBinary=''
1661+
fi
16071662

1608-
# Build the debuginfo file name.
1609-
local destFileName=$binaryName.debuginfo
1663+
# Once we have a tuple for a binary path that exists, process it.
1664+
if [ "$processEntry" == "1" ]
1665+
then
1666+
# Get the binary name without path.
1667+
local binaryName=`basename $pathToBinary`
16101668

1611-
# Build the destination directory for the debuginfo file.
1612-
local currentDir=`pwd`
1613-
local destDir=$currentDir/debuginfo/$buildid
1669+
# Build the debuginfo file name.
1670+
local destFileName=$binaryName.debuginfo
16141671

1615-
# Build the full path to the debuginfo file.
1616-
local destPath=$destDir/$destFileName
1672+
# Build the destination directory for the debuginfo file.
1673+
local currentDir=`pwd`
1674+
local destDir=$currentDir/debuginfo/$buildid
16171675

1618-
# Check to see if the DSO contains symbols, and if so, build the debuginfo file.
1619-
local noSymbols=`objdump -t $pathToBinary | grep "no symbols" -c`
1620-
if [ "$noSymbols" == "0" ]
1621-
then
1622-
LogAppend "Generating debuginfo for $binaryName with buildid=$buildid"
1623-
RunSilent "mkdir -p $destDir"
1624-
RunSilent "objcopy --only-keep-debug $pathToBinary $destPath"
1625-
else
1626-
LogAppend "Skipping $binaryName with buildid=$buildid. No symbol information."
1676+
# Build the full path to the debuginfo file.
1677+
local destPath=$destDir/$destFileName
1678+
1679+
# Check to see if the DSO contains symbols, and if so, build the debuginfo file.
1680+
local noSymbols=`$objdumpcmd -t $pathToBinary | grep "no symbols" -c`
1681+
if [ "$noSymbols" == "0" ]
1682+
then
1683+
LogAppend "Generating debuginfo for $binaryName with buildid=$buildid"
1684+
RunSilent "mkdir -p $destDir"
1685+
RunSilent "objcopy --only-keep-debug $pathToBinary $destPath"
1686+
else
1687+
LogAppend "Skipping $binaryName with buildid=$buildid. No symbol information."
1688+
fi
16271689
fi
1628-
fi
1629-
done
1690+
done
16301691

1631-
WriteStatus "...FINISHED"
1692+
WriteStatus "...FINISHED"
1693+
fi
16321694

1633-
WriteStatus "Resolving JIT and R2R symbols."
1695+
WriteStatus "Resolving JIT and R2R symbols"
16341696

16351697
originalFile="perf.data"
16361698
inputFile="perf-jit.data"
@@ -2035,8 +2097,8 @@ then
20352097
exit 0
20362098
fi
20372099

2038-
# Ensure prerequisites are installed.
2039-
EnsurePrereqsInstalled
2100+
# Discover external commands that will be called by this script.
2101+
DiscoverCommands
20402102

20412103
# Initialize the log.
20422104
if [ "$1" != "stop" ]
@@ -2047,6 +2109,9 @@ fi
20472109
# Process arguments.
20482110
ProcessArguments $@
20492111

2112+
# Ensure prerequisites are installed.
2113+
EnsurePrereqsInstalled
2114+
20502115
# Take the appropriate action.
20512116
if [ "$action" == "collect" ] || [ "$action" == "start" ]
20522117
then

0 commit comments

Comments
 (0)