Sponsored Content
Top Forums UNIX for Advanced & Expert Users How to find out memory & cpu usage of a process Post 302365205 by jim mcnamara on Monday 26th of October 2009 09:51:08 AM
Old 10-26-2009
You will have to write C code this primitive example using getrusage(). Note -- not all systems fully support all of getrusage():
Code:
/* rusage.c
    usage:
    rusage  [executable_file_or_script]
*/
#include <sys/types.h>
#include <stdlib.h>
#include <sys/resrouces.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>

char cmd[ARG_MAX]={0x0};

int dump(void)
{
		struct rusage s;
		struct rusage *p=&s;
		
  	getrusage(RUSAGE_CHILDREN, p);
		fprintf(stdout, "maximum resident set size              =%ld\n", p->ru_maxrss);
		fprintf(stdout, "integral resident set size             =%ld\n", p->ru_idrss);
		fprintf(stdout, "page faults not requiring physical I/O =%ld\n", p->ru_minflt);
		fprintf(stdout, "page faults requiring physical I/O     =%ld\n", p->ru_majflt);
		fprintf(stdout, "swaps                                  =%ld\n", p->ru_nswap);
		fprintf(stdout, "block input operations                 =%ld\n", p->ru_inblock);
		fprintf(stdout, "block output operations                =%ld\n", p->ru_oublock);
		fprintf(stdout, "messages sent                          =%ld\n", p->ru_msgsnd);
		fprintf(stdout, "messages received                      =%ld\n", p->ru_msgrcv);
		fprintf(stdout, "signals received                       =%ld\n", p->ru_nsignals);
		fprintf(stdout, "voluntary context switches             =%ld\n", p->ru_nvcsw);
		fprintf(stdout, "involuntary context switches           =%ld\n", p->ru_nivcsw);
  	return 1;
}

int main(int argc, char *argv[])
{
    char *p=cmd;
    int i=1;
    ptrdiff_t d=ARG_MAX;

    for(i=1; i < argc; i++)
    {
        size_t len=strlen(argv[i]);
        if( d - len > ARG_MAX)
        {
           sprintf(p, "%s ", argv[i]);
           d-=len;
           p+=(len +1);
        }
        else
        {
        	errno=E2BIG;
        	perror("Invalid parameter list");
        	exit(1);
        }
    }
    if(*cmd)
    {
	  	system(cmd);
	  	dump();
	  }
	  else
	  {
	  	errno=EINVAL;
	  	perror("cannot execute command");
	  	exit(1);
	  }
    return 0;
}

 

10 More Discussions You Might Find Interesting

1. Programming

Monitor CPU usage and Memory Usage

how can i monitor usages of CPU, Memory, Hard disk etc. under SUN Solaries through a c program or java program i want to store that data into database so i can show it graphically thanks in advance (2 Replies)
Discussion started by: Gajanad Bihani
2 Replies

2. HP-UX

How to determine cpu&memory percentage usage per user

Using HP-UX v11 Need to monitor cpu and memory usage, total for system and separately for each user in command-line mode. Found out next ways to monitor total cpu usage under hp-ux: 1) vmstat, also shows free memory 2) sar -M ps -eo user,pcpu - does not work, means 'user-defined format'... (4 Replies)
Discussion started by: hp-ux-user
4 Replies

3. Programming

CPU usage and memory usage

Please tell me solaris functions/api for getting following information 1- Function that tells how much memory used by current process 2- Function that tells how much memory used by all running processes 3- Function that tells how much CPU is used by current process 4- Function that tells how... (1 Reply)
Discussion started by: mansoorulhaq
1 Replies

4. UNIX for Dummies Questions & Answers

cpu, memory and virtual memory usage

Hi All, Does anyone know what the best commands in the UNIX command line are for obtaining this info: current CPU usage memory usage virtual memory usage preferably with date and time parameters too? thanks ocelot (4 Replies)
Discussion started by: ocelot
4 Replies

5. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

6. Solaris

current CPU usage, memory usage, disk I/O oid(snmp)

Hi, I want to monitor the current cpu usage, monitor usage , disk I/o and network utlization for solaris using SNMP. I want the oids for above tasks. can you please tell me that Thank you (2 Replies)
Discussion started by: S_venkatesh
2 Replies

7. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

8. AIX

How to trace cpu/memory usage for a process

I don't know when the process will start and end, I need write a script to trace it's cpu/memory usage when it is runing. How to write this script? (2 Replies)
Discussion started by: rainbow_bean
2 Replies

9. Shell Programming and Scripting

Shell script for logging cpu and memory usage of a Linux process

I am looking for a way to log and graphically display cpu and RAM usage of linux processes over time. Since I couldn't find a simple tool to so (I tried zabbix and munin but installation failed) I started writing a shell script to do so The script file parses the output of top command through... (2 Replies)
Discussion started by: andy_dufresne
2 Replies

10. Solaris

Find memory usage for a process

I have multiple oracle databases on one server. All the database are running from the same user i.e. oraent. The process for each database can be distinguished by the ps -ef command Eg : ps -ef | grep oraentThe Output : oraent 5361 1 0 20:58:05 ? 0:00 oracledb1... (11 Replies)
Discussion started by: yashreads
11 Replies
getrusage(3C)						   Standard C Library Functions 					     getrusage(3C)

