-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm-placement.py
executable file
·893 lines (714 loc) · 23.3 KB
/
vm-placement.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
#!/usr/bin/env python3
# (C) Copyright 2022- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
#
import argparse
import logging
import os
import random
import re
import socket
from collections import defaultdict
import numpy as np
import openstack
import yaml
from tqdm import tqdm
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
LOG = logging.getLogger(__name__)
API = openstack.connect()
PROJECTS = {}
USERS = {}
with open(os.path.join(os.path.dirname(__file__), "vm-placement.yaml")) as f:
CONFIG = yaml.safe_load(f)
parser = argparse.ArgumentParser()
group = parser.add_argument_group("data collection options")
group.add_argument("--uptime", action="store_true")
group.add_argument("--diagnostics", action="store_true")
group.add_argument("--detailed", action="store_true")
group = parser.add_argument_group("display options")
group.add_argument(
"--sort",
choices=[
"default",
"read",
"write",
"rx",
"tx",
"memory_free",
"vcpus_used",
"load",
],
default="default",
)
group.add_argument("--reverse", action="store_true")
parser.add_argument("--stopped", action="store_true", help="List stopped VMs")
parser.add_argument("--status")
parser.add_argument("--csv", action="store_true")
parser.add_argument("--list", action="store_true")
parser.add_argument("--placement", action="store_true")
parser.add_argument("--make-space", type=int, default=0)
parser.add_argument("--doit", action="store_true")
parser.add_argument("--nowait", action="store_true")
group = parser.add_argument_group("fitness options")
group.add_argument("--moves-weight", type=int, default=1)
parser.add_argument("--head-room", type=int, default=0)
group = parser.add_argument_group("pygad options")
group.add_argument("--generations", type=int, default=100)
group.add_argument("--population", type=int, default=1000)
group.add_argument("--random", action="store_true")
group.add_argument("--plot", action="store_true")
group.add_argument(
"--crossover-type",
choices=["single_point", "two_points", "uniform", "scattered"],
default="single_point",
)
group.add_argument(
"--mutation-type",
choices=["random", "swap", "inversion", "scramble", "adaptive"],
default="random",
)
group.add_argument(
"--parent-selection-type",
choices=["sss", "rws", "sus", "rank", "random", "tournament"],
default="sss",
)
ARGS = parser.parse_args()
def bytes_to_string(n, zero="0"):
if n == 0:
return zero
if n < 0:
return f"-{bytes_to_string(-n)}"
u = ["", "K", "M", "G", "T", "P"]
i = 0
while n >= 1024:
n /= 1024.0
i += 1
return "%g%s" % (int(n * 10 + 0.5) / 10.0, u[i])
def round_up(x, size=1024 * 1024 * 1024):
return int((x + size - 1) / size) * size
def round_down(x, size=1024 * 1024 * 1024):
return int(x / size) * size
class Wrapper:
def __init__(self, openstack_object):
self._openstack_object = openstack_object
for k, v in openstack_object.toDict().items():
setattr(self, k, v)
class Hypervisor(Wrapper):
def __init__(self, hypervisor):
super().__init__(hypervisor)
self.servers = []
self.memory_size *= 1024 * 1024
self.memory_used *= 1024 * 1024
self.memory_free *= 1024 * 1024
if ARGS.uptime:
self.uptime = (
API.compute.get_hypervisor_uptime(self.id)["uptime"]
.strip()
.split(" ", 1)[1]
)
m = re.search(r"load average: ([\d\.]+), ([\d\.]+), ([\d\.]+)", self.uptime)
if m:
self.load_average = tuple(float(m.group(x)) for x in (1, 2, 3))
else:
self.load_average = ("?", "?", "?")
m = re.search(r"up (\d+ days?)", self.uptime)
if m.group(1):
self.up = m.group(1)
else:
self.up = "?"
self.load = max(self.load_average)
else:
self.load_average = ("?", "?", "?")
self.up = "?"
self.load = 0
def append(self, server):
self.servers.append(server)
@property
def default(self):
return self.name
@property
def rx(self):
return sum(s.rx for s in self.servers)
@property
def tx(self):
return sum(s.tx for s in self.servers)
@property
def read(self):
return sum(s.read for s in self.servers)
@property
def write(self):
return sum(s.write for s in self.servers)
@property
def io(self):
return self.tx + self.rx + self.read + self.write
class Server(Wrapper):
def __init__(self, server):
super().__init__(server)
self._diagnostics = None
# Compatibility
if hasattr(self, 'hypervisor_hostname'):
self.host = self.hypervisor_hostname
if "id" in self.flavor and self.flavor["id"] in FLAVORS:
self.flavor = FLAVORS[self.flavor["id"]]
self.memory_size = self.flavor.ram * 1024 * 1024
self.vcpus = self.flavor.vcpus
else:
self.memory_size = self.flavor.get("ram", 0) * 1024 * 1024
self.vcpus = self.flavor.get("vcpus", 0)
self.rx = 0
self.tx = 0
self.read = 0
self.write = 0
if ARGS.diagnostics:
for k, v in self.diagnostics.items():
if k.endswith("_rx"):
self.rx += v
if k.endswith("_tx"):
self.tx += v
if k.endswith("_read"):
self.read += v
if k.endswith("_write"):
self.write += v
@property
def project_name(self):
p = PROJECTS.get(self.project_id, self.project_id)
if isinstance(p, str):
return p
return p.name
@property
def user_name(self):
p = USERS.get(self.user_id, self.user_id)
if isinstance(p, str):
return p
return p.name
@property
def flavor_name(self):
assert "id" not in self.flavor
return self.flavor["original_name"]
@property
def image_name(self):
p = IMAGES.get(self.image["id"], self.image["id"])
if p is None:
# assert False, (self.image, IMAGES)
return "?"
if isinstance(p, str):
return p
return p.name
@property
def floating(self):
return self.accessIPv4
@property
def dns(self):
f = self.floating
if f != "":
dns = socket.getnameinfo((f, 0), 0)[0]
if dns != f:
return dns
return ""
@property
def comment(self):
d = self.dns
if d == "":
return ""
try:
h = socket.gethostbyname(self.dns)
except socket.error:
return f"Error resolving {self.dns}"
if h != self.floating:
return f"DNS mismatch {h}"
return ""
@property
def diagnostics(self):
if self._diagnostics is None:
LOG.info(f"Getting diagnostics for {self.id}")
self._diagnostics = API.compute.get(
f"/servers/{self.id}/diagnostics"
).json()
return self._diagnostics
class Project(Wrapper):
pass
class User(Wrapper):
pass
class Image(Wrapper):
pass
LOG.info("Get list of projects")
PROJECTS = {h.id: Project(h) for h in API.list_projects()}
LOG.info(f"Found {len(PROJECTS)} project(s)")
LOG.info("Get list of users")
USERS = {h.id: Project(h) for h in API.list_users()}
LOG.info(f"Found {len(USERS)} user(s)")
LOG.info("Get list of image")
IMAGES = {h.id: Image(h) for h in API.list_images()}
LOG.info(f"Found {len(IMAGES)} image(s)")
LOG.info("Get list of flavors")
FLAVORS = {h.id: Image(h) for h in API.list_flavors()}
LOG.info(f"Found {len(FLAVORS)} flavor(s)")
LOG.info("Get list of hypervisors")
HYPERVISORS = {h.name: Hypervisor(h) for h in API.list_hypervisors()}
LOG.info(f"Found {len(HYPERVISORS)} hypervisor(s)")
LOG.info("Get list of servers (this may take a while)")
SERVERS = [
Server(h) for h in API.list_servers(all_projects=True, detailed=ARGS.detailed)
]
LOG.info(f"Found {len(SERVERS)} server(s)")
if not ARGS.stopped and not ARGS.csv:
HYPERVISORS.pop(None, None)
HOSTS = list(HYPERVISORS.values())
hosts_len = max(len(x.name) for x in HOSTS)
names_len = 0
for s in SERVERS:
if s.host in HYPERVISORS:
HYPERVISORS[s.host].append(s)
else:
LOG.warning(f"No hypervisors found [{s.host}] for server {s.name}")
names_len = max(names_len, len(s.name))
hosts_len = max(hosts_len, names_len) + 3
def sorter(x):
return getattr(x, ARGS.sort)
def display(HOSTS):
total = 0
free = 0
total_cpus = 0
total_cpus_used = 0
for host in sorted(
HOSTS,
key=lambda x: sorter(x),
reverse=ARGS.reverse,
):
total += host.memory_size
free += host.memory_free
print(
host.name.ljust(hosts_len),
bytes_to_string(host.memory_size),
"free",
bytes_to_string(host.memory_free),
"vcpus",
host.vcpus,
"used",
host.vcpus_used,
"up",
host.up,
"load average %s, %s, %s" % host.load_average,
)
total_cpus += host.vcpus
total_cpus_used += host.vcpus_used
print(
" ",
"".ljust(names_len),
"mem".rjust(4),
"cpu".rjust(3),
"rd".rjust(7),
"wr".rjust(7),
"rx".rjust(7),
"tx".rjust(7),
)
for vm in sorted(host.servers, key=lambda x: x.name):
print(
" ",
vm.name.ljust(names_len),
bytes_to_string(vm.memory_size).rjust(4),
str(vm.vcpus).rjust(3),
bytes_to_string(vm.read, "-").rjust(7),
bytes_to_string(vm.write, "-").rjust(7),
bytes_to_string(vm.rx, "-").rjust(7),
bytes_to_string(vm.tx, "-").rjust(7),
vm.id,
vm.status,
)
print(
" ",
"".ljust(names_len),
"".rjust(4),
"".rjust(3),
bytes_to_string(host.read, "-").rjust(7),
bytes_to_string(host.write, "-").rjust(7),
bytes_to_string(host.rx, "-").rjust(7),
bytes_to_string(host.tx, "-").rjust(7),
bytes_to_string(host.io, "-"),
)
print()
print("----")
print(
"Total",
bytes_to_string(total),
", free",
bytes_to_string(free),
", CPUs",
total_cpus,
", CPUs used",
total_cpus_used,
)
HYPERVISORS = list(HYPERVISORS.values())
VMS = []
for host in HYPERVISORS:
VMS += host.servers
if ARGS.csv:
print(
"project",
"name",
"status",
"user",
"memory_size",
"vcpus",
"accessIPv4",
"dns",
"created",
"updated",
"description",
"id",
"flavor",
"image",
"comment",
sep=",",
)
for vm in sorted(VMS, key=lambda x: (x.status, x.project_name, x.name)):
if ARGS.status is not None:
if vm.status.lower() != ARGS.status.lower():
continue
# print(dir(vm))
print(
vm.project_name,
vm.name,
vm.status,
vm.user_name,
vm.memory_size,
vm.vcpus,
vm.floating,
vm.dns,
vm.created,
vm.updated,
vm.description,
vm.id,
vm.flavor_name,
vm.image_name,
vm.comment,
sep=",",
)
exit(0)
if ARGS.list:
for vm in sorted(VMS, key=lambda x: (x.group, x.name)):
print(vm.group, vm.name, f"({vm.description})", sep="\t")
exit(0)
AFFINITY = {
"always_with": 1,
"never_with": -1,
}
def affinity(a, b):
name_a = a.name
name_b = b.name
for k, v in CONFIG["affinity"].items():
ma = re.match(f"^{k}$", name_a)
if ma:
for position in ("always_with", "never_with"):
if position in v:
mb = re.match(f"^{v[position]}$", name_b)
if mb:
if ma.groups() == mb.groups():
return AFFINITY[position]
return 0
class GAHost:
def __init__(self, host):
self._host = host
self.default = self._host.default
self.vcpus = self._host.vcpus
self.name = self._host.name
self.memory_size = round_down(self._host.memory_size)
self.number = int(re.search(r"(\d+)", self.name).group(1))
def finalise(self):
self.memory_free = self.memory_size
self.memory_used = 0
self.vcpus_used = 0
self.rx = 0
self.tx = 0
self.read = 0
self.write = 0
for s in self.servers:
memory_size = round_up(s.memory_size)
self.memory_used += memory_size
self.memory_free -= memory_size
self.vcpus_used += s.vcpus
self.rx += s.rx
self.tx += s.tx
self.read += s.read
self.write += s.write
self.io = self.tx + self.rx + self.read + self.write
class Cost:
pass
def placement():
import pygad
num_hosts = len(HYPERVISORS)
num_vms = len(VMS)
head_room = ARGS.head_room * 1024 * 1024 * 1024
if head_room:
LOG.info(f"Headroom {bytes_to_string(head_room)}")
make_space = ARGS.make_space * 1024 * 1024 * 1024
if make_space:
LOG.info(f"Make space for {bytes_to_string(make_space)}")
num_genes = num_vms
moves_weight = ARGS.moves_weight
current = np.zeros(num_genes, dtype=np.int64)
v = 0
for i, target in enumerate(HYPERVISORS):
for vm in target.servers:
current[v] = i
v += 1
love = set()
hate = set()
ga_hosts = [GAHost(h) for h in HYPERVISORS]
for i, v in enumerate(VMS):
for j, w in enumerate(VMS):
if j > i:
a = affinity(v, w)
if a > 0:
love.add((i, j))
if a < 0:
hate.add((i, j))
a = affinity(w, v)
if a > 0:
love.add((i, j))
if a < 0:
hate.add((i, j))
print("num_hosts", num_hosts)
print("num_vms", num_vms)
def placement_cost(solution, report=False):
hard_violations = 0
soft_violations = 0
location = defaultdict(set)
collocated = set()
moves = 0
appart = 0
same = 0
extra_cpus = []
cpus = []
for a, b in zip(current, solution):
if a != b:
moves += 1
for h in ga_hosts:
h.servers = []
for vm, host in enumerate(solution):
ga_hosts[host].servers.append(VMS[vm])
others = location[host]
for o in others:
if o < vm:
collocated.add((o, vm))
else:
collocated.add((vm, o))
location[host].add(vm)
for h in ga_hosts:
h.finalise()
for h in ga_hosts:
if h.memory_free < head_room:
hard_violations += 1
cpus.append(h.vcpus_used)
if h.vcpus_used > h.vcpus:
extra_cpus.append(h.vcpus_used - h.vcpus)
else:
extra_cpus.append(0)
soft_violations += np.std(cpus)
for h in hate:
if h in collocated:
hard_violations += 1
same += 1
for h in love:
if h not in collocated:
hard_violations += 1
appart += 1
##########################################################
movements = []
for i, (a, b) in enumerate(zip(current, solution)):
if a != b and VMS[i].memory_size != 0:
movements.append(
(
VMS[i].memory_size,
ga_hosts[b].name,
VMS[i].host,
)
)
sizes = {h.name: h.memory_free for h in ga_hosts}
for memory, target, source in movements:
sizes[source] += memory
sizes[target] -= memory
for s in sizes.values():
if s < 0:
hard_violations += 1
##########################################################
if report:
print("moves", moves)
print("cpus", max(extra_cpus), np.std(cpus), extra_cpus)
print("appart", appart, len(love))
for h in love:
if h not in collocated:
print(" Not collocated", VMS[h[0]].name, VMS[h[1]].name)
print("same", same, len(hate))
for h in hate:
if h in collocated:
print(" Not separated", VMS[h[0]].name, VMS[h[1]].name)
return hard_violations * 100000 + soft_violations * 1000 + moves * moves_weight
def make_space_cost(solution, report=False):
moves = 0
soft_violations = 0
for a, b in zip(current, solution):
if a != b:
moves += 1
hard_violations = 0
for h in ga_hosts:
h.servers = []
for vm, host in enumerate(solution):
ga_hosts[host].servers.append(VMS[vm])
for h in ga_hosts:
h.finalise()
for h in ga_hosts:
if h.memory_free < head_room:
hard_violations += 1
if h.vcpus_used > 2 * h.vcpus:
hard_violations += 1
##########################################################
hosts_with_space = 0
for h in ga_hosts:
if h.memory_free >= make_space + head_room:
hosts_with_space += 1
if hosts_with_space == 0:
hard_violations += 1000
##########################################################
movements = []
for i, (a, b) in enumerate(zip(current, solution)):
if a != b and VMS[i].memory_size != 0:
movements.append(
(
VMS[i].memory_size,
ga_hosts[b].name,
VMS[i].host,
)
)
sizes = {h.name: h.memory_free for h in ga_hosts}
for memory, target, source in movements:
sizes[source] += memory
sizes[target] -= memory
for s in sizes.values():
if s < 0:
hard_violations += 1
##########################################################
if report:
print("moves", moves)
print("hosts with space", hosts_with_space)
return hard_violations * 100000 + soft_violations * 10 + moves * moves_weight
cost = make_space_cost if ARGS.make_space else placement_cost
print("COST", cost)
def fitness(solution, solution_idx):
return -cost(solution)
print("Before")
c = cost(current, True)
print("cost", c)
# Start with current position so we minimize the moves
pop_size = ARGS.population
population = np.zeros((pop_size, num_genes))
for i in range(pop_size):
if i == 0:
population[i, :] = current
else:
population[i, :] = current
random.shuffle(population[i, :])
if ARGS.random:
population = None
num_generations = ARGS.generations
with tqdm(total=num_generations) as pbar:
ga_instance = pygad.GA(
num_generations=num_generations,
num_parents_mating=max(2, int(pop_size * 0.05)),
fitness_func=fitness,
num_genes=num_genes,
sol_per_pop=pop_size,
initial_population=population,
parent_selection_type=ARGS.parent_selection_type,
crossover_type=ARGS.crossover_type,
mutation_type=ARGS.mutation_type,
mutation_num_genes=3,
mutation_by_replacement=True,
gene_type=int,
gene_space=dict(low=0, high=num_hosts),
on_generation=lambda _: pbar.update(1),
)
ga_instance.run()
solution, _, _ = ga_instance.best_solution()
print("After")
c = cost(solution, True)
print("cost", c)
# print(solution)
if ARGS.plot:
ga_instance.plot_fitness()
#
moves = []
sizes = {h.name: h.memory_free for h in ga_hosts}
# LOG.info("SIZE %s", sizes)
for i, (a, b) in enumerate(zip(current, solution)):
if a != b:
if VMS[i].status != "ACTIVE":
LOG.warning(f"Ignoring '{VMS[i].name}' with status {VMS[i].status}")
continue
moves.append((VMS[i], ga_hosts[b].name))
for server, target in moves:
id, name, source, memory = (
server.id,
server.name,
server.host,
server.memory_size,
)
LOG.info(
(
f"Moving {name} (mem={bytes_to_string(memory)}) "
f'{source.split(".")[0]} (free={bytes_to_string(sizes[source])}) => '
f'{target.split(".")[0]} (free={bytes_to_string(sizes[target])})"'
)
)
if ARGS.doit:
try:
API.compute.live_migrate_server(id, host=target)
if not ARGS.nowait:
API.compute.wait_for_server(server._openstack_object)
LOG.info("Move successful")
except (
openstack.exceptions.ConflictException,
openstack.exceptions.BadRequestException,
) as e:
LOG.error(e)
LOG.error("Move failed")
with open("moves.sh", "w") as f:
print("export OS_COMPUTE_API_VERSION=2.30", file=f)
print(f"# moves: {len(moves)}", file=f)
for i, (server, target) in enumerate(moves):
id, name, source, memory = (
server.id,
server.name,
server.host,
server.memory_size,
)
print("date +%H:%M:%S", file=f)
print(
(
f'echo "{i+1}/{len(moves)} {name} (vm={bytes_to_string(memory)}) '
f'{source.split(".")[0]} ({bytes_to_string(sizes[source])}) => '
f'{target.split(".")[0]} ({bytes_to_string(sizes[target])})"'
),
file=f,
)
wait = "" if ARGS.nowait else "--wait"
print(
f"openstack server migrate {wait} --live-migration --host {target} {id}",
file=f,
)
sizes[source] += memory
sizes[target] -= memory
return ga_hosts
if ARGS.placement:
placement()
else:
display(HYPERVISORS)