git: 423b6f5be5e4 - main - benchmarks/mangohud: the port had been improved (+)

From: Alexey Dokuchaev <danfe_at_FreeBSD.org>
Date: Wed, 15 Dec 2021 09:48:04 UTC
The branch main has been updated by danfe:

URL: https://cgit.FreeBSD.org/ports/commit/?id=423b6f5be5e49123a4888fe201c47d9c0edddb5f

commit 423b6f5be5e49123a4888fe201c47d9c0edddb5f
Author:     Alexey Dokuchaev <danfe@FreeBSD.org>
AuthorDate: 2021-12-15 09:45:18 +0000
Commit:     Alexey Dokuchaev <danfe@FreeBSD.org>
CommitDate: 2021-12-15 09:45:18 +0000

    benchmarks/mangohud: the port had been improved (+)
    
    - Report CPU frequency and temperature on the HUD
    - For gathering system information, use sysctlbyname() if
      possible instead of capturing external programs' output
---
 benchmarks/mangohud/Makefile                       |  1 +
 benchmarks/mangohud/files/patch-src_cpu.cpp        | 72 ++++++++++++++++++++++
 .../mangohud/files/patch-src_file__utils.cpp       | 41 +++++++++++-
 benchmarks/mangohud/files/patch-src_file__utils.h  | 10 +++
 benchmarks/mangohud/files/patch-src_overlay.cpp    | 16 +++++
 benchmarks/mangohud/files/patch-src_vulkan.cpp     | 32 +++++-----
 6 files changed, 156 insertions(+), 16 deletions(-)

diff --git a/benchmarks/mangohud/Makefile b/benchmarks/mangohud/Makefile
index 2be3c8377b8f..3ba206c81ed3 100644
--- a/benchmarks/mangohud/Makefile
+++ b/benchmarks/mangohud/Makefile
@@ -2,6 +2,7 @@
 
 PORTNAME=	mangohud
 PORTVERSION=	0.6.5
+PORTREVISION=	1
 DISTVERSIONPREFIX=	v
 CATEGORIES=	benchmarks graphics
 MASTER_SITES=	https://wrapdb.mesonbuild.com/v1/projects/imgui/1.81/1/get_zip?dummy=/:igw
