I have a c++ program with opencv library which takes an image as input and perform pose estimation,color detection,phog. When I run this program from the command line it takes around 4-5sec to complete. It takes around 60%cpu. When I try to run the same program from two different command line windows at the same time the process takes around 10-15 sec to finish and both the process finish in almost the same time. The CPU Usage reaches upto 100%.

I have a website which calls this c++ exe using exec() command. So when two users try to upload an image and run it takes more time as I explained above in the command line. Is this because the c++ program involves high computation and the CPU reaches 100% it slows down? But I read that the CPU reaching 100% is not a bad thing as the computer is using its full capacity to run the program. So is this because of my c++ program or is it something to do with my server(computer) settings? This is probably not the apache server problem because when I try to run it from the command line also it slows down. I am using a quad core processor and all the 4 CPU reaches 100% when I try to run the same process at the same time so I think that its distributed among all the processor. So I have few more questions:

1) Can this be solved by using multithreading in my c++ code?As for now I am not using it but will multithreading make the c++ code more computationally expensive and increase the CPU usage(if this is the problem).

2) What can be the reason of it slowing down? Is the process in a queue and each process is ran only a certain amount of time and it switches between the two process?

3) If this is because it involves high computation will it help if I change some functions to opencv gpu functions?

4) Is there a way I can solve this problems any ideas or tips?

I have inserted the result of top when running one process and running the same process twice at the same time:

Version5 is the process,running it once enter image description here Two Version5 running at the same time enter image description here

  • Your code is already using multi-threading since it's using more than 100% of CPU time. Disabling multi-threading may actually help if it's a cash trashing issue as processes would run on different processors and therefore have separate caches (L1 at least) – Stéphane Chazelas Feb 20 '14 at 12:43
  • @StephaneChazelas "Cash trashing"? 8-) How do you disable multi-threading? Can that be done at all from outside the application? After all it is possible even on a single-core CPU. – Hauke Laging Feb 20 '14 at 18:01

The reason for that may be cache trashing. CPU load is not just pure computation. It is CPU activity i.e. everything except sleeping and waiting for I/O.

If a single process is running then it can probably use most of the CPU cache(s). If you start a second process then the Kernel may be clever enough to share the code between the two processes but they do not share (most of) the data.

Thus if the relevant (to overall CPU activity) parts of the code use data with high locality and the amount of this data is more than half of the cache size then both processes throw each other's data out of the cache all the time. I.e. the average memory latency increases seriously. Memory access is part of the CPU load.

I am not aware of any possibility to see which share of the accesses has been served by which cache level or by RAM.

Another possibility is that the process need such a big amount of memory that swapping (the "even outer cache...") becomes a problem. But that seems not so probable to me in this case.

  • I am using boost shared memory to load the datas from the database to the memory...do you think this might be a problem? – user1583647 Feb 20 '14 at 8:57
  • Shared memory should never be a problem. You may start one process with highest and one with lowest CPU priority. I expect that the execution time decreases. So should increasing the time after which a process is interrupted by the kernel. I guess that can be configured somewhere but I have no idea where. – Hauke Laging Feb 20 '14 at 9:04
  • @user1583647, it may be a problem if there's something in place to exclude concurrent access to that shared memory and if both processes try to access that memory all the time. – Stéphane Chazelas Feb 20 '14 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.