ProcessStatisticsReader.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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<AllProcessesStatistics> 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. AllProcessesStatistics all_processes_statistics;
  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. auto& json_obj = json.value().as_object();
  35. json_obj.get("processes").as_array().for_each([&](auto& value) {
  36. const JsonObject& process_object = value.as_object();
  37. Core::ProcessStatistics process;
  38. // kernel data first
  39. process.pid = process_object.get("pid").to_u32();
  40. process.pgid = process_object.get("pgid").to_u32();
  41. process.pgp = process_object.get("pgp").to_u32();
  42. process.sid = process_object.get("sid").to_u32();
  43. process.uid = process_object.get("uid").to_u32();
  44. process.gid = process_object.get("gid").to_u32();
  45. process.ppid = process_object.get("ppid").to_u32();
  46. process.nfds = process_object.get("nfds").to_u32();
  47. process.kernel = process_object.get("kernel").to_bool();
  48. process.name = process_object.get("name").to_string();
  49. process.executable = process_object.get("executable").to_string();
  50. process.tty = process_object.get("tty").to_string();
  51. process.pledge = process_object.get("pledge").to_string();
  52. process.veil = process_object.get("veil").to_string();
  53. process.amount_virtual = process_object.get("amount_virtual").to_u32();
  54. process.amount_resident = process_object.get("amount_resident").to_u32();
  55. process.amount_shared = process_object.get("amount_shared").to_u32();
  56. process.amount_dirty_private = process_object.get("amount_dirty_private").to_u32();
  57. process.amount_clean_inode = process_object.get("amount_clean_inode").to_u32();
  58. process.amount_purgeable_volatile = process_object.get("amount_purgeable_volatile").to_u32();
  59. process.amount_purgeable_nonvolatile = process_object.get("amount_purgeable_nonvolatile").to_u32();
  60. auto& thread_array = process_object.get_ptr("threads")->as_array();
  61. process.threads.ensure_capacity(thread_array.size());
  62. thread_array.for_each([&](auto& value) {
  63. auto& thread_object = value.as_object();
  64. Core::ThreadStatistics thread;
  65. thread.tid = thread_object.get("tid").to_u32();
  66. thread.times_scheduled = thread_object.get("times_scheduled").to_u32();
  67. thread.name = thread_object.get("name").to_string();
  68. thread.state = thread_object.get("state").to_string();
  69. thread.ticks_user = thread_object.get("ticks_user").to_u32();
  70. thread.ticks_kernel = thread_object.get("ticks_kernel").to_u32();
  71. thread.cpu = thread_object.get("cpu").to_u32();
  72. thread.priority = thread_object.get("priority").to_u32();
  73. thread.syscall_count = thread_object.get("syscall_count").to_u32();
  74. thread.inode_faults = thread_object.get("inode_faults").to_u32();
  75. thread.zero_faults = thread_object.get("zero_faults").to_u32();
  76. thread.cow_faults = thread_object.get("cow_faults").to_u32();
  77. thread.unix_socket_read_bytes = thread_object.get("unix_socket_read_bytes").to_u32();
  78. thread.unix_socket_write_bytes = thread_object.get("unix_socket_write_bytes").to_u32();
  79. thread.ipv4_socket_read_bytes = thread_object.get("ipv4_socket_read_bytes").to_u32();
  80. thread.ipv4_socket_write_bytes = thread_object.get("ipv4_socket_write_bytes").to_u32();
  81. thread.file_read_bytes = thread_object.get("file_read_bytes").to_u32();
  82. thread.file_write_bytes = thread_object.get("file_write_bytes").to_u32();
  83. process.threads.append(move(thread));
  84. });
  85. // and synthetic data last
  86. process.username = username_from_uid(process.uid);
  87. all_processes_statistics.processes.append(move(process));
  88. });
  89. all_processes_statistics.total_ticks_scheduled = json_obj.get("total_ticks").to_u64();
  90. all_processes_statistics.total_ticks_scheduled_kernel = json_obj.get("total_ticks_kernel").to_u64();
  91. return all_processes_statistics;
  92. }
  93. Optional<AllProcessesStatistics> ProcessStatisticsReader::get_all()
  94. {
  95. RefPtr<Core::File> proc_all_file;
  96. return get_all(proc_all_file);
  97. }
  98. String ProcessStatisticsReader::username_from_uid(uid_t uid)
  99. {
  100. if (s_usernames.is_empty()) {
  101. setpwent();
  102. while (auto* passwd = getpwent())
  103. s_usernames.set(passwd->pw_uid, passwd->pw_name);
  104. endpwent();
  105. }
  106. auto it = s_usernames.find(uid);
  107. if (it != s_usernames.end())
  108. return (*it).value;
  109. return String::number(uid);
  110. }
  111. }