ProcessStatisticsLinux.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/String.h>
  7. #include <LibCore/File.h>
  8. #include <LibWebView/Platform/ProcessStatistics.h>
  9. #include <unistd.h>
  10. namespace WebView {
  11. static auto user_hz = sysconf(_SC_CLK_TCK);
  12. static auto page_size = sysconf(_SC_PAGESIZE);
  13. static auto ncpu_online = sysconf(_SC_NPROCESSORS_ONLN);
  14. ErrorOr<void> update_process_statistics(ProcessStatistics& statistics)
  15. {
  16. // Read the total time scheduled from /proc/stat, and each process's usage from /proc/pid/stat
  17. // Calculate the CPU percentage for each process based on the total time scheduled and the time spent in the process
  18. static auto proc_stat = TRY(Core::File::open("/proc/stat"sv, Core::File::OpenMode::Read));
  19. TRY(proc_stat->seek(0, SeekMode::SetPosition));
  20. char buf[1024] = {};
  21. auto buffer = Bytes { buf, sizeof(buf) };
  22. auto line = TRY(proc_stat->read_some(buffer));
  23. int user_time = 0;
  24. int system_time = 0;
  25. int idle_time = 0;
  26. int irq_time = 0;
  27. int softirq_time = 0;
  28. // user, nice (ignored), system, idle, iowait (ignored), irq, softirq, steal (ignored), guest (ignored), guest_nice (ignored)
  29. int res = sscanf(reinterpret_cast<char const*>(line.data()), "cpu %d %*d %d %d %*d %d %d", &user_time, &system_time, &idle_time, &irq_time, &softirq_time);
  30. if (res != 5)
  31. return Error::from_string_literal("Failed to parse /proc/stat");
  32. u64 const total_time_scheduled = user_time + system_time + idle_time + irq_time + softirq_time;
  33. float const total_time_scheduled_diff = static_cast<float>(total_time_scheduled) - statistics.total_time_scheduled;
  34. statistics.total_time_scheduled = total_time_scheduled;
  35. for (auto& process : statistics.processes) {
  36. auto proc_pid_stat_or_error = Core::File::open(MUST(String::formatted("/proc/{}/stat", process.pid)), Core::File::OpenMode::Read);
  37. if (proc_pid_stat_or_error.is_error()) {
  38. // FIXME: Remove stale process from process list?
  39. continue;
  40. }
  41. auto proc_pid_stat = proc_pid_stat_or_error.release_value();
  42. line = TRY(proc_pid_stat->read_some(buffer));
  43. // We only care about fields 14 (utime), 15 (stime), and 24 (rss)
  44. unsigned long utime = 0;
  45. unsigned long stime = 0;
  46. long rss = 0;
  47. // 1 2 3 4 5 6 7 8 9 10 11 12 13 14* 15* 16 17 18 19 20 21 22 23 24*
  48. res = sscanf(reinterpret_cast<char const*>(line.data()), "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu %*d %*d %*d %*d %*d %*d %*u %*u %ld", &utime, &stime, &rss);
  49. if (res != 3)
  50. return Error::from_string_literal("Failed to parse /proc/pid/stat");
  51. process.memory_usage_bytes = rss * page_size;
  52. u64 const time_process = utime + stime;
  53. float const time_scheduled_diff = static_cast<float>(time_process) - process.time_spent_in_process;
  54. process.time_spent_in_process = time_process;
  55. process.cpu_percent = 0.0;
  56. if (total_time_scheduled_diff > 0) {
  57. process.cpu_percent = time_scheduled_diff / (total_time_scheduled_diff / ncpu_online) * 100.0f;
  58. }
  59. }
  60. return {};
  61. }
  62. }