process_tree.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. # This file is part of pybootchartgui.
  2. # pybootchartgui is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # pybootchartgui is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with pybootchartgui. If not, see <http://www.gnu.org/licenses/>.
  12. class ProcessTree:
  13. """ProcessTree encapsulates a process tree. The tree is built from log files
  14. retrieved during the boot process. When building the process tree, it is
  15. pruned and merged in order to be able to visualize it in a comprehensible
  16. manner.
  17. The following pruning techniques are used:
  18. * idle processes that keep running during the last process sample
  19. (which is a heuristic for a background processes) are removed,
  20. * short-lived processes (i.e. processes that only live for the
  21. duration of two samples or less) are removed,
  22. * the processes used by the boot logger are removed,
  23. * exploders (i.e. processes that are known to spawn huge meaningless
  24. process subtrees) have their subtrees merged together,
  25. * siblings (i.e. processes with the same command line living
  26. concurrently -- thread heuristic) are merged together,
  27. * process runs (unary trees with processes sharing the command line)
  28. are merged together.
  29. """
  30. LOGGER_PROC = 'bootchart-colle'
  31. EXPLODER_PROCESSES = set(['hwup'])
  32. def __init__(self, writer, kernel, psstats, sample_period,
  33. monitoredApp, prune, idle, taskstats,
  34. accurate_parentage, for_testing = False):
  35. self.writer = writer
  36. self.process_tree = []
  37. self.taskstats = taskstats
  38. if psstats is None:
  39. process_list = kernel
  40. elif kernel is None:
  41. process_list = psstats.process_map.values()
  42. else:
  43. process_list = list(kernel) + list(psstats.process_map.values())
  44. self.process_list = sorted(process_list, key = lambda p: p.pid)
  45. self.sample_period = sample_period
  46. self.build()
  47. if not accurate_parentage:
  48. self.update_ppids_for_daemons(self.process_list)
  49. self.start_time = self.get_start_time(self.process_tree)
  50. self.end_time = self.get_end_time(self.process_tree)
  51. self.duration = self.end_time - self.start_time
  52. self.idle = idle
  53. if for_testing:
  54. return
  55. removed = self.merge_logger(self.process_tree, self.LOGGER_PROC, monitoredApp, False)
  56. writer.status("merged %i logger processes" % removed)
  57. if prune:
  58. p_processes = self.prune(self.process_tree, None)
  59. p_exploders = self.merge_exploders(self.process_tree, self.EXPLODER_PROCESSES)
  60. p_threads = self.merge_siblings(self.process_tree)
  61. p_runs = self.merge_runs(self.process_tree)
  62. writer.status("pruned %i process, %i exploders, %i threads, and %i runs" % (p_processes, p_exploders, p_threads, p_runs))
  63. self.sort(self.process_tree)
  64. self.start_time = self.get_start_time(self.process_tree)
  65. self.end_time = self.get_end_time(self.process_tree)
  66. self.duration = self.end_time - self.start_time
  67. self.num_proc = self.num_nodes(self.process_tree)
  68. def build(self):
  69. """Build the process tree from the list of top samples."""
  70. self.process_tree = []
  71. for proc in self.process_list:
  72. if not proc.parent:
  73. self.process_tree.append(proc)
  74. else:
  75. proc.parent.child_list.append(proc)
  76. def sort(self, process_subtree):
  77. """Sort process tree."""
  78. for p in process_subtree:
  79. p.child_list.sort(key = lambda p: p.pid)
  80. self.sort(p.child_list)
  81. def num_nodes(self, process_list):
  82. "Counts the number of nodes in the specified process tree."""
  83. nodes = 0
  84. for proc in process_list:
  85. nodes = nodes + self.num_nodes(proc.child_list)
  86. return nodes + len(process_list)
  87. def get_start_time(self, process_subtree):
  88. """Returns the start time of the process subtree. This is the start
  89. time of the earliest process.
  90. """
  91. if not process_subtree:
  92. return 100000000
  93. return min( [min(proc.start_time, self.get_start_time(proc.child_list)) for proc in process_subtree] )
  94. def get_end_time(self, process_subtree):
  95. """Returns the end time of the process subtree. This is the end time
  96. of the last collected sample.
  97. """
  98. if not process_subtree:
  99. return -100000000
  100. return max( [max(proc.start_time + proc.duration, self.get_end_time(proc.child_list)) for proc in process_subtree] )
  101. def get_max_pid(self, process_subtree):
  102. """Returns the max PID found in the process tree."""
  103. if not process_subtree:
  104. return -100000000
  105. return max( [max(proc.pid, self.get_max_pid(proc.child_list)) for proc in process_subtree] )
  106. def update_ppids_for_daemons(self, process_list):
  107. """Fedora hack: when loading the system services from rc, runuser(1)
  108. is used. This sets the PPID of all daemons to 1, skewing
  109. the process tree. Try to detect this and set the PPID of
  110. these processes the PID of rc.
  111. """
  112. rcstartpid = -1
  113. rcendpid = -1
  114. rcproc = None
  115. for p in process_list:
  116. if p.cmd == "rc" and p.ppid // 1000 == 1:
  117. rcproc = p
  118. rcstartpid = p.pid
  119. rcendpid = self.get_max_pid(p.child_list)
  120. if rcstartpid != -1 and rcendpid != -1:
  121. for p in process_list:
  122. if p.pid > rcstartpid and p.pid < rcendpid and p.ppid // 1000 == 1:
  123. p.ppid = rcstartpid
  124. p.parent = rcproc
  125. for p in process_list:
  126. p.child_list = []
  127. self.build()
  128. def prune(self, process_subtree, parent):
  129. """Prunes the process tree by removing idle processes and processes
  130. that only live for the duration of a single top sample. Sibling
  131. processes with the same command line (i.e. threads) are merged
  132. together. This filters out sleepy background processes, short-lived
  133. processes and bootcharts' analysis tools.
  134. """
  135. def is_idle_background_process_without_children(p):
  136. process_end = p.start_time + p.duration
  137. return not p.active and \
  138. process_end >= self.start_time + self.duration and \
  139. p.start_time > self.start_time and \
  140. p.duration > 0.9 * self.duration and \
  141. self.num_nodes(p.child_list) == 0
  142. num_removed = 0
  143. idx = 0
  144. while idx < len(process_subtree):
  145. p = process_subtree[idx]
  146. if parent != None or len(p.child_list) == 0:
  147. prune = False
  148. if is_idle_background_process_without_children(p):
  149. prune = True
  150. elif p.duration <= 2 * self.sample_period:
  151. # short-lived process
  152. prune = True
  153. if prune:
  154. process_subtree.pop(idx)
  155. for c in p.child_list:
  156. process_subtree.insert(idx, c)
  157. num_removed += 1
  158. continue
  159. else:
  160. num_removed += self.prune(p.child_list, p)
  161. else:
  162. num_removed += self.prune(p.child_list, p)
  163. idx += 1
  164. return num_removed
  165. def merge_logger(self, process_subtree, logger_proc, monitored_app, app_tree):
  166. """Merges the logger's process subtree. The logger will typically
  167. spawn lots of sleep and cat processes, thus polluting the
  168. process tree.
  169. """
  170. num_removed = 0
  171. for p in process_subtree:
  172. is_app_tree = app_tree
  173. if logger_proc == p.cmd and not app_tree:
  174. is_app_tree = True
  175. num_removed += self.merge_logger(p.child_list, logger_proc, monitored_app, is_app_tree)
  176. # don't remove the logger itself
  177. continue
  178. if app_tree and monitored_app != None and monitored_app == p.cmd:
  179. is_app_tree = False
  180. if is_app_tree:
  181. for child in p.child_list:
  182. self.merge_processes(p, child)
  183. num_removed += 1
  184. p.child_list = []
  185. else:
  186. num_removed += self.merge_logger(p.child_list, logger_proc, monitored_app, is_app_tree)
  187. return num_removed
  188. def merge_exploders(self, process_subtree, processes):
  189. """Merges specific process subtrees (used for processes which usually
  190. spawn huge meaningless process trees).
  191. """
  192. num_removed = 0
  193. for p in process_subtree:
  194. if processes in processes and len(p.child_list) > 0:
  195. subtreemap = self.getProcessMap(p.child_list)
  196. for child in subtreemap.values():
  197. self.merge_processes(p, child)
  198. num_removed += len(subtreemap)
  199. p.child_list = []
  200. p.cmd += " (+)"
  201. else:
  202. num_removed += self.merge_exploders(p.child_list, processes)
  203. return num_removed
  204. def merge_siblings(self, process_subtree):
  205. """Merges thread processes. Sibling processes with the same command
  206. line are merged together.
  207. """
  208. num_removed = 0
  209. idx = 0
  210. while idx < len(process_subtree)-1:
  211. p = process_subtree[idx]
  212. nextp = process_subtree[idx+1]
  213. if nextp.cmd == p.cmd:
  214. process_subtree.pop(idx+1)
  215. idx -= 1
  216. num_removed += 1
  217. p.child_list.extend(nextp.child_list)
  218. self.merge_processes(p, nextp)
  219. num_removed += self.merge_siblings(p.child_list)
  220. idx += 1
  221. if len(process_subtree) > 0:
  222. p = process_subtree[-1]
  223. num_removed += self.merge_siblings(p.child_list)
  224. return num_removed
  225. def merge_runs(self, process_subtree):
  226. """Merges process runs. Single child processes which share the same
  227. command line with the parent are merged.
  228. """
  229. num_removed = 0
  230. idx = 0
  231. while idx < len(process_subtree):
  232. p = process_subtree[idx]
  233. if len(p.child_list) == 1 and p.child_list[0].cmd == p.cmd:
  234. child = p.child_list[0]
  235. p.child_list = list(child.child_list)
  236. self.merge_processes(p, child)
  237. num_removed += 1
  238. continue
  239. num_removed += self.merge_runs(p.child_list)
  240. idx += 1
  241. return num_removed
  242. def merge_processes(self, p1, p2):
  243. """Merges two process' samples."""
  244. p1.samples.extend(p2.samples)
  245. p1.samples.sort( key = lambda p: p.time )
  246. p1time = p1.start_time
  247. p2time = p2.start_time
  248. p1.start_time = min(p1time, p2time)
  249. pendtime = max(p1time + p1.duration, p2time + p2.duration)
  250. p1.duration = pendtime - p1.start_time