added missign files

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-MutexOptim@4596 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2011-09-08 19:28:53 +00:00
parent 2bedbcea77
commit cb155221a9
2 changed files with 168 additions and 0 deletions

View File

@ -0,0 +1,79 @@
#include <iostream>
#include <list>
#include <math.h>
#include <serialiser/rsserial.h>
#include "pqiqos.h"
pqiQoS::pqiQoS(uint32_t nb_levels,float alpha)
: _item_queues(nb_levels),_alpha(alpha)
{
assert(pow(alpha,nb_levels) < 1e+20) ;
float c = 1.0f ;
float inc = alpha ;
_nb_items = 0 ;
for(int i=((int)nb_levels)-1;i>=0;--i,c *= alpha)
{
_item_queues[i]._threshold = c ;
_item_queues[i]._counter = 0 ;
_item_queues[i]._inc = inc ;
}
}
void pqiQoS::print() const
{
std::cerr << "pqiQoS: " << _item_queues.size() << " levels, alpha=" << _alpha ;
std::cerr << " Size = " << _nb_items ;
std::cerr << " Queues: " ;
for(uint32_t i=0;i<_item_queues.size();++i)
std::cerr << _item_queues[i]._items.size() << " " ;
std::cerr << std::endl;
}
void pqiQoS::in_rsItem(RsItem *item)
{
if(item->priority_level() >= _item_queues.size())
{
std::cerr << "pqiQoS::in_rsRawItem() ****Warning****: priority " << item->priority_level() << " out of scope [0," << _item_queues.size()-1 << "]. Priority will be clamped to maximum value." << std::endl;
item->setPriorityLevel(_item_queues.size()-1) ;
}
_item_queues[item->priority_level()].push(item) ;
++_nb_items ;
}
RsItem *pqiQoS::out_rsItem()
{
// Go through the queues. Increment counters.
if(_nb_items == 0)
return NULL ;
float inc = 1.0f ;
int i = _item_queues.size()-1 ;
while(i > 0 && _item_queues[i]._items.empty())
--i, inc = _item_queues[i]._inc ;
int last = i ;
for(int j=i;j>=0;--j)
if( (!_item_queues[j]._items.empty()) && ((_item_queues[j]._counter += inc) >= _item_queues[j]._threshold ))
{
last = j ;
_item_queues[j]._counter -= _item_queues[j]._threshold ;
}
if(last >= 0)
{
assert(_nb_items > 0) ;
--_nb_items ;
return _item_queues[last].pop();
}
else
return NULL ;
}

View File

@ -0,0 +1,89 @@
/*
* libretroshare/src/pqi: pqiqos.h
*
* 3P/PQI network interface for RetroShare.
*
* Copyright 2004-2008 by Cyril Soler
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "csoler@users.sourceforge.net"
*
*/
// This class handles the prioritisation of RsItem, based on the
// priority level. The QoS algorithm must ensure that:
//
// - lower priority items get out with lower rate than high priority items
// - items of equal priority get out of the queue in the same order than they got in
// - items of level n+1 are output \alpha times more often than items of level n.
// \alpha is a constant that is not necessarily an integer, but strictly > 1.
// - the set of possible priority levels is finite, and pre-determined.
//
#include <vector>
#include <list>
class RsItem ;
class pqiQoS
{
public:
pqiQoS(uint32_t max_levels,float alpha) ;
class ItemQueue
{
public:
RsItem *pop()
{
if(_items.empty())
return NULL ;
RsItem *item = _items.front() ;
_items.pop_front() ;
return item ;
}
void push(RsItem *item)
{
_items.push_back(item) ;
}
float _threshold ;
float _counter ;
float _inc ;
std::list<RsItem*> _items ;
};
// This function pops items from the queue, y order of priority
//
RsItem *out_rsItem() ;
// This function is used to queue items.
//
void in_rsItem(RsItem *item) ;
void print() const ;
uint64_t qos_queue_size() const { return _nb_items ; }
private:
// This vector stores the lists of items with equal priorities.
//
std::vector<ItemQueue> _item_queues ;
float _alpha ;
uint64_t _nb_items ;
};