Sponsored Content
Top Forums UNIX for Advanced & Expert Users Process on CPU inside syscall Post 302696909 by jim mcnamara on Wednesday 5th of September 2012 04:44:26 PM
Old 09-05-2012
So, this is the output of the code below, Solaris does not fully support getprusage,
it uses /proc instead,
red colors are related, green colors are related, blue has to do with being out of context or being pushed onto the interrupt stack, one of the 44 times it got interrupted/context switched.
System trap time: for .0204900 second - this is how traps (going to kernel mode for example) are handled. This is the time it takes to initiate into kernel mode for 10+ million syscalls. Maybe this is what you mean. Traps also involve signal processing, when the process comes back from a context switch.

Code:
appworx> ./tmer
10085397
Resource usage for PID 25360:
  LWP ID: 0
  Number of LWPs: 1
  Timestamp: 5711964.772491400
  Creation time: 5711960.622692600
  Termination time: 0.0
  Real (elapsed) time: 4.149402400
  User CPU time: 3.618758600
  System CPU time: 0.530350100
  System trap CPU time: 0.204900
  Text page fault CPU time: 0.0
  Data page fault CPU time: 0.0
  Kernel page fault CPU time: 0.0
  User lock wait time: 0.0
  Other sleep time: 0.0
  CPU latency time: 0.59100
  Stopped time: 0.29700
  Minor faults: 0
  Major faults: 0
  Number of swaps: 0
  Input blocks: 0
  Output blocks: 0
  Messages sent: 0
  Messages received: 0
  Signals received: 0
  Voluntary context switches: 0
  Involuntary context switches: 44
  System calls: 10085457
  Characters read/written: 9

C code
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h> 
#include <time.h>
#ifdef NEED_SNPRINTF
#include <sys/procfs.h>
#else
#include <sys/old_procfs.h>
#endif
#include <limits.h>


int getprusage (pid_t, prusage_t *);
void print_rusage(pid_t, prusage_t *);
void tmer(int, char **);
char dest[64]={0x0};


int main (int argc, char **argv)
{
	prusage_t buf;
	
  tmer(argc, argv);
  if(getprusage (-1, &buf) == -1)
	{
		perror("getprusage failed"); 
		exit(1);
	}		
  print_rusage (getpid (), &buf);
	return 0;
}

void tmer(int argc, char **argv)
{
    size_t add =(argc>1) ? atol(argv[1]): 5;
    time_t tm=time(NULL);
    double z=0;
    tm+=add;
    while(time(NULL) < tm) z++;
    printf("%.0f\n", z);
    return;
}


int getprusage (pid_t pid, prusage_t *pr_usage)
{
	int fd;
	char name [PATH_MAX];

	if (pid == -1)
		snprintf (name, PATH_MAX, "/proc/%ld", (long) getpid ());
	else
		snprintf (name, PATH_MAX, "/proc/%ld", (long) pid);

	if ((fd = open (name, O_RDONLY)) == -1)
		return (-1);

	if (ioctl (fd, PIOCUSAGE, pr_usage) == -1) {
		close (fd);
		return (-1);
	}
	else {
		close (fd);
		return (0);
	}
}

 void print_rusage (pid_t pid, prusage_t *buf)
{
  printf ("Resource usage for PID %ld:\n", (long) pid);
  printf ("  LWP ID: %ld\n", (long) buf -> pr_lwpid);
  printf ("  Number of LWPs: %d\n", (int) buf -> pr_count);
  printf ("  Timestamp: %ld.%ld\n", buf -> pr_tstamp.tv_sec,
  	buf -> pr_tstamp.tv_nsec);
  printf ("  Creation time: %ld.%ld\n", buf -> pr_create.tv_sec,
  	buf -> pr_create.tv_nsec);
  printf ("  Termination time: %ld.%ld\n", buf -> pr_term.tv_sec,
  	buf -> pr_term.tv_nsec);
  printf ("  Real (elapsed) time: %ld.%ld\n", buf -> pr_rtime.tv_sec,
  	buf -> pr_rtime.tv_nsec);
  printf ("  User CPU time: %ld.%ld\n", buf -> pr_utime.tv_sec,
  	buf -> pr_utime.tv_nsec);
  printf ("  System CPU time: %ld.%ld\n", buf -> pr_stime.tv_sec,
  	buf -> pr_stime.tv_nsec);
  printf ("  System trap CPU time: %ld.%ld\n", buf -> pr_ttime.tv_sec,
  	buf -> pr_ttime.tv_nsec);
  printf ("  Text page fault CPU time: %ld.%ld\n", buf -> pr_tftime.tv_sec,
  	buf -> pr_tftime.tv_nsec);
  printf ("  Data page fault CPU time: %ld.%ld\n", buf -> pr_dftime.tv_sec,
  	buf -> pr_dftime.tv_nsec);
  printf ("  Kernel page fault CPU time: %ld.%ld\n", buf -> pr_kftime.tv_sec,
  	buf -> pr_kftime.tv_nsec);
  printf ("  User lock wait time: %ld.%ld\n", buf -> pr_ltime.tv_sec,
  	buf -> pr_ltime.tv_nsec);
  printf ("  Other sleep time: %ld.%ld\n", buf -> pr_slptime.tv_sec,
  	buf -> pr_slptime.tv_nsec);
  printf ("  CPU latency time: %ld.%ld\n", buf -> pr_wtime.tv_sec,
  	buf -> pr_wtime.tv_nsec);
  printf ("  Stopped time: %ld.%ld\n", buf -> pr_stoptime.tv_sec,
  	buf -> pr_stoptime.tv_nsec);
  printf ("  Minor faults: %ld\n", buf -> pr_minf);
  printf ("  Major faults: %ld\n", buf -> pr_majf);
  printf ("  Number of swaps: %ld\n", buf -> pr_nswap);
  printf ("  Input blocks: %ld\n", buf -> pr_inblk);
  printf ("  Output blocks: %ld\n", buf -> pr_oublk);
  printf ("  Messages sent: %ld\n", buf -> pr_msnd);
  printf ("  Messages received: %ld\n", buf -> pr_mrcv);
  printf ("  Signals received: %ld\n", buf -> pr_sigs);
  printf ("  Voluntary context switches: %ld\n", buf -> pr_vctx);
  printf ("  Involuntary context switches: %ld\n", buf -> pr_ictx);
  printf ("  System calls: %ld\n", buf -> pr_sysc);
	printf ("  Characters read/written: %ld\n", buf -> pr_ioch); 
	return;
}

 

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

