Callgrind / kcachegrind call graph output implies functions call eachother when they don't - profiling

I'm profiling C++ code using callgrind and then visualizing it in kcachegrind.
For example, if my program is:
main() {
function1();
function2();
}
I would expect main() to have two edges, one to function1() and everything called by function1(), and one to function2() and everything called by function2().
What I instead get is main() has an edge to function1(), and function1() in turn has an edge to function2(). This just seems wrong to me, can someone elaborate on why this is?

Related

How to avoid coding same code twice in (if, if else) statement, when the code has to run if one is true?

Consider this pseudo code:
if(p){
foo()
bar1()
}
if else(q){
foo()
bar2()
}
Is there a way of avoiding writing the call for function foo() twice? I could write:
if(p||q){
foo()
}
...
But is that the only solution?
Keep in mind there is no problem with calling the same function from both if and else, one of the reasons we do function it's reusability and that is proper way to reuse it.
One of the options to avoid calling the same function:
if(p||q){
foo();
if (p){
bar1();
}
else {
bar2();
}
}
Also remember that readability of the code is VERY important and although it's opinion based, I think you way is clearer

Parallelizing C-Code Module in C++ Program

My situation:
I have C code running on a microcontroller. To test this code I have written a test program in C++ that checks the C-functions. Since the test functions are very slow, I wanted to do the whole thing in parallel. However, I don't have much experience.
For example, I have a program module in C that looks like this:
/* c-code: */
static int a=0;
void set_a(int value){
a = value;
}
void inc_a(void){
a++;
}
int get_a(void){
return a;
}
Now I want to parallelize these functions in C++. However, I am bothered by the global variable a, which cannot be avoided in my situation.
In the QT environment I want to perform an "asynchronous run" of the function inc_a. This works but does not improve:
int foo(int somevalue){
set_a(somevalue);
inc_a();
return get_a();
}
int myinput = 1,myoutput;
QFuture<int> future = QtConcurrent::run(foo,myinput);
future.waitForFinished();
myoutput = future.result();
This is what I want:
int myinput1 = 1,myoutput1;
int myinput2 = 8,myoutput2;
QFuture<int> future1 = QtConcurrent::run(foo,myinput1);
QFuture<int> future2 = QtConcurrent::run(foo,myinput2);
future1.waitForFinished();
future2.waitForFinished();
myoutput1 = future1.result();
myoutput2 = future2.result();
So my first question is (to be sure): is it correct that the variable a (in C) is now the same in both threads? If not, I have to look over my code again.If yes, how do I solve the problem as elegantly as possible? I thought of creating two C-program modules with the same functionality. However, this makes the program very maintenance-unfriendly:
/* c-code: */
static int a1=0;
void set_a1(int value){
a1 = value;
}
void inc_a1(void){
a1++;
}
int get_a1(void){
return a1;
}
static int a2=0;
void set_a2(int value){
a2 = value;
}
void inc_a2(void){
a2++;
}
int get_a2(void){
return a2;
}
Is there a better way?
You are out of luck.
Ideally, rewrite your testable asset so that it carries round a state struct containing all those pesky globals, and maybe you will get away with it.
Vroomfondel also suggests that wrapping the offending C code in a namespace might hide the issue, if the code can be made to compile as C++.
You could create as many namespaces as you want parallel threads:
namespace TEST1
{
#include "offender.c"
}
namespace TEST2
{
#include "offender.c"
}
RetCode DoTest(int instance, TestId testid)
{
switch (instance)
{
case 1: return TEST1::DoTest(testid);
case 2: return TEST2::DoTest(testid);
}
return OUT_OF_RANGE;
}
If your target really uses global state and can't be changed, then you could consider using forks.
In a fork, a complete copy of the current state is made for the child to run in, and they both resume with just enough info so you know which is the child and which is the owner. You can also set up a pipe for them to communicate with each other. When a test completes, it transmits its status and exits its forked process.
Forks can be really good for test suites because each fork starts with a completely clean environment.
There is a /lot/ more to getting forking right than I think is reasonable to put as an answer to this question.
The third option is to drive the program externally, so that some monitor script or program launches multiple parallel instances that each run linearly through a subset of the test list. Ideally build in features so the monitor can dispatch tests on demand and load-balance.

Main function for a child process

I have a small but confusing problem...
First question is what is the use of main.I know the question is silly and simple but i have a problem.I have written a code
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
int main(){
pid_t ty;
ty=fork();
if(ty==0){
const char* x="/home/brucewilson/Desktop/jack_sahoo_teja_CDP/hey2";
static char *argv[]={"echo","Foo is my name.",NULL};
int main(){//observe this is second main in my child process
printf("hello");
}
int add(){
printf("5");
}
main();
add();
}
}`
Did you observe the second main function i used in my child process well the compiler gave me no error for this..Added to this it gave me the output as "hello" and 5.
And obviously the below code would give an error...
int main(){
printf("main");
main();
}
int main(){
}
So my question is why did it work for the child process?So is the notion that im assuming to be true that is no function can be named main() and every child process will have a main function shared from its parent is false.Please explain me what is going underneath this code inside my system because the child process assumes main just as another function and it doesnot need a main function also.Then how will the child process know from where should it start?
You are using a non-standard GCC extension known as 'nested functions'.
You second example fails because you aren't nesting the second definition of main() so it conflicts with the first one.

Performance function calls per frame

I'm a game developer therefore performance is really important to me.
My simple question:
I have a lot of checks(button clicks,collisions,whatever) running per frame, but I don't want to put everything in one function, therefore I would split them into other functions and just call them:
void Tick()
{
//Check 1 ..... lots of code
//Check 2 ...... lots of code
//Check 3 ..... lots of code
}
to
void Tick()
{
funcCheck1();
funcCheck2();
funcCheck3();
}
void funcCheck1()
{
//check1 lots of code
}
void funcCheck2()
{
//check2 lots of code
}
void funcCheck3()
{
//check3 lots of code
}
Does the function call per frame has any performance impact?(not inlined)
Clearly the second version is much more readable.
If you don't pass any complex objects by value, the overhead of calling several functions instead of putting all code in one function should be negligible (e.g.
put function parameters on top of the stack, add space for the return type, jump to the beginning of the called function's code)
You cannot say for sure, specifically that the compiler could inline small function automatically. The only way to be sure is to use a profiler and compare the two scenarios.

Persistent storage across several runs

I was wondering what would be the best solution for having a storage container that does not loose its contents over several execution times (runs) without using input-output to the filesystem or external database.
Say I have a class foo() which stores integers. From main() I want to call a method that adds an integer and the class does not forget about its former contents.
//
// Data storage accross different runs
// This should go into the daemon process
//
#include<iostream>
#include<list>
using namespace std;
class foo {
public:
foo(int add): add(add) {}
void store(int i) {
vec.push_back( i + add);
}
private:
list<int> vec;
int add;
};
The main function should check for an already running daemon - if not starts it.
//
// Main program. Should check whether daemon runs already, if not starts it.
//
void main(int argc, char *argv[]) {
// if (daemon is not running)
// start daemon( some_number )
// call daemon::add( atoi(argv[1]) );
}
How would one do this best with shared libraries or with a daemon process? Storage and caller program are on the same Linux host.
Look at Linux Pipes for interprocess communication.
http://linux.die.net/man/2/pipe
Named pipes is one way. If you want non blocking though you might want to try the message queue route. Here is a link to one of the system calls http://linux.die.net/man/2/msgctl, you can look at the other calls from there.
May consider http://en.wikipedia.org/wiki/Memcached