diff --git a/benchmarks/mangohud/files/patch-src_cpu.cpp b/benchmarks/mangohud/files/patch-src_cpu.cpp
new file mode 100644
index 000000000000..8f40b66beeeb
--- /dev/null
+++ b/benchmarks/mangohud/files/patch-src_cpu.cpp
@@ -0,0 +1,72 @@
+--- src/cpu.cpp.orig	2021-07-08 06:23:59 UTC
++++ src/cpu.cpp
+@@ -227,6 +227,7 @@ bool CPUStats::UpdateCPUData()
+ 
+ bool CPUStats::UpdateCoreMhz() {
+     m_coreMhz.clear();
++#if defined(__linux__)
+     std::ifstream cpuInfo(PROCCPUINFOFILE);
+     std::string row;
+     size_t i = 0;
+@@ -238,6 +239,13 @@ bool CPUStats::UpdateCoreMhz() {
+             i++;
+         }
+     }
++#elif defined(__FreeBSD__)
++    char sysctl_name[32];
++    for (int i = 0; i < m_cpuData.size(); i++) {
++        snprintf(sysctl_name, sizeof(sysctl_name), "dev.cpu.%d.freq", i);
++        m_cpuData[i].mhz = read_sysctl<int>(sysctl_name);
++    }
++#endif
+     m_cpuDataTotal.cpu_mhz = 0;
+     for (auto data : m_cpuData)
+         m_cpuDataTotal.cpu_mhz += data.mhz;
+@@ -246,6 +254,7 @@ bool CPUStats::UpdateCoreMhz() {
+ }
+ 
+ bool CPUStats::UpdateCpuTemp() {
++#if defined(__linux__)
+     if (!m_cpuTempFile)
+         return false;
+ 
+@@ -256,6 +265,23 @@ bool CPUStats::UpdateCpuTemp() {
+     m_cpuDataTotal.temp = temp / 1000;
+ 
+     return ret;
++#elif defined(__FreeBSD__)
++    char sysctl_name[32];
++    int dK, i, temp;
++    for (temp = i = 0; i < m_cpuData.size(); i++) {
++        snprintf(sysctl_name, sizeof(sysctl_name), "dev.cpu.%d.temperature", i);
++        dK = read_sysctl<int>(sysctl_name);
++        if (dK < 0) {
++            // If we could not read some core's temperature, store
++            // the last read (bogus) value and return false early.
++            m_cpuDataTotal.temp = dK;
++            return false;
++        }
++        temp += dK - 2731;
++    }
++    m_cpuDataTotal.temp = temp / (10 * m_cpuData.size());
++    return m_cpuDataTotal.temp > 0;
++#endif
+ }
+ 
+ static bool get_cpu_power_k10temp(CPUPowerData* cpuPowerData, int& power) {
+@@ -407,6 +433,7 @@ static bool find_fallback_temp_input(const std::string
+     return false;
+ }
+ 
++#ifdef __linux__
+ bool CPUStats::GetCpuFile() {
+     if (m_cpuTempFile)
+         return true;
+@@ -447,6 +474,7 @@ bool CPUStats::GetCpuFile() {
+     }
+     return true;
+ }
++#endif
+ 
+ static bool find_input(const std::string& path, const char* input_prefix, std::string& input, const std::string& name)
+ {
diff --git a/benchmarks/mangohud/files/patch-src_file__utils.cpp b/benchmarks/mangohud/files/patch-src_file__utils.cpp
index 4ae32c1fc2b2..6aeba2b796d7 100644
--- a/benchmarks/mangohud/files/patch-src_file__utils.cpp
+++ b/benchmarks/mangohud/files/patch-src_file__utils.cpp
@@ -1,7 +1,44 @@
 --- src/file_utils.cpp.orig	2021-07-08 06:23:59 UTC
 +++ src/file_utils.cpp
-@@ -109,7 +109,7 @@ std::string read_symlink(const char * link)
+@@ -2,6 +2,7 @@
+ #include "string_utils.h"
+ #include <sys/types.h>
+ #include <sys/stat.h>
++#include <sys/sysctl.h>
+ #include <unistd.h>
+ #include <dirent.h>
+ #include <limits.h>
+@@ -107,9 +108,36 @@ std::string read_symlink(const char * link)
+     return std::string(result, (count > 0) ? count : 0);
+ }
  
++template <>
++int read_sysctl(const char* name)
++{
++    int value;
++    size_t len = sizeof(value);
++
++    if (sysctlbyname(name, &value, &len, NULL, 0) == 0)
++        return value;
++    else return -1;
++}
++
++template <>
++std::string read_sysctl(const char* name)
++{
++    size_t len;
++
++    // How large buffer do we need?
++    if (sysctlbyname(name, NULL, &len, NULL, 0) != 0)
++        return "";
++
++    char value[len];
++    // Now read the actual value into it.
++    if (sysctlbyname(name, value, &len, NULL, 0) == 0)
++        return value;
++    else return "";
++}
++
  std::string get_exe_path()
  {
 -    return read_symlink("/proc/self/exe");
@@ -9,7 +46,7 @@
  }
  
  std::string get_wine_exe_name(bool keep_ext)
-@@ -119,14 +119,14 @@ std::string get_wine_exe_name(bool keep_ext)
+@@ -119,14 +147,14 @@ std::string get_wine_exe_name(bool keep_ext)
          return std::string();
      }
  
diff --git a/benchmarks/mangohud/files/patch-src_file__utils.h b/benchmarks/mangohud/files/patch-src_file__utils.h
new file mode 100644
index 000000000000..b06efb584aa7
--- /dev/null
+++ b/benchmarks/mangohud/files/patch-src_file__utils.h
@@ -0,0 +1,10 @@
+--- src/file_utils.h.orig	2021-07-08 06:23:59 UTC
++++ src/file_utils.h
+@@ -19,6 +19,7 @@ std::vector<std::string> ls(const char* root, const ch
+ bool file_exists(const std::string& path);
+ bool dir_exists(const std::string& path);
+ std::string read_symlink(const char * link);
++template <typename T> T read_sysctl(const char*);
+ std::string get_exe_path();
+ std::string get_wine_exe_name(bool keep_ext = false);
+ std::string get_home_dir();
diff --git a/benchmarks/mangohud/files/patch-src_overlay.cpp b/benchmarks/mangohud/files/patch-src_overlay.cpp
new file mode 100644
index 000000000000..2b7a5c69cd11
--- /dev/null
+++ b/benchmarks/mangohud/files/patch-src_overlay.cpp
@@ -0,0 +1,16 @@
+--- src/overlay.cpp.orig	2021-07-08 06:23:59 UTC
++++ src/overlay.cpp
+@@ -28,12 +28,11 @@ void update_hw_info(struct swapchain_stats& sw_stats, 
+     }
+    if (params.enabled[OVERLAY_PARAM_ENABLED_cpu_stats] || logger->is_active()) {
+       cpuStats.UpdateCPUData();
+-#ifdef __gnu_linux__
+-
+       if (params.enabled[OVERLAY_PARAM_ENABLED_core_load] || params.enabled[OVERLAY_PARAM_ENABLED_cpu_mhz])
+          cpuStats.UpdateCoreMhz();
+       if (params.enabled[OVERLAY_PARAM_ENABLED_cpu_temp] || logger->is_active() || params.enabled[OVERLAY_PARAM_ENABLED_graphs])
+          cpuStats.UpdateCpuTemp();
++#ifdef __gnu_linux__
+       if (params.enabled[OVERLAY_PARAM_ENABLED_cpu_power])
+          cpuStats.UpdateCpuPower();
+ #endif
diff --git a/benchmarks/mangohud/files/patch-src_vulkan.cpp b/benchmarks/mangohud/files/patch-src_vulkan.cpp
index 7c8876346f01..fcd45bb6b633 100644
--- a/benchmarks/mangohud/files/patch-src_vulkan.cpp
+++ b/benchmarks/mangohud/files/patch-src_vulkan.cpp
@@ -21,7 +21,7 @@
     enabled[OVERLAY_PARAM_ENABLED_cpu_temp] = cpuStats.GetCpuFile()
                             && enabled[OVERLAY_PARAM_ENABLED_cpu_temp];
     enabled[OVERLAY_PARAM_ENABLED_cpu_power] = cpuStats.InitCpuPowerData()
-@@ -600,21 +600,24 @@ void init_gpu_stats(uint32_t& vendorID, overlay_params
+@@ -600,21 +600,21 @@ void init_gpu_stats(uint32_t& vendorID, overlay_params
  }
  
  void init_system_info(){
@@ -31,26 +31,30 @@
           unsetenv("LD_PRELOAD");
  
 -      ram =  exec("cat /proc/meminfo | grep 'MemTotal' | awk '{print $2}'");
-+      ram = exec("grep MemTotal " PROCDIR "/meminfo | awk '{print $2}'");
-       trim(ram);
+-      trim(ram);
 -      cpu =  exec("cat /proc/cpuinfo | grep 'model name' | tail -n1 | sed 's/^.*: //' | sed 's/([^)]*)/()/g' | tr -d '(/)'");
-+      cpu = exec("grep 'model name' " PROCDIR "/cpuinfo | tail -n1 | sed 's/^.*: //' | sed 's/([^)]*)/()/g' | tr -d '(/)'");
-       trim(cpu);
-       kernel = exec("uname -r");
-       trim(kernel);
-       os = exec("cat /etc/*-release | grep 'PRETTY_NAME' | cut -d '=' -f 2-");
-       os.erase(remove(os.begin(), os.end(), '\"' ), os.end());
-       trim(os);
+-      trim(cpu);
+-      kernel = exec("uname -r");
+-      trim(kernel);
+-      os = exec("cat /etc/*-release | grep 'PRETTY_NAME' | cut -d '=' -f 2-");
+-      os.erase(remove(os.begin(), os.end(), '\"' ), os.end());
+-      trim(os);
 -      cpusched = read_line("/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor");
++      ram = to_string(sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE)
++            / 1024 / 1024) + " MB";
++      cpu = read_sysctl<string>("hw.model");
++      kernel = to_string(read_sysctl<int>("kern.osreldate"));
++      os = read_sysctl<string>("kern.ostype") + " " +
++           read_sysctl<string>("kern.osrelease");
 +      gpu = exec("pciconf -lv | grep -A2 ^vgapci0 | tail -1 | cut -d\\' -f2");
 +      trim(gpu);
 +      driver = exec("glxinfo -B | grep 'OpenGL version' | sed 's/^.*: //'");
 +      trim(driver);
-+      cpusched = exec("sysctl -b kern.sched.name");
++      cpusched = read_sysctl<string>("kern.sched.name");
  
        const char* mangohud_recursion = getenv("MANGOHUD_RECURSION");
        if (!mangohud_recursion) {
-@@ -669,6 +672,8 @@ void init_system_info(){
+@@ -669,6 +669,8 @@ void init_system_info(){
        else {
             wineVersion = "";
        }
@@ -59,7 +63,7 @@
        // check for gamemode and vkbasalt
        stringstream ss;
        string line;
-@@ -686,6 +691,7 @@ void init_system_info(){
+@@ -686,6 +688,7 @@ void init_system_info(){
           if (HUDElements.gamemode_bol && HUDElements.vkbasalt_bol)
              break;
        }
@@ -67,7 +71,7 @@
  
        if (ld_preload)
           setenv("LD_PRELOAD", ld_preload, 1);
-@@ -697,7 +703,6 @@ void init_system_info(){
+@@ -697,7 +700,6 @@ void init_system_info(){
                  << "Gpu:" << gpu << "\n"
                  << "Driver:" << driver << "\n"
                  << "CPU Scheduler:" << cpusched << std::endl;