3.0.1 (revision 00fb798b)
|
This is a short example of how to use the OTF2 writing interface. This example is available as source code in the file otf2_writer_example.c . See also otf2_openmp_writer_example.c and otf2_pthread_writer_example.c when writing with multiple threads.
First include the OTF2 header.
For this example an additional include statement is necessary.
Furthermore this example uses a function delivering dummy timestamps. Real world applications will use a timer like clock_gettime.
Define a pre and post flush callback. If no memory is left in OTF2's internal memory buffer or the writer handle is closed a memory buffer flushing routine is triggered. The pre flush callback is triggered right before a buffer flush. It needs to return either OTF2_FLUSH to flush the recorded data to a file or OTF2_NO_FLUSH to suppress flushing data to a file. The post flush callback is triggered right after a memory buffer flush. It has to return a current timestamp which is recorded to mark the time spent in a buffer flush. Note that while the pre flush callback is always called, the post flush callback is only called during measurement when flushing an event file. In particular this means that closing the writer handle will only trigger the pre flush callback, and therefore the pre and post flush callbacks must be independent of each other–it is not permissible to start an operation in the pre flush callback and rely on the post flush callback to complete it. The callbacks are passed via a struct to OTF2.
Now everything is prepared to begin with the main program.
Create new archive handle.
Set the previously defined flush callbacks.
We will operate in a serial context.
Now we can create the event files. Though physical files aren't created yet.
Get a local event writer for location 0.
Write an enter and a leave record for region 0 to the local event writer.
Now close the event writer, before closing the event files collectively.
After we wrote all of the events we close the event files again.
The local definition files are optional, but create them nevertheless to please the reader.
Now write the global definitions by getting a writer object for it.
We need to define the clock used for this trace and the overall timestamp range.
Now we can start writing the referenced definitions, starting with the strings.
Write definition for the code region which was just entered and left to the global definition writer.
Write the system tree including a definition for the location group to the global definition writer.
Write a definition for the location to the global definition writer.
At the end, close the archive and exit.
To compile your program use a command like the following. Note that we need to activate the C99 standard explicitly for GCC.
Now you can link your program with: