Skip to content

Commit

Permalink
param: gpu_list
Browse files Browse the repository at this point in the history
  • Loading branch information
flightlessmango committed Oct 12, 2024
1 parent 55bd152 commit e422dc8
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 36 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ Parameters that are enabled by default have to be explicitly disabled. These (cu
| `gpu_load_value` | Set the values for medium and high load e.g `gpu_load_value=50,90` |
| `gpu_name` | Display GPU name from pci.ids |
| `gpu_voltage` | Display GPU voltage (only works on AMD GPUs) |
| `gpu_list` | List GPUs to display `gpu_list=0,1` |
| `hide_fsr_sharpness` | Hides the sharpness info for the `fsr` option (only available in gamescope) |
| `histogram` | Change FPS graph to histogram |
| `horizontal` | Display Mangohud in a horizontal position |
Expand Down
2 changes: 2 additions & 0 deletions data/MangoHud.conf
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ gpu_stats
# gpu_fan
## gpu_voltage only works on AMD GPUs
# gpu_voltage
## Select list of GPUs to display
# gpu_list=0,1

### Display the current CPU information
cpu_stats
Expand Down
37 changes: 36 additions & 1 deletion src/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using namespace std::chrono_literals;
#include <string>
namespace fs = ghc::filesystem;

GPUS::GPUS() {
GPUS::GPUS(overlay_params* params) : params(params) {
std::vector<std::string> gpu_entries;

for (const auto& entry : fs::directory_iterator("/sys/class/drm")) {
Expand Down Expand Up @@ -165,4 +165,39 @@ void GPUS::find_active_gpu() {
SPDLOG_DEBUG("failed to find active GPU");
}

int GPU::index_in_selected_gpus() {
auto selected_gpus = gpus->selected_gpus();
auto it = std::find_if(selected_gpus.begin(), selected_gpus.end(),
[this](const std::shared_ptr<GPU>& gpu) {
return gpu.get() == this;
});
if (it != selected_gpus.end()) {
return std::distance(selected_gpus.begin(), it);
}
return -1;
}

std::string GPU::gpu_text() {
std::string gpu_text;
size_t index = this->index_in_selected_gpus();
if (gpus->selected_gpus().size() > 1) {
gpu_text = "GPU" + std::to_string(index);
if (gpus->params->gpu_text.size() > index)
gpu_text = gpus->params->gpu_text[index];
} else {
gpu_text = "GPU";
}
return gpu_text;
}

std::string GPU::vram_text() {
std::string vram_text;
size_t index = this->index_in_selected_gpus();
if (gpus->selected_gpus().size() > 1)
vram_text = "VRAM" + std::to_string(index);
else
vram_text = "VRAM";
return vram_text;
}

std::unique_ptr<GPUS> gpus = nullptr;
36 changes: 31 additions & 5 deletions src/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,24 @@ class GPU {
return nullptr;
}

std::string gpu_text();
std::string vram_text();

private:
uint32_t device_id;
std::thread thread;

int index_in_selected_gpus();
};

class GPUS {
public:
std::vector<std::shared_ptr<GPU>> available_gpus;
std::mutex metrics_mutex;

std::mutex mutex;
overlay_params* params;

void find_active_gpu();
GPUS();
GPUS(overlay_params* params);

void pause() {
for (auto& gpu : available_gpus)
Expand All @@ -131,8 +136,9 @@ class GPUS {
std::shared_ptr<GPU> active_gpu() {
if (!available_gpus.empty()){
for (auto gpu : available_gpus) {
if (gpu->is_active)
if (gpu->is_active) {
return gpu;
}
}
}

Expand All @@ -146,11 +152,31 @@ class GPUS {
}

void get_metrics() {
std::lock_guard<std::mutex> lock(metrics_mutex);
std::lock_guard<std::mutex> lock(mutex);
for (auto gpu : available_gpus)
gpu->get_metrics();
}

std::vector<std::shared_ptr<GPU>> selected_gpus() {
std::lock_guard<std::mutex> lock(mutex);
std::vector<std::shared_ptr<GPU>> vec;
if (!params->gpu_list.empty()) {
for (unsigned index : params->gpu_list) {
if (index < available_gpus.size()) {
if (available_gpus[index])
vec.push_back(available_gpus[index]);
}
}
// if the user hasn't selected any GPUs, we use the active one
} else {
if (active_gpu())
vec.push_back(active_gpu());

}

return vec;
}

private:
std::string get_pci_device_address(const std::string& drm_card_path);
};
Expand Down
44 changes: 15 additions & 29 deletions src/hud_elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,27 +185,13 @@ void HudElements::version(){

void HudElements::gpu_stats(){
if (!gpus)
gpus = std::make_unique<GPUS>();
gpus = std::make_unique<GPUS>(HUDElements.params);

std::lock_guard<std::mutex> lock(gpus->metrics_mutex);
int i = 0;
size_t i = 0;
if (HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_gpu_stats]){
for (auto gpu : gpus->available_gpus) {
if (HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_active_gpu] && !gpu->is_active)
continue;

std::string gpu_text;
for (auto& gpu : gpus->selected_gpus()) {
ImguiNextColumnFirstItem();
if (HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_active_gpu]) {
gpu_text = "GPU";
if (HUDElements.params->gpu_text.size() > (size_t)i)
gpu_text = HUDElements.params->gpu_text[i];
} else {
gpu_text = "GPU" + std::to_string(i);
if (HUDElements.params->gpu_text.size() > (size_t)i)
gpu_text = HUDElements.params->gpu_text[i];
}
HUDElements.TextColored(HUDElements.colors.gpu, "%s", gpu_text.c_str());
HUDElements.TextColored(HUDElements.colors.gpu, "%s", gpu->gpu_text().c_str());

ImguiNextColumnOrNewRow();
auto text_color = HUDElements.colors.text;
Expand Down Expand Up @@ -511,22 +497,22 @@ void HudElements::io_stats(){

void HudElements::vram(){
if (!gpus)
gpus = std::make_unique<GPUS>();
gpus = std::make_unique<GPUS>(HUDElements.params);

if (HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_vram]){
std::lock_guard<std::mutex> lock(gpus->metrics_mutex);
int i = 0;
size_t i = 0;
if (HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_gpu_stats]){
for (auto gpu : gpus->available_gpus) {
for (auto& gpu : gpus->selected_gpus()) {
ImguiNextColumnFirstItem();
if (HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_active_gpu] && !gpu->is_active)
continue;
// Just iterate through the user selected GPUs
if (!HUDElements.params->gpu_list.empty())
for (auto& gpu_index : HUDElements.params->gpu_list)
if (gpu_index < gpus->available_gpus.size())
if (i != gpu_index)
continue;

if (HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_active_gpu]) {
HUDElements.TextColored(HUDElements.colors.vram, "VRAM");
} else {
HUDElements.TextColored(HUDElements.colors.vram, ("VRAM" + to_string(i)).c_str());
}

HUDElements.TextColored(HUDElements.colors.vram, gpu->vram_text().c_str());

ImguiNextColumnOrNewRow();
// Add gtt_used to vram usage for APUs
Expand Down
16 changes: 16 additions & 0 deletions src/overlay_params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,22 @@ parse_str_tokenize(const char *str, const std::string& delims = ",:+", bool btri
return data;
}

static std::vector<unsigned>
parse_gpu_list(const char *str) {

std::vector<unsigned int> result;
std::stringstream ss{std::string(str)};
std::string item;

while (std::getline(ss, item, ',')) {
unsigned int num = static_cast<unsigned int>(std::stoul(item));
printf("parsing gpu list num: %i\n", num);
result.push_back(num);
}

return result;
}


static unsigned
parse_unsigned(const char *str)
Expand Down
3 changes: 2 additions & 1 deletion src/overlay_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ typedef unsigned long KeySym;
OVERLAY_PARAM_BOOL(present_mode) \
OVERLAY_PARAM_BOOL(time_no_label) \
OVERLAY_PARAM_BOOL(display_server) \
OVERLAY_PARAM_BOOL(active_gpu) \
OVERLAY_PARAM_CUSTOM(fps_sampling_period) \
OVERLAY_PARAM_CUSTOM(output_folder) \
OVERLAY_PARAM_CUSTOM(output_file) \
Expand Down Expand Up @@ -200,6 +199,7 @@ typedef unsigned long KeySym;
OVERLAY_PARAM_CUSTOM(device_battery) \
OVERLAY_PARAM_CUSTOM(fps_metrics) \
OVERLAY_PARAM_CUSTOM(network) \
OVERLAY_PARAM_CUSTOM(gpu_list) \

enum overlay_param_position {
LAYER_POSITION_TOP_LEFT,
Expand Down Expand Up @@ -326,6 +326,7 @@ struct overlay_params {
std::vector<std::string> device_battery;
std::vector<std::string> fps_metrics;
std::vector<std::string> network;
std::vector<unsigned> gpu_list;
};

const extern char *overlay_param_names[];
Expand Down

0 comments on commit e422dc8

Please sign in to comment.