| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 18:47:47 +02:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/HashMap.h>
 | 
					
						
							| 
									
										
										
										
											2019-11-26 21:25:45 +01:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-03 10:08:51 -07:00
										 |  |  | #include <LibCore/File.h>
 | 
					
						
							| 
									
										
										
										
											2019-11-26 21:25:45 +01:00
										 |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | namespace Core { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct ThreadStatistics { | 
					
						
							| 
									
										
										
										
											2020-08-09 01:08:24 +02:00
										 |  |  |     pid_t tid; | 
					
						
							| 
									
										
										
										
											2019-11-26 21:25:45 +01:00
										 |  |  |     unsigned times_scheduled; | 
					
						
							| 
									
										
										
										
											2020-12-03 22:12:50 -07:00
										 |  |  |     unsigned ticks_user; | 
					
						
							|  |  |  |     unsigned ticks_kernel; | 
					
						
							| 
									
										
										
										
											2019-11-26 21:35:24 +01:00
										 |  |  |     unsigned syscall_count; | 
					
						
							|  |  |  |     unsigned inode_faults; | 
					
						
							|  |  |  |     unsigned zero_faults; | 
					
						
							|  |  |  |     unsigned cow_faults; | 
					
						
							| 
									
										
										
										
											2019-12-01 17:36:06 +01:00
										 |  |  |     unsigned unix_socket_read_bytes; | 
					
						
							|  |  |  |     unsigned unix_socket_write_bytes; | 
					
						
							|  |  |  |     unsigned ipv4_socket_read_bytes; | 
					
						
							|  |  |  |     unsigned ipv4_socket_write_bytes; | 
					
						
							|  |  |  |     unsigned file_read_bytes; | 
					
						
							|  |  |  |     unsigned file_write_bytes; | 
					
						
							| 
									
										
										
										
											2019-11-26 21:25:45 +01:00
										 |  |  |     String state; | 
					
						
							| 
									
										
										
										
											2020-06-27 22:36:15 -06:00
										 |  |  |     u32 cpu; | 
					
						
							| 
									
										
										
										
											2019-12-30 18:46:17 +01:00
										 |  |  |     u32 priority; | 
					
						
							|  |  |  |     u32 effective_priority; | 
					
						
							| 
									
										
										
										
											2019-12-07 12:50:30 -07:00
										 |  |  |     String name; | 
					
						
							| 
									
										
										
										
											2019-11-26 21:25:45 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-05-16 18:47:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | struct ProcessStatistics { | 
					
						
							| 
									
										
										
										
											2019-07-17 22:22:22 +02:00
										 |  |  |     // Keep this in sync with /proc/all.
 | 
					
						
							|  |  |  |     // From the kernel side:
 | 
					
						
							| 
									
										
										
										
											2019-05-16 18:47:47 +02:00
										 |  |  |     pid_t pid; | 
					
						
							| 
									
										
										
										
											2020-09-06 19:04:47 +02:00
										 |  |  |     pid_t pgid; | 
					
						
							|  |  |  |     pid_t pgp; | 
					
						
							|  |  |  |     pid_t sid; | 
					
						
							| 
									
										
										
										
											2019-05-16 18:47:47 +02:00
										 |  |  |     uid_t uid; | 
					
						
							| 
									
										
										
										
											2019-07-17 22:22:22 +02:00
										 |  |  |     gid_t gid; | 
					
						
							|  |  |  |     pid_t ppid; | 
					
						
							|  |  |  |     unsigned nfds; | 
					
						
							|  |  |  |     String name; | 
					
						
							| 
									
										
										
										
											2020-12-27 00:54:13 +01:00
										 |  |  |     String executable; | 
					
						
							| 
									
										
										
										
											2019-07-17 22:22:22 +02:00
										 |  |  |     String tty; | 
					
						
							| 
									
										
										
										
											2020-01-11 21:17:07 +01:00
										 |  |  |     String pledge; | 
					
						
							| 
									
										
										
										
											2020-01-21 18:56:23 +01:00
										 |  |  |     String veil; | 
					
						
							| 
									
										
										
										
											2019-07-17 22:22:22 +02:00
										 |  |  |     size_t amount_virtual; | 
					
						
							|  |  |  |     size_t amount_resident; | 
					
						
							|  |  |  |     size_t amount_shared; | 
					
						
							| 
									
										
										
										
											2019-12-29 12:28:32 +01:00
										 |  |  |     size_t amount_dirty_private; | 
					
						
							| 
									
										
										
										
											2019-12-29 12:45:58 +01:00
										 |  |  |     size_t amount_clean_inode; | 
					
						
							| 
									
										
										
										
											2019-12-09 19:16:58 +01:00
										 |  |  |     size_t amount_purgeable_volatile; | 
					
						
							|  |  |  |     size_t amount_purgeable_nonvolatile; | 
					
						
							| 
									
										
										
										
											2019-07-17 22:22:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     Vector<Core::ThreadStatistics> threads; | 
					
						
							| 
									
										
										
										
											2019-11-26 21:25:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-17 22:22:22 +02:00
										 |  |  |     // synthetic
 | 
					
						
							|  |  |  |     String username; | 
					
						
							| 
									
										
										
										
											2019-05-16 18:47:47 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | class ProcessStatisticsReader { | 
					
						
							| 
									
										
										
										
											2019-05-16 18:47:47 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-01-03 10:08:51 -07:00
										 |  |  |     static Optional<HashMap<pid_t, Core::ProcessStatistics>> get_all(RefPtr<Core::File>&); | 
					
						
							| 
									
										
										
										
											2021-01-01 20:58:47 -07:00
										 |  |  |     static Optional<HashMap<pid_t, Core::ProcessStatistics>> get_all(); | 
					
						
							| 
									
										
										
										
											2019-05-16 18:47:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2019-07-10 13:56:28 +02:00
										 |  |  |     static String username_from_uid(uid_t); | 
					
						
							|  |  |  |     static HashMap<uid_t, String> s_usernames; | 
					
						
							| 
									
										
										
										
											2019-05-16 18:47:47 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |