📄 stat.cpp
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Authors : Benjamin GAUTHIER - 24 Mar 2004 * Joseph BANINO * Olivier JACQUES * Richard GAYRAUD * From Hewlett Packard Company. * Wolfgang Beck * */#include <iostream>#include <fstream>#include <iomanip>#include "sipp.hpp"#include "screen.hpp"/*** Local definitions (macros)*//*** Warning! All DISPLAY_ macros must be called where f FILE is** defined. This is to allow printing to stdout or a file. */#define DISPLAY_LINE()\ fprintf(f," ------------------------------------------------------------------------------ \r\n")#define DISPLAY_DLINE()\ fprintf(f,"================================================================================\r\n")#define DISPLAY_CROSS_LINE()\ fprintf(f,"-------------------------+---------------------------+--------------------------\r\n")#define DISPLAY_HEADER()\ fprintf(f," Counter Name | Periodic value | Cumulative value\r\n")#define DISPLAY_TXT_COL(T1, V1, V2)\ fprintf(f," %-22.22s | %-25.25s |", T1, V1); fprintf(f," %-24.24s \r\n", V2)#define DISPLAY_VAL_RATEF_COL(T1, V1, V2)\ fprintf(f," %-22.22s | %8.3f cps | %8.3f cps \r\n", T1, V1, V2)#define DISPLAY_2VAL(T1, V1, V2)\ fprintf(f," %-22.22s | %8d | %8d \r\n", T1, V1, V2)#define DISPLAY_CUMUL(T1, V1)\ fprintf(f," %-22.22s | | %8d \r\n", T1, V1)#define DISPLAY_PERIO(T1, V1)\ fprintf(f," %-22.22s | %8d | \r\n", T1, V1)#define DISPLAY_VALF(T1, V1)\ fprintf(f," %-22.22s | %8.3f ms \r\n", T1, V1)#define DISPLAY_VAL_RATEF(T1, V1)\ fprintf(f," %-22.22s | %8.3f cps \r\n", T1, V1)#define DISPLAY_VAL_RATE(T1, V1)\ fprintf(f," %-22.22s | %8d cps \r\n", T1, V1)#define DISPLAY_VAL(T1, V1)\ fprintf(f," %-22.22s : %8d \r\n", T1, V1)#define DISPLAY_2VALF(T1, V1, T2, V2)\ fprintf(f," %-22.22s : %8.2f | %-7.7s : %8.2f \r\n", T1, V1, T2, V2)#define DISPLAY_3VAL(T1, V1, T2, V2, T3, V3)\ fprintf(f," %-22.22s : %8d | %-7.7s : %8d | %-12.12s : %5d \r\n", T1, V1, T2, V2, T3, V3)#define DISPLAY_3VALF(T1, V1, T2, V2, T3, V3)\ fprintf(f," %-22.22s : %8.3f | %-7.7s : %8.3f | %-12.12s : %5.1f \r\n", T1, V1, T2, V2, T3, V3)#define DISPLAY_TXT(T1, V1)\ fprintf(f," %-22.22s | %-52.52s \r\n", T1, V1)#define DISPLAY_INFO(T1)\ fprintf(f," %-77.77s \r\n", T1)#define DISPLAY_REPART(T1, T2, V1)\ fprintf(f," %8d ms <= n < %8d ms : %10d %-29.29s \r\n", T1, T2, V1, "")#define DISPLAY_LAST_REPART(T1, V1)\ fprintf(f," %14.14s n >= %8d ms : %10d %-29.29s \r\n", "", T1, V1, "")#define RESET_COUNTERS(PT)\ memset (PT, 0, CStat::E_NB_COUNTER * sizeof(unsigned long))#define RESET_PD_COUNTERS(PT) \{ \ int i; \ for (i=CStat::CPT_PD_IncomingCallCreated; \ i<=CStat::CPT_PD_AutoAnswered; \ i++) \ PT[i] = (unsigned long) 0; \}#define RESET_PL_COUNTERS(PT) \{ \ int i; \ for (i=CStat::CPT_PL_IncomingCallCreated; \ i<=CStat::CPT_PL_AutoAnswered; \ i++) \ PT[i] = (unsigned long) 0; \}/* __________________________________________________________________________ C L A S S CS t a t __________________________________________________________________________*/CStat* CStat::instance(){ if ( M_instance == NULL ) M_instance = new CStat(); return M_instance;}void CStat::close (){ if (M_ResponseTimeRepartition != NULL) delete [] M_ResponseTimeRepartition; if (M_CallLengthRepartition != NULL) delete [] M_CallLengthRepartition; if(M_outputStream != NULL) { M_outputStream->close(); delete M_outputStream; } if(M_fileName != NULL) delete [] M_fileName; if(M_outputStreamRtt != NULL) { M_outputStreamRtt->close(); delete M_outputStreamRtt; } if(M_fileNameRtt != NULL) delete [] M_fileNameRtt; if(M_dumpRespTime != NULL) delete [] M_dumpRespTime ; M_SizeOfResponseTimeRepartition = 0; M_SizeOfCallLengthRepartition = 0; M_CallLengthRepartition = NULL; M_fileName = NULL; M_ResponseTimeRepartition = NULL; M_outputStream = NULL; M_outputStreamRtt = NULL; M_fileNameRtt = NULL; M_dumpRespTime = NULL; // On last position if (M_instance != NULL) delete M_instance; M_instance = NULL;}int CStat::init () { // reset of all counter RESET_COUNTERS(M_counters); GET_TIME (&M_startTime); memcpy (&M_pdStartTime, &M_startTime, sizeof (struct timeval)); memcpy (&M_plStartTime, &M_startTime, sizeof (struct timeval)); M_outputStream = NULL; M_headerAlreadyDisplayed = false; M_outputStreamRtt = NULL; M_headerAlreadyDisplayedRtt = false; return(1);}int CStat::isWellFormed(char * P_listeStr, int * nombre){ char * ptr = P_listeStr; int sizeOf; bool isANumber; (*nombre) = 0; sizeOf = strlen(P_listeStr); // getting the number if(sizeOf > 0) { // is the string well formed ? [0-9] [,] isANumber = false; for(int i=0; i<=sizeOf; i++) { switch(ptr[i]) { case ',': if(isANumber == false) { return(0); } else { (*nombre)++; } isANumber = false; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': isANumber = true; break; case '\t': case ' ' : break; case '\0': if(isANumber == false) { return(0); } else { (*nombre)++; } break; default: return(0); } } // enf for } return(1);}int CStat::createIntegerTable(char * P_listeStr, unsigned int ** listeInteger, int * sizeOfList){ int nb=0; char * ptr = P_listeStr; char * ptr_prev = P_listeStr; unsigned int current_int; int sizeOf; bool isANumber; if(isWellFormed(P_listeStr, sizeOfList) == 1) { (*listeInteger) = new unsigned int[(*sizeOfList)]; while((*ptr) != ('\0')) { if((*ptr) == ',') { sscanf(ptr_prev, "%u", ¤t_int); if (nb<(*sizeOfList)) (*listeInteger)[nb] = current_int; nb++; ptr_prev = ptr+1; } ptr++; } // on lit le dernier sscanf(ptr_prev, "%u", ¤t_int); if (nb<(*sizeOfList)) (*listeInteger)[nb] = current_int; nb++; return(1); } return(0);}void CStat::setFileName(char * P_name, char * P_extension){ int sizeOf, sizeOfExtension; if(P_name != NULL) { // +6 for PID sizeOf = strlen(P_name) + 6; if(sizeOf > 0) { if(P_extension != NULL) { sizeOfExtension = strlen(P_extension); if(sizeOfExtension > 0) { if(M_fileName != NULL) delete [] M_fileName; sizeOf += sizeOfExtension; M_fileName = new char[MAX_PATH]; sprintf(M_fileName, "%s_%d_", P_name, getpid()); strcat(M_fileName, P_extension); } else { if(M_fileName != NULL) delete [] M_fileName; sizeOf += strlen(DEFAULT_EXTENSION); M_fileName = new char[MAX_PATH]; sprintf(M_fileName, "%s_%d_", P_name, getpid()); strcat(M_fileName, DEFAULT_EXTENSION); } } else { if(M_fileName != NULL) delete [] M_fileName; sizeOf += strlen(DEFAULT_EXTENSION); M_fileName = new char[MAX_PATH]; sprintf(M_fileName, "%s_%d_", P_name, getpid()); strcat(M_fileName, DEFAULT_EXTENSION); } } else { cerr << "new file name length is null - " << "keeping the default filename : " << DEFAULT_FILE_NAME << endl; } } else { cerr << "new file name is NULL ! - keeping the default filename : " << DEFAULT_FILE_NAME << endl; }}void CStat::setFileName(char * P_name){ int sizeOf, sizeOfExtension; if(P_name != NULL) { sizeOf = strlen(P_name); if(sizeOf > 0) { if(M_fileName != NULL) delete [] M_fileName; M_fileName = new char[sizeOf+1]; strcpy(M_fileName, P_name); } else { cerr << "new file name length is null - " "keeping the default filename : " << DEFAULT_FILE_NAME << endl; } } else { cerr << "new file name is NULL ! - keeping the default filename : " << DEFAULT_FILE_NAME << endl; }}void CStat::initRtt(char * P_name, char * P_extension, unsigned long P_report_freq_dumpRtt) { int sizeOf, sizeOfExtension, L_i; if(P_name != NULL) { sizeOf = strlen(P_name) ; if(sizeOf > 0) { // 4 for '_rtt' and 6 for pid sizeOf += 10 ; sizeOfExtension = strlen(P_extension); if(M_fileNameRtt != NULL) delete [] M_fileNameRtt; sizeOf += sizeOfExtension; M_fileNameRtt = new char[sizeOf+1]; sprintf (M_fileNameRtt, "%s_%d_rtt%s", P_name, getpid(),P_extension); } else { cerr << "new file name length is null - " << "keeping the default filename : " << DEFAULT_FILE_NAME << endl; } } else { cerr << "new file name is NULL ! - keeping the default filename : " << DEFAULT_FILE_NAME << endl; } // calculate M_time_ref M_time_ref = (double)M_startTime.tv_sec*1000.0 + (double)M_startTime.tv_usec/1000.0 ; // initiate the table dump response time M_report_freq_dumpRtt = P_report_freq_dumpRtt ; M_dumpRespTime = new T_value_rtt [P_report_freq_dumpRtt] ; if ( M_dumpRespTime == NULL ) { cerr << "Memory allocation failure" << endl; exit(EXIT_FATAL_ERROR); } for (L_i = 0 ; L_i < P_report_freq_dumpRtt; L_i ++) { M_dumpRespTime[L_i].date = 0.0; M_dumpRespTime[L_i].rtt = 0.0; }}void CStat::setRepartitionCallLength(char * P_listeStr){ unsigned int * listeInteger; int sizeOfListe; if(createIntegerTable(P_listeStr, &listeInteger, &sizeOfListe) == 1) { initRepartition(listeInteger, sizeOfListe, &M_CallLengthRepartition, &M_SizeOfCallLengthRepartition); } else { M_CallLengthRepartition = NULL; M_SizeOfCallLengthRepartition = 0; } delete [] listeInteger; listeInteger = NULL;}void CStat::setRepartitionResponseTime (char * P_listeStr){ unsigned int * listeInteger; int sizeOfListe; if(createIntegerTable(P_listeStr, &listeInteger, &sizeOfListe) == 1) { initRepartition(listeInteger, sizeOfListe, &M_ResponseTimeRepartition, &M_SizeOfResponseTimeRepartition); } else { M_CallLengthRepartition = NULL; M_SizeOfCallLengthRepartition = 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -