Skip to content

Commit

Permalink
Merge pull request #371 from stsdc/fix-vram-percentage
Browse files Browse the repository at this point in the history
Fixes VRAM percentage #369
  • Loading branch information
stsdc authored Feb 29, 2024
2 parents 002146e + 1e0645d commit 192efee
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
18 changes: 13 additions & 5 deletions src/Resources/GPU/GPUAmd.vala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public class Monitor.GPUAmd : IGPU, Object {
public SessionManager? session_manager { get; protected set; }
public SessionManager ? session_manager { get; protected set; }

public Gee.HashMap<string, HwmonTemperature> hwmon_temperatures { get; set; }

Expand All @@ -9,24 +9,30 @@ public class Monitor.GPUAmd : IGPU, Object {

public int memory_percentage { get; protected set; }

public int memory_vram_used { get; protected set; }
public double memory_vram_used { get; protected set; }

public double memory_vram_total { get; set; }

public double temperature { get; protected set; }

construct {
// session_manager = get_sessman ();
// session_manager = get_sessman ();
}

private void update_temperature () {
temperature = double.parse (hwmon_temperatures.get ("edge").input) / 1000;
}

private void update_memory_vram_used () {
memory_vram_used = int.parse (get_sysfs_value ("/sys/class/drm/card0/device/mem_info_vram_used"));
memory_vram_used = double.parse (get_sysfs_value ("/sys/class/drm/card0/device/mem_info_vram_used"));
}

private void update_memory_vram_total () {
memory_vram_total = double.parse (get_sysfs_value ("/sys/class/drm/card0/device/mem_info_vram_total"));;
}

private void update_memory_percentage () {
memory_percentage = int.parse (get_sysfs_value ("/sys/class/drm/card0/device/mem_busy_percent"));
memory_percentage = (int) (Math.round ((memory_vram_used / memory_vram_total) * 100));
}

private void update_percentage () {
Expand All @@ -36,7 +42,9 @@ public class Monitor.GPUAmd : IGPU, Object {
public void update () {
update_temperature ();
update_memory_vram_used ();
update_memory_vram_total ();
update_memory_percentage ();
update_percentage ();
}

}
44 changes: 29 additions & 15 deletions src/Resources/GPU/GPUNvidia.vala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public class Monitor.GPUNvidia : IGPU, Object {
public SessionManager? session_manager { get; protected set; }
public SessionManager ? session_manager { get; protected set; }

public Gee.HashMap<string, HwmonTemperature> hwmon_temperatures { get; set; }

Expand All @@ -9,7 +9,9 @@ public class Monitor.GPUNvidia : IGPU, Object {

public int memory_percentage { get; protected set; }

public int memory_vram_used { get; protected set; }
public double memory_vram_used { get; protected set; }

public double memory_vram_total { get; set; }

public double temperature { get; protected set; }

Expand All @@ -21,7 +23,7 @@ public class Monitor.GPUNvidia : IGPU, Object {

public int nvidia_percentage = 0;

public char *nvidia_used = "";
public char * nvidia_used = "";

public bool nvidia_resources_temperature;

Expand All @@ -32,7 +34,7 @@ public class Monitor.GPUNvidia : IGPU, Object {
public X.Display nvidia_display;

construct {
// session_manager = get_sessman ();
// session_manager = get_sessman ();
nvidia_display = new X.Display ();
}

Expand All @@ -43,7 +45,7 @@ public class Monitor.GPUNvidia : IGPU, Object {
0,
NV_CTRL_GPU_CORE_TEMPERATURE,
&nvidia_temperature
);
);

if (!nvidia_resources_temperature) {
warning ("Could not query NV_CTRL_GPU_CORE_TEMPERATURE attribute!\n");
Expand All @@ -57,11 +59,11 @@ public class Monitor.GPUNvidia : IGPU, Object {
0,
NV_CTRL_USED_DEDICATED_GPU_MEMORY,
&nvidia_memory_vram_used
);
);

if (!nvidia_resources_vram_used) {
warning ("Could not query NV_CTRL_USED_DEDICATED_GPU_MEMORY attribute!\n");
return ;
return;
}

nvidia_resources_used = NVCtrl.XNVCTRLQueryTargetStringAttribute (
Expand All @@ -71,28 +73,39 @@ public class Monitor.GPUNvidia : IGPU, Object {
0,
NV_CTRL_STRING_GPU_UTILIZATION,
&nvidia_used
);
);

// var str_used = (string)nvidia_used;
nvidia_percentage = int.parse (((string)nvidia_used).split_set ("=,")[1]);
nvidia_memory_percentage = int.parse (((string)nvidia_used).split_set ("=,")[3]);
nvidia_percentage = int.parse (((string) nvidia_used).split_set ("=,")[1]);
nvidia_memory_percentage = int.parse (((string) nvidia_used).split_set ("=,")[3]);
debug ("USED_GRAPHICS: %d%\n", nvidia_percentage);
debug ("USED_MEMORY: %d%\n", nvidia_memory_percentage);

if (!nvidia_resources_used) {
warning ("Could not query NV_CTRL_STRING_GPU_UTILIZATION attribute!\n");
return ;
return;
}

}

private void update_temperature () { temperature = nvidia_temperature; }
private void update_temperature () {
temperature = nvidia_temperature;
}

private void update_memory_vram_used () {
memory_vram_used = (double) nvidia_memory_vram_used;
}

private void update_memory_vram_used () { memory_vram_used = nvidia_memory_vram_used; }
private void update_memory_vram_total () {
}

private void update_memory_percentage () { memory_percentage = nvidia_memory_percentage; }
private void update_memory_percentage () {
memory_percentage = nvidia_memory_percentage;
}

private void update_percentage () { percentage = nvidia_percentage; }
private void update_percentage () {
percentage = nvidia_percentage;
}

public void update () {
update_nv_resources ();
Expand All @@ -101,4 +114,5 @@ public class Monitor.GPUNvidia : IGPU, Object {
update_memory_percentage ();
update_percentage ();
}

}
9 changes: 7 additions & 2 deletions src/Resources/GPU/IGPU.vala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public interface Monitor.IGPU : Object {
public abstract SessionManager? session_manager { get; public set; }
public abstract SessionManager ? session_manager { get; public set; }

public abstract Gee.HashMap<string, HwmonTemperature> hwmon_temperatures { get; set; }

Expand All @@ -15,14 +15,18 @@ public interface Monitor.IGPU : Object {

public abstract int memory_percentage { get; protected set; }

public abstract int memory_vram_used { get; protected set; }
public abstract double memory_vram_used { get; protected set; }

public abstract double memory_vram_total { get; protected set; }

public abstract double temperature { get; protected set; }

public abstract void update_temperature ();

public abstract void update_memory_vram_used ();

public abstract void update_memory_vram_total ();

public abstract void update_memory_percentage ();

public abstract void update_percentage ();
Expand All @@ -40,4 +44,5 @@ public interface Monitor.IGPU : Object {
}
return content;
}

}

0 comments on commit 192efee

Please sign in to comment.