process -> 1 cpu

Is there a way I can assign processes to different processors? I know in windows xp you can set process affinity, and wondered if there is a *nix equivelant. (2 Replies)
Discussion started by: Solitare
2 Replies

2. UNIX for Dummies Questions & Answers

process not using enough cpu

I am running solaris 9 on a SUn 480r. It is running SAS statistical software, these processes in full flow normally run at about 50-60% cpu (theres nothing else really running on the box) this is fine, and the SAS jobs get run nice and quick. However over the last few weeks everytime a SAS job is... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

3. Solaris

How to find which process is using up too much CPU

Hi, I need to find which processes are hogging the cpu up. please advise. Thanks (1 Reply)
Discussion started by: 0ktalmagik
1 Replies

4. AIX

CPU usage of a process

I'm trying to monitor the CPU usage of a process and output that value to a file or variable. I know topas or nmon can tell me this in interactive mode but what I need is topas-looking output that allows me to write to a file after a discrete interval. Unlike nmon data collection to a file on top... (5 Replies)
Discussion started by: robot23
5 Replies

5. UNIX for Dummies Questions & Answers

how to get persistant cpu utilization values per process per cpu in linux (! top,ps)

hi, i want to know cpu utilizatiion per process per cpu..for single processor also if multicore in linux ..to use these values in shell script to kill processes exceeding cpu utilization.ps (pcpu) command does not give exact values..top does not give persistant values..psstat,vmstat..does njot... (3 Replies)
Discussion started by: pankajd
3 Replies

6. UNIX for Dummies Questions & Answers

100% cpu process

hi guys you know we got an issue and my support line is linux I need to make sure my linux is OK...now the issue I have this process db2sysc reaching 10% CPU utilization yeah that's a DB2 process my DB guys wants to make sure linux is not the culprit but I have no idea the situation is... (3 Replies)
Discussion started by: karlochacon
3 Replies

7. Red Hat

Process by CPU

Hello All, I am preparing a script to capture the processes consuming more CPU. So is there any way that i can sort & redirect to file only those processes consuming more than 5.0 % using ps command itself. Regards Ankit (3 Replies)
Discussion started by: ajaincv
3 Replies

8. Emergency UNIX and Linux Support

CPU and memory utilization of a process, by process name

Can someone please help me with a script that will help in identifying the CPU & memory usage by a process name, rather than a process id.This is to primarily analyze the consumption of resources, for performance tweaking. G (4 Replies)
Discussion started by: ggayathri
4 Replies

9. Shell Programming and Scripting

If a process cpu % is higer than

Is this possible as part of a bigger script to do? eg: elif myprocess.bin cpu% > 12 exit 1 elif is there a command for this? thanks (9 Replies)
Discussion started by: bilboNIX
9 Replies
All times are GMT -4. The time now is 07:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy