Make own CPU allocation for CaaS default CPU pool
[ta/infra-ansible.git] / roles / allocate_cpu_cores / tasks / main.yml
1 ---
2 # Copyright 2019 Nokia
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16
17 - name: Prepare.
18   set_fact:
19     cpu_remain: []
20     cpu_sets: {}
21
22 - name: Get CPU topology.
23   get_cpu_topology:
24     var: cpu_topology
25 - debug:
26     var: cpu_topology
27
28 - name: Make default performance profile.
29   set_fact:
30     perf_profile: {
31       platform_cpus: "{{ cpu_topology | cpu_topology_defaults(hosts[hostname]['service_profiles'], platform_cpus, virtual_env, hosts | length < 2) }}"
32     }
33 - debug:
34     var: perf_profile
35
36 - name: Get performance profile.
37   set_fact:
38     perf_profile: "{{ perf_profile | combine(performance_profiles[hosts[hostname]['performance_profiles'][0]]) }}"
39   when: "'performance_profiles' in hosts[hostname]"
40 - debug:
41     var: perf_profile
42
43 - name: Make CPU sets.
44   include_tasks: make_cpu_sets.yaml
45   loop_control:
46     loop_var: cpu_set
47   with_items:
48       - { 'name': 'ovs_dpdk', 'opt': "{{ cpu_set_mapping['ovs_dpdk']['opt'] }}", 'where': 'tail' }
49       - { 'name': 'platform', 'opt': "{{ cpu_set_mapping['platform']['opt'] }}", 'where': 'head' }
50   when: "cpu_set.opt in perf_profile"
51
52 - name: Initialize VM CPU share.
53   set_fact:
54     vm_share_percent: 100
55
56 - name: Set collocated VM CPU set.
57   set_fact:
58     vm_share_percent: "{{ 100 - (perf_profile['caas_cpu_pool_share'] | default(caas_cpu_pool_share_default) | int) }}"
59   when:
60     - "'compute' in hosts[hostname]['service_profiles']"
61     - hosts[hostname]['service_profiles'] | intersect(caas_service_profiles)
62
63 - name: Make VM CPU sets.
64   include_tasks: make_cpu_sets.yaml
65   loop_control:
66     loop_var: cpu_set
67   with_items:
68     - { 'name': 'vm', 'request': "{{ cpu_topology | get_cpu_count_by_percent(vm_share_percent) }}", 'where': 'tail' }
69   when:
70     - "'compute' in hosts[hostname]['service_profiles']"
71
72 - name: Make CaaS default CPU set.
73   include_tasks: make_cpu_sets.yaml
74   loop_control:
75     loop_var: cpu_set
76   with_items:
77     - { 'name': 'caas_default', 'request': "{{ cpu_topology | cpu_topology_defaults(hosts[hostname]['service_profiles'], caas_default_pool_cpus, virtual_env, hosts | length < 2) }}", 'where': 'tail' }
78   when:
79     - hosts[hostname]['service_profiles'] | intersect(caas_service_profiles)
80
81 - name: Calculate CaaS CPU counts
82   set_fact:
83     perf_profile: "{{ perf_profile | combine({ item.key: cpu_topology | get_cpu_count_by_percent(item.value) }) }}"
84   with_dict: "{{ perf_profile['caas_cpu_pools'] | default({}) }}"
85   when: hosts[hostname]['service_profiles'] | intersect(caas_service_profiles)
86
87 - name: Make CaaS CPU sets.
88   include_tasks: make_cpu_sets.yaml
89   loop_control:
90     loop_var: cpu_set
91   with_items:
92     - { 'name': 'caas_exclusive', 'opt': "{{ cpu_set_mapping['caas_exclusive']['opt'] }}", 'where': 'tail' }
93     - { 'name': 'caas_shared',    'opt': "{{ cpu_set_mapping['caas_shared']['opt'] }}",    'where': 'tail' }
94   when:
95     - "cpu_set.opt in perf_profile"
96     - hosts[hostname]['service_profiles'] | intersect(caas_service_profiles)
97
98 - name: Make set of remaining CPUs.
99   set_fact:
100     cpu_remain: "{{ cpu_remain | union(item) }}"
101   with_items: "{{ cpu_topology.values() }}"
102 - debug:
103     var: cpu_remain
104
105 - name: Allocate remaining CPUs to CaaS default.
106   set_fact:
107     cpu_sets: "{{ cpu_sets | combine({ 'caas_default': cpu_sets['caas_default'] | default([]) | union(cpu_remain) }) }}"
108     cpu_remain: []
109   when:
110     - "cpu_remain | length > 0"
111     - hosts[hostname]['service_profiles'] | intersect(caas_service_profiles)
112
113 - name: Allocate remaining CPUs to platform.
114   set_fact:
115     cpu_sets: "{{ cpu_sets | combine({ 'platform': cpu_sets['platform'] | default([]) | union(cpu_remain) }) }}"
116     cpu_remain: []
117   when:
118     - "cpu_remain | length > 0"
119     - not hosts[hostname]['service_profiles'] | intersect(caas_service_profiles)
120
121 - debug:
122     var: cpu_sets
123
124 - name: Make sure facts directory exists.
125   file:
126     path: "{{ allocation_fact_file | dirname }}"
127     state: directory
128     mode: 0755
129
130 - name: Save CPU allocation.
131   template:
132     src: cpu_allocation.fact.j2
133     dest: "{{ allocation_fact_file }}"
134   register: cpu_allocation
135
136 - name: Refresh facts.
137   setup:
138   when:
139     - cpu_allocation.changed
140     - not skip_facts_reload | default(False)