CProcessStatisticsReader.cpp 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.pledge = process_object.get("pledge").to_string();
  34. process.amount_virtual = process_object.get("amount_virtual").to_u32();
  35. process.amount_resident = process_object.get("amount_resident").to_u32();
  36. process.amount_shared = process_object.get("amount_shared").to_u32();
  37. process.amount_dirty_private = process_object.get("amount_dirty_private").to_u32();
  38. process.amount_clean_inode = process_object.get("amount_clean_inode").to_u32();
  39. process.amount_purgeable_volatile = process_object.get("amount_purgeable_volatile").to_u32();
  40. process.amount_purgeable_nonvolatile = process_object.get("amount_purgeable_nonvolatile").to_u32();
  41. process.icon_id = process_object.get("icon_id").to_int();
  42. auto& thread_array = process_object.get_ptr("threads")->as_array();
  43. process.threads.ensure_capacity(thread_array.size());
  44. thread_array.for_each([&](auto& value) {
  45. auto& thread_object = value.as_object();
  46. CThreadStatistics thread;
  47. thread.tid = thread_object.get("tid").to_u32();
  48. thread.times_scheduled = thread_object.get("times_scheduled").to_u32();
  49. thread.name = thread_object.get("name").to_string();
  50. thread.state = thread_object.get("state").to_string();
  51. thread.ticks = thread_object.get("ticks").to_u32();
  52. thread.priority = thread_object.get("priority").to_u32();
  53. thread.effective_priority = thread_object.get("effective_priority").to_u32();
  54. thread.syscall_count = thread_object.get("syscall_count").to_u32();
  55. thread.inode_faults = thread_object.get("inode_faults").to_u32();
  56. thread.zero_faults = thread_object.get("zero_faults").to_u32();
  57. thread.cow_faults = thread_object.get("cow_faults").to_u32();
  58. thread.unix_socket_read_bytes = thread_object.get("unix_socket_read_bytes").to_u32();
  59. thread.unix_socket_write_bytes = thread_object.get("unix_socket_write_bytes").to_u32();
  60. thread.ipv4_socket_read_bytes = thread_object.get("ipv4_socket_read_bytes").to_u32();
  61. thread.ipv4_socket_write_bytes = thread_object.get("ipv4_socket_write_bytes").to_u32();
  62. thread.file_read_bytes = thread_object.get("file_read_bytes").to_u32();
  63. thread.file_write_bytes = thread_object.get("file_write_bytes").to_u32();
  64. process.threads.append(move(thread));
  65. });
  66. // and synthetic data last
  67. process.username = username_from_uid(process.uid);
  68. map.set(process.pid, process);
  69. });
  70. return map;
  71. }
  72. String CProcessStatisticsReader::username_from_uid(uid_t uid)
  73. {
  74. if (s_usernames.is_empty()) {
  75. setpwent();
  76. while (auto* passwd = getpwent())
  77. s_usernames.set(passwd->pw_uid, passwd->pw_name);
  78. endpwent();
  79. }
  80. auto it = s_usernames.find(uid);
  81. if (it != s_usernames.end())
  82. return (*it).value;
  83. return String::number(uid);
  84. }