Skip to content

Commit

Permalink
[MOS] Media memory profiler
Browse files Browse the repository at this point in the history
Enable memory profiler log to capture the graphics memory footprint.
  • Loading branch information
zxye authored and intel-mediadev committed Sep 30, 2022
1 parent 7b9b014 commit bdcdd19
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
50 changes: 50 additions & 0 deletions media_driver/linux/common/os/i915/mos_bufmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ struct mos_bufmgr_gem {
mos_vma_heap vma_heap[MEMZONE_COUNT];
bool use_softpin;
bool softpin_va1Malign;

#define MEM_PROFILER_BUFFER_SIZE 256
char mem_profiler_buffer[MEM_PROFILER_BUFFER_SIZE];
char* mem_profiler_path;
int mem_profiler_fd;
} mos_bufmgr_gem;

#define DRM_INTEL_RELOC_FENCE (1<<0)
Expand Down Expand Up @@ -1125,6 +1130,15 @@ mos_gem_bo_alloc_internal(struct mos_bufmgr *bufmgr,
bo_gem->tiling_mode = I915_TILING_NONE;
bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
bo_gem->stride = 0;
if (bufmgr_gem->mem_profiler_fd != -1)
{
snprintf(bufmgr_gem->mem_profiler_buffer, MEM_PROFILER_BUFFER_SIZE, "GEM_CREATE, %d, %d, %lu, %d, %s\n", getpid(), bo_gem->bo.handle, bo_gem->bo.size,bo_gem->mem_region, name);
ret = write(bufmgr_gem->mem_profiler_fd, bufmgr_gem->mem_profiler_buffer, strnlen(bufmgr_gem->mem_profiler_buffer, MEM_PROFILER_BUFFER_SIZE));
if (ret == -1)
{
MOS_DBG("Failed to write to %s: %s\n", bufmgr_gem->mem_profiler_path, strerror(errno));
}
}

/* drm_intel_gem_bo_free calls DRMLISTDEL() for an uninitialized
list (vma_list), so better set the list head here */
Expand Down Expand Up @@ -1536,6 +1550,15 @@ mos_gem_bo_free(struct mos_linux_bo *bo)
MOS_DBG("DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
bo_gem->gem_handle, bo_gem->name, strerror(errno));
}
if (bufmgr_gem->mem_profiler_fd != -1)
{
snprintf(bufmgr_gem->mem_profiler_buffer, MEM_PROFILER_BUFFER_SIZE, "GEM_CLOSE, %d, %d, %lu, %d\n", getpid(), bo->handle,bo->size,bo_gem->mem_region);
ret = write(bufmgr_gem->mem_profiler_fd, bufmgr_gem->mem_profiler_buffer, strnlen(bufmgr_gem->mem_profiler_buffer, MEM_PROFILER_BUFFER_SIZE));
if (ret == -1)
{
MOS_DBG("Failed to write to %s: %s\n", bufmgr_gem->mem_profiler_path, strerror(errno));
}
}

if (bufmgr_gem->use_softpin)
{
Expand Down Expand Up @@ -2468,6 +2491,11 @@ mos_bufmgr_gem_destroy(struct mos_bufmgr *bufmgr)
mos_vma_heap_finish(&bufmgr_gem->vma_heap[MEMZONE_SYS]);
mos_vma_heap_finish(&bufmgr_gem->vma_heap[MEMZONE_DEVICE]);

if (bufmgr_gem->mem_profiler_fd != -1)
{
close(bufmgr_gem->mem_profiler_fd);
}

free(bufmgr);
}

Expand Down Expand Up @@ -4629,6 +4657,28 @@ mos_bufmgr_gem_init(int fd, int batch_size)
goto exit;
}

