进程工具

public final class ProcessUtil
extends Object

java.lang.Object
com.android.sts.common.ProcessUtil


用于查找、等待和终止设备上进程的各种帮助程序

概括

嵌套类

class ProcessUtil.KillException

常数

long PROCESS_POLL_PERIOD_MS

long PROCESS_WAIT_TIMEOUT_MS

公共方法

static Optional<IFileEntry> findFileLoadedByProcess (ITestDevice device, String process, String filenameSubstr)

返回由指定进程加载的具有指定名称的第一个文件的文件条目

static Optional<List<String>> findFilesLoadedByProcess (ITestDevice device, int pid, Pattern filePattern)

返回由指定进程加载的指定文件的文件名。

static Optional<String> getProcessName (ITestDevice device, int pid)

从 /proc/pid/cmdline 获取内容。

static boolean killAll (ITestDevice device, String pgrepRegex, long timeoutMs, boolean expectExist)

向所有匹配模式的进程发送 SIGKILL。

static boolean killAll (ITestDevice device, String pgrepRegex, long timeoutMs)

向所有匹配模式的进程发送 SIGKILL。

static void killPid (ITestDevice device, int pid, long timeoutMs)

向进程发送 SIGKILL 并等待其退出。

static void killPid (ITestDevice device, int pid, int signal, long timeoutMs)

向进程发送信号并等待其退出。

static Optional<List<String>> listOpenFiles (ITestDevice device, int pid)

返回指定进程当前打开的文件名。

static Optional<Integer> pidOf (ITestDevice device, String pgrepRegex)

获取与传递给“pgrep”的模式匹配的单个 pid。

static Optional<Map<Integer, String>> pidsOf (ITestDevice device, String pgrepRegex)

获取与传递给“pgrep”的模式匹配的 pid。

static void waitPidExited (ITestDevice device, int pid)

等待进程退出。

static void waitPidExited (ITestDevice device, int pid, long timeoutMs)

等待进程退出。

static Map<Integer, String> waitProcessRunning (ITestDevice device, String pgrepRegex, long timeoutMs)

等待,直到找到给定正则表达式的正在运行的进程。

static Map<Integer, String> waitProcessRunning (ITestDevice device, String pgrepRegex)

等待,直到找到给定正则表达式的正在运行的进程。

static AutoCloseable withProcessKill (ITestDevice device, String pgrepRegex, Runnable beforeCloseKill, long timeoutMs)

在测试开始和结束时终止进程。

static AutoCloseable withProcessKill (ITestDevice device, String pgrepRegex, Runnable beforeCloseKill)

在测试开始和结束时终止进程。

常数

PROCESS_POLL_PERIOD_MS

public static final long PROCESS_POLL_PERIOD_MS

常量值:250 (0x00000000000000fa)

PROCESS_WAIT_TIMEOUT_MS

public static final long PROCESS_WAIT_TIMEOUT_MS

常量值:10000 (0x0000000000002710)

公共方法

查找通过进程加载的文件

public static Optional<IFileEntry> findFileLoadedByProcess (ITestDevice device, 
                String process, 
                String filenameSubstr)

返回由指定进程加载的具有指定名称的第一个文件的文件条目

参数
device ITestDevice :要运行的设备

process String : 要查找的进程的 pgrep 模式

filenameSubstr String : 进程加载的文件名/路径的一部分

退货
Optional<IFileEntry>设备上文件路径的可选 IFileEntry(如果存在)。

投掷
DeviceNotAvailableException

查找按进程加载的文件

public static Optional<List<String>> findFilesLoadedByProcess (ITestDevice device, 
                int pid, 
                Pattern filePattern)

返回由指定进程加载的指定文件的文件名。

参数
device ITestDevice :要运行的设备

pid int : 要搜索的进程的 id

filePattern Pattern :要返回的文件名模式

退货
Optional<List<String>>过滤后的文件的可选;如果找不到进程或无法读取打开的文件,则为空。

投掷
DeviceNotAvailableException

获取进程名

public static Optional<String> getProcessName (ITestDevice device, 
                int pid)

从 /proc/pid/cmdline 获取内容。

参数
device ITestDevice :要使用的设备

pid int :要获取其名称的进程的 id

退货
Optional<String> /proc/pid/cmdline 内容的可选字符串;如果找不到 pid,则为空

投掷
DeviceNotAvailableException

全杀

public static boolean killAll (ITestDevice device, 
                String pgrepRegex, 
                long timeoutMs, 
                boolean expectExist)

向所有匹配模式的进程发送 SIGKILL。

参数
device ITestDevice :要使用的设备

pgrepRegex String : 表示 pgrep 正则表达式的字符串

timeoutMs long :抛出 TimeoutException 之前等待多长时间

expectExist boolean : 当没有进程被杀死时是否应该抛出异常

退货
boolean是否有进程被杀死

投掷
DeviceNotAvailableException
TimeoutException
ProcessUtil.KillException

全杀

public static boolean killAll (ITestDevice device, 
                String pgrepRegex, 
                long timeoutMs)

向所有匹配模式的进程发送 SIGKILL。

参数
device ITestDevice :要使用的设备

pgrepRegex String : 表示 pgrep 正则表达式的字符串

timeoutMs long :抛出 TimeoutException 之前等待多长时间

退货
boolean是否有进程被杀死

投掷
DeviceNotAvailableException
TimeoutException
ProcessUtil.KillException

杀死Pid

