-
Notifications
You must be signed in to change notification settings - Fork 24
Add GPU power consumption, per process, via nvidia-smi pmon #83
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
Draft
Stapuck
wants to merge
2
commits into
joular:39-support-gpu-energy-monitoring-per-process-and-per-application
Choose a base branch
from
Stapuck:develop
base: 39-support-gpu-energy-monitoring-per-process-and-per-application
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.
+143
−0
Draft
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| with Ada.Text_IO; use Ada.Text_IO; | ||
| with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; | ||
| with GNAT.OS_Lib; use GNAT.OS_Lib; | ||
| with Ada.Containers.Vectors; | ||
| with Ada.Strings.Fixed; | ||
| with Ada.Command_Line; | ||
|
|
||
| with Nvidia_SMI; use Nvidia_SMI; | ||
|
|
||
| procedure Gpu_Power_By_Process is | ||
|
|
||
| type PID_Type is new Integer; | ||
|
|
||
| type SM_Usage is record | ||
| PID : PID_Type; | ||
| SM : Float; | ||
| end record; | ||
|
|
||
| package SM_Vector is new | ||
| Ada.Containers.Vectors (Index_Type => Natural, Element_Type => SM_Usage); | ||
| use SM_Vector; | ||
|
|
||
| -- Helper to execute a shell command and return the output as string | ||
| function Execute_Command (Command : String) return String is | ||
| Output : String (1 .. 10_000); | ||
| Len : Integer; | ||
| begin | ||
| Len := GNAT.OS_Lib.Shell (Command, Output'Address, Output'Length); | ||
| return Output (1 .. Len); | ||
| end Execute_Command; | ||
|
|
||
| -- Get the SM usage per PID from nvidia-smi pmon | ||
| function Get_SM_By_PID return SM_Vector.Vector is | ||
| Raw_Output : constant String := Execute_Command ("nvidia-smi pmon -s u"); -- run nvidia-smi pmon -s um pour plus d'info mémoire. | ||
| Lines : SM_Vector.Vector; | ||
| Line_Start : Positive := 1; | ||
| Line_End : Natural; | ||
| begin | ||
| declare | ||
| Output_Lines : constant String := Raw_Output; | ||
| use Ada.Strings.Fixed; | ||
| Line : String; | ||
| Count : Natural := 0; | ||
| begin | ||
| for L of Output_Lines'Range loop | ||
| exit when Count > 1000; | ||
| Line_End := | ||
| Index (Output_Lines (Line_Start .. Output_Lines'Last), ASCII.LF); | ||
| exit when Line_End = 0; | ||
| Line := Output_Lines (Line_Start .. Line_Start + Line_End - 2); | ||
| Line_Start := Line_Start + Line_End; | ||
|
|
||
| if Line'Length > 0 and then Line (1) /= '#' then | ||
| declare | ||
| Tokens : constant String := Line; | ||
| PID_Val : PID_Type := | ||
| Integer'Value (Trim (Tokens (1 .. 5), Ada.Strings.Left)); | ||
| SM_Val : Float := | ||
| Float'Value (Trim (Tokens (27 .. 30), Ada.Strings.Left)); | ||
| begin | ||
| Lines.Append ((PID => PID_Val, SM => SM_Val)); | ||
| exception | ||
| when others => | ||
| null; | ||
| end; | ||
| end if; | ||
| Count := Count + 1; | ||
| end loop; | ||
|
|
||
| return Lines; | ||
| end; | ||
| end Get_SM_By_PID; | ||
|
|
||
| -- Compute GPU consumption for a specific PID | ||
| function Estimate_PID_Consumption (Target_PID : PID_Type) return Float is | ||
| SM_Data : SM_Vector.Vector := Get_SM_By_PID; | ||
| Total_SM : Float := 0.0; | ||
| PID_SM : Float := 0.0; | ||
| Total_Power : Float := Float(Get_Nvidia_SMI_Power); | ||
| begin | ||
| for Usage of SM_Data loop | ||
| Total_SM := Total_SM + Usage.SM; | ||
| if Usage.PID = Target_PID then | ||
| PID_SM := PID_SM + Usage.SM; | ||
| end if; | ||
| end loop; | ||
|
|
||
| if Total_SM = 0.0 then | ||
| return 0.0; | ||
| end if; | ||
|
|
||
| -- return (PID_SM / Total_SM) * Total_Power; | ||
| return (PID_SM * Total_Power) / Total_SM; | ||
| end Estimate_PID_Consumption; | ||
|
|
||
| begin | ||
| if Ada.Command_Line.Argument_Count < 1 then | ||
| Put_Line ("Usage: gpu_power_by_process <PID>"); | ||
| return; | ||
| end if; | ||
|
|
||
| declare | ||
| PID : PID_Type := | ||
| Integer'Value (Ada.Command_Line.Argument (1)); | ||
| Estimated_Conso : Float := Estimate_PID_Consumption (PID); | ||
| begin | ||
| Put_Line | ||
| ("Estimated GPU Power Consumption for PID " | ||
| & PID'Image | ||
| & ": " | ||
| & Estimated_Conso'Image | ||
| & " W"); | ||
| end; | ||
| end Gpu_Power_By_Process; | ||
|
|
||
|
|
||
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,24 @@ | ||
| with Ada.Containers.Vectors; | ||
|
|
||
| package Gpu_Power_By_Process is | ||
|
|
||
| type PID_Type is new Integer; | ||
|
|
||
| type SM_Usage is record | ||
| PID : PID_Type; | ||
| SM : Float; | ||
| end record; | ||
|
|
||
| package SM_Vector is new Ada.Containers.Vectors (Index_Type => Natural, Element_Type => SM_Usage); | ||
|
|
||
| -- Exécute une commande shell et retourne la sortie sous forme de chaîne | ||
| -- revoir ? | ||
| function Execute_Command (Command : String) return String; | ||
|
|
||
| -- Récupère l'utilisation SM par PID à partir de nvidia-smi pmon | ||
| function Get_SM_By_PID return SM_Vector.Vector; | ||
|
|
||
| -- Estime la consommation GPU d'un processus donné (PID) | ||
| function Estimate_PID_Consumption (Target_PID : PID_Type) return Float; | ||
|
|
||
| end Gpu_Power_By_Process; |
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.
add
-c 1to run nvidia-smi once (otherwise it'll run continuously).So it will be
nvidia-smi pmon -s u -c 1