|
| 1 | +(* |
| 2 | + * Copyright (C) 2006-2009 Citrix Systems Inc. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published |
| 6 | + * by the Free Software Foundation; version 2.1 only. with the special |
| 7 | + * exception on linking described in file LICENSE. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Lesser General Public License for more details. |
| 13 | + *) |
| 14 | +(** Functions relating to memory requirements of Xen domains *) |
| 15 | + |
| 16 | +let ( +++ ) = Int64.add |
| 17 | +let ( --- ) = Int64.sub |
| 18 | +let ( *** ) = Int64.mul |
| 19 | +let ( /// ) = Int64.div |
| 20 | + |
| 21 | +(* === Memory conversion factors ============================================ *) |
| 22 | + |
| 23 | +let bytes_per_kib = 1024L |
| 24 | +let bytes_per_mib = 1048576L |
| 25 | +let bytes_per_page = 4096L |
| 26 | +let kib_per_page = bytes_per_page /// bytes_per_kib |
| 27 | +let kib_per_mib = 1024L |
| 28 | +let pages_per_mib = bytes_per_mib /// bytes_per_page |
| 29 | + |
| 30 | +(* === Arithmetic functions ================================================= *) |
| 31 | + |
| 32 | +(** Returns true if (and only if) the specified argument is a power of 2. *) |
| 33 | +let is_power_of_2 n = |
| 34 | + (n > 0) && (n land (0 - n) = n) |
| 35 | + |
| 36 | +let round_down_to_multiple_of x y = |
| 37 | + (x /// y) *** y |
| 38 | + |
| 39 | +let round_up_to_multiple_of x y = |
| 40 | + ((x +++ y --- 1L) /// y) *** y |
| 41 | + |
| 42 | +(* === Memory rounding functions ============================================ *) |
| 43 | + |
| 44 | +let round_up = round_up_to_multiple_of |
| 45 | +let round_down = round_down_to_multiple_of |
| 46 | + |
| 47 | +let round_bytes_down_to_nearest_page_boundary v = round_down v bytes_per_page |
| 48 | +let round_bytes_down_to_nearest_mib v = round_down v bytes_per_mib |
| 49 | +let round_kib_down_to_nearest_page_boundary v = round_down v kib_per_page |
| 50 | +let round_kib_up_to_nearest_page_boundary v = round_up v kib_per_page |
| 51 | +let round_kib_up_to_nearest_mib v = round_up v kib_per_mib |
| 52 | +let round_pages_up_to_nearest_mib v = round_up v pages_per_mib |
| 53 | + |
| 54 | +(* === Division functions =================================================== *) |
| 55 | + |
| 56 | +let divide_rounding_down numerator denominator = |
| 57 | + numerator /// denominator |
| 58 | + |
| 59 | +let divide_rounding_up numerator denominator = |
| 60 | + (numerator +++ denominator --- 1L) /// denominator |
| 61 | + |
| 62 | +(* === Memory unit conversion functions ===================================== *) |
| 63 | + |
| 64 | +let bytes_of_kib value = value *** bytes_per_kib |
| 65 | +let bytes_of_pages value = value *** bytes_per_page |
| 66 | +let bytes_of_mib value = value *** bytes_per_mib |
| 67 | +let kib_of_mib value = value *** kib_per_mib |
| 68 | +let kib_of_pages value = value *** kib_per_page |
| 69 | +let pages_of_mib value = value *** pages_per_mib |
| 70 | + |
| 71 | +let kib_of_bytes_free value = divide_rounding_down value bytes_per_kib |
| 72 | +let pages_of_bytes_free value = divide_rounding_down value bytes_per_page |
| 73 | +let pages_of_kib_free value = divide_rounding_down value kib_per_page |
| 74 | +let mib_of_bytes_free value = divide_rounding_down value bytes_per_mib |
| 75 | +let mib_of_kib_free value = divide_rounding_down value kib_per_mib |
| 76 | +let mib_of_pages_free value = divide_rounding_down value pages_per_mib |
| 77 | + |
| 78 | +let kib_of_bytes_used value = divide_rounding_up value bytes_per_kib |
| 79 | +let pages_of_bytes_used value = divide_rounding_up value bytes_per_page |
| 80 | +let pages_of_kib_used value = divide_rounding_up value kib_per_page |
| 81 | +let mib_of_bytes_used value = divide_rounding_up value bytes_per_mib |
| 82 | +let mib_of_kib_used value = divide_rounding_up value kib_per_mib |
| 83 | +let mib_of_pages_used value = divide_rounding_up value pages_per_mib |
| 84 | + |
| 85 | +(* === Domain memory breakdown ============================================== *) |
| 86 | + |
| 87 | +(* ╤ ╔══════════╗ ╤ *) |
| 88 | +(* │ ║ shadow ║ │ *) |
| 89 | +(* │ ╠══════════╣ │ *) |
| 90 | +(* overhead │ ║ extra ║ │ *) |
| 91 | +(* │ ║ external ║ │ *) |
| 92 | +(* │ ╠══════════╣ ╤ │ *) |
| 93 | +(* │ ║ extra ║ │ │ *) |
| 94 | +(* │ ║ internal ║ │ │ *) |
| 95 | +(* ╪ ╠══════════╣ ╤ │ │ footprint *) |
| 96 | +(* │ ║ video ║ │ │ │ *) |
| 97 | +(* │ ╠══════════╣ ╤ ╤ │ actual │ xen │ *) |
| 98 | +(* │ ║ ║ │ │ │ / │ maximum │ *) |
| 99 | +(* │ ║ ║ │ │ │ target │ │ *) |
| 100 | +(* │ ║ guest ║ │ │ build │ / │ │ *) |
| 101 | +(* │ ║ ║ │ │ start │ total │ │ *) |
| 102 | +(* static │ ║ ║ │ │ │ │ │ *) |
| 103 | +(* maximum │ ╟──────────╢ │ ╧ ╧ ╧ ╧ *) |
| 104 | +(* │ ║ ║ │ *) |
| 105 | +(* │ ║ ║ │ *) |
| 106 | +(* │ ║ balloon ║ │ build *) |
| 107 | +(* │ ║ ║ │ maximum *) |
| 108 | +(* │ ║ ║ │ *) |
| 109 | +(* ╧ ╚══════════╝ ╧ *) |
| 110 | + |
| 111 | +(* === Domain memory breakdown: HVM guests ================================== *) |
| 112 | + |
| 113 | +module type MEMORY_MODEL_DATA = sig |
| 114 | + val extra_internal_mib : int64 |
| 115 | + val extra_external_mib : int64 |
| 116 | +end |
| 117 | + |
| 118 | +module HVM_memory_model_data : MEMORY_MODEL_DATA = struct |
| 119 | + let extra_internal_mib = 1L |
| 120 | + let extra_external_mib = 1L |
| 121 | +end |
| 122 | + |
| 123 | +module Linux_memory_model_data : MEMORY_MODEL_DATA = struct |
| 124 | + let extra_internal_mib = 0L |
| 125 | + let extra_external_mib = 1L |
| 126 | +end |
| 127 | + |
| 128 | +type memory_config = { |
| 129 | + build_max_mib : int64; |
| 130 | + build_start_mib : int64; |
| 131 | + xen_max_mib : int64; |
| 132 | + shadow_mib : int64; |
| 133 | + required_host_free_mib : int64; |
| 134 | +} |
| 135 | + |
| 136 | +module Memory_model (D : MEMORY_MODEL_DATA) = struct |
| 137 | + |
| 138 | + let build_max_mib static_max_mib video_mib = static_max_mib --- (Int64.of_int video_mib) |
| 139 | + |
| 140 | + let build_start_mib target_mib video_mib = target_mib --- (Int64.of_int video_mib) |
| 141 | + |
| 142 | + let xen_max_offset_mib = D.extra_internal_mib |
| 143 | + |
| 144 | + let xen_max_mib target_mib = target_mib +++ xen_max_offset_mib |
| 145 | + |
| 146 | + let shadow_mib static_max_mib vcpu_count multiplier = |
| 147 | + let vcpu_pages = 256L *** (Int64.of_int vcpu_count) in |
| 148 | + let p2m_map_pages = static_max_mib in |
| 149 | + let shadow_resident_pages = static_max_mib in |
| 150 | + let total_mib = mib_of_pages_used |
| 151 | + (vcpu_pages +++ p2m_map_pages +++ shadow_resident_pages) in |
| 152 | + let total_mib_multiplied = |
| 153 | + Int64.of_float ((Int64.to_float total_mib) *. multiplier) in |
| 154 | + max 1L total_mib_multiplied |
| 155 | + |
| 156 | + let overhead_mib static_max_mib vcpu_count multiplier = |
| 157 | + D.extra_internal_mib +++ |
| 158 | + D.extra_external_mib +++ |
| 159 | + (shadow_mib static_max_mib vcpu_count multiplier) |
| 160 | + |
| 161 | + let footprint_mib target_mib static_max_mib vcpu_count multiplier = |
| 162 | + target_mib +++ (overhead_mib static_max_mib vcpu_count multiplier) |
| 163 | + |
| 164 | + let shadow_multiplier_default = 1.0 |
| 165 | + |
| 166 | + let full_config static_max_mib video_mib target_mib vcpus shadow_multiplier = |
| 167 | + { |
| 168 | + build_max_mib = build_max_mib static_max_mib video_mib; |
| 169 | + build_start_mib = build_start_mib target_mib video_mib; |
| 170 | + xen_max_mib = xen_max_mib static_max_mib; |
| 171 | + shadow_mib = shadow_mib static_max_mib vcpus shadow_multiplier; |
| 172 | + required_host_free_mib = footprint_mib target_mib static_max_mib vcpus shadow_multiplier; |
| 173 | + } |
| 174 | +end |
| 175 | + |
| 176 | +module HVM = Memory_model (HVM_memory_model_data) |
| 177 | +module Linux = Memory_model (Linux_memory_model_data) |
| 178 | + |
| 179 | + |
0 commit comments