1 // Copyright (c) 2015 ARM Limited
2 // All rights reserved.
4 // The license below extends only to copyright in the software and shall
5 // not be construed as granting a license to any other intellectual
6 // property including but not limited to intellectual property relating
7 // to a hardware implementation of the functionality of the software
8 // licensed hereunder. You may use the software subject to the license
9 // terms below provided that you ensure that this notice is replicated
10 // unmodified and in its entirety in all distributions of the software,
11 // modified or unmodified, in source code or in binary form.
13 // Redistribution and use in source and binary forms, with or without
14 // modification, are permitted provided that the following conditions are
15 // met: redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer;
17 // redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution;
20 // neither the name of the copyright holders nor the names of its
21 // contributors may be used to endorse or promote products derived from
22 // this software without specific prior written permission.
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 // Copyright 2009-2014 Sandia Coporation. Under the terms
37 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
38 // Government retains certain rights in this software.
40 // Copyright (c) 2009-2014, Sandia Corporation
41 // All rights reserved.
43 // For license information, see the LICENSE file in the current directory.
45 #include <sst_config.h>
46 #include <Python.h> // Before serialization to prevent spurious warnings
47 #include <sst/core/serialization.h>
52 #include <boost/tokenizer.hpp>
56 #include <sim/core.hh>
57 #include <sim/init.hh>
58 #include <sim/init_signals.hh>
59 #include <sim/system.hh>
60 #include <sim/sim_object.hh>
61 #include <base/misc.hh>
62 #include <base/debug.hh>
64 #ifdef fatal // gem5 sets this
69 #include <sst/core/params.h>
70 #include <sst/core/link.h>
71 #include <sst/core/timeConverter.h>
72 #include <sst/core/debug.h>
75 using namespace SST::gem5
;
77 gem5Component::gem5Component(ComponentId_t id
, Params
¶ms
) :
80 dbg
.init("@t:gem5:@p():@l " + getName() + ": ", 0, 0,
81 (Output::output_location_t
)params
.find_integer("comp_debug", 0));
82 info
.init("gem5:" + getName() + ": ", 0, 0, Output::STDOUT
);
84 TimeConverter
*clock
= registerClock(
85 params
.find_string("frequency", "1GHz"),
86 new Clock::Handler
<gem5Component
>(this, &gem5Component::clockTick
));
88 // This sets how many gem5 cycles we'll need to simulate per clock tick
89 sim_cycles
= clock
->getFactor();
91 // Disable gem5's inform() messages.
94 std::string cmd
= params
.find_string("cmd", "");
96 _abort(gem5Component
, "Component %s must have a 'cmd' parameter.\n",
100 std::vector
<char*> args
;
101 args
.push_back(const_cast<char*>("sst.x")); // TODO: Compute this somehow?
102 splitCommandArgs(cmd
, args
);
103 args
.push_back(const_cast<char*>("--initialize-only"));
104 dbg
.output(CALL_INFO
, "Command string: [sst.x %s --initialize-only]\n",
106 for (size_t i
= 0; i
< args
.size(); ++i
) {
107 dbg
.output(CALL_INFO
, " Arg [%02zu] = %s\n", i
, args
[i
]);
110 std::vector
<char*> flags
;
111 std::string gem5DbgFlags
= params
.find_string("gem5DebugFlags", "");
112 splitCommandArgs(gem5DbgFlags
, flags
);
113 for (auto flag
: flags
) {
114 dbg
.output(CALL_INFO
, " Setting Debug Flag [%s]\n", flag
);
118 ExternalMaster::registerHandler("sst", this); // these are idempotent
119 ExternalSlave ::registerHandler("sst", this);
121 // Initialize m5 special signal handling.
124 initPython(args
.size(), &args
[0]);
126 // tell the simulator not to end without us
127 registerAsPrimaryComponent();
128 primaryComponentDoNotEndSim();
130 clocks_processed
= 0;
133 gem5Component::~gem5Component(void)
139 gem5Component::init(unsigned phase
)
141 for (auto m
: masters
) {
144 for (auto s
: slaves
) {
150 gem5Component::setup(void)
152 // Switch connectors from initData to regular Sends
153 for (auto m
: masters
) {
156 for (auto s
: slaves
) {
162 gem5Component::finish(void)
164 for (auto m
: masters
) {
167 info
.output("Complete. Clocks Processed: %"PRIu64
"\n", clocks_processed
);
171 gem5Component::clockTick(Cycle_t cycle
)
173 dbg
.output(CALL_INFO
, "Cycle %lu\n", cycle
);
175 for (auto m
: masters
) {
179 GlobalSimLoopExitEvent
*event
= simulate(sim_cycles
);
181 if (event
!= simulate_limit_event
) {
182 info
.output("exiting: curTick()=%lu cause=`%s` code=%d\n",
183 curTick(), event
->getCause().c_str(), event
->getCode());
184 primaryComponentOKToEndSim();
193 gem5Component::splitCommandArgs(std::string
&cmd
,
194 std::vector
<char *> &args
)
196 std::string
sep1("\\");
197 std::string
sep2(" ");
198 std::string
sep3("\"\'");
200 boost::escaped_list_separator
<char> els(sep1
, sep2
, sep3
);
201 boost::tokenizer
<boost::escaped_list_separator
<char>> tok(cmd
, els
);
203 for (auto beg
: tok
) {
204 args
.push_back(strdup(beg
.c_str()));
210 gem5Component::initPython(int argc
, char *argv
[])
212 const char * m5MainCommands
[] = {
215 0 // sentinel is required
218 PyObject
*mainModule
,*mainDict
;
220 Py_SetProgramName(argv
[0]); // optional but recommended
224 int ret
= initM5Python();
226 _abort(gem5Component
, "Python failed to initialize. Code: %d\n", ret
);
229 PySys_SetArgv(argc
, argv
);
231 mainModule
= PyImport_AddModule("__main__");
234 mainDict
= PyModule_GetDict(mainModule
);
238 const char **command
= m5MainCommands
;
240 // evaluate each command in the m5MainCommands array (basically a
241 // bunch of python statements.
243 result
= PyRun_String(*command
, Py_file_input
, mainDict
, mainDict
);
254 ExternalMaster::Port
*
255 gem5Component::getExternalPort(const std::string
&name
,
256 ExternalMaster
&owner
, const std::string
&port_data
)
258 std::string
s(name
); // bridges non-& result and &-arg
259 auto master
= new ExtMaster(this, info
, owner
, s
);
260 masters
.push_back(master
);
265 gem5Component::getExternalPort(const std::string
&name
,
266 ExternalSlave
&owner
, const std::string
&port_data
)
268 std::string
s(name
); // bridges non-& result and &-arg
269 auto slave
= new ExtSlave(this, info
, owner
, s
);
270 slaves
.push_back(slave
);