"Fossies" - the Fresh Open Source Software Archive

Member "icinga2-2.10.5/lib/base/ringbuffer.cpp" (23 May 2019, 3351 Bytes) of package /linux/misc/icinga2-2.10.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ringbuffer.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.10.0_vs_2.10.1.

    1 /******************************************************************************
    2  * Icinga 2                                                                   *
    3  * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
    4  *                                                                            *
    5  * This program is free software; you can redistribute it and/or              *
    6  * modify it under the terms of the GNU General Public License                *
    7  * as published by the Free Software Foundation; either version 2             *
    8  * of the License, or (at your option) any later version.                     *
    9  *                                                                            *
   10  * This program is distributed in the hope that it will be useful,            *
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
   13  * GNU General Public License for more details.                               *
   14  *                                                                            *
   15  * You should have received a copy of the GNU General Public License          *
   16  * along with this program; if not, write to the Free Software Foundation     *
   17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
   18  ******************************************************************************/
   19 
   20 #include "base/ringbuffer.hpp"
   21 #include "base/objectlock.hpp"
   22 #include "base/utility.hpp"
   23 #include <algorithm>
   24 
   25 using namespace icinga;
   26 
   27 RingBuffer::RingBuffer(RingBuffer::SizeType slots)
   28     : m_Slots(slots, 0), m_TimeValue(0), m_InsertedValues(0)
   29 { }
   30 
   31 RingBuffer::SizeType RingBuffer::GetLength() const
   32 {
   33     boost::mutex::scoped_lock lock(m_Mutex);
   34     return m_Slots.size();
   35 }
   36 
   37 void RingBuffer::InsertValue(RingBuffer::SizeType tv, int num)
   38 {
   39     boost::mutex::scoped_lock lock(m_Mutex);
   40 
   41     InsertValueUnlocked(tv, num);
   42 }
   43 
   44 void RingBuffer::InsertValueUnlocked(RingBuffer::SizeType tv, int num)
   45 {
   46     RingBuffer::SizeType offsetTarget = tv % m_Slots.size();
   47 
   48     if (m_TimeValue == 0)
   49         m_InsertedValues = 1;
   50 
   51     if (tv > m_TimeValue) {
   52         RingBuffer::SizeType offset = m_TimeValue % m_Slots.size();
   53 
   54         /* walk towards the target offset, resetting slots to 0 */
   55         while (offset != offsetTarget) {
   56             offset++;
   57 
   58             if (offset >= m_Slots.size())
   59                 offset = 0;
   60 
   61             m_Slots[offset] = 0;
   62 
   63             if (m_TimeValue != 0 && m_InsertedValues < m_Slots.size())
   64                 m_InsertedValues++;
   65         }
   66 
   67         m_TimeValue = tv;
   68     }
   69 
   70     m_Slots[offsetTarget] += num;
   71 }
   72 
   73 int RingBuffer::UpdateAndGetValues(RingBuffer::SizeType tv, RingBuffer::SizeType span)
   74 {
   75     boost::mutex::scoped_lock lock(m_Mutex);
   76 
   77     return UpdateAndGetValuesUnlocked(tv, span);
   78 }
   79 
   80 int RingBuffer::UpdateAndGetValuesUnlocked(RingBuffer::SizeType tv, RingBuffer::SizeType span)
   81 {
   82     InsertValueUnlocked(tv, 0);
   83 
   84     if (span > m_Slots.size())
   85         span = m_Slots.size();
   86 
   87     int off = m_TimeValue % m_Slots.size();
   88     int sum = 0;
   89     while (span > 0) {
   90         sum += m_Slots[off];
   91 
   92         if (off == 0)
   93             off = m_Slots.size();
   94 
   95         off--;
   96         span--;
   97     }
   98 
   99     return sum;
  100 }
  101 
  102 double RingBuffer::CalculateRate(RingBuffer::SizeType tv, RingBuffer::SizeType span)
  103 {
  104     boost::mutex::scoped_lock lock(m_Mutex);
  105 
  106     int sum = UpdateAndGetValuesUnlocked(tv, span);
  107     return sum / static_cast<double>(std::min(span, m_InsertedValues));
  108 }