From 08fb2c51062115c0f0b2423a5d9094dfcebb1cc1 Mon Sep 17 00:00:00 2001
From: Florian Schmaus <flow@cs.fau.de>
Date: Thu, 17 Feb 2022 18:12:11 +0100
Subject: [PATCH] [LinuxVersion] Cache linux version to avoid uname() syscall

Also add error handling to uname() call.
---
 emper/lib/LinuxVersion.cpp | 13 ++++++++++---
 emper/lib/LinuxVersion.hpp |  1 +
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/emper/lib/LinuxVersion.cpp b/emper/lib/LinuxVersion.cpp
index 86bad3f1..0c819773 100644
--- a/emper/lib/LinuxVersion.cpp
+++ b/emper/lib/LinuxVersion.cpp
@@ -34,10 +34,17 @@ static auto checked_strtol(const std::string& s) -> long {
 
 namespace emper::lib {
 
+std::string LinuxVersion::globalVersion;
+
 LinuxVersion::LinuxVersion() {
-	struct utsname buf;
-	uname(&buf);
-	version = std::string(buf.release);
+	if (globalVersion.empty()) {
+		struct utsname buf;
+		if (uname(&buf)) {
+			DIE_MSG_ERRNO("Failed to invoke uname()");
+		}
+		globalVersion = std::string(buf.release);
+	}
+	version = globalVersion;
 }
 
 // Returns 1 if s is smaller, -1 if this is smaller, 0 if equal
diff --git a/emper/lib/LinuxVersion.hpp b/emper/lib/LinuxVersion.hpp
index 93f70e15..2d3b5713 100644
--- a/emper/lib/LinuxVersion.hpp
+++ b/emper/lib/LinuxVersion.hpp
@@ -9,6 +9,7 @@ class TestLinuxVersion;
 namespace emper::lib {
 class LinuxVersion {
 	friend class ::TestLinuxVersion;
+	static std::string globalVersion;
 	std::string version;
 
 	auto compare(const std::string& s) -> int;
-- 
GitLab