📄 slog_pstat.c
字号:
ierr = fbuf_deposit( &( slog->pstat->Nbin ), SLOG_typesz[ ui32 ], 1, tmp_fbuf ); if ( ierr < 1 ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Write() - Cannot " "deposit the Nbin to the SLOG filebuffer, " fmt_ui32" bytes written so far\n", Nbytes_written ); fflush( errfile ); return SLOG_FAIL; } Nbytes_written += ierr * SLOG_typesz[ ui32 ]; ierr = fbuf_deposit( &( slog->pstat->starttime ), SLOG_typesz[ ts ], 1, tmp_fbuf ); if ( ierr < 1 ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Write() - Cannot " "deposit the StartTime to the SLOG filebuffer, " fmt_ui32" bytes written so far\n", Nbytes_written ); fflush( errfile ); return SLOG_FAIL; } Nbytes_written += ierr * SLOG_typesz[ ts ]; ierr = fbuf_deposit( &( slog->pstat->endtime ), SLOG_typesz[ ts ], 1, tmp_fbuf ); if ( ierr < 1 ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Write() - Cannot " "deposit the EndTime to the SLOG filebuffer, " fmt_ui32" bytes written so far\n", Nbytes_written ); fflush( errfile ); return SLOG_FAIL; } Nbytes_written += ierr * SLOG_typesz[ ts ]; for ( idx = 0; idx < (int)slog->pstat->Nset; idx++ ) { ierr = SLOG_StatSet_DepositToFbuf( slog->pstat->sets[ idx ], tmp_fbuf ); if ( ierr == SLOG_FAIL ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Write() - Cannot " "deposit %d-th StatSet to the SLOG filebuffer, " fmt_ui32" bytes written so far\n", idx, Nbytes_written ); fflush( errfile ); return SLOG_FAIL; } Nbytes_written += ierr; } /* File Buffer usage test */ if ( Nbytes_written != expected_bsize ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Write() - " "Nbytes written("fmt_ui32") != expected_bsize " "written("fmt_ui32")\n", Nbytes_written, expected_bsize ); fflush( errfile ); return SLOG_FAIL; } /* Empty the content of tmp_fbuf to the disk at the specified location */ ierr = slog_fseek( slog->fd, slog->hdr->fptr2statistics, SEEK_SET ); if ( ierr != 0 ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Write() - " "slog_fseek( slog->hdr->fptr2statistics ) fails\n" ); fflush( errfile ); return SLOG_FAIL; } fbuf_empty( tmp_fbuf ); fflush( slog->fd ); fbuf_free( tmp_fbuf ); return SLOG_SUCCESS;}int SLOG_PSTAT_Read( SLOG_STREAM *slog ){ SLOG_fptr file_loc_saved; filebuf_t *tmp_fbuf; SLOG_uint32 expected_bsize; SLOG_uint32 Nbytes_read; SLOG_uint32 pstat_Nset; SLOG_uint32 pstat_Nbin; int ierr; int idx; if ( slog == NULL ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - " "the input SLOG_STREAM is NULL\n" ); fflush( errfile ); return SLOG_FAIL; } if ( slog->hdr == NULL ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - " "the input SLOG_hdr_t is NULL\n" ); fflush( errfile ); return SLOG_FAIL; } if ( slog->hdr->fptr2statistics == SLOG_fptr_NULL ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - \n" "\t""the SLOG_STREAM's fptr2statistics points at " "SLOG_fptr_NULL\n" ); fflush( errfile ); return SLOG_FAIL; } /* Save the current file pointer position */ file_loc_saved = slog_ftell( slog->fd ); /* Locate where to read the Preview Statistics Array */ ierr = slog_fseek( slog->fd, slog->hdr->fptr2statistics, SEEK_SET ); if ( ierr != 0 ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - " "slog_fseek( slog->hdr->fptr2statistics ) fails\n" ); fflush( errfile ); return SLOG_FAIL; } /* Read in the sizes of the Preview Statistics Arrays */ ierr = bswp_fread( &( pstat_Nset ), sizeof( SLOG_uint32 ), 1, slog->fd ); if ( ierr < 1 ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - \n" "\t""Fails at reading the total number of sets " "of the Preview Statistics Arrays\n" ); fflush( errfile ); return SLOG_FAIL; } ierr = bswp_fread( &( pstat_Nbin ), sizeof( SLOG_uint32 ), 1, slog->fd ); if ( ierr < 1 ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - \n" "\t""Fails at reading the bin size " "of each Preview Statistics Array\n" ); fflush( errfile ); return SLOG_FAIL; } if ( pstat_Nset == 0 || pstat_Nbin == 0 ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - Warning !!! \n" "\t""Invalid sizes of the Preview Statistics Arrays, " "Nset = "fmt_ui32", Nbin = "fmt_ui32"\n", pstat_Nset, pstat_Nbin ); fprintf( errfile, "\t""This may be casued by incompleted statistics " "collection process during the 2nd pass." ); fflush( errfile ); return SLOG_SUCCESS; } /* Compute the expected size of the file buffer */ expected_bsize = SLOG_typesz[ ts ] * 2 + SLOG_PSTAT_SizeOfStatSetInFile( pstat_Nbin ) * pstat_Nset; /* Create and Initialize the file buffer */ tmp_fbuf = ( filebuf_t * ) fbuf_create( expected_bsize ); if ( tmp_fbuf == NULL ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - " "fbuf_create() fails!\n" ); fflush( errfile ); return SLOG_FAIL; } fbuf_filedesc( tmp_fbuf, slog->fd ); /* Fill up the file buffer from the disk */ if ( fbuf_refill( tmp_fbuf ) == EOF ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - \n" "\t""fbuf_refill( tmp_fbuf ) fails\n" ); fflush( errfile ); return SLOG_EOF; } /* Create the SLOG_pstat_t data structure in SLOG_STREAM if either pstat_Nset or pstat_Nbin is zero, or both are zero, slog->pstat will remain unchanged, i.e. NULL */ slog->pstat = SLOG_PSTAT_Create( pstat_Nbin ); if ( slog->pstat == NULL ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - \n" "\t""SLOG_PSTAT_Create( Nbin="fmt_ui32" ) fails\n", pstat_Nbin ); fflush( errfile ); return SLOG_FAIL; } slog->pstat = SLOG_PSTAT_CreateContent( slog->pstat, pstat_Nset ); if ( slog->pstat == NULL ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - \n" "\t""SLOG_PSTAT_CreateContent( Nset="fmt_ui32" ) " "fails\n", pstat_Nset ); fflush( errfile ); return SLOG_FAIL; } /* Withdraw SLOG_pstat_t from the tmp_fbuf */ Nbytes_read = 0; ierr = fbuf_withdraw( &( slog->pstat->starttime ), SLOG_typesz[ ts ], 1, tmp_fbuf ); if ( ierr < 1 ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - Cannot " "withdraw the StartTime from the SLOG filebuffer, " fmt_ui32" bytes read so far\n", Nbytes_read ); fflush( errfile ); return SLOG_FAIL; } Nbytes_read += ierr * SLOG_typesz[ ts ]; ierr = fbuf_withdraw( &( slog->pstat->endtime ), SLOG_typesz[ ts ], 1, tmp_fbuf ); if ( ierr < 1 ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - Cannot " "withdraw the EndTime from the SLOG filebuffer, " fmt_ui32" bytes read so far\n", Nbytes_read ); fflush( errfile ); return SLOG_FAIL; } Nbytes_read += ierr * SLOG_typesz[ ts ]; for ( idx = 0; idx < (int)slog->pstat->Nset; idx++ ) { ierr = SLOG_StatSet_WithdrawFromFbuf( slog->pstat->sets[ idx ], tmp_fbuf ); if ( ierr == SLOG_FAIL ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - Cannot " "withdraw %d-th StatSet to the SLOG filebuffer, " fmt_ui32" bytes read so far\n", idx, Nbytes_read ); fflush( errfile ); return SLOG_FAIL; } Nbytes_read += ierr; } /* File Buffer usage test */ if ( Nbytes_read != expected_bsize ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - \n" "\t""Nbytes read("fmt_ui32") != expected_bsize " "read("fmt_ui32")\n", Nbytes_read, expected_bsize ); fflush( errfile ); return SLOG_FAIL; } /* Restore the saved file pointer position */ ierr = slog_fseek( slog->fd, file_loc_saved, SEEK_SET ); if ( ierr != 0 ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Read() - slog_fseek() " "fails, cannot restore the orginal file position " "before the end of the subroutine call\n" ); fflush( errfile ); return SLOG_FAIL; } fbuf_free( tmp_fbuf ); return SLOG_SUCCESS;}int SLOG_PSTAT_SetBinWidthInTime( SLOG_STREAM *slog ){ SLOG_pstat_t *pstat; SLOG_dir_t *dir; if ( slog == NULL ) { fprintf( errfile, __FILE__":SLOG_PSTAT_SetBinWidthInTime() - " "the input SLOG_STREAM is NULL\n" ); fflush( errfile ); return SLOG_FAIL; } if ( slog->pstat == NULL ) { fprintf( errfile, __FILE__":SLOG_PSTAT_SetBinWidthInTime() - " "the input SLOG_pstat_t is NULL\n" ); fflush( errfile ); return SLOG_FAIL; } if ( ( slog->frame_dir ).Nframe > 0 && ( slog->frame_dir ).entries == NULL ) { fprintf( errfile, __FILE__":SLOG_PSTAT_SetBinWidthInTime() - " "the input SLOG_dir_t is inconsistent!\n" ); fflush( errfile ); return SLOG_FAIL; } if ( ( slog->frame_dir ).Nframe < 1 ) return SLOG_SUCCESS; dir = &( slog->frame_dir ); pstat = slog->pstat; pstat->starttime = ( dir->entries[ 0 ] ).starttime; pstat->endtime = ( dir->entries[ dir->Nframe-1 ] ).endtime; if ( pstat->Nbin > 0 ) pstat->bin_width_in_time = ( pstat->endtime - pstat->starttime ) / pstat->Nbin; else pstat->bin_width_in_time = ( pstat->endtime - pstat->starttime ); return SLOG_SUCCESS;}static double div_quod_int( double numerator, double denominator, int *quodient ){ double fval; int iquodient; double remainder; fval = numerator / denominator; iquodient = ( int ) fval; remainder = fval - ( double ) iquodient; *quodient = iquodient; return remainder;}static double SLOG_PSTAT_div( double numerator, double denominator, int upperbound, int *quodient ){ int iquodient; double remainder; remainder = div_quod_int( numerator, denominator, &iquodient ); if ( iquodient < 0 || iquodient > upperbound) {#if defined( DEBUG ) fprintf( errfile, __FILE__ ":SLOG_PSTAT_div() - Warning!! \n" "\t""the computed integral quodient is " "out of range\n" "\t""Original : iquodient = %d, remainder = %f, " "upperbound = %d\n", iquodient, remainder, upperbound ); fflush( errfile );#endif if ( iquodient < 0 ) { remainder += iquodient; iquodient = 0; } else { remainder += iquodient - upperbound; iquodient = upperbound; }#if defined( DEBUG ) fprintf( errfile, "\t""Corrected : iquodient = %d, remainder = %f, " "upperbound = %d\n", iquodient, remainder, upperbound ); fflush( errfile );#endif } /* Endof if ( iquodient < 0 || iquodient > upperbound ) */ *quodient = iquodient; return remainder;}int SLOG_PSTAT_Update( SLOG_pstat_t *pstat, const SLOG_intvlrec_blist_t *src_bbuf ){ const SLOG_intvlrec_lptr_t *lptr; const SLOG_intvlrec_t *irec; SLOG_statset_t *statset; SLOG_time bin_width_in_time; SLOG_time file_starttime; SLOG_uint32 last_bin_idx; SLOG_int32 set_idx; SLOG_int32 bin_idx; SLOG_time adj_irec_starttime, adj_irec_endtime; SLOG_int32 beg_idx, end_idx; SLOG_statbintype beg_rem, end_rem; if ( pstat == NULL ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Update() - " "the input SLOG_pstat_t pointer is NULL\n" ); fflush( errfile ); return SLOG_FAIL; } bin_width_in_time = pstat->bin_width_in_time; file_starttime = pstat->starttime; last_bin_idx = pstat->Nbin - 1; if ( bin_width_in_time <= 0.0 ) { fprintf( errfile, __FILE__":SLOG_PSTAT_Update() - \n" "\t""the input SLOG_pstat_t's bin_width_in_time = " fmt_time"\n", bin_width_in_time ); fflush( errfile ); return SLOG_FAIL; } for ( lptr = src_bbuf->lhead; lptr != NULL; lptr = lptr->next ) { irec = lptr->irec;#if defined( NO_ARROW_STAT ) /* Exclude the Off-diagonal record, i.e. arrow, when counting the event for statistical preview */ if ( ! SLOG_global_IsOffDiagRec( irec->rectype ) ) {#endif set_idx = SLOG_PSTAT_GetSeqIdx( pstat, irec->intvltype ); if ( set_idx != SLOG_FAIL ) { statset = pstat->sets[ set_idx ]; adj_irec_starttime = irec->starttime - file_starttime; adj_irec_endtime = adj_irec_starttime + irec->duration; beg_rem = SLOG_PSTAT_div( adj_irec_starttime, bin_width_in_time, last_bin_idx, &beg_idx ); end_rem = SLOG_PSTAT_div( adj_irec_endtime, bin_width_in_time, last_bin_idx, &end_idx ); if ( end_idx > beg_idx ) { statset->bins[ beg_idx ] += 1.0 - beg_rem; for ( bin_idx = beg_idx + 1; bin_idx < end_idx; bin_idx++ ) statset->bins[ bin_idx ] += 1.0; statset->bins[ end_idx ] += end_rem; } else if ( end_idx == beg_idx ) statset->bins[ beg_idx ] += end_rem - beg_rem;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -