ProcessStatisticsReader.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/ByteBuffer.h>
  27. #include <AK/JsonArray.h>
  28. #include <AK/JsonObject.h>
  29. #include <AK/JsonValue.h>
  30. #include <LibCore/File.h>
  31. #include <LibCore/ProcessStatisticsReader.h>
  32. #include <pwd.h>
  33. #include <stdio.h>
  34. namespace Core {
  35. HashMap<uid_t, String> ProcessStatisticsReader::s_usernames;
  36. Optional<HashMap<pid_t, Core::ProcessStatistics>> ProcessStatisticsReader::get_all(RefPtr<Core::File>& proc_all_file)
  37. {
  38. if (proc_all_file) {
  39. if (!proc_all_file->seek(0, Core::File::SeekMode::SetPosition)) {
  40. fprintf(stderr, "ProcessStatisticsReader: Failed to refresh /proc/all: %s\n", proc_all_file->error_string());
  41. return {};
  42. }
  43. } else {
  44. proc_all_file = Core::File::construct("/proc/all");
  45. if (!proc_all_file->open(Core::IODevice::ReadOnly)) {
  46. fprintf(stderr, "ProcessStatisticsReader: Failed to open /proc/all: %s\n", proc_all_file->error_string());
  47. return {};
  48. }
  49. }
  50. HashMap<pid_t, Core::ProcessStatistics> map;
  51. auto file_contents = proc_all_file->read_all();
  52. auto json = JsonValue::from_string(file_contents);
  53. if (!json.has_value())
  54. return {};
  55. json.value().as_array().for_each([&](auto& value) {
  56. const JsonObject& process_object = value.as_object();
  57. Core::ProcessStatistics process;
  58. // kernel data first
  59. process.pid = process_object.get("pid").to_u32();
  60. process.pgid = process_object.get("pgid").to_u32();
  61. process.pgp = process_object.get("pgp").to_u32();
  62. process.sid = process_object.get("sid").to_u32();
  63. process.uid = process_object.get("uid").to_u32();
  64. process.gid = process_object.get("gid").to_u32();
  65. process.ppid = process_object.get("ppid").to_u32();
  66. process.nfds = process_object.get("nfds").to_u32();
  67. process.name = process_object.get("name").to_string();
  68. process.executable = process_object.get("executable").to_string();
  69. process.tty = process_object.get("tty").to_string();
  70. process.pledge = process_object.get("pledge").to_string();
  71. process.veil = process_object.get("veil").to_string();
  72. process.amount_virtual = process_object.get("amount_virtual").to_u32();
  73. process.amount_resident = process_object.get("amount_resident").to_u32();
  74. process.amount_shared = process_object.get("amount_shared").to_u32();
  75. process.amount_dirty_private = process_object.get("amount_dirty_private").to_u32();
  76. process.amount_clean_inode = process_object.get("amount_clean_inode").to_u32();
  77. process.amount_purgeable_volatile = process_object.get("amount_purgeable_volatile").to_u32();
  78. process.amount_purgeable_nonvolatile = process_object.get("amount_purgeable_nonvolatile").to_u32();
  79. auto& thread_array = process_object.get_ptr("threads")->as_array();
  80. process.threads.ensure_capacity(thread_array.size());
  81. thread_array.for_each([&](auto& value) {
  82. auto& thread_object = value.as_object();
  83. Core::ThreadStatistics thread;
  84. thread.tid = thread_object.get("tid").to_u32();
  85. thread.times_scheduled = thread_object.get("times_scheduled").to_u32();
  86. thread.name = thread_object.get("name").to_string();
  87. thread.state = thread_object.get("state").to_string();
  88. thread.ticks_user = thread_object.get("ticks_user").to_u32();
  89. thread.ticks_kernel = thread_object.get("ticks_kernel").to_u32();
  90. thread.cpu = thread_object.get("cpu").to_u32();
  91. thread.priority = thread_object.get("priority").to_u32();
  92. thread.syscall_count = thread_object.get("syscall_count").to_u32();
  93. thread.inode_faults = thread_object.get("inode_faults").to_u32();
  94. thread.zero_faults = thread_object.get("zero_faults").to_u32();
  95. thread.cow_faults = thread_object.get("cow_faults").to_u32();
  96. thread.unix_socket_read_bytes = thread_object.get("unix_socket_read_bytes").to_u32();
  97. thread.unix_socket_write_bytes = thread_object.get("unix_socket_write_bytes").to_u32();
  98. thread.ipv4_socket_read_bytes = thread_object.get("ipv4_socket_read_bytes").to_u32();
  99. thread.ipv4_socket_write_bytes = thread_object.get("ipv4_socket_write_bytes").to_u32();
  100. thread.file_read_bytes = thread_object.get("file_read_bytes").to_u32();
  101. thread.file_write_bytes = thread_object.get("file_write_bytes").to_u32();
  102. process.threads.append(move(thread));
  103. });
  104. // and synthetic data last
  105. process.username = username_from_uid(process.uid);
  106. map.set(process.pid, process);
  107. });
  108. return map;
  109. }
  110. Optional<HashMap<pid_t, Core::ProcessStatistics>> ProcessStatisticsReader::get_all()
  111. {
  112. RefPtr<Core::File> proc_all_file;
  113. return get_all(proc_all_file);
  114. }
  115. String ProcessStatisticsReader::username_from_uid(uid_t uid)
  116. {
  117. if (s_usernames.is_empty()) {
  118. setpwent();
  119. while (auto* passwd = getpwent())
  120. s_usernames.set(passwd->pw_uid, passwd->pw_name);
  121. endpwent();
  122. }
  123. auto it = s_usernames.find(uid);
  124. if (it != s_usernames.end())
  125. return (*it).value;
  126. return String::number(uid);
  127. }
  128. }