gem5  v20.1.0.0
traffic_gen.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2013, 2016-2020 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
38 
39 #include <libgen.h>
40 #include <unistd.h>
41 
42 #include <fstream>
43 #include <sstream>
44 
45 #include "base/intmath.hh"
46 #include "base/random.hh"
47 #include "debug/TrafficGen.hh"
48 #include "params/TrafficGen.hh"
49 #include "sim/stats.hh"
50 #include "sim/system.hh"
51 
52 using namespace std;
53 
54 TrafficGen::TrafficGen(const TrafficGenParams* p)
55  : BaseTrafficGen(p),
56  configFile(p->config_file),
57  currState(0)
58 {
59 }
60 
62 TrafficGenParams::create()
63 {
64  return new TrafficGen(this);
65 }
66 
67 void
69 {
71 
72  parseConfig();
73 }
74 
75 void
77 {
79 
80  // when not restoring from a checkpoint, make sure we kick things off
81  if (system->isTimingMode()) {
82  DPRINTF(TrafficGen, "Timing mode, activating request generator\n");
83  start();
84  } else {
86  "Traffic generator is only active in timing mode\n");
87  }
88 }
89 
90 void
92 {
94 
96 }
97 
98 void
100 {
101  // @todo In the case of a stateful generator state such as the
102  // trace player we would also have to restore the position in the
103  // trace playback and the tick offset
105 
107 }
108 
109 std::string
110 TrafficGen::resolveFile(const std::string &name)
111 {
112  // Do nothing for empty and absolute file names
113  if (name.empty() || name[0] == '/')
114  return name;
115 
116  char *config_path = strdup(configFile.c_str());
117  char *config_dir = dirname(config_path);
118  const std::string config_rel = csprintf("%s/%s", config_dir, name);
119  free(config_path);
120 
121  // Check the path relative to the config file first
122  if (access(config_rel.c_str(), R_OK) == 0)
123  return config_rel;
124 
125  // Fall back to the old behavior and search relative to the
126  // current working directory.
127  return name;
128 }
129 
130 void
132 {
133  // keep track of the transitions parsed to create the matrix when
134  // done
135  vector<Transition> transitions;
136 
137  // open input file
138  ifstream infile;
139  infile.open(configFile.c_str(), ifstream::in);
140  if (!infile.is_open()) {
141  fatal("Traffic generator %s config file not found at %s\n",
142  name(), configFile);
143  }
144 
145  bool init_state_set = false;
146 
147  // read line by line and determine the action based on the first
148  // keyword
149  string keyword;
150  string line;
151 
152  while (getline(infile, line).good()) {
153  // see if this line is a comment line, and if so skip it
154  if (line.find('#') != 1) {
155  // create an input stream for the tokenization
156  istringstream is(line);
157 
158  // determine the keyword
159  is >> keyword;
160 
161  if (keyword == "STATE") {
162  // parse the behaviour of this state
163  uint32_t id;
164  Tick duration;
165  string mode;
166 
167  is >> id >> duration >> mode;
168 
169  if (mode == "TRACE") {
170  string traceFile;
171  Addr addrOffset;
172 
173  is >> traceFile >> addrOffset;
174  traceFile = resolveFile(traceFile);
175 
176  states[id] = createTrace(duration, traceFile, addrOffset);
177  DPRINTF(TrafficGen, "State: %d TraceGen\n", id);
178  } else if (mode == "IDLE") {
179  states[id] = createIdle(duration);
180  DPRINTF(TrafficGen, "State: %d IdleGen\n", id);
181  } else if (mode == "EXIT") {
182  states[id] = createExit(duration);
183  DPRINTF(TrafficGen, "State: %d ExitGen\n", id);
184  } else if (mode == "LINEAR" || mode == "RANDOM" ||
185  mode == "DRAM" || mode == "DRAM_ROTATE" ||
186  mode == "NVM") {
187  uint32_t read_percent;
188  Addr start_addr;
189  Addr end_addr;
190  Addr blocksize;
191  Tick min_period;
192  Tick max_period;
193  Addr data_limit;
194 
195  is >> read_percent >> start_addr >> end_addr >>
196  blocksize >> min_period >> max_period >> data_limit;
197 
198  DPRINTF(TrafficGen, "%s, addr %x to %x, size %d,"
199  " period %d to %d, %d%% reads\n",
200  mode, start_addr, end_addr, blocksize, min_period,
201  max_period, read_percent);
202 
203 
204  if (mode == "LINEAR") {
205  states[id] = createLinear(duration, start_addr,
206  end_addr, blocksize,
207  min_period, max_period,
208  read_percent, data_limit);
209  DPRINTF(TrafficGen, "State: %d LinearGen\n", id);
210  } else if (mode == "RANDOM") {
211  states[id] = createRandom(duration, start_addr,
212  end_addr, blocksize,
213  min_period, max_period,
214  read_percent, data_limit);
215  DPRINTF(TrafficGen, "State: %d RandomGen\n", id);
216  } else if (mode == "DRAM" || mode == "DRAM_ROTATE" ||
217  mode == "NVM") {
218  // stride size (bytes) of the request for achieving
219  // required hit length
220  unsigned int stride_size;
221  unsigned int page_size;
222  unsigned int nbr_of_banks;
223  unsigned int nbr_of_banks_util;
224  unsigned _addr_mapping;
225  unsigned int nbr_of_ranks;
226 
227  is >> stride_size >> page_size >> nbr_of_banks >>
228  nbr_of_banks_util >> _addr_mapping >>
229  nbr_of_ranks;
230  Enums::AddrMap addr_mapping =
231  static_cast<Enums::AddrMap>(_addr_mapping);
232 
233  if (stride_size > page_size)
234  warn("Memory generator stride size (%d) is greater"
235  " than page size (%d) of the memory\n",
236  blocksize, page_size);
237 
238  // count the number of sequential packets to
239  // generate
240  unsigned int num_seq_pkts = 1;
241 
242  if (stride_size > blocksize) {
243  num_seq_pkts = divCeil(stride_size, blocksize);
244  DPRINTF(TrafficGen, "stride size: %d "
245  "block size: %d, num_seq_pkts: %d\n",
246  stride_size, blocksize, num_seq_pkts);
247  }
248 
249  if (mode == "DRAM") {
250  states[id] = createDram(duration, start_addr,
251  end_addr, blocksize,
252  min_period, max_period,
253  read_percent, data_limit,
254  num_seq_pkts, page_size,
255  nbr_of_banks,
256  nbr_of_banks_util,
257  addr_mapping,
258  nbr_of_ranks);
259  DPRINTF(TrafficGen, "State: %d DramGen\n", id);
260  } else if (mode == "DRAM_ROTATE") {
261  // Will rotate to the next rank after rotating
262  // through all banks, for each command type.
263  // In the 50% read case, series will be issued
264  // for both RD & WR before the rank in incremented
265  unsigned int max_seq_count_per_rank =
266  (read_percent == 50) ? nbr_of_banks_util * 2
267  : nbr_of_banks_util;
268 
269  states[id] = createDramRot(duration, start_addr,
270  end_addr, blocksize,
271  min_period, max_period,
272  read_percent,
273  data_limit,
274  num_seq_pkts, page_size,
275  nbr_of_banks,
276  nbr_of_banks_util,
277  addr_mapping,
278  nbr_of_ranks,
279  max_seq_count_per_rank);
280  DPRINTF(TrafficGen, "State: %d DramRotGen\n", id);
281  } else {
282  states[id] = createNvm(duration, start_addr,
283  end_addr, blocksize,
284  min_period, max_period,
285  read_percent, data_limit,
286  num_seq_pkts, page_size,
287  nbr_of_banks,
288  nbr_of_banks_util,
289  addr_mapping,
290  nbr_of_ranks);
291  DPRINTF(TrafficGen, "State: %d NvmGen\n", id);
292  }
293  }
294  } else {
295  fatal("%s: Unknown traffic generator mode: %s",
296  name(), mode);
297  }
298  } else if (keyword == "TRANSITION") {
300 
301  is >> transition.from >> transition.to >> transition.p;
302 
303  transitions.push_back(transition);
304 
305  DPRINTF(TrafficGen, "Transition: %d -> %d\n", transition.from,
306  transition.to);
307  } else if (keyword == "INIT") {
308  // set the initial state as the active state
309  is >> currState;
310 
311  init_state_set = true;
312 
313  DPRINTF(TrafficGen, "Initial state: %d\n", currState);
314  }
315  }
316  }
317 
318  if (!init_state_set)
319  fatal("%s: initial state not specified (add 'INIT <id>' line "
320  "to the config file)\n", name());
321 
322  // resize and populate state transition matrix
323  transitionMatrix.resize(states.size());
324  for (size_t i = 0; i < states.size(); i++) {
325  transitionMatrix[i].resize(states.size());
326  }
327 
328  for (vector<Transition>::iterator t = transitions.begin();
329  t != transitions.end(); ++t) {
330  transitionMatrix[t->from][t->to] = t->p;
331  }
332 
333  // ensure the egress edges do not have a probability larger than
334  // one
335  for (size_t i = 0; i < states.size(); i++) {
336  double sum = 0;
337  for (size_t j = 0; j < states.size(); j++) {
338  sum += transitionMatrix[i][j];
339  }
340 
341  // avoid comparing floating point numbers
342  if (abs(sum - 1.0) > 0.001)
343  fatal("%s has transition probability != 1 for state %d\n",
344  name(), i);
345  }
346 
347  // close input file
348  infile.close();
349 }
350 
351 size_t
353 {
354  double p = random_mt.random<double>();
355  assert(currState < transitionMatrix.size());
356  double cumulative = 0.0;
357  size_t i = 0;
358  do {
359  cumulative += transitionMatrix[currState][i];
360  ++i;
361  } while (cumulative < p && i < transitionMatrix[currState].size());
362 
363  return i - 1;
364 }
365 
366 std::shared_ptr<BaseGen>
368 {
369  // Return the initial state if there isn't an active generator,
370  // otherwise perform a state transition.
371  if (activeGenerator)
372  currState = nextState();
373 
374  DPRINTF(TrafficGen, "Transition to state %d\n", currState);
375  return states[currState];
376 }
TrafficGen::Transition
Struct to represent a probabilistic transition during parsing.
Definition: traffic_gen.hh:103
BaseTrafficGen::createDramRot
std::shared_ptr< BaseGen > createDramRot(Tick duration, Addr start_addr, Addr end_addr, Addr blocksize, Tick min_period, Tick max_period, uint8_t read_percent, Addr data_limit, unsigned int num_seq_pkts, unsigned int page_size, unsigned int nbr_of_banks, unsigned int nbr_of_banks_util, Enums::AddrMap addr_mapping, unsigned int nbr_of_ranks, unsigned int max_seq_count_per_rank)
Definition: base.cc:425
fatal
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:183
TrafficGen::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: traffic_gen.cc:91
traffic_gen.hh
warn
#define warn(...)
Definition: logging.hh:239
system.hh
TrafficGen::resolveFile
std::string resolveFile(const std::string &name)
Resolve a file path in the configuration file.
Definition: traffic_gen.cc:110
TrafficGen::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: traffic_gen.cc:99
SimObject::initState
virtual void initState()
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: sim_object.cc:91
System::isTimingMode
bool isTimingMode() const
Is the system in timing mode?
Definition: system.hh:269
UNSERIALIZE_SCALAR
#define UNSERIALIZE_SCALAR(scalar)
Definition: serialize.hh:797
RiscvISA::sum
Bitfield< 18 > sum
Definition: registers.hh:609
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
BaseTrafficGen::createRandom
std::shared_ptr< BaseGen > createRandom(Tick duration, Addr start_addr, Addr end_addr, Addr blocksize, Tick min_period, Tick max_period, uint8_t read_percent, Addr data_limit)
Definition: base.cc:387
BaseTrafficGen::activeGenerator
std::shared_ptr< BaseGen > activeGenerator
Currently active generator.
Definition: base.hh:332
BaseTrafficGen::transition
void transition()
Transition to the next generator.
Definition: base.cc:229
random.hh
Tick
uint64_t Tick
Tick count type.
Definition: types.hh:63
TrafficGen::states
std::unordered_map< uint32_t, std::shared_ptr< BaseGen > > states
Map of generator states.
Definition: traffic_gen.hh:116
std::vector
STL vector class.
Definition: stl.hh:37
MipsISA::is
Bitfield< 24, 22 > is
Definition: pra_constants.hh:232
TrafficGen::init
void init() override
init() is called after all C++ SimObjects have been created and all ports are connected.
Definition: traffic_gen.cc:68
BaseTrafficGen::createDram
std::shared_ptr< BaseGen > createDram(Tick duration, Addr start_addr, Addr end_addr, Addr blocksize, Tick min_period, Tick max_period, uint8_t read_percent, Addr data_limit, unsigned int num_seq_pkts, unsigned int page_size, unsigned int nbr_of_banks, unsigned int nbr_of_banks_util, Enums::AddrMap addr_mapping, unsigned int nbr_of_ranks)
Definition: base.cc:401
random_mt
Random random_mt
Definition: random.cc:96
stats.hh
ArmISA::j
Bitfield< 24 > j
Definition: miscregs_types.hh:54
divCeil
T divCeil(const T &a, const U &b)
Definition: intmath.hh:114
cp
Definition: cprintf.cc:40
BaseTrafficGen
The traffic generator is a module that generates stimuli for the memory system, based on a collection...
Definition: base.hh:64
TrafficGen::nextState
size_t nextState()
Use the transition matrix to find the next state index.
Definition: traffic_gen.cc:352
TrafficGen::initState
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: traffic_gen.cc:76
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:234
BaseTrafficGen::createLinear
std::shared_ptr< BaseGen > createLinear(Tick duration, Addr start_addr, Addr end_addr, Addr blocksize, Tick min_period, Tick max_period, uint8_t read_percent, Addr data_limit)
Definition: base.cc:373
BaseTrafficGen::init
void init() override
init() is called after all C++ SimObjects have been created and all ports are connected.
Definition: base.cc:103
ArmISA::mode
Bitfield< 4, 0 > mode
Definition: miscregs_types.hh:70
BaseTrafficGen::createTrace
std::shared_ptr< BaseGen > createTrace(Tick duration, const std::string &trace_file, Addr addr_offset)
Definition: base.cc:519
TrafficGen::nextGenerator
std::shared_ptr< BaseGen > nextGenerator() override
Definition: traffic_gen.cc:367
TrafficGen::currState
uint32_t currState
Index of the current state.
Definition: traffic_gen.hh:113
TrafficGen::TrafficGen
TrafficGen(const TrafficGenParams *p)
Definition: traffic_gen.cc:54
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
name
const std::string & name()
Definition: trace.cc:50
SERIALIZE_SCALAR
#define SERIALIZE_SCALAR(scalar)
Definition: serialize.hh:790
TrafficGen::transitionMatrix
std::vector< std::vector< double > > transitionMatrix
State transition matrix.
Definition: traffic_gen.hh:110
BaseTrafficGen::start
void start()
Definition: base.cc:281
BaseTrafficGen::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: base.cc:131
SimObject::name
virtual const std::string name() const
Definition: sim_object.hh:133
std
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
ArmISA::t
Bitfield< 5 > t
Definition: miscregs_types.hh:67
TrafficGen::parseConfig
void parseConfig()
Parse the config file and build the state map and transition matrix.
Definition: traffic_gen.cc:131
Random::random
std::enable_if< std::is_integral< T >::value, T >::type random()
Use the SFINAE idiom to choose an implementation based on whether the type is integral or floating po...
Definition: random.hh:86
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:63
BaseTrafficGen::createIdle
std::shared_ptr< BaseGen > createIdle(Tick duration)
Definition: base.cc:359
TrafficGen::configFile
const std::string configFile
The config file to parse.
Definition: traffic_gen.hh:73
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
intmath.hh
CheckpointIn
Definition: serialize.hh:67
BaseTrafficGen::createExit
std::shared_ptr< BaseGen > createExit(Tick duration)
Definition: base.cc:366
TrafficGen
The traffic generator is a module that generates stimuli for the memory system, based on a collection...
Definition: traffic_gen.hh:67
csprintf
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:158
BaseTrafficGen::system
System *const system
The system used to determine which mode we are currently operating in.
Definition: base.hh:73
ArmISA::id
Bitfield< 33 > id
Definition: miscregs_types.hh:247
BaseTrafficGen::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: base.cc:151
BaseTrafficGen::createNvm
std::shared_ptr< BaseGen > createNvm(Tick duration, Addr start_addr, Addr end_addr, Addr blocksize, Tick min_period, Tick max_period, uint8_t read_percent, Addr data_limit, unsigned int num_seq_pkts, unsigned int buffer_size, unsigned int nbr_of_banks, unsigned int nbr_of_banks_util, Enums::AddrMap addr_mapping, unsigned int nbr_of_ranks)
Definition: base.cc:495

Generated on Wed Sep 30 2020 14:02:10 for gem5 by doxygen 1.8.17