Nltransfer API examples

From NetLogger

Jump to: navigation, search

Contents

Logging everything to the same place (version=3.6.5)

Download

Download tarball from:

http://dsd.lbl.gov/NetLogger/download/dev/netlogger-c-3.6.5.tar.gz

Run

Build as usual, then run the 2nd part of the nltransfer_example in the examples dir:

cd examples
./nltransfer_example -2

The important thing to notice is that all the logging is going to the logfile. If you provide a filename, then all you get is the result string printed out, and foo.log has everything else:

 ./nltransfer_example -2 foo.log

Code

Main function

The main function for the example is shown below. Of course, it needs a few other functions from the file to actually run (get_my_guid(), bang_the_drum_slowly(), etc.), but you should be able to get the idea from this.

/**
 * Run the second example
 * Log everything to the same place.
 */
int run2(const char *logfile, int debug_mode /* ignored */ )
{    
    char err_why[1024]; /* for error reporting */
    char *guid;
    NL_summ_T prog_summ, int_summ;
    NL_log_T log;
    const char *stdout_logfile = "-";
    char *result_str;
    
    /* Get a GUID */
    get_my_guid(&guid);
    /* Set log file to stdout if NULL */
    if (!logfile)
        logfile = stdout_logfile;
        
    /* 
        (1) Create NetLogger log instance 
    */
    /* Open log, return on error */
    log = NL_open(logfile);
    if (!log) {
        sprintf(err_why,"opening '%s'", logfile);
        goto error;
    }
    /* Set log to DEBUG level */
    NL_set_level(log, NL_LVL_DEBUG);    

    /* 
        (2) Create whole-program summarizer
    */
    prog_summ = NL_summ();
    /* Make summarizer output go to log, too */
    NL_summ_set_shared_output(prog_summ, log);
    /* Add NL_transfer events to summarizer.
       Set interval to -1 to mean "until NL_finalize()".
     */
    NL_transfer_init(prog_summ, -1,  NL_LVL_DEBUG);
    /* Don't let summarizer 'consume' the events,
     * we want them to "pass through" and get logged
     * in their full detail.
     */
    NL_transfer_set_passthrough(prog_summ);
    /* Add this summarizer to log instance */
    NL_summ_add_log(prog_summ, log);
    
    /*
        (3) Create interval summarizer
            See comments for (2) above.
            Only real difference is interval for NL_transfer_init()
    */
    int_summ = NL_summ();
    NL_summ_set_shared_output(int_summ, log);
    /* 1,000,000 microseconds = 1 second interval */
    NL_transfer_init(int_summ, 1000000,  NL_LVL_DEBUG);
    NL_transfer_set_passthrough(int_summ);
    NL_summ_add_log(int_summ, log);
    
    /*
        (4) Log some activity
     */
    {
        NL_transfer_blockid_t block_id;
        NL_transfer_streamid_t stream_id;
        NL_transfer_op_t op;
        double nbytes;
        
        for (block_id = 0; block_id < 2; block_id++) {
            for (stream_id = 11; stream_id < 13; stream_id++) {
                for (op=NL_TRANSFER_DISK_READ; op < NL_TRANSFER_NOEVENT; op++) {
                    NL_transfer_start(log, NL_LVL_DEBUG, op, 
                                      guid, stream_id, block_id);
                    nbytes = bang_the_drum_slowly();
                    NL_transfer_end(log, NL_LVL_DEBUG, op,
                                    guid, stream_id, block_id, nbytes);
                }
            }
        }
    }
    
    /*
        (5) Finalize handles and get result string.
            Then close everything up
     */
    NL_transfer_finalize(prog_summ);
    NL_summ_del(int_summ); /* flushes it, too! */
    result_str = NL_transfer_get_result_string(prog_summ, guid);
    printf("RESULT STRING:\n%s", result_str);
    NL_summ_del(prog_summ);
    NL_close(log);
    
    return 0;
    
error:
    fprintf(stderr,"Error:%s:%s\n", err_why, NL_err_str());
    return -1;
}


Personal tools