Hi,

I need a method to get the load of a process in percent of cpu time on a
per second basis, just like top on Linux does. Top on OpenBSD seems to
calculate the %cpu like ps. ps(1) states the following:

%cpu    Alias: pcpu.  The CPU utilization of the process; this is
             a decaying average over up to a minute of previous (real)
             time.  Since the time base over which this is computed
             varies (since processes may be very young) it is possible
             for the sum of all %cpu fields to exceed 100%.


As I couldn't find something that does the calculation on a per second
basis, I started to write something on my own. For that reason I plan to
look at struct kinfo_proc2 field p_cpticks of libkvm, but this field
gives me some odd values, which I'm unable to interpret. Could you
please comment on the following code?

int main(int argc, char** argv)
{
        kvm_t *kd;
        struct kinfo_proc2 *info;
        int cnt;

        if(argc != 2)
        {
                printf("Please provide pid\n");
                exit(1);
        }


        kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "error");
        if(!kd)
                exit(1);

        if(cnt != 1)
        {
                printf("No process found\n");
                exit(1);
        }

        info = kvm_getproc2(kd, KERN_PROC_PID, atoi(argv[1]),
sizeof(struct kinfo_proc2), &cnt);

        printf("%x\n",(double)info->p_cpticks);

        exit(0);
}

Strange value:

$ for((i=0;i<4;i++)); do ./cpucalc 28175 ; done
6
6
6
6


Any help would be appreciated

--
Sebastian Spies                      e-mail: sebastian.sp...@de-cix.net

[demime 1.01d removed an attachment of type application/pgp-signature which had 
a name of signature.asc]

Reply via email to