timerlist.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # SPDX-License-Identifier: GPL-2.0
  2. #
  3. # Copyright 2019 Google LLC.
  4. import binascii
  5. import gdb
  6. from linux import constants
  7. from linux import cpus
  8. from linux import rbtree
  9. from linux import utils
  10. timerqueue_node_type = utils.CachedType("struct timerqueue_node").get_type()
  11. hrtimer_type = utils.CachedType("struct hrtimer").get_type()
  12. def ktime_get():
  13. """Returns the current time, but not very accurately
  14. We can't read the hardware timer itself to add any nanoseconds
  15. that need to be added since we last stored the time in the
  16. timekeeper. But this is probably good enough for debug purposes."""
  17. tk_core = gdb.parse_and_eval("&tk_core")
  18. return tk_core['timekeeper']['tkr_mono']['base']
  19. def print_timer(rb_node, idx):
  20. timerqueue = utils.container_of(rb_node, timerqueue_node_type.pointer(),
  21. "node")
  22. timer = utils.container_of(timerqueue, hrtimer_type.pointer(), "node")
  23. function = str(timer['function']).split(" ")[1].strip("<>")
  24. softexpires = timer['_softexpires']
  25. expires = timer['node']['expires']
  26. now = ktime_get()
  27. text = " #{}: <{}>, {}, ".format(idx, timer, function)
  28. text += "S:{:02x}\n".format(int(timer['state']))
  29. text += " # expires at {}-{} nsecs [in {} to {} nsecs]\n".format(
  30. softexpires, expires, softexpires - now, expires - now)
  31. return text
  32. def print_active_timers(base):
  33. curr = base['active']['next']['node']
  34. curr = curr.address.cast(rbtree.rb_node_type.get_type().pointer())
  35. idx = 0
  36. while curr:
  37. yield print_timer(curr, idx)
  38. curr = rbtree.rb_next(curr)
  39. idx += 1
  40. def print_base(base):
  41. text = " .base: {}\n".format(base.address)
  42. text += " .index: {}\n".format(base['index'])
  43. text += " .resolution: {} nsecs\n".format(constants.LX_hrtimer_resolution)
  44. text += " .get_time: {}\n".format(base['get_time'])
  45. if constants.LX_CONFIG_HIGH_RES_TIMERS:
  46. text += " .offset: {} nsecs\n".format(base['offset'])
  47. text += "active timers:\n"
  48. text += "".join([x for x in print_active_timers(base)])
  49. return text
  50. def print_cpu(hrtimer_bases, cpu, max_clock_bases):
  51. cpu_base = cpus.per_cpu(hrtimer_bases, cpu)
  52. jiffies = gdb.parse_and_eval("jiffies_64")
  53. tick_sched_ptr = gdb.parse_and_eval("&tick_cpu_sched")
  54. ts = cpus.per_cpu(tick_sched_ptr, cpu)
  55. text = "cpu: {}\n".format(cpu)
  56. for i in range(max_clock_bases):
  57. text += " clock {}:\n".format(i)
  58. text += print_base(cpu_base['clock_base'][i])
  59. if constants.LX_CONFIG_HIGH_RES_TIMERS:
  60. fmts = [(" .{} : {} nsecs", 'expires_next'),
  61. (" .{} : {}", 'hres_active'),
  62. (" .{} : {}", 'nr_events'),
  63. (" .{} : {}", 'nr_retries'),
  64. (" .{} : {}", 'nr_hangs'),
  65. (" .{} : {}", 'max_hang_time')]
  66. text += "\n".join([s.format(f, cpu_base[f]) for s, f in fmts])
  67. text += "\n"
  68. if constants.LX_CONFIG_TICK_ONESHOT:
  69. fmts = [(" .{} : {}", 'nohz_mode'),
  70. (" .{} : {} nsecs", 'last_tick'),
  71. (" .{} : {}", 'tick_stopped'),
  72. (" .{} : {}", 'idle_jiffies'),
  73. (" .{} : {}", 'idle_calls'),
  74. (" .{} : {}", 'idle_sleeps'),
  75. (" .{} : {} nsecs", 'idle_entrytime'),
  76. (" .{} : {} nsecs", 'idle_waketime'),
  77. (" .{} : {} nsecs", 'idle_exittime'),
  78. (" .{} : {} nsecs", 'idle_sleeptime'),
  79. (" .{}: {} nsecs", 'iowait_sleeptime'),
  80. (" .{} : {}", 'last_jiffies'),
  81. (" .{} : {}", 'next_timer'),
  82. (" .{} : {} nsecs", 'idle_expires')]
  83. text += "\n".join([s.format(f, ts[f]) for s, f in fmts])
  84. text += "\njiffies: {}\n".format(jiffies)
  85. text += "\n"
  86. return text
  87. def print_tickdevice(td, cpu):
  88. dev = td['evtdev']
  89. text = "Tick Device: mode: {}\n".format(td['mode'])
  90. if cpu < 0:
  91. text += "Broadcast device\n"
  92. else:
  93. text += "Per CPU device: {}\n".format(cpu)
  94. text += "Clock Event Device: "
  95. if dev == 0:
  96. text += "<NULL>\n"
  97. return text
  98. text += "{}\n".format(dev['name'])
  99. text += " max_delta_ns: {}\n".format(dev['max_delta_ns'])
  100. text += " min_delta_ns: {}\n".format(dev['min_delta_ns'])
  101. text += " mult: {}\n".format(dev['mult'])
  102. text += " shift: {}\n".format(dev['shift'])
  103. text += " mode: {}\n".format(dev['state_use_accessors'])
  104. text += " next_event: {} nsecs\n".format(dev['next_event'])
  105. text += " set_next_event: {}\n".format(dev['set_next_event'])
  106. members = [('set_state_shutdown', " shutdown: {}\n"),
  107. ('set_state_periodic', " periodic: {}\n"),
  108. ('set_state_oneshot', " oneshot: {}\n"),
  109. ('set_state_oneshot_stopped', " oneshot stopped: {}\n"),
  110. ('tick_resume', " resume: {}\n")]
  111. for member, fmt in members:
  112. if dev[member]:
  113. text += fmt.format(dev[member])
  114. text += " event_handler: {}\n".format(dev['event_handler'])
  115. text += " retries: {}\n".format(dev['retries'])
  116. return text
  117. def pr_cpumask(mask):
  118. nr_cpu_ids = 1
  119. if constants.LX_NR_CPUS > 1:
  120. nr_cpu_ids = gdb.parse_and_eval("nr_cpu_ids")
  121. inf = gdb.inferiors()[0]
  122. bits = mask['bits']
  123. num_bytes = (nr_cpu_ids + 7) / 8
  124. buf = utils.read_memoryview(inf, bits, num_bytes).tobytes()
  125. buf = binascii.b2a_hex(buf)
  126. if type(buf) is not str:
  127. buf=buf.decode()
  128. chunks = []
  129. i = num_bytes
  130. while i > 0:
  131. i -= 1
  132. start = i * 2
  133. end = start + 2
  134. chunks.append(buf[start:end])
  135. if i != 0 and i % 4 == 0:
  136. chunks.append(',')
  137. extra = nr_cpu_ids % 8
  138. if 0 < extra <= 4:
  139. chunks[0] = chunks[0][0] # Cut off the first 0
  140. return "".join(chunks)
  141. class LxTimerList(gdb.Command):
  142. """Print /proc/timer_list"""
  143. def __init__(self):
  144. super(LxTimerList, self).__init__("lx-timerlist", gdb.COMMAND_DATA)
  145. def invoke(self, arg, from_tty):
  146. hrtimer_bases = gdb.parse_and_eval("&hrtimer_bases")
  147. max_clock_bases = gdb.parse_and_eval("HRTIMER_MAX_CLOCK_BASES")
  148. text = "Timer List Version: gdb scripts\n"
  149. text += "HRTIMER_MAX_CLOCK_BASES: {}\n".format(max_clock_bases)
  150. text += "now at {} nsecs\n".format(ktime_get())
  151. for cpu in cpus.each_online_cpu():
  152. text += print_cpu(hrtimer_bases, cpu, max_clock_bases)
  153. if constants.LX_CONFIG_GENERIC_CLOCKEVENTS:
  154. if constants.LX_CONFIG_GENERIC_CLOCKEVENTS_BROADCAST:
  155. bc_dev = gdb.parse_and_eval("&tick_broadcast_device")
  156. text += print_tickdevice(bc_dev, -1)
  157. text += "\n"
  158. mask = gdb.parse_and_eval("tick_broadcast_mask")
  159. mask = pr_cpumask(mask)
  160. text += "tick_broadcast_mask: {}\n".format(mask)
  161. if constants.LX_CONFIG_TICK_ONESHOT:
  162. mask = gdb.parse_and_eval("tick_broadcast_oneshot_mask")
  163. mask = pr_cpumask(mask)
  164. text += "tick_broadcast_oneshot_mask: {}\n".format(mask)
  165. text += "\n"
  166. tick_cpu_devices = gdb.parse_and_eval("&tick_cpu_device")
  167. for cpu in cpus.each_online_cpu():
  168. tick_dev = cpus.per_cpu(tick_cpu_devices, cpu)
  169. text += print_tickdevice(tick_dev, cpu)
  170. text += "\n"
  171. gdb.write(text)
  172. LxTimerList()