Buffer.h

1 #ifndef __NOXIMBUFFER_H__
2 #define __NOXIMBUFFER_H__
3 
4 #include <cassert>
5 #include <queue>
6 #include "DataStructs.h"
7 using namespace std;
class Buffer {

.................

};

#endif

  public:

1     Buffer(); //构造函数
2 
3     virtual ~ Buffer() {//虚析构函数解决因delete基类指针导致的资源泄漏
4     } 
 1 Buffer::Buffer()
 2 {
 3   SetMaxBufferSize(GlobalParams::buffer_depth);
 4   max_occupancy = 0;
 5   hold_time = 0.0;
 6   last_event = 0.0;
 7   hold_time_sum = 0.0;
 8   previous_occupancy = 0;
 9   mean_occupancy = 0.0;
10   true_buffer = true;
11   full_cycles_counter = 0;
12   last_front_flit_seq = NOT_VALID;
13   deadlock_detected = false;
14 }
Buffer()
 1     void SetMaxBufferSize(const unsigned int bms);    // Set buffer max size (in flits)
 2 
 3     unsigned int GetMaxBufferSize() const;    // Get max buffer size
 4 
 5     unsigned int getCurrentFreeSlots() const;    // free buffer slots
 6 
 7     bool IsFull() const;    // Returns true if buffer is full
 8 
 9     bool IsEmpty() const;    // Returns true if buffer is empty
10 
11     virtual void Drop(const Flit & flit) const;    // Called by Push() when buffer is full
12 
13     virtual void Empty() const;    // Called by Pop() when buffer is empty
14 
15     void Push(const Flit & flit);    // Push a flit. Calls Drop method if buffer is full
16 
17     Flit Pop();        // Pop a flit
18 
19     Flit Front() const;    // Return a copy of the first flit in the buffer
20 
21     unsigned int Size() const;
22 
23     void ShowStats(std::ostream & out);
24 
25     void Disable();
26 
27     void Print();
28     
29     bool deadlockFree();
30     void deadlockCheck();
31 
32     void setLabel(string);
33     string getLabel() const;
1 void Buffer::SetMaxBufferSize(const unsigned int bms)
2 {
3   assert(bms > 0);
4 
5   max_buffer_size = bms;
6 }
SetMaxBufferSize(const unsigned int bms)
1 unsigned int Buffer::GetMaxBufferSize() const
2 {
3   return max_buffer_size;
4 }
GetMaxBufferSize() const
1 unsigned int Buffer::getCurrentFreeSlots() const
2 {
3   return (GetMaxBufferSize() - Size());
4 }
getCurrentFreeSlots() const
1 unsigned int Buffer::Size() const
2 {
3   return buffer.size();
4 }
Size() const
 1 bool Buffer::IsFull() const
 2 {
 3   return buffer.size() == max_buffer_size;
 4 }
 5 
 6 bool Buffer::IsEmpty() const
 7 {
 8   return buffer.size() == 0;
 9 }
10 
11 void Buffer::Drop(const Flit & flit) const
12 {
13   assert(false);
14 }
15 
16 void Buffer::Empty() const
17 {
18   assert(false);
19 }
View Code
 1 void Buffer::Push(const Flit & flit)
 2 {
 3     SaveOccupancyAndTime();
 4 
 5     if (IsFull())
 6         Drop(flit);
 7     else
 8         buffer.push(flit);
 9 
10     UpdateMeanOccupancy();
11 
12     if (max_occupancy < buffer.size())
13         max_occupancy = buffer.size();
14 }
Push(const Flit & flit)
 1 Flit Buffer::Pop()
 2 {
 3   Flit f;
 4 
 5   SaveOccupancyAndTime();
 6 
 7   if (IsEmpty())
 8     Empty();
 9   else {
10     f = buffer.front();
11     buffer.pop();
12   }
13 
14   UpdateMeanOccupancy();
15 
16   return f;
17 }
Pop()
 1 Flit Buffer::Front() const
 2 {
 3   Flit f;
 4 
 5   if (IsEmpty())
 6     Empty();
 7   else
 8     f = buffer.front();
 9 
10   return f;
11 }
Front() const
1 void Buffer::SaveOccupancyAndTime()
2 {
3   previous_occupancy = buffer.size();
4   hold_time = (sc_time_stamp().to_double() / GlobalParams::clock_period_ps) - last_event;
5   last_event = sc_time_stamp().to_double() / GlobalParams::clock_period_ps;
6 }
SaveOccupancyAndTime()
1 void Buffer::UpdateMeanOccupancy()
2 {
3   double current_time = sc_time_stamp().to_double() / GlobalParams::clock_period_ps;
4   if (current_time - GlobalParams::reset_time < GlobalParams::stats_warm_up_time)
5     return;
6 
7   mean_occupancy = mean_occupancy * (hold_time_sum/(hold_time_sum+hold_time)) + (1.0/(hold_time_sum+hold_time)) * hold_time * buffer.size();
8   hold_time_sum += hold_time;
9 }
UpdateMeanOccupancy()
1 void Buffer::ShowStats(std::ostream & out)
2 {
3   if (true_buffer)
4     out << "\t" << mean_occupancy << "\t" << max_occupancy;
5   else
6     out << "\t\t";
7 }
ShowStats(std::ostream & out)

  private:

 1     bool true_buffer;
 2     bool deadlock_detected;
 3 
 4     int full_cycles_counter;
 5     int last_front_flit_seq;
 6 
 7     string label;
 8 
 9     unsigned int max_buffer_size;
10 
11     queue < Flit > buffer;
12 
13     unsigned int max_occupancy;
14     double hold_time, last_event, hold_time_sum;
15     double mean_occupancy;
16     int    previous_occupancy;
17     
18     void SaveOccupancyAndTime();
19     void UpdateMeanOccupancy();

猜你喜欢

转载自www.cnblogs.com/cpsmile/p/9721386.html