<commit_before>// $Id$

/*
Copyright (c) 2007-2009, Trustees of The Leland Stanford Junior University
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this 
list of conditions and the following disclaimer in the documentation and/or 
other materials provided with the distribution.
Neither the name of the Stanford University nor the names of its contributors 
may be used to endorse or promote products derived from this software without 
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*buffer_state.cpp
 *
 * This class is the buffere state of the next router down the channel
 * tracks the credit and how much of the buffer is in use 
 */

#include <iostream>
#include <cstdlib>
#include <cassert>

#include "booksim.hpp"
#include "buffer_state.hpp"
#include "random_utils.hpp"

BufferState::BufferState( const Configuration& config, 
			  Module *parent, const string& name ) : 
Module( parent, name ), _shared_occupied(0)
{
  _vc_buf_size     = config.GetInt( "vc_buf_size" );
  _shared_buf_size = config.GetInt( "shared_buf_size" );
  _vcs          = config.GetInt( "num_vcs" );
  
  _wait_for_tail_credit = config.GetInt( "wait_for_tail_credit" );
  _vc_busy_when_full = config.GetInt( "vc_busy_when_full" );

  _in_use.resize(_vcs, false);
  _tail_sent.resize(_vcs, false);
  _cur_occupied.resize(_vcs, 0);

  /* each flit is given a type and these types can only exists in 
   * specific virtual channels
   */
  _vc_range_begin.resize(Flit::NUM_FLIT_TYPES);
  _vc_sel_last.resize(Flit::NUM_FLIT_TYPES);
  _vc_range_size.resize(Flit::NUM_FLIT_TYPES);

  _vc_range_begin[Flit::READ_REQUEST] 
    = config.GetInt( "read_request_begin_vc" );
  _vc_sel_last[Flit::READ_REQUEST]
    = config.GetInt( "read_request_end_vc" );
  _vc_range_size[Flit::READ_REQUEST] 
    = _vc_sel_last[Flit::READ_REQUEST] - _vc_range_begin[Flit::READ_REQUEST] + 1;

  _vc_range_begin[Flit::WRITE_REQUEST] 
    = config.GetInt( "write_request_begin_vc" );
  _vc_sel_last[Flit::WRITE_REQUEST]
    = config.GetInt( "write_request_end_vc" );
  _vc_range_size[Flit::WRITE_REQUEST] 
    = _vc_sel_last[Flit::WRITE_REQUEST] - _vc_range_begin[Flit::WRITE_REQUEST] + 1;

  _vc_range_begin[Flit::READ_REPLY] 
    = config.GetInt( "read_reply_begin_vc" );
  _vc_sel_last[Flit::READ_REPLY]
    = config.GetInt( "read_reply_end_vc" );
  _vc_range_size[Flit::READ_REPLY] 
    = _vc_sel_last[Flit::READ_REPLY] - _vc_range_begin[Flit::READ_REPLY] + 1;

  _vc_range_begin[Flit::WRITE_REPLY] 
    = config.GetInt( "write_reply_begin_vc" );
  _vc_sel_last[Flit::WRITE_REPLY]
    = config.GetInt( "write_reply_end_vc" );
  _vc_range_size[Flit::WRITE_REPLY] 
    = _vc_sel_last[Flit::WRITE_REPLY] - _vc_range_begin[Flit::WRITE_REPLY] + 1;

  _vc_range_begin[Flit::ANY_TYPE] = 0 ;
  _vc_range_size[Flit::ANY_TYPE]   = _vcs ;
  _vc_sel_last[Flit::ANY_TYPE] = _vcs - 1 ;
}

void BufferState::ProcessCredit( Credit *c )
{
  assert( c );

  for ( int v = 0; v < c->vc_cnt; ++v ) {
    assert( ( c->vc[v] >= 0 ) && ( c->vc[v] < _vcs ) );

    if ( ( _wait_for_tail_credit ) && 
	 ( !_in_use[c->vc[v]] ) ) {
      Error( "Received credit for idle buffer" );
    }

    if ( _cur_occupied[c->vc[v]] > 0 ) {
      if(_cur_occupied[c->vc[v]] > _vc_buf_size) {
	--_shared_occupied;
      }
      --_cur_occupied[c->vc[v]];

      if ( ( _cur_occupied[c->vc[v]] == 0 ) && 
	   ( _tail_sent[c->vc[v]] ) ) {
	_in_use[c->vc[v]] = false;
      }
    } else {
      cout << "VC = " << c->vc[v] << endl;
      Error( "Buffer occupancy fell below zero" );
    }
  }
}


void BufferState::SendingFlit( Flit *f )
{
  assert( f && ( f->vc >= 0 ) && ( f->vc < _vcs ) );

  if ( ( _shared_occupied >= _shared_buf_size ) &&
       ( _cur_occupied[f->vc] >= _vc_buf_size ) ) {
    Error( "Flit sent to full buffer" );
  } else {
    if ( _cur_occupied[f->vc] >= _vc_buf_size ) {
      ++_shared_occupied;
    }
    ++_cur_occupied[f->vc];
    
    if ( f->tail ) {
      _tail_sent[f->vc] = true;
      
      if ( !_wait_for_tail_credit ) {
	_in_use[f->vc] = false;
      }
    }
  }
}

