summaryrefslogtreecommitdiffstats
path: root/src/prtstat.c
blob: d14c65942b3fc9be884e8066286af7611e61ebd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * prtstat.c - Print a processes stat file
 *
 * Copyright (C) 2009-2024 Craig Small
 * Based upon a shell script pstat by martin f. krafft <madduck@madduck.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "i18n.h"
#include "prtstat.h"

#define NORETURN __attribute__((__noreturn__))

static long sc_clk_tck;

static void usage(const char *errormsg) NORETURN;

static void usage(const char *errormsg)
{
  if (errormsg != NULL)
	fprintf(stderr, "%s\n", errormsg);
  fprintf(stderr,
	  _
	  ("Usage: prtstat [options] PID ...\n"
	   "       prtstat -V\n"
	   "Print information about a process\n"
	   "    -r,--raw       Raw display of information\n"
	   "    -V,--version   Display version information and exit\n"
	  ));
  exit(1);
}

static void print_version(void)
{
  fprintf(stderr, _("prtstat (PSmisc) %s\n"), VERSION);
  fprintf(stderr, _( "Copyright (C) 2009-2024 Craig Small\n\n"));
  fprintf(stderr, _(
		"PSmisc comes with ABSOLUTELY NO WARRANTY.\n"
		"This is free software, and you are welcome to redistribute it under\n"
		"the terms of the GNU General Public License.\n"
		"For more information about these matters, see the files named COPYING.\n"));
}

static char *print_state(const char state)
{
  switch(state) {
	case 'R':
	  return _("running");
	case 'S':
	  return _("sleeping");
	case 'D':
	  return _("disk sleep");
	case 'Z':
	  return _("zombie");
	case 'T':
	  return _("traced");
	case 'W':
	  return _("paging");
  }
  return _("unknown");
}

#define RAW_STAT(afmt,aname, aval, bfmt, bname, bval) \
  printf("%12.11s: %-15"afmt"\t%22.21s: %"bfmt"\n",(aname),(aval),(bname),(bval))

static double convert_time(const unsigned long ticks)
{
  assert(sc_clk_tck > 0);
  return (float)ticks / (float)sc_clk_tck;
}

static void convert_bytes(char *buf, unsigned long bytes)
{
  if (bytes > (10000000))
	sprintf(buf, "%lu MB",bytes/1000000L);
  else if (bytes > (10000))
	sprintf(buf, "%lu kB", bytes/1000L);
  else
	sprintf(buf, "%lu B", bytes);
}

/* comes from SCHED_* from linux/sched.h */
static char *convert_policy(const unsigned int policy)
{
  static char *policy_names[] = { "normal", "fifo","rr", "batch", "iso", "idle" };
  if (policy < 6)
	return policy_names[policy];
  return "unknown";
}

/* minor is bits 31-20 and 7-0, major is 15-8 */
static char *convert_tty(int tty_nr)
{
  static char buf[20];
  sprintf(buf, "%d:%d",(tty_nr & 0xff00)>>8,(tty_nr & 0xff)|((tty_nr & 0xfff00000)>>20));
  return buf;
}


