0

I would like to get instruction count for bubblesort algorithm using Intel PIN. I thought that maybe I could analyse how instruction count increases, when there is more data to sort. But the result varies between executions for the same set of data.

For example few outputs: 1662097, 1811453, 1832990, 1745621

It varies quite a lot. Why isn't it the same? Is there a way to achieve it? How am I supposed to get any useful information from that? There is no way of telling how increase of data set influenced number of instructions, when it isn't even stable for the same execution.

Pintool code (copy-pasted from Pin User Guide):

#include <iostream>
#include <fstream>
#include "pin.H"

ofstream OutFile;

// The running count of instructions is kept here
// make it static to help the compiler optimize docount
static UINT64 icount = 0;

// This function is called before every instruction is executed
VOID docount() { icount++; }

// Pin calls this function every time a new instruction is encountered
VOID Instruction(INS ins, VOID *v)
{
    // Insert a call to docount before every instruction, no arguments are passed
    INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_END);
}

KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
    "o", "inscount.out", "specify output file name");

// This function is called when the application exits
VOID Fini(INT32 code, VOID *v)
{
    // Write to a file since cout and cerr maybe closed by the application
    OutFile.setf(ios::showbase);
    OutFile << "Count " << icount << endl;
    OutFile.close();
}

/* ===================================================================== */
/* Print Help Message                                                    */
/* ===================================================================== */

INT32 Usage()
{
    cerr << "This tool counts the number of dynamic instructions executed" << endl;
    cerr << endl << KNOB_BASE::StringKnobSummary() << endl;
    return -1;
}

/* ===================================================================== */
/* Main                                                                  */
/* ===================================================================== */
/*   argc, argv are the entire command line: pin -t <toolname> -- ...    */
/* ===================================================================== */

int main(int argc, char * argv[])
{
    // Initialize pin
    if (PIN_Init(argc, argv)) return Usage();

    OutFile.open(KnobOutputFile.Value().c_str());

    // Register Instruction to be called to instrument instructions
    INS_AddInstrumentFunction(Instruction, 0);

    // Register Fini to be called when the application exits
    PIN_AddFiniFunction(Fini, 0);

    // Start the program, never returns
    PIN_StartProgram();

    return 0;
}

Bubblesort implementation (nothing extraordinary, intentionally O(n^2)):

void bubblesort(int table[], int size) {
    int i, j, temp;
    for (i = 0; i < size - 1; i++)
    {
        for (j = 0; j < size - 1 - i; j++)
        {
            if (table[j] > table[j + 1])
            {
                temp = table[j + 1];
                table[j + 1] = table[j];
                table[j] = temp;
            }
        }
    }
}
softkdp
  • 3
  • 2
  • 1
    Your question cannot be answered without showing the code of the pintool. The bubblesort implementation may also be required. – Hadi Brais Aug 07 '19 at 20:33
  • 1
    To add to @HadiBrais 's comment, PIN starts very early in the process life (in the system loader) and traces **everything** in the process, including the runtime and external libraries (which is usually not what people want). PIN can be limited to trace a single binary or even a single function (see `RTN_xxx` functions). Different instruction count for a same program and same input is dependent on the state of the system at a `t` time if you start to trace from the system loader (which is the default behavior). – Neitsa Aug 08 '19 at 12:09
  • @Neitsa I would love to limit the trace. I've tried using RTN functions, however I couldn't understand example from PIN User Guide enough to modify it to count my specific function. Have you ever used it in that way? If so, could you give me an example how to do it? – softkdp Aug 08 '19 at 15:44
  • @Neitsa In my program main function calls bubblesort function. When I use RTN to count instructions (as shown here: https://software.intel.com/sites/landingpage/pintool/docs/97619/Pin/html/index.html#ProcInstrCount) in the output I can find main function but there is no routine named "bubblesort". Is it there under some other name or why is it missing? – softkdp Aug 08 '19 at 16:33
  • Did you search function names by PIN_UndecorateSymbolName() ? – Mos Moh Mar 30 '20 at 16:03

0 Answers0