/* Copyright 2003-2014 James F. Duff */
/* License and disclaimer: http://www.eight-cubed.com/disclaimer.html */

#define __NEW_STARLET 1

#include <stdio.h>
#include <stdlib.h>
#include <ssdef.h>
#include <stsdef.h>
#include <va_rangedef.h>
#include <lib$routines.h>
#include <starlet.h>

#include "errchk.h"

#define ARRAY_SIZE 512 * 2000

static char array[ARRAY_SIZE];


/******************************************************************************/
static void load_array (void) {

static int r0_status;
static VA_RANGE inadr;
static unsigned int i;
static unsigned int j;
static double f1;
static double f2;

    /*
    ** Purge our entire working set so we can't use the fact that a
    ** previous invokation of this routine faulted all the pages in.
    */
    inadr.va_range$ps_start_va = 0;
    inadr.va_range$ps_end_va = (void *)0x7FFFFFFF;
    r0_status = sys$purgws (&inadr);
    errchk_sig (r0_status);

    /*
    ** Get a "known" sequence of random numbers.  That is, generate the same
    ** sequence each time this routine is called.
    */
    srand (10);

    /*
    ** Initialize the timing routine.
    */
    lib$init_timer ();

    /*
    ** Fill the array with junk.
    */
    for (i = 0; i < ARRAY_SIZE * 10; i++) {
        f1 = 1.0 * rand ();
        f2 = 1.0 * rand ();
        j = (int)(f1 / f2 * (double)ARRAY_SIZE);
        j %= ARRAY_SIZE;
        array[j] = (char)i;
    }

    /*
    ** Display how long the for loop took.  What we are really interested in
    ** is how many pagefaults occured.
    */
    lib$show_timer ();
}


/******************************************************************************/
int main (void) {
/*
** Demonstrate how to lock data into the working set.  This program was
** tested with the WSQUOTA for the account set to 2000.
*/
 
static int r0_status;
static VA_RANGE inadr;
static VA_RANGE outadr;

    /*
    ** Get the start and end address of the array.
    */
    inadr.va_range$ps_start_va = &array[0];
    inadr.va_range$ps_end_va = (char *)inadr.va_range$ps_start_va +
                               sizeof (array);

    /*
    ** Lock the array into the working set.
    */
    r0_status = sys$lkwset (&inadr,
                            &outadr,
                            0);
    if (r0_status == SS$_LKWSETFUL) {
        (void)printf ("You exceeded the number of pages you can lock in your\n"
                      "working set.  Try bumping your WSQUOTA up a bit and\n"
                      "logging out and back in, then running this program\n"
                      "again.\n");
        exit (EXIT_FAILURE);
    } else {
        errchk_sig (r0_status);
    }

    (void)printf ("These are the statistics for the run with the array locked"
                  " in the WS:\n");
    load_array ();

    /*
    ** Unlock the pages of the working set that map the array.
    */
    r0_status = sys$ulwset (&outadr,
                            0,
                            0);
    errchk_sig (r0_status);

    (void)printf ("These are the statistics for the run with the array"
                  " not locked:\n");
    load_array ();

    (void)printf ("Hopefully, there is a significant difference in the number"
                  " of pagefaults.\n");
}

Back to the master examples list.