ProcessStatisticsReader.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2018-2020, 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. #include <stdio.h>
  14. namespace Core {
  15. HashMap<uid_t, String> ProcessStatisticsReader::s_usernames;
  16. Optional<HashMap<pid_t, Core::ProcessStatistics>> ProcessStatisticsReader::get_all(RefPtr<Core::File>& proc_all_file)
  17. {
  18. if (proc_all_file) {
  19. if (!proc_all_file->seek(0, Core::SeekMode::SetPosition)) {
  20. fprintf(stderr, "ProcessStatisticsReader: Failed to refresh /proc/all: %s\n", proc_all_file->error_string());
  21. return {};
  22. }
  23. } else {
  24. proc_all_file = Core::File::construct("/proc/all");
  25. if (!proc_all_file->open(Core::OpenMode::ReadOnly)) {
  26. fprintf(stderr, "ProcessStatisticsReader: Failed to open /proc/all: %s\n", proc_all_file->error_string());
  27. return {};
  28. }
  29. }
  30. HashMap<pid_t, Core::ProcessStatistics> map;
  31. auto file_contents = proc_all_file->read_all();
  32. auto json = JsonValue::from_string(file_contents);
  33. if (!json.has_value())
  34. return {};
  35. json.value().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. map.set(process.pid, process);
  88. });
  89. return map;
  90. }
  91. Optional<HashMap<pid_t, Core::ProcessStatistics>> ProcessStatisticsReader::get_all()
  92. {
  93. RefPtr<Core::File> proc_all_file;
  94. return get_all(proc_all_file);
  95. }
  96. String ProcessStatisticsReader::username_from_uid(uid_t uid)
  97. {
  98. if (s_usernames.is_empty()) {
  99. setpwent();
  100. while (auto* passwd = getpwent())
  101. s_usernames.set(passwd->pw_uid, passwd->pw_name);
  102. endpwent();
  103. }
  104. auto it = s_usernames.find(uid);
  105. if (it != s_usernames.end())
  106. return (*it).value;
  107. return String::number(uid);
  108. }
  109. }