|
| 1 | +(* |
| 2 | + * Copyright (c) Cloud Software Group, 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 | + |
| 15 | +exception Invalid_cstate of (int option * int option) |
| 16 | + |
| 17 | +exception Invalid_cstate_string of string |
| 18 | + |
| 19 | +exception Parse_cmd_output_failure of string |
| 20 | + |
| 21 | +exception Command_failure of string |
| 22 | + |
| 23 | +let ( let* ) = Option.bind |
| 24 | + |
| 25 | +let valid cstate = Option.fold ~none:true ~some:(fun v -> v >= 0) cstate |
| 26 | + |
| 27 | +let raise_parse_fail s e = |
| 28 | + let msg = Printf.sprintf "output: %s; error: %s" s (Printexc.to_string e) in |
| 29 | + raise (Parse_cmd_output_failure msg) |
| 30 | + |
| 31 | +let to_string (cstate, sub_cstate) = |
| 32 | + match (cstate, sub_cstate) with |
| 33 | + | None, _ -> |
| 34 | + "" |
| 35 | + | Some v, None when v >= 0 -> |
| 36 | + Int.to_string v |
| 37 | + | Some v, Some sub_v when v >= 0 && sub_v >= 0 -> |
| 38 | + Printf.sprintf "%d,%d" v sub_v |
| 39 | + | _ -> |
| 40 | + raise (Invalid_cstate (cstate, sub_cstate)) |
| 41 | + |
| 42 | +let of_string s = |
| 43 | + let cstate, sub_cstate = |
| 44 | + match String.split_on_char ',' s with |
| 45 | + | [""] -> |
| 46 | + (None, None) |
| 47 | + | [state] -> ( |
| 48 | + try (Some (int_of_string state), None) |
| 49 | + with _ -> raise (Invalid_cstate_string s) |
| 50 | + ) |
| 51 | + | [state; sub_state] -> ( |
| 52 | + try (Some (int_of_string state), Some (int_of_string sub_state)) |
| 53 | + with _ -> raise (Invalid_cstate_string s) |
| 54 | + ) |
| 55 | + | _ -> |
| 56 | + raise (Invalid_cstate_string s) |
| 57 | + in |
| 58 | + if valid cstate && valid sub_cstate then |
| 59 | + (cstate, sub_cstate) |
| 60 | + else |
| 61 | + raise (Invalid_cstate_string s) |
| 62 | + |
| 63 | +let parse_xenpm_response resp = |
| 64 | + let parse_max_cstate_line line = |
| 65 | + let line = String.trim line in |
| 66 | + let* cstate_word = |
| 67 | + try Scanf.sscanf line "max C-state set to %s" Option.some |
| 68 | + with e -> raise_parse_fail resp e |
| 69 | + in |
| 70 | + match cstate_word with |
| 71 | + | "unlimited" -> |
| 72 | + None |
| 73 | + | s -> ( |
| 74 | + try Scanf.sscanf s "C%d" Option.some with e -> raise_parse_fail resp e |
| 75 | + ) |
| 76 | + in |
| 77 | + let parse_sub_cstate_line line = |
| 78 | + let line = String.trim line in |
| 79 | + let* sub_cstate_word = |
| 80 | + try Scanf.sscanf line "max C-substate set to %s succeeded" Option.some |
| 81 | + with e -> raise_parse_fail resp e |
| 82 | + in |
| 83 | + match sub_cstate_word with |
| 84 | + | "unlimited" -> |
| 85 | + None |
| 86 | + | s -> ( |
| 87 | + try Some (int_of_string s) with e -> raise_parse_fail resp e |
| 88 | + ) |
| 89 | + in |
| 90 | + match String.split_on_char '\n' resp with |
| 91 | + | [] | [""] -> |
| 92 | + raise (Parse_cmd_output_failure resp) |
| 93 | + | cstate_line :: [] | [cstate_line; ""] -> |
| 94 | + (parse_max_cstate_line cstate_line, None) |
| 95 | + | cstate_line :: sub_cstate_line :: _ -> |
| 96 | + (parse_max_cstate_line cstate_line, parse_sub_cstate_line sub_cstate_line) |
| 97 | + |
| 98 | +let parse_xencmdline_response resp = |
| 99 | + (* the response may be |
| 100 | + "" -> unlimited |
| 101 | + "max_cstate=N" -> max cstate N |
| 102 | + "max_cstate=N,M" -> max cstate N, max c-sub-state M *) |
| 103 | + let resp = String.trim resp in |
| 104 | + match Astring.String.fields ~is_sep:(fun c -> c = '=' || c = ',') resp with |
| 105 | + | [""] -> |
| 106 | + (None, None) |
| 107 | + | ["max_cstate"; state] -> ( |
| 108 | + try (Some (int_of_string state), None) with e -> raise_parse_fail resp e |
| 109 | + ) |
| 110 | + | ["max_cstate"; state; sub_state] -> ( |
| 111 | + try (Some (int_of_string state), Some (int_of_string sub_state)) |
| 112 | + with e -> raise_parse_fail resp e |
| 113 | + ) |
| 114 | + | _ -> |
| 115 | + raise (Parse_cmd_output_failure resp) |
| 116 | + |
| 117 | +(* xenpm examples: |
| 118 | + # xenpm set-max-cstate 0 0 |
| 119 | + max C-state set to C0 |
| 120 | + max C-substate set to 0 succeeded |
| 121 | + # xenpm set-max-cstate 0 |
| 122 | + max C-state set to C0 |
| 123 | + max C-substate set to unlimited succeeded |
| 124 | + # xenpm set-max-cstate unlimited |
| 125 | + max C-state set to unlimited |
| 126 | + # xenpm set-max-cstate -1 |
| 127 | + Missing, excess, or invalid argument(s) |
| 128 | +*) |
| 129 | +let xenpm_set cstate sub_cstate = |
| 130 | + let args = |
| 131 | + match (cstate, sub_cstate) with |
| 132 | + | None, _ -> |
| 133 | + ["set-max-cstate"; "unlimited"] |
| 134 | + | Some v, None when v >= 0 -> |
| 135 | + ["set-max-cstate"; string_of_int v] |
| 136 | + | Some v, Some sub_v when v >= 0 && sub_v >= 0 -> |
| 137 | + ["set-max-cstate"; string_of_int v; string_of_int sub_v] |
| 138 | + | _ -> |
| 139 | + raise (Invalid_cstate (cstate, sub_cstate)) |
| 140 | + in |
| 141 | + let resp = |
| 142 | + try Helpers.call_script !Xapi_globs.xenpm_bin args |
| 143 | + with e -> |
| 144 | + raise |
| 145 | + (Command_failure |
| 146 | + (Printf.sprintf "xenpm_set failed: %s" (Printexc.to_string e)) |
| 147 | + ) |
| 148 | + in |
| 149 | + match parse_xenpm_response resp with |
| 150 | + | None, _ -> |
| 151 | + if cstate <> None then |
| 152 | + raise |
| 153 | + (Command_failure |
| 154 | + (Printf.sprintf "get unmatched value in xenpm output %s" resp) |
| 155 | + ) |
| 156 | + | cstate_in_resp, sub_cstate_in_resp -> |
| 157 | + if (cstate_in_resp, sub_cstate_in_resp) <> (cstate, sub_cstate) then |
| 158 | + raise |
| 159 | + (Command_failure |
| 160 | + (Printf.sprintf "get unmatched value in xenpm output %s" resp) |
| 161 | + ) |
| 162 | + |
| 163 | +let xen_cmdline_set cstate sub_cstate = |
| 164 | + let args = |
| 165 | + match (cstate, sub_cstate) with |
| 166 | + | None, _ -> |
| 167 | + ["--delete-xen"; "max_cstate"] |
| 168 | + | Some v, None when v >= 0 -> |
| 169 | + ["--set-xen"; Printf.sprintf "max_cstate=%d" v] |
| 170 | + | Some v, Some sub_v when v >= 0 && sub_v >= 0 -> |
| 171 | + ["--set-xen"; Printf.sprintf "max_cstate=%d,%d" v sub_v] |
| 172 | + | _ -> |
| 173 | + raise (Invalid_cstate (cstate, sub_cstate)) |
| 174 | + in |
| 175 | + try Helpers.call_script !Xapi_globs.xen_cmdline_script args |> ignore |
| 176 | + with e -> |
| 177 | + raise |
| 178 | + (Command_failure |
| 179 | + (Printf.sprintf "xen_cmdline_set failed: %s" (Printexc.to_string e)) |
| 180 | + ) |
| 181 | + |
| 182 | +let xen_cmdline_get () = |
| 183 | + let args = ["--get-xen"; "max_cstate"] in |
| 184 | + let resp = |
| 185 | + try Helpers.call_script !Xapi_globs.xen_cmdline_script args |
| 186 | + with e -> |
| 187 | + raise |
| 188 | + (Command_failure |
| 189 | + (Printf.sprintf "xen_cmdline_get failed: %s" (Printexc.to_string e)) |
| 190 | + ) |
| 191 | + in |
| 192 | + parse_xencmdline_response resp |
0 commit comments