Engine Profiling: Difference between revisions

From Spring
Jump to navigationJump to search
Hoijui (talk | contribs)
→Post processing oprof data: fixes and simplifications
Hoijui (talk | contribs)
→Post processing oprof data: rework post-processing, add more compleete script
Line 79: Line 79:
=== Post processing ''oprof'' data ===
=== Post processing ''oprof'' data ===


We will first explain the post processing steps, and then give a script that should work for most general spring profiling tasks.
'''Extract the data'''
The data is now stored in ''oprofile''s cache, and we need to extract the parts of it that we are interested in, and write them to a file.
With the settings we used above, ''oprofile'' indexes stuff by thread- and process-ID. In order to generate a report, we will have to tell which program, which threads and which sampling sessions to include.
With the settings we used above, ''oprofile'' indexes stuff by thread- and process-ID. In order to generate a report, we will have to tell which program, which threads and which sampling sessions to include.
If you do not care much, you can just merge thread data and sampling data.
If you do not care much, you can just merge thread data and sampling data, as we do here too.
 


'''Converting the data file to a dot-graph file'''


'''Extract the data'''
[https://code.google.com/p/jrfonseca/wiki/Gprof2Dot gprof2dot] takes ''oprof'' data and outputs a nice dot-graph. We use it to generate the dot graph out of ''oprofile''s <code>--callgraph</code> version.
<code><pre>
SPRING_INSTALL_PATH=~/Projects/spring/installs/cmake/master/linux64/debug
BIN_ENGINE=${SPRING_INSTALL_PATH}/spring
BIN_AI_AAI=${SPRING_INSTALL_PATH}/AI/Skirmish/AAI/0.9/libSkirmishAI.so
MY_BINARIES="${BIN_ENGINE} ${BIN_AI_AAI}"
BASE_OUT_FILE_PATH=./springProfilingData


# Caution: this would take a long time!
#          ~ 3h for a spring in-game time of 5min
#        --debug-info \
#          without it, it shrinks to ~ 10s
opreport \
        --long-filenames \
        --callgraph \
        --demangle=smart \
        --merge=tid,tgid \
        --symbols ${MY_BINARIES} \
        --output-file ${BASE_OUT_FILE_PATH}.oprof
</pre></code>


<code>--callgraph</code> tells it to plot a full call-graph table, which can be parsed by other tools.
'''Finalizing (Creating an image out of the graph file)'''
If instead you want a flat CSV table, omit it.


Then you can either use [https://code.google.com/p/jrfonseca/wiki/XDot XDot] to view it directly, or create an image out of the graph file. We will use [http://www.graphviz.org/ GraphViz]'s ''dot'' utility to render to an image.


'''Converting the data file to a dot-graph file'''


[https://code.google.com/p/jrfonseca/wiki/Gprof2Dot gprof2dot] takes ''oprof'' data and outputs a nice dot-graph.
'''Post Processing Script'''
To generate the dot graph out of the <code>--callgraph</code> version, use this command:
<code><pre>
<code><pre>
gprof2dot --format=oprofile --output=${BASE_OUT_FILE_PATH}.dot ${BASE_OUT_FILE_PATH}.oprof
#!/bin/sh
</pre></code>
 
SPRING_INSTALL_PATH=$(dirname $(which spring))
BIN_ENGINE=${SPRING_INSTALL_PATH}/spring
BIN_AIS=$(ls ${SPRING_INSTALL_PATH}/AI/Skirmish/*/*/libSkirmishAI.so)
 
SETTINGS_CREATE_TABLE=0
SETTINGS_CREATE_CALLGRAPH=1
SETTINGS_CREATE_PNG=1
SETTINGS_CREATE_SVG=1
SETTINGS_CLEANUP=1
 
function profileBins() {
BASE_OUT_FILE_PATH=$1
MY_BINARIES="$2"
 
# extract relevant oprof data as table
if [ ${SETTINGS_CREATE_TABLE} == 1 ]; then
opreport \
--long-filenames \
--demangle=smart \
--merge=tid,tgid \
--symbols ${MY_BINARIES} \
--output-file ${BASE_OUT_FILE_PATH}.txt
fi


Then you can either use [https://code.google.com/p/jrfonseca/wiki/XDot XDot] to view it directly, or create an image out of the graph file.
if [ ${SETTINGS_CREATE_CALLGRAPH} == 1 ]; then
# extract relevant oprof data as callgraph
opreport \
--long-filenames \
--callgraph \
--demangle=smart \
--merge=tid,tgid \
--symbols ${MY_BINARIES} \
--output-file ${BASE_OUT_FILE_PATH}.oprof


# create graph file
gprof2dot --format=oprofile --output=${BASE_OUT_FILE_PATH}.dot ${BASE_OUT_FILE_PATH}.oprof


'''Createing an image out of the graph file'''
# create images
if [ ${SETTINGS_CREATE_PNG} == 1 ]; then
dot -Tpng ${BASE_OUT_FILE_PATH}.dot > ${BASE_OUT_FILE_PATH}.png
fi
if [ ${SETTINGS_CREATE_SVG} == 1 ]; then
dot -Tsvg ${BASE_OUT_FILE_PATH}.dot > ${BASE_OUT_FILE_PATH}.svg
fi
# ... likewise for other formats


We will use [http://www.graphviz.org/ GraphViz]'s ''dot'' utility to render to an image.
if [ ${SETTINGS_CLEANUP} == 1 ]; then
rm ${BASE_OUT_FILE_PATH}.oprof
rm ${BASE_OUT_FILE_PATH}.dot
fi
fi
}


#profileBins ./profiling_engine        "${BIN_ENGINE}"
profileBins ./profiling_ais            "${BIN_AIS}"
profileBins ./profiling_engine_and_ais "${BIN_ENGINE} ${BIN_AIS}"
</pre></code>
This will leave you with these graph files:
<code><pre>
<code><pre>
# for PNG
./profiling_ais.png
dot -Tpng ${BASE_OUT_FILE_PATH}.dot > ${BASE_OUT_FILE_PATH}.png
./profiling_ais.svg
# ... or for SVG
./profiling_engine_and_ais.png
dot -Tsvg ${BASE_OUT_FILE_PATH}.dot > ${BASE_OUT_FILE_PATH}.svg
./profiling_engine_and_ais.svg
# .. and likewise for other formats
</pre></code>
</pre></code>
The result will look about like this:
http://wiki.jrfonseca.googlecode.com/hg/xdot-profile2.png


== Windows ==
== Windows ==

Revision as of 20:57, 9 December 2010

Development < Engine Profiling

Engine Profiling

Linux

Intro

There are several ways to profile an application.

gprof

The compiler injects special code in the binary that self profiles it when it runs. this is what cmake -DCMAKE_BUILD_TYPE=PROFILING is here for. We are not going to use this. It does not interfere with normal program's work-flow. With spring, it gets so slow that it is unusable for profiling anything in late game (which is where it is needed the most). It requires special compiling options.


oprof

OProfile uses statistical sampling. It is a kernel module, which every n clock ticks, registers which function is currently being processed. It is lightweight; it may run with almost 0 performance impact. This method always profiles your profiles your whole system; relevant info has to be filtered out after the profiling run has finished. The downside of statistical profiling, is that it requires at least a certain sample size to achieve good accuracy. Also remember, that oprof still requires the debug symbols, to resolve addresses into function names. It can be turned on/off at any time, so for instance, you may skip spring loading so you would only profile in-game runtime. You can also merge multiple profiling sessions.

Setting up oprof

Required stuff:

  • oprof kernel module
  • oprof package

Using oprof

Note: Modern CPUs have special statistical sampling timing functions to make profiling with software like oprof fast.

You will have to load the kernel module. The profiling daemon is controlled using opcontrol as root. As regular user, you can do post-processing using opreport.


Recommended settings

  • use a call-graph depth of size 16
  • disable kernel profiling & enable user-space profiling
  • separate each application in its own profile
  • separate each thread in its own profile; you can always merge them later, using opreport


A typical profiling session

# We need to be root
su -

# In case the kernel module is not already loaded ...
insmod oprofile

# This has to be sent only once; it will be saved
opcontrol --event=CPU_CLK_UNHALTED:1000000:0x0:0:1 \
          --callgraph=16 \
          --separate=thread,library \
          --no-vmlinux

# -- Bring spring into a state where you want to start profiling --

# start the profiler
opcontrol --start

# -- In spring, do whatever you want to profile --

# stop the profiler
opcontrol --stop

# in order to make the profile data available to user-space,
# you have to dump the data
opcontrol --dump


Post processing oprof data

We will first explain the post processing steps, and then give a script that should work for most general spring profiling tasks.


Extract the data

The data is now stored in oprofiles cache, and we need to extract the parts of it that we are interested in, and write them to a file. With the settings we used above, oprofile indexes stuff by thread- and process-ID. In order to generate a report, we will have to tell which program, which threads and which sampling sessions to include. If you do not care much, you can just merge thread data and sampling data, as we do here too.


Converting the data file to a dot-graph file

gprof2dot takes oprof data and outputs a nice dot-graph. We use it to generate the dot graph out of oprofiles --callgraph version.


Finalizing (Creating an image out of the graph file)

Then you can either use XDot to view it directly, or create an image out of the graph file. We will use GraphViz's dot utility to render to an image.


Post Processing Script

#!/bin/sh

SPRING_INSTALL_PATH=$(dirname $(which spring))
BIN_ENGINE=${SPRING_INSTALL_PATH}/spring
BIN_AIS=$(ls ${SPRING_INSTALL_PATH}/AI/Skirmish/*/*/libSkirmishAI.so)

SETTINGS_CREATE_TABLE=0
SETTINGS_CREATE_CALLGRAPH=1
SETTINGS_CREATE_PNG=1
SETTINGS_CREATE_SVG=1
SETTINGS_CLEANUP=1

function profileBins() {
	BASE_OUT_FILE_PATH=$1
	MY_BINARIES="$2"

	# extract relevant oprof data as table
	if [ ${SETTINGS_CREATE_TABLE} == 1 ]; then
		opreport \
				--long-filenames \
				--demangle=smart \
				--merge=tid,tgid \
				--symbols ${MY_BINARIES} \
				--output-file ${BASE_OUT_FILE_PATH}.txt
	fi

	if [ ${SETTINGS_CREATE_CALLGRAPH} == 1 ]; then
		# extract relevant oprof data as callgraph
		opreport \
				--long-filenames \
				--callgraph \
				--demangle=smart \
				--merge=tid,tgid \
				--symbols ${MY_BINARIES} \
				--output-file ${BASE_OUT_FILE_PATH}.oprof

		# create graph file
		gprof2dot --format=oprofile --output=${BASE_OUT_FILE_PATH}.dot ${BASE_OUT_FILE_PATH}.oprof

		# create images
		if [ ${SETTINGS_CREATE_PNG} == 1 ]; then
			dot -Tpng ${BASE_OUT_FILE_PATH}.dot > ${BASE_OUT_FILE_PATH}.png
		fi
		if [ ${SETTINGS_CREATE_SVG} == 1 ]; then
			dot -Tsvg ${BASE_OUT_FILE_PATH}.dot > ${BASE_OUT_FILE_PATH}.svg
		fi
		# ... likewise for other formats

		if [ ${SETTINGS_CLEANUP} == 1 ]; then
			rm ${BASE_OUT_FILE_PATH}.oprof
			rm ${BASE_OUT_FILE_PATH}.dot
		fi
	fi
}

#profileBins ./profiling_engine         "${BIN_ENGINE}"
profileBins ./profiling_ais            "${BIN_AIS}"
profileBins ./profiling_engine_and_ais "${BIN_ENGINE} ${BIN_AIS}"

This will leave you with these graph files:

./profiling_ais.png
./profiling_ais.svg
./profiling_engine_and_ais.png
./profiling_engine_and_ais.svg

Windows

<Please add>