gem5  v21.0.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 <cmath>
43 #include <fstream>
44 #include <sstream>
45 
46 #include "base/intmath.hh"
47 #include "base/random.hh"
48 #include "debug/TrafficGen.hh"
49 #include "params/TrafficGen.hh"
50 #include "sim/stats.hh"
51 #include "sim/system.hh"
52 
53 TrafficGen::TrafficGen(const TrafficGenParams &p)
54  : BaseTrafficGen(p),
55  configFile(p.config_file),
56  currState(0)
57 {
58 }
59 
60 void
62 {
64 
65  parseConfig();
66 }
67 
68 void
70 {
72 
73  // when not restoring from a checkpoint, make sure we kick things off
74  if (system->isTimingMode()) {
75  DPRINTF(TrafficGen, "Timing mode, activating request generator\n");
76  start();
77  } else {
79  "Traffic generator is only active in timing mode\n");
80  }
81 }
82 
83 void
85 {
87 
89 }
90 
91 void
93 {
94  // @todo In the case of a stateful generator state such as the
95  // trace player we would also have to restore the position in the
96  // trace playback and the tick offset
98 
100 }
101 
102 std::string
103 TrafficGen::resolveFile(const std::string &name)
104 {
105  // Do nothing for empty and absolute file names
106  if (name.empty() || name[0] == '/')
107  return name;
108 
109  char *config_path = strdup(configFile.c_str());
110  char *config_dir = dirname(config_path);
111  const std::string config_rel = csprintf("%s/%s", config_dir, name);
112  free(config_path);
113 
114  // Check the path relative to the config file first
115  if (access(config_rel.c_str(), R_OK) == 0)
116  return config_rel;
117 
118  // Fall back to the old behavior and search relative to the
119  // current working directory.
120  return name;
121 }
122 
123 void
125 {
126  // keep track of the transitions parsed to create the matrix when
127  // done
128  std::vector<Transition> transitions;
129 
130  // open input file
131  std::ifstream infile;
132  infile.open(configFile.c_str(), std::ifstream::in);
133  if (!infile.is_open()) {
134  fatal("Traffic generator %s config file not found at %s\n",
135  name(), configFile);
136  }
137 
138  bool init_state_set = false;
139 
140  // read line by line and determine the action based on the first
141  // keyword
142  std::string keyword;
143  std::string line;
144 
145  while (getline(infile, line).good()) {
146  // see if this line is a comment line, and if so skip it
147  if (line.find('#') != 1) {
148  // create an input stream for the tokenization
149  std::istringstream is(line);
150 
151  // determine the keyword
152  is >> keyword;
153 
154  if (keyword == "STATE") {
155  // parse the behaviour of this state
156  uint32_t id;
157  Tick duration;
158  std::string mode;
159 
160  is >> id >> duration >> mode;
161 
162  if (mode == "TRACE") {
163  std::string traceFile;
164  Addr addrOffset;
165 
166  is >> traceFile >> addrOffset;
167  traceFile = resolveFile(traceFile);
168 
169  states[id] = createTrace(duration, traceFile, addrOffset);
170  DPRINTF(TrafficGen, "State: %d TraceGen\n", id);
171  } else if (mode == "IDLE") {
172  states[id] = createIdle(duration);
173  DPRINTF(TrafficGen, "State: %d IdleGen\n", id);
174  } else if (mode == "EXIT") {
175  states[id] = createExit(duration);
176  DPRINTF(TrafficGen, "State: %d ExitGen\n", id);
177  } else if (mode == "LINEAR" || mode == "RANDOM" ||
178  mode == "DRAM" || mode == "DRAM_ROTATE" ||
179  mode == "NVM") {
180  uint32_t read_percent;
181  Addr start_addr;
182  Addr end_addr;
183  Addr blocksize;
184  Tick min_period;
185  Tick max_period;
186  Addr data_limit;
187 
188  is >> read_percent >> start_addr >> end_addr >>
189  blocksize >> min_period >> max_period >> data_limit;
190 
191  DPRINTF(TrafficGen, "%s, addr %x to %x, size %d,"
192  " period %d to %d, %d%% reads\n",
193  mode, start_addr, end_addr, blocksize, min_period,
194  max_period, read_percent);
195 
196 
197  if (mode == "LINEAR") {
198  states[id] = createLinear(duration, start_addr,
199  end_addr, blocksize,
200  min_period, max_period,
201  read_percent, data_limit);
202  DPRINTF(TrafficGen, "State: %d LinearGen\n", id);
203  } else if (mode == "RANDOM") {
204  states[id] = createRandom(duration, start_addr,
205  end_addr, blocksize,
206  min_period, max_period,
207  read_percent, data_limit);
208  DPRINTF(TrafficGen, "State: %d RandomGen\n", id);
209  } else if (mode == "DRAM" || mode == "DRAM_ROTATE" ||
210  mode == "NVM") {
211  // stride size (bytes) of the request for achieving
212  // required hit length
213  unsigned int stride_size;
214  unsigned int page_size;
215  unsigned int nbr_of_banks;
216  unsigned int nbr_of_banks_util;
217  unsigned _addr_mapping;
218  unsigned int nbr_of_ranks;
219 
220  is >> stride_size >> page_size >> nbr_of_banks >>
221  nbr_of_banks_util >> _addr_mapping >>
222  nbr_of_ranks;
223  Enums::AddrMap addr_mapping =
224  static_cast<Enums::AddrMap>(_addr_mapping);
225 
226  if (stride_size > page_size)
227  warn("Memory generator stride size (%d) is greater"
228  " than page size (%d) of the memory\n",
229  blocksize, page_size);
230 
231  // count the number of sequential packets to
232  // generate
233  unsigned int num_seq_pkts = 1;
234 
235  if (stride_size > blocksize) {
236  num_seq_pkts = divCeil(stride_size, blocksize);
237  DPRINTF(TrafficGen, "stride size: %d "
238  "block size: %d, num_seq_pkts: %d\n",
239  stride_size, blocksize, num_seq_pkts);
240  }
241 
242  if (mode == "DRAM") {
243  states[id] = createDram(duration, start_addr,
244  end_addr, blocksize,
245  min_period, max_period,
246  read_percent, data_limit,
247  num_seq_pkts, page_size,
248  nbr_of_banks,
249  nbr_of_banks_util,
250  addr_mapping,
251  nbr_of_ranks);
252  DPRINTF(TrafficGen, "State: %d DramGen\n", id);
253  } else if (mode == "DRAM_ROTATE") {
254  // Will rotate to the next rank after rotating
255  // through all banks, for each command type.
256  // In the 50% read case, series will be issued
257  // for both RD & WR before the rank in incremented
258  unsigned int max_seq_count_per_rank =
259  (read_percent == 50) ? nbr_of_banks_util * 2
260  : nbr_of_banks_util;
261 
262  states[id] = createDramRot(duration, start_addr,
263  end_addr, blocksize,
264  min_period, max_period,
265  read_percent,
266  data_limit,
267  num_seq_pkts, page_size,
268  nbr_of_banks,
269  nbr_of_banks_util,
270  addr_mapping,
271  nbr_of_ranks,
272  max_seq_count_per_rank);
273  DPRINTF(TrafficGen, "State: %d DramRotGen\n", id);
274  } else {
275  states[id] = createNvm(duration, start_addr,
276  end_addr, blocksize,
277  min_period, max_period,
278  read_percent, data_limit,
279  num_seq_pkts, page_size,
280  nbr_of_banks,
281  nbr_of_banks_util,
282  addr_mapping,
283  nbr_of_ranks);
284  DPRINTF(TrafficGen, "State: %d NvmGen\n", id);
285  }
286  }
287  } else {
288  fatal("%s: Unknown traffic generator mode: %s",
289  name(), mode);
290  }
291  } else if (keyword == "TRANSITION") {
293 
294  is >> transition.from >> transition.to >> transition.p;
295 
296  transitions.push_back(transition);
297 
298  DPRINTF(TrafficGen, "Transition: %d -> %d\n", transition.from,
299  transition.to);
300  } else if (keyword == "INIT") {
301  // set the initial state as the active state
302  is >> currState;
303 
304  init_state_set = true;
305 
306  DPRINTF(TrafficGen, "Initial state: %d\n", currState);
307  }
308  }
309  }
310 
311  if (!init_state_set)
312  fatal("%s: initial state not specified (add 'INIT <id>' line "
313  "to the config file)\n", name());
314 
315  // resize and populate state transition matrix
316  transitionMatrix.resize(states.size());
317  for (size_t i = 0; i < states.size(); i++) {
318  transitionMatrix[i].resize(states.size());
319  }
320 
321  for (std::vector<Transition>::iterator t = transitions.begin();
322  t != transitions.end(); ++t) {
323  transitionMatrix[t->from][t->to] = t->p;
324  }
325 
326  // ensure the egress edges do not have a probability larger than
327  // one
328  for (size_t i = 0; i < states.size(); i++) {
329  double sum = 0;
330  for (size_t j = 0; j < states.size(); j++) {
331  sum += transitionMatrix[i][j];
332  }
333 
334  // avoid comparing floating point numbers
335  if (std::fabs(sum - 1.0) > 0.001) {
336  fatal("%s has transition probability != 1 for state %d\n",
337  name(), i);
338  }
339  }
340 
341  // close input file
342  infile.close();
343 }
344 
345 size_t
347 {
348  double p = random_mt.random<double>();
349  assert(currState < transitionMatrix.size());
350  double cumulative = 0.0;
351  size_t i = 0;
352  do {
353  cumulative += transitionMatrix[currState][i];
354  ++i;
355  } while (cumulative < p && i < transitionMatrix[currState].size());
356 
357  return i - 1;
358 }
359 
360 std::shared_ptr<BaseGen>
362 {
363  // Return the initial state if there isn't an active generator,
364  // otherwise perform a state transition.
365  if (activeGenerator)
366  currState = nextState();
367 
368  DPRINTF(TrafficGen, "Transition to state %d\n", currState);
369  return states[currState];
370 }
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:430
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:84
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:103
TrafficGen::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: traffic_gen.cc:92
SimObject::initState
virtual void initState()
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: sim_object.cc:88
System::isTimingMode
bool isTimingMode() const
Is the system in timing mode?
Definition: system.hh:264
UNSERIALIZE_SCALAR
#define UNSERIALIZE_SCALAR(scalar)
Definition: serialize.hh:591
RiscvISA::sum
Bitfield< 18 > sum
Definition: registers.hh:634
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:392
BaseTrafficGen::activeGenerator
std::shared_ptr< BaseGen > activeGenerator
Currently active generator.
Definition: base.hh:339
BaseTrafficGen::transition
void transition()
Transition to the next generator.
Definition: base.cc:228
random.hh
Tick
uint64_t Tick
Tick count type.
Definition: types.hh:59
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:61
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:406
Random::random
std::enable_if_t< std::is_integral< T >::value, T > random()
Use the SFINAE idiom to choose an implementation based on whether the type is integral or floating po...
Definition: random.hh:86
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:37
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:346
TrafficGen::initState
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: traffic_gen.cc:69
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:237
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:378
BaseTrafficGen::init
void init() override
init() is called after all C++ SimObjects have been created and all ports are connected.
Definition: base.cc:102
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:540
TrafficGen::nextGenerator
std::shared_ptr< BaseGen > nextGenerator() override
Definition: traffic_gen.cc:361
TrafficGen::currState
uint32_t currState
Index of the current state.
Definition: traffic_gen.hh:113
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:148
name
const std::string & name()
Definition: trace.cc:48
SERIALIZE_SCALAR
#define SERIALIZE_SCALAR(scalar)
Definition: serialize.hh:584
TrafficGen::transitionMatrix
std::vector< std::vector< double > > transitionMatrix
State transition matrix.
Definition: traffic_gen.hh:110
BaseTrafficGen::start
void start()
Definition: base.cc:280
BaseTrafficGen::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: base.cc:130
SimObject::name
virtual const std::string name() const
Definition: sim_object.hh:182
ArmISA::t
Bitfield< 5 > t
Definition: miscregs_types.hh:67
TrafficGen::TrafficGen
TrafficGen(const TrafficGenParams &p)
Definition: traffic_gen.cc:53
TrafficGen::parseConfig
void parseConfig()
Parse the config file and build the state map and transition matrix.
Definition: traffic_gen.cc:124
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:64
BaseTrafficGen::createIdle
std::shared_ptr< BaseGen > createIdle(Tick duration)
Definition: base.cc:364
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:68
BaseTrafficGen::createExit
std::shared_ptr< BaseGen > createExit(Tick duration)
Definition: base.cc:371
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:150
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:500

Generated on Tue Jun 22 2021 15:28:27 for gem5 by doxygen 1.8.17