Re: CPU and Memory Usage

From: Richard B. Johnson (root@chaos.analogic.com)
Date: Wed Mar 01 2000 - 08:47:32 EST


On Tue, 29 Feb 2000, G. Sudhanva wrote:

> Hi!
>
> How do I find out the CPU and memory usage of a process in my system
> using only kernel data-structures (I don't want to sue "ps" or any
> other utility) ?
>
> Sudhanva

That info is in the /proc file-system. Here is an example of how to use
it (a mini - ps):

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
/*
 * File status.c Created 23-NOV-1999 Richard B. Johnson
 *
 * Shows the status of executing tasks on the Platinum Computer.
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <dirent.h>

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
#define PID_LEN 0x10
#define NAM_LEN 0x20
#define CMD_LEN 0x30
#define STA_LEN 0x10
#define PAD_LEN 0x10
#define MEM_LEN 0x10
#define EXE_LEN 0x10
#define CPU_LEN 0x10
#define TOT_LEN (PID_LEN + NAM_LEN + CMD_LEN + STA_LEN + PAD_LEN)

typedef struct {
    char buf[TOT_LEN];
    char pid[PID_LEN];
    char nam[NAM_LEN];
    char cmd[CMD_LEN];
    char sta[STA_LEN];
    char mem[MEM_LEN];
    char cpu[CPU_LEN];
    char exe[EXE_LEN];
    } MEM;
#if !defined(HZ)
#define HZ 100
#endif
static const char prc[]="/proc";
static const char key[]="/proc/stat";
static const char fmt[]="%-6s%-16s%-6s%-8s%-6s%-6s%-10s\n";
#define IsNumber(v) (((v) > (char)0x2f) && ((v) < (char)0x3a))
#define isizeof(n) ((int)sizeof(n))
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
/*
 * Function prototypes.
 */
int main(void);
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/

int main()
{
    unsigned long tim = 0L;
    DIR *dir;
    FILE *file;
    struct dirent *d;
    MEM *me;
    int fd;
    size_t len, sub;

    if((dir = opendir(prc)) == NULL)
        exit(EXIT_FAILURE);
    (void)nice(-18);
    if((me = (MEM *) malloc(sizeof(MEM))) == NULL)
    {
        (void)closedir(dir);
        exit(EXIT_FAILURE);
    }
    memset(me, 0x00, sizeof(MEM));
    printf(fmt, "PID", "Name", "Stat", "CPU", "Mem", "Exe", "Command line");
    while((d = readdir(dir)) != NULL)
    {
        if(IsNumber(*d->d_name))
        {
            (void)sprintf(me->buf, "%s/%s/stat", prc, d->d_name);
            if((file = fopen(me->buf, "r")) != NULL)
            {
                (void)fscanf(file, "%s%s%s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%lu",
                                    me->pid, me->nam, me->sta, &tim);
                (void)fclose(file);
            }
            (void)sprintf(me->buf, "%s/%s/cmdline", prc, d->d_name);
            if((fd = open(me->buf, O_RDONLY)) >0)
            {
                if((len = read(fd, me->cmd, sizeof(me->cmd))) > 0)
                {
                    me->cmd[len] = (char) 0x00;
                    while(len > (sub = strlen(me->cmd)))
                        me->cmd[sub] = (char) 0x20;
                }
                else
                    *me->cmd = (char) 0x00;
                (void)close(fd);
            }
            (void)sprintf(me->buf, "%s/%s/status", prc, d->d_name);
            if((file = fopen(me->buf, "r")) != NULL)
            {
                while(fgets(me->buf, isizeof(me->buf), file) != NULL)
                {
                    if(!!sscanf(me->buf, "VmSize:%s", me->mem))
                        break;
                }
                while(fgets(me->buf, isizeof(me->buf), file) != NULL)
                {
                    if(sscanf(me->buf, "VmExe:%s", me->exe))
                        break;
                }
                (void)fclose(file);
            }
            sprintf(me->cpu, "%lu.%02lu", tim/HZ, tim%HZ);
            printf(fmt, me->pid, me->nam, me->sta, me->cpu, me->mem, me->exe, me->cmd);
        }
    }
    free(me);
    (void)closedir(dir);
    return EXIT_SUCCESS;
}
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/

Cheers,
Dick Johnson

Penguin : Linux version 2.3.41 on an i686 machine (800.63 BogoMips).

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Tue Mar 07 2000 - 21:00:09 EST