Vendor agnostic gputop
Subsystems (2)
cover Vendor agnostic gputop
From: Tvrtko Ursulin <[email protected]>
This is a pile of patches which implements a rudimentary vendor agnostic gputop tool based of the new DRM spec as documented in Documentation/gpu/drm-usage-stats.rst.
First part of the series is code refactoring which should be reasonably stable, I've tested it all while working on it both against intel_gpu_top and gputop, albeit probably needs more and better comments.
The actual tools itself (last patch) works but I think not quite nice enough to be user facing. Ideally someone gets intrigued enough to improve it? Although it is only IGT so maybe it is good enough for a start? Not sure. To start with it depends on more drivers adopting the spec.
Fundamental difference between intel_gpu_top and gputop is that the former is centered around a single card and only shows processes belonging to it. Gputop on the other hand has an idea to show all processes with DRM file descriptors open and sort them into groups per card. It also makes no effort to provide sorting modes, well any interactivity, or any pretty names for GPUs or engines.
It looks like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DRM minor 0
PID NAME render copy video
3816 kwin_x11 |███▎ || || || |
3523 Xorg |▊ || || || |
1120449 mpv | || ||▋ || |
1120529 glxgears |▋ || || || |
1120449 mpv |▍ || || || |
3860 plasmashell |▏ || || || |
4764 krunner | || || || |
575206 chrome | || || || |
833481 firefox | || || || |
892924 thunderbird | || || || |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I did test it as well with two cards and confirmed that too works.
Rob Clark also tested it with a patch which exports the respective data from the msm driver and confirmed that too works.
Tvrtko Ursulin (8):
lib: Extract igt_drm_clients from intel_gpu_top
libdrmfdinfo: Allow specifying custom engine map
libdrmclients: Record client drm minor
libdrmclient: Support multiple DRM cards
libdrmfdinfo: Track largest engine index libdrmclient/intel_gpu_top: Decouple hardcoded engine assumptions
libdrmclient: Enforce client status sort order in the library
gputop: Basic vendor agnostic GPU top tool
lib/igt_drm_clients.c | 451 +++++++++++++++++++++++++++ lib/igt_drm_clients.h | 97 ++++++ lib/igt_drm_fdinfo.c | 50 ++- lib/igt_drm_fdinfo.h | 16 +- lib/meson.build | 8 + tests/i915/drm_fdinfo.c | 18 +- tools/gputop.c | 276 +++++++++++++++++ tools/intel_gpu_top.c | 663 ++++++++++------------------------------ tools/meson.build | 7 +- 9 files changed, 1067 insertions(+), 519 deletions(-) create mode 100644 lib/igt_drm_clients.c create mode 100644 lib/igt_drm_clients.h create mode 100644 tools/gputop.c
Signature · 1 line
2.32.0
PATCH 1/8 lib: Extract igt_drm_clients from intel_gpu_top
From: Tvrtko Ursulin <[email protected]>
Code movement with some improvements to prepare for further work in making a vendor agnostic gputop tool possible.
--- lib/igt_drm_clients.c | 395 ++++++++++++++++++++++++++++++++ lib/igt_drm_clients.h | 96 ++++++++ lib/meson.build | 8 + tools/intel_gpu_top.c | 521 ++++++------------------------------------ tools/meson.build | 2 +- 5 files changed, 573 insertions(+), 449 deletions(-) create mode 100644 lib/igt_drm_clients.c create mode 100644 lib/igt_drm_clients.h
lib/igt_drm_clients.c new file +395 -0
@@ -0,0 +1,395 @@ +/* +* Copyright © 2022 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +*/ + +#include <assert.h> +#include <ctype.h> +#include <dirent.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <strings.h> +#include <stdlib.h> +#include <sys/stat.h> +#include <sys/sysmacros.h> +#include <sys/types.h> +#include <unistd.h> + +#include "igt_drm_clients.h" +#include "igt_drm_fdinfo.h" + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE (array) (sizeof(array) / sizeof(array[0])) +#endif + +struct igt_drm_clients *igt_drm_clients_init (void *private_data) +{ +struct igt_drm_clients *clients; + + clients =malloc (sizeof (*clients)); +if (!clients) +return NULL ; + +memset (clients,0 ,sizeof (*clients)); + + clients->private_data = private_data; + +return clients; +} + +struct igt_drm_client * +igt_drm_clients_find (struct igt_drm_clients *clients, +enum igt_drm_client_status status, +unsigned int id) +{ +unsigned int start, num; +struct igt_drm_client *c; + + start = status ==IGT_DRM_CLIENT_FREE ? clients->active_clients :0 ;/* Free block at the end. */ + num = clients->num_clients - start; + +for (c = &clients->client[start]; num; c++, num--) { +if (status != c->status) +continue ; + +if (status ==IGT_DRM_CLIENT_FREE || c->id == id) +return c; + } + +return NULL ; +} + +static void +igt_drm_client_update (struct igt_drm_client *c,unsigned int pid,char *name, +const struct drm_client_fdinfo *info) +{ +unsigned int i; + +if (c->pid != pid) + c->pid = pid; + +if (strcmp (c->name, name)) { +char *p; + +strncpy (c->name, name,sizeof (c->name) -1 ); +strncpy (c->print_name, name,sizeof (c->print_name) -1 ); + + p = c->print_name; +while (*p) { +if (!isprint (*p)) + *p ='*' ; + p++; + } + } + + c->last_runtime =0 ; + c->total_runtime =0 ; + +for (i =0 ; i < c->clients->num_classes; i++) { +assert (i <ARRAY_SIZE (info->busy)); + +if (info->busy[i] < c->last[i]) +continue ;/* It will catch up soon. */ + + c->total_runtime += info->busy[i]; + c->val[i] = info->busy[i] - c->last[i]; + c->last_runtime += c->val[i]; + c->last[i] = info->busy[i]; + } + + c->samples++; + c->status =IGT_DRM_CLIENT_ALIVE ; +} + +static void +igt_drm_client_add (struct igt_drm_clients *clients, +const struct drm_client_fdinfo *info, +unsigned int pid,char *name) +{ +struct igt_drm_client *c; + +assert (!igt_drm_clients_find (clients,IGT_DRM_CLIENT_ALIVE , info->id)); + + c =igt_drm_clients_find (clients,IGT_DRM_CLIENT_FREE ,0 ); +if (!c) { +unsigned int idx = clients->num_clients; + + clients->num_clients += (clients->num_clients +2 ) /2 ; + clients->client =realloc (clients->client, + clients->num_clients *sizeof (*c)); +assert (clients->client); + + c = &clients->client[idx]; +memset (c,0 , (clients->num_clients - idx) *sizeof (*c)); + } + + c->id = info->id; + c->clients = clients; + c->val =calloc (clients->num_classes,sizeof (c->val)); + c->last =calloc (clients->num_classes,sizeof (c->last)); +assert (c->val && c->last); + +igt_drm_client_update (c, pid, name, info); +} + +void igt_drm_client_free (struct igt_drm_client *c) +{ +free (c->val); +free (c->last); +memset (c,0 ,sizeof (*c)); +} + +struct igt_drm_clients * +igt_drm_clients_sort (struct igt_drm_clients *clients, +int (*cmp)(const void *,const void *)) +{ +unsigned int active, free; +struct igt_drm_client *c; +int tmp; + +if (!clients) +return clients; + +qsort (clients->client, clients->num_clients,sizeof (*clients->client), + cmp); + +/* Trim excessive array space. */ + active =0 ; +igt_for_each_drm_client (clients, c, tmp) { +if (c->status !=IGT_DRM_CLIENT_ALIVE ) +break ;/* Active clients are first in the array. */ + active++; + } + + clients->active_clients = active; + + free = clients->num_clients - active; +if (free > clients->num_clients /2 ) { + active = clients->num_clients - free /2 ; +if (active != clients->num_clients) { + clients->num_clients = active; + clients->client =realloc (clients->client, + clients->num_clients * +sizeof (*c)); + } + } + +return clients; +} + +void igt_drm_clients_free (struct igt_drm_clients *clients) +{ +struct igt_drm_client *c; +unsigned int tmp; + +igt_for_each_drm_client (clients, c, tmp) +igt_drm_client_free (c); + +free (clients->client); +free (clients); +} + +static DIR *opendirat (int at,const char *name) +{ +DIR *dir; +int fd; + + fd =openat (at, name,O_DIRECTORY ); +if (fd <0 ) +return NULL ; + + dir =fdopendir (fd); +if (!dir) +close (fd); + +return dir; +} + +static size_t readat2buf (char *buf,const size_t sz,int at,const char *name) +{ +size_t count; +int fd; + + fd =openat (at, name,O_RDONLY ); +if (fd <0 ) +return 0 ; + + buf[sz -1 ] =0 ; + count =read (fd, buf, sz); + buf[count -1 ] =0 ; +close (fd); + +return count; +} + +static bool get_task_name (const char *buffer,char *out,unsigned long sz) +{ +char *s =index (buffer,'(' ); +char *e =rindex (buffer,')' ); +unsigned int len; + +if (!s || !e) +return false; +assert (e >= s); + + len = e - ++s; +if (!len || (len +1 ) >= sz) +return false; + +strncpy (out, s, len); + out[len] =0 ; + +return true; +} + +static bool is_drm_fd (int fd_dir,const char *name) +{ +struct stat stat; +int ret; + + ret =fstatat (fd_dir, name, &stat,0 ); + +return ret ==0 && + (stat.st_mode &S_IFMT ) ==S_IFCHR && +major (stat.st_rdev) ==226 ; +} + +struct igt_drm_clients * +igt_drm_clients_scan (struct igt_drm_clients *clients, +bool (*filter_client)(const struct igt_drm_clients *, +const struct drm_client_fdinfo *)) +{ +struct dirent *proc_dent; +struct igt_drm_client *c; +DIR *proc_dir; +int tmp; + +if (!clients) +return clients; + +igt_for_each_drm_client (clients, c, tmp) { +assert (c->status !=IGT_DRM_CLIENT_PROBE ); +if (c->status ==IGT_DRM_CLIENT_ALIVE ) + c->status =IGT_DRM_CLIENT_PROBE ; +else +break ;/* Free block at the end of array. */ + } + + proc_dir =opendir ("/proc" ); +if (!proc_dir) +return clients; + +while ((proc_dent =readdir (proc_dir)) !=NULL ) { +int pid_dir =-1 , fd_dir =-1 ; +struct dirent *fdinfo_dent; +char client_name[64 ] = { }; +unsigned int client_pid; +DIR *fdinfo_dir =NULL ; +char buf[4096 ]; +size_t count; + +if (proc_dent->d_type !=DT_DIR ) +continue ; +if (!isdigit (proc_dent->d_name[0 ])) +continue ; + + pid_dir =openat (dirfd (proc_dir), proc_dent->d_name, +O_DIRECTORY |O_RDONLY ); +if (pid_dir <0 ) +continue ; + + count =readat2buf (buf,sizeof (buf), pid_dir,"stat" ); +if (!count) + goto next; + + client_pid =atoi (buf); +if (!client_pid) + goto next; + +if (!get_task_name (buf, client_name,sizeof (client_name))) + goto next; + + fd_dir =openat (pid_dir,"fd" ,O_DIRECTORY |O_RDONLY ); +if (fd_dir <0 ) + goto next; + + fdinfo_dir =opendirat (pid_dir,"fdinfo" ); +if (!fdinfo_dir) + goto next; + +while ((fdinfo_dent =readdir (fdinfo_dir)) !=NULL ) { +struct drm_client_fdinfo info; + +if (fdinfo_dent->d_type !=DT_REG ) +continue ; +if (!isdigit (fdinfo_dent->d_name[0 ])) +continue ; + +if (!is_drm_fd (fd_dir, fdinfo_dent->d_name)) +continue ; + +memset (&info,0 ,sizeof (info)); + +if (!__igt_parse_drm_fdinfo (dirfd (fdinfo_dir), + fdinfo_dent->d_name, + &info)) +continue ; + +if (filter_client && !filter_client (clients, &info)) +continue ; + +if (igt_drm_clients_find (clients,IGT_DRM_CLIENT_ALIVE , + info.id)) +continue ;/* Skip duplicate fds. */ + + c =igt_drm_clients_find (clients,IGT_DRM_CLIENT_PROBE , + info.id); +if (!c) +igt_drm_client_add (clients, &info, client_pid, + client_name); +else +igt_drm_client_update (c, client_pid, + client_name, &info); + } + +next: +if (fdinfo_dir) +closedir (fdinfo_dir); +if (fd_dir >=0 ) +close (fd_dir); +if (pid_dir >=0 ) +close (pid_dir); + } + +closedir (proc_dir); + +igt_for_each_drm_client (clients, c, tmp) { +if (c->status ==IGT_DRM_CLIENT_PROBE ) +igt_drm_client_free (c); +else if (c->status ==IGT_DRM_CLIENT_FREE ) +break ; + } + +return clients; +}
lib/igt_drm_clients.h new file +96 -0
96 lines not on this page · raw message
lib/meson.build +8 -0
@@ -18,6 +18,7 @@ lib_sources = [ 'igt_debugfs.c', 'igt_device.c', 'igt_device_scan.c', + 'igt_drm_clients.h', 'igt_drm_fdinfo.c', 'igt_aux.c', 'igt_gt.c',
@@ -219,6 +220,13 @@ lib_igt_device_scan_build = static_library('igt_device_scan', lib_igt_device_scan = declare_dependency(link_with : lib_igt_device_scan_build, include_directories : inc) +lib_igt_drm_clients_build = static_library('igt_drm_clients', + ['igt_drm_clients.c'], + include_directories : inc) + +lib_igt_drm_clients = declare_dependency(link_with : lib_igt_drm_clients_build, + include_directories : inc) + lib_igt_drm_fdinfo_build = static_library('igt_drm_fdinfo', ['igt_drm_fdinfo.c'], include_directories : inc)
tools/intel_gpu_top.c +73 -448
736 lines not on this page · raw message
tools/meson.build +1 -1
@@ -91,7 +91,7 @@ install_subdir('registers', install_dir : datadir) executable('intel_gpu_top', 'intel_gpu_top.c', install : true, install_rpath : bindir_rpathdir, - dependencies : [lib_igt_perf,lib_igt_device_scan,lib_igt_drm_fdinfo,math]) + dependencies : [lib_igt_perf,lib_igt_device_scan,lib_igt_drm_clients,lib_igt_drm_fdinfo,math]) executable('amd_hdmi_compliance', 'amd_hdmi_compliance.c', dependencies : [tool_deps],
Signature · 1 line
2.32.0
Re: [igt-dev] [PATCH i-g-t 1/8] lib: Extract igt_drm_clients from intel_gpu_top
Quoted 35 lines · Tvrtko Ursulin
From: Tvrtko Ursulin <[email protected]>
Code movement with some improvements to prepare for further work in making a vendor agnostic gputop tool possible.
Signed-off-by Tvrtko Ursulin <[email protected]>--- lib/igt_drm_clients.c | 395 ++++++++++++++++++++++++++++++++ lib/igt_drm_clients.h | 96 ++++++++ lib/meson.build | 8 + tools/intel_gpu_top.c | 521 ++++++------------------------------------ tools/meson.build | 2 +- 5 files changed, 573 insertions(+), 449 deletions(-) create mode 100644 lib/igt_drm_clients.c create mode 100644 lib/igt_drm_clients.hlib/igt_drm_clients.c new file +13
-0@@ -0,0 +1,395 @@ +/* +* Copyright © 2022 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software.
lib/igt_drm_clients.c line 23
Quoted 10 lines
lib/igt_drm_clients.c new file +10
-0+* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +*/
Add SPDX line to new files please.
Quoted 362 lines
lib/igt_drm_clients.c new file +362
-0+ +#include <assert.h> +#include <ctype.h> +#include <dirent.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <strings.h> +#include <stdlib.h> +#include <sys/stat.h> +#include <sys/sysmacros.h> +#include <sys/types.h> +#include <unistd.h> + +#include "igt_drm_clients.h" +#include "igt_drm_fdinfo.h" + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE (array) (sizeof(array) / sizeof(array[0])) +#endif + +struct igt_drm_clients *igt_drm_clients_init (void *private_data) +{ +struct igt_drm_clients *clients; + + clients =malloc (sizeof (*clients)); +if (!clients) +return NULL ; + +memset (clients,0 ,sizeof (*clients)); + + clients->private_data = private_data; + +return clients; +} + +struct igt_drm_client * +igt_drm_clients_find (struct igt_drm_clients *clients, +enum igt_drm_client_status status, +unsigned int id) +{ +unsigned int start, num; +struct igt_drm_client *c; + + start = status ==IGT_DRM_CLIENT_FREE ? clients->active_clients :0 ;/* Free block at the end. */ + num = clients->num_clients - start; + +for (c = &clients->client[start]; num; c++, num--) { +if (status != c->status) +continue ; + +if (status ==IGT_DRM_CLIENT_FREE || c->id == id) +return c; + } + +return NULL ; +} + +static void +igt_drm_client_update (struct igt_drm_client *c,unsigned int pid,char *name, +const struct drm_client_fdinfo *info) +{ +unsigned int i; + +if (c->pid != pid) + c->pid = pid; + +if (strcmp (c->name, name)) { +char *p; + +strncpy (c->name, name,sizeof (c->name) -1 ); +strncpy (c->print_name, name,sizeof (c->print_name) -1 ); + + p = c->print_name; +while (*p) { +if (!isprint (*p)) + *p ='*' ; + p++; + } + } + + c->last_runtime =0 ; + c->total_runtime =0 ; + +for (i =0 ; i < c->clients->num_classes; i++) { +assert (i <ARRAY_SIZE (info->busy)); + +if (info->busy[i] < c->last[i]) +continue ;/* It will catch up soon. */ + + c->total_runtime += info->busy[i]; + c->val[i] = info->busy[i] - c->last[i]; + c->last_runtime += c->val[i]; + c->last[i] = info->busy[i]; + } + + c->samples++; + c->status =IGT_DRM_CLIENT_ALIVE ; +} + +static void +igt_drm_client_add (struct igt_drm_clients *clients, +const struct drm_client_fdinfo *info, +unsigned int pid,char *name) +{ +struct igt_drm_client *c; + +assert (!igt_drm_clients_find (clients,IGT_DRM_CLIENT_ALIVE , info->id)); + + c =igt_drm_clients_find (clients,IGT_DRM_CLIENT_FREE ,0 ); +if (!c) { +unsigned int idx = clients->num_clients; + + clients->num_clients += (clients->num_clients +2 ) /2 ; + clients->client =realloc (clients->client, + clients->num_clients *sizeof (*c)); +assert (clients->client); + + c = &clients->client[idx]; +memset (c,0 , (clients->num_clients - idx) *sizeof (*c)); + } + + c->id = info->id; + c->clients = clients; + c->val =calloc (clients->num_classes,sizeof (c->val)); + c->last =calloc (clients->num_classes,sizeof (c->last)); +assert (c->val && c->last); + +igt_drm_client_update (c, pid, name, info); +} + +void igt_drm_client_free (struct igt_drm_client *c) +{ +free (c->val); +free (c->last); +memset (c,0 ,sizeof (*c)); +} + +struct igt_drm_clients * +igt_drm_clients_sort (struct igt_drm_clients *clients, +int (*cmp)(const void *,const void *)) +{ +unsigned int active, free; +struct igt_drm_client *c; +int tmp; + +if (!clients) +return clients; + +qsort (clients->client, clients->num_clients,sizeof (*clients->client), + cmp); + +/* Trim excessive array space. */ + active =0 ; +igt_for_each_drm_client (clients, c, tmp) { +if (c->status !=IGT_DRM_CLIENT_ALIVE ) +break ;/* Active clients are first in the array. */ + active++; + } + + clients->active_clients = active; + + free = clients->num_clients - active; +if (free > clients->num_clients /2 ) { + active = clients->num_clients - free /2 ; +if (active != clients->num_clients) { + clients->num_clients = active; + clients->client =realloc (clients->client, + clients->num_clients * +sizeof (*c)); + } + } + +return clients; +} + +void igt_drm_clients_free (struct igt_drm_clients *clients) +{ +struct igt_drm_client *c; +unsigned int tmp; + +igt_for_each_drm_client (clients, c, tmp) +igt_drm_client_free (c); + +free (clients->client); +free (clients); +} + +static DIR *opendirat (int at,const char *name) +{ +DIR *dir; +int fd; + + fd =openat (at, name,O_DIRECTORY ); +if (fd <0 ) +return NULL ; + + dir =fdopendir (fd); +if (!dir) +close (fd); + +return dir; +} + +static size_t readat2buf (char *buf,const size_t sz,int at,const char *name) +{ +size_t count; +int fd; + + fd =openat (at, name,O_RDONLY ); +if (fd <0 ) +return 0 ; + + buf[sz -1 ] =0 ; + count =read (fd, buf, sz); + buf[count -1 ] =0 ; +close (fd); + +return count; +} + +static bool get_task_name (const char *buffer,char *out,unsigned long sz) +{ +char *s =index (buffer,'(' ); +char *e =rindex (buffer,')' ); +unsigned int len; + +if (!s || !e) +return false; +assert (e >= s); + + len = e - ++s; +if (!len || (len +1 ) >= sz) +return false; + +strncpy (out, s, len); + out[len] =0 ; + +return true; +} + +static bool is_drm_fd (int fd_dir,const char *name) +{ +struct stat stat; +int ret; + + ret =fstatat (fd_dir, name, &stat,0 ); + +return ret ==0 && + (stat.st_mode &S_IFMT ) ==S_IFCHR && +major (stat.st_rdev) ==226 ; +} + +struct igt_drm_clients * +igt_drm_clients_scan (struct igt_drm_clients *clients, +bool (*filter_client)(const struct igt_drm_clients *, +const struct drm_client_fdinfo *)) +{ +struct dirent *proc_dent; +struct igt_drm_client *c; +DIR *proc_dir; +int tmp; + +if (!clients) +return clients; + +igt_for_each_drm_client (clients, c, tmp) { +assert (c->status !=IGT_DRM_CLIENT_PROBE ); +if (c->status ==IGT_DRM_CLIENT_ALIVE ) + c->status =IGT_DRM_CLIENT_PROBE ; +else +break ;/* Free block at the end of array. */ + } + + proc_dir =opendir ("/proc" ); +if (!proc_dir) +return clients; + +while ((proc_dent =readdir (proc_dir)) !=NULL ) { +int pid_dir =-1 , fd_dir =-1 ; +struct dirent *fdinfo_dent; +char client_name[64 ] = { }; +unsigned int client_pid; +DIR *fdinfo_dir =NULL ; +char buf[4096 ]; +size_t count; + +if (proc_dent->d_type !=DT_DIR ) +continue ; +if (!isdigit (proc_dent->d_name[0 ])) +continue ; + + pid_dir =openat (dirfd (proc_dir), proc_dent->d_name, +O_DIRECTORY |O_RDONLY ); +if (pid_dir <0 ) +continue ; + + count =readat2buf (buf,sizeof (buf), pid_dir,"stat" ); +if (!count) + goto next; + + client_pid =atoi (buf); +if (!client_pid) + goto next; + +if (!get_task_name (buf, client_name,sizeof (client_name))) + goto next; + + fd_dir =openat (pid_dir,"fd" ,O_DIRECTORY |O_RDONLY ); +if (fd_dir <0 ) + goto next; + + fdinfo_dir =opendirat (pid_dir,"fdinfo" ); +if (!fdinfo_dir) + goto next; + +while ((fdinfo_dent =readdir (fdinfo_dir)) !=NULL ) { +struct drm_client_fdinfo info; + +if (fdinfo_dent->d_type !=DT_REG ) +continue ; +if (!isdigit (fdinfo_dent->d_name[0 ])) +continue ; + +if (!is_drm_fd (fd_dir, fdinfo_dent->d_name)) +continue ; + +memset (&info,0 ,sizeof (info)); + +if (!__igt_parse_drm_fdinfo (dirfd (fdinfo_dir), + fdinfo_dent->d_name, + &info)) +continue ; + +if (filter_client && !filter_client (clients, &info)) +continue ; + +if (igt_drm_clients_find (clients,IGT_DRM_CLIENT_ALIVE , + info.id)) +continue ;/* Skip duplicate fds. */ + + c =igt_drm_clients_find (clients,IGT_DRM_CLIENT_PROBE , + info.id); +if (!c) +igt_drm_client_add (clients, &info, client_pid, + client_name); +else +igt_drm_client_update (c, client_pid, + client_name, &info); + } + +next: +if (fdinfo_dir) +closedir (fdinfo_dir); +if (fd_dir >=0 ) +close (fd_dir); +if (pid_dir >=0 ) +close (pid_dir); + } + +closedir (proc_dir);
lib/igt_drm_clients.c line 395
Quoted 10 lines
lib/igt_drm_clients.c new file +10
-0+ +igt_for_each_drm_client (clients, c, tmp) { +if (c->status ==IGT_DRM_CLIENT_PROBE ) +igt_drm_client_free (c); +else if (c->status ==IGT_DRM_CLIENT_FREE ) +break ; + } + +return clients; +}
All this stuff needs documentation.
Signature · 1 line
Petri Latvala
Quoted 907 lines
lib/igt_drm_clients.h new file +96
-0@@ -0,0 +1,96 @@ +/* +* Copyright © 2022 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +*/ + +#ifndef IGT_DRM_CLIENTS_H +#define IGT_DRM_CLIENTS_H + +#include <stdint.h> + +struct drm_client_fdinfo ; + +enum igt_drm_client_status { +IGT_DRM_CLIENT_FREE =0 ,/* mbz */ +IGT_DRM_CLIENT_ALIVE , +IGT_DRM_CLIENT_PROBE +}; + +struct igt_drm_client_engine_class { +unsigned int engine_class; +const char *name; +unsigned int num_engines; +}; + +struct igt_drm_clients ; + +struct igt_drm_client { +struct igt_drm_clients *clients; + +enum igt_drm_client_status status; +unsigned int id; +unsigned int pid; +char name[24 ]; +char print_name[24 ]; +unsigned int samples; +unsigned long total_runtime; +unsigned long last_runtime; +unsigned long *val; +uint64_t *last; +}; + +struct igt_drm_clients { +unsigned int num_clients; +unsigned int active_clients; + +unsigned int num_classes; +struct igt_drm_client_engine_class *engine_class; + +void *private_data; + +struct igt_drm_client *client;/* Must be last. */ +}; + +#define igt_for_each_drm_client (clients, c, tmp) \ + for ((tmp) = (clients)->num_clients, c = (clients)->client; \ + (tmp > 0); (tmp)--, (c)++) + +struct igt_drm_clients *igt_drm_clients_init (void *private_data); +void igt_drm_clients_free (struct igt_drm_clients *clients); + +struct igt_drm_clients * +igt_drm_clients_scan (struct igt_drm_clients *clients, +bool (*filter_client)(const struct igt_drm_clients *, +const struct drm_client_fdinfo *)); + +struct igt_drm_client * +igt_drm_clients_find (struct igt_drm_clients *clients, +enum igt_drm_client_status status, +unsigned int id); + +struct igt_drm_clients * +igt_drm_clients_sort (struct igt_drm_clients *clients, +int (*cmp)(const void *,const void *)); + +void igt_drm_client_free (struct igt_drm_client *c); + +#endif /* IGT_DRM_CLIENTS_H */ lib/meson.build +8
-0@@ -18,6 +18,7 @@ lib_sources = [ 'igt_debugfs.c', 'igt_device.c', 'igt_device_scan.c', + 'igt_drm_clients.h', 'igt_drm_fdinfo.c', 'igt_aux.c', 'igt_gt.c',@@ -219,6 +220,13 @@ lib_igt_device_scan_build = static_library('igt_device_scan', lib_igt_device_scan = declare_dependency(link_with : lib_igt_device_scan_build, include_directories : inc) +lib_igt_drm_clients_build = static_library('igt_drm_clients', + ['igt_drm_clients.c'], + include_directories : inc) + +lib_igt_drm_clients = declare_dependency(link_with : lib_igt_drm_clients_build, + include_directories : inc) + lib_igt_drm_fdinfo_build = static_library('igt_drm_fdinfo', ['igt_drm_fdinfo.c'], include_directories : inc)tools/intel_gpu_top.c +73
-448@@ -46,6 +46,7 @@#include <sys/sysmacros.h> #include "igt_perf.h" +#include "igt_drm_clients.h" #include "igt_drm_fdinfo.h" #define ARRAY_SIZE (arr) (sizeof(arr)/sizeof(arr[0]))@@ -80,16 +81,10 @@ struct engine {struct pmu_counter sema; }; -struct engine_class { -unsigned int class; -const char *name; -unsigned int num_engines; -}; -struct engines {unsigned int num_engines;unsigned int num_classes; -struct engine_class *class; +struct igt_drm_client_engine_class *class;unsigned int num_counters;DIR *root;int fd;@@ -638,163 +633,10 @@ static void pmu_sample(struct engines *engines) } } -enum client_status { -FREE =0 ,/* mbz */ -ALIVE , -PROBE -}; - -struct clients ; - -struct client { -struct clients *clients; - -enum client_status status; -unsigned int id; -unsigned int pid; -char name[24 ]; -char print_name[24 ]; -unsigned int samples; -unsigned long total_runtime; -unsigned long last_runtime; -unsigned long *val; -uint64_t *last; -}; - -struct clients { -unsigned int num_clients; -unsigned int active_clients; - -unsigned int num_classes; -struct engine_class *class; - -char pci_slot[64 ]; - -struct client *client; -}; - -#define for_each_client (clients, c, tmp) \ - for ((tmp) = (clients)->num_clients, c = (clients)->client; \ - (tmp > 0); (tmp)--, (c)++) - -static struct clients *init_clients (const char *pci_slot) -{ -struct clients *clients; - - clients =malloc (sizeof (*clients)); -if (!clients) -return NULL ; - -memset (clients,0 ,sizeof (*clients)); - -strncpy (clients->pci_slot, pci_slot,sizeof (clients->pci_slot)); - -return clients; -} - -static struct client * -find_client (struct clients *clients,enum client_status status,unsigned int id) -{ -unsigned int start, num; -struct client *c; - - start = status ==FREE ? clients->active_clients :0 ;/* Free block at the end. */ - num = clients->num_clients - start; - -for (c = &clients->client[start]; num; c++, num--) { -if (status != c->status) -continue ; - -if (status ==FREE || c->id == id) -return c; - } - -return NULL ; -} - -static void -update_client (struct client *c,unsigned int pid,char *name, -const struct drm_client_fdinfo *info) -{ -unsigned int i; - -if (c->pid != pid) - c->pid = pid; - -if (strcmp (c->name, name)) { -char *p; - -strncpy (c->name, name,sizeof (c->name) -1 ); -strncpy (c->print_name, name,sizeof (c->print_name) -1 ); - - p = c->print_name; -while (*p) { -if (!isprint (*p)) - *p ='*' ; - p++; - } - } - - c->last_runtime =0 ; - c->total_runtime =0 ; - -for (i =0 ; i < c->clients->num_classes; i++) { -assert (i <ARRAY_SIZE (info->busy)); - -if (info->busy[i] < c->last[i]) -continue ;/* It will catch up soon. */ - - c->total_runtime += info->busy[i]; - c->val[i] = info->busy[i] - c->last[i]; - c->last_runtime += c->val[i]; - c->last[i] = info->busy[i]; - } - - c->samples++; - c->status =ALIVE ; -} - -static void -add_client (struct clients *clients,const struct drm_client_fdinfo *info, -unsigned int pid,char *name) -{ -struct client *c; - -assert (!find_client (clients,ALIVE , info->id)); - - c =find_client (clients,FREE ,0 ); -if (!c) { -unsigned int idx = clients->num_clients; - - clients->num_clients += (clients->num_clients +2 ) /2 ; - clients->client =realloc (clients->client, - clients->num_clients *sizeof (*c)); -assert (clients->client); - - c = &clients->client[idx]; -memset (c,0 , (clients->num_clients - idx) *sizeof (*c)); - } - - c->id = info->id; - c->clients = clients; - c->val =calloc (clients->num_classes,sizeof (c->val)); - c->last =calloc (clients->num_classes,sizeof (c->last)); -assert (c->val && c->last); - -update_client (c, pid, name, info); -} - -static void free_client (struct client *c) -{ -free (c->val); -free (c->last); -memset (c,0 ,sizeof (*c)); -} -static int client_last_cmp (const void *_a,const void *_b) { -const struct client *a = _a; -const struct client *b = _b; +const struct igt_drm_client *a = _a; +const struct igt_drm_client *b = _b;long tot_a, tot_b;/* @@ -803,8 +645,8 @@ static int client_last_cmp(const void *_a, const void *_b)* id. */ - tot_a = a->status ==ALIVE ? a->last_runtime :-1 ; - tot_b = b->status ==ALIVE ? b->last_runtime :-1 ; + tot_a = a->status ==IGT_DRM_CLIENT_ALIVE ? a->last_runtime :-1 ; + tot_b = b->status ==IGT_DRM_CLIENT_ALIVE ? b->last_runtime :-1 ; tot_b -= tot_a;if (tot_b >0 )@@ -817,12 +659,12 @@ static int client_last_cmp(const void *_a, const void *_b)static int client_total_cmp (const void *_a,const void *_b) { -const struct client *a = _a; -const struct client *b = _b; +const struct igt_drm_client *a = _a; +const struct igt_drm_client *b = _b;long tot_a, tot_b; - tot_a = a->status ==ALIVE ? a->total_runtime :-1 ; - tot_b = b->status ==ALIVE ? b->total_runtime :-1 ; + tot_a = a->status ==IGT_DRM_CLIENT_ALIVE ? a->total_runtime :-1 ; + tot_b = b->status ==IGT_DRM_CLIENT_ALIVE ? b->total_runtime :-1 ; tot_b -= tot_a;if (tot_b >0 )@@ -835,12 +677,12 @@ static int client_total_cmp(const void *_a, const void *_b)static int client_id_cmp (const void *_a,const void *_b) { -const struct client *a = _a; -const struct client *b = _b; +const struct igt_drm_client *a = _a; +const struct igt_drm_client *b = _b;int id_a, id_b; - id_a = a->status ==ALIVE ? a->id :-1 ; - id_b = b->status ==ALIVE ? b->id :-1 ; + id_a = a->status ==IGT_DRM_CLIENT_ALIVE ? a->id :-1 ; + id_b = b->status ==IGT_DRM_CLIENT_ALIVE ? b->id :-1 ; id_b -= id_a;if (id_b >0 )@@ -853,12 +695,12 @@ static int client_id_cmp(const void *_a, const void *_b)static int client_pid_cmp (const void *_a,const void *_b) { -const struct client *a = _a; -const struct client *b = _b; +const struct igt_drm_client *a = _a; +const struct igt_drm_client *b = _b;int pid_a, pid_b; - pid_a = a->status ==ALIVE ? a->pid :INT_MAX ; - pid_b = b->status ==ALIVE ? b->pid :INT_MAX ; + pid_a = a->status ==IGT_DRM_CLIENT_ALIVE ? a->pid :INT_MAX ; + pid_b = b->status ==IGT_DRM_CLIENT_ALIVE ? b->pid :INT_MAX ; pid_b -= pid_a;if (pid_b >0 )@@ -871,56 +713,19 @@ static int client_pid_cmp(const void *_a, const void *_b)static int (*client_cmp)(const void *,const void *) = client_last_cmp; -static struct clients *sort_clients (struct clients *clients, -int (*cmp)(const void *,const void *)) -{ -unsigned int active, free; -struct client *c; -int tmp; - -if (!clients) -return clients; - -qsort (clients->client, clients->num_clients,sizeof (*clients->client), - cmp); - -/* Trim excessive array space. */ - active =0 ; -for_each_client (clients, c, tmp) { -if (c->status !=ALIVE ) -break ;/* Active clients are first in the array. */ - active++; - } - - clients->active_clients = active; - - free = clients->num_clients - active; -if (free > clients->num_clients /2 ) { - active = clients->num_clients - free /2 ; -if (active != clients->num_clients) { - clients->num_clients = active; - clients->client =realloc (clients->client, - clients->num_clients * -sizeof (*c)); - } - } - -return clients; -} -static bool aggregate_pids = true; -static struct clients *display_clients (struct clients *clients) +static struct igt_drm_clients *display_clients (struct igt_drm_clients *clients) { -struct client *ac, *c, *cp =NULL ; -struct clients *aggregated; +struct igt_drm_client *ac, *c, *cp =NULL ; +struct igt_drm_clients *aggregated;int tmp, num =0 ;if (!aggregate_pids) goto out;/* Sort by pid first to make it easy to aggregate while walking. */ -sort_clients (clients, client_pid_cmp); +igt_drm_clients_sort (clients, client_pid_cmp); aggregated =calloc (1 ,sizeof (*clients));assert (aggregated);@@ -929,23 +734,24 @@ static struct clients *display_clients(struct clients *clients)assert (ac); aggregated->num_classes = clients->num_classes; - aggregated->class = clients->class; + aggregated->engine_class = clients->engine_class; + aggregated->private_data = clients->private_data; aggregated->client = ac; -for_each_client (clients, c, tmp) { +igt_for_each_drm_client (clients, c, tmp) {unsigned int i; -if (c->status ==FREE ) +if (c->status ==IGT_DRM_CLIENT_FREE )break ; -assert (c->status ==ALIVE ); +assert (c->status ==IGT_DRM_CLIENT_ALIVE );if (!cp || c->pid != cp->pid) { ac = &aggregated->client[num++];/* New pid. */ ac->clients = aggregated; - ac->status =ALIVE ; + ac->status =IGT_DRM_CLIENT_ALIVE ; ac->id = -c->pid; ac->pid = c->pid;strcpy (ac->name, c->name);@@ -975,215 +781,21 @@ static struct clients *display_clients(struct clients *clients) clients = aggregated; out: -return sort_clients (clients, client_cmp); +return igt_drm_clients_sort (clients, client_cmp); } -static void free_clients (struct clients *clients) +static void free_display_clients (struct igt_drm_clients *clients) { -struct client *c; -unsigned int tmp; +struct igt_drm_client *c; +int tmp; -for_each_client (clients, c, tmp) { +igt_for_each_drm_client (clients, c, tmp)free (c->val); -free (c->last); - }free (clients->client);free (clients); } -static bool is_drm_fd (DIR *fd_dir,const char *name) -{ -struct stat stat; -int ret; - - ret =fstatat (dirfd (fd_dir), name, &stat,0 ); - -return ret ==0 && - (stat.st_mode &S_IFMT ) ==S_IFCHR && -major (stat.st_rdev) ==226 ; -} - -static bool get_task_name (const char *buffer,char *out,unsigned long sz) -{ -char *s =index (buffer,'(' ); -char *e =rindex (buffer,')' ); -unsigned int len; - -if (!s || !e) -return false; -assert (e >= s); - - len = e - ++s; -if (!len || (len +1 ) >= sz) -return false; - -strncpy (out, s, len); - out[len] =0 ; - -return true; -} - -static DIR *opendirat (DIR *at,const char *name) -{ -DIR *dir; -int fd; - - fd =openat (dirfd (at), name,O_DIRECTORY ); -if (fd <0 ) -return NULL ; - - dir =fdopendir (fd); -if (!dir) -close (fd); - -return dir; -} - -static FILE *fropenat (DIR *at,const char *name) -{ -FILE *f; -int fd; - - fd =openat (dirfd (at), name,O_RDONLY ); -if (fd <0 ) -return NULL ; - - f =fdopen (fd,"r" ); -if (!f) -close (fd); - -return f; -} - -static size_t freadat2buf (char *buf,const size_t sz,DIR *at,const char *name) -{ -size_t count; -FILE *f; - - f =fropenat (at, name); -if (!f) -return 0 ; - - buf[sz -1 ] =0 ; - count =fread (buf,1 , sz, f); - buf[count -1 ] =0 ; -fclose (f); - -return count; -} - -static struct clients *scan_clients (struct clients *clients) -{ -struct dirent *proc_dent; -struct client *c; -DIR *proc_dir; -int tmp; - -if (!clients) -return clients; - -for_each_client (clients, c, tmp) { -assert (c->status !=PROBE ); -if (c->status ==ALIVE ) - c->status =PROBE ; -else -break ;/* Free block at the end of array. */ - } - - proc_dir =opendir ("/proc" ); -if (!proc_dir) -return clients; - -while ((proc_dent =readdir (proc_dir)) !=NULL ) { -DIR *pid_dir =NULL , *fd_dir =NULL , *fdinfo_dir =NULL ; -struct dirent *fdinfo_dent; -char client_name[64 ] = { }; -unsigned int client_pid; -char buf[4096 ]; -size_t count; - -if (proc_dent->d_type !=DT_DIR ) -continue ; -if (!isdigit (proc_dent->d_name[0 ])) -continue ; - - pid_dir =opendirat (proc_dir, proc_dent->d_name); -if (!pid_dir) -continue ; - - count =freadat2buf (buf,sizeof (buf), pid_dir,"stat" ); -if (!count) - goto next; - - client_pid =atoi (buf); -if (!client_pid) - goto next; - -if (!get_task_name (buf, client_name,sizeof (client_name))) - goto next; - - fd_dir =opendirat (pid_dir,"fd" ); -if (!fd_dir) - goto next; - - fdinfo_dir =opendirat (pid_dir,"fdinfo" ); -if (!fdinfo_dir) - goto next; - -while ((fdinfo_dent =readdir (fdinfo_dir)) !=NULL ) { -struct drm_client_fdinfo info = { }; - -if (fdinfo_dent->d_type !=DT_REG ) -continue ; -if (!isdigit (fdinfo_dent->d_name[0 ])) -continue ; - -if (!is_drm_fd (fd_dir, fdinfo_dent->d_name)) -continue ; - -if (!__igt_parse_drm_fdinfo (dirfd (fdinfo_dir), - fdinfo_dent->d_name, - &info)) -continue ; - -if (strcmp (info.driver,"i915" )) -continue ; -if (strcmp (info.pdev, clients->pci_slot)) -continue ; -if (find_client (clients,ALIVE , info.id)) -continue ;/* Skip duplicate fds. */ - - c =find_client (clients,PROBE , info.id); -if (!c) -add_client (clients, &info, client_pid, - client_name); -else -update_client (c, client_pid, client_name, - &info); - } - -next: -if (fdinfo_dir) -closedir (fdinfo_dir); -if (fd_dir) -closedir (fd_dir); -if (pid_dir) -closedir (pid_dir); - } - -closedir (proc_dir); - -for_each_client (clients, c, tmp) { -if (c->status ==PROBE ) -free_client (c); -else if (c->status ==FREE ) -break ; - } - -return display_clients (clients); -} -static const char *bars[] = {" " ,"▏" ,"▎" ,"▍" ,"▌" ,"▋" ,"▊" ,"▉" ,"█" };static void n_spaces (const unsigned int n)@@ -1886,15 +1498,15 @@ print_engines_footer(struct engines *engines, double t,static int class_cmp (const void *_a,const void *_b) { -const struct engine_class *a = _a; -const struct engine_class *b = _b; +const struct igt_drm_client_engine_class *a = _a; +const struct igt_drm_client_engine_class *b = _b; -return a->class - b->class; +return a->engine_class - b->engine_class; }static void init_engine_classes (struct engines *engines) { -struct engine_class *classes; +struct igt_drm_client_engine_class *classes;unsigned int i, num;int max =-1 ;@@ -1921,7 +1533,7 @@ static void init_engine_classes(struct engines *engines) }for (i =0 ; i < num; i++) { - classes[i].class = i; + classes[i].engine_class = i; classes[i].name =class_display_name (i); }@@ -2063,7 +1675,7 @@ print_engines(struct engines *engines, double t, int lines, int w, int h) }static int -print_clients_header (struct clients *clients,int lines, +print_clients_header (struct igt_drm_clients *clients,int lines,int con_w,int con_h,int *class_w) {if (output_mode ==INTERACTIVE ) {@@ -2085,19 +1697,19 @@ print_clients_header(struct clients *clients, int lines,int width;for (i =0 ; i < clients->num_classes; i++) { -if (clients->class[i].num_engines) +if (clients->engine_class[i].num_engines) num_active++; } *class_w = width = (con_w - len) / num_active;for (i =0 ; i < clients->num_classes; i++) { -const char *name = clients->class[i].name; +const char *name = clients->engine_class[i].name;int name_len =strlen (name);int pad = (width - name_len) /2 ;int spaces = width - pad - name_len; -if (!clients->class[i].num_engines) +if (!clients->engine_class[i].num_engines)continue ;/* Assert in the ideal world. */ if (pad <0 || spaces <0 )@@ -2124,10 +1736,10 @@ static bool numeric_clients;static bool filter_idle;static int -print_client (struct client *c,struct engines *engines,double t,int lines, +print_client (struct igt_drm_client *c,struct engines *engines,double t,int lines,int con_w,int con_h,unsigned int period_us,int *class_w) { -struct clients *clients = c->clients; +struct igt_drm_clients *clients = c->clients;unsigned int i;if (output_mode ==INTERACTIVE ) {@@ -2141,11 +1753,11 @@ print_client(struct client *c, struct engines *engines, double t, int lines,for (i =0 ; c->samples >1 && i < clients->num_classes; i++) {double pct; -if (!clients->class[i].num_engines) +if (!clients->engine_class[i].num_engines)continue ;/* Assert in the ideal world. */ pct = (double )c->val[i] / period_us /1e3 *100 / - clients->class[i].num_engines; + clients->engine_class[i].num_engines;/* * Guard against possible time-drift between sampling @@ -2177,7 +1789,7 @@ print_client(struct client *c, struct engines *engines, double t, int lines,double pct;snprintf (buf,sizeof (buf),"%s" , - clients->class[i].name); + clients->engine_class[i].name); pops->open_struct (buf); pct = (double )c->val[i] / period_us /1e3 *100 ;@@ -2199,7 +1811,7 @@ print_client(struct client *c, struct engines *engines, double t, int lines, }static int -print_clients_footer (struct clients *clients,double t, +print_clients_footer (struct igt_drm_clients *clients,double t,int lines,int con_w,int con_h) {if (output_mode ==INTERACTIVE ) {@@ -2403,10 +2015,21 @@ static void show_help_screen(void) "\n"); } +static bool client_match (const struct igt_drm_clients *clients, +const struct drm_client_fdinfo *info) +{ +if (strcmp (info->driver,"i915" )) +return false; +if (strcmp (info->pdev, clients->private_data)) +return false; + +return true; +} +int main (int argc,char **argv) {unsigned int period_us =DEFAULT_PERIOD_MS *1000 ; -struct clients *clients =NULL ; +struct igt_drm_clients *clients =NULL ;int con_w =-1 , con_h =-1 ;char *output_path =NULL ;struct engines *engines;@@ -2545,24 +2168,24 @@ int main(int argc, char **argv) ret =EXIT_SUCCESS ; - clients =init_clients (card.pci_slot_name[0 ] ? - card.pci_slot_name :IGPU_PCI ); + clients =igt_drm_clients_init (strdup (card.pci_slot_name[0 ] ? + card.pci_slot_name :IGPU_PCI ));init_engine_classes (engines);if (clients) { clients->num_classes = engines->num_classes; - clients->class = engines->class; + clients->engine_class = engines->class; }pmu_sample (engines); -scan_clients (clients); +igt_drm_clients_scan (clients, client_match); codename =igt_device_get_pretty_name (&card, false);while (!stop_top) { -struct clients *disp_clients; +struct igt_drm_clients *disp_clients;bool consumed = false;int j, lines =0 ;struct winsize ws; -struct client *c; +struct igt_drm_client *c;double t;/* Update terminal size. */ @@ -2581,7 +2204,9 @@ int main(int argc, char **argv)pmu_sample (engines); t = (double )(engines->ts.cur - engines->ts.prev) /1e9 ; - disp_clients =scan_clients (clients); + disp_clients = +display_clients (igt_drm_clients_scan (clients, + client_match));if (stop_top)break ;@@ -2609,9 +2234,9 @@ int main(int argc, char **argv) con_w, con_h, &class_w); -for_each_client (disp_clients, c, j) { -assert (c->status !=PROBE ); -if (c->status !=ALIVE ) +igt_for_each_drm_client (disp_clients, c, j) { +assert (c->status !=IGT_DRM_CLIENT_PROBE ); +if (c->status !=IGT_DRM_CLIENT_ALIVE )break ;/* Active clients are first in the array. */ if (lines >= con_h)@@ -2635,7 +2260,7 @@ int main(int argc, char **argv)break ;if (disp_clients != clients) -free_clients (disp_clients); +free_display_clients (disp_clients);if (output_mode ==INTERACTIVE )process_stdin (period_us);tools/meson.build +1
-1@@ -91,7 +91,7 @@ install_subdir('registers', install_dir : datadir) executable('intel_gpu_top', 'intel_gpu_top.c', install : true, install_rpath : bindir_rpathdir, - dependencies : [lib_igt_perf,lib_igt_device_scan,lib_igt_drm_fdinfo,math]) + dependencies : [lib_igt_perf,lib_igt_device_scan,lib_igt_drm_clients,lib_igt_drm_fdinfo,math]) executable('amd_hdmi_compliance', 'amd_hdmi_compliance.c', dependencies : [tool_deps],Signature · 1 line
2.32.0
Re: [igt-dev] [PATCH i-g-t 1/8] lib: Extract igt_drm_clients from intel_gpu_top
Quoted 40 lines · Petri Latvala
Quoted 39 lines · Tvrtko Ursulin
From: Tvrtko Ursulin <[email protected]>
Code movement with some improvements to prepare for further work in making a vendor agnostic gputop tool possible.
Signed-off-by Tvrtko Ursulin <[email protected]>--- lib/igt_drm_clients.c | 395 ++++++++++++++++++++++++++++++++ lib/igt_drm_clients.h | 96 ++++++++ lib/meson.build | 8 + tools/intel_gpu_top.c | 521 ++++++------------------------------------ tools/meson.build | 2 +- 5 files changed, 573 insertions(+), 449 deletions(-)create mode 100644
lib/igt_drm_clients.c
create mode 100644lib/igt_drm_clients.hlib/igt_drm_clients.c new file +17
-0@@ -0,0 +1,395 @@ +/* +* Copyright © 2022 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
Quoted 8 lines
lib/igt_drm_clients.c line 23
Quoted 6 lines
lib/igt_drm_clients.c new file +6
-0+* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +*/ Add SPDX line to new files please.
Oops I thought that was just the kernel thing!
Quoted 365 lines
Quoted 365 lines
lib/igt_drm_clients.c new file +365
-0+#include <assert.h> +#include <ctype.h> +#include <dirent.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <strings.h> +#include <stdlib.h> +#include <sys/stat.h> +#include <sys/sysmacros.h> +#include <sys/types.h> +#include <unistd.h> + +#include "igt_drm_clients.h" +#include "igt_drm_fdinfo.h" + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE (array) (sizeof(array) / sizeof(array[0])) +#endif + +struct igt_drm_clients *igt_drm_clients_init (void *private_data) +{ +struct igt_drm_clients *clients; + + clients =malloc (sizeof (*clients)); +if (!clients) +return NULL ; + +memset (clients,0 ,sizeof (*clients)); + + clients->private_data = private_data; + +return clients; +} + +struct igt_drm_client * +igt_drm_clients_find (struct igt_drm_clients *clients, +enum igt_drm_client_status status, +unsigned int id) +{ +unsigned int start, num; +struct igt_drm_client *c; + + start = status ==IGT_DRM_CLIENT_FREE ? clients->active_clients :0 ;/* Free block at the end. */ + num = clients->num_clients - start; + +for (c = &clients->client[start]; num; c++, num--) { +if (status != c->status) +continue ; + +if (status ==IGT_DRM_CLIENT_FREE || c->id == id) +return c; + } + +return NULL ; +} + +static void +igt_drm_client_update (struct igt_drm_client *c,unsigned int pid,char *name, +const struct drm_client_fdinfo *info) +{ +unsigned int i; + +if (c->pid != pid) + c->pid = pid; + +if (strcmp (c->name, name)) { +char *p; + +strncpy (c->name, name,sizeof (c->name) -1 ); +strncpy (c->print_name, name,sizeof (c->print_name) -1 ); + + p = c->print_name; +while (*p) { +if (!isprint (*p)) + *p ='*' ; + p++; + } + } + + c->last_runtime =0 ; + c->total_runtime =0 ; + +for (i =0 ; i < c->clients->num_classes; i++) { +assert (i <ARRAY_SIZE (info->busy)); + +if (info->busy[i] < c->last[i]) +continue ;/* It will catch up soon. */ + + c->total_runtime += info->busy[i]; + c->val[i] = info->busy[i] - c->last[i]; + c->last_runtime += c->val[i]; + c->last[i] = info->busy[i]; + } + + c->samples++; + c->status =IGT_DRM_CLIENT_ALIVE ; +} + +static void +igt_drm_client_add (struct igt_drm_clients *clients, +const struct drm_client_fdinfo *info, +unsigned int pid,char *name) +{ +struct igt_drm_client *c; + +assert (!igt_drm_clients_find (clients,IGT_DRM_CLIENT_ALIVE , info->id)); + + c =igt_drm_clients_find (clients,IGT_DRM_CLIENT_FREE ,0 ); +if (!c) { +unsigned int idx = clients->num_clients; + + clients->num_clients += (clients->num_clients +2 ) /2 ; + clients->client =realloc (clients->client, + clients->num_clients *sizeof (*c)); +assert (clients->client); + + c = &clients->client[idx]; +memset (c,0 , (clients->num_clients - idx) *sizeof (*c)); + } + + c->id = info->id; + c->clients = clients; + c->val =calloc (clients->num_classes,sizeof (c->val)); + c->last =calloc (clients->num_classes,sizeof (c->last)); +assert (c->val && c->last); + +igt_drm_client_update (c, pid, name, info); +} + +void igt_drm_client_free (struct igt_drm_client *c) +{ +free (c->val); +free (c->last); +memset (c,0 ,sizeof (*c)); +} + +struct igt_drm_clients * +igt_drm_clients_sort (struct igt_drm_clients *clients, +int (*cmp)(const void *,const void *)) +{ +unsigned int active, free; +struct igt_drm_client *c; +int tmp; + +if (!clients) +return clients; + +qsort (clients->client, clients->num_clients,sizeof (*clients->client), + cmp); + +/* Trim excessive array space. */ + active =0 ; +igt_for_each_drm_client (clients, c, tmp) { +if (c->status !=IGT_DRM_CLIENT_ALIVE ) +break ;/* Active clients are first in the array. */ + active++; + } + + clients->active_clients = active; + + free = clients->num_clients - active; +if (free > clients->num_clients /2 ) { + active = clients->num_clients - free /2 ; +if (active != clients->num_clients) { + clients->num_clients = active; + clients->client =realloc (clients->client, + clients->num_clients * +sizeof (*c)); + } + } + +return clients; +} + +void igt_drm_clients_free (struct igt_drm_clients *clients) +{ +struct igt_drm_client *c; +unsigned int tmp; + +igt_for_each_drm_client (clients, c, tmp) +igt_drm_client_free (c); + +free (clients->client); +free (clients); +} + +static DIR *opendirat (int at,const char *name) +{ +DIR *dir; +int fd; + + fd =openat (at, name,O_DIRECTORY ); +if (fd <0 ) +return NULL ; + + dir =fdopendir (fd); +if (!dir) +close (fd); + +return dir; +} + +static size_t readat2buf (char *buf,const size_t sz,int at,const char *name) +{ +size_t count; +int fd; + + fd =openat (at, name,O_RDONLY ); +if (fd <0 ) +return 0 ; + + buf[sz -1 ] =0 ; + count =read (fd, buf, sz); + buf[count -1 ] =0 ; +close (fd); + +return count; +} + +static bool get_task_name (const char *buffer,char *out,unsigned long sz) +{ +char *s =index (buffer,'(' ); +char *e =rindex (buffer,')' ); +unsigned int len; + +if (!s || !e) +return false; +assert (e >= s); + + len = e - ++s; +if (!len || (len +1 ) >= sz) +return false; + +strncpy (out, s, len); + out[len] =0 ; + +return true; +} + +static bool is_drm_fd (int fd_dir,const char *name) +{ +struct stat stat; +int ret; + + ret =fstatat (fd_dir, name, &stat,0 ); + +return ret ==0 && + (stat.st_mode &S_IFMT ) ==S_IFCHR && +major (stat.st_rdev) ==226 ; +} + +struct igt_drm_clients * +igt_drm_clients_scan (struct igt_drm_clients *clients, +bool (*filter_client)(const struct igt_drm_clients *, +const struct drm_client_fdinfo *)) +{ +struct dirent *proc_dent; +struct igt_drm_client *c; +DIR *proc_dir; +int tmp; + +if (!clients) +return clients; + +igt_for_each_drm_client (clients, c, tmp) { +assert (c->status !=IGT_DRM_CLIENT_PROBE ); +if (c->status ==IGT_DRM_CLIENT_ALIVE ) + c->status =IGT_DRM_CLIENT_PROBE ; +else +break ;/* Free block at the end of array. */ + } + + proc_dir =opendir ("/proc" ); +if (!proc_dir) +return clients; + +while ((proc_dent =readdir (proc_dir)) !=NULL ) { +int pid_dir =-1 , fd_dir =-1 ; +struct dirent *fdinfo_dent; +char client_name[64 ] = { }; +unsigned int client_pid; +DIR *fdinfo_dir =NULL ; +char buf[4096 ]; +size_t count; + +if (proc_dent->d_type !=DT_DIR ) +continue ; +if (!isdigit (proc_dent->d_name[0 ])) +continue ; + + pid_dir =openat (dirfd (proc_dir), proc_dent->d_name, +O_DIRECTORY |O_RDONLY ); +if (pid_dir <0 ) +continue ; + + count =readat2buf (buf,sizeof (buf), pid_dir,"stat" ); +if (!count) + goto next; + + client_pid =atoi (buf); +if (!client_pid) + goto next; + +if (!get_task_name (buf, client_name,sizeof (client_name))) + goto next; + + fd_dir =openat (pid_dir,"fd" ,O_DIRECTORY |O_RDONLY ); +if (fd_dir <0 ) + goto next; + + fdinfo_dir =opendirat (pid_dir,"fdinfo" ); +if (!fdinfo_dir) + goto next; + +while ((fdinfo_dent =readdir (fdinfo_dir)) !=NULL ) { +struct drm_client_fdinfo info; + +if (fdinfo_dent->d_type !=DT_REG ) +continue ; +if (!isdigit (fdinfo_dent->d_name[0 ])) +continue ; + +if (!is_drm_fd (fd_dir, fdinfo_dent->d_name)) +continue ; + +memset (&info,0 ,sizeof (info)); + +if (!__igt_parse_drm_fdinfo (dirfd (fdinfo_dir), + fdinfo_dent->d_name, + &info)) +continue ; + +if (filter_client && !filter_client (clients, &info)) +continue ; + +if (igt_drm_clients_find (clients,IGT_DRM_CLIENT_ALIVE , + info.id)) +continue ;/* Skip duplicate fds. */ + + c =igt_drm_clients_find (clients,IGT_DRM_CLIENT_PROBE , + info.id); +if (!c) +igt_drm_client_add (clients, &info, client_pid, + client_name); +else +igt_drm_client_update (c, client_pid, + client_name, &info); + } + +next: +if (fdinfo_dir) +closedir (fdinfo_dir); +if (fd_dir >=0 ) +close (fd_dir); +if (pid_dir >=0 ) +close (pid_dir); + } + +closedir (proc_dir); + +igt_for_each_drm_client (clients, c, tmp) { +if (c->status ==IGT_DRM_CLIENT_PROBE ) +igt_drm_client_free (c);
Quoted 8 lines
lib/igt_drm_clients.c line 395
Quoted 6 lines
lib/igt_drm_clients.c new file +6
-0+else if (c->status ==IGT_DRM_CLIENT_FREE ) +break ; + } + +return clients; +}All this stuff needs documentation.
A bit more documentation yes, cover letter acknowledged that. ;)
Regards,
Tvrtko
Re: [igt-dev] [PATCH i-g-t 1/8] lib: Extract igt_drm_clients from intel_gpu_top
Quoted 51 lines · Tvrtko Ursulin
Quoted 40 lines · Petri Latvala
Quoted 39 lines · Tvrtko Ursulin
From: Tvrtko Ursulin <[email protected]>
Code movement with some improvements to prepare for further work in making a vendor agnostic gputop tool possible.
Signed-off-by Tvrtko Ursulin <[email protected]>--- lib/igt_drm_clients.c | 395 ++++++++++++++++++++++++++++++++ lib/igt_drm_clients.h | 96 ++++++++ lib/meson.build | 8 + tools/intel_gpu_top.c | 521 ++++++------------------------------------ tools/meson.build | 2 +- 5 files changed, 573 insertions(+), 449 deletions(-)create mode 100644
lib/igt_drm_clients.c
create mode 100644lib/igt_drm_clients.hlib/igt_drm_clients.c new file +17
-0@@ -0,0 +1,395 @@ +/* +* Copyright © 2022 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL Quoted 8 lines
lib/igt_drm_clients.c line 23
Quoted 6 lines
lib/igt_drm_clients.c new file +6
-0+* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +*/ Add SPDX line to new files please.
Quoted 1 line
Oops I thought that was just the kernel thing!
We're slowly getting more SPDX declarations where we can. Which is basically only new files.
Quoted 375 lines
Quoted 366 lines
Quoted 366 lines
lib/igt_drm_clients.c new file +365
-0+#include <assert.h> +#include <ctype.h> +#include <dirent.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <strings.h> +#include <stdlib.h> +#include <sys/stat.h> +#include <sys/sysmacros.h> +#include <sys/types.h> +#include <unistd.h> + +#include "igt_drm_clients.h" +#include "igt_drm_fdinfo.h" + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE (array) (sizeof(array) / sizeof(array[0])) +#endif + +struct igt_drm_clients *igt_drm_clients_init (void *private_data) +{ +struct igt_drm_clients *clients; + + clients =malloc (sizeof (*clients)); +if (!clients) +return NULL ; + +memset (clients,0 ,sizeof (*clients)); + + clients->private_data = private_data; + +return clients; +} + +struct igt_drm_client * +igt_drm_clients_find (struct igt_drm_clients *clients, +enum igt_drm_client_status status, +unsigned int id) +{ +unsigned int start, num; +struct igt_drm_client *c; + + start = status ==IGT_DRM_CLIENT_FREE ? clients->active_clients :0 ;/* Free block at the end. */ + num = clients->num_clients - start; + +for (c = &clients->client[start]; num; c++, num--) { +if (status != c->status) +continue ; + +if (status ==IGT_DRM_CLIENT_FREE || c->id == id) +return c; + } + +return NULL ; +} + +static void +igt_drm_client_update (struct igt_drm_client *c,unsigned int pid,char *name, +const struct drm_client_fdinfo *info) +{ +unsigned int i; + +if (c->pid != pid) + c->pid = pid; + +if (strcmp (c->name, name)) { +char *p; + +strncpy (c->name, name,sizeof (c->name) -1 ); +strncpy (c->print_name, name,sizeof (c->print_name) -1 ); + + p = c->print_name; +while (*p) { +if (!isprint (*p)) + *p ='*' ; + p++; + } + } + + c->last_runtime =0 ; + c->total_runtime =0 ; + +for (i =0 ; i < c->clients->num_classes; i++) { +assert (i <ARRAY_SIZE (info->busy)); + +if (info->busy[i] < c->last[i]) +continue ;/* It will catch up soon. */ + + c->total_runtime += info->busy[i]; + c->val[i] = info->busy[i] - c->last[i]; + c->last_runtime += c->val[i]; + c->last[i] = info->busy[i]; + } + + c->samples++; + c->status =IGT_DRM_CLIENT_ALIVE ; +} + +static void +igt_drm_client_add (struct igt_drm_clients *clients, +const struct drm_client_fdinfo *info, +unsigned int pid,char *name) +{ +struct igt_drm_client *c; + +assert (!igt_drm_clients_find (clients,IGT_DRM_CLIENT_ALIVE , info->id)); + + c =igt_drm_clients_find (clients,IGT_DRM_CLIENT_FREE ,0 ); +if (!c) { +unsigned int idx = clients->num_clients; + + clients->num_clients += (clients->num_clients +2 ) /2 ; + clients->client =realloc (clients->client, + clients->num_clients *sizeof (*c)); +assert (clients->client); + + c = &clients->client[idx]; +memset (c,0 , (clients->num_clients - idx) *sizeof (*c)); + } + + c->id = info->id; + c->clients = clients; + c->val =calloc (clients->num_classes,sizeof (c->val)); + c->last =calloc (clients->num_classes,sizeof (c->last)); +assert (c->val && c->last); + +igt_drm_client_update (c, pid, name, info); +} + +void igt_drm_client_free (struct igt_drm_client *c) +{ +free (c->val); +free (c->last); +memset (c,0 ,sizeof (*c)); +} + +struct igt_drm_clients * +igt_drm_clients_sort (struct igt_drm_clients *clients, +int (*cmp)(const void *,const void *)) +{ +unsigned int active, free; +struct igt_drm_client *c; +int tmp; + +if (!clients) +return clients; + +qsort (clients->client, clients->num_clients,sizeof (*clients->client), + cmp); + +/* Trim excessive array space. */ + active =0 ; +igt_for_each_drm_client (clients, c, tmp) { +if (c->status !=IGT_DRM_CLIENT_ALIVE ) +break ;/* Active clients are first in the array. */ + active++; + } + + clients->active_clients = active; + + free = clients->num_clients - active; +if (free > clients->num_clients /2 ) { + active = clients->num_clients - free /2 ; +if (active != clients->num_clients) { + clients->num_clients = active; + clients->client =realloc (clients->client, + clients->num_clients * +sizeof (*c)); + } + } + +return clients; +} + +void igt_drm_clients_free (struct igt_drm_clients *clients) +{ +struct igt_drm_client *c; +unsigned int tmp; + +igt_for_each_drm_client (clients, c, tmp) +igt_drm_client_free (c); + +free (clients->client); +free (clients); +} + +static DIR *opendirat (int at,const char *name) +{ +DIR *dir; +int fd; + + fd =openat (at, name,O_DIRECTORY ); +if (fd <0 ) +return NULL ; + + dir =fdopendir (fd); +if (!dir) +close (fd); + +return dir; +} + +static size_t readat2buf (char *buf,const size_t sz,int at,const char *name) +{ +size_t count; +int fd; + + fd =openat (at, name,O_RDONLY ); +if (fd <0 ) +return 0 ; + + buf[sz -1 ] =0 ; + count =read (fd, buf, sz); + buf[count -1 ] =0 ; +close (fd); + +return count; +} + +static bool get_task_name (const char *buffer,char *out,unsigned long sz) +{ +char *s =index (buffer,'(' ); +char *e =rindex (buffer,')' ); +unsigned int len; + +if (!s || !e) +return false; +assert (e >= s); + + len = e - ++s; +if (!len || (len +1 ) >= sz) +return false; + +strncpy (out, s, len); + out[len] =0 ; + +return true; +} + +static bool is_drm_fd (int fd_dir,const char *name) +{ +struct stat stat; +int ret; + + ret =fstatat (fd_dir, name, &stat,0 ); + +return ret ==0 && + (stat.st_mode &S_IFMT ) ==S_IFCHR && +major (stat.st_rdev) ==226 ; +} + +struct igt_drm_clients * +igt_drm_clients_scan (struct igt_drm_clients *clients, +bool (*filter_client)(const struct igt_drm_clients *, +const struct drm_client_fdinfo *)) +{ +struct dirent *proc_dent; +struct igt_drm_client *c; +DIR *proc_dir; +int tmp; + +if (!clients) +return clients; + +igt_for_each_drm_client (clients, c, tmp) { +assert (c->status !=IGT_DRM_CLIENT_PROBE ); +if (c->status ==IGT_DRM_CLIENT_ALIVE ) + c->status =IGT_DRM_CLIENT_PROBE ; +else +break ;/* Free block at the end of array. */ + } + + proc_dir =opendir ("/proc" ); +if (!proc_dir) +return clients; + +while ((proc_dent =readdir (proc_dir)) !=NULL ) { +int pid_dir =-1 , fd_dir =-1 ; +struct dirent *fdinfo_dent; +char client_name[64 ] = { }; +unsigned int client_pid; +DIR *fdinfo_dir =NULL ; +char buf[4096 ]; +size_t count; + +if (proc_dent->d_type !=DT_DIR ) +continue ; +if (!isdigit (proc_dent->d_name[0 ])) +continue ; + + pid_dir =openat (dirfd (proc_dir), proc_dent->d_name, +O_DIRECTORY |O_RDONLY ); +if (pid_dir <0 ) +continue ; + + count =readat2buf (buf,sizeof (buf), pid_dir,"stat" ); +if (!count) + goto next; + + client_pid =atoi (buf); +if (!client_pid) + goto next; + +if (!get_task_name (buf, client_name,sizeof (client_name))) + goto next; + + fd_dir =openat (pid_dir,"fd" ,O_DIRECTORY |O_RDONLY ); +if (fd_dir <0 ) + goto next; + + fdinfo_dir =opendirat (pid_dir,"fdinfo" ); +if (!fdinfo_dir) + goto next; + +while ((fdinfo_dent =readdir (fdinfo_dir)) !=NULL ) { +struct drm_client_fdinfo info; + +if (fdinfo_dent->d_type !=DT_REG ) +continue ; +if (!isdigit (fdinfo_dent->d_name[0 ])) +continue ; + +if (!is_drm_fd (fd_dir, fdinfo_dent->d_name)) +continue ; + +memset (&info,0 ,sizeof (info)); + +if (!__igt_parse_drm_fdinfo (dirfd (fdinfo_dir), + fdinfo_dent->d_name, + &info)) +continue ; + +if (filter_client && !filter_client (clients, &info)) +continue ; + +if (igt_drm_clients_find (clients,IGT_DRM_CLIENT_ALIVE , + info.id)) +continue ;/* Skip duplicate fds. */ + + c =igt_drm_clients_find (clients,IGT_DRM_CLIENT_PROBE , + info.id); +if (!c) +igt_drm_client_add (clients, &info, client_pid, + client_name); +else +igt_drm_client_update (c, client_pid, + client_name, &info); + } + +next: +if (fdinfo_dir) +closedir (fdinfo_dir); +if (fd_dir >=0 ) +close (fd_dir); +if (pid_dir >=0 ) +close (pid_dir); + } + +closedir (proc_dir); + +igt_for_each_drm_client (clients, c, tmp) { +if (c->status ==IGT_DRM_CLIENT_PROBE ) +igt_drm_client_free (c);Quoted 8 lines
lib/igt_drm_clients.c line 395
Quoted 6 lines
lib/igt_drm_clients.c new file +6
-0+else if (c->status ==IGT_DRM_CLIENT_FREE ) +break ; + } + +return clients; +}All this stuff needs documentation.
Quoted 1 line
A bit more documentation yes, cover letter acknowledged that. ;)
Shows you how focused I am reading cover letters :/
Signature · 1 line
Petri Latvala
PATCH 2/8 libdrmfdinfo: Allow specifying custom engine map
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Instead of hard coding the engine names, allow a map of names to indices to either be passed in or it gets auto-detected (less efficient) while parsing.
--- lib/igt_drm_clients.c | 7 +++--- lib/igt_drm_clients.h | 3 ++- lib/igt_drm_fdinfo.c | 48 +++++++++++++++++++++++++++-------------- lib/igt_drm_fdinfo.h | 15 ++++++++++--- tests/i915/drm_fdinfo.c | 18 ++++++++++++---- tools/intel_gpu_top.c | 13 +++++++++-- 6 files changed, 75 insertions(+), 29 deletions(-)
lib/igt_drm_clients.c +4 -3
@@ -278,7 +278,8 @@ static bool is_drm_fd(int fd_dir, const char *name)struct igt_drm_clients *igt_drm_clients_scan (struct igt_drm_clients *clients,bool (*filter_client)(const struct igt_drm_clients *, -const struct drm_client_fdinfo *)) +const struct drm_client_fdinfo *), +const char **name_map,unsigned int map_entries) {struct dirent *proc_dent;struct igt_drm_client *c;
@@ -352,8 +353,8 @@ igt_drm_clients_scan(struct igt_drm_clients *clients,memset (&info,0 ,sizeof (info));if (!__igt_parse_drm_fdinfo (dirfd (fdinfo_dir), - fdinfo_dent->d_name, - &info)) + fdinfo_dent->d_name, &info, + name_map, map_entries))continue ;if (filter_client && !filter_client (clients, &info))
lib/igt_drm_clients.h +2 -1
@@ -80,7 +80,8 @@ void igt_drm_clients_free(struct igt_drm_clients *clients);struct igt_drm_clients *igt_drm_clients_scan (struct igt_drm_clients *clients,bool (*filter_client)(const struct igt_drm_clients *, -const struct drm_client_fdinfo *)); +const struct drm_client_fdinfo *), +const char **name_map,unsigned int map_entries);struct igt_drm_client * igt_drm_clients_find(struct igt_drm_clients *clients,
lib/igt_drm_fdinfo.c +32 -16
97 lines not on this page · raw message
lib/igt_drm_fdinfo.h +12 -3
@@ -39,6 +39,7 @@ struct drm_client_fdinfo {unsigned int num_engines;unsigned int capacity[DRM_CLIENT_FDINFO_MAX_ENGINES ]; +char names[DRM_CLIENT_FDINFO_MAX_ENGINES ][256 ];uint64_t busy[DRM_CLIENT_FDINFO_MAX_ENGINES ]; };
@@ -47,11 +48,15 @@ struct drm_client_fdinfo {* * @drm_fd: DRM file descriptor * @info: Structure to populate with read data +* @name_map: Optional array of strings representing engine names +* @map_entries: Number of strings in the names array * * Returns the number of valid drm fdinfo keys found or zero if not all * mandatory keys were present or no engines found. */ -unsigned int igt_parse_drm_fdinfo (int drm_fd,struct drm_client_fdinfo *info); +unsigned int +igt_parse_drm_fdinfo (int drm_fd,struct drm_client_fdinfo *info, +const char **name_map,unsigned int map_entries);/** * __igt_parse_drm_fdinfo: Parses the drm fdinfo file
@@ -59,11 +64,15 @@ unsigned int igt_parse_drm_fdinfo(int drm_fd, struct drm_client_fdinfo *info);* @dir: File descriptor pointing to /proc/pid/fdinfo directory * @fd: String representation of the file descriptor number to parse. * @info: Structure to populate with read data +* @name_map: Optional array of strings representing engine names +* @map_entries: Number of strings in the names array * * Returns the number of valid drm fdinfo keys found or zero if not all * mandatory keys were present or no engines found. */ -unsigned int __igt_parse_drm_fdinfo (int dir,const char *fd, -struct drm_client_fdinfo *info); +unsigned int +__igt_parse_drm_fdinfo (int dir,const char *fd, +struct drm_client_fdinfo *info, +const char **name_map,unsigned int map_entries);#endif /* IGT_DRM_FDINFO_H */
tests/i915/drm_fdinfo.c +14 -4
@@ -34,6 +34,13 @@ IGT_TEST_DESCRIPTION("Test the i915 drm fdinfo data");const double tolerance =0.05f ;const unsigned long batch_duration_ns =500e6 ; +static const char *engine_map[] = { +"render" , +"copy" , +"video" , +"video-enhance" , +}; +#define __assert_within_epsilon (x, ref, tol_up, tol_down) \ igt_assert_f((double)(x) <= (1.0 + (tol_up)) * (double)(ref) && \ (double)(x) >= (1.0 - (tol_down)) * (double)(ref), \
@@ -50,7 +57,8 @@ static void basics(int i915, unsigned int num_classes)struct drm_client_fdinfo info = { };unsigned int ret; - ret =igt_parse_drm_fdinfo (i915, &info); + ret =igt_parse_drm_fdinfo (i915, &info, engine_map, +ARRAY_SIZE (engine_map));igt_assert (ret);igt_assert (!strcmp (info.driver,"i915" ));
@@ -181,7 +189,8 @@ static uint64_t read_busy(int i915, unsigned int class) {struct drm_client_fdinfo info = { }; -igt_assert (igt_parse_drm_fdinfo (i915, &info)); +igt_assert (igt_parse_drm_fdinfo (i915, &info, engine_map, +ARRAY_SIZE (engine_map)));return info.busy[class]; }
@@ -267,7 +276,8 @@ static void read_busy_all(int i915, uint64_t *val) {struct drm_client_fdinfo info = { }; -igt_assert (igt_parse_drm_fdinfo (i915, &info)); +igt_assert (igt_parse_drm_fdinfo (i915, &info, engine_map, +ARRAY_SIZE (engine_map)));memcpy (val, info.busy,sizeof (info.busy)); }
@@ -459,7 +469,7 @@ igt_main i915 =__drm_open_driver (DRIVER_INTEL );igt_require_gem (i915); -igt_require (igt_parse_drm_fdinfo (i915, &info)); +igt_require (igt_parse_drm_fdinfo (i915, &info,NULL ,0 )); ctx =intel_ctx_create_all_physical (i915);
tools/intel_gpu_top.c +11 -2
@@ -2030,6 +2030,12 @@ int main(int argc, char **argv) {unsigned int period_us =DEFAULT_PERIOD_MS *1000 ;struct igt_drm_clients *clients =NULL ; +static const char *engine_map[] = { +"render" , +"copy" , +"video" , +"video-enhance" , + };int con_w =-1 , con_h =-1 ;char *output_path =NULL ;struct engines *engines;
@@ -2177,7 +2183,8 @@ int main(int argc, char **argv) }pmu_sample (engines); -igt_drm_clients_scan (clients, client_match); +igt_drm_clients_scan (clients, client_match, engine_map, +ARRAY_SIZE (engine_map)); codename =igt_device_get_pretty_name (&card, false);while (!stop_top) {
@@ -2206,7 +2213,9 @@ int main(int argc, char **argv) disp_clients =display_clients (igt_drm_clients_scan (clients, - client_match)); + client_match, + engine_map, +ARRAY_SIZE (engine_map)));if (stop_top)break ;
Signature · 1 line
2.32.0
PATCH 3/8 libdrmclients: Record client drm minor
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Prepare for supporting clients belonging to multiple DRM cards by storing the DRM minor in the client record.
--- lib/igt_drm_clients.c | 22 ++++++++++++++-------- lib/igt_drm_clients.h | 1 + 2 files changed, 15 insertions(+), 8 deletions(-)
lib/igt_drm_clients.c +14 -8
@@ -125,7 +125,7 @@ igt_drm_client_update(struct igt_drm_client *c, unsigned int pid, char *name,static void igt_drm_client_add (struct igt_drm_clients *clients,const struct drm_client_fdinfo *info, -unsigned int pid,char *name) +unsigned int pid,char *name,unsigned int drm_minor) {struct igt_drm_client *c;
@@ -145,6 +145,7 @@ igt_drm_client_add(struct igt_drm_clients *clients, } c->id = info->id; + c->drm_minor = drm_minor; c->clients = clients; c->val =calloc (clients->num_classes,sizeof (c->val)); c->last =calloc (clients->num_classes,sizeof (c->last));
@@ -263,16 +264,21 @@ static bool get_task_name(const char *buffer, char *out, unsigned long sz)return true; } -static bool is_drm_fd (int fd_dir,const char *name) +static bool is_drm_fd (int fd_dir,const char *name,unsigned int *minor) {struct stat stat;int ret; ret =fstatat (fd_dir, name, &stat,0 ); -return ret ==0 && - (stat.st_mode &S_IFMT ) ==S_IFCHR && -major (stat.st_rdev) ==226 ; +if (ret ==0 && + (stat.st_mode &S_IFMT ) ==S_IFCHR && +major (stat.st_rdev) ==226 ) { + *minor =minor (stat.st_rdev); +return true; + } + +return false; } struct igt_drm_clients *
@@ -302,10 +308,10 @@ igt_drm_clients_scan(struct igt_drm_clients *clients,return clients;while ((proc_dent =readdir (proc_dir)) !=NULL ) { +unsigned int client_pid, minor =0 ;int pid_dir =-1 , fd_dir =-1 ;struct dirent *fdinfo_dent;char client_name[64 ] = { }; -unsigned int client_pid;DIR *fdinfo_dir =NULL ;char buf[4096 ];size_t count;
@@ -347,7 +353,7 @@ igt_drm_clients_scan(struct igt_drm_clients *clients,if (!isdigit (fdinfo_dent->d_name[0 ]))continue ; -if (!is_drm_fd (fd_dir, fdinfo_dent->d_name)) +if (!is_drm_fd (fd_dir, fdinfo_dent->d_name, &minor))continue ;memset (&info,0 ,sizeof (info));
@@ -368,7 +374,7 @@ igt_drm_clients_scan(struct igt_drm_clients *clients, info.id);if (!c)igt_drm_client_add (clients, &info, client_pid, - client_name); + client_name, minor);else igt_drm_client_update (c, client_pid, client_name, &info);
lib/igt_drm_clients.h +1 -0
@@ -48,6 +48,7 @@ struct igt_drm_client {enum igt_drm_client_status status;unsigned int id; +unsigned int drm_minor;unsigned int pid;char name[24 ];char print_name[24 ];
Signature · 1 line
2.32.0
PATCH 4/8 libdrmclient: Support multiple DRM cards
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Require DRM minor match during client lookup.
--- lib/igt_drm_clients.c | 14 ++++++++------ lib/igt_drm_clients.h | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-)
lib/igt_drm_clients.c +8 -6
@@ -61,7 +61,7 @@ struct igt_drm_clients *igt_drm_clients_init(void *private_data)struct igt_drm_client *igt_drm_clients_find (struct igt_drm_clients *clients,enum igt_drm_client_status status, -unsigned int id) +unsigned int drm_minor,unsigned int id) {unsigned int start, num;struct igt_drm_client *c;
@@ -73,7 +73,8 @@ igt_drm_clients_find(struct igt_drm_clients *clients,if (status != c->status)continue ; -if (status ==IGT_DRM_CLIENT_FREE || c->id == id) +if (status ==IGT_DRM_CLIENT_FREE || + (drm_minor == c->drm_minor && c->id == id))return c; }
@@ -129,9 +130,10 @@ igt_drm_client_add(struct igt_drm_clients *clients, {struct igt_drm_client *c; -assert (!igt_drm_clients_find (clients,IGT_DRM_CLIENT_ALIVE , info->id)); +assert (!igt_drm_clients_find (clients,IGT_DRM_CLIENT_ALIVE , + drm_minor, info->id)); - c =igt_drm_clients_find (clients,IGT_DRM_CLIENT_FREE ,0 ); + c =igt_drm_clients_find (clients,IGT_DRM_CLIENT_FREE ,0 ,0 );if (!c) {unsigned int idx = clients->num_clients;
@@ -367,11 +369,11 @@ igt_drm_clients_scan(struct igt_drm_clients *clients,continue ;if (igt_drm_clients_find (clients,IGT_DRM_CLIENT_ALIVE , - info.id)) + minor, info.id))continue ;/* Skip duplicate fds. */ c =igt_drm_clients_find (clients,IGT_DRM_CLIENT_PROBE , - info.id); + minor, info.id);if (!c)igt_drm_client_add (clients, &info, client_pid, client_name, minor);
lib/igt_drm_clients.h +1 -1
@@ -87,7 +87,7 @@ igt_drm_clients_scan(struct igt_drm_clients *clients,struct igt_drm_client *igt_drm_clients_find (struct igt_drm_clients *clients,enum igt_drm_client_status status, -unsigned int id); +unsigned int drm_minor,unsigned int id);struct igt_drm_clients * igt_drm_clients_sort(struct igt_drm_clients *clients,
Signature · 1 line
2.32.0
PATCH 5/8 libdrmfdinfo: Track largest engine index
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Prep code for incoming work.
--- lib/igt_drm_fdinfo.c | 2 ++ lib/igt_drm_fdinfo.h | 1 + 2 files changed, 3 insertions(+)
lib/igt_drm_fdinfo.c +2 -0
@@ -162,6 +162,8 @@ __igt_parse_drm_fdinfo(int dir, const char *fd, struct drm_client_fdinfo *info, info->capacity[idx] =1 ; info->busy[idx] = val; info->num_engines++; +if (idx > info->last_engine_index) + info->last_engine_index = idx; } }else if (!strncmp (l,"drm-engine-capacity-" ,20 )) { idx = parse_engine(l, info,
lib/igt_drm_fdinfo.h +1 -0
@@ -38,6 +38,7 @@ struct drm_client_fdinfo {unsigned long id;unsigned int num_engines; +unsigned int last_engine_index;unsigned int capacity[DRM_CLIENT_FDINFO_MAX_ENGINES ];char names[DRM_CLIENT_FDINFO_MAX_ENGINES ][256 ];uint64_t busy[DRM_CLIENT_FDINFO_MAX_ENGINES ];
Signature · 1 line
2.32.0
PATCH 6/8 libdrmclient/intel_gpu_top: Decouple hardcoded engine assumptions
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Intel_gpu_top gets it's main engine configuration data via PMU probe and uses that for per client view as well. Furthemore code so far assumed only clients belonging from a single DRM card would be tracked in a single clients list.
Break this inter-dependency by moving the engine data to be per client and also have libdrmclient probe the engine configuration independently using the previously added libdrmfdinfo facilities.
--- lib/igt_drm_clients.c | 34 +++++++++++-- lib/igt_drm_clients.h | 11 ++--- tools/intel_gpu_top.c | 112 ++++++++++++++++++++++++++++++------------ 3 files changed, 117 insertions(+), 40 deletions(-)
lib/igt_drm_clients.c +31 -3
@@ -107,7 +107,7 @@ igt_drm_client_update(struct igt_drm_client *c, unsigned int pid, char *name, c->last_runtime =0 ; c->total_runtime =0 ; -for (i =0 ; i < c->clients->num_classes; i++) { +for (i =0 ; i <= c->engines->max_engine_id; i++) {assert (i <ARRAY_SIZE (info->busy));if (info->busy[i] < c->last[i])
@@ -129,6 +129,7 @@ igt_drm_client_add(struct igt_drm_clients *clients, unsigned int pid, char *name, unsigned int drm_minor) { struct igt_drm_client *c; +unsigned int i;assert (!igt_drm_clients_find (clients,IGT_DRM_CLIENT_ALIVE , drm_minor, info->id));
@@ -149,8 +150,28 @@ igt_drm_client_add(struct igt_drm_clients *clients, c->id = info->id; c->drm_minor = drm_minor; c->clients = clients; - c->val =calloc (clients->num_classes,sizeof (c->val)); - c->last =calloc (clients->num_classes,sizeof (c->last)); + c->engines =malloc (sizeof (*c->engines)); +assert (c->engines); +memset (c->engines,0 ,sizeof (*c->engines)); + c->engines->capacity =calloc (info->last_engine_index +1 , +sizeof (*c->engines->capacity)); +assert (c->engines->capacity); + c->engines->names =calloc (info->last_engine_index +1 , +sizeof (*c->engines->names)); +assert (c->engines->names); + +for (i =0 ; i <= info->last_engine_index; i++) { +if (!info->capacity[i]) +continue ; + + c->engines->capacity[i] = info->capacity[i]; + c->engines->names[i] =strdup (info->names[i]); +assert (c->engines->names[i]); + c->engines->num_engines++; + c->engines->max_engine_id = i; + } + c->val =calloc (c->engines->max_engine_id +1 ,sizeof (c->val)); + c->last =calloc (c->engines->max_engine_id +1 ,sizeof (c->last));assert (c->val && c->last);igt_drm_client_update (c, pid, name, info);
@@ -158,6 +179,13 @@ igt_drm_client_add(struct igt_drm_clients *clients,void igt_drm_client_free (struct igt_drm_client *c) { +unsigned int i; + +for (i =0 ; i <= c->engines->max_engine_id; i++) +free (c->engines->names[i]); +free (c->engines->capacity); +free (c->engines->names); +free (c->engines);free (c->val);free (c->last);memset (c,0 ,sizeof (*c));
lib/igt_drm_clients.h +5 -6
@@ -35,10 +35,11 @@ enum igt_drm_client_status { IGT_DRM_CLIENT_PROBE }; -struct igt_drm_client_engine_class { -unsigned int engine_class; -const char *name; +struct igt_drm_client_engines {unsigned int num_engines; +unsigned int max_engine_id; +unsigned int *capacity; +char **names; };struct igt_drm_clients ;
@@ -47,6 +48,7 @@ struct igt_drm_client {struct igt_drm_clients *clients;enum igt_drm_client_status status; +struct igt_drm_client_engines *engines;unsigned int id;unsigned int drm_minor;unsigned int pid;
@@ -63,9 +65,6 @@ struct igt_drm_clients {unsigned int num_clients;unsigned int active_clients; -unsigned int num_classes; -struct igt_drm_client_engine_class *engine_class; -void *private_data;struct igt_drm_client *client;/* Must be last. */
tools/intel_gpu_top.c +81 -31
258 lines not on this page · raw message
Signature · 1 line
2.32.0
PATCH 7/8 libdrmclient: Enforce client status sort order in the library
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Some libdrmclient operations require that inactive clients are last in the list. Rather than relying on callers of the library sort routine to implement their comparison callbacks correctly, enforce this order directly in the library and let callers comparison callbacks concern themselves only with ordering they are interested in.
--- lib/igt_drm_clients.c | 25 +++++++++++-- lib/igt_drm_clients.h | 2 +- tools/intel_gpu_top.c | 81 +++++++++++++++++++------------------------ 3 files changed, 58 insertions(+), 50 deletions(-)
lib/igt_drm_clients.c +22 -3
@@ -191,10 +191,29 @@ void igt_drm_client_free(struct igt_drm_client *c)memset (c,0 ,sizeof (*c)); } +struct sort_context +{ +int (*user_cmp)(const void *,const void *,void *); +}; + +static int sort_cmp (const void *_a,const void *_b,void *_ctx) +{ +const struct sort_context *ctx = _ctx; +const struct igt_drm_client *a = _a; +const struct igt_drm_client *b = _b; +int cmp = b->status - a->status; + +if (cmp ==0 ) +return ctx->user_cmp (_a, _b, _ctx); +else +return cmp; +} +struct igt_drm_clients *igt_drm_clients_sort (struct igt_drm_clients *clients, -int (*cmp)(const void *,const void *)) +int (*cmp)(const void *,const void *,void *)) { +struct sort_context ctx = { .user_cmp = cmp };unsigned int active, free;struct igt_drm_client *c;int tmp;
@@ -202,8 +221,8 @@ igt_drm_clients_sort(struct igt_drm_clients *clients,if (!clients)return clients; -qsort (clients->client, clients->num_clients,sizeof (*clients->client), - cmp); +qsort_r (clients->client, clients->num_clients,sizeof (*clients->client), + sort_cmp, &ctx);/* Trim excessive array space. */ active =0 ;
lib/igt_drm_clients.h +1 -1
@@ -90,7 +90,7 @@ igt_drm_clients_find(struct igt_drm_clients *clients,struct igt_drm_clients *igt_drm_clients_sort (struct igt_drm_clients *clients, -int (*cmp)(const void *,const void *)); +int (*cmp)(const void *,const void *,void *));void igt_drm_client_free (struct igt_drm_client *c);
tools/intel_gpu_top.c +35 -46
127 lines not on this page · raw message
Signature · 1 line
2.32.0
PATCH 8/8 gputop: Basic vendor agnostic GPU top tool
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Rudimentary vendor agnostic example of how lib_igt_drm_clients can be used to display a sorted by card and usage list of processes using GPUs.
--- tools/gputop.c | 276 ++++++++++++++++++++++++++++++++++++++++++++++ tools/meson.build | 5 + 2 files changed, 281 insertions(+) create mode 100644 tools/gputop.c
tools/gputop.c new file +276 -0
@@ -0,0 +1,276 @@ +/* +* Copyright © 2022 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +* DEALINGS IN THE SOFTWARE. +*/ + +#include <assert.h> +#include <ctype.h> +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <inttypes.h> +#include <limits.h> +#include <locale.h> +#include <math.h> +#include <poll.h> +#include <signal.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/ioctl.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> +#include <termios.h> +#include <sys/sysmacros.h> +#include <stdbool.h> + +#include "igt_drm_clients.h" +#include "igt_drm_fdinfo.h" + +#define ARRAY_SIZE (arr) (sizeof(arr)/sizeof(arr[0])) + +static const char *bars[] = {" " ,"▏" ,"▎" ,"▍" ,"▌" ,"▋" ,"▊" ,"▉" ,"█" }; + +static void n_spaces (const unsigned int n) +{ +unsigned int i; + +for (i =0 ; i < n; i++) +putchar (' ' ); +} + +static void print_percentage_bar (double percent,int max_len) +{ +int bar_len, i, len = max_len -2 ; +const int w =8 ; + +assert (max_len >0 ); + + bar_len =ceil (w * percent * len /100.0 ); +if (bar_len > w * len) + bar_len = w * len; + +putchar ('|' ); + +for (i = bar_len; i >= w; i -= w) +printf ("%s" , bars[w]); +if (i) +printf ("%s" , bars[i]); + + len -= (bar_len + (w -1 )) / w; +n_spaces (len); + +putchar ('|' ); +} + +static int +print_client_header (struct igt_drm_client *c,int lines,int con_w,int con_h, +int *engine_w) +{ +const char *pidname =" PID NAME " ; +int ret, len =strlen (pidname); + +if (lines++ >= con_h || len >= con_w) +return lines; +printf ("\033[7m" ); + ret =printf ("DRM minor %u" , c->drm_minor); +n_spaces (con_w - ret); + +if (lines++ >= con_h) +return lines; +printf ("\n%s" , pidname); + +if (c->engines->num_engines) { +unsigned int i; +int width; + + *engine_w = width = (con_w - len) / c->engines->num_engines; + +for (i =0 ; i <= c->engines->max_engine_id; i++) { +const char *name = c->engines->names[i]; +int name_len =strlen (name); +int pad = (width - name_len) /2 ; +int spaces = width - pad - name_len; + +if (!name) +continue ; + +if (pad <0 || spaces <0 ) +continue ; + +n_spaces (pad); +printf ("%s" , name); +n_spaces (spaces); + len += pad + name_len + spaces; + } + } + +n_spaces (con_w - len); +printf ("\033[0m\n" ); + +return lines; +} + + +static bool +newheader (const struct igt_drm_client *c,const struct igt_drm_client *pc) +{ +return !pc || c->drm_minor != pc->drm_minor; +} + +static bool filter_idle = true; + +static int +print_client (struct igt_drm_client *c,struct igt_drm_client **prevc, +double t,int lines,int con_w,int con_h, +unsigned int period_us,int *engine_w) +{ +unsigned int i; + +if (filter_idle && (!c->total_runtime || c->samples <2 )) +return lines; + +if (newheader (c, *prevc)) { + lines =print_client_header (c, lines, con_w, con_h, engine_w); +if (lines >= con_h) +return lines; + } + + *prevc = c; + +printf ("%8u %17s " , c->pid, c->print_name); + lines++; + +for (i =0 ; c->samples >1 && i <= c->engines->max_engine_id; i++) { +double pct; + +if (!c->engines->capacity[i]) +continue ; + + pct = (double )c->val[i] / period_us /1e3 *100 / + c->engines->capacity[i]; + +/* +* Guard against possible time-drift between sampling +* client data and time we obtained our time-delta from +* PMU. +*/ +if (pct >100.0 ) + pct =100.0 ; + +print_percentage_bar (pct, *engine_w); + } + +putchar ('\n' ); + +return lines; +} + +static int +__client_id_cmp (const struct igt_drm_client *a, +const struct igt_drm_client *b) +{ +if (a->id > b->id) +return 1 ; +else if (a->id < b->id) +return -1 ; +else +return 0 ; +} + +static int client_cmp (const void *_a,const void *_b,void *unused) +{ +const struct igt_drm_client *a = _a; +const struct igt_drm_client *b = _b; +long val_a, val_b; + + val_a = a->drm_minor; + val_b = b->drm_minor; +if (val_a > val_b) +return 1 ; +else if (val_b > val_a) +return -1 ; + + val_a = a->last_runtime; + val_b = b->last_runtime; +if (val_a == val_b) +return __client_id_cmp (a, b); +else if (val_b > val_a) +return 1 ; +else +return -1 ; + +} + +int main (int argc,char **argv) +{ +unsigned int period_us =2e6 ; +struct igt_drm_clients *clients =NULL ; +int con_w =-1 , con_h =-1 ; + + clients =igt_drm_clients_init (NULL ); +if (!clients) +exit (1 ); + +igt_drm_clients_scan (clients,NULL ,NULL ,0 ); + +for (;;) { +struct igt_drm_client *c, *prevc =NULL ; +int i, engine_w =0 , lines =0 ; +struct winsize ws; + +if (ioctl (0 ,TIOCGWINSZ , &ws) !=-1 ) { + con_w = ws.ws_col; + con_h = ws.ws_row; +if (con_w ==0 && con_h ==0 ) { +/* Serial console. */ + con_w =80 ; + con_h =24 ; + } + } + +igt_drm_clients_scan (clients,NULL ,NULL ,0 ); +igt_drm_clients_sort (clients, client_cmp); + +printf ("\033[H\033[J" ); + +igt_for_each_drm_client (clients, c, i) { +assert (c->status !=IGT_DRM_CLIENT_PROBE ); +if (c->status !=IGT_DRM_CLIENT_ALIVE ) +break ;/* Active clients are first in the array. */ + + lines =print_client (c, &prevc, (double )period_us /1e6 , + lines, con_w, con_h, period_us, + &engine_w); +if (lines >= con_h) +break ; + } + +if (lines++ < con_h) +printf ("\n" ); + +usleep (period_us); + } + +return 0 ; +}
tools/meson.build +5 -0
@@ -69,6 +69,11 @@ if libudev.found() install : true) endif +executable('gputop', 'gputop.c', + install : true, + install_rpath : bindir_rpathdir, + dependencies : [lib_igt_drm_clients,lib_igt_drm_fdinfo,math]) + intel_l3_parity_src = [ 'intel_l3_parity.c', 'intel_l3_udev_listener.c' ] executable('intel_l3_parity', sources : intel_l3_parity_src, dependencies : tool_deps,
Signature · 1 line
2.32.0
Patchwork
CI[igt-dev] ✗ Fi.CI.BAT: failure for Vendor agnostic gputop
In reply to Tvrtko Ursulin== Series Details ==
== Series Details ==
Series: Vendor agnostic gputop
URL : https://patchwork.freedesktop.org/series/102175/
State : failure
== Summary ==
CI Bug Log - changes from CI_DRM_11455 -> IGTPW_6872
====================================================
Summary
-------
**FAILURE**
Serious unknown changes coming with IGTPW_6872 absolutely need to be verified manually.
If you think the reported changes have nothing to do with the changes introduced in IGTPW_6872, please notify your bug team to allow them to document this new failure mode, which will reduce false positives in CI.
External URL: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/index.html
Participating hosts (47 -> 47)
------------------------------
Additional (2): fi-cml-u2 fi-pnv-d510
Missing (2): fi-bsw-cyan fi-bdw-samus
Possible new issues
-------------------
Here are the unknown changes that may have been introduced in IGTPW_6872:
### IGT changes ###
#### Possible regressions ####
* igt@i915_selftest@live@hangcheck:
- bat-dg1-5: [PASS][1] -> [INCOMPLETE][2]
[1]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_11455/bat-dg1-5/igt@i915_selftest@[email protected]
[2]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/bat-dg1-5/igt@i915_selftest@[email protected]
#### Suppressed ####
The following results come from untrusted machines, tests, or statuses. They do not affect the overall result.
* igt@i915_selftest@live@gt_heartbeat:
- {fi-ehl-2}: [PASS][3] -> [DMESG-FAIL][4]
[3]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_11455/fi-ehl-2/igt@i915_selftest@live@gt_heartbeat.html
[4]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-ehl-2/igt@i915_selftest@live@gt_heartbeat.html
Known issues
------------
Here are the changes found in IGTPW_6872 that come from known issues:
### IGT changes ###
#### Issues hit ####
* igt@amdgpu/amd_cs_nop@fork-compute0:
- fi-blb-e6850: NOTRUN -> [SKIP][5] ([fdo#109271]) +17 similar issues
[5]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-blb-e6850/igt@amdgpu/[email protected]
* igt@amdgpu/amd_cs_nop@sync-compute0:
- fi-cml-u2: NOTRUN -> [SKIP][6] ([fdo#109315]) +17 similar issues
[6]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-cml-u2/igt@amdgpu/[email protected]
* igt@gem_exec_fence@basic-busy@bcs0:
- fi-cml-u2: NOTRUN -> [SKIP][7] ([i915#1208]) +1 similar issue
[7]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-cml-u2/igt@gem_exec_fence@[email protected]
* igt@gem_huc_copy@huc-copy:
- fi-pnv-d510: NOTRUN -> [SKIP][8] ([fdo#109271]) +57 similar issues
[8]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-pnv-d510/igt@[email protected]
- fi-cml-u2: NOTRUN -> [SKIP][9] ([i915#2190])
[9]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-cml-u2/igt@[email protected]
* igt@gem_lmem_swapping@parallel-random-engines:
- fi-cml-u2: NOTRUN -> [SKIP][10] ([i915#4613]) +3 similar issues
[10]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-cml-u2/igt@[email protected]
* igt@i915_selftest@live@gt_engines:
- bat-dg1-6: [PASS][11] -> [INCOMPLETE][12] ([i915#4418])
[11]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_11455/bat-dg1-6/igt@i915_selftest@live@gt_engines.html
[12]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/bat-dg1-6/igt@i915_selftest@live@gt_engines.html
* igt@i915_selftest@live@hangcheck:
- fi-hsw-4770: [PASS][13] -> [INCOMPLETE][14] ([i915#4785])
[13]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_11455/fi-hsw-4770/igt@i915_selftest@[email protected]
[14]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-hsw-4770/igt@i915_selftest@[email protected]
* igt@kms_chamelium@dp-hpd-fast:
- fi-cml-u2: NOTRUN -> [SKIP][15] ([fdo#109284] / [fdo#111827]) +8 similar issues
[15]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-cml-u2/igt@[email protected]
* igt@kms_cursor_legacy@basic-busy-flip-before-cursor-legacy:
- fi-cml-u2: NOTRUN -> [SKIP][16] ([fdo#109278]) +1 similar issue
[16]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-cml-u2/igt@[email protected]
* igt@kms_force_connector_basic@force-load-detect:
- fi-cml-u2: NOTRUN -> [SKIP][17] ([fdo#109285])
[17]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-cml-u2/igt@[email protected]
* igt@kms_pipe_crc_basic@compare-crc-sanitycheck-pipe-c:
- fi-pnv-d510: NOTRUN -> [SKIP][18] ([fdo#109271] / [i915#5341])
[18]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-pnv-d510/igt@[email protected]
* igt@kms_pipe_crc_basic@compare-crc-sanitycheck-pipe-d:
- fi-cml-u2: NOTRUN -> [SKIP][19] ([fdo#109278] / [i915#533])
[19]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-cml-u2/igt@[email protected]
* igt@kms_setmode@basic-clone-single-crtc:
- fi-cml-u2: NOTRUN -> [SKIP][20] ([i915#3555])
[20]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-cml-u2/igt@[email protected]
* igt@prime_vgem@basic-userptr:
- fi-cml-u2: NOTRUN -> [SKIP][21] ([i915#3301])
[21]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-cml-u2/igt@[email protected]
* igt@runner@aborted:
- bat-dg1-6: NOTRUN -> [FAIL][22] ([i915#4312] / [i915#5257])
[22]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/bat-dg1-6/igt@[email protected]
#### Possible fixes ####
* igt@i915_selftest@live@requests:
- fi-blb-e6850: [DMESG-FAIL][23] ([i915#4528]) -> [PASS][24]
[23]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_11455/fi-blb-e6850/igt@i915_selftest@[email protected]
[24]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-blb-e6850/igt@i915_selftest@[email protected]
* igt@kms_flip@basic-flip-vs-modeset@a-edp1:
- fi-tgl-u2: [DMESG-WARN][25] ([i915#402]) -> [PASS][26]
[25]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_11455/fi-tgl-u2/igt@kms_flip@[email protected]
[26]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/fi-tgl-u2/igt@kms_flip@[email protected]
{name}: This element is suppressed. This means it is ignored when computing
the status of the difference (SUCCESS, WARNING, or FAILURE).
[fdo#109271]: https://bugs.freedesktop.org/show_bug.cgi?id=109271
[fdo#109278]: https://bugs.freedesktop.org/show_bug.cgi?id=109278
[fdo#109284]: https://bugs.freedesktop.org/show_bug.cgi?id=109284
[fdo#109285]: https://bugs.freedesktop.org/show_bug.cgi?id=109285
[fdo#109315]: https://bugs.freedesktop.org/show_bug.cgi?id=109315
[fdo#111827]: https://bugs.freedesktop.org/show_bug.cgi?id=111827
[i915#1208]: https://gitlab.freedesktop.org/drm/intel/issues/1208
[i915#2190]: https://gitlab.freedesktop.org/drm/intel/issues/2190
[i915#3301]: https://gitlab.freedesktop.org/drm/intel/issues/3301
[i915#3555]: https://gitlab.freedesktop.org/drm/intel/issues/3555
[i915#402]: https://gitlab.freedesktop.org/drm/intel/issues/402
[i915#4312]: https://gitlab.freedesktop.org/drm/intel/issues/4312
[i915#4418]: https://gitlab.freedesktop.org/drm/intel/issues/4418
[i915#4528]: https://gitlab.freedesktop.org/drm/intel/issues/4528
[i915#4613]: https://gitlab.freedesktop.org/drm/intel/issues/4613
[i915#4785]: https://gitlab.freedesktop.org/drm/intel/issues/4785
[i915#5257]: https://gitlab.freedesktop.org/drm/intel/issues/5257
[i915#533]: https://gitlab.freedesktop.org/drm/intel/issues/533
[i915#5341]: https://gitlab.freedesktop.org/drm/intel/issues/5341
Build changes
-------------
* CI: CI-20190529 -> None
* IGT: IGT_6410 -> IGTPW_6872
CI-20190529: 20190529
CI_DRM_11455: 548f9565ec7689e227b24ed9ecd5dd9e521fc7be @ git://anongit.freedesktop.org/gfx-ci/linux
IGTPW_6872: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/index.html
IGT_6410: cc359d5d2d3fc8be50340ba0379bacaf5c37bc5f @ https://gitlab.freedesktop.org/drm/igt-gpu-tools.git
== Logs ==
For more details see: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6872/index.html