static void print_raw_stat(const int pid,struct proc_info *pr)
{
  RAW_STAT("d","pid",pid,"s","comm",pr->comm);
  RAW_STAT("c","state",pr->state, "d","ppid",pr->ppid);
  RAW_STAT("d","pgrp",pr->pgrp, "d","session",pr->session);
  RAW_STAT("d","tty_nr",pr->tty_nr, "d","tpgid",pr->tp_gid);
  RAW_STAT("x","flags",pr->flags, "lu","minflt",pr->minflt);
  RAW_STAT("lu","cminflt",pr->cminflt, "lu","majflt",pr->majflt);
  RAW_STAT("lu","cmajflt",pr->cmajflt, "lu","utime",pr->utime);
  RAW_STAT("lu","stime",pr->stime, "ld","cutime",pr->cutime);
  RAW_STAT("ld","cstime",pr->cstime, "ld","priority",pr->priority);
  RAW_STAT("ld","nice",pr->nice, "ld","num_threads",pr->num_threads);
  RAW_STAT("ld","itrealvalue",pr->itrealvalue, "llu","starttime",pr->starttime);
  RAW_STAT("lu","vsize",pr->vsize, "ld","rss",pr->rss);
  RAW_STAT("lu","rsslim",pr->rsslim, "lu","startcode",pr->startcode);
  RAW_STAT("lu","endcode",pr->endcode, "lu","startstack",pr->startstack);
  RAW_STAT("lX","kstkesp",pr->kstesp, "lX","kstkeip",pr->ksteip);
  RAW_STAT("lu","wchan",pr->wchan, "lu","nswap",pr->nswap);
  RAW_STAT("lu","cnswap",pr->wchan, "d","exit_signal",pr->exit_signal);
  RAW_STAT("d","processor",pr->processor, "u","rt_priority",pr->rt_priority);
  RAW_STAT("u","policy",pr->policy, "llu","delayaccr_blkio_ticks",pr->blkio);
  RAW_STAT("lu","guest_time",pr->guest_time, "ld","cguest_time",pr->cguest_time);
}
static void print_formated_stat(const int pid,struct proc_info *pr)
{
  char buf_vsize[100];
  char buf_rss[100];
  char buf_rsslim[100];
  long page_size;

  page_size = sysconf(_SC_PAGESIZE);
  assert(page_size>1);

  printf(_(
		"Process: %-14s\t\tState: %c (%s)\n"
		"  CPU#:  %-3d\t\tTTY: %s\tThreads: %ld\n"),
	  pr->comm, pr->state, print_state(pr->state),
	  pr->processor, convert_tty(pr->tty_nr), pr->num_threads);
  printf(_(
		"Process, Group and Session IDs\n"
		"  Process ID: %d\t\t  Parent ID: %d\n"
		"    Group ID: %d\t\t Session ID: %d\n"
	    "  T Group ID: %d\n\n"),
	  pid, pr->ppid, pr->pgrp, pr->session, pr->tp_gid);
  printf(_(
		"Page Faults\n"
		"  This Process    (minor major): %8lu  %8lu\n"
		"  Child Processes (minor major): %8lu  %8lu\n"),
	  pr->minflt, pr->majflt, pr->cminflt, pr->cmajflt);
  printf(_(
		"CPU Times\n"
		"  This Process    (user system guest blkio): %6.2f %6.2f %6.2f %6.2f\n"
		"  Child processes (user system guest):       %6.2f %6.2f %6.2f\n"),
	  convert_time(pr->utime), convert_time(pr->stime), convert_time(pr->guest_time), convert_time(pr->blkio),
	  convert_time(pr->cutime), convert_time(pr->cstime), convert_time(pr->cguest_time));
  convert_bytes(buf_vsize, pr->vsize);
  convert_bytes(buf_rss, pr->rss*page_size);
  convert_bytes(buf_rsslim, pr->rsslim);
  printf(_(
		"Memory\n"
		"  Vsize:       %-10s\n"
		"  RSS:         %-10s \t\t RSS Limit: %s\n"
		"  Code Start:  %#-10lx\t\t Code Stop:  %#-10lx\n"
		"  Stack Start: %#-10lx\n"
		"  Stack Pointer (ESP): %#10lx\t Inst Pointer (EIP): %#10lx\n"),
	  buf_vsize, buf_rss, buf_rsslim,
	  pr->startcode, pr->endcode, 
	  pr->startstack, pr->kstesp, pr->ksteip);
  printf(_(
		"Scheduling\n"
		"  Policy: %s\n"
		"  Nice:   %ld \t\t RT Priority: %ld %s\n"),
	  convert_policy(pr->policy),
	  pr->nice, (pr->priority>0?pr->priority-20:1-pr->priority),
	  (pr->priority>0?"(non RT)":""));




}
static void print_stat(const int pid, const opt_type options)
{
  char *pathname;
  char buf[BUFSIZ];
  char *bptr;
  FILE *fp;

  struct proc_info *pr = NULL;

  if ( (asprintf(&pathname, "/proc/%d/stat",(int)pid)) < 0) {
	perror(_("asprintf in print_stat failed.\n"));
	exit(1);
  }
  if ( (fp = fopen(pathname,"r")) == NULL) {
	if (errno == ENOENT) 
	  fprintf(stderr, _("Process with pid %d does not exist.\n"), pid);
	else
	  fprintf(stderr, _("Unable to open stat file for pid %d (%s)\n"),(int)pid,strerror(errno));
	free(pathname);
	free(pr);
	return;
  }
  free(pathname);

  if (fgets(buf,BUFSIZ,fp) == NULL) {
      fclose(fp);
      return;
  }
  fclose(fp);
  bptr = strchr(buf, '(');
  if (bptr == NULL) return;
  bptr++;
  if ((pr = malloc(sizeof(struct proc_info))) == NULL) {
      fprintf(stderr, _("Unable to allocate memory for proc_info\n"));
      return;
  }
  pr->comm = NULL;
  if (sscanf(bptr,
	  "%m[^)]) "
	  "%c "
	  "%d %d %d %d %d %d"
	  "%lu %lu %lu %lu " /*flts*/
	  "%lu %lu %lu %lu " /*times */
	  "%ld %ld %ld %ld " /* nice, priority, threads, itreal*/
	  "%llu " /*startime*/
	  "%lu %ld %lu " /* vsize, rss, rslim */
	  "%lu %lu %lu " /* startcode endcode startstack */
	  "%lu %lu " /* stack and ip */
	  "%*s %*s %*s %*s " /* signals - ignore as they are obsolete */
	  "%lu %lu %lu " /* wchan nswap cnswap */
      "%d %d %u"
	  "%u %llu " /* policy blkio */
	  "%lu %lu ", /* guest time cguest time */
	   &pr->comm,
	   &pr->state,
	   &pr->ppid, &pr->pgrp, &pr->session, &pr->tty_nr, &pr->tp_gid, &pr->flags,
	   &pr->minflt, &pr->cminflt, &pr->majflt, &pr->cmajflt,
	   &pr->utime, &pr->stime, &pr->cutime, &pr->cstime,
	   &pr->priority, &pr->nice, &pr->num_threads, &pr->itrealvalue,
	   &pr->starttime,
	   &pr->vsize, &pr->rss, &pr->rsslim,
	   &pr->startcode, &pr->endcode, &pr->startstack,
	   &pr->kstesp, &pr->ksteip,
	   &pr->wchan, &pr->nswap, &pr->cnswap,
	   &pr->exit_signal, &pr->processor, &pr->rt_priority,
	   &pr->policy, &pr->blkio, 
	   &pr->guest_time, &pr->cguest_time
		 ) == 39) {
    if (options & OPT_RAW)
        print_raw_stat(pid, pr);
    else
        print_formated_stat(pid, pr);
         } else 
             fprintf(stderr, _("Unable to scan stat file"));
  free(pr->comm);
  free(pr);
}

