/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <descrip.h>
#include <starlet.h>
#include <rms.h>
#include <ssdef.h>
#include <iodef.h>
#include <fibdef.h>
#include <atrdef.h>

#include "high_res_timer.h"

struct FAT
{
    unsigned long fat$v_rtype: 4;
    unsigned long fat$v_fileorg: 4;
    unsigned char fat$b_rattrib;
    unsigned short fat$w_rsize;
    unsigned short fat$w_hiblkh,fat$w_hiblkl;
    unsigned short fat$w_efblkh,fat$w_efblkl;
    unsigned short fat$w_ffbyte;
    unsigned char fat$b_bktsize;
    unsigned char fat$b_vfcsize;
    unsigned short fat$w_maxrec;
    unsigned short fat$w_defext;
    unsigned short fat$w_gbc;
    unsigned char fill_1[8];
    unsigned short fat$w_versions;
};

static long long offset(int blkno, int bytno)
{
    return (blkno - 1) * 512L + bytno;
}

#define PAR_FACTOR 60

static void test(const char *fnm, int expect, int blksiz)
{
    long stat;
    unsigned short iosb[PAR_FACTOR][4];
    struct FAB fab;
    struct NAM nam;
    struct dsc$descriptor_s devdesc;
    short chan;
    struct fibdef fib;
    long fibdesc[] = { FIB$K_LENGTH, (long)&fib };
    struct FAT fat;
    struct atrdef atr[] = { {ATR$S_RECATTR, ATR$C_RECATTR, &fat},{ 0, 0, NULL } };
    long eof;
    short ffb;
    int adjblksiz;
    char *buf;
    char expfnm[NAM$C_MAXRSS];
    int ix, len, n, i;
    int nread, slot, efn;
    int outstand[PAR_FACTOR] = { 0 };
    TIMECOUNT_T t1, t2;
    t1 = GET_TIMECOUNT;
    buf = malloc(PAR_FACTOR * blksiz);
    fab = cc$rms_fab;
    nam = cc$rms_nam;
    fab.fab$l_fna = (char *)fnm;
    fab.fab$b_fns = strlen(fnm);
    fab.fab$b_fac = FAB$M_GET;
    fab.fab$l_nam = &nam;
    nam.nam$b_rss = sizeof(expfnm) - 1;
    nam.nam$l_rsa = expfnm;
    stat = sys$open(&fab, 0, 0);
    if(!(stat & 1))
    {
        printf("sys$open stat = %d\n", stat);
    }
    stat = sys$close(&fab, 0, 0);
    if(!(stat & 1))
    {
       printf("sys$close stat = %d\n", stat);
    }
    devdesc.dsc$b_dtype = DSC$K_DTYPE_T;
    devdesc.dsc$b_class = DSC$K_CLASS_S;
    devdesc.dsc$w_length = strlen(expfnm);
    devdesc.dsc$a_pointer = expfnm;
    stat = sys$assign(&devdesc, &chan, 0, 0, 0);
    if(!(stat & 1))
    {
       printf("sys$assign stat = %d\n", stat);
    }
    memset(&fib, 0, sizeof(fib));
    fib.fib$w_fid[0] = nam.nam$w_fid[0];
    fib.fib$w_fid[1] = nam.nam$w_fid[1];
    fib.fib$w_fid[2] = nam.nam$w_fid[2];
    stat = sys$qiow(0, chan, IO$_ACCESS | IO$M_ACCESS, iosb, 0, 0, &fibdesc, 0, 0, 0, &atr, 0);
    if(!(stat & 1))
    {
       printf("sys$qiow io$_access stat = %d\n", stat);
    }
    eof = (fat.fat$w_efblkh << 16) | fat.fat$w_efblkl;
    ffb = fat.fat$w_ffbyte;
    ix = 1;
    n = 0;
    nread = 0;
    for(;;)
    {
        if(ix > eof) break;
        if(offset(ix, blksiz) <= offset(eof, ffb))
        {
            adjblksiz = blksiz;
        }
        else
        {
            adjblksiz = offset(eof, ffb) - offset(ix, 0);
        }
        slot = nread % PAR_FACTOR;
        efn = slot + 1; 
        if(outstand[slot])
        {
            stat = sys$synch(efn, iosb[slot]);
            if(!(stat & 1))
            {
               printf("sys$synch stat = %d\n", stat);
            }
            if(iosb[slot][0] == SS$_ENDOFFILE)
            {
                break;
            }
            else if(!(iosb[slot][0] & 1))
            {
               printf("sys$qio io$_readvblk iosb[0] = %d\n", iosb[slot][0]);
            }
            len = (iosb[slot][2] << 16) | iosb[slot][1];
            for(i = 0; i < len; i++)
            {
                if(buf[slot * blksiz + i] == '\n') n++;
            }
            outstand[slot] = 0;
        }
        stat = sys$qio(efn, chan, IO$_READVBLK, iosb[slot], 0, 0, buf + slot * blksiz, adjblksiz, ix, 0, 0, 0);
        if(!(stat & 1))
        {
           printf("sys$qio io$_readvblk stat = %d\n", stat);
        }
        outstand[slot] = 1;
        ix = ix + blksiz / 512;
        nread++;
    }
    for(slot = 0; slot < PAR_FACTOR; slot++)
    {
        if(outstand[slot])
        {
            efn = slot + 1; 
            stat = sys$synch(efn, iosb[slot]);
            if(iosb[slot][0] == SS$_ENDOFFILE)
            {
                continue;
            }
            else if(!(iosb[slot][0] & 1))
            {
               printf("sys$qio io$_readvblk iosb[0] = %d\n", iosb[slot][0]);
            }
            len = (iosb[slot][2] << 16) | iosb[slot][1];
            for(i = 0; i < len; i++)
            {
                if(buf[slot * blksiz + i] == '\n') n++;
            }
            outstand[slot] = 0;
        }
    }
    if(n != expect)
    {
        printf("%d lines\n", n);
    }
    stat = sys$qiow(0, chan, IO$_DEACCESS, iosb, 0, 0, &fibdesc, 0, 0, 0, &atr, 0);
    if(!(stat & 1))
    {
       printf("sys$qiow io$_deaccess stat = %d\n", stat);
    }
    stat = sys$dassgn(chan);
    if(!(stat & 1))
    {
       printf("sys$dassgn stat = %d\n", stat);
    }
    free(buf);
    t2 = GET_TIMECOUNT;
    printf("sys$qio (block size %d) : %d ms\n", blksiz, (int)((t2 - t1) * 1000 / UNITS_PER_SECOND));
}

int main(int argc, char *argv[])
{
    int blksiz[] = { 512, 5120, 51200, 512000 };
    int i, j;
    for(j = 0; j < 4; j++)
    {
        for(i = 0; i < 10; i++)
        {
            test(argv[1], atoi(argv[2]), blksiz[j]);
        }
    }
    return 0;
}
