ProcessStatisticsReader.cpp 5.9 KB

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