ProcessStatisticsReader.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteBuffer.h>
  7. #include <AK/JsonArray.h>
  8. #include <AK/JsonObject.h>
  9. #include <AK/JsonValue.h>
  10. #include <LibCore/File.h>
  11. #include <LibCore/ProcessStatisticsReader.h>
  12. #include <pwd.h>
  13. namespace Core {
  14. HashMap<uid_t, String> ProcessStatisticsReader::s_usernames;
  15. Optional<Vector<Core::ProcessStatistics>> ProcessStatisticsReader::get_all(RefPtr<Core::File>& proc_all_file)
  16. {
  17. if (proc_all_file) {
  18. if (!proc_all_file->seek(0, Core::SeekMode::SetPosition)) {
  19. warnln("ProcessStatisticsReader: Failed to refresh /proc/all: {}", proc_all_file->error_string());
  20. return {};
  21. }
  22. } else {
  23. proc_all_file = Core::File::construct("/proc/all");
  24. if (!proc_all_file->open(Core::OpenMode::ReadOnly)) {
  25. warnln("ProcessStatisticsReader: Failed to open /proc/all: {}", proc_all_file->error_string());
  26. return {};
  27. }
  28. }
  29. Vector<Core::ProcessStatistics> processes;
  30. auto file_contents = proc_all_file->read_all();
  31. auto json = JsonValue::from_string(file_contents);
  32. if (!json.has_value())
  33. return {};
  34. json.value().as_array().for_each([&](auto& value) {
  35. const JsonObject& process_object = value.as_object();
  36. Core::ProcessStatistics process;
  37. // kernel data first
  38. process.pid = process_object.get("pid").to_u32();
  39. process.pgid = process_object.get("pgid").to_u32();
  40. process.pgp = process_object.get("pgp").to_u32();
  41. process.sid = process_object.get("sid").to_u32();
  42. process.uid = process_object.get("uid").to_u32();
  43. process.gid = process_object.get("gid").to_u32();
  44. process.ppid = process_object.get("ppid").to_u32();
  45. process.nfds = process_object.get("nfds").to_u32();
  46. process.kernel = process_object.get("kernel").to_bool();
  47. process.name = process_object.get("name").to_string();
  48. process.executable = process_object.get("executable").to_string();
  49. process.tty = process_object.get("tty").to_string();
  50. process.pledge = process_object.get("pledge").to_string();
  51. process.veil = process_object.get("veil").to_string();
  52. process.amount_virtual = process_object.get("amount_virtual").to_u32();
  53. process.amount_resident = process_object.get("amount_resident").to_u32();
  54. process.amount_shared = process_object.get("amount_shared").to_u32();
  55. process.amount_dirty_private = process_object.get("amount_dirty_private").to_u32();
  56. process.amount_clean_inode = process_object.get("amount_clean_inode").to_u32();
  57. process.amount_purgeable_volatile = process_object.get("amount_purgeable_volatile").to_u32();
  58. process.amount_purgeable_nonvolatile = process_object.get("amount_purgeable_nonvolatile").to_u32();
  59. auto& thread_array = process_object.get_ptr("threads")->as_array();
  60. process.threads.ensure_capacity(thread_array.size());
  61. thread_array.for_each([&](auto& value) {
  62. auto& thread_object = value.as_object();
  63. Core::ThreadStatistics thread;
  64. thread.tid = thread_object.get("tid").to_u32();
  65. thread.times_scheduled = thread_object.get("times_scheduled").to_u32();
  66. thread.name = thread_object.get("name").to_string();
  67. thread.state = thread_object.get("state").to_string();
  68. thread.ticks_user = thread_object.get("ticks_user").to_u32();
  69. thread.ticks_kernel = thread_object.get("ticks_kernel").to_u32();
  70. thread.cpu = thread_object.get("cpu").to_u32();
  71. thread.priority = thread_object.get("priority").to_u32();
  72. thread.syscall_count = thread_object.get("syscall_count").to_u32();
  73. thread.inode_faults = thread_object.get("inode_faults").to_u32();
  74. thread.zero_faults = thread_object.get("zero_faults").to_u32();
  75. thread.cow_faults = thread_object.get("cow_faults").to_u32();
  76. thread.unix_socket_read_bytes = thread_object.get("unix_socket_read_bytes").to_u32();
  77. thread.unix_socket_write_bytes = thread_object.get("unix_socket_write_bytes").to_u32();
  78. thread.ipv4_socket_read_bytes = thread_object.get("ipv4_socket_read_bytes").to_u32();
  79. thread.ipv4_socket_write_bytes = thread_object.get("ipv4_socket_write_bytes").to_u32();
  80. thread.file_read_bytes = thread_object.get("file_read_bytes").to_u32();
  81. thread.file_write_bytes = thread_object.get("file_write_bytes").to_u32();
  82. process.threads.append(move(thread));
  83. });
  84. // and synthetic data last
  85. process.username = username_from_uid(process.uid);
  86. processes.append(move(process));
  87. });
  88. return processes;
  89. }
  90. Optional<Vector<Core::ProcessStatistics>> ProcessStatisticsReader::get_all()
  91. {
  92. RefPtr<Core::File> proc_all_file;
  93. return get_all(proc_all_file);
  94. }
  95. String ProcessStatisticsReader::username_from_uid(uid_t uid)
  96. {
  97. if (s_usernames.is_empty()) {
  98. setpwent();
  99. while (auto* passwd = getpwent())
  100. s_usernames.set(passwd->pw_uid, passwd->pw_name);
  101. endpwent();
  102. }
  103. auto it = s_usernames.find(uid);
  104. if (it != s_usernames.end())
  105. return (*it).value;
  106. return String::number(uid);
  107. }
  108. }