public static void killPid (ITestDevice device, 
                int pid, 
                long timeoutMs)

向进程发送 SIGKILL 并等待其退出。

参数
device ITestDevice :要使用的设备

pid int : 等待退出的进程的 id

timeoutMs long :抛出 TimeoutException 之前等待多长时间

投掷
DeviceNotAvailableException
TimeoutException
ProcessUtil.KillException

杀死Pid

public static void killPid (ITestDevice device, 
                int pid, 
                int signal, 
                long timeoutMs)

向进程发送信号并等待其退出。

参数
device ITestDevice :要使用的设备

pid int : 等待退出的进程的 id

signal int :发送到进程的信号

timeoutMs long :抛出 TimeoutException 之前等待多长时间

投掷
DeviceNotAvailableException
TimeoutException
ProcessUtil.KillException

列出打开的文件

public static Optional<List<String>> listOpenFiles (ITestDevice device, 
                int pid)

返回指定进程当前打开的文件名。

参数
device ITestDevice :要运行的设备

pid int : 要搜索的进程的 id

退货
Optional<List<String>>打开文件的可选;如果找不到进程或无法读取打开的文件,则为空。

投掷
DeviceNotAvailableException

进程号

public static Optional<Integer> pidOf (ITestDevice device, 
                String pgrepRegex)

获取与传递给“pgrep”的模式匹配的单个 pid。当有多个 PID 与该模式匹配时,抛出IllegalArgumentException

参数
device ITestDevice :要使用的设备

pgrepRegex String : 表示 pgrep 正则表达式的字符串

退货
Optional<Integer> pid 的可选整数;如果 pgrep 没有返回 EXIT_SUCCESS 则为空

投掷
DeviceNotAvailableException
IllegalArgumentException

pidsOf

public static Optional<Map<Integer, String>> pidsOf (ITestDevice device, 
                String pgrepRegex)

获取与传递给“pgrep”的模式匹配的 pid。因为 /proc/pid/comm 被截断,所以 `pgrep` 与 `-f` 一起传递以检查完整的命令行。

参数
device ITestDevice :要使用的设备

pgrepRegex String : 表示 pgrep 正则表达式的字符串

退货
Optional<Map<Integer, String>> pid 到命令行的可选映射;如果 pgrep 没有返回 EXIT_SUCCESS 则为空

投掷
DeviceNotAvailableException

等待Pid退出

public static void waitPidExited (ITestDevice device, 
                int pid)

等待进程退出。这不是等待它改变,而是根本不存在。在轮询之间重用 pid 是可能的,但不太可能

参数
device ITestDevice :要使用的设备

pid int : 等待退出的进程的 id

投掷
TimeoutException
DeviceNotAvailableException
ProcessUtil.KillException

等待Pid退出

public static void waitPidExited (ITestDevice device, 
                int pid, 
                long timeoutMs)

等待进程退出。这不是等待它改变,而是根本不存在。在轮询之间重用 pid 是可能的,但不太可能

参数
device ITestDevice :要使用的设备

pid int : 等待退出的进程的 id

timeoutMs long :抛出 TimeoutException 之前等待多长时间

投掷
TimeoutException
DeviceNotAvailableException
ProcessUtil.KillException

等待进程运行

public static Map<Integer, String> waitProcessRunning (ITestDevice device, 
                String pgrepRegex, 
                long timeoutMs)

等待,直到找到给定正则表达式的正在运行的进程。

参数
device ITestDevice :要使用的设备

pgrepRegex String : 表示 pgrep 正则表达式的字符串

timeoutMs long :抛出 TimeoutException 之前等待多长时间

退货
Map<Integer, String>来自 pidsOf(...) 的 pid 到命令映射

投掷
TimeoutException
DeviceNotAvailableException

等待进程运行

public static Map<Integer, String> waitProcessRunning (ITestDevice device, 
                String pgrepRegex)

等待,直到找到给定正则表达式的正在运行的进程。

参数
device ITestDevice :要使用的设备

pgrepRegex String : 表示 pgrep 正则表达式的字符串

退货
Map<Integer, String>来自 pidsOf(...) 的 pid 到命令映射

投掷
TimeoutException
DeviceNotAvailableException

进程终止

public static AutoCloseable withProcessKill (ITestDevice device, 
                String pgrepRegex, 
                Runnable beforeCloseKill, 
                long timeoutMs)

在测试开始和结束时终止进程。

参数
device ITestDevice :要使用的设备

pgrepRegex String :要终止的进程的名称模式,以提供给 pgrep

beforeCloseKill Runnable :在测试结束时在正常环境中终止进程之前需要清理的任何操作的可运行状态。可以为空。

timeoutMs long :等待进程终止的时间(以毫秒为单位)

退货
AutoCloseable当进程关闭时会再次终止进程的对象

投掷
DeviceNotAvailableException
TimeoutException
ProcessUtil.KillException

进程终止

public static AutoCloseable withProcessKill (ITestDevice device, 
                String pgrepRegex, 
                Runnable beforeCloseKill)

在测试开始和结束时终止进程。

参数
device ITestDevice :要使用的设备

pgrepRegex String :要终止的进程的名称模式,以提供给 pgrep

beforeCloseKill Runnable :在测试结束时在正常环境中终止进程之前需要清理的任何操作的可运行状态。可以为空。

退货
AutoCloseable当进程关闭时会再次终止进程的对象

投掷
DeviceNotAvailableException
TimeoutException
ProcessUtil.KillException