bufmgr_gem->mem_profiler_path = getenv("MEDIA_MEMORY_PROFILER_LOG");
if (bufmgr_gem->mem_profiler_path != nullptr)
{
if (strcmp(bufmgr_gem->mem_profiler_path, "/sys/kernel/debug/tracing/trace_marker") == 0)
{
ret = bufmgr_gem->mem_profiler_fd = open(bufmgr_gem->mem_profiler_path, O_WRONLY );
}
else
{
ret = bufmgr_gem->mem_profiler_fd = open(bufmgr_gem->mem_profiler_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
}

if (ret == -1)
{
MOS_DBG("Failed to open %s: %s\n", bufmgr_gem->mem_profiler_path, strerror(errno));
}
}
else
{
bufmgr_gem->mem_profiler_fd = -1;
}

memclear(aperture);
ret = drmIoctl(bufmgr_gem->fd,
DRM_IOCTL_I915_GEM_GET_APERTURE,
Expand Down
50 changes: 50 additions & 0 deletions media_driver/linux/common/os/i915_production/mos_bufmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ typedef struct mos_bufmgr_gem {
bool softpin_va1Malign;

BufmgrPrelim *prelim;

#define MEM_PROFILER_BUFFER_SIZE 256
char mem_profiler_buffer[MEM_PROFILER_BUFFER_SIZE];
char* mem_profiler_path;
int mem_profiler_fd;
} mos_bufmgr_gem;

#define DRM_INTEL_RELOC_FENCE (1<<0)
Expand Down Expand Up @@ -1171,6 +1176,15 @@ mos_gem_bo_alloc_internal(struct mos_bufmgr *bufmgr,
bo_gem->tiling_mode = I915_TILING_NONE;
bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
bo_gem->stride = 0;
if (bufmgr_gem->mem_profiler_fd != -1)
{
snprintf(bufmgr_gem->mem_profiler_buffer, MEM_PROFILER_BUFFER_SIZE, "GEM_CREATE, %d, %d, %lu, %d, %s\n", getpid(), bo_gem->bo.handle, bo_gem->bo.size,bo_gem->mem_region, name);
ret = write(bufmgr_gem->mem_profiler_fd, bufmgr_gem->mem_profiler_buffer, strnlen(bufmgr_gem->mem_profiler_buffer, MEM_PROFILER_BUFFER_SIZE));
if (ret == -1)
{
MOS_DBG("Failed to write to %s: %s\n", bufmgr_gem->mem_profiler_path, strerror(errno));
}
}

/* drm_intel_gem_bo_free calls DRMLISTDEL() for an uninitialized
list (vma_list), so better set the list head here */
Expand Down Expand Up @@ -1582,6 +1596,15 @@ mos_gem_bo_free(struct mos_linux_bo *bo)
MOS_DBG("DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
bo_gem->gem_handle, bo_gem->name, strerror(errno));
}
if (bufmgr_gem->mem_profiler_fd != -1)
{
snprintf(bufmgr_gem->mem_profiler_buffer, MEM_PROFILER_BUFFER_SIZE, "GEM_CLOSE, %d, %d, %lu, %d\n", getpid(), bo->handle,bo->size,bo_gem->mem_region);
ret = write(bufmgr_gem->mem_profiler_fd, bufmgr_gem->mem_profiler_buffer, strnlen(bufmgr_gem->mem_profiler_buffer, MEM_PROFILER_BUFFER_SIZE));
if (ret == -1)
{
MOS_DBG("Failed to write to %s: %s\n", bufmgr_gem->mem_profiler_path, strerror(errno));
}
}

if (bufmgr_gem->use_softpin)
{
Expand Down Expand Up @@ -2529,6 +2552,11 @@ mos_bufmgr_gem_destroy(struct mos_bufmgr *bufmgr)
bufmgr_gem->prelim = nullptr;
}

if (bufmgr_gem->mem_profiler_fd != -1)
{
close(bufmgr_gem->mem_profiler_fd);
}

free(bufmgr);
}

Expand Down Expand Up @@ -4727,6 +4755,28 @@ mos_bufmgr_gem_init(int fd, int batch_size)
goto exit;
}

bufmgr_gem->mem_profiler_path = getenv("MEDIA_MEMORY_PROFILER_LOG");
if (bufmgr_gem->mem_profiler_path != nullptr)
{
if (strcmp(bufmgr_gem->mem_profiler_path, "/sys/kernel/debug/tracing/trace_marker") == 0)
{
ret = bufmgr_gem->mem_profiler_fd = open(bufmgr_gem->mem_profiler_path, O_WRONLY );
}
else
{
ret = bufmgr_gem->mem_profiler_fd = open(bufmgr_gem->mem_profiler_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
}

if (ret == -1)
{
MOS_DBG("Failed to open %s: %s\n", bufmgr_gem->mem_profiler_path, strerror(errno));
}
}
else
{
bufmgr_gem->mem_profiler_fd = -1;
}

memclear(aperture);
ret = drmIoctl(bufmgr_gem->fd,
DRM_IOCTL_I915_GEM_GET_APERTURE,
Expand Down

0 comments on commit bdcdd19

Please sign in to comment.