top.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/HashMap.h>
  7. #include <AK/JsonArray.h>
  8. #include <AK/JsonObject.h>
  9. #include <AK/QuickSort.h>
  10. #include <AK/String.h>
  11. #include <AK/Vector.h>
  12. #include <LibCore/ArgsParser.h>
  13. #include <LibCore/ProcessStatisticsReader.h>
  14. #include <LibCore/System.h>
  15. #include <LibMain/Main.h>
  16. #include <signal.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <sys/ioctl.h>
  20. #include <unistd.h>
  21. struct TopOption {
  22. enum class SortBy {
  23. Pid,
  24. Tid,
  25. Priority,
  26. UserName,
  27. State,
  28. Virt,
  29. Phys,
  30. Cpu,
  31. Name
  32. };
  33. SortBy sort_by { SortBy::Cpu };
  34. int delay_time { 1 };
  35. };
  36. struct ThreadData {
  37. int tid;
  38. pid_t pid;
  39. pid_t pgid;
  40. pid_t pgp;
  41. pid_t sid;
  42. uid_t uid;
  43. gid_t gid;
  44. pid_t ppid;
  45. unsigned nfds;
  46. String name;
  47. String tty;
  48. size_t amount_virtual;
  49. size_t amount_resident;
  50. size_t amount_shared;
  51. unsigned syscall_count;
  52. unsigned inode_faults;
  53. unsigned zero_faults;
  54. unsigned cow_faults;
  55. u64 time_scheduled;
  56. u64 time_scheduled_since_prev { 0 };
  57. unsigned cpu_percent { 0 };
  58. unsigned cpu_percent_decimal { 0 };
  59. u32 priority;
  60. String username;
  61. String state;
  62. };
  63. struct PidAndTid {
  64. bool operator==(PidAndTid const& other) const
  65. {
  66. return pid == other.pid && tid == other.tid;
  67. }
  68. pid_t pid;
  69. int tid;
  70. };
  71. namespace AK {
  72. template<>
  73. struct Traits<PidAndTid> : public GenericTraits<PidAndTid> {
  74. static unsigned hash(PidAndTid const& value) { return pair_int_hash(value.pid, value.tid); }
  75. };
  76. }
  77. struct Snapshot {
  78. HashMap<PidAndTid, ThreadData> map;
  79. u64 total_time_scheduled { 0 };
  80. u64 total_time_scheduled_kernel { 0 };
  81. };
  82. static Snapshot get_snapshot()
  83. {
  84. auto all_processes = Core::ProcessStatisticsReader::get_all();
  85. if (!all_processes.has_value())
  86. return {};
  87. Snapshot snapshot;
  88. for (auto& process : all_processes.value().processes) {
  89. for (auto& thread : process.threads) {
  90. ThreadData thread_data;
  91. thread_data.tid = thread.tid;
  92. thread_data.pid = process.pid;
  93. thread_data.pgid = process.pgid;
  94. thread_data.pgp = process.pgp;
  95. thread_data.sid = process.sid;
  96. thread_data.uid = process.uid;
  97. thread_data.gid = process.gid;
  98. thread_data.ppid = process.ppid;
  99. thread_data.nfds = process.nfds;
  100. thread_data.name = process.name;
  101. thread_data.tty = process.tty;
  102. thread_data.amount_virtual = process.amount_virtual;
  103. thread_data.amount_resident = process.amount_resident;
  104. thread_data.amount_shared = process.amount_shared;
  105. thread_data.syscall_count = thread.syscall_count;
  106. thread_data.inode_faults = thread.inode_faults;
  107. thread_data.zero_faults = thread.zero_faults;
  108. thread_data.cow_faults = thread.cow_faults;
  109. thread_data.time_scheduled = (u64)thread.time_user + (u64)thread.time_kernel;
  110. thread_data.priority = thread.priority;
  111. thread_data.state = thread.state;
  112. thread_data.username = process.username;
  113. snapshot.map.set({ process.pid, thread.tid }, move(thread_data));
  114. }
  115. }
  116. snapshot.total_time_scheduled = all_processes->total_time_scheduled;
  117. snapshot.total_time_scheduled_kernel = all_processes->total_time_scheduled_kernel;
  118. return snapshot;
  119. }
  120. static bool g_window_size_changed = true;
  121. static struct winsize g_window_size;
  122. static void parse_args(Main::Arguments arguments, TopOption& top_option)
  123. {
  124. Core::ArgsParser::Option sort_by_option {
  125. Core::ArgsParser::OptionArgumentMode::Required,
  126. "Sort by field [pid, tid, pri, user, state, virt, phys, cpu, name]",
  127. "sort-by",
  128. 's',
  129. nullptr,
  130. [&top_option](char const* s) {
  131. StringView sort_by_option { s, strlen(s) };
  132. if (sort_by_option == "pid"sv)
  133. top_option.sort_by = TopOption::SortBy::Pid;
  134. else if (sort_by_option == "tid"sv)
  135. top_option.sort_by = TopOption::SortBy::Tid;
  136. else if (sort_by_option == "pri"sv)
  137. top_option.sort_by = TopOption::SortBy::Priority;
  138. else if (sort_by_option == "user"sv)
  139. top_option.sort_by = TopOption::SortBy::UserName;
  140. else if (sort_by_option == "state"sv)
  141. top_option.sort_by = TopOption::SortBy::State;
  142. else if (sort_by_option == "virt"sv)
  143. top_option.sort_by = TopOption::SortBy::Virt;
  144. else if (sort_by_option == "phys"sv)
  145. top_option.sort_by = TopOption::SortBy::Phys;
  146. else if (sort_by_option == "cpu"sv)
  147. top_option.sort_by = TopOption::SortBy::Cpu;
  148. else if (sort_by_option == "name"sv)
  149. top_option.sort_by = TopOption::SortBy::Name;
  150. else
  151. return false;
  152. return true;
  153. }
  154. };
  155. Core::ArgsParser args_parser;
  156. args_parser.set_general_help("Display information about processes");
  157. args_parser.add_option(top_option.delay_time, "Delay time interval in seconds", "delay-time", 'd', nullptr);
  158. args_parser.add_option(move(sort_by_option));
  159. args_parser.parse(arguments);
  160. }
  161. static bool check_quit()
  162. {
  163. char c = '\0';
  164. read(STDIN_FILENO, &c, sizeof(c));
  165. return c == 'q' || c == 'Q';
  166. }
  167. static int g_old_stdin;
  168. static void restore_stdin()
  169. {
  170. fcntl(STDIN_FILENO, F_SETFL, g_old_stdin);
  171. }
  172. static void enable_nonblocking_stdin()
  173. {
  174. g_old_stdin = fcntl(STDIN_FILENO, F_GETFL);
  175. fcntl(STDIN_FILENO, F_SETFL, g_old_stdin | O_NONBLOCK);
  176. atexit(restore_stdin);
  177. }
  178. ErrorOr<int> serenity_main(Main::Arguments arguments)
  179. {
  180. TRY(Core::System::pledge("stdio rpath tty sigaction"));
  181. TRY(Core::System::unveil("/sys/kernel/processes", "r"));
  182. TRY(Core::System::unveil("/etc/passwd", "r"));
  183. unveil(nullptr, nullptr);
  184. signal(SIGWINCH, [](int) {
  185. g_window_size_changed = true;
  186. });
  187. TRY(Core::System::pledge("stdio rpath tty"));
  188. TopOption top_option;
  189. parse_args(arguments, top_option);
  190. enable_nonblocking_stdin();
  191. Vector<ThreadData*> threads;
  192. auto prev = get_snapshot();
  193. usleep(10000);
  194. for (;;) {
  195. if (g_window_size_changed) {
  196. TRY(Core::System::ioctl(STDOUT_FILENO, TIOCGWINSZ, &g_window_size));
  197. g_window_size_changed = false;
  198. }
  199. auto current = get_snapshot();
  200. auto total_scheduled_diff = current.total_time_scheduled - prev.total_time_scheduled;
  201. printf("\033[3J\033[H\033[2J");
  202. printf("\033[47;30m%6s %3s %3s %-9s %-13s %6s %6s %4s %s\033[K\033[0m\n",
  203. "PID",
  204. "TID",
  205. "PRI",
  206. "USER",
  207. "STATE",
  208. "VIRT",
  209. "PHYS",
  210. "%CPU",
  211. "NAME");
  212. for (auto& it : current.map) {
  213. auto pid_and_tid = it.key;
  214. if (pid_and_tid.pid == 0)
  215. continue;
  216. auto jt = prev.map.find(pid_and_tid);
  217. if (jt == prev.map.end())
  218. continue;
  219. auto time_scheduled_before = (*jt).value.time_scheduled;
  220. auto time_scheduled_diff = it.value.time_scheduled - time_scheduled_before;
  221. it.value.time_scheduled_since_prev = time_scheduled_diff;
  222. it.value.cpu_percent = total_scheduled_diff > 0 ? ((time_scheduled_diff * 100) / total_scheduled_diff) : 0;
  223. it.value.cpu_percent_decimal = total_scheduled_diff > 0 ? (((time_scheduled_diff * 1000) / total_scheduled_diff) % 10) : 0;
  224. threads.append(&it.value);
  225. }
  226. quick_sort(threads, [&top_option](auto* p1, auto* p2) {
  227. switch (top_option.sort_by) {
  228. case TopOption::SortBy::Pid:
  229. return p2->pid > p1->pid;
  230. case TopOption::SortBy::Tid:
  231. return p2->tid > p1->tid;
  232. case TopOption::SortBy::Priority:
  233. return p2->priority > p1->priority;
  234. case TopOption::SortBy::UserName:
  235. return p2->username > p1->username;
  236. case TopOption::SortBy::State:
  237. return p2->state > p1->state;
  238. case TopOption::SortBy::Virt:
  239. return p2->amount_virtual < p1->amount_virtual;
  240. case TopOption::SortBy::Phys:
  241. return p2->amount_resident < p1->amount_resident;
  242. case TopOption::SortBy::Name:
  243. return p2->name > p1->name;
  244. case TopOption::SortBy::Cpu:
  245. return p2->cpu_percent * 10 + p2->cpu_percent_decimal < p1->cpu_percent * 10 + p1->cpu_percent_decimal;
  246. default:
  247. return p2->time_scheduled_since_prev < p1->time_scheduled_since_prev;
  248. }
  249. });
  250. int row = 0;
  251. for (auto* thread : threads) {
  252. int nprinted = printf("%6d %3d %2u %-9s %-13s %6zu %6zu %2u.%1u ",
  253. thread->pid,
  254. thread->tid,
  255. thread->priority,
  256. thread->username.characters(),
  257. thread->state.characters(),
  258. thread->amount_virtual / 1024,
  259. thread->amount_resident / 1024,
  260. thread->cpu_percent,
  261. thread->cpu_percent_decimal);
  262. int remaining = g_window_size.ws_col - nprinted;
  263. fwrite(thread->name.characters(), 1, max(0, min(remaining, (int)thread->name.length())), stdout);
  264. putchar('\n');
  265. if (++row >= (g_window_size.ws_row - 2))
  266. break;
  267. }
  268. threads.clear_with_capacity();
  269. prev = move(current);
  270. for (int sleep_slice = 0; sleep_slice < top_option.delay_time * 1000; sleep_slice += 100) {
  271. if (check_quit())
  272. exit(0);
  273. usleep(100 * 1000);
  274. }
  275. }
  276. }