1 | package de.uka.ipd.sdq.prototype.framework; |
2 | |
3 | import de.uka.ipd.sdq.measurement.strategies.activeresource.DegreeOfAccuracyEnum; |
4 | import de.uka.ipd.sdq.measurement.strategies.activeresource.IDemandStrategy; |
5 | import de.uka.ipd.sdq.measurement.strategies.activeresource.ResourceTypeEnum; |
6 | import de.uka.ipd.sdq.measurement.strategies.activeresource.cpu.CalculatePrimesDemand; |
7 | import de.uka.ipd.sdq.measurement.strategies.activeresource.cpu.CountNumbersDemand; |
8 | import de.uka.ipd.sdq.measurement.strategies.activeresource.cpu.FFTDemand; |
9 | import de.uka.ipd.sdq.measurement.strategies.activeresource.cpu.FibonacciDemand; |
10 | import de.uka.ipd.sdq.measurement.strategies.activeresource.cpu.MandelbrotDemand; |
11 | import de.uka.ipd.sdq.measurement.strategies.activeresource.cpu.SortArrayDemand; |
12 | import de.uka.ipd.sdq.measurement.strategies.activeresource.cpu.VoidDemand; |
13 | import de.uka.ipd.sdq.measurement.strategies.activeresource.cpu.WaitDemand; |
14 | import de.uka.ipd.sdq.measurement.strategies.activeresource.hdd.ReadLargeChunksDemand; |
15 | import de.uka.ipd.sdq.simucomframework.variables.StackContext; |
16 | |
17 | /** |
18 | * Assigns strategies for resource demand simulation. Also starts calibration |
19 | * for active resources. |
20 | * |
21 | * Strategy is chosen by its name. |
22 | * |
23 | * @author Sebastian Lehrig, Thomas Zolynski |
24 | */ |
25 | public class AbstractResourceEnvironment { |
26 | |
27 | protected static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getRootLogger(); |
28 | |
29 | /** CPU strategies, names */ |
30 | protected static final String[] cpuStrategies = { "primes", "count_numbers", "fft", "fibonacci", "mandelbrot", "sortarray", "void", "wait" }; |
31 | /** CPU strategies, corresponding classes */ |
32 | protected static final Class<?>[] cpuStrategiesClasses = { CalculatePrimesDemand.class, CountNumbersDemand.class, FFTDemand.class, FibonacciDemand.class, MandelbrotDemand.class, SortArrayDemand.class, VoidDemand.class, WaitDemand.class }; |
33 | |
34 | /** HDD strategies, names */ |
35 | protected static final String[] hddStrategies = { "largeChunks" }; |
36 | /** HDD strategies, corresponding classes */ |
37 | protected static final Class<?>[] hddStrategiesClasses = { ReadLargeChunksDemand.class }; |
38 | |
39 | /** |
40 | * Maps a strategy string to a strategy class. |
41 | * |
42 | * @param usedStrategy name of the strategy |
43 | * @param strategies array of strategy names |
44 | * @param strategiesClasses array of corresponding strategy classes |
45 | * @return new instance of the strategy class whose name was given |
46 | */ |
47 | private static IDemandStrategy getStrategy(String usedStrategy, String[] strategies, Class<?>[] strategiesClasses) |
48 | { |
49 | IDemandStrategy strategy = null; |
50 | for (int i = 0; i < strategies.length; i ++) { |
51 | if (strategies[i].equals(usedStrategy)) { |
52 | try { |
53 | |
54 | strategy = (IDemandStrategy) strategiesClasses[i].newInstance(); |
55 | } catch (InstantiationException e) { |
56 | logger.error("CPU strategy " + usedStrategy + " can not be instantiated"); |
57 | } catch (IllegalAccessException e) { |
58 | } |
59 | } |
60 | } |
61 | return strategy; |
62 | } |
63 | |
64 | /** |
65 | * Starts the CPU calibration. |
66 | * |
67 | * @param usedStrategy name of the used strategy |
68 | * @param calibrationPath path where old calibration runs can be retrieved from and new ones will be stored |
69 | * @param accuracy accuracy of the calibration: LOW, MEDIUM or HIGH |
70 | * @param processingRate processing rate |
71 | */ |
72 | protected static void setUpCPU(String usedStrategy, String calibrationPath, DegreeOfAccuracyEnum accuracy, String processingRate) { |
73 | IDemandStrategy strategy = getStrategy(usedStrategy, cpuStrategies, cpuStrategiesClasses); |
74 | |
75 | if (strategy == null) { |
76 | logger.warn("CPU strategy " + (usedStrategy == null ? "" : usedStrategy) + " not found. Using Fibonacci instead"); |
77 | strategy = new FibonacciDemand(); |
78 | } |
79 | |
80 | registerStrategy(calibrationPath, accuracy, processingRate, strategy, de.uka.ipd.sdq.measurement.strategies.activeresource.ResourceTypeEnum.CPU); |
81 | } |
82 | |
83 | /** |
84 | * Starts the HDD calibration. |
85 | * |
86 | * @param usedStrategy name of the used strategy |
87 | * @param calibrationPath path where old calibration runs can be retrieved from and new ones will be stored |
88 | * @param accuracy accuracy of the calibration: LOW, MEDIUM or HIGH |
89 | * @param processingRate processing rate |
90 | */ |
91 | protected static void setUpHDD(String usedStrategy, String calibrationPath, DegreeOfAccuracyEnum accuracy, String processingRate) { |
92 | IDemandStrategy strategy = getStrategy(usedStrategy, cpuStrategies, cpuStrategiesClasses);; |
93 | |
94 | if (strategy == null) { |
95 | logger.warn("HDD strategy " + (usedStrategy == null ? "" : usedStrategy) + " not found. Using LargeChunks instead"); |
96 | strategy = new de.uka.ipd.sdq.measurement.strategies.activeresource.hdd.ReadLargeChunksDemand(); |
97 | } |
98 | |
99 | registerStrategy(calibrationPath, accuracy, processingRate, strategy, de.uka.ipd.sdq.measurement.strategies.activeresource.ResourceTypeEnum.HDD); |
100 | } |
101 | |
102 | private static void registerStrategy(String calibrationPath, DegreeOfAccuracyEnum accuracy, String processingRate, IDemandStrategy strategy, ResourceTypeEnum resourceType) { |
103 | double procRate = StackContext.evaluateStatic(processingRate, Double.class); |
104 | strategy.initializeStrategy(accuracy, procRate, calibrationPath); |
105 | |
106 | de.uka.ipd.sdq.prototype.framework.strategies.DemandConsumerStrategiesRegistry.singleton().registerStrategyFor(resourceType, strategy); |
107 | } |
108 | |
109 | /** |
110 | * Performs the "delay" resource demand. |
111 | * TODO: Does not really fit into this class. However, we didn't find a more suiting one. |
112 | * |
113 | * @param delay delay duration in ms |
114 | */ |
115 | public static void performDelay(double delay) { |
116 | try { |
117 | Thread.sleep(Math.round(delay)); |
118 | } catch (InterruptedException e) { |
119 | logger.error("Interrupted while performing a delay", e); |
120 | } |
121 | } |
122 | } |