GameServerManagers / GameServerManagers/LinuxGSM

[Bug]: Inaccurate CPU core count & frequency reporting in distro_info.sh on multi-core systems with E & P cores

Open
#4,522 0 comments 0 reactions 0 assignees View on GitHub
command: details command: install distro: Ubuntu type: bug
Dominant language
Shell
Stars
4.9k
Forks
864
PR merge metrics
No merged PRs in 30d

Description

### User story

As an admin, I want the distro_info.sh script to accurately report the frequencies and # of all CPU cores, including both E and P cores.

### Game

All

### Linux distro

Ubuntu 22.04

### Command

command: details

### Further information

The script `distro_info.sh` currently has limitations in accurately reporting CPU information for systems with hybrid architectures that include both Efficiency (E-cores) and Performance (P-cores) cores and systems that support Multithreading.

The bugs can be found in the output of `./gameserver details`.
![Screenshot](https://github.com/GameServerManagers/LinuxGSM/assets/38057464/bd44e8c2-6438-4caf-99ec-543ce46b60bd)

# CPU Frequency Reporting:
The script uses a method to fetch the CPU frequency that does not account for the differing frequencies of E and P cores. It only captures the frequency of a single core, which can lead to misleading information about the system's capabilities: [info_distro.sh#L142](https://github.com/GameServerManagers/LinuxGSM/blob/master/lgsm/modules/info_distro.sh#L142)

> cpufreqency="$(awk -F: '/cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//')" # e.g 2394.503

This should be updated to reflect a more accurate representation of the system's CPU frequencies, especially for hybrid architectures.

- A possible solution would be to list the number of cores running at different frequencies:

```bash
declare -A freq_count

# Loop through each core and collect frequencies
while IFS=: read -r label cpu_id; do
cpu_freq=$(awk -v id="$cpu_id" '$1 == "processor" {p=$3} $1 == "cpu" && $2 == "MHz" && p == id {print $4; exit}' /proc/cpuinfo)
((freq_count[$cpu_freq]++))
done < <(awk -F: '/processor/ {print $1 ":" $2}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//')

# Sort by frequency
for freq in "${!freq_count[@]}"; do
echo "$freq ${freq_count[$freq]}"
done | sort -rn -k1,1 | awk '{print $2"x " $1 " MHz"}' | paste -sd, - | sed 's/,/, /g'
```

- Or use the average frequency of all cores:

```bash
# Initialize sum of frequencies and core count
total_freq=0
core_count=0

# Loop through each core
for cpu_id in $(awk -F: '/processor/ {print $2}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//'); do
# Extract the frequency of the current core
cpu_freq=$(awk -v id="$cpu_id" '$1 == "processor" {p=$3} $1 == "cpu" && $2 == "MHz" && p == id {print $4; exit}' /proc/cpuinfo)
# Add the frequency to the total
total_freq=$(echo "$total_freq + $cpu_freq" | bc)
# Increment the core count
core_count=$((core_count + 1))
done

# Calculate the average frequency
if [ $core_count -gt 0 ]; then
avg_freq=$(echo "scale=2; $total_freq / $core_count" | bc)
echo "Average CPU Frequency: $avg_freq MHz"
fi
```

# CPU Core Count Reporting
Additionally, the script inaccurately reports the total number of CPU cores in systems with hybrid architectures. For example, on an Intel i7-13700K, which has 16 physical cores (8 E-cores and 8 P-cores), the script reports a total of 24 cores. This discrepancy arises from the following line: [info_distro.sh#141](https://github.com/GameServerManagers/LinuxGSM/blob/master/lgsm/modules/info_distro.sh#L141)

> cpucores="$(awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo)"

This is likely to be an issue of how logical cores are counted versus physical cores, especially in the context of Intel's Thread Director technology, which can present more logical processors to the operating system than the physical core count.

- The solution is to use the value of `cpu cores` listed in `/proc/cpuinfo`:
```bash
cpucores="$(awk -F': ' '/cpu cores/ {print $2; exit}' /proc/cpuinfo)"
```

### Relevant log output

_No response_

### Steps to reproduce

**These bugs might only arise on bare metal servers**
1. Install any gameserver
2. Run `./gameserver details`
- Wrong number of cores displayed (if CPU supports Hyperthreading)
- Only the frequency of a single core is displayed.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.