---

# can be configured by tuned profile sap-hana, entry "energy_perf_bias=performance"
#   in included tuned profile throughput-performance
# More details on EPB are in: https://github.com/torvalds/linux/blob/be1fcde604e429691771ce70230668af8097e29b/arch/x86/kernel/cpu/intel_epb.c
- name: Assert - Perform steps for checking EPB
  block:
    - name: Check if system supports Intel's performance bias setting (x86_64 platform only)
      shell: LC_ALL=C cpupower info | head -1 | awk '{print $2, $3, $4}'
      check_mode: no
      register: __sap_hana_preconfigure_register_cpupower_info_epb_supported_assert
      ignore_errors: true
      changed_when: no

    - name: Assert - Display the output of the cpupower info command
      debug:
        var: __sap_hana_preconfigure_register_cpupower_info_epb_supported_assert.stdout_lines,
             __sap_hana_preconfigure_register_cpupower_info_epb_supported_assert.stderr_lines

    - name: Check EPB if possible (x86_64 platform only)
      block:
        - name: Get info about file /etc/init.d/boot.local
          stat:
            path: /etc/init.d/boot.local
          register: __sap_hana_preconfigure_register_stat_boot_local_epb_assert

        - name: Assert that file /etc/init.d/boot.local exists (x86_64 platform only)
          assert:
            that: __sap_hana_preconfigure_register_stat_boot_local_epb_assert.stat.exists
            fail_msg: "FAIL: File /etc/init.d/boot.local does not exist!"
            success_msg: "PASS: File /etc/init.d/boot.local exist."
          ignore_errors: "{{ sap_hana_preconfigure_assert_ignore_errors|d(false) }}"

        - name: Assert that file /etc/init.d/boot.local is a regular file (x86_64 platform only)
          assert:
            that: __sap_hana_preconfigure_register_stat_boot_local_epb_assert.stat.isreg
            fail_msg: "FAIL: File /etc/init.d/boot.local is not a regular file!"
            success_msg: "PASS: File /etc/init.d/boot.local is a regular file."
          ignore_errors: "{{ sap_hana_preconfigure_assert_ignore_errors|d(false) }}"
          when: __sap_hana_preconfigure_register_stat_boot_local_epb_assert.stat.exists

        - name: Assert that the mode of file /etc/init.d/boot.local is 0755 (x86_64 platform only)
          assert:
            that: "__sap_hana_preconfigure_register_stat_boot_local_epb_assert.stat.mode == '0755'"
            fail_msg: "FAIL: File /etc/init.d/boot.local has mode '{{ __sap_hana_preconfigure_register_stat_boot_local_epb_assert.stat.mode }}'
                       but the expected mode is '0755'!"
            success_msg: "PASS: File /etc/init.d/boot.local has mode '0755'."
          ignore_errors: "{{ sap_hana_preconfigure_assert_ignore_errors|d(false) }}"
          when: __sap_hana_preconfigure_register_stat_boot_local_epb_assert.stat.exists

        - name: Get the contents of file /etc/init.d/boot.local (x86_64 platform only)
          command: grep cpupower /etc/init.d/boot.local
          register: __sap_hana_preconfigure_register_boot_local_cpupower_assert
          changed_when: no
          ignore_errors: yes
          when: __sap_hana_preconfigure_register_stat_boot_local_epb_assert.stat.exists

        - name: Assert that EPB is configured at boot time (x86_64 platform only)
          assert:
            that: "__sap_hana_preconfigure_register_boot_local_cpupower_assert.stdout == 'cpupower set -b 0'"
            fail_msg: "FAIL: EPB is not configured to bet set to '0' ('performance') at boot time!"
            success_msg: "PASS: EPB is configured to bet set to '0' ('performance') at boot time."
          ignore_errors: "{{ sap_hana_preconfigure_assert_ignore_errors|d(false) }}"
          when: __sap_hana_preconfigure_register_stat_boot_local_epb_assert.stat.exists

        - name: Get the current setting of EPB (x86_64 platform only)
          shell: cpupower info -b | awk '/perf-bias/{print $NF}'
          register: __sap_hana_preconfigure_register_cpupower_info_epb_assert
          changed_when: no
          ignore_errors: yes

        - name: Assert - Display the output of the cpupower info command again
          debug:
            var: __sap_hana_preconfigure_register_cpupower_info_epb_assert

        - name: Assert that the current setting of EPB is '0' (x86_64 platform only)
          assert:
            that: "__sap_hana_preconfigure_register_cpupower_info_epb_assert.stdout == '0'"
            fail_msg: "FAIL: EPB is currently not set to '0' ('performance')!"
            success_msg: "PASS: EPB is currently set to '0' ('performance')."
          ignore_errors: "{{ sap_hana_preconfigure_assert_ignore_errors|d(false) }}"

      when: "__sap_hana_preconfigure_register_cpupower_info_epb_supported_assert.stdout != 'does not support'"

    - name: Report if the system does not support EPB
      debug:
        msg: "INFO: The system does not support Intel's performance bias setting (EPB)."
      when: "__sap_hana_preconfigure_register_cpupower_info_epb_supported_assert.stdout == 'does not support'"

  when: ansible_architecture == 'x86_64' and
        (not sap_hana_preconfigure_use_tuned or
           sap_hana_preconfigure_assert_all_config
        )
