2

I want to measure the total time spent in a C function within Linux. The function may be called at the same time from different threads, and the times spent should be summed together. How can I do this measurement from Linux? I have looked at the clock() function and to calculate the difference between the start and end of the function.

I found one solution using clock() in this thread within Stackoverflow: How to measure total time spent in a function?

But from what I understand this will also include the CPU processing from threads executes some other function during the time of measurement. Is that a correct assumption?

Is there some other way to do this measurement within Linux?

Community
  • 1
  • 1
Mansena
  • 31
  • 3
  • "Is there some other way to do this measurement within Linux" **Yes**, clock_gettime with CLOCK_THREAD_CPUTIME_ID. – n. m. could be an AI Apr 05 '17 at 13:22
  • This sounds like an [XY problem.](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) What do you plan on doing with the values you get? Measuring the time spent in a function is usually done with a purpose. What is your purpose? The best way to do that should be your question. – Andrew Henle Apr 05 '17 at 14:23
  • The purpose is the following. I have a process that handles data traffic and then this traffic is delivered to a client via a callback function, i.e. it will execute within the same process. I want to measure how much of the capacity that the server part is using. One way I thought of would be to measure the time spent in the callback function and subtract that from the total execution time of the process. – Mansena Apr 06 '17 at 05:12

3 Answers3

3

Your question states that you are using Linux.

You can use the getrusage(2) system call with the RUSAGE_THREAD parameter, which will give you the accumulated statistics for the currently running thread.

By comparing what's in ru_utime, and perhaps ru_stime also, before and after your function runs, you should be able to determine how much time the function has accumulated in CPU time, for the currently running thread.

Lather, rinse, repeat, for all threads, and add them up.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • This would be a good way to do it but when I tried it I noticed I got the resolution in milliseconds and not microseconds. Then the start time and end time had the same value. – Mansena Apr 06 '17 at 18:17
  • According to http://elinux.org/Kernel_Timer_Systems#Old_timer_wheel.2Fjiffy_replacement_proposals, the Linux kernel tracks CPU time on milliseconds' basis. The Linux kernel simply does not track CPU time at such a high level granularity. – Sam Varshavchik Apr 06 '17 at 18:37
1

A very good tool for performance analysis is perf (available with recent linux kernels):

Record performance data with

perf record <command>

and then analyze it with

perf report

Compile your program with debug symbols for useful results.

Ctx
  • 18,090
  • 24
  • 36
  • 51
0

getting time from from clock() and gettimeofday() family functions are good for obtaining precise time difference between two consequent calls, but not good for obtaining time spent in functions, because of thread and process rescheduling of operating system and IO blocking, there isn't any guarantee which your thread/process could obtain CPU until finishes its operations, so you can't relay on time difference. You have two choice for this

  1. Using profiling softwares such as Intel V-Tune and Intel Inspector which will utilize the hardware performance counters

  2. Using Realtime linux kernel, scheduling your process with FIFO scheduler and use time difference, in FIFO scheduler no one interrupt your program so you can safely use the time difference as time spent in functions, using clock(), gettimeofday() or even more precise rdtsc

e.jahandar
  • 1,715
  • 12
  • 30