common/readdata.c
///////////////////////////////////////////////////////////////////////////////
// Filename: readdata.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: for performance measurements: read the data generated by
//          writedata(). The time used to read all the data is displayed.
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date     Time     Name      Description   
// -------- -------- --------  ------------------------------------------------
// 96/02/27 18:32:15 muellerg: created
//
///////////////////////////////////////////////////////////////////////////////
// Feature test switches ///////////////////////////// Feature test switches //
    /* NONE */
// System headers /////////////////////////////////////////// System headers //
#include <stdlib.h>
// Local headers ///////////////////////////////////////////// Local headers //
#include "../common.h"
#include "readwritedata.h"
// Macros /////////////////////////////////////////////////////////// Macros //
    /* NONE */
// File scope objects /////////////////////////////////// File scope objects //
    /* NONE */
// External variables, functions, and classes ///////////// External objects //
    /* NONE */
// Signal catching functions ///////////////////// Signal catching functions //
    /* NONE */
// Structures, unions, and class definitions /////////////////// Definitions //
    /* NONE */
// Functions and class implementation /// Functions and class implementation //
/*
 * readdata(): read the data generated by writedata() and writes out how
 *             long it took to read all the data
 *             the function needs a descriptor to read from, so it is
 *             only usable for IPC facilities which allow reading and writing 
 *             data to descriptors.
 *
 * in: name: base name for logfile(s)
 *      ext: extension (e.g. "read" or "write")
 *       fd: descriptor to read from
 *      pid: process to synchronize with (if 0, no synchronization is done,
 *           e.g. if processes run on different computers...)
 *           to use these facility the caller has to install a special
 *           signal handler with  synchronization_init();
 *
 * returns: true if everything is ok, else false
 *
 */
bool 
readdata(char *name, char *ext, int fd, pid_t pid)
{
    char *buffer=new char[size_max];
    if(!buffer) return false;
    
    measurement mes(name, ext, measurement_max);
    int i, size;
    for(size=1; size <= size_max; size *= 2 )
    {
        if(pid)
        {
            // signal other process that we are ready to read
            synchronization_signal_parent(pid);
            // and wait for acknowledgment
            synchronization_wait();
        }           
        // start timer
        mes.start(size, how_often);
        
        for(i=0; i < how_often; i++)
        {
            readn(fd, buffer, size);
        } 
        // store data about measurement
        mes.end();
    }
    mes.writeout_logfile();
    mes.writeout_plain_logfile();
    delete[] buffer;
    return true;    
}
// Main /////////////////////////////////////////////////////////////// Main //
    /* NONE */