Line data Source code
1 : // Copyright (c) 2013-2022 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/eventdispatcher
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 : #pragma once
20 :
21 : // advgetopt
22 : //
23 : #include <advgetopt/utils.h>
24 :
25 :
26 : // C++
27 : //
28 : #include <memory>
29 : #include <string>
30 :
31 :
32 : // C
33 : //
34 : #include <unistd.h>
35 :
36 :
37 :
38 : namespace cppprocess
39 : {
40 :
41 :
42 :
43 : enum class process_state_t : char
44 : {
45 : // IMPORTANT: all states are not available on all versions of Linux
46 : //
47 : PROCESS_STATE_UNKNOWN = '?', // could not read state
48 : PROCESS_STATE_RUNNING = 'R',
49 : PROCESS_STATE_SLEEPING = 'S',
50 : PROCESS_STATE_DISK_SLEEP = 'D',
51 : PROCESS_STATE_ZOMBIE = 'Z',
52 : PROCESS_STATE_STOPPED = 'T',
53 : PROCESS_STATE_TRACING_STOP = 't',
54 : PROCESS_STATE_PAGING = 'W',
55 : PROCESS_STATE_DEAD = 'X',
56 : PROCESS_STATE_DEAD2 = 'x',
57 : PROCESS_STATE_WAKE_KILL = 'K',
58 : PROCESS_STATE_WAKING = 'W',
59 : PROCESS_STATE_PARKED = 'P',
60 : };
61 :
62 :
63 1 : class process_info
64 : {
65 : public:
66 : typedef std::shared_ptr<process_info> pointer_t;
67 :
68 : process_info(pid_t pid);
69 :
70 : // basic process information
71 : //
72 : pid_t get_pid();
73 : pid_t get_ppid();
74 : pid_t get_pgid();
75 :
76 : // re-add function(s) as required, implement the retrieval in one of
77 : // the specialized load_...() functions.
78 : //
79 :
80 : // process name/arguments
81 : //
82 : std::string get_name();
83 : std::string get_command();
84 : std::string get_basename();
85 : std::size_t get_args_size();
86 : std::string get_arg(int index);
87 :
88 : // CPU info, priority
89 : //
90 : process_state_t get_state(bool force = true);
91 : int get_cpu_percent();
92 : void get_times(unsigned long long & utime, unsigned long long & stime, unsigned long long & cutime, unsigned long long & cstime);
93 : int get_priority();
94 : int get_nice();
95 :
96 : // memory info
97 : //
98 : void get_page_faults(std::uint64_t & major, std::uint64_t & minor);
99 : std::uint64_t get_total_size();
100 : std::uint64_t get_rss_size();
101 :
102 : // I/O info
103 : //
104 : void get_tty(int & major, int & minor);
105 :
106 : private:
107 : void load_stat(bool force = false);
108 : void load_cmdline();
109 :
110 : pid_t f_pid = -1; // if -1, process died
111 :
112 : // load_stat()
113 : //
114 : std::string f_name = std::string();
115 : process_state_t f_state = process_state_t::PROCESS_STATE_UNKNOWN;
116 : pid_t f_ppid = -1; // if -1, not yet loaded (see load_stat())
117 : gid_t f_pgid = -1;
118 : pid_t f_session = -1;
119 : int f_tty_major = -1;
120 : int f_tty_minor = -1;
121 : gid_t f_fp_group = -1;
122 : int f_kernel_flags = -1;
123 : std::uint64_t f_minor_faults = -1;
124 : std::uint64_t f_children_minor_faults = -1;
125 : std::uint64_t f_major_faults = -1;
126 : std::uint64_t f_children_major_faults = -1;
127 : int f_user_time = -1;
128 : int f_system_time = -1;
129 : int f_children_user_time = -1;
130 : int f_children_system_time = -1;
131 : int f_priority = -1;
132 : int f_nice = -1;
133 : int f_num_threads = -1;
134 : std::int64_t f_start_time = -1;
135 : std::int64_t f_virtual_size = -1;
136 : std::int64_t f_rss = -1;
137 : std::int64_t f_rss_limit = -1;
138 : std::int64_t f_start_code = -1;
139 : std::int64_t f_end_code = -1;
140 : std::int64_t f_start_stack = -1;
141 : std::int64_t f_kernel_esp = -1;
142 : std::int64_t f_kernel_eip = -1;
143 : int f_wchan = -1;
144 : int f_exit_signal = -1;
145 : int f_processor = -1;
146 : int f_rt_priority = -1;
147 : int f_schedule_policy = -1;
148 : int f_delayacct_blkio_ticks = -1;
149 : std::int64_t f_guest_time = -1;
150 : std::int64_t f_children_guest_time = -1;
151 : std::int64_t f_start_data = -1;
152 : std::int64_t f_end_data = -1;
153 : std::int64_t f_start_break = -1;
154 : std::int64_t f_arg_start = -1;
155 : std::int64_t f_arg_end = -1;
156 : std::int64_t f_env_start = -1;
157 : std::int64_t f_env_end = -1;
158 : int f_exit_code = -1;
159 :
160 : // load_cmdline()
161 : //
162 : advgetopt::string_list_t
163 : f_args = advgetopt::string_list_t();
164 : };
165 :
166 :
167 :
168 :
169 : } // namespace cppprocess
170 : // vim: ts=4 sw=4 et
|