Creating labels in programs, C code example – HP XC System 3.x Software User Manual
Page 67
Creating Labels in Programs
You can use a function such as popen() to invoke the hpcpictl label command within an
application and assign a label to specific code areas in the application.
For example, you can profile the execution phase of an application only, without the initialization,
reporting, or finalization phases. This is analogous to benchmarks, which typically report results
for only the execution phase. In addition, you can use different labels to distinguish major phases
of an application, such as initialization and multiple subphases of execution.
Invoke the hpcpictl label command from within the application to assign labels to correspond
to the different application phases. One way to invoke hpcpictl label is to write a C routine
that is similar to the following:
sprintf(command, "/opt/hpcpi/bin/hpcpictl label %s -pgid this /bin/cat",
labelName);
labeler = popen(command, "w");
sleep(1);
In this example, hpcpictl starts a cat process, which is used only to keep the HPCPI label
active.
The sleep() call introduces a delay in the calling process that helps HPCPI partition events
you want to select for the label.
In addition, this example specifies -pgid this as the label selector, which selects all executables
with the same process group ID as the cat process, including the calling program. Alternatively,
you can use specify -pid %d with the return value from getpid() to select only the calling
program.
An application can also use environment variable values to enable or disable the use of HPCPI
labels, and to provide the label name or a portion of the label name.
C Code Example
The following do_hpcpi_label() function was inserted in a C program to control HPCPI
labels. The program was invoked from a shell script as follows:
foreach n ($sizes)
env HPCPI_LABEL=n$n a.out $n
end
The C code is as follows:
#include
#include
:
:
static void
do_hpcpi_label(int start)
{
static FILE * labeler = NULL;
if (labeler) {
pclose(labeler);
labeler = NULL;
sleep(1);
}
if (start) {
const char * label = getenv("HPCPI_LABEL");
if (label) {
char command[1024];
const char * hpcpictl = "/opt/hpcpi/bin/hpcpictl";
sprintf(command,"%s label %s -pid %d /bin/cat",
hpcpictl,label,getpid());
labeler = popen(command, "w");
if (labeler) {
sleep(1);
Creating Labels in Programs
67