How can I get a processor's CPU Percentage?

you know that.. in HP-UX..
in sys/pstat.h

struct pst_status {
long pst_idx; /* Index for further pstat() requests /
long pst_uid; /
Real UID /
long pst_pid; /
Process ID /
....
float pst_pctcpu; /
%cpu for this process during p_time
....
}

when, need to get a processor's cpu used %,
we use pst_status.pst_pctcpu..
but i want to know other way.. not use pst_status.pst_pctcpu.
How can I get that??

and what is p_time ??

Kernels always have some code built in to gather statistics. In the case of cpus, several times a second a clock routine fires off and increments one element of a structure. The structure will have counters for idle, user, system, and so on.

In the old days, you could get the structure if you knew its name. You would run nlist(3) on the kernel's symbol table to get the address of the structure. Then you opened /dev/kmem. did a seek to the address and read the structure. The c definations of the structures were usually in /usr/include. This still can be made to work on hp-ux, but the required information is no longer supplied in /usr/include. Skilled gurus can reverse-engineer it, but it's difficult. Vic Able has done this to get lsof to work on recent versions of HP-UX, a feat that very few could match.

New system calls like pstat are the way of the future. And pstat works well enough. Why do you want an alternative?

first of all..
thanks for your kindly reply..

I want to know about getting cpu percentage not use structure
because in SCO-UNIX, there is no cpu percentage ..but only
cpu time ( timestruc_t pr_utime; )
so.. I asked get that percantage without structure..

I can get get cpu percentage in HP-UX using pstat.h..
but.. How can I get cpu percentage in SCO-UNIX ??

/** SCO-UNIX , procfs.h */
typedef struct pstatus {
....
pid_t pr_pid; /
Process id /
pid_t pr_ppid; /
Parent process id /
pid_t pr_pgid; /
Process group id /
pid_t pr_sid; /
Session id /
timestruc_t pr_utime; /
Process user cpu time /
timestruc_t pr_stime; /
Process system cpu time /
timestruc_t pr_cutime; /
Sum of children's user times /
timestruc_t pr_cstime; /
Sum of children's system times /
sigset_t pr_sigtrace; /
Mask of traced signals /
fltset_t pr_flttrace; /
Mask of traced faults /
sysset_t pr_sysentry; /
Mask of system calls traced on
} pstatus_t;

I believe this is similar to what you read in /proc/stat on Linux. In Linux, you can read the timer ticks spent on user, system and IO, IRQ etc using /proc/stat. But you need to calculate the CPU % by adding the total time spent against each category. Generally following is a common approach. If you look at source of procps (vmstat.c) you might be able to get an idea how it's done on Linux.

All CPU = User + System + Idle time

User CPU % = User / All CPU * 100%

System CPU % = System / All CPU * 100%

But you have to check where you can place pr_cutime and pr_cstime. It looks like it's part of User time.