ProcessStatisticsReader.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/JsonArray.h>
  27. #include <AK/JsonObject.h>
  28. #include <AK/JsonValue.h>
  29. #include <LibCore/File.h>
  30. #include <LibCore/ProcessStatisticsReader.h>
  31. #include <pwd.h>
  32. #include <stdio.h>
  33. namespace Core {
  34. HashMap<uid_t, String> ProcessStatisticsReader::s_usernames;
  35. HashMap<pid_t, Core::ProcessStatistics> ProcessStatisticsReader::get_all()
  36. {
  37. auto file = Core::File::construct("/proc/all");
  38. if (!file->open(Core::IODevice::ReadOnly)) {
  39. fprintf(stderr, "ProcessStatisticsReader: Failed to open /proc/all: %s\n", file->error_string());
  40. return {};
  41. }
  42. HashMap<pid_t, Core::ProcessStatistics> map;
  43. auto file_contents = file->read_all();
  44. auto json = JsonValue::from_string(file_contents);
  45. ASSERT(json.has_value());
  46. json.value().as_array().for_each([&](auto& value) {
  47. const JsonObject& process_object = value.as_object();
  48. Core::ProcessStatistics process;
  49. // kernel data first
  50. process.pid = process_object.get("pid").to_u32();
  51. process.pgid = process_object.get("pgid").to_u32();
  52. process.pgp = process_object.get("pgp").to_u32();
  53. process.sid = process_object.get("sid").to_u32();
  54. process.uid = process_object.get("uid").to_u32();
  55. process.gid = process_object.get("gid").to_u32();
  56. process.ppid = process_object.get("ppid").to_u32();
  57. process.nfds = process_object.get("nfds").to_u32();
  58. process.name = process_object.get("name").to_string();
  59. process.tty = process_object.get("tty").to_string();
  60. process.pledge = process_object.get("pledge").to_string();
  61. process.veil = process_object.get("veil").to_string();
  62. process.amount_virtual = process_object.get("amount_virtual").to_u32();
  63. process.amount_resident = process_object.get("amount_resident").to_u32();
  64. process.amount_shared = process_object.get("amount_shared").to_u32();
  65. process.amount_dirty_private = process_object.get("amount_dirty_private").to_u32();
  66. process.amount_clean_inode = process_object.get("amount_clean_inode").to_u32();
  67. process.amount_purgeable_volatile = process_object.get("amount_purgeable_volatile").to_u32();
  68. process.amount_purgeable_nonvolatile = process_object.get("amount_purgeable_nonvolatile").to_u32();
  69. process.icon_id = process_object.get("icon_id").to_int();
  70. auto& thread_array = process_object.get_ptr("threads")->as_array();
  71. process.threads.ensure_capacity(thread_array.size());
  72. thread_array.for_each([&](auto& value) {
  73. auto& thread_object = value.as_object();
  74. Core::ThreadStatistics thread;
  75. thread.tid = thread_object.get("tid").to_u32();
  76. thread.times_scheduled = thread_object.get("times_scheduled").to_u32();
  77. thread.name = thread_object.get("name").to_string();
  78. thread.state = thread_object.get("state").to_string();
  79. thread.ticks = thread_object.get("ticks").to_u32();
  80. thread.cpu = thread_object.get("cpu").to_u32();
  81. thread.priority = thread_object.get("priority").to_u32();
  82. thread.effective_priority = thread_object.get("effective_priority").to_u32();
  83. thread.syscall_count = thread_object.get("syscall_count").to_u32();
  84. thread.inode_faults = thread_object.get("inode_faults").to_u32();
  85. thread.zero_faults = thread_object.get("zero_faults").to_u32();
  86. thread.cow_faults = thread_object.get("cow_faults").to_u32();
  87. thread.unix_socket_read_bytes = thread_object.get("unix_socket_read_bytes").to_u32();
  88. thread.unix_socket_write_bytes = thread_object.get("unix_socket_write_bytes").to_u32();
  89. thread.ipv4_socket_read_bytes = thread_object.get("ipv4_socket_read_bytes").to_u32();
  90. thread.ipv4_socket_write_bytes = thread_object.get("ipv4_socket_write_bytes").to_u32();
  91. thread.file_read_bytes = thread_object.get("file_read_bytes").to_u32();
  92. thread.file_write_bytes = thread_object.get("file_write_bytes").to_u32();
  93. process.threads.append(move(thread));
  94. });
  95. // and synthetic data last
  96. process.username = username_from_uid(process.uid);
  97. map.set(process.pid, process);
  98. });
  99. return map;
  100. }
  101. String ProcessStatisticsReader::username_from_uid(uid_t uid)
  102. {
  103. if (s_usernames.is_empty()) {
  104. setpwent();
  105. while (auto* passwd = getpwent())
  106. s_usernames.set(passwd->pw_uid, passwd->pw_name);
  107. endpwent();
  108. }
  109. auto it = s_usernames.find(uid);
  110. if (it != s_usernames.end())
  111. return (*it).value;
  112. return String::number(uid);
  113. }
  114. }