summaryrefslogtreecommitdiff
path: root/src/mem/ruby/tester/test_framework.cc
blob: cc11abe8ae0122a8a29b42de4e4838524646cc70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408

/*
    Copyright (C) 1999-2008 by Mark D. Hill and David A. Wood for the
    Wisconsin Multifacet Project.  Contact: gems@cs.wisc.edu
    http://www.cs.wisc.edu/gems/

    --------------------------------------------------------------------

    This file is part of the Ruby Multiprocessor Memory System Simulator, 
    a component of the Multifacet GEMS (General Execution-driven 
    Multiprocessor Simulator) software toolset originally developed at 
    the University of Wisconsin-Madison.

    Ruby was originally developed primarily by Milo Martin and Daniel
    Sorin with contributions from Ross Dickson, Carl Mauer, and Manoj
    Plakal.

    Substantial further development of Multifacet GEMS at the
    University of Wisconsin was performed by Alaa Alameldeen, Brad
    Beckmann, Jayaram Bobba, Ross Dickson, Dan Gibson, Pacia Harper,
    Derek Hower, Milo Martin, Michael Marty, Carl Mauer, Michelle Moravan,
    Kevin Moore, Andrew Phelps, Manoj Plakal, Daniel Sorin, Haris Volos, 
    Min Xu, and Luke Yen.
    --------------------------------------------------------------------

    If your use of this software contributes to a published paper, we
    request that you (1) cite our summary paper that appears on our
    website (http://www.cs.wisc.edu/gems/) and (2) e-mail a citation
    for your published paper to gems@cs.wisc.edu.

    If you redistribute derivatives of this software, we request that
    you notify us and either (1) ask people to register with us at our
    website (http://www.cs.wisc.edu/gems/) or (2) collect registration
    information and periodically send it to us.

    --------------------------------------------------------------------

    Multifacet GEMS is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General Public
    License as published by the Free Software Foundation.

    Multifacet GEMS 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
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with the Multifacet GEMS; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307, USA

    The GNU General Public License is contained in the file LICENSE.

### END HEADER ###
*/

/*
 * $Id$
 *
 */

using namespace std;

#include "mem/ruby/tester/test_framework.hh"
#include "mem/protocol/protocol_name.hh"
#include "getopt.hh"
#include "mem/ruby/tester/DeterministicDriver.hh"
#include "mem/ruby/tester/RaceyDriver.hh"
#include "mem/ruby/common/Driver.hh"
#include "mem/ruby/recorder/Tracer.hh"


#include <string>
#include <map>
#include <iostream>
#include <assert.h>
#include <vector>
#include <string>
#include <sstream>
#include <sys/wait.h>

#include "mem/ruby/libruby.hh"

// FIXME: should really make this a class if can't figure out how to make a function to get the ruby parameter
static void set_defaults();
static void parseOptions(int argc, char **argv);
static void usageInstructions();
static void checkArg(char ch);
static void tester_initialize(int argc, char **argv);
static void tester_playback_trace();
static void tester_destroy();
static void hit_callback(int64_t request_id);

// Tester variables
string driver_type;
string generator_type;
Driver * m_driver_ptr;
int g_tester_length;
int num_completions;
Time g_think_time;
Time g_wait_time;
int num_procs;
static string trace_filename;
bool replaying;

void tester_main(int argc, char **argv)
{
  tester_initialize(argc, argv);
  
  if (trace_filename != "") {
    // playback a trace (for multicast-mask prediction)
    replaying = true;
    tester_playback_trace();
  }


  tester_destroy();
}

vector<string> tokenizeMyString(string str, string delims)
{
  vector<string> tokens;
  char* pch;
  char* tmp;
  const char* c_delims = delims.c_str();
  tmp = new char[str.length()+1];
  strcpy(tmp, str.c_str());
  pch = strtok(tmp, c_delims);
  while (pch != NULL) {
    tokens.push_back(string(pch));
    pch = strtok(NULL, c_delims);
  }
  delete [] tmp;
  return tokens;
}


vector<string> getPorts(const char* cfg_script, int cfg_script_argc, char* cfg_script_argv[])
{
  stringstream cfg_output;

  // first we execute the Ruby-lang configuration script
  int fd[2];
  int pid;
  if (pipe(fd) == -1) {
    perror("Error Creating Pipe");
    exit(EXIT_FAILURE);
  }
  
  pid = fork();
  if (pid == -1){
    perror("Error forking");
    exit(EXIT_FAILURE);
  }

  if (!pid) {
    // child
    close(fd[0]); // close the read end of the pipe
    // replace stdout with the write pipe
    if (dup2(fd[1], STDOUT_FILENO) == -1) {
      perror("Error redirecting stdout");
      exit(EXIT_FAILURE);
    }
#define QUOTE_MACRO(x, y) QUOTE_TXT(x,y)
#define QUOTE_TXT(x, y) #x y
    if (execlp("ruby", "ruby", "-I", QUOTE_MACRO(GEMS_ROOT, "/ruby/config"), QUOTE_MACRO(GEMS_ROOT, "/tests/list_ports.rb"), cfg_script, NULL)) {
      perror("execlp");
      exit(EXIT_FAILURE);
    }
  } else {
    close(fd[1]);   

    int child_status;
    if (wait(&child_status) == -1) {
      perror("wait");
      exit(EXIT_FAILURE);
    }
    if (child_status != EXIT_SUCCESS) {
      exit(EXIT_FAILURE);
    }
    
    char buf[100];
    int bytes_read;
    while( (bytes_read = read(fd[0], buf, 100)) > 0 ) {
      for (int i=0;i<bytes_read;i++) {
	cfg_output << buf[i];
      }
    }
    assert(bytes_read == 0);
    close(fd[0]);
  }
  string line;
  getline(cfg_output, line);

  return tokenizeMyString(line, " ");
}



