From 2218cda8d53bde8a186efff54404c0b5be7a7abc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edwin=20T=C3=B6r=C3=B6k?= Date: Thu, 22 Aug 2024 16:27:10 +0100 Subject: [PATCH] CP-52709: autotune service bindir --- ocaml/hwtune/dune | 5 +++++ ocaml/hwtune/xshwtune.ml | 44 ++++++++++++++++++++++++++++++++++++++++ scripts/Makefile | 1 + scripts/xshwtune.service | 15 ++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 ocaml/hwtune/dune create mode 100644 ocaml/hwtune/xshwtune.ml create mode 100644 scripts/xshwtune.service diff --git a/ocaml/hwtune/dune b/ocaml/hwtune/dune new file mode 100644 index 00000000000..37139c593c8 --- /dev/null +++ b/ocaml/hwtune/dune @@ -0,0 +1,5 @@ +(executable + (public_name xshwtune) + (libraries xapi_timeslice unix xapi-log) + (package xapi) +) diff --git a/ocaml/hwtune/xshwtune.ml b/ocaml/hwtune/xshwtune.ml new file mode 100644 index 00000000000..a46237442ef --- /dev/null +++ b/ocaml/hwtune/xshwtune.ml @@ -0,0 +1,44 @@ +(* Tunes parameters for the hardware and kernel that we are currently running on. *) + +module D = Debug.Make (struct let name = "xshwtune" end) + +let () = + Debug.log_to_stdout () ; + let recommended = Xapi_timeslice.Recommended.measure () in + D.debug "Recommended OCaml timeslice: %.6fs" recommended ; + + let itimer_min = + Sys.set_signal Sys.sigvtalrm Sys.Signal_ignore ; + let _ = + Unix.setitimer Unix.ITIMER_VIRTUAL + Unix.{it_value= 1e-6; it_interval= 1e-6} + in + let actual = Unix.getitimer Unix.ITIMER_VIRTUAL in + actual.Unix.it_value + in + + D.debug "POSIX itimer granularity: %.6fs" itimer_min ; + let recommended = Float.max itimer_min recommended in + + (* just in case something goes very wrong, ensure it is not too small or big *) + let recommended = recommended |> Float.max 0.001 |> Float.min 0.050 in + D.debug "Adjusted timeslice: %.6fs" recommended ; + + (* Use consistent rounding in debug messages and conf file, + by converting to string in a single place. + *) + let recommended = Printf.sprintf "%.3f" recommended in + + D.info "OCaml timeslice: %s" recommended ; + + let write_conf path = + Out_channel.with_open_text path @@ fun ch -> + Printf.fprintf ch "timeslice=%s" recommended + in + + Array.iteri + (fun i arg -> + if i > 0 then + write_conf arg + ) + Sys.argv diff --git a/scripts/Makefile b/scripts/Makefile index 503e7838546..64980496000 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -58,6 +58,7 @@ install: $(IDATA) xapi-domains.service $(DESTDIR)/usr/lib/systemd/system/xapi-domains.service $(IDATA) generate-iscsi-iqn.service $(DESTDIR)/usr/lib/systemd/system/generate-iscsi-iqn.service $(IDATA) xapi.service $(DESTDIR)/usr/lib/systemd/system/xapi.service + $(IDATA) xshwtune.service $(DESTDIR)/usr/lib/systemd/system/xshwtune.service $(IDATA) attach-static-vdis.service $(DESTDIR)/usr/lib/systemd/system/attach-static-vdis.service $(IDATA) save-boot-info.service $(DESTDIR)/usr/lib/systemd/system/save-boot-info.service $(IDATA) mpathalert.service $(DESTDIR)/usr/lib/systemd/system/mpathalert.service diff --git a/scripts/xshwtune.service b/scripts/xshwtune.service new file mode 100644 index 00000000000..c6ad638726c --- /dev/null +++ b/scripts/xshwtune.service @@ -0,0 +1,15 @@ +[Unit] +Description=Tune system for current hardware and kernel +Before=xapi.service xenopsd-xc.service +ConditionPathExists=/etc/xenserver/features.d/ocamltimeslice +ConditionPathExists=!@ETCDIR@/xenopsd.conf.d/xshwtune.conf + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecCondition=/bin/bash -c '[ "$($