获取单个正在运行的进程Start-Time

时间:2015-07-22 06:51:01

标签: android linux process linux-kernel runtime

我正在使用以下代码来获取Android设备上当前正在运行的所有流程。

// Get running processes
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses();

我还尝试使用以下代码从每个pid中获取Android设备中的所有单个进程开始时间 - &#34;进程ID&#34;在以下文件目录中:&#34; / proc / [PID] / stat&#34;从linux获得:

public static long getStartTime(final int pid) throws IOException {

final String path = ("/proc/" + pid + "/stat");
final String stat;
final String field2End = ") ";
final String fieldSep = " ";
final BufferedReader reader = new BufferedReader(new FileReader(path));

 try {
        stat = reader.readLine();
        System.out.println("******Stat******"+ stat);
    } finally {
        reader.close();
    }

try {
        final String[] fields = stat.substring(stat.lastIndexOf(field2End)).split(fieldSep);
        final long processstartTime = .....;
        ...(change processstartTime from clock tick to seconds & return processstartTime value)...
}
}

我需要从以下Linux目录路径获取进程StartTime:&#34; / proc / pid / stat&#34;对于Android设备中的每个运行进程。此时,当我调试以下Linux目录路径的语句:&#34; / proc / [PID] / stat&#34;,在以下代码行中:System.out.println("******Stat******"+ stat);,我得到输出调试as:

  

****** Stat ****** 642(flipboard.app)S 2848 2848 0 0 -1 4194624 126020 0 1019 0 2441 632 0 0 20 0 101 0 7040346 1079652352 7233 4294967295 1 1 0 0 0 0 4612 0 38120 4294967295 0 0 17 1 0 0 0 0 0 0 0 0

此外,我知道进程的start_time是以时钟周期为单位进行测量,因此要将其转换为秒,我需要将以下内容称为&#34; start_time / hertz&#34;。

现在的问题是,如何在&#34; / proc / [PID] / stat&#34;中获得正在运行的进程开始时间?有人可以帮忙吗?感谢。

1 个答案:

答案 0 :(得分:1)

单个处理时间的loggin可以通过这种方式完成:

public static long getStartTime(final int pid) throws
        IOException {
    final long SYSTEM_CLK_TCK= 100; //needed as value in /proc/[PID]/stat file driectory is in clock ticks,100 is used to convert clock ticks to secs
    final int fieldStartTime = 20; //column 20 of the /proc/[PID]/stat file driectory
try {
        System.out.println("******String path******"+ path);
        stat = reader.readLine();
        System.out.println("******String stat******"+ stat);
    } finally {
        reader.close();
    }
try {
        final String[] fields = stat.substring(stat.lastIndexOf(field2End)).split(fieldSep);
        final long startTime = Long.parseLong(fields[fieldStartTime]);
        System.out.println("******fieldstarttime based on clock ticks******"+ startTime);

        return startTime * msInSec / SYSTEM_CLK_TCK;
}
相关问题