暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

readproc.rs 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. use procinfo::pid;
  2. use procinfo::loadavg;
  3. /// Returns the PID and PPID of the current process.
  4. /// Throws an error if the current process doesn't exist (should never occur).
  5. pub fn self_pids() -> Result<(i32, i32), &'static str> {
  6. match pid::stat_self() {
  7. Ok(stat) => Ok((stat.pid, stat.ppid)),
  8. Err(_) => Err("PID not alive: PID and PPID not found"),
  9. }
  10. }
  11. /// Returns the command (string) belonging to the given PID.
  12. /// Throws an error if the given PID doesn't exist.
  13. pub fn get_pid_command(pid: i32) -> Result<String, &'static str> {
  14. match pid::stat(pid) {
  15. Ok(stat) => Ok(stat.command),
  16. Err(_) => Err("PID not alive: no command name found"),
  17. }
  18. }
  19. /// Returns the last created command (string) of the system.
  20. /// Throws an error if there is no last Command.
  21. pub fn get_last_created_command() -> Result<String, &'static str> {
  22. match loadavg() {
  23. Ok(stat) => {
  24. let last_pid = stat.last_created_pid;
  25. match pid::stat(last_pid) {
  26. Ok(st) => Ok(st.command),
  27. Err(_) => Err("No last command via PID found"),
  28. }
  29. }
  30. Err(_) => Err("No last command found"),
  31. }
  32. }
  33. /// Returns the number of threads belonging to the given PID.
  34. /// Throws an error if the given PID doesn't exist.
  35. pub fn get_thread_count(pid: i32) -> Result<u32, &'static str> {
  36. match pid::stat(pid) {
  37. Ok(stat) => Ok(stat.num_threads as u32),
  38. Err(_) => Err("PID not alive: no threads counted"),
  39. }
  40. }
  41. /// Returns the number of total tasks running in the system.
  42. /// Throws an error if the total number of tasks doesn't exist.
  43. pub fn get_task_total() -> Result<u32, &'static str> {
  44. match loadavg() {
  45. Ok(stat) => Ok(stat.tasks_total),
  46. Err(_) => Err("No total count of tasks in system found"),
  47. }
  48. }
  49. /// Returns the size of the virtual, code and data memory size of the current process.
  50. /// Throws an error if the current process doesn't exist (should never occur).
  51. pub fn get_ownprocess_mem() -> Result<(usize, usize, usize), &'static str> {
  52. match pid::stat_self() {
  53. Ok(stat) => {
  54. let csize = stat.end_code - stat.start_code;
  55. let dsize = stat.end_data - stat.start_data;
  56. Ok((stat.vsize, csize, dsize))
  57. }
  58. Err(_) => Err("PID not alive: no memory found"),
  59. }
  60. }