-
Notifications
You must be signed in to change notification settings - Fork 18
[RFC] ANDROID: GPU frequency tracing support #28
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
ssebinraj
wants to merge
1
commit into
projectceladon:android/celadon
Choose a base branch
from
ssebinraj:dev_gpufreqtracer
base: android/celadon
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.
Open
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,290 @@ | ||
| // SPDX-License-Identifier: GPL-2.0 | ||
| /* | ||
| * Copyright © 2024 Intel Corporation | ||
| */ | ||
|
|
||
| #include "xe_gpufreqtracer.h" | ||
|
|
||
| #include <linux/workqueue.h> | ||
| #include <linux/timer.h> | ||
| #include <linux/slab.h> | ||
| #include <linux/jiffies.h> | ||
| #include <linux/kernel.h> | ||
| #include <linux/moduleparam.h> | ||
| #include <linux/types.h> | ||
| #include <linux/container_of.h> | ||
| #include <linux/gfp.h> | ||
| #include <linux/atomic.h> | ||
|
|
||
| #include "xe_device.h" | ||
| #include "xe_gt.h" | ||
| #include "xe_gt_types.h" | ||
| #include "xe_guc_pc.h" | ||
| #include "xe_module.h" | ||
|
|
||
| #define CREATE_TRACE_POINTS | ||
| #include "xe_gpufreqtracer_trace.h" | ||
|
|
||
| /** | ||
| * struct xe_gpufreqtracer_gt_data - Per-GT frequency monitoring data | ||
| * @gt: Reference to the GT | ||
| * @timer: Timer for periodic monitoring | ||
| * @work: Work item for frequency sampling | ||
| * @last_frequency: Last reported frequency to avoid duplicate reports (atomic) | ||
| * @monitoring_active: Whether monitoring is currently active (atomic) | ||
| */ | ||
| struct xe_gpufreqtracer_gt_data { | ||
| struct xe_gt *gt; | ||
| struct timer_list timer; | ||
| struct work_struct work; | ||
| atomic_t last_frequency; | ||
| atomic_t monitoring_active; | ||
| }; | ||
|
|
||
| /** | ||
| * struct xe_gpufreqtracer_data - Per-device frequency tracer data | ||
| * @xe: Reference to the XE device | ||
| * @gt_data: Array of per-GT monitoring data | ||
| */ | ||
| struct xe_gpufreqtracer_data { | ||
| struct xe_device *xe; | ||
| struct xe_gpufreqtracer_gt_data *gt_data; | ||
| }; | ||
|
|
||
|
|
||
| /** | ||
| * xe_gpufreqtracer_sample_work - Worker function to sample GPU frequency. | ||
| * @work: Pointer to the work_struct representing the scheduled work. | ||
| * | ||
| * This function is executed in a workqueue context to periodically sample | ||
| * the GPU frequency and perform any necessary tracing or logging operations. | ||
| * It is part of the GPU frequency tracer subsystem. | ||
| */ | ||
| static void xe_gpufreqtracer_sample_work(struct work_struct *work) | ||
| { | ||
| struct xe_gpufreqtracer_gt_data *gt_data = | ||
| container_of(work, struct xe_gpufreqtracer_gt_data, work); | ||
| struct xe_gt *gt = gt_data->gt; | ||
| struct xe_guc_pc *pc = >->uc.guc.pc; | ||
| u32 current_freq; | ||
| u32 last_freq; | ||
|
|
||
| if (!atomic_read(>_data->monitoring_active)) { | ||
| drm_warn(>_to_xe(gt)->drm, "monitoring not active for GT%u, exiting", | ||
| gt->info.id); | ||
| return; | ||
| } | ||
|
|
||
| current_freq = xe_guc_pc_get_act_freq(pc) * 1000; /* Convert MHz to KHz */ | ||
| last_freq = atomic_read(>_data->last_frequency); | ||
|
|
||
| /* Only report if frequency has changed or this is the first sample */ | ||
| if (current_freq != last_freq) { | ||
| drm_dbg(>_to_xe(gt)->drm, "GT%u frequency changed, tracing %u KHz", | ||
| gt->info.id, current_freq); | ||
| trace_gpu_frequency(current_freq, gt->info.id); | ||
| atomic_set(>_data->last_frequency, current_freq); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * xe_gpufreqtracer_timer_callback - Timer callback for GPU frequency tracer | ||
| * @timer: Pointer to the timer_list structure associated with this callback | ||
| * | ||
| * This function is invoked when the timer associated with the GPU frequency tracer expires. | ||
| * It is responsible for handling periodic tasks related to GPU frequency tracing, such as | ||
| * sampling or logging frequency data. | ||
| */ | ||
| static void xe_gpufreqtracer_timer_callback(struct timer_list *timer) | ||
| { | ||
| struct xe_gpufreqtracer_gt_data *gt_data = | ||
| container_of(timer, struct xe_gpufreqtracer_gt_data, timer); | ||
|
|
||
| if (atomic_read(>_data->monitoring_active)) { | ||
| queue_work(system_highpri_wq, >_data->work); | ||
| mod_timer(>_data->timer, jiffies + | ||
| msecs_to_jiffies(xe_modparam.gpufreq_monitoring_interval_ms)); | ||
| } else { | ||
| drm_warn(>_to_xe(gt_data->gt)->drm, "timer callback for GT%u but monitoring inactive", | ||
| gt_data->gt->info.id); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * xe_gpufreqtracer_init - Initialize GPU frequency tracer for a device | ||
| * @xe: The XE device | ||
| * | ||
| * Sets up the frequency tracer infrastructure for all GTs in the device. | ||
| * | ||
| * Return: 0 on success, negative error code on failure | ||
| */ | ||
| int xe_gpufreqtracer_init(struct xe_device *xe) | ||
| { | ||
| struct xe_gpufreqtracer_data *tracer_data; | ||
| struct xe_gt *gt; | ||
| u8 tile_id; | ||
| int ret = 0; | ||
|
|
||
| tracer_data = kzalloc(sizeof(*tracer_data), GFP_KERNEL); | ||
| if (!tracer_data) | ||
| return -ENOMEM; | ||
|
|
||
| tracer_data->xe = xe; | ||
|
|
||
| /* Allocate GT data array based on actual GT count */ | ||
| tracer_data->gt_data = kcalloc(xe->info.gt_count, | ||
| sizeof(*tracer_data->gt_data), | ||
| GFP_KERNEL); | ||
| if (!tracer_data->gt_data) { | ||
| ret = -ENOMEM; | ||
| goto err_free_tracer; | ||
| } | ||
|
|
||
| /* Initialize per-GT data */ | ||
| for_each_gt(gt, xe, tile_id) { | ||
| struct xe_gpufreqtracer_gt_data *gt_data = | ||
| &tracer_data->gt_data[gt->info.id]; | ||
|
|
||
| drm_dbg(&xe->drm, "initializing GT%u (tile %u)", gt->info.id, tile_id); | ||
|
|
||
| gt_data->gt = gt; | ||
| atomic_set(>_data->monitoring_active, 0); | ||
| atomic_set(>_data->last_frequency, 0); | ||
|
|
||
| INIT_WORK(>_data->work, xe_gpufreqtracer_sample_work); | ||
| timer_setup(>_data->timer, xe_gpufreqtracer_timer_callback, 0); | ||
|
|
||
| drm_dbg(&xe->drm, "GT%u initialized with global interval=%u ms", | ||
| gt->info.id, xe_modparam.gpufreq_monitoring_interval_ms); | ||
| } | ||
|
|
||
| xe->gpufreqtracer_data = tracer_data; | ||
| return 0; | ||
|
|
||
| err_free_tracer: | ||
| drm_err(&xe->drm, "initialization failed, freeing tracer data"); | ||
| kfree(tracer_data); | ||
| return ret; | ||
| } | ||
|
|
||
| /** | ||
| * xe_gpufreqtracer_fini - Cleanup GPU frequency tracer for a device | ||
| * @xe: The XE device | ||
| * | ||
| * Stops all monitoring and cleans up tracer resources. | ||
| */ | ||
| void xe_gpufreqtracer_fini(struct xe_device *xe) | ||
| { | ||
| struct xe_gpufreqtracer_data *tracer_data = xe->gpufreqtracer_data; | ||
| struct xe_gt *gt; | ||
| u8 tile_id; | ||
|
|
||
| if (!tracer_data) { | ||
| drm_warn(&xe->drm, "no tracer data found, nothing to cleanup"); | ||
| return; | ||
| } | ||
|
|
||
| /* Stop all monitoring */ | ||
| for_each_gt(gt, xe, tile_id) { | ||
| drm_dbg(&xe->drm, "stopping monitoring for GT%u", gt->info.id); | ||
| xe_gpufreqtracer_stop_monitoring(gt); | ||
| } | ||
|
|
||
| kfree(tracer_data->gt_data); | ||
| kfree(tracer_data); | ||
| xe->gpufreqtracer_data = NULL; | ||
| } | ||
|
|
||
| /** | ||
| * xe_gpufreqtracer_report_frequency_change - Report frequency change directly | ||
| * @gt: The GT instance | ||
| * @frequency_khz: The new frequency in KHz | ||
| * | ||
| * Reports a frequency change immediately through the tracepoint. | ||
| */ | ||
| void xe_gpufreqtracer_report_frequency_change(struct xe_gt *gt, u32 frequency_khz) | ||
| { | ||
| drm_dbg(>_to_xe(gt)->drm, "direct frequency report for GT%u: %u KHz", | ||
| gt->info.id, frequency_khz); | ||
|
|
||
| if (frequency_khz > 0) { | ||
| trace_gpu_frequency(frequency_khz, gt->info.id); | ||
| drm_dbg(>_to_xe(gt)->drm, "traced frequency change for GT%u", gt->info.id); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * xe_gpufreqtracer_start_monitoring - Start periodic frequency monitoring | ||
| * @gt: The GT instance | ||
| * | ||
| * Starts periodic sampling of GPU frequency for the specified GT using the global | ||
| * monitoring interval from module parameters. | ||
| * | ||
| * Return: 0 on success, negative error code on failure | ||
| */ | ||
| int xe_gpufreqtracer_start_monitoring(struct xe_gt *gt) | ||
| { | ||
| struct xe_gpufreqtracer_data *tracer_data = gt_to_xe(gt)->gpufreqtracer_data; | ||
| struct xe_gpufreqtracer_gt_data *gt_data; | ||
|
|
||
| if (!tracer_data) { | ||
| drm_warn(>_to_xe(gt)->drm, "no tracer data for GT%u, not supported", gt->info.id); | ||
| return -EOPNOTSUPP; | ||
| } | ||
|
|
||
| if (gt->info.id >= gt_to_xe(gt)->info.gt_count) { | ||
| drm_err(>_to_xe(gt)->drm, "invalid GT ID %u, max supported is %u", | ||
| gt->info.id, gt_to_xe(gt)->info.gt_count - 1); | ||
| return -EINVAL; | ||
| } | ||
|
|
||
| gt_data = &tracer_data->gt_data[gt->info.id]; | ||
|
|
||
| if (atomic_read(>_data->monitoring_active)) { | ||
| drm_warn(>_to_xe(gt)->drm, "monitoring already active for GT%u", gt->info.id); | ||
| return -EALREADY; | ||
| } | ||
|
|
||
| atomic_set(>_data->monitoring_active, 1); | ||
| atomic_set(>_data->last_frequency, 0); | ||
|
|
||
| /* Start the timer using global interval */ | ||
| mod_timer(>_data->timer, jiffies + | ||
| msecs_to_jiffies(xe_modparam.gpufreq_monitoring_interval_ms)); | ||
|
|
||
| drm_dbg(>_to_xe(gt)->drm, "monitoring started for GT%u with interval %u ms", | ||
| gt->info.id, xe_modparam.gpufreq_monitoring_interval_ms); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * xe_gpufreqtracer_stop_monitoring - Stop periodic frequency monitoring | ||
| * @gt: The GT instance | ||
| * | ||
| * Stops periodic sampling of GPU frequency for the specified GT. | ||
| */ | ||
| void xe_gpufreqtracer_stop_monitoring(struct xe_gt *gt) | ||
| { | ||
| struct xe_gpufreqtracer_data *tracer_data = gt_to_xe(gt)->gpufreqtracer_data; | ||
| struct xe_gpufreqtracer_gt_data *gt_data; | ||
|
|
||
| if (!tracer_data || gt->info.id >= gt_to_xe(gt)->info.gt_count) { | ||
| drm_err(>_to_xe(gt)->drm, "invalid tracer data or GT ID %u for stop request", | ||
| gt->info.id); | ||
| return; | ||
| } | ||
|
|
||
| gt_data = &tracer_data->gt_data[gt->info.id]; | ||
|
|
||
| if (!atomic_read(>_data->monitoring_active)) { | ||
| drm_warn(>_to_xe(gt)->drm, "monitoring not active for GT%u, nothing to stop", | ||
| gt->info.id); | ||
| return; | ||
| } | ||
|
|
||
| atomic_set(>_data->monitoring_active, 0); | ||
|
|
||
| del_timer_sync(>_data->timer); | ||
| cancel_work_sync(>_data->work); | ||
| } | ||
Oops, something went wrong.
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.
do we need to create dedicated work queue? we can use system_highpri_wq a global workqueue provided by the kernel.
upstream people might ask this questions. Lets have a proper reason for the same.
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.
As per the comment code has been modified.