void tester_initialize(int argc, char **argv)
{
  const char* cfg_file = argv[1];

  set_defaults();
  parseOptions(argc, argv);

  libruby_init(cfg_file);
  libruby_print_config(std::cout);

  vector<string> port_names = getPorts(cfg_file, 0, NULL);
  vector<RubyPortHandle> ports;

  for (vector<string>::const_iterator it = port_names.begin(); it != port_names.end(); it++)
    ports.push_back(libruby_get_port((*it).c_str(), hit_callback));
  
  if (driver_type == "Deterministic") {
    m_driver_ptr = new DeterministicDriver(generator_type, num_completions, num_procs, g_think_time, g_wait_time, g_tester_length);
  }
  else if (driver_type == "Racey") {
    m_driver_ptr = new RaceyDriver(num_procs, g_tester_length);
  }
 /* else if (driver_type == "Synthetic") {
    m_driver_ptr = new SyntheticDriver();
  }
  }*/

  if (trace_filename == "") {
    m_driver_ptr->go();
  }
}

void tester_playback_trace()
{
  replaying = true;
  assert(trace_filename != "");
  cout << "Reading trace from file '" << trace_filename << "'..." << endl;
  Tracer * replayer = new Tracer("noname");
  int read = replayer->playbackTrace(trace_filename);
  cout << "(" << read << " requests read)" << endl;
  if (read == 0) {
    ERROR_MSG("Zero items read from tracefile.");
  }
}

void tester_destroy()
{
  m_driver_ptr->printStats(cout);
  libruby_destroy();
  cerr << "Success: " << CURRENT_PROTOCOL << endl;
}


void hit_callback(int64_t request_id)
{
  if (!replaying) {
    m_driver_ptr->hitCallback(request_id); 
  }
}

// ************************************************************************
// *** Functions for parsing the command line parameters for the tester ***
// ************************************************************************



static struct option const long_options[] =
{
  {"help", no_argument, NULL, 'h'},
  {"number of processors", required_argument, NULL, 'p'},
  {"test run length", required_argument, NULL, 'l'},
  {"generator think time", required_argument, NULL, 'k'},
  {"generator wait time", required_argument, NULL, 'w'},
  {"driver type", required_argument, NULL, 'd'},
  {"generator type", required_argument, NULL, 'g'},
  {"num completions before pass", required_argument, NULL, 'n'},
  {"test tracer", required_argument, NULL, 'z'},
  {NULL, 0, NULL, 0}
};


// This is awkward and temporary, need the defaults config, and also need functions to
// just lookup a parameter in the configuration file
// Ideally the default values are set by libruby_init and then a function is provided to 
// set values at run-time
static void set_defaults() {
  replaying = false;
  g_tester_length = 0;
  g_think_time = 10;
  g_wait_time = 10;
  
  num_procs = 1;
  trace_filename = "";
  num_completions = 1;
  driver_type = "Deterministic";
  generator_type = "DetermSeriesGETSGenerator";
}

static void parseOptions(int argc, char **argv)
{
  cout << "Parsing command line arguments:" << endl;

  // construct the short arguments string
  int counter = 0;
  string short_options;
  while (long_options[counter].name != NULL) {
    short_options += char(long_options[counter].val);
    if (long_options[counter].has_arg == required_argument) {
      short_options += char(':');
    }
    counter++;
  }

  char c;
  /* Parse command line options.  */
  bool error;
  while ((c = getopt_long (argc, argv, short_options.c_str(), long_options, (int *) 0)) != EOF) {
    switch (c) {
    case 0:
      break;
    case 'h':
      usageInstructions();
      break;
    case 'p':
      checkArg(c);
      cout << "  number of processors = " << optarg << endl;
      num_procs = atoi( optarg );
      break;
    case 'l': {
      checkArg(c);
      g_tester_length = atoi(optarg);
      cout << "  length of run = " << g_tester_length << endl;
      if (g_tester_length == 0) {
        usageInstructions();
      }
      break;
    }
    case 'k': {
      checkArg(c);
      g_think_time = atoi(optarg);
      break;
    }
    case 'w': {
      checkArg(c);
      g_wait_time = atoi(optarg);
      break;
    }
    case 'd':
      checkArg(c);
      cout << "  driver type = " << optarg << endl;
      driver_type = strdup( optarg );
      break;
    case 'g':
      checkArg(c);
      cout << "  generator type = " << optarg << endl;
      generator_type = strdup( optarg );
      break;
    case 'n':
      checkArg(c);
      cout << "  num completions before pass = " << optarg << endl;
      num_completions = atoi( optarg );
      break;
   case 'z': 
      checkArg(c);
      trace_filename = string(optarg);
      cout << "  tracefile = " << trace_filename << endl;
      break;
     
    default:
      cerr << "parameter '" << c << "' unknown" << endl;
      usageInstructions();
    }
  }
}

static void usageInstructions()
{
  cerr << endl;
  cerr << "Options:" << endl;

  // print options
  int counter = 0;
  while (long_options[counter].name != NULL) {
    cerr << "  -" << char(long_options[counter].val);
    if (long_options[counter].has_arg == required_argument) {
      cerr << " <arg>";
    }
    cerr << "  --" << long_options[counter].name;
    if (long_options[counter].has_arg == required_argument) {
      cerr << " <arg>";
    }
    cerr << endl;
    counter++;
  }

  cerr << endl;
  g_debug_ptr->usageInstructions();
  cerr << endl;

  exit(1);
}

static void checkArg(char ch)
{
  if (optarg == NULL) {
    cerr << "Error: parameter '" << ch << "' missing required argument" << endl;
    usageInstructions();
  }
}