void BufferState::TakeBuffer( int vc )
{
  assert( ( vc >= 0 ) && ( vc < _vcs ) );

  if ( _in_use[vc] ) {
    cout << "TakeBuffer( " << vc << " )" << endl;<commit_msg>only mark VC as unused upon returning tail credit if wait_for_tail_credit is actually enabled<commit_after>// $Id$

/*
Copyright (c) 2007-2009, Trustees of The Leland Stanford Junior University
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this 
list of conditions and the following disclaimer in the documentation and/or 
other materials provided with the distribution.
Neither the name of the Stanford University nor the names of its contributors 
may be used to endorse or promote products derived from this software without 
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*buffer_state.cpp
 *
 * This class is the buffere state of the next router down the channel
 * tracks the credit and how much of the buffer is in use 
 */

#include <iostream>
#include <cstdlib>
#include <cassert>

#include "booksim.hpp"
#include "buffer_state.hpp"
#include "random_utils.hpp"

BufferState::BufferState( const Configuration& config, 
			  Module *parent, const string& name ) : 
Module( parent, name ), _shared_occupied(0)
{
  _vc_buf_size     = config.GetInt( "vc_buf_size" );
  _shared_buf_size = config.GetInt( "shared_buf_size" );
  _vcs          = config.GetInt( "num_vcs" );
  
  _wait_for_tail_credit = config.GetInt( "wait_for_tail_credit" );
  _vc_busy_when_full = config.GetInt( "vc_busy_when_full" );

  _in_use.resize(_vcs, false);
  _tail_sent.resize(_vcs, false);
  _cur_occupied.resize(_vcs, 0);

  /* each flit is given a type and these types can only exists in 
   * specific virtual channels
   */
  _vc_range_begin.resize(Flit::NUM_FLIT_TYPES);
  _vc_sel_last.resize(Flit::NUM_FLIT_TYPES);
  _vc_range_size.resize(Flit::NUM_FLIT_TYPES);

  _vc_range_begin[Flit::READ_REQUEST] 
    = config.GetInt( "read_request_begin_vc" );
  _vc_sel_last[Flit::READ_REQUEST]
    = config.GetInt( "read_request_end_vc" );
  _vc_range_size[Flit::READ_REQUEST] 
    = _vc_sel_last[Flit::READ_REQUEST] - _vc_range_begin[Flit::READ_REQUEST] + 1;

  _vc_range_begin[Flit::WRITE_REQUEST] 
    = config.GetInt( "write_request_begin_vc" );
  _vc_sel_last[Flit::WRITE_REQUEST]
    = config.GetInt( "write_request_end_vc" );
  _vc_range_size[Flit::WRITE_REQUEST] 
    = _vc_sel_last[Flit::WRITE_REQUEST] - _vc_range_begin[Flit::WRITE_REQUEST] + 1;

  _vc_range_begin[Flit::READ_REPLY] 
    = config.GetInt( "read_reply_begin_vc" );
  _vc_sel_last[Flit::READ_REPLY]
    = config.GetInt( "read_reply_end_vc" );
  _vc_range_size[Flit::READ_REPLY] 
    = _vc_sel_last[Flit::READ_REPLY] - _vc_range_begin[Flit::READ_REPLY] + 1;

  _vc_range_begin[Flit::WRITE_REPLY] 
    = config.GetInt( "write_reply_begin_vc" );
  _vc_sel_last[Flit::WRITE_REPLY]
    = config.GetInt( "write_reply_end_vc" );
  _vc_range_size[Flit::WRITE_REPLY] 
    = _vc_sel_last[Flit::WRITE_REPLY] - _vc_range_begin[Flit::WRITE_REPLY] + 1;

  _vc_range_begin[Flit::ANY_TYPE] = 0 ;
  _vc_range_size[Flit::ANY_TYPE]   = _vcs ;
  _vc_sel_last[Flit::ANY_TYPE] = _vcs - 1 ;
}

void BufferState::ProcessCredit( Credit *c )
{
  assert( c );

  for ( int v = 0; v < c->vc_cnt; ++v ) {
    assert( ( c->vc[v] >= 0 ) && ( c->vc[v] < _vcs ) );

    if ( ( _wait_for_tail_credit ) && 
	 ( !_in_use[c->vc[v]] ) ) {
      Error( "Received credit for idle buffer" );
    }

    if ( _cur_occupied[c->vc[v]] > 0 ) {
      if(_cur_occupied[c->vc[v]] > _vc_buf_size) {
	--_shared_occupied;
      }
      --_cur_occupied[c->vc[v]];

      if ( _wait_for_tail_credit &&
	   ( _cur_occupied[c->vc[v]] == 0 ) && 
	   ( _tail_sent[c->vc[v]] ) ) {
	assert(_in_use[c->vc[v]]);
	_in_use[c->vc[v]] = false;
      }
    } else {
      cout << "VC = " << c->vc[v] << endl;
      Error( "Buffer occupancy fell below zero" );
    }
  }
}


void BufferState::SendingFlit( Flit *f )
{
  assert( f && ( f->vc >= 0 ) && ( f->vc < _vcs ) );

  if ( ( _shared_occupied >= _shared_buf_size ) &&
       ( _cur_occupied[f->vc] >= _vc_buf_size ) ) {
    Error( "Flit sent to full buffer" );
  } else {
    if ( _cur_occupied[f->vc] >= _vc_buf_size ) {
      ++_shared_occupied;
    }
    ++_cur_occupied[f->vc];
    
    if ( f->tail ) {
      _tail_sent[f->vc] = true;
      
      if ( !_wait_for_tail_credit ) {
	assert(_in_use[f->vc]);
	_in_use[f->vc] = false;
      }
    }
  }
}

void BufferState::TakeBuffer( int vc )
{
  assert( ( vc >= 0 ) && ( vc < _vcs ) );

  if ( _in_use[vc] ) {
    cout << "TakeBuffer( " << vc << " )" << endl;
{'fraction_non_alphanumeric': 0.11817501869857891, 'fraction_numerical': 0.004113687359760658, 'mean_word_length': 3.9615027829313543, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1162617', 'n_tokens_mistral': 4000, 'n_tokens_neox': 3786, 'n_words': 949}