-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.cpp
More file actions
147 lines (130 loc) · 4.03 KB
/
cpu.cpp
File metadata and controls
147 lines (130 loc) · 4.03 KB
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
/*******************************************************************
*
* DESCRIPTION: class CPU (processes jobs with a specified
* distribution)
*
* AUTHOR: Amir Barylko & Jorge Beyoglonian
* v2: Alejandro Troccoli
*
* EMAIL: mailto://amir@dc.uba.ar
* mailto://jbeyoglo@dc.uba.ar
* mailto://atroccol@dc.uba.ar
*
* DATE: 27/6/1998
* DATE: 01/02/2001
*
*******************************************************************/
/** include files **/
#include <math.h> // fabs( ... )
#include "cpu.h" // base header
#include "message.h" // InternalMessage ....
#include "distri.h" // class Distribution
#include "parsimu.h" // class ParallelMainSimulator
#include "strutil.h" // str2float( ... )
using namespace std;
/*******************************************************************
* CLASS CPUState
*********************************************************************/
/*******************************************************************
* Function Name: operator =
*********************************************************************/
CPUState& CPUState::operator=(CPUState& thisState) {
(AtomicState &)*this = (AtomicState &) thisState;
this->pid = thisState.pid;
return *this;
}
/*******************************************************************
* Function Name: copyState
*********************************************************************/
void CPUState::copyState(CPUState *rhs) {
*this = *((CPUState *) rhs);
}
/*******************************************************************
* Function Name: getSize
*********************************************************************/
int CPUState::getSize() const {
return sizeof( CPUState );
}
/*******************************************************************
* CLASS CPU
*********************************************************************/
/*******************************************************************
* Function Name: CPU
* Description: constructor
********************************************************************/
CPU::CPU( const string &name )
: Atomic( name )
, in( addInputPort( "in" ) )
, out( addOutputPort( "out" ) )
{
try
{
dist = Distribution::create( ParallelMainSimulator::Instance().getParameter( description(), "distribution" ) );
MASSERT( dist );
for ( register int i = 0 ; i < dist->varCount() ; i++ )
{
string parameter( ParallelMainSimulator::Instance().getParameter( description(), dist->getVar(i) ) ) ;
dist->setVar( i, str2float( parameter ) ) ;
}
} catch( InvalidDistribution &e )
{
e.addText( "The model " + description() + " has distribution problems!" ) ;
e.print(cerr);
MTHROW( e ) ;
} catch( MException &e )
{
MTHROW( e ) ;
}
}
/*******************************************************************
* Function Name: externalFunction
* Description: the CPU receives one job
********************************************************************/
Model &CPU::externalFunction( const ExternalMessage &msg )
{
#ifdef DEVS_DELAY_INTERNAL
string a;
for (int i = 1; i < 100000; i++ ) {
if ( a.length() > 100 )
a = "";
else
a += i;
}
#endif
pid( static_cast< int >( msg.value() )) ;
#ifndef DEVS_NOTRANDOM
holdIn( AtomicState::active, VTime( static_cast<float>( fabs(distribution().get() ) ) ) ) ;
#else
holdIn( AtomicState::active, VTime( 0,0,15,0 ) ) ;
#endif
return *this ;
}
/*******************************************************************
* Function Name: internalFunction
********************************************************************/
Model &CPU::internalFunction( const InternalMessage & )
{
#ifdef DEVS_DELAY_INTERNAL
string a;
for (int i = 1; i < 100000; i++ ) {
if ( a.length() > 100 )
a = "";
else
a += i;
}
#endif
passivate();
return *this;
}
/*******************************************************************
* Function Name: outputFunction
********************************************************************/
Model &CPU::outputFunction( const CollectMessage &msg )
{
sendOutput( msg.time(), out, pid() );
return *this;
}
CPU::~CPU()
{
delete dist;
}