/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* gos720 src/gos/2d/XTOP/contrib/programs/xload/get_load64.c 1.1         */
/*                                                                        */
/* Licensed Materials - Property of IBM                                   */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2002                   */
/* All Rights Reserved                                                    */
/*                                                                        */
/* US Government Users Restricted Rights - Use, duplication or            */
/* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.      */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/* SCCSID_BEGIN_TAG                                                    */
#ifndef lint
static char sccsid[] = "@(#)90  1.1  src/gos/2d/XTOP/contrib/programs/xload/get_load64.c, xsample, gos720 5/22/02 10:34:43";
#endif
/* SCCSID_END_TAG                                                      */
/*

Copyright (c) 1989  X Consortium

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the X Consortium shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from the X Consortium.

*/

/*
 * get_load64 - get system load on 64-bit AIX systems
 *
 * Authors:  Many and varied...
 *
 * Call InitLoadPoint64() to initialize.
 * GetLoadPoint64() is a callback for the StripChart widget.
 */

#include <X11/Xos.h>
#include <X11/Intrinsic.h>
#include <stdio.h>

#include <nlist.h>

#include <sys/param.h>
#define word word_t
#include <sys/sysinfo.h>
#undef word
#undef n_type
#define n_type n_value

#define FSHIFT  16
#define FSCALE  (1<<FSHIFT)

#ifdef SVR4
#ifndef FSCALE
#define FSCALE  (1 << 8)
#endif
#endif

extern void exit();

static xload_error();

#ifndef KMEM_FILE
#define KMEM_FILE "/dev/kmem"
#endif

#ifndef KERNEL_FILE
#define KERNEL_FILE "/unix"
#endif

#ifndef KERNEL_LOAD_VARIABLE
#define KERNEL_LOAD_VARIABLE "avenrun"
#endif

static struct nlist64 namelist[] = {          /* namelist for vmunix grubbing */
#define LOADAV 0
    {KERNEL_LOAD_VARIABLE},
    {0}
};

static kmem;
static long long loadavg_seek;


InitLoadPoint64()
{
    knlist( (struct nlist *)namelist, 1, sizeof(struct nlist64));

    if (namelist[LOADAV].n_type == 0 ||
        namelist[LOADAV].n_value == 0) {
        xload_error("cannot get name list from", KERNEL_FILE);
        exit(-1);
    }
    loadavg_seek = namelist[LOADAV].n_value;

    kmem = open64(KMEM_FILE, O_RDONLY);
    if (kmem < 0) xload_error("cannot open", KMEM_FILE);
}

/* ARGSUSED */
void GetLoadPoint64( w, closure, call_data )
     Widget     w;              /* unused */
     caddr_t    closure;        /* unused */
     caddr_t    call_data;      /* pointer to (double) return value */
{
        double *loadavg = (double *)call_data;

        (void) lseek64(kmem, loadavg_seek, 0);

        {
          static firsttime = TRUE;
          static unsigned long long avenrun = 0;

          (void) lseek64(kmem, loadavg_seek, 0);
          (void) read(kmem, (char *)&avenrun, sizeof(avenrun));
          if (firsttime)
            {
              *loadavg = 0.0;
              firsttime = FALSE;
            }
          else
            {
              sleep(1);
              (void) lseek64(kmem, loadavg_seek, 0);
              (void) read(kmem, (char *)&avenrun, sizeof(avenrun));
              *loadavg = (double)avenrun/FSCALE;
            }
          /* otherwise we leave load alone. */
        }
        return;
}

static xload_error(str1, str2)
char *str1, *str2;
{
    (void) fprintf(stderr,"xload: %s %s\n", str1, str2);
    exit(-1);
}

