CProcessStatisticsReader.cpp 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <AK/JsonArray.h>
  2. #include <AK/JsonObject.h>
  3. #include <AK/JsonValue.h>
  4. #include <LibCore/CFile.h>
  5. #include <LibCore/CProcessStatisticsReader.h>
  6. #include <pwd.h>
  7. #include <stdio.h>
  8. HashMap<uid_t, String> CProcessStatisticsReader::s_usernames;
  9. HashMap<pid_t, CProcessStatistics> CProcessStatisticsReader::get_all()
  10. {
  11. auto file = CFile::construct("/proc/all");
  12. if (!file->open(CIODevice::ReadOnly)) {
  13. fprintf(stderr, "CProcessStatisticsReader: Failed to open /proc/all: %s\n", file->error_string());
  14. return {};
  15. }
  16. HashMap<pid_t, CProcessStatistics> map;
  17. auto file_contents = file->read_all();
  18. auto json = JsonValue::from_string({ file_contents.data(), (size_t)file_contents.size() });
  19. json.as_array().for_each([&](auto& value) {
  20. const JsonObject& process_object = value.as_object();
  21. CProcessStatistics process;
  22. // kernel data first
  23. process.pid = process_object.get("pid").to_u32();
  24. process.pgid = process_object.get("pgid").to_u32();
  25. process.pgp = process_object.get("pgp").to_u32();
  26. process.sid = process_object.get("sid").to_u32();
  27. process.uid = process_object.get("uid").to_u32();
  28. process.gid = process_object.get("gid").to_u32();
  29. process.ppid = process_object.get("ppid").to_u32();
  30. process.nfds = process_object.get("nfds").to_u32();
  31. process.name = process_object.get("name").to_string();
  32. process.tty = process_object.get("tty").to_string();
  33. process.amount_virtual = process_object.get("amount_virtual").to_u32();
  34. process.amount_resident = process_object.get("amount_resident").to_u32();
  35. process.amount_shared = process_object.get("amount_shared").to_u32();
  36. process.amount_dirty_private = process_object.get("amount_dirty_private").to_u32();
  37. process.amount_clean_inode = process_object.get("amount_clean_inode").to_u32();
  38. process.amount_purgeable_volatile = process_object.get("amount_purgeable_volatile").to_u32();
  39. process.amount_purgeable_nonvolatile = process_object.get("amount_purgeable_nonvolatile").to_u32();
  40. process.icon_id = process_object.get("icon_id").to_int();
  41. auto& thread_array = process_object.get_ptr("threads")->as_array();
  42. process.threads.ensure_capacity(thread_array.size());
  43. thread_array.for_each([&](auto& value) {
  44. auto& thread_object = value.as_object();
  45. CThreadStatistics thread;
  46. thread.tid = thread_object.get("tid").to_u32();
  47. thread.times_scheduled = thread_object.get("times_scheduled").to_u32();
  48. thread.name = thread_object.get("name").to_string();
  49. thread.state = thread_object.get("state").to_string();
  50. thread.ticks = thread_object.get("ticks").to_u32();
  51. thread.priority = thread_object.get("priority").to_string();
  52. thread.syscall_count = thread_object.get("syscall_count").to_u32();
  53. thread.inode_faults = thread_object.get("inode_faults").to_u32();
  54. thread.zero_faults = thread_object.get("zero_faults").to_u32();
  55. thread.cow_faults = thread_object.get("cow_faults").to_u32();
  56. thread.unix_socket_read_bytes = thread_object.get("unix_socket_read_bytes").to_u32();
  57. thread.unix_socket_write_bytes = thread_object.get("unix_socket_write_bytes").to_u32();
  58. thread.ipv4_socket_read_bytes = thread_object.get("ipv4_socket_read_bytes").to_u32();
  59. thread.ipv4_socket_write_bytes = thread_object.get("ipv4_socket_write_bytes").to_u32();
  60. thread.file_read_bytes = thread_object.get("file_read_bytes").to_u32();
  61. thread.file_write_bytes = thread_object.get("file_write_bytes").to_u32();
  62. process.threads.append(move(thread));
  63. });
  64. // and synthetic data last
  65. process.username = username_from_uid(process.uid);
  66. map.set(process.pid, process);
  67. });
  68. return map;
  69. }
  70. String CProcessStatisticsReader::username_from_uid(uid_t uid)
  71. {
  72. if (s_usernames.is_empty()) {
  73. setpwent();
  74. while (auto* passwd = getpwent())
  75. s_usernames.set(passwd->pw_uid, passwd->pw_name);
  76. endpwent();
  77. }
  78. auto it = s_usernames.find(uid);
  79. if (it != s_usernames.end())
  80. return (*it).value;
  81. return String::number(uid);
  82. }