1

I'm optimizing the performance of some codes, and I find that the call of func() in third-party shared library is too slow every a few minutes. For example, if I called it for 10 times in a loop, the first call was very slow compared the other calls. A few minutes later I repeated the same operation and got the same result. I suspect that the root cause is cache missing(data and instructions). So if I can load the instructions to cache before calling this function, maybe the first call of this function will be faster than before.

I plan to using memcpy() to access the address of func() before calling it. But the problem is that func() calls other functions which also are defined in shared library, and I can't access the address of those functions.

According to this question: How to make backtrace()/backtrace_symbols() print the function names?, I know backtrace()/backtrace_symbols() can be used to print stack backtrace, but is it possible print stack backtrace of function call in third-party shared library?

BTW: I would appreciate any advice and suggestions about preloading instructions to cache.

Pezo
  • 1,458
  • 9
  • 15
Neal
  • 81
  • 4
  • I think it's called using a debugger, and thus, this isn't about C anymore. As for first calling being too slow, the library needs to be loaded in before it's possible to call anything, and it isn't done until it's required. – Purple Ice Nov 03 '18 at 10:30
  • sorry, I don't express the problem clearly. The problem is that if I called the function for 10 times, the first call would be slow compared to other calls. A few minutes later I called the function for 10 times again, and the first call was still the slowest one. – Neal Nov 03 '18 at 11:34
  • 1
    What you seem to want is not a backtrace, but the other direction (all functions called from `func`, directly or indirectly). – Pezo Nov 03 '18 at 11:35
  • Also, use single backticks for highlighting code instead of HTML tags, like so: \`func()\` – Pezo Nov 03 '18 at 11:38
  • Assuming that you have the source code of the third party library, **you should use a profiler** to see what is slow and then take appropriate action. If you don't have the source code, then ask technical support from the library author. Another possibility might be that your computer does not have enough memory so it mostly reload code from the disk each time. Have you confirmed that it depends on the delay and not each 10 times? Does the library do some cleanup every minute or so because it use a lot of resources and don't want to hold them for an extended period of time? – Phil1970 Nov 03 '18 at 15:02
  • @Phil1970 I don’t have the source code. The memory is big enough(16G) to run the application, and the application doesn’t use too much memory. In my test the first call is 10 times slower than the next 9 same function calls(about 200ns vs 20ns). I can see the function list of the library by ‘nm’ command, and I don’t find clear clues of cleanup function. – Neal Nov 04 '18 at 14:04
  • Have you tried some other tests like calling once every few minutes? Or don't wait between calls? Or call 5 or 20 times instead of 10. The more things you try, the better you will understand what is happening. And even if the application do not used a lot of memory, if you have other big applications that are running, then can have an impact. Do you have a SSD? What kind of operations does the library does? – Phil1970 Nov 04 '18 at 21:45

0 Answers0