0

I am using Visual Studio Entreprise 2015.

I want to profile a simple program (basically revisiting What every programmer should know about memory on my processor).

So I created the little program below :

#include <iostream>
#include "Header.h"
using namespace std;


void simpleReadWalk(l* start, int nb) {

    struct l* c = start;
    for (int i = 0; i < nb; i++) {
        c = c->n;
    }
}

int main() {

    int size = sizeof(struct l);
    int wsSize = 1 << 25; // working set size 2^10
    int nb = wsSize / size;

    struct l* start = new struct l;
    struct l* current = start;
    for (int i = 0; i < nb - 1; i++) {
        struct l* nextone = new struct l;
        current->n = nextone;
        current = nextone;
    }

    simpleReadWalk(start, nb);
}

When I launch a instrument profiling, let's say for observing the number of cycles to run the instructions (to be chosen in the performance profiling option), I have the following :

enter image description here

In this picture, I expected to have the function simpleReadWalk where i added a red question mark: I want the CPU counters values for this function and not the entire main !

I must add : I ticked : "add small functions" in the profiler options (which can be seen in the figure below).

What did I miss please ?

Thank you in advance.

Note : in order to have the profiler working I had to run the following as taken here, don't think this is a problem but who knows, and at worse this may help other people ending on that question:

vsperfcmd /admin:driver,install
vsperfcmd /admin:driver,start /admin:service,start
vsperfcmd /admin:security,allow,FullAccess,<myusernamegoeshere>
vsperfcmd /admin:driver,autostart,on

===================================

EDIT 1 :

@norisknofun made a very good remark : the simpleReadWalk method may simply not be linked.

Unfortunately, debug mode is not allowed in instrumentation mode. So it comes down to force the inclusion of the file in release using another trick.

I tried using the trick based on a double pragma instructions as per force visual studio to link all symbols in a lib file

The source code is as follows :

#include <iostream>
#include "Header.h"
#include <stdio.h>

using namespace std;

/**
* Access values in a sequential fashion but using their pointer.
*/
void simpleReadWalk(l start, int nb) {
    #define FORCE_LINK_THIS(x) int force_link_##x = 0;
    struct l c = start;
    for (int i = 0; i < nb; i++) {
        c = *(c.n);
    }
}

/**
* Pack values in a array.
*/
void init(l* tab, int nb) {
    #define FORCE_LINK_THAT(x) { extern int force_link_##x; force_link_##x = 1; }
    for (int i = 0; i < nb; i++) {
        struct l nextone;
        tab[i] = nextone;
        if (i > 0) {
            tab[i - 1].n = &tab[i];
        }
    }
}

int main() {

    const int k = 25; // k like in 2^k
    const int size = sizeof(struct l);
    const int wsSize = 1 << k; // working set size 2^25
    const int nb = wsSize / size;

    struct l * tab = new struct l[nb];

    init(tab, nb);
    simpleReadWalk(tab[0], nb);

}

Unfortunately, this leads to the same results. main and init function are seen, but not the simpleReadWalk function :

enter image description here

Maybe this is not surprising since that tread relates to excluded functions from libraries (and not core source code ?).

Therefore, following https://msdn.microsoft.com/en-us/library/bxwfs976(v=vs.140).aspx I tried to add the following option as follows in VS 2015 :

enter image description here

And this gives the same result : no trace of simpleReadWalk.

At this point :

  • We suspect - but we are not 100 % sure - that simpleReadWalk is not linked. How can we be sure that is excluded ?
  • If simpleReadWalk is excluded, it is not clear why the /OPT:NOREF does not work in release.
Community
  • 1
  • 1
Fafhrd
  • 436
  • 2
  • 6
  • Your are using "Release", so I dont think your method is even linked because `simpleReadWalk` does not do anything. Try in Debug mode – norisknofun Feb 24 '16 at 14:40
  • Try [this](http://stackoverflow.com/questions/599035/force-visual-studio-to-link-all-symbols-in-a-lib-file) to force the linkage of all symbols (even if they are useless) – norisknofun Feb 24 '16 at 14:43
  • This is a very good remark ! Unfortunately, debug mode is not allowed in instrumentation mode. So I went to try to force the linkage of the (possibly) excluded function using the tips in the referred question. The code looks like this now (broke things down to get a better understanding). – Fafhrd Feb 24 '16 at 15:03
  • _The code looks like this now_ : where ? – norisknofun Feb 24 '16 at 15:18
  • Sorry , there has been a SO shortage : it was in R/O and I couldn't modify my comment hence the "half-written reply" (hit return and not "shift-return" for new line a,d SO was N/A...) I'll put new info in the POST instead as comments do not seem the best place to do it. – Fafhrd Feb 24 '16 at 15:22
  • try to add a simple `std::cout << "hello\n";` at the end of simpleReadWalk – norisknofun Feb 24 '16 at 15:39
  • yes ... The profiling report in this case do include both `init` and `simpleReadWalk ` functions. – Fafhrd Feb 24 '16 at 16:21
  • There is actually a tick box for this option in Linker -> Optimization to activate the /OPT:NOREF option. Problem : this seems designed for unreferenced function : "/OPT:REF eliminates functions and data that are never referenced; /OPT:NOREF keeps functions and data that are never referenced." The simpleReadWalk function is indeed referenced in main. I suspect an optimisation about dead code elimination (since it is acutally a dead code). So I desactivate optimisation (from `O2`to `/Od` : no optimation). This soves the problem : I see my simpleReadWrite function in the profiling report ! – Fafhrd Feb 24 '16 at 16:43
  • And I see it takes this function 576 inclusives cycles to sequentially traverse my list of 524288 items which means a totally aberrant figure per list element, but that's another story. Thanks ! – Fafhrd Feb 24 '16 at 16:47
  • @norisknofun Can you write your opinion as an answer so that I can mark your answer as accepted ? (it seems I can't mark a comment as the accepted answer) – Fafhrd Feb 24 '16 at 16:50
  • ok, thanks for proposal – norisknofun Feb 24 '16 at 16:53

1 Answers1

0

You can :

  1. try to force the linkage of all symbols (even if they are useless)
  2. add a simple std::cout << "hello\n"; at the end of simpleReadWalk

As you mentionned yourlself, desactivate optimisation (from O2to /Od : no optimation) can have impact on linkage.

norisknofun
  • 859
  • 1
  • 8
  • 24