int main(int argc, char *argv[])
{
  int optc;
  struct stat st;
  int pptr;
  int pid;
  opt_type opt_flags = 0;

  struct option options[] = {
	{"raw"		,0, NULL, 'r' },
	{"version", 0, NULL, 'V'},
	{ 0, 0, 0, 0}
  };

#ifdef ENABLE_NLS
  /* Set up the i18n */
  setlocale(LC_ALL, "");
  bindtextdomain(PACKAGE, LOCALEDIR);
  textdomain(PACKAGE);
#endif

  while ((optc = getopt_long(argc, argv, "rV", options, NULL)) != -1) {
	switch(optc) {
	  case 'r':
		opt_flags |= OPT_RAW;
		break;
	  case 'V':
		print_version();
		return 0;
	  case '?':
		usage(_("Invalid option"));
		break;
	}
  } /* while */
  if (argc <= optind)
	usage(_("You must provide at least one PID."));

  if (stat("/proc/self/stat", &st) == -1)
  {
	fprintf(stderr, _("/proc is not mounted, cannot stat /proc/self/stat.\n"));
	exit(1);
  }
  sc_clk_tck = sysconf(_SC_CLK_TCK);
  for(pptr = optind; pptr < argc; pptr++)
  {
	pid = atoi(argv[pptr]);
	print_stat(pid, opt_flags);
  }

  return 0;
}