NAME
getrusage - get information about resource utilization SYNOPSIS
#include <sys/resource.h> int getrusage(int who, struct rusage *r_usage); DESCRIPTION
The getrusage() function provides measures of the resources used by the current process, its terminated and waited-for child processes, or the current light weight process (LWP). If the value of the who argument is RUSAGE_SELF, information is returned about resources used by the current process. If the value of the who argument is RUSAGE_CHILDREN, information is returned about resources used by the terminated and waited-for children of the current process. If the child is never waited for (for instance, if the parent has SA_NOCLDWAIT set or sets SIGCHLD to SIG_IGN), the resource information for the child process is discarded and not included in the resource information provided by getrusage(). If the value of the who argument is RUSAGE_LWP, information is returned about resources used by the current LWP. The r_usage argument is a pointer to an object of type struct rusage in which the returned information is stored. The members of rusage are as follows: struct timeval ru_utime; /* user time used */ struct timeval ru_stime; /* system time used */ long ru_maxrss; /* maximum resident set size */ long ru_idrss; /* integral resident set size */ long ru_minflt; /* page faults not requiring physical I/O */ long ru_majflt; /* page faults requiring physical I/O */ long ru_nswap; /* swaps */ long ru_inblock; /* block input operations */ long ru_oublock; /* block output operations */ long ru_msgsnd; /* messages sent */ long ru_msgrcv; /* messages received */ long ru_nsignals; /* signals received */ long ru_nvcsw; /* voluntary context switches */ long ru_nivcsw; /* involuntary context switches */ The structure members are interpreted as follows: ru_utime The total amount of time spent executing in user mode. Time is given in seconds and microseconds. ru_stime The total amount of time spent executing in system mode. Time is given in seconds and microseconds. ru_maxrss The maximum resident set size. Size is given in pages (the size of a page, in bytes, is given by the getpagesize(3C) func- tion). See the NOTES section of this page. ru_idrss An "integral" value indicating the amount of memory in use by a process while the process is running. This value is the sum of the resident set sizes of the process running when a clock tick occurs. The value is given in pages times clock ticks. It does not take sharing into account. See the NOTES section of this page. ru_minflt The number of page faults serviced which did not require any physical I/O activity. See the NOTES section of this page. ru_majflt The number of page faults serviced which required physical I/O activity. This could include page ahead operations by the kernel. See the NOTES section of this page. ru_nswap The number of times a process was swapped out of main memory. ru_inblock The number of times the file system had to perform input in servicing a read(2) request. ru_oublock The number of times the file system had to perform output in servicing a write(2) request. ru_msgsnd The number of messages sent over sockets. ru_msgrcv The number of messages received from sockets. ru_nsignals The number of signals delivered. ru_nvcsw The number of times a context switch resulted due to a process voluntarily giving up the processor before its time slice was completed (usually to await availability of a resource). ru_nivcsw The number of times a context switch resulted due to a higher priority process becoming runnable or because the current process exceeded its time slice. RETURN VALUES
Upon successful completion, getrusage() returns 0. Otherwise, -1 is returned and errno is set to indicate the error. ERRORS
The getrusage() function will fail if: EFAULT The address specified by the r_usage argument is not in a valid portion of the process' address space. EINVAL The who parameter is not a valid value. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ SEE ALSO
sar(1M), read(2), times(2), write(2), getpagesize(3C), gettimeofday(3C), wait(3C), attributes(5), standards(5) NOTES
The ru_maxrss, ru_ixrss, ru_idrss, and ru_isrss members of the rusage structure are set to 0 in this implementation. The numbers ru_inblock and ru_oublock account only for real I/O, and are approximate measures at best. Data supplied by the cache mechanism is charged only to the first process to read and the last process to write the data. The way resident set size is calculated is an approximation, and could misrepresent the true resident set size. Page faults can be generated from a variety of sources and for a variety of reasons. The customary cause for a page fault is a direct ref- erence by the program to a page which is not in memory. Now, however, the kernel can generate page faults on behalf of the user, for exam- ple, servicing read(2) and write(2) functions. Also, a page fault can be caused by an absent hardware translation to a page, even though the page is in physical memory. In addition to hardware detected page faults, the kernel may cause pseudo page faults in order to perform some housekeeping. For example, the kernel may generate page faults, even if the pages exist in physical memory, in order to lock down pages involved in a raw I/O request. By definition, major page faults require physical I/O, while minor page faults do not require physical I/O. For example, reclaiming the page from the free list would avoid I/O and generate a minor page fault. More commonly, minor page faults occur during process startup as references to pages which are already in memory. For example, if an address space faults on some "hot" executable or shared library, this results in a minor page fault for the address space. Also, any one doing a read(2) or write(2) to something that is in the page cache will get a minor page fault(s) as well. There is no way to obtain information about a child process which has not yet terminated. SunOS 5.10 2 Jul 2004 getrusage(3C)
All times are GMT -4. The time now is 08:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy