MINGW-packages icon indicating copy to clipboard operation
MINGW-packages copied to clipboard

-pg instrumented program has empty gprof output

Open NeomerArcana opened this issue 3 years ago • 6 comments

I've also written everything up here https://stackoverflow.com/questions/67240792/gprof-producing-no-output-for-a-program-that-takes-reasonable-time-to-execute so that someone finding this issue later will know they're connected.

The program code:

    //test_gprof.c
    #include<stdio.h>

    void new_func1(void)
    {
        printf("\n Inside new_func1()\n");
        int i = 0;

        for(;i<0xffffffee;i++);

        return;
    }

    void func1(void)
    {
        printf("\n Inside func1 \n");
        int i = 0;

        for(;i<0xffffffff;i++);
        new_func1();

        return;
    }

    static void func2(void)
    {
        printf("\n Inside func2 \n");
        int i = 0;

        for(;i<0xffffffaa;i++);
        return;
    }

    int main(void)
    {
        printf("\n Inside main()\n");
        int i = 0;

        for(;i<0xffffff;i++);
        func1();
        func2();

        return 0;
    }

I have tried building and compiling either as single or combined steps with both 32 and 64 bit versions of Mingw64 from MSYS2, and tried both GCC and G++. For example:

g++ -g -pg main.cpp -o CPPTESTS-d.exe

The program compiles and executes successfully. After execution, a gmon.out file is generated. Executing gprof to interpret this file:

gprof CPPTests-d.exe gmon.out > gprofoutput.txt

produces the following in gprofoutput.txt:

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  Ts/call  Ts/call  name    

 %         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
		   listing.

calls      the number of times this function was invoked, if
		   this function is profiled, else blank.

 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
	   else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this
	   function is profiled, else blank.

name       the name of the function.  This is the minor sort
		   for this listing. The index shows the location of
	   the function in the gprof listing. If the index is
	   in parenthesis it shows where it would appear in
	   the gprof listing if it were to be printed.

Copyright (C) 2012-2021 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.

			 Call graph (explanation follows)


granularity: each sample hit covers 4 byte(s) no time propagated

