-
-
Notifications
You must be signed in to change notification settings - Fork 48
Introduce per process GPU usage #490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stsdc
wants to merge
21
commits into
main
Choose a base branch
from
stsdc/process-gpu-usage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+164
−10
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
cac2491
Introduce per process GPU usage
stsdc d5f9686
Add some comments
stsdc 7a38179
Fix lint
stsdc 13b61ff
Merge branch 'main' into stsdc/process-gpu-usage
stsdc 5243cfc
Get update_interval for the per process GPU usage calculation from th…
stsdc 2ba0e9b
Merge branch 'main' into stsdc/process-gpu-usage
stsdc 2d82102
Refactor get_usage_gpu()
stsdc 6cf1db2
Merge branch 'main' into stsdc/process-gpu-usage
stsdc 555949f
Merge branch 'main' into stsdc/process-gpu-usage
stsdc 70e7325
Merge branch 'main' into stsdc/process-gpu-usage
stsdc d9677e7
Merge branch 'main' into stsdc/process-gpu-usage
stsdc 35202a7
Refactor GPU usage handling by introducing ProcessDRM class
stsdc 54b9a86
Refactor ProcessDRM
stsdc 8bd4800
Remove leftovers
stsdc 61666d8
Merge branch 'main' into stsdc/process-gpu-usage
stsdc ad32fec
Refactor Process and ProcessDRM
stsdc 2ff77ec
Merge branch 'main' into stsdc/process-gpu-usage
stsdc bf609c3
Comment update
stsdc 6ba1f60
Remove trailing whitespaces
stsdc cbb0746
Merge branch 'main' into stsdc/process-gpu-usage
ryonakano 821c530
Make clear that percentage calculation is performed using nanoseconds
stsdc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,112 @@ | ||||||
| /* | ||||||
| * SPDX-License-Identifier: GPL-3.0-or-later | ||||||
| * SPDX-FileCopyrightText: 2025 elementary, Inc. (https://elementary.io) | ||||||
| */ | ||||||
|
|
||||||
| public class Monitor.ProcessDRM { | ||||||
| /** Time spent busy in nanoseconds by the render engine executing | ||||||
| * workloads from the last time it was read | ||||||
| */ | ||||||
| private uint64 last_engine_render; | ||||||
| private uint64 last_engine_gfx; | ||||||
|
|
||||||
|
|
||||||
| public double gpu_percentage { get; private set; } | ||||||
|
|
||||||
| private int pid; | ||||||
| private int update_interval; | ||||||
|
|
||||||
| public ProcessDRM (int pid, int update_interval) { | ||||||
| this.pid = pid; | ||||||
| this.update_interval = update_interval; | ||||||
|
|
||||||
| last_engine_render = 0; | ||||||
| last_engine_gfx = 0; | ||||||
| } | ||||||
|
|
||||||
| public void update () { | ||||||
| string path_fdinfo = "/proc/%d/fdinfo".printf (pid); | ||||||
| string path_fd = "/proc/%d/fd".printf (pid); | ||||||
|
|
||||||
|
|
||||||
| var drm_files = new Gee.ArrayList<GLib.File ?> (); | ||||||
|
|
||||||
| try { | ||||||
| Dir dir = Dir.open (path_fdinfo, 0); | ||||||
| string ? name = null; | ||||||
|
|
||||||
| while ((name = dir.read_name ()) != null) { | ||||||
|
|
||||||
| // skip standard fds | ||||||
| if (name == "0" || name == "1" || name == "2") { | ||||||
| continue; | ||||||
| } | ||||||
| string path = Path.build_filename (path_fdinfo, name); | ||||||
|
|
||||||
| int fd_dir_fd = Posix.open (path_fd, Posix.O_RDONLY | Posix.O_DIRECTORY, 0); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
You don't need to explicit the |
||||||
| bool is_drm = is_drm_fd (fd_dir_fd, name); | ||||||
| Posix.close (fd_dir_fd); | ||||||
|
|
||||||
| if (is_drm) { | ||||||
| var drm_file = File.new_for_path (path); | ||||||
| drm_files.add (drm_file); | ||||||
| } | ||||||
| } | ||||||
| } catch (FileError err) { | ||||||
| // prevent flooding logs with permission errors | ||||||
| if (!(err is FileError.ACCES)) { | ||||||
| warning (err.message); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| foreach (var drm_file in drm_files) { | ||||||
| try { | ||||||
| var dis = new DataInputStream (drm_file.read ()); | ||||||
| string ? line; | ||||||
|
|
||||||
| while ((line = dis.read_line ()) != null) { | ||||||
| var splitted_line = line.split (":"); | ||||||
| switch (splitted_line[0]) { | ||||||
| case "drm-engine-gfx": | ||||||
| update_engine (splitted_line[1], ref last_engine_gfx); | ||||||
| break; | ||||||
| // for i915 there is only drm-engine-render to check | ||||||
| case "drm-engine-render": | ||||||
| update_engine (splitted_line[1], ref last_engine_render); | ||||||
| break; | ||||||
| default: | ||||||
| // Ignore other entries | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| } catch (Error err) { | ||||||
| if (!(err is FileError.ACCES)) { | ||||||
| warning ("Can't read fdinfo: '%s' %d", err.message, err.code); | ||||||
| } | ||||||
| } | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private void update_engine (string line, ref uint64 last_engine) { | ||||||
| var engine = uint64.parse (line.strip ().split (" ")[0]); | ||||||
| if (last_engine != 0) { | ||||||
| gpu_percentage = calculate_percentage (engine, last_engine, update_interval); | ||||||
| } | ||||||
| last_engine = engine; | ||||||
| } | ||||||
|
|
||||||
| private static double calculate_percentage (uint64 engine, uint64 last_engine, int interval) { | ||||||
| // Since values in the files are in nanoseconds, it is also needed to convert interval to nanoseconds (10^9) | ||||||
| return 100 * ((double) (engine - last_engine)) / (interval * 1e9); | ||||||
stsdc marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| } | ||||||
|
|
||||||
| // Based on nvtop | ||||||
| // https://github.com/Syllo/nvtop/blob/4bf5db248d7aa7528f3a1ab7c94f504dff6834e4/src/extract_processinfo_fdinfo.c#L88 | ||||||
| static bool is_drm_fd (int fd_dir_fd, string name) { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| Posix.Stat stat; | ||||||
| int ret = Posix.fstatat (fd_dir_fd, name, out stat, 0); | ||||||
| return ret == 0 && (stat.st_mode & Posix.S_IFMT) == Posix.S_IFCHR && Posix.major (stat.st_rdev) == 226; | ||||||
| } | ||||||
|
|
||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should handle error of
Posix.open()when it returns -1