Skip to content

Commit d4d84c0

Browse files
committed
Added earlier detection of corner case
1 parent af81ac6 commit d4d84c0

1 file changed

Lines changed: 65 additions & 59 deletions

File tree

treecript/collector.py

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -174,70 +174,76 @@ def analyse_list_of_processes(
174174
],
175175
)
176176

177-
threads_processor_num: "Set[int]" = set()
178-
threads_core_num: "Set[str]" = set()
179-
threads_cpu_num: "Set[str]" = set()
180-
for thr in child_threads:
181-
processor_id: "int" = child_d["cpu_num"]
182-
if thr.id > 0 and thr.id != child_d["pid"]:
183-
try:
184-
processor_id = psutil.Process(thr.id).cpu_num()
185-
except psutil.NoSuchProcess:
186-
# For transient cases
187-
pass
188-
threads_processor_num.add(processor_id)
189-
processor_id_str = str(processor_id)
190-
191-
unambiguous_core_id = processor2corecpu[processor_id_str]
192-
threads_core_num.add(unambiguous_core_id[1])
193-
threads_cpu_num.add(unambiguous_core_id[0])
194-
195-
child_d["threads_processor_num"] = threads_processor_num
196-
child_d["threads_core_num"] = threads_core_num
197-
child_d["threads_cpu_num"] = threads_cpu_num
198-
199-
# First attempt: already recorded
200-
if len(docker_following_pids) > 0:
201-
new_ppid = docker_following_pids.get(child_pid_int)
202-
# Patching the reference to the parent
203-
if new_ppid is not None:
204-
# logger.error(
205-
# f"switched {child_pid_int} parent: {child_d['ppid']} => {new_ppid}"
206-
# )
207-
child_d["ppid"] = new_ppid
208-
209-
# Are there docker instances involved?
177+
# The process suddenly disappeared
210178
if (
211-
len(container_data_rw) > 0
212-
and child_d["name"] == "docker"
213-
and child_d["cmdline"][1] == "run"
179+
child_d["cpu_times"] is not None
180+
and child_d["memory_full_info"] is not None
181+
and child_d["cmdline"] is not None
214182
):
215-
# logger.error(f"Candidate docker {child_pid_int}")
216-
# Matching
217-
i_c_t_t: "Optional[int]"
218-
for i_c_t_t, c_t_t in enumerate(container_data_rw):
219-
(
220-
container_id,
221-
container_creation,
222-
container_image,
223-
container_pid,
224-
) = c_t_t
225-
if (
226-
container_creation > child_d["create_time"]
227-
and container_image in child_d["cmdline"]
228-
):
229-
new_pid_pairs.append((container_pid, child_pid_int))
183+
threads_processor_num: "Set[int]" = set()
184+
threads_core_num: "Set[str]" = set()
185+
threads_cpu_num: "Set[str]" = set()
186+
for thr in child_threads:
187+
processor_id: "int" = child_d["cpu_num"]
188+
if thr.id > 0 and thr.id != child_d["pid"]:
189+
try:
190+
processor_id = psutil.Process(thr.id).cpu_num()
191+
except psutil.NoSuchProcess:
192+
# For transient cases
193+
pass
194+
threads_processor_num.add(processor_id)
195+
processor_id_str = str(processor_id)
196+
197+
unambiguous_core_id = processor2corecpu[processor_id_str]
198+
threads_core_num.add(unambiguous_core_id[1])
199+
threads_cpu_num.add(unambiguous_core_id[0])
200+
201+
child_d["threads_processor_num"] = threads_processor_num
202+
child_d["threads_core_num"] = threads_core_num
203+
child_d["threads_cpu_num"] = threads_cpu_num
204+
205+
# First attempt: already recorded
206+
if len(docker_following_pids) > 0:
207+
new_ppid = docker_following_pids.get(child_pid_int)
208+
# Patching the reference to the parent
209+
if new_ppid is not None:
230210
# logger.error(
231-
# f"Matched docker {child_pid_int} => child {container_pid}"
211+
# f"switched {child_pid_int} parent: {child_d['ppid']} => {new_ppid}"
232212
# )
233-
break
234-
else:
235-
i_c_t_t = None
236-
237-
if i_c_t_t is not None:
238-
del container_data_rw[i_c_t_t]
213+
child_d["ppid"] = new_ppid
239214

240-
children_dicts.append((child_d, mode_w))
215+
# Are there docker instances involved?
216+
if (
217+
len(container_data_rw) > 0
218+
and child_d["name"] == "docker"
219+
and child_d["cmdline"][1] == "run"
220+
):
221+
# logger.error(f"Candidate docker {child_pid_int}")
222+
# Matching
223+
i_c_t_t: "Optional[int]"
224+
for i_c_t_t, c_t_t in enumerate(container_data_rw):
225+
(
226+
container_id,
227+
container_creation,
228+
container_image,
229+
container_pid,
230+
) = c_t_t
231+
if (
232+
container_creation > child_d["create_time"]
233+
and container_image in child_d["cmdline"]
234+
):
235+
new_pid_pairs.append((container_pid, child_pid_int))
236+
# logger.error(
237+
# f"Matched docker {child_pid_int} => child {container_pid}"
238+
# )
239+
break
240+
else:
241+
i_c_t_t = None
242+
243+
if i_c_t_t is not None:
244+
del container_data_rw[i_c_t_t]
245+
246+
children_dicts.append((child_d, mode_w))
241247
elif mode_w == "w" and child_pid_int in recorded_pids:
242248
# Keeping the internal structures tidied up
243249
del recorded_pids[child_pid_int]

0 commit comments

Comments
 (0)