00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 const char *XrdMonCtrBufferCVSID = "$Id: XrdMonCtrBuffer.cc 30949 2009-11-02 16:37:58Z ganis $";
00014
00015 #include "XrdMon/XrdMonCtrBuffer.hh"
00016 #include "XrdMon/XrdMonCtrDebug.hh"
00017 #include "XrdSys/XrdSysHeaders.hh"
00018
00019 #include <iomanip>
00020 using std::cout;
00021 using std::endl;
00022 using std::setw;
00023
00024 XrdMonCtrBuffer* XrdMonCtrBuffer::_instance = 0;
00025
00026 XrdMonCtrBuffer::XrdMonCtrBuffer()
00027 : _head(0),
00028 _tail(0),
00029 _noElems(0),
00030 _max(0),
00031 _aver(0),
00032 _noKInAver(0),
00033 _last1Kmax(0),
00034 _last1Ktotal(0),
00035 _counter1K(1000)
00036 {}
00037
00038 XrdMonCtrBuffer*
00039 XrdMonCtrBuffer::instance() {
00040 if ( 0 == _instance ) {
00041 _instance = new XrdMonCtrBuffer();
00042 }
00043 return _instance;
00044 }
00045
00046 void
00047 XrdMonCtrBuffer::push_back(XrdMonCtrPacket* p) {
00048 XrdSysMutexHelper mh;
00049 mh.Lock(&_mutex);
00050 if ( 0 == _head ) {
00051 _tail = _head = new Elem(p);
00052 } else {
00053 _tail->next = new Elem(p);
00054 _tail = _tail->next;
00055 }
00056
00057 ++_noElems;
00058 _cond.Signal();
00059 }
00060
00061 XrdMonCtrPacket*
00062 XrdMonCtrBuffer::pop_front() {
00063
00064 while ( 0 == _noElems ) {
00065 _cond.Wait(3600);
00066 if ( 0 != _head ) {
00067 break;
00068 }
00069 }
00070
00071 collectStats();
00072
00073 XrdMonCtrPacket* p = 0;
00074 Elem* e = 0;
00075 {
00076 XrdSysMutexHelper mh;
00077 mh.Lock(&_mutex);
00078 p = _head->packet;
00079 e = _head;
00080 if ( _head == _tail ) {
00081 _head = _tail = _head->next;
00082 } else {
00083 _head = _head->next;
00084 }
00085 --_noElems;
00086 }
00087 delete e;
00088 return p;
00089 }
00090
00091 void
00092 XrdMonCtrBuffer::printList(const char* txt)
00093 {
00094 XrdSysMutexHelper mh;
00095 mh.Lock(&XrdMonCtrDebug::_mutex);
00096 cout << txt << " #" << _noElems << " h" << (int *) _head << " t" << (int *) _tail << " ";
00097 Elem* e = _head;
00098 while ( e ) {
00099 cout << e << ":{" << (int *) e->packet << ", ->" << (int *) e->next << "} ";
00100 e = e->next;
00101 }
00102 cout << endl;
00103 }
00104
00105 void
00106 XrdMonCtrBuffer::collectStats()
00107 {
00108 _last1Ktotal += _noElems;
00109 if ( _noElems > _last1Kmax ) {
00110 _last1Kmax = _noElems;
00111 }
00112 if ( --_counter1K == 0 ) {
00113 int last1Kaver = _last1Ktotal/1000;
00114 if ( _max < _last1Kmax ) {
00115 _max = _last1Kmax;
00116 }
00117 _aver = (_aver*_noKInAver + last1Kaver) / (_noKInAver+1);
00118 ++_noKInAver;
00119 {
00120 XrdSysMutexHelper mh;
00121 mh.Lock(&XrdMonCtrDebug::_mutex);
00122 cout << "Packet buffer stats: 1K: max " << setw(3) << _last1Kmax
00123 << ", aver " << setw(1) << last1Kaver
00124 << ". Overall: max " << setw(3) << _max
00125 << ", aver " << setw(1) << _aver
00126 << ", noCalls " << setw(3) << _noKInAver
00127 << "K" << endl;
00128 }
00129 _last1Kmax = 0;
00130 _last1Ktotal = 0;
00131 _counter1K = 1000;
00132 }
00133 }