How to compute the amount of time a thread has been running on cpu

From: Amit kumar <>
Date: Wed, 18 Aug 2021 09:43:14 UTC
I am trying to figure out how long a thread(sitting on a particular CPU's real-time RUNQ) has been running on the CPU, (might help if I can collect the runtime stats of the tread across the CPUs in the case it did run on other CPUs too).

I came across td_runtime and ts_runtime.

  ts_slptime = 18008,
  ts_runtime = 414196,

  td_slptick = 0,
  td_blktick = 0,
  td_incruntime = 36984047370,
  td_runtime = 55246215882,
  td_lastcpu = 0,
  td_oncpu = -1,

looking at the code it seems ts_runtime is a decayed sum, but I dint find the same for td_runtime.

Can someone help me find out how long this thread has been running and which paramere is more reliable?

Another question
I see db_show_thread, converts ticks to ms, using the calculation listed below, is this correct or just an approximation?

delta = (u_int)ticks - (u_int)td->td_swinvoltick;
db_printf(" last involuntary switch: %d ms ago\n",
   1000 * delta / hz);