1 | /* $Id: second.c,v 1.1.1.3 2004-10-03 04:59:48 ghudson Exp $ */ |
---|
2 | |
---|
3 | /* Copyright (C) 1998-99 Martin Baulig |
---|
4 | This file is part of LibGTop 1.0. |
---|
5 | |
---|
6 | Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. |
---|
7 | |
---|
8 | LibGTop is free software; you can redistribute it and/or modify it |
---|
9 | under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2 of the License, |
---|
11 | or (at your option) any later version. |
---|
12 | |
---|
13 | LibGTop is distributed in the hope that it will be useful, but WITHOUT |
---|
14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
16 | for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with LibGTop; see the file COPYING. If not, write to the |
---|
20 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
21 | Boston, MA 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include <locale.h> |
---|
25 | #include <math.h> |
---|
26 | #include <stdio.h> |
---|
27 | #include <libintl.h> |
---|
28 | |
---|
29 | #include <glibtop.h> |
---|
30 | #include <glibtop/open.h> |
---|
31 | #include <glibtop/close.h> |
---|
32 | |
---|
33 | #include <glibtop/union.h> |
---|
34 | #include <glibtop/sysdeps.h> |
---|
35 | |
---|
36 | static void |
---|
37 | output (pid_t pid) |
---|
38 | { |
---|
39 | glibtop_union data; |
---|
40 | char *args; |
---|
41 | unsigned i; |
---|
42 | |
---|
43 | #if HAVE_LIBGTOP_SMP |
---|
44 | unsigned long total; |
---|
45 | double p_total, p_utime, p_stime; |
---|
46 | double b_total, b_utime, b_stime; |
---|
47 | double s_total, s_utime, s_stime; |
---|
48 | double my_utime, my_stime; |
---|
49 | unsigned int ncpu; |
---|
50 | #endif |
---|
51 | |
---|
52 | printf ("\n"); |
---|
53 | |
---|
54 | glibtop_get_proc_state (&data.proc_state, pid); |
---|
55 | |
---|
56 | printf ("Proc_State PID %5d (0x%08lx): " |
---|
57 | "'%s', %c, %lu, %lu\n", (int) pid, |
---|
58 | (unsigned long) data.proc_state.flags, |
---|
59 | data.proc_state.cmd, data.proc_state.state, |
---|
60 | (unsigned long) data.proc_state.uid, |
---|
61 | (unsigned long) data.proc_state.gid); |
---|
62 | |
---|
63 | glibtop_get_proc_uid (&data.proc_uid, pid); |
---|
64 | |
---|
65 | printf ("Proc_Uid PID %5d (0x%08lx): " |
---|
66 | "%d %d %d %d %d %d %d %d %d %d %d %d\n", (int) pid, |
---|
67 | (unsigned long) data.proc_uid.flags, |
---|
68 | data.proc_uid.uid, data.proc_uid.euid, |
---|
69 | data.proc_uid.gid, data.proc_uid.egid, |
---|
70 | data.proc_uid.pid, data.proc_uid.ppid, |
---|
71 | data.proc_uid.pgrp, data.proc_uid.session, |
---|
72 | data.proc_uid.tty, data.proc_uid.tpgid, |
---|
73 | data.proc_uid.priority, data.proc_uid.nice); |
---|
74 | |
---|
75 | glibtop_get_proc_mem (&data.proc_mem, pid); |
---|
76 | |
---|
77 | printf ("Proc_Mem PID %5d (0x%08lx): " |
---|
78 | "%lu %lu %lu %lu %lu %lu\n", (int) pid, |
---|
79 | (unsigned long) data.proc_mem.flags, |
---|
80 | (unsigned long) data.proc_mem.size, |
---|
81 | (unsigned long) data.proc_mem.vsize, |
---|
82 | (unsigned long) data.proc_mem.resident, |
---|
83 | (unsigned long) data.proc_mem.share, |
---|
84 | (unsigned long) data.proc_mem.rss, |
---|
85 | (unsigned long) data.proc_mem.rss_rlim); |
---|
86 | |
---|
87 | glibtop_get_proc_segment (&data.proc_segment, pid); |
---|
88 | |
---|
89 | printf ("Proc_Segment PID %5d (0x%08lx): " |
---|
90 | "%lu %lu %lu %lu %lu %lu %lu %lu\n", (int) pid, |
---|
91 | (unsigned long) data.proc_segment.flags, |
---|
92 | (unsigned long) data.proc_segment.text_rss, |
---|
93 | (unsigned long) data.proc_segment.shlib_rss, |
---|
94 | (unsigned long) data.proc_segment.data_rss, |
---|
95 | (unsigned long) data.proc_segment.stack_rss, |
---|
96 | (unsigned long) data.proc_segment.dirty_size, |
---|
97 | (unsigned long) data.proc_segment.start_code, |
---|
98 | (unsigned long) data.proc_segment.end_code, |
---|
99 | (unsigned long) data.proc_segment.start_stack); |
---|
100 | |
---|
101 | glibtop_get_proc_time (&data.proc_time, pid); |
---|
102 | |
---|
103 | printf ("Proc_Time PID %5d (0x%08lx): " |
---|
104 | "%lu %lu %lu %lu %lu %lu %lu %lu %lu\n", (int) pid, |
---|
105 | (unsigned long) data.proc_time.flags, |
---|
106 | (unsigned long) data.proc_time.start_time, |
---|
107 | (unsigned long) data.proc_time.rtime, |
---|
108 | (unsigned long) data.proc_time.utime, |
---|
109 | (unsigned long) data.proc_time.stime, |
---|
110 | (unsigned long) data.proc_time.cutime, |
---|
111 | (unsigned long) data.proc_time.cstime, |
---|
112 | (unsigned long) data.proc_time.timeout, |
---|
113 | (unsigned long) data.proc_time.it_real_value, |
---|
114 | (unsigned long) data.proc_time.frequency); |
---|
115 | |
---|
116 | glibtop_get_proc_signal (&data.proc_signal, pid); |
---|
117 | |
---|
118 | printf ("Proc_Signal PID %5d (0x%08lx): " |
---|
119 | "%lu %lu %lu %lu\n", (int) pid, |
---|
120 | (unsigned long) data.proc_signal.flags, |
---|
121 | (unsigned long) data.proc_signal.signal, |
---|
122 | (unsigned long) data.proc_signal.blocked, |
---|
123 | (unsigned long) data.proc_signal.sigignore, |
---|
124 | (unsigned long) data.proc_signal.sigcatch); |
---|
125 | |
---|
126 | glibtop_get_proc_kernel (&data.proc_kernel, pid); |
---|
127 | |
---|
128 | printf ("Proc_Kernel PID %5d (0x%08lx): " |
---|
129 | "%lu %lu %lu %lu %lu 0x%lx 0x%lx 0x%lx (%s)\n", (int) pid, |
---|
130 | (unsigned long) data.proc_kernel.flags, |
---|
131 | (unsigned long) data.proc_kernel.k_flags, |
---|
132 | (unsigned long) data.proc_kernel.min_flt, |
---|
133 | (unsigned long) data.proc_kernel.maj_flt, |
---|
134 | (unsigned long) data.proc_kernel.cmin_flt, |
---|
135 | (unsigned long) data.proc_kernel.cmaj_flt, |
---|
136 | (unsigned long) data.proc_kernel.kstk_esp, |
---|
137 | (unsigned long) data.proc_kernel.kstk_eip, |
---|
138 | (unsigned long) data.proc_kernel.nwchan, |
---|
139 | data.proc_kernel.wchan); |
---|
140 | |
---|
141 | printf ("\n"); |
---|
142 | |
---|
143 | args = glibtop_get_proc_args (&data.proc_args, pid, 0); |
---|
144 | |
---|
145 | for (i = 0; i < data.proc_args.size; i++) { |
---|
146 | if (args [i]) continue; |
---|
147 | args [i] = '|'; |
---|
148 | } |
---|
149 | |
---|
150 | printf ("Proc_Args PID %5d (0x%08lx): %lu - '%s'\n", (int) pid, |
---|
151 | (unsigned long) data.proc_args.flags, |
---|
152 | (unsigned long) data.proc_args.size, |
---|
153 | args ? args : ""); |
---|
154 | |
---|
155 | g_free (args); |
---|
156 | |
---|
157 | printf ("\n"); |
---|
158 | |
---|
159 | #if HAVE_LIBGTOP_SMP |
---|
160 | ncpu = glibtop_global_server->ncpu; |
---|
161 | |
---|
162 | glibtop_get_proc_time (&data.proc_time, pid); |
---|
163 | |
---|
164 | total = (unsigned long) data.proc_time.utime + |
---|
165 | (unsigned long) data.proc_time.stime; |
---|
166 | |
---|
167 | p_total = total ? (double) total : 1.0; |
---|
168 | |
---|
169 | p_utime = (double) data.proc_time.utime * 100.0 / p_total; |
---|
170 | p_stime = (double) data.proc_time.stime * 100.0 / p_total; |
---|
171 | |
---|
172 | b_total = p_total / ncpu; |
---|
173 | b_utime = (double) data.proc_time.utime / ncpu; |
---|
174 | b_stime = (double) data.proc_time.stime / ncpu; |
---|
175 | |
---|
176 | s_total = 0.0; s_utime = 0.0; s_stime = 0.0; |
---|
177 | |
---|
178 | printf ("Proc_Time PID %5d (0x%08lx): %12lu %12lu %12lu\n", (int) pid, |
---|
179 | (unsigned long) data.proc_time.flags, total, |
---|
180 | (unsigned long) data.proc_time.utime, |
---|
181 | (unsigned long) data.proc_time.stime); |
---|
182 | |
---|
183 | for (i = 0; i < ncpu; i++) { |
---|
184 | unsigned long this_total; |
---|
185 | |
---|
186 | this_total = (unsigned long) data.proc_time.xcpu_utime [i] + |
---|
187 | (unsigned long) data.proc_time.xcpu_stime [i]; |
---|
188 | |
---|
189 | printf ("CPU %3d PID %5d (0x%08lx): %12lu %12lu %12lu\n", i, |
---|
190 | (int) pid, (unsigned long) data.proc_time.flags, this_total, |
---|
191 | (unsigned long) data.proc_time.xcpu_utime [i], |
---|
192 | (unsigned long) data.proc_time.xcpu_stime [i]); |
---|
193 | |
---|
194 | s_total += fabs (((double) this_total) - b_total); |
---|
195 | s_utime += fabs (((double) data.proc_time.xcpu_utime [i]) - b_utime); |
---|
196 | s_stime += fabs (((double) data.proc_time.xcpu_stime [i]) - b_stime); |
---|
197 | } |
---|
198 | |
---|
199 | printf ("\n"); |
---|
200 | |
---|
201 | printf ("Proc_Time PID %5d (0x%08lx): %12.3f %12.3f %12.3f\n", (int) pid, |
---|
202 | (unsigned long) data.proc_time.flags, 100.0, p_utime, p_stime); |
---|
203 | |
---|
204 | for (i = 0; i < ncpu; i++) { |
---|
205 | double this_p_total, this_p_utime, this_p_stime; |
---|
206 | unsigned long this_total; |
---|
207 | |
---|
208 | this_total = (unsigned long) data.proc_time.xcpu_utime [i] + |
---|
209 | (unsigned long) data.proc_time.xcpu_stime [i]; |
---|
210 | |
---|
211 | this_p_total = (double) this_total * 100.0 / p_total; |
---|
212 | |
---|
213 | this_p_utime = (double) data.proc_time.xcpu_utime [i] * 100.0 / p_total; |
---|
214 | this_p_stime = (double) data.proc_time.xcpu_stime [i] * 100.0 / p_total; |
---|
215 | |
---|
216 | printf ("CPU %3d PID %5d (0x%08lx): %12.3f %12.3f %12.3f\n", i, |
---|
217 | (int) pid, (unsigned long) data.proc_time.flags, |
---|
218 | this_p_total, this_p_utime, this_p_stime); |
---|
219 | } |
---|
220 | |
---|
221 | printf ("\n"); |
---|
222 | |
---|
223 | my_utime = (unsigned long) data.proc_time.utime ? |
---|
224 | (double) data.proc_time.utime : 1.0; |
---|
225 | my_stime = (unsigned long) data.proc_time.stime ? |
---|
226 | (double) data.proc_time.stime : 1.0; |
---|
227 | |
---|
228 | printf ("SPIN: %31s %12.3f %12.3f %12.3f\n", "", s_total * 100.0 / p_total, |
---|
229 | s_utime * 100.0 / my_utime, s_stime * 100.0 / my_stime); |
---|
230 | |
---|
231 | printf ("\n"); |
---|
232 | #endif |
---|
233 | } |
---|
234 | |
---|
235 | int |
---|
236 | main (int argc, char *argv []) |
---|
237 | { |
---|
238 | glibtop_proclist proclist; |
---|
239 | glibtop_sysdeps sysdeps; |
---|
240 | unsigned *ptr, pid, i; |
---|
241 | |
---|
242 | setlocale (LC_ALL, ""); |
---|
243 | bindtextdomain (GETTEXT_PACKAGE, GTOPLOCALEDIR); |
---|
244 | textdomain (GETTEXT_PACKAGE); |
---|
245 | |
---|
246 | glibtop_init (); |
---|
247 | |
---|
248 | glibtop_get_sysdeps (&sysdeps); |
---|
249 | |
---|
250 | printf ("Sysdeps (0x%08lx): %lu, %lu, %lu, %lu, %lu, " |
---|
251 | "%lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu\n", |
---|
252 | (unsigned long) sysdeps.flags, |
---|
253 | (unsigned long) sysdeps.cpu, |
---|
254 | (unsigned long) sysdeps.mem, |
---|
255 | (unsigned long) sysdeps.swap, |
---|
256 | (unsigned long) sysdeps.uptime, |
---|
257 | (unsigned long) sysdeps.loadavg, |
---|
258 | (unsigned long) sysdeps.shm_limits, |
---|
259 | (unsigned long) sysdeps.msg_limits, |
---|
260 | (unsigned long) sysdeps.sem_limits, |
---|
261 | (unsigned long) sysdeps.proclist, |
---|
262 | (unsigned long) sysdeps.proc_state, |
---|
263 | (unsigned long) sysdeps.proc_uid, |
---|
264 | (unsigned long) sysdeps.proc_mem, |
---|
265 | (unsigned long) sysdeps.proc_time, |
---|
266 | (unsigned long) sysdeps.proc_signal, |
---|
267 | (unsigned long) sysdeps.proc_kernel, |
---|
268 | (unsigned long) sysdeps.proc_segment); |
---|
269 | |
---|
270 | if ((argc == 2) && (sscanf (argv [1], "%d", &pid) == 1)) { |
---|
271 | output (pid); |
---|
272 | |
---|
273 | exit (0); |
---|
274 | } |
---|
275 | |
---|
276 | printf ("\n"); |
---|
277 | |
---|
278 | ptr = glibtop_get_proclist (&proclist, 0, 0); |
---|
279 | |
---|
280 | printf ("Proclist (0x%08lx): %lu, %lu, %lu\n", |
---|
281 | (unsigned long) proclist.flags, |
---|
282 | (unsigned long) proclist.number, |
---|
283 | (unsigned long) proclist.size, |
---|
284 | (unsigned long) proclist.total); |
---|
285 | |
---|
286 | if (!ptr) exit (1); |
---|
287 | |
---|
288 | for (i = 0; i < proclist.number; i++) { |
---|
289 | |
---|
290 | pid = ptr [i]; |
---|
291 | |
---|
292 | output (pid); |
---|
293 | } |
---|
294 | |
---|
295 | g_free (ptr); |
---|
296 | |
---|
297 | exit (0); |
---|
298 | } |
---|
299 | |
---|
300 | |
---|
301 | |
---|