index % time    self  children    called     name

 This table describes the call tree of the program, and was sorted by
 the total amount of time spent in each function and its children.

 Each entry in this table consists of several lines.  The line with the
 index number at the left hand margin lists the current function.
 The lines above it list the functions that called this function,
 and the lines below it list the functions this one called.
 This line lists:
	 index	A unique number given to each element of the table.
		Index numbers are sorted numerically.
		The index number is printed next to every function name so
		it is easier to look up where the function is in the table.

	 % time	This is the percentage of the `total' time that was spent
		in this function and its children.  Note that due to
		different viewpoints, functions excluded by options, etc,
		these numbers will NOT add up to 100%.

	 self	This is the total amount of time spent in this function.

	 children	This is the total amount of time propagated into this
		function by its children.

	 called	This is the number of times the function was called.
		If the function called itself recursively, the number
		only includes non-recursive calls, and is followed by
		a `+' and the number of recursive calls.

	 name	The name of the current function.  The index number is
		printed after it.  If the function is a member of a
		cycle, the cycle number is printed between the
		function's name and the index number.


 For the function's parents, the fields have the following meanings:

	 self	This is the amount of time that was propagated directly
		from the function into this parent.

	 children	This is the amount of time that was propagated from
		the function's children into this parent.

	 called	This is the number of times this parent called the
		function `/' the total number of times the function
		was called.  Recursive calls to the function are not
		included in the number after the `/'.

	 name	This is the name of the parent.  The parent's index
		number is printed after it.  If the parent is a
		member of a cycle, the cycle number is printed between
		the name and the index number.

 If the parents of the function cannot be determined, the word
 `<spontaneous>' is printed in the `name' field, and all the other
 fields are blank.

 For the function's children, the fields have the following meanings:

	 self	This is the amount of time that was propagated directly
		from the child into the function.

	 children	This is the amount of time that was propagated from the
		child's children to the function.

	 called	This is the number of times the function called
		this child `/' the total number of times the child
		was called.  Recursive calls by the child are not
		listed in the number after the `/'.

	 name	This is the name of the child.  The child's index
		number is printed after it.  If the child is a
		member of a cycle, the cycle number is printed
		between the name and the index number.

 If there are any cycles (circles) in the call graph, there is an
 entry for the cycle-as-a-whole.  This entry shows who called the
 cycle (as parents) and the members of the cycle (as children.)
 The `+' recursive calls entry shows the number of function calls that
 were internal to the cycle, and the calls entry for each member shows,
 for that member, how many times it was called from other members of
 the cycle.

Copyright (C) 2012-2021 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.

Index by function name

which you can see it's completely empty.

I managed to produce results in gmon.out that were parsed correctly with gprof and gave me timing for all the functions in my test program. Making no changes, not even closing the MSYS2 terminal, I then recompiled the program with the same command, executed it again, and back to having an empty results.

I haven't been able to recreate this behaviour. All I can say for sure is that it worked once without any changes to the compilation process. The program takes significant time to execute, so my understanding is that it should result in some numbers.

It's unclear to me if it's something during compilation (hard to believe, it was compiled using the last command in the terminal history), or something on my machine when I execute it. Or if it's a problem with gprof translating the gmon.out file.

I've executed the program on a different machine (it's a similar Windows 10 Surface Pro) that has never been used for development. It's produced gmon.out also seems to have no results when parsed through gprof.

I've checked that the object files / executable are correctly instrumented by running in the MSYS terminal:

nm CPPTESTS-d.exe | grep mcount

which shows that it at least exists in the executable. I think this has to do with my machine, hence why I executed it elsewhere as well, due to the presence of the mcount symbols. But well, it's not getting me past it.

NeomerArcana avatar Apr 29 '21 03:04 NeomerArcana

I downloaded 8.1.0 of Mingw64-Builds and everything appears to work correctly for it. I can even parse a generated gmon.out from 8.1.0 with the latest MSYS2 and it produces the gprof output correctly. Running a MSYS2 compiled program does not seem to produce correct results in gmon.out for either version of gprof to process.

So it appears to be something in MSYS2 version of G++/GCC, or at least something in more recent versions.

NeomerArcana avatar Apr 29 '21 07:04 NeomerArcana

If you profile with -z all functions will be listed with 0 counts and 0 time spent. So it's definitely a problem in the way that the code is being initially instrumented by the -pg switch.

NeomerArcana avatar Apr 29 '21 10:04 NeomerArcana

This is still an issue with GCC 12.1 unfortunately

20k avatar May 25 '22 07:05 20k

Yeah, I have just run into this. And despite the comment on stackoverflow, gcc 12 doesn't appear to help

ThosRTanner avatar Jun 25 '22 11:06 ThosRTanner

add a -no-pie to the options of the gcc command.

stevenclarke9 avatar Oct 05 '22 05:10 stevenclarke9

-no-pie unfortunately doesn't help here - it used to fix it in an older version of gcc, but it hasn't worked for a long time

20k avatar Oct 05 '22 18:10 20k

I read a recent comment in this discussion on stackoverflow.com which claimed that the long-standing MINGW Gprof bug was fixed in MSYS2 MINGW GCC 12.1.

However, after conducting a test on my own MSYS2 installation, it seems that the claim is not true, at least for MSYS2 MINGW GCC 12.2 as at 14 November 2022.

I compared results of a test of the MINGW GCC 8.1 compiler tools (which work properly) against the current MSYS2 MINGW GCC 12.2 compiler tools (which don't work).

As someone stated earlier, the problem appears to be in the GCC compiler code generation and not in the Gprof utility itself.

PS the suggestion to add compiler option -no-pie also does not work.

mayd avatar Nov 14 '22 06:11 mayd