Programmatically getting CPU utilization on AIX - Aix

This is a discussion on Programmatically getting CPU utilization on AIX - Aix ; Hi All, I am trying to write a C program on AIX that monitors the CPU utilization. The program has to check the usage every 1 minutes (say), if the utilization is more that 95%, it has to write to ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: Programmatically getting CPU utilization on AIX

  1. Programmatically getting CPU utilization on AIX

    Hi All,

    I am trying to write a C program on AIX that monitors the CPU
    utilization. The program has to check the usage every 1 minutes (say),
    if the utilization is more that 95%, it has to write to a log. I am
    not able to find from the documentation on how to do this. In HP-UX,
    we can use the 'pstat_getprocessor' function to get the cpu details.
    Please let me know if there something similar in AIX?


  2. Re: Programmatically getting CPU utilization on AIX

    ananthbv@gmail.com writes:

    > Hi All,
    >
    > I am trying to write a C program on AIX that monitors the CPU
    > utilization. The program has to check the usage every 1 minutes (say),
    > if the utilization is more that 95%, it has to write to a log. I am
    > not able to find from the documentation on how to do this. In HP-UX,
    > we can use the 'pstat_getprocessor' function to get the cpu details.
    > Please let me know if there something similar in AIX?
    >



    <http://publib.boulder.ibm.com/infoce...5r3/index.jsp?

    Do a search for perfstat, and pick "Perfstat API Programming". For an
    example of code that purportedly works, have a look at the AIX gmond
    daemon source code that is part of ganglia at ganglia.sourceforge.net

    Hmmm, there's a simpler way ... you could look at
    /usr/samples/libperstat/perfsample.c instead .

    For a completely different approach, search instead for SPMI, and pick
    "System Performance Measurement Interface Programming Guide". If you
    head this direction, have a look at the sample code in
    /usr/samples/perfagent/server.

    Before you seriously begin, I suggest a survey of the info in the
    "Performance Toolbox Version 2 and 3 Guide and Reference". HotSet
    recording may be able to do what you want accomplished.

    For a brute force implemenation, one could probably pipe the logging
    output of topas through a perl or other filter :-)

    HTH.

    --
    #include /* I don't speak for IBM ... */
    /* Heck, I don't even speak for myself */
    /* Don't believe me ? Ask my wife :-) */
    Richard D. Latham lathamr@us.ibm.com

  3. Re: Programmatically getting CPU utilization on AIX

    On May 16, 7:41 pm, anant...@gmail.com wrote:
    > Hi All,
    >
    > I am trying to write a C program on AIX that monitors the CPU
    > utilization. The program has to check the usage every 1 minutes (say),
    > if the utilization is more that 95%, it has to write to a log. I am
    > not able to find from the documentation on how to do this. In HP-UX,
    > we can use the 'pstat_getprocessor' function to get the cpu details.
    > Please let me know if there something similar in AIX?


    You can get load avg kind of information (similar to the one reported
    by
    uptime command). Anyway, %cpu utilization would then just is a matter
    of
    dividing by #cpus, and multiplying by 100.

    loadavg gets updated in the system variable "avenrun[]" which is an
    array of 3
    long integers (for last 5, 10, 15 min respectively). so you just have
    to access
    this variable (and divide it by 65536 for proper scaling), and voila!
    you have
    the "uptime" kind of output.

    If you want to access this in kernel space, you just declare "extern
    long avenrun[]",
    and during linking it could just get properly linked (you have to use
    proper .exp/.imp
    files though).

    If you're in userland, try to access this variable through /dev/kmem.
    Below is a program
    I wrote sometimes back...not sure if it works all right now, but
    anyway, it should be
    enough to at least give you an idea.


    #include
    #include
    #include
    #include
    #include

    #define AVES 3
    #define SCALE 65536

    int Kmem;
    struct nlist Nl[] = { "avenrun" };

    static int kread(off_t addr, char *buf, unsigned len);

    main(int argc, char *argv[])
    {
    int i;
    long avenrun[AVES];

    if ((Kmem = open("/dev/kmem", O_RDONLY, 0)) < 0) {
    (void) printf("can't open /dev/kmem: %s\n",
    strerror(errno));
    exit(1);
    }
    if (knlist(&Nl, 1, sizeof(struct nlist)) || Nl[0].n_value ==
    NULL) {
    (void) printf("nlist(avenrun) failure: %s\n",
    strerror(errno));
    exit(1);
    }
    if (kread((off_t)Nl[0].n_value, (char *)avenrun,
    sizeof(avenrun))) {
    (void) printf("can't read avenrun\n");
    exit(1);
    }
    (void) printf("load average: ");
    for (i = 0; i < AVES; i++) {
    if (i)
    fputs(", ", stdout);
    (void) printf("%.2f", (double)avenrun[i]/SCALE);
    }
    putchar('\n');
    exit(0);

    }

    static int
    kread(off_t addr, char *buf, unsigned len)
    {
    int br;

    if (lseek(Kmem, addr, L_SET) == (off_t)-1L)
    return(-1);
    br = read(Kmem, buf, len);
    return((br == len) ? 0 : 1);
    }


  4. Re: Programmatically getting CPU utilization on AIX


    Thanks very much for your replies!

    On May 16, 5:26 pm, lath...@us.ibm.com (Richard D. Latham) wrote:
    > anant...@gmail.com writes:
    > > Hi All,

    >
    > > I am trying to write a C program on AIX that monitors the CPU
    > > utilization. The program has to check the usage every 1 minutes (say),
    > > if the utilization is more that 95%, it has to write to a log. I am
    > > not able to find from the documentation on how to do this. In HP-UX,
    > > we can use the 'pstat_getprocessor' function to get the cpu details.
    > > Please let me know if there something similar in AIX?

    >
    > <http://publib.boulder.ibm.com/infoce...5r3/index.jsp?
    >
    > Do a search for perfstat, and pick "Perfstat API Programming". For an
    > example of code that purportedly works, have a look at the AIX gmond
    > daemon source code that is part of ganglia at ganglia.sourceforge.net
    >
    > Hmmm, there's a simpler way ... you could look at
    > /usr/samples/libperstat/perfsample.c instead .


    The perfstat API is great. I'll go through the samples.


    On May 17, 4:48 am, "kcnain...@gmail.com" wrote:
    > On May 16, 7:41 pm, anant...@gmail.com wrote:
    >
    > > Hi All,

    >
    > > I am trying to write a C program on AIX that monitors the CPU
    > > utilization. The program has to check the usage every 1 minutes (say),
    > > if the utilization is more that 95%, it has to write to a log. I am
    > > not able to find from the documentation on how to do this. In HP-UX,
    > > we can use the 'pstat_getprocessor' function to get the cpu details.
    > > Please let me know if there something similar in AIX?

    >
    > You can get load avg kind of information (similar to the one reported
    > by
    > uptime command). Anyway, %cpu utilization would then just is a matter
    > of
    > dividing by #cpus, and multiplying by 100.
    >
    > loadavg gets updated in the system variable "avenrun[]" which is an
    > array of 3
    > long integers (for last 5, 10, 15 min respectively). so you just have
    > to access
    > this variable (and divide it by 65536 for proper scaling), and voila!
    > you have
    > the "uptime" kind of output.
    >
    > If you want to access this in kernel space, you just declare "extern
    > long avenrun[]",
    > and during linking it could just get properly linked (you have to use
    > proper .exp/.imp
    > files though).
    >
    > If you're in userland, try to access this variable through /dev/kmem.
    > Below is a program
    > I wrote sometimes back...not sure if it works all right now, but
    > anyway, it should be
    > enough to at least give you an idea.


    By %utilization, I actually meant the usage as shown by top, as below:

    CPU0 states: 0.1% user, 0.0% system, 0.0% nice, 99.0% idle
    CPU1 states: 1.0% user, 0.0% system, 1.0% nice, 98.0% idle

    what symbol name should i use in this case?

    Also, where can i get more info on the kernel symbols? For ex: if want
    to find the available and total swap memory on the system, how can i
    find out what symbol should be passed to nlist()? is it present in
    some file?

    - Hide quoted text -

    >
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > #define AVES 3
    > #define SCALE 65536
    >
    > int Kmem;
    > struct nlist Nl[] = { "avenrun" };
    >
    > static int kread(off_t addr, char *buf, unsigned len);
    >
    > main(int argc, char *argv[])
    > {
    > int i;
    > long avenrun[AVES];
    >
    > if ((Kmem = open("/dev/kmem", O_RDONLY, 0)) < 0) {
    > (void) printf("can't open /dev/kmem: %s\n",
    > strerror(errno));
    > exit(1);
    > }
    > if (knlist(&Nl, 1, sizeof(struct nlist)) || Nl[0].n_value ==
    > NULL) {
    > (void) printf("nlist(avenrun) failure: %s\n",
    > strerror(errno));
    > exit(1);
    > }
    > if (kread((off_t)Nl[0].n_value, (char *)avenrun,
    > sizeof(avenrun))) {
    > (void) printf("can't read avenrun\n");
    > exit(1);
    > }
    > (void) printf("load average: ");
    > for (i = 0; i < AVES; i++) {
    > if (i)
    > fputs(", ", stdout);
    > (void) printf("%.2f", (double)avenrun[i]/SCALE);
    > }
    > putchar('\n');
    > exit(0);
    >
    > }
    >
    > static int
    > kread(off_t addr, char *buf, unsigned len)
    > {
    > int br;
    >
    > if (lseek(Kmem, addr, L_SET) == (off_t)-1L)
    > return(-1);
    > br = read(Kmem, buf, len);
    > return((br == len) ? 0 : 1);
    >
    >
    >
    > }
    >
    > - Show quoted text -



  5. Re: Programmatically getting CPU utilization on AIX

    On May 18, 9:07 am, ananth wrote:
    > Thanks very much for your replies!
    >
    > On May 16, 5:26 pm, lath...@us.ibm.com (Richard D. Latham) wrote:
    >
    >
    >
    > > anant...@gmail.com writes:
    > > > Hi All,

    >
    > > > I am trying to write a C program on AIX that monitors the CPU
    > > > utilization. The program has to check the usage every 1 minutes (say),
    > > > if the utilization is more that 95%, it has to write to a log. I am
    > > > not able to find from the documentation on how to do this. In HP-UX,
    > > > we can use the 'pstat_getprocessor' function to get the cpu details.
    > > > Please let me know if there something similar in AIX?

    >
    > > <http://publib.boulder.ibm.com/infoce...5r3/index.jsp?

    >
    > > Do a search for perfstat, and pick "Perfstat API Programming". For an
    > > example of code that purportedly works, have a look at the AIX gmond
    > > daemon source code that is part of ganglia at ganglia.sourceforge.net

    >
    > > Hmmm, there's a simpler way ... you could look at
    > > /usr/samples/libperstat/perfsample.c instead .

    >
    > The perfstat API is great. I'll go through the samples.
    >
    > On May 17, 4:48 am, "kcnain...@gmail.com" wrote:
    >
    >
    >
    > > On May 16, 7:41 pm, anant...@gmail.com wrote:

    >
    > > > Hi All,

    >
    > > > I am trying to write a C program on AIX that monitors the CPU
    > > > utilization. The program has to check the usage every 1 minutes (say),
    > > > if the utilization is more that 95%, it has to write to a log. I am
    > > > not able to find from the documentation on how to do this. In HP-UX,
    > > > we can use the 'pstat_getprocessor' function to get the cpu details.
    > > > Please let me know if there something similar in AIX?

    >
    > > You can get load avg kind of information (similar to the one reported
    > > by
    > > uptime command). Anyway, %cpu utilization would then just is a matter
    > > of
    > > dividing by #cpus, and multiplying by 100.

    >
    > > loadavg gets updated in the system variable "avenrun[]" which is an
    > > array of 3
    > > long integers (for last 5, 10, 15 min respectively). so you just have
    > > to access
    > > this variable (and divide it by 65536 for proper scaling), and voila!
    > > you have
    > > the "uptime" kind of output.

    >
    > > If you want to access this in kernel space, you just declare "extern
    > > long avenrun[]",
    > > and during linking it could just get properly linked (you have to use
    > > proper .exp/.imp
    > > files though).

    >
    > > If you're in userland, try to access this variable through /dev/kmem.
    > > Below is a program
    > > I wrote sometimes back...not sure if it works all right now, but
    > > anyway, it should be
    > > enough to at least give you an idea.

    >
    > By %utilization, I actually meant the usage as shown by top, as below:
    >
    > CPU0 states: 0.1% user, 0.0% system, 0.0% nice, 99.0% idle
    > CPU1 states: 1.0% user, 0.0% system, 1.0% nice, 98.0% idle
    >
    > what symbol name should i use in this case?
    >
    > Also, where can i get more info on the kernel symbols? For ex: if want
    > to find the available and total swap memory on the system, how can i
    > find out what symbol should be passed to nlist()? is it present in
    > some file?
    >


    All the exported symbols should be in one of
    the /usr/lib/*.exp files. Dig through these files,
    search for these symbol definitions in the header
    files /usr/include/* and do some google-ing. With
    luck you might get what you want.

    Also, there were many similar topics posted
    on this newsgroup earlier (at least one by me
    For example, search for "avenrun" in this
    newsgroup. Read them up.

    All the best.
    -Kalash


  6. Re: Programmatically getting CPU utilization on AIX

    If you want some example code, then Nigel "NMON" Griffiths has put
    some "Roll Your Own" code on the IBM 5L Wiki

    Ref: http://www-941.ibm.com/collaboration.../WikiPtype/ryo

    HTH
    Mark Taylor



  7. Re: Programmatically getting CPU utilization on AIX


    Thanks a lot Kalash and Mark.


    On May 18, 1:52 pm, Mark Taylor wrote:
    > If you want some example code, then Nigel "NMON" Griffiths has put
    > some "Roll Your Own" code on the IBM 5L Wiki
    >
    > Ref:http://www-941.ibm.com/collaboration.../WikiPtype/ryo
    >
    > HTH
    > Mark Taylor




+ Reply to Thread