| 1 | !include once #1034919 // Include OSHI library 6.1.5 | 
| 2 | |
| 3 | import oshi.util.*; | 
| 4 | import oshi.software.os.*; | 
| 5 | import oshi.software.os.OSProcess; | 
| 6 | import oshi.software.os.linux.*; | 
| 7 | import oshi.software.os.linux.LinuxOperatingSystem.ProcPidStat; | 
| 8 | import oshi.util.platform.linux.ProcUtil; | 
| 9 | import oshi.software.os.OperatingSystem.ProcessSort; | 
| 10 | |
| 11 | static OSProcess[] oshi_listProcesses_withoutOpenFiles(int limit, ProcessSort sort) {
 | 
| 12 | new L<OSProcess> procs; | 
| 13 | File[] pids = ProcUtil.getPidFiles(); | 
| 14 | |
| 15 | // now for each file (with digit name) get process info | 
| 16 |   for (File pid : pids) {
 | 
| 17 | OSProcess proc = oshi_getProcess_withoutOpenFiles(ParseUtil.parseIntOrDefault(pid.getName(), 0)); | 
| 18 | if (proc != null) | 
| 19 | procs.add(proc); | 
| 20 | } | 
| 21 | List<OSProcess> sorted = processSort(procs, limit, sort); | 
| 22 | return sorted.toArray(new OSProcess[sorted.size()]); | 
| 23 | } | 
| 24 | |
| 25 | static OSProcess oshi_getProcess_withoutOpenFiles(int pid) {
 | 
| 26 | String path = ""; | 
| 27 | Pointer buf = new Memory(1024); | 
| 28 |   int size = Libc.INSTANCE.readlink(String.format("/proc/%d/exe", pid), buf, 1023);
 | 
| 29 |   if (size > 0) {
 | 
| 30 | path = buf.getString(0).substring(0, size); | 
| 31 | } | 
| 32 |   Map<String, String> io = FileUtil.getKeyValueMapFromFile(String.format("/proc/%d/io", pid), ":");
 | 
| 33 | // See man proc for how to parse /proc/[pid]/stat | 
| 34 | long now = System.currentTimeMillis(); | 
| 35 |   String stat = FileUtil.getStringFromFile(String.format("/proc/%d/stat", pid));
 | 
| 36 | // A race condition may leave us with an empty string | 
| 37 | if (stat.isEmpty()) null; | 
| 38 | new OSProcess proc; | 
| 39 | // We can get name and status more easily from /proc/pid/status which we | 
| 40 | // call later, so just get the numeric bits here | 
| 41 | proc.setProcessID(pid); | 
| 42 | int[] PROC_PID_STAT_ORDERS = cast get(oshi_operatingSystem(), 'PROC_PID_STAT_ORDERS); | 
| 43 | long[] statArray = ParseUtil.parseStringToLongArray(stat, PROC_PID_STAT_ORDERS, procPidStatLength, ' '); | 
| 44 | proc.setParentProcessID((int) statArray[ProcPidStat.PPID.ordinal()]); | 
| 45 | proc.setThreadCount((int) statArray[ProcPidStat.THREAD_COUNT.ordinal()]); | 
| 46 | proc.setPriority((int) statArray[ProcPidStat.PRIORITY.ordinal()]); | 
| 47 | proc.setVirtualSize(statArray[ProcPidStat.VSZ.ordinal()]); | 
| 48 | proc.setResidentSetSize(statArray[ProcPidStat.RSS.ordinal()] * this.memoryPageSize); | 
| 49 | proc.setKernelTime(statArray[ProcPidStat.KERNEL_TIME.ordinal()] * 1000L / USER_HZ); | 
| 50 | proc.setUserTime(statArray[ProcPidStat.USER_TIME.ordinal()] * 1000L / USER_HZ); | 
| 51 | long BOOT_TIME = cast getOpt(oshi_operatingSystem(), 'BOOT_TIME); | 
| 52 | proc.setStartTime(BOOT_TIME + statArray[ProcPidStat.START_TIME.ordinal()] * 1000L / USER_HZ); | 
| 53 | // BOOT_TIME could be up to 5ms off. In rare cases when a process has | 
| 54 | // started within 5ms of boot it is possible to get negative uptime. | 
| 55 |   if (proc.getStartTime() >= now) {
 | 
| 56 | proc.setStartTime(now - 1); | 
| 57 | } | 
| 58 | proc.setUpTime(now - proc.getStartTime()); | 
| 59 | // See man proc for how to parse /proc/[pid]/io | 
| 60 | proc.setBytesRead(ParseUtil.parseLongOrDefault(MapUtil.getOrDefault(io, "read_bytes", ""), 0L)); | 
| 61 | proc.setBytesWritten(ParseUtil.parseLongOrDefault(MapUtil.getOrDefault(io, "write_bytes", ""), 0L)); | 
| 62 | |
| 63 | // gets the open files count | 
| 64 |   List<String> openFilesList = ExecutingCommand.runNative(String.format("ls -f /proc/%d/fd", pid));
 | 
| 65 | proc.setOpenFiles(openFilesList.size() - 1L); | 
| 66 | |
| 67 |   Map<String, String> status = FileUtil.getKeyValueMapFromFile(String.format("/proc/%d/status", pid), ":");
 | 
| 68 | proc.setName(MapUtil.getOrDefault(status, "Name", "")); | 
| 69 | proc.setPath(path); | 
| 70 |   switch (MapUtil.getOrDefault(status, "State", "U").charAt(0)) {
 | 
| 71 | case 'R': | 
| 72 | proc.setState(OSProcess.State.RUNNING); | 
| 73 | break; | 
| 74 | case 'S': | 
| 75 | proc.setState(OSProcess.State.SLEEPING); | 
| 76 | break; | 
| 77 | case 'D': | 
| 78 | proc.setState(OSProcess.State.WAITING); | 
| 79 | break; | 
| 80 | case 'Z': | 
| 81 | proc.setState(OSProcess.State.ZOMBIE); | 
| 82 | break; | 
| 83 | case 'T': | 
| 84 | proc.setState(OSProcess.State.STOPPED); | 
| 85 | break; | 
| 86 | default: | 
| 87 | proc.setState(OSProcess.State.OTHER); | 
| 88 | break; | 
| 89 | } | 
| 90 | proc.setUserID(ParseUtil.whitespaces.split(MapUtil.getOrDefault(status, "Uid", ""))[0]); | 
| 91 | proc.setGroupID(ParseUtil.whitespaces.split(MapUtil.getOrDefault(status, "Gid", ""))[0]); | 
| 92 | |
| 93 | LinuxUserGroupInfo userGroupInfo = cast get(oshi_operatingSystem(), 'userGroupInfo); | 
| 94 | OSUser user = userGroupInfo.getUser(proc.getUserID()); | 
| 95 | if (user != null) | 
| 96 | proc.setUser(user.getUserName()); | 
| 97 | proc.setGroup(userGroupInfo.getGroupName(proc.getGroupID())); | 
| 98 | |
| 99 | // THe /proc/pid/cmdline value is null-delimited | 
| 100 |   proc.setCommandLine(FileUtil.getStringFromFile(String.format("/proc/%d/cmdline", pid)));
 | 
| 101 |   pcall {
 | 
| 102 |       String cwdLink = String.format("/proc/%d/cwd", pid);
 | 
| 103 | String cwd = new File(cwdLink).getCanonicalPath(); | 
| 104 |       if (!cwd.equals(cwdLink)) {
 | 
| 105 | proc.setCurrentWorkingDirectory(cwd); | 
| 106 | } | 
| 107 | } | 
| 108 | ret proc; | 
| 109 | } | 
download show line numbers debug dex old transpilations
Travelled to 12 computer(s): bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1019621 | 
| Snippet name: | oshi_listProcesses_withoutOpenFiles [dev.] | 
| Eternal ID of this version: | #1019621/15 | 
| Text MD5: | ea942e3d39d62f7148d44b2ac9495769 | 
| Author: | stefan | 
| Category: | javax / sensors | 
| Type: | JavaX fragment (include) | 
| Public (visible to everyone): | Yes | 
| Archived (hidden from active list): | No | 
| Created/modified: | 2022-03-15 15:11:46 | 
| Source code size: | 4840 bytes / 109 lines | 
| Pitched / IR pitched: | No / No | 
| Views / Downloads: | 503 / 616 | 
| Version history: | 14 change(s) | 
| Referenced in: | [show references] |