1 | package de.uka.ipd.sdq.simucomframework.usage; |
2 | |
3 | import org.apache.log4j.Logger; |
4 | |
5 | import de.uka.ipd.sdq.reliability.core.FailureStatistics; |
6 | import de.uka.ipd.sdq.scheduler.IActiveResource; |
7 | import de.uka.ipd.sdq.simucomframework.Context; |
8 | import de.uka.ipd.sdq.simucomframework.SimuComSimProcess; |
9 | import de.uka.ipd.sdq.simucomframework.model.SimuComModel; |
10 | |
11 | /** |
12 | * Implementation of the workload driver interface for open workloads |
13 | * |
14 | * @author Steffen Becker |
15 | * |
16 | */ |
17 | public class OpenWorkload extends SimuComSimProcess implements IWorkloadDriver { |
18 | |
19 | private String interArrivalTime; |
20 | private IUserFactory userFactory; |
21 | private String usageScenarioId; |
22 | |
23 | private static Logger logger = Logger.getLogger(OpenWorkload.class |
24 | .getName()); |
25 | |
26 | /** |
27 | * Counter for usage scenario runs. |
28 | */ |
29 | |
30 | /** |
31 | * Constructor of the open workload driver |
32 | * |
33 | * @param model |
34 | * The simulation model this driver belongs to |
35 | * @param userFactory |
36 | * The factory which is used to bread the users |
37 | * @param interArrivalTime |
38 | * The time to wait between leaving a new user to its fate |
39 | */ |
40 | public OpenWorkload(SimuComModel model, IUserFactory userFactory, |
41 | String interArrivalTime, String usageScenarioId) { |
42 | super(model, "OpenWorkloadUserMaturationChamber"); |
43 | this.interArrivalTime = interArrivalTime; |
44 | this.userFactory = userFactory; |
45 | this.usageScenarioId = usageScenarioId; |
46 | } |
47 | |
48 | public void run() { |
49 | this.scheduleAt(0); |
50 | } |
51 | |
52 | @Override |
53 | protected void internalLifeCycle() { |
54 | FailureStatistics.getInstance().reset(); |
55 | |
56 | // As long as the simulation is running, new OpenWorkloadUsers are |
57 | // generated and started: |
58 | while (getModel().getSimulationControl().isRunning()) { |
59 | |
60 | try { |
61 | // Generate and execute the new user: |
62 | generateUser(); |
63 | |
64 | // Wait for inter-arrival time: |
65 | waitForNextUser(); |
66 | |
67 | // Count the new user: |
68 | if (this.getModel().getConfig().getSimulateFailures()) { |
69 | FailureStatistics.getInstance().increaseRunCount(); |
70 | FailureStatistics.getInstance().printRunCount(logger); |
71 | } |
72 | } |
73 | catch (OutOfMemoryError e) { |
74 | // the system is overloaded. stop simulation |
75 | logger.info("Stopping simulation run due to memory constraints."); |
76 | getModel().getSimulationControl().stop(); |
77 | } |
78 | } |
79 | |
80 | // Print failure statistics: |
81 | // if (this.getModel().getConfig().getSimulateFailures()) { |
82 | // FailureStatistics.getInstance().printFailureStatistics(logger); |
83 | // } |
84 | } |
85 | |
86 | private void waitForNextUser() { |
87 | double interArrivalTimeSample = (Double) Context.evaluateStatic( |
88 | interArrivalTime, Double.class); |
89 | logger.debug("Waiting for " + interArrivalTimeSample |
90 | + " before spawing the next user"); |
91 | this.hold(interArrivalTimeSample); |
92 | } |
93 | |
94 | private IUser generateUser() { |
95 | logger.debug("Spawning New User..."); |
96 | IUser user = userFactory.createUser(usageScenarioId); |
97 | user.startUserLife(); |
98 | return user; |
99 | } |
100 | |
101 | } |