root / trunk / src / haizea / common / utils.py @ 641
1 |
# -------------------------------------------------------------------------- #
|
---|---|
2 |
# Copyright 2006-2009, University of Chicago #
|
3 |
# Copyright 2008-2009, Distributed Systems Architecture Group, Universidad #
|
4 |
# Complutense de Madrid (dsa-research.org) #
|
5 |
# #
|
6 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
|
7 |
# not use this file except in compliance with the License. You may obtain #
|
8 |
# a copy of the License at #
|
9 |
# #
|
10 |
# http://www.apache.org/licenses/LICENSE-2.0 #
|
11 |
# #
|
12 |
# Unless required by applicable law or agreed to in writing, software #
|
13 |
# distributed under the License is distributed on an "AS IS" BASIS, #
|
14 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
|
15 |
# See the License for the specific language governing permissions and #
|
16 |
# limitations under the License. #
|
17 |
# -------------------------------------------------------------------------- #
|
18 |
|
19 |
from mx import DateTime |
20 |
from math import ceil, floor |
21 |
from cPickle import dump, load, HIGHEST_PROTOCOL |
22 |
from datetime import datetime |
23 |
|
24 |
def generate_config_name(profile, tracefile, injectedfile): |
25 |
tracename=tracefile.split("/")[-1].split(".")[0] |
26 |
|
27 |
if injectedfile != None and injectedfile != "None": |
28 |
injectname=injectedfile.split("/")[-1].split(".")[0] |
29 |
name = tracename + "+" + injectname
|
30 |
else:
|
31 |
name = tracename |
32 |
|
33 |
name = profile + "_" + name
|
34 |
return name
|
35 |
|
36 |
def round_datetime_delta(d): |
37 |
return DateTime.DateTimeDelta(d.day, d.hour, d.minute, int(ceil(d.second))) |
38 |
|
39 |
def round_datetime(d): |
40 |
d += DateTime.TimeDelta(seconds=0.5)
|
41 |
return DateTime.DateTime(d.year, d.month, d.day, d.hour, d.minute, int(floor(d.second))) |
42 |
|
43 |
def UNIX2DateTime(t): |
44 |
return DateTime.TimestampFromTicks(t)
|
45 |
|
46 |
|
47 |
def vnodemapstr(vnodes): |
48 |
if len(vnodes) == 0: |
49 |
return "UNUSED" |
50 |
else:
|
51 |
return ",".join(["L"+`l`+"V"+`v` for (l, v) in vnodes]) |
52 |
|
53 |
# Based on http://norvig.com/python-iaq.html
|
54 |
def abstract(): |
55 |
import inspect |
56 |
caller = inspect.stack()[1][3] |
57 |
raise NotImplementedError(caller + ' must be implemented in subclass') |
58 |
|
59 |
def pickle(data, file): |
60 |
f = open (file, "w") |
61 |
dump(data, f, protocol = HIGHEST_PROTOCOL) |
62 |
f.close() |
63 |
|
64 |
def unpickle(file): |
65 |
f = open (file, "r") |
66 |
data = load(f) |
67 |
f.close() |
68 |
return data
|
69 |
|
70 |
|
71 |
LEASE_ID = 1
|
72 |
|
73 |
def get_lease_id(): |
74 |
global LEASE_ID
|
75 |
l = LEASE_ID |
76 |
LEASE_ID += 1
|
77 |
return l
|
78 |
|
79 |
def reset_lease_id_counter(): |
80 |
global LEASE_ID
|
81 |
LEASE_ID = 1
|
82 |
|
83 |
def pretty_nodemap(nodes): |
84 |
pnodes = list(set(nodes.values())) |
85 |
normmap = [([y[0] for y in nodes.items() if y[1]==x], x) for x in pnodes] |
86 |
for m in normmap: m[0].sort() |
87 |
s = " ".join([", ".join(["V"+`y` for y in x[0]])+" -> P" + `x[1]` for x in normmap]) |
88 |
return s
|
89 |
|
90 |
def estimate_transfer_time(size, bandwidth): |
91 |
bandwidthMBs = float(bandwidth) / 8 |
92 |
seconds = size / bandwidthMBs |
93 |
return round_datetime_delta(DateTime.TimeDelta(seconds = seconds))
|
94 |
|
95 |
def xmlrpc_marshall_singlevalue(value): |
96 |
if isinstance(value, DateTime.DateTimeType): |
97 |
return datetime.fromtimestamp(value)
|
98 |
elif isinstance(value, DateTime.DateTimeDeltaType): |
99 |
return value.seconds
|
100 |
else:
|
101 |
return value
|
102 |
|
103 |
def import_class(fq_name): |
104 |
fq_name = fq_name.split(".")
|
105 |
package_name = ".".join(fq_name[:-1]) |
106 |
class_name = fq_name[-1]
|
107 |
module = __import__(package_name, fromlist=[class_name])
|
108 |
exec("cls = module.%s" % class_name)
|
109 |
return cls
|
110 |
|
111 |
class Singleton(object): |
112 |
"""
|
113 |
A singleton base class.
|
114 |
Based on: http://code.activestate.com/recipes/52558/
|
115 |
"""
|
116 |
_singleton = None
|
117 |
def __new__(cls, *args, **kwargs): |
118 |
if cls._singleton == None: |
119 |
cls._singleton = object.__new__(cls, *args, **kwargs)
|
120 |
return cls._singleton
|
121 |
|
122 |
@classmethod
|
123 |
def get_singleton(cls): |
124 |
if '_singleton' not in vars(cls): |
125 |
return None |
126 |
else:
|
127 |
return cls._singleton
|
128 |
|
129 |
|
130 |
def get_config(): |
131 |
from haizea.core.manager import Manager |
132 |
return Manager.get_singleton().config
|
133 |
|
134 |
def get_accounting(): |
135 |
from haizea.core.manager import Manager |
136 |
return Manager.get_singleton().accounting
|
137 |
|
138 |
def get_clock(): |
139 |
from haizea.core.manager import Manager |
140 |
return Manager.get_singleton().clock
|
141 |
|
142 |
def get_policy(): |
143 |
from haizea.core.manager import Manager |
144 |
return Manager.get_singleton().policy
|
145 |
|
146 |
class InvalidStateMachineTransition(Exception): |
147 |
pass
|
148 |
|
149 |
class StateMachine(object): |
150 |
def __init__(self, initial_state, transitions, state_str = None): |
151 |
self.state = initial_state
|
152 |
self.transitions = transitions
|
153 |
self.state_str = state_str
|
154 |
|
155 |
def change_state(self, new_state): |
156 |
valid_next_states = [x[0] for x in self.transitions[self.state]] |
157 |
if new_state in valid_next_states: |
158 |
self.state = new_state
|
159 |
else:
|
160 |
raise InvalidStateMachineTransition, "Invalid transition. State is %s, wanted to change to %s, can only change to %s" % (self.get_state_str(self.state), self.get_state_str(new_state), [self.get_state_str(x) for x in valid_next_states]) |
161 |
|
162 |
def get_state(self): |
163 |
return self.state |
164 |
|
165 |
def get_state_str(self, state): |
166 |
if self.state_str == None: |
167 |
return "%s" % state |
168 |
else:
|
169 |
return self.state_str[state] |
170 |
|