Getting Process Information on Unix

Sometimes it is interesting what a process doing at operating system level. This allows us to investigate whether there some kind of wait or latency, such as lack of cpu. On Unix you can see in several ways, which I propose here is through system calls and using C. This code applies to any type of process, regardless belonging to Oracle, Sybase, SAP, Siebel, etc.

This testcase is made on Solaris 10. I have to say that this information can be obtained in the case of Oracle using the view V$SYSSTAT (I think it is available from the 8i onwards).

This is the source code (ANSI C);

#include <stdio.h>
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/syscall.h>
#include <sys/procfs.h>
#include <sys/param.h>
#include <fcntl.h>
#include <strings.h>
/*** prototyped functions  ***/
int lee_informacion (char *);
void salida (void);
void cabecera (void);
/*** Version  ***/
void salida (void)
{
  printf (“\nVersion 2, Antonio NAVARRO,@2001.\n”);
}
/*** Get information  ***/
int lee_informacion (char * fichero)
{
  int fd; /* descriptor de fichero */
  char camino [100]; /* camino al fichero */
  prusage_t pusage; /* structura del sistema */
  strcpy (camino, “/proc/”);
  strcat (camino, fichero);
  if ((fd = open (camino, O_RDONLY)) == NULL)
  {
    printf (“>>> Error, openning the process.\n”);   /*** Error condition ***/
    salida ();
    exit(-1);
  }
  if (ioctl (fd, PIOCUSAGE, & pusage) < 0)
  {
    printf (“>>> Error, performing ioctl.\n”);       /*** Error condition ***/
    salida ();
    exit (-1);
  }
  printf (“——————————————————-\n”);
  printf (“Timestamp : %d\n”, pusage.pr_tstamp);
  printf (“Creation timestamp : %d\n”, pusage.pr_create);
  printf (“Total time (elapsed) : %d\n”, pusage.pr_rtime);
  printf (“CPU user time: %d\n”, pusage.pr_utime);
  printf (“CPU system time : %d\n”, pusage.pr_stime);
  printf (“Wait-cpu (latency) time : %d\n”, pusage.pr_wtime);
  printf (“Minor page faults : %d\n”, pusage.pr_minf);
  printf (“Major page faults : %d\n”, pusage.pr_majf);
  printf (“Swaps : %d\n”, pusage.pr_nswap);
  printf (“Input blocks : %d\n”, pusage.pr_inblk);
  printf (“Output blocks : %d\n”, pusage.pr_oublk);
  printf (“Messages sent : %d\n”, pusage.pr_msnd);
  printf (“Messages received : %d\n”, pusage.pr_mrcv);
  printf (“Voluntary context switches : %d\n”, pusage.pr_vctx);
  printf (“Involuntary context switches : %d\n”, pusage.pr_ictx);
  printf (“Chars read and written : %d\n”, pusage.pr_ioch);
  printf (“——————————————————-\n”);
  return (0);
}
int main (int argc, char * argv [])
{
  if (argc != 2)
  {
    printf (“Usage: %s <PID> \n”, argv [0]);
    salida ();
    exit (-1);
  }
  lee_informacion (argv [1]);
  salida ();
  exit (0);
}

Here you can see a example;

FOTO1_IOT

Look for a process running;

FOTO2_IOT

Retry the command again;

FOTO3_IOT

HTH – Antonio NAVARRO

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s