From 117686996b5df364917d05d8965baeca61b5366e Mon Sep 17 00:00:00 2001 From: Stephen Kyle Date: Fri, 10 Jul 2020 10:12:25 +0100 Subject: [PATCH] target: support threads in ps Adds 'tid' attribute to PsEntry namedtuple. This is equal to the PID of the process. Adds 'threads=True' parameter to target.ps(). When true, PsEntrys will be returned for all threads on the target, not just processes. The 'tid' will be the distinct PID for each thread, rather than the owning process. --- devlib/target.py | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/devlib/target.py b/devlib/target.py index ee1ffcb..64cb6dc 100644 --- a/devlib/target.py +++ b/devlib/target.py @@ -1163,16 +1163,20 @@ class LinuxTarget(Target): else: return [] - def ps(self, **kwargs): - command = 'ps -eo user,pid,ppid,vsize,rss,wchan,pcpu,state,fname' + def ps(self, threads=False, **kwargs): + ps_flags = '-eo' + if threads: + ps_flags = '-eLo' + command = 'ps {} user,pid,tid,ppid,vsize,rss,wchan,pcpu,state,fname'.format(ps_flags) + lines = iter(convert_new_lines(self.execute(command)).split('\n')) next(lines) # header result = [] for line in lines: - parts = re.split(r'\s+', line, maxsplit=8) + parts = re.split(r'\s+', line, maxsplit=9) if parts and parts != ['']: - result.append(PsEntry(*(parts[0:1] + list(map(int, parts[1:5])) + parts[5:]))) + result.append(PsEntry(*(parts[0:1] + list(map(int, parts[1:6])) + parts[6:]))) if not kwargs: return result @@ -1419,18 +1423,32 @@ class AndroidTarget(Target): result.append(entry.pid) return result - def ps(self, **kwargs): - lines = iter(convert_new_lines(self.execute('ps')).split('\n')) + def ps(self, threads=False, **kwargs): + maxsplit = 9 if threads else 8 + command = 'ps' + if threads: + command = 'ps -AT' + + lines = iter(convert_new_lines(self.execute(command)).split('\n')) next(lines) # header result = [] for line in lines: - parts = line.split(None, 8) + parts = line.split(None, maxsplit) if not parts: continue - if len(parts) == 8: + + wchan_missing = False + if len(parts) == maxsplit: + wchan_missing = True + + if not threads: + # Duplicate PID into TID location. + parts.insert(2, parts[1]) + + if wchan_missing: # wchan was blank; insert an empty field where it should be. - parts.insert(5, '') - result.append(PsEntry(*(parts[0:1] + list(map(int, parts[1:5])) + parts[5:]))) + parts.insert(6, '') + result.append(PsEntry(*(parts[0:1] + list(map(int, parts[1:6])) + parts[6:]))) if not kwargs: return result else: @@ -1854,7 +1872,7 @@ class AndroidTarget(Target): self.write_value(self._charging_enabled_path, int(bool(enabled))) FstabEntry = namedtuple('FstabEntry', ['device', 'mount_point', 'fs_type', 'options', 'dump_freq', 'pass_num']) -PsEntry = namedtuple('PsEntry', 'user pid ppid vsize rss wchan pc state name') +PsEntry = namedtuple('PsEntry', 'user pid tid ppid vsize rss wchan pc state name') LsmodEntry = namedtuple('LsmodEntry', ['name', 'size', 'use_count', 'used_by'])