Add 'projects/rocprofiler/' from commit '16ae2e90c6157e98e846d2bccbaaf533ca5e662a'

git-subtree-dir: projects/rocprofiler
git-subtree-mainline: 2a52e3974d
git-subtree-split: 16ae2e90c6
This commit is contained in:
systems-assistant[bot]
2025-07-22 22:52:43 +00:00
386 changed files with 216890 additions and 0 deletions
@@ -0,0 +1,40 @@
resources:
repositories:
- repository: pipelines_repo
type: github
endpoint: ROCm
name: ROCm/ROCm
variables:
- group: common
- template: /.azuredevops/variables-global.yml@pipelines_repo
trigger:
batch: true
branches:
include:
- amd-master
- amd-staging
paths:
exclude:
- .github
- doc
- LICENSE
- '*.md'
pr:
autoCancel: true
branches:
include:
- amd-master
- amd-staging
paths:
exclude:
- .github
- doc
- LICENSE
- '*.md'
drafts: false
jobs:
- template: ${{ variables.CI_COMPONENT_PATH }}/rocprofiler.yml@pipelines_repo
+60
View File
@@ -0,0 +1,60 @@
---
Language: Cpp
BasedOnStyle: Google
AccessModifierOffset: -1
ConstructorInitializerIndentWidth: 4
AlignEscapedNewlinesLeft: false
AlignTrailingComments: true
AlignConsecutiveAssignments: false
AlignOperands: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakTemplateDeclarations: false
AlwaysBreakBeforeMultilineStrings: true
BreakBeforeBinaryOperators: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BinPackParameters: true
ColumnLimit: 100
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: true
IndentWrappedFunctionNames: false
IndentFunctionDeclarationAfterType: false
MaxEmptyLinesToKeep: 2
KeepEmptyLinesAtTheStartOfBlocks: false
NamespaceIndentation: None
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
DerivePointerAlignment: false
PointerAlignment: Left
SpacesBeforeTrailingComments: 2
Cpp11BracedListStyle: true
Standard: Auto
IndentWidth: 2
TabWidth: 8
UseTab: Never
BreakBeforeBraces: Attach
SpacesInParentheses: false
SpacesInAngles: false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: true
SpaceBeforeAssignmentOperators: true
ContinuationIndentWidth: 4
CommentPragmas: '^ IWYU pragma:'
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
SpaceBeforeParens: ControlStatements
DisableFormat: false
SortIncludes: false
...
+30
View File
@@ -0,0 +1,30 @@
---
Checks: "-*,\
misc-*,\
-misc-incorrect-roundings,\
-misc-macro-parentheses,\
-misc-misplaced-widening-cast,\
-misc-static-assert,\
-misc-no-recursion,\
-misc-non-private-member-variables-in-classes,\
modernize-*,\
-modernize-deprecated-headers,\
-modernize-raw-string-literal,\
-modernize-return-braced-init-list,\
-modernize-use-transparent-functors,\
-modernize-use-trailing-return-type,\
-modernize-avoid-c-arrays,\
-modernize-use-auto,\
-modernize-concat-nested-namespaces,\
-modernize-use-nodiscard,\
performance-*,\
readability-*,\
-readability-function-size,\
-readability-identifier-naming,\
"
CheckOptions:
- key: readability-braces-around-statements.ShortStatementLines
value: '2'
- key: readability-implicit-bool-conversion.AllowPointerConditions
value: '1'
...
+98
View File
@@ -0,0 +1,98 @@
parse:
additional_commands:
find_package:
flags:
- EXACT
- QUIET
- MODULE
- REQUIRED
- CONFIG
- NO_MODULE
- GLOBAL
- NO_POLICY_SCOPE
- BYPASS_PROVIDER
- NO_DEFAULT_PATH
- NO_PACKAGE_ROOT_PATH
- NO_CMAKE_PATH
- NO_CMAKE_ENVIRONMENT_PATH
- NO_SYSTEM_ENVIRONMENT_PATH
- NO_CMAKE_PACKAGE_REGISTRY
- NO_CMAKE_BUILDS_PATH
- NO_CMAKE_SYSTEM_PATH
- NO_CMAKE_INSTALL_PREFIX
- NO_CMAKE_SYSTEM_PACKAGE_REGISTRY
- CMAKE_FIND_ROOT_PATH_BOTH
- ONLY_CMAKE_FIND_ROOT_PATH
- NO_CMAKE_FIND_ROOT_PATH
kwargs:
COMPONENTS: '*'
OPTIONAL_COMPONENTS: '*'
NAMES: '*'
CONFIGS: '*'
HINTS: '*'
PATHS: '*'
REGISTRY_VIEW: '*'
PATH_SUFFIXES: '*'
override_spec: {}
vartags: []
proptags: []
format:
disable: false
line_width: 90
tab_size: 4
use_tabchars: false
fractional_tab_policy: use-space
max_subgroups_hwrap: 2
max_pargs_hwrap: 8
max_rows_cmdline: 2
separate_ctrl_name_with_space: false
separate_fn_name_with_space: false
dangle_parens: false
dangle_align: child
min_prefix_chars: 4
max_prefix_chars: 10
max_lines_hwrap: 2
line_ending: unix
command_case: lower
keyword_case: upper
always_wrap: []
enable_sort: true
autosort: false
require_valid_layout: false
layout_passes: {}
markup:
bullet_char: '*'
enum_char: .
first_comment_is_literal: true
literal_comment_pattern: ^#
fence_pattern: ^\s*([`~]{3}[`~]*)(.*)$
ruler_pattern: ^\s*[^\w\s]{3}.*[^\w\s]{3}$
explicit_trailing_pattern: '#<'
hashruler_min_length: 10
canonicalize_hashrulers: true
enable_markup: true
lint:
disabled_codes: []
function_pattern: '[0-9a-z_]+'
macro_pattern: '[0-9A-Z_]+'
global_var_pattern: '[A-Z][0-9A-Z_]+'
internal_var_pattern: _[A-Z][0-9A-Z_]+
local_var_pattern: '[a-z][a-z0-9_]+'
private_var_pattern: _[0-9a-z_]+
public_var_pattern: '[A-Z][0-9A-Z_]+'
argument_var_pattern: '[a-z][a-z0-9_]+'
keyword_pattern: '[A-Z][0-9A-Z_]+'
max_conditionals_custom_parser: 2
min_statement_spacing: 1
max_statement_spacing: 2
max_returns: 6
max_branches: 12
max_arguments: 5
max_localvars: 15
max_statements: 50
encode:
emit_byteorder_mark: false
input_encoding: utf-8
output_encoding: utf-8
misc:
per_command: {}
+5
View File
@@ -0,0 +1,5 @@
disabled: false
scmId: gh-emu-rocm
branchesToScan:
- amd-staging
- amd-mainline
+15
View File
@@ -0,0 +1,15 @@
name: Rocm Validation Suite KWS
on:
push:
branches: [amd-staging, amd-mainline]
pull_request:
types: [opened, synchronize, reopened]
workflow_dispatch:
jobs:
kws:
if: ${{ github.event_name == 'pull_request' }}
uses: AMD-ROCm-Internal/rocm_ci_infra/.github/workflows/kws.yml@mainline
secrets: inherit
with:
pr_number: ${{github.event.pull_request.number}}
base_branch: ${{github.base_ref}}
@@ -0,0 +1,25 @@
name: ROCm CI Caller
on:
pull_request:
branches: [amd-staging, amd-npi, release/rocm-rel-*]
types: [opened, reopened, synchronize]
push:
branches: [amd-mainline]
workflow_dispatch:
issue_comment:
types: [created]
jobs:
call-workflow:
if: ${{ github.event_name != 'issue_comment' || github.event.comment.body == '!verify' }}
uses: AMD-ROCm-Internal/rocm_ci_infra/.github/workflows/rocm_ci.yml@mainline
secrets: inherit
with:
input_sha: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
input_pr_num: ${{ github.event_name == 'pull_request' && github.event.pull_request.number || 0 }}
input_pr_url: ${{ github.event_name == 'pull_request' && github.event.pull_request.html_url || '' }}
input_pr_title: ${{ github.event_name == 'pull_request' && github.event.pull_request.title || '' }}
repository_name: ${{ github.repository }}
base_ref: ${{ github.event_name == 'pull_request' && github.base_ref || github.ref }}
trigger_event_type: ${{ github.event_name }}
@@ -0,0 +1,17 @@
name: Sync amd-mainline to public repository
on:
push:
branches: [ amd-mainline ]
jobs:
git-mirror:
runs-on: ubuntu-latest
steps:
- name: git-sync
uses: AMD-ROCm-Internal/rocprofiler-github-actions@git-sync-v3
with:
source_repo: "https://${{ secrets.TOKEN }}@github.com/AMD-ROCm-Internal/rocprofiler.git"
source_branch: "amd-mainline"
destination_repo: "https://${{ secrets.EXT_TOKEN }}@github.com/ROCm/rocprofiler.git"
destination_branch: "amd-mainline"
@@ -0,0 +1,17 @@
name: Sync amd-staging to public repository
on:
push:
branches: [ amd-staging ]
jobs:
git-mirror:
runs-on: ubuntu-latest
steps:
- name: git-sync
uses: AMD-ROCm-Internal/rocprofiler-github-actions@git-sync-v3
with:
source_repo: "https://${{ secrets.TOKEN }}@github.com/AMD-ROCm-Internal/rocprofiler.git"
source_branch: "amd-staging"
destination_repo: "https://${{ secrets.EXT_TOKEN }}@github.com/ROCm/rocprofiler.git"
destination_branch: "amd-staging"
+6
View File
@@ -0,0 +1,6 @@
build
compile_commands.json
.cache
.DS_Store
plugin/att/__pycache__
.github
+6
View File
@@ -0,0 +1,6 @@
[submodule "perfetto"]
path = plugin/perfetto/perfetto
url = https://github.com/google/perfetto.git
branch = releases/v44.x
@@ -0,0 +1,141 @@
default: false # includes/excludes all rules by default
# Heading levels should only increment by one level at a time <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md001>
MD001: true
# Heading style <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md003>
MD003: true
# Unordered list style <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md004>
MD004: true
# Inconsistent indentation for list items at the same level <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md005>
MD005: true
# Consider starting bulleted lists at the beginning of the line <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md006>
MD006: true
# Unordered list indentation <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md007>
MD007: true
# Trailing spaces <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md009>
MD009: true
# Hard tabs <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md010>
MD010: true
# Reversed link syntax <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md011>
MD011: true
# Multiple consecutive blank lines <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md012>
MD012: true
# Line length <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md013>
MD013: false
# Dollar signs used before commands without showing output <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md014>
MD014: false
# No space after hash on atx style heading <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md018>
MD018: true
# Multiple spaces after hash on atx style heading <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md019>
MD019: true
# No space inside hashes on closed atx style heading <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md020>
MD020: true
# Multiple spaces inside hashes on closed atx style heading <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md021>
MD021: true
# Headings should be surrounded by blank lines <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md022>
MD022: true
# Headings must start at the beginning of the line <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md023>
MD023: true
# Multiple headings with the same content <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md024>
MD024:
allow_different_nesting: true
# Multiple top level headings in the same document <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md025>
MD025: true
# Trailing punctuation in heading <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md026>
MD026: true
# Multiple spaces after blockquote symbol <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md027>
MD027: true
# Blank line inside blockquote <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md028>
MD028: false
# Ordered list item prefix <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md029>
MD029:
style: 'one'
# Spaces after list markers <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md030>
MD030: true
# Fenced code blocks should be surrounded by blank lines <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md031>
MD031: true
# Lists should be surrounded by blank lines <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md032>
MD032: true
# Inline HTML <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md033>
MD033: true
# Bare URL used <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md034>
MD034: true
# Horizontal rule style <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md035>
MD035:
style: '***'
# Emphasis used instead of a heading <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md036>
MD036: true
# Spaces inside emphasis markers <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md037>
MD037: true
# Spaces inside code span elements <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md038>
MD038: true
# Spaces inside link text <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md039>
MD039: true
# Fenced code blocks should have a language specified <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md040>
MD040: true
# First line in file should be a top level heading <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md041>
MD041: true
# No empty links <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md042>
MD042: true
# Required heading structure <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md043>
MD043: false
# Proper names should have the correct capitalization <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md044>
MD044: false
# Images should have alternate text (alt text) <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md045>
MD045: false
# Code block style <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md046>
MD046:
style: 'fenced'
# Files should end with a single newline character <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md047>
MD047: true
# Code fence style <https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md048>
MD048:
style: 'backtick'
# Custom rules:
CHANGELOG-RULE-001: true
CHANGELOG-RULE-002: true
CHANGELOG-RULE-003: true
CHANGELOG-RULE-004: true
+395
View File
@@ -0,0 +1,395 @@
# Changelog for ROCprofiler
Full documentation for ROCprofiler is available at
[docs.amd.com](https://docs.amd.com/bundle/ROCm-Profiling-Tools-User-Guide-v5.3)
As of ROCm 5.5, the ROCm Profiler will not use terminologies like `rocmtools` or
`rocsight` to describe `rocrofiler` as was done in ROCm 5.4. To identify the
separation of the two versions of `rocprofiler`, the terms `rocprofilerV1` and
`rocprofilerV2` will be used. The `rocprofilerV2` API is currently considered a
beta release and subject to changes in future releases.
## ROCprofiler for rocm 5.4.4
In ROCm 5.4 the naming of the ROCm Profiler related files is:
| ROCm 5.4 | rocprofilerv1 | rocmtools |
|-----------------|-------------------------------------|---------------------------------|
| **Tool script** | `bin/rocprof` | `bin/rocsight` |
| **API include** | `include/rocprofiler/rocprofiler.h` | `include/rocmtools/rocmtools.h` |
| **API library** | `lib/librocprofiler64.so.1` | `lib/librocmtools.so.1` |
The ROCm Profiler Tool that uses `rocprofilerV1` can be invoked using the
following command:
```sh
rocprof …
```
To write a custom tool based on the `rocprofilerV1` API do the following:
```C
main.c:
#include <rocprofiler/rocprofiler.h> // Use the rocprofilerV1 API
int main() {
// Use the rocprofilerV1 API
return 0;
}
```
This can be built in the following manner:
```sh
gcc main.c -I/opt/rocm-5.4.4/include -L/opt/rocm-5.4.4/lib -lrocprofiler64
```
The resulting `a.out` will depend on
`/opt/rocm-5.4.4/lib/librocprofiler64.so.1`.
The ROCm Profiler that uses `rocprofilerV2` API can be invoked using the
following command:
```sh
rocsight …
```
To write a custom tool based on the `rocmtools` API do the following:
```C
main.c:
#include <rocmtools/rocmtools.h> // Use the rocmtools API
int main() {
// Use the rocmtools API
return 0;
}
```
This can be built in the following manner:
```sh
gcc main.c -I/opt/rocm-5.4.4/include -L/opt/rocm-5.4.4/lib -lrocmtools
```
The resulting `a.out` will depend on `/opt/rocm-5.4.4/lib/librocmtools.so.1`.
## ROCprofiler for rocm 5.5.0
In ROCm 5.5 the `rocprofilerv1` and `rocprofilerv2` include and library files
are merged into single files. The `rocmtools` available in ROCm 5.4 is also
available in ROCm 5.5 but is deprecated and will be removed in a future release.
| ROCm 5.5 | rocprofilerv1 | rocprofilerv2 | rocmtools *(deprecated)* |
|-----------------|-------------------------------------|-------------------------------------|---------------------------------|
| **Tool script** | `bin/rocprof` | `bin/rocprofv2` | `bin/rocsight` |
| **API include** | `include/rocprofiler/rocprofiler.h` | `include/rocprofiler/rocprofiler.h` | `include/rocmtools/rocmtools.h` |
| **API library** | `lib/librocprofiler64.so.1` | `lib/librocprofiler64.so.1` | `lib/librocmtools.so.1` |
The ROCm Profiler Tool that uses `rocprofilerV1` can be invoked using the
following command:
```sh
rocprof …
```
To write a custom tool based on the `rocprofilerV1` API it is necessary to
define the macro `ROCPROFILER_V1`:
```C
main.c:
#define ROCPROFILER_V1
#include <rocprofiler/rocprofiler.h>
int main() {
// Use the rocprofilerV1 API
return 0;
}
```
This can be built in the following manner:
```sh
gcc main.c -I/opt/rocm-5.5.0/include -L/opt/rocm-5.5.0/lib -lrocprofiler64
```
The resulting `a.out` will depend on
`/opt/rocm-5.5.0/lib/librocprofiler64.so.1`.
The ROCm Profiler that uses `rocprofilerV2` API can be invoked using the
following command:
```sh
rocprofv2 …
```
To write a custom tool based on the `rocprofilerV2` API do the following:
```C
main.c:
#include <rocprofiler/rocprofiler.h>
int main() {
// Use the rocprofilerV2 API
return 0;
}
```
This can be built in the following manner:
```sh
gcc main.c -I/opt/rocm-5.5.0/include -L/opt/rocm-5.5.0/lib -lrocprofiler64
```
The resulting `a.out` will depend on
`/opt/rocm-5.5.0/lib/librocprofiler64.so.1`.
## ROCprofiler for rocm 5.6.0
In ROCm 5.6 the `rocprofilerv1` and `rocprofilerv2` include and library files of
ROCm 5.5 are split into separate files. The `rocmtools` files that were
deprecated in ROCm 5.5 have been removed.
| ROCm 5.6 | rocprofilerv1 | rocprofilerv2 |
|-----------------|-------------------------------------|----------------------------------------|
| **Tool script** | `bin/rocprof` | `bin/rocprofv2` |
| **API include** | `include/rocprofiler/rocprofiler.h` | `include/rocprofiler/v2/rocprofiler.h` |
| **API library** | `lib/librocprofiler.so.1` | `lib/librocprofiler.so.2` |
The ROCm Profiler Tool that uses `rocprofilerV1` can be invoked using the
following command:
```sh
rocprof …
```
To write a custom tool based on the `rocprofilerV1` API do the following:
```C
main.c:
#include <rocprofiler/rocprofiler.h> // Use the rocprofilerV1 API
int main() {
// Use the rocprofilerV1 API
return 0;
}
```
This can be built in the following manner:
```sh
gcc main.c -I/opt/rocm-5.6.0/include -L/opt/rocm-5.6.0/lib -lrocprofiler64
```
The resulting `a.out` will depend on
`/opt/rocm-5.6.0/lib/librocprofiler64.so.1`.
The ROCm Profiler that uses `rocprofilerV2` API can be invoked using the
following command:
```sh
rocprofv2 …
```
To write a custom tool based on the `rocprofilerV2` API do the following:
```C
main.c:
#include <rocprofiler/v2/rocprofiler.h> // Use the rocprofilerV2 API
int main() {
// Use the rocprofilerV2 API
return 0;
}
```
This can be built in the following manner:
```sh
gcc main.c -I/opt/rocm-5.6.0/include -L/opt/rocm-5.6.0/lib -lrocprofiler64v2
```
The resulting `a.out` will depend on
`/opt/rocm-5.6.0/lib/librocprofiler64.so.2`.
### Optimized
- Improved Test Suite
### Added
- 'end_time' need to be disabled in roctx_trace.txt
- support for hsa_amd_memory_async_copy_on_engine API function trace
### Fixed
- rocprof in ROcm/5.4.0 gpu selector broken.
- rocprof in ROCm/5.4.1 fails to generate kernel info.
- rocprof clobbers LD_PRELOAD.
## ROCprofiler for rocm 5.7.0
### Navi support
Rocprofiler for ROCm 5.7 added support for counter collection (PMC) and advanced thread tracing (ATT) for Navi21 and Navi31 GPUs.
- On Navi3x, counter collection requires the GPU to be in a stable power state. See README.md for instructions. HIP RT in ATT not yet supported.
### Changed
- ATT analysis will not run by default. For ATT to have the same behavior as 5.5, use --plugin att <as.s> --mode network
- Kernel Names are now removed from HIP API records, users of the API can get the kernel names from the corresponding HIP Dispatch OPS using the correlation ID, this change was done to optimize and to manage the data copied.
- Removing Replay modes as we discovered that some of them will corrupt the applications' behavior, we will re-add them once we implement the fix for them.
### Optimized
- Improved ATT parser performance and file sizes.
- Now profiler autocorrect user input errors for pmc and throws exception for wrong input with this message:"Bad input metric. usage --> pmc: [counter1] [counter2]"
### Added
- Every API trace in V2 reported synchronously will have two records, one for Enter phase and for Exit phase
- File Plugin now reports the HSA OPS operation kind as part of the output text
- MI300 counters support for rocprof v1 and v2.
- Support for MI300 XCC modes for rocprof v2.
- MI300 individual XCC counters dumped per-xcc as separate records but with same record-id and kernel dispatch info
- Naming for MPI ranks. Filenames containing "%rank" are replaced by variables "MPI_RANK", "OMPI_COMM_WORLD_RANK" or "MV2_COMM_WORLD_RANK".
- MPI Rank will appear in perfetto track names.
- File plugin has been split to File & CLI plugins, CLI plugin is responsible for showing results on the terminal screen and will be automatically the choice if no -d option given in rocprof, File plugin on the other hand is responsible for writing the output results in files if -d option is given.
- Structure of the results is different for both CLI & File plugin; File plugin will make sure every type of result is in a separate file, starting by specifying the header; CLI plugin will have the records in the old way.
Example for file plugin output:
```string
Dispatch_ID,GPU_ID,Queue_ID,Queue_Index,PID,TID,GRD,WGR,LDS,SCR,Arch_VGPR,ACCUM_VGPR,SGPR,Wave_Size,SIG,OBJ,Kernel_Name,Start_Timestamp,End_Timestamp,Correlation_ID,GRBM_COUNT
1,4,1,1,1584730,1584730,10,10,0,0,8,0,16,64,140464978048000,1,"helloworld(char*, char*) (.kd)",0,140469300947216,33,12637.000000
```
```string
Domain,Function,Kernel_Name,Start_Timestamp,End_Timestamp,Correlation_ID
HIP_API_DOMAIN,hipGetDeviceProperties,,316678074094190,316678074098929,1
HIP_API_DOMAIN,hipMalloc,,316678074105702,316678074130851,2
HIP_API_DOMAIN,hipMalloc,,316678074131382,316678074136111,3
```
- Removing Record IDs from tracer records in CLI plugin.
- Added Flush Interval and Trace Period functionality, where --flush-interval [time_in_ms], for flushing the buffers every given interval by the user, and --trace-period [delay]:[trace_time]:[interval], where delay is the time to wait before starting session, trace_time is the time between every start and stop session and interval the time between two consecutive sessions (omitting interval = infinite). For more details please refer to the ROCProfV2 tool usage document.
- Added requirements.txt to be used to install all the necessary python3 packages.
- ATT plugin:
- Added --mode, --mpi and --depth parameters.
- Limiting file name sizes for large kernels.
- SE_MASK parameter for input.txt, a binary mask specifying for which shader engines to collect from.
On GFX9, SEs are masked out completely. On Navi only part of the data is masked.
The use of SE_MASK=0x1 is heavily encouraged to avoid packet lost events.
- "ROCPROFILER_MAX_ATT_PROFILES" environment variable can be set. Previously fixed at 16, now the default is 1.
- Increased default ATT buffer size per collection to 1GB. Added "BUFFER_SIZE=size" (in MB) parameter to set buffer size.
- Added "DISPATCH=id" or "DISPATCH=id,rank" to set which dispatch ids to profile for which MPI rank.
### Fixed
- Samples are fixed to show the new usage of phases.
- Plugin option validates the plugin names.
- Fixing rocsys, for rocsys options, rocsys -h can be called
- "--output-file" option ignored when no output folder was specified.
- Perfetto crash when using ROCTX and/or no output file specified.
- Parsing of the getpc, setpc and swappc instructions with registers loaded from scratch space.
- Some browsers caching ATT data from older kernels.
- Navi2x GPUs required the first counter to be GRBM. This is fixed in 5.7.
- If ROCPROFILER_METRICS_PATH environment variable is not set, the counters xml path will be taken from the following path (../libexec/rocprofiler/counters/derived_counters.xml) which is relative to librocprofiler64.so.2.0.0
- Repeated base metrics were not being properly reused by derived counters.
- Fixed wrong dispatch ID on kernel.txt
## ROCprofiler for rocm 6.0
### Added
- Updated supported GPU architectures in README with profiler versions
- Automatic ISA dumping for ATT. See README.
- CSV mode for ATT. See README.
- Added option to control kernel name truncation.
- Limit rocprof(v1) srcipt usage to only supported architectures.
- Added Tool versioning to be able to run rocprofv2 using rocprof. See README for more information.
- Added Plugin Versioning way in rocprofv2. See README for more details.
- Added --version in the rocprof and rocprofv2 to be able to see the current rocprof/v2 version along with ROCm version information.
- Extended rocprof(v1) support for MI300.
## ROCprofiler for rocm 6.1
### Added
- ATT: Continuous mode. The input file now has a DISPATCH_RANGE=begin,end that specifies continuous ATT collection during the application run.
- ATT: Ability to dump raw codeobjects as .out files with ISA_CAPTURE_MODE=[0,1,2]
- Updated README and CMakeLists.txt for rocm-llvm-dev dependency.
### Removed
- ATT: Network mode, since it was redundant with file mode.
- ATT: Generated isa.s file, since the functionality was moved from collection to parsing time.
### Fixed
- Multiple fixes and optimizations for ATT (Advanced Thread Tracer).
- Fixed format of presenting MI300 counters for individual XCCs.
- Fixed ROCprofiler to match versioning changes in HIP Runtime.
- Fixed plugins race condition.
- Updated metrics to MI300.
## ROCprofiler for rocm 6.2
### Removed
- pcsampler sample code has been removed due to deprecation from v2.
## ROCProfiler for ROCm 6.3
### Added
- JSON output plugin for `rocprofv2`. The JSON file matches Google Trace Format making it easy to load on Perfetto, Chrome tracing, or Speedscope. For Speedscope, use `--disable-json-data-flows` option as speedscope doesn't work with data flows.
- `--no-serialization` flag to disable kernel serialization when `rocprofv2` is in counter collection mode. This allows `rocprofv2` to avoid deadlock when profiling certain programs in counter collection mode.
- `FP64_ACTIVE` and `ENGINE_ACTIVE` metrics to AMD Instinct MI300 accelerator
- New HIP APIs with struct defined inside union.
- Early checks to confirm the eligibility of ELF file in ATT plugin
- Support for kernel name filtering in `rocprofv2`
- Barrier bit to read and stop packets
- ROCProfiler support for gfx1150 and gfx1151
- ATT support for gfx12
- gfx12 support
### Changed
- Extended lifetime for proxy queues
- Setting the `trace-start` option for `rocprof` to `off` now disables kernel tracing
- `libpciaccess-dev` functions now load with `dlopen`
- `PcieAccessApi*` api and `void* libpciaccess_handle` are now initialized to `nullptr`
### Removed
- Obsolete BSD and GPL licenses
- `libsystemd-dev` from `CMakeLists.txt`
### Optimized
- ROCProfiler Performance improved to reduce profiling time for large workloads of counter collection
- Fixed bandwidth measurement in MI300
- Fixed Perfetto plugin issue of `roctx` trace not getting displayed
- Fixed `--help` for counter collection
- Fixed signal management issues in `queue.cpp`
- Fixed Perfetto tracks for multi-GPU
- Fixed Perfetto plugin usage with `rocsys`
- Fixed incorrect number of columns in the output CSV files for counter collection and kernel tracing
- Fixed the ROCProfiler hang issue when running kernel trace, thread trace, or counter collection on Iree benchmark for AMD Instinct MI300 accelerator
- Fixed build errors thrown during parsing of unions
- Fixed the system hang caused while running `--kernel-trace` with Perfetto for certain applications
- Fixed missing profiler records issue caused while running `--trace-period`
- Fixed the hang issue of `ProfilerAPITest` of `runFeatureTests` on AMD Instinct MI300 accelerator
- Fixed segment fault on Navi32
## ROCProfiler for ROCm 6.4
### Added
- README updated for kernel filtration
- Tool deprecation message for rocprofiler v1,v2
- Adding ops 16,32,64 metrics for rdc
### Resolved issues
- Fixed issue where invalid UTF-8 characters in a trace log would crash the program
+598
View File
@@ -0,0 +1,598 @@
# ##############################################################################
# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
#
# 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 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.
# ##############################################################################
cmake_minimum_required(VERSION 3.18.0 FATAL_ERROR)
set(CMAKE_C_FLAGS_COVERAGE_INIT
"-Og -g3 -fno-omit-frame-pointer -fprofile-abs-path -fprofile-arcs -ftest-coverage --coverage"
CACHE STRING "C flags for code coverage builds")
set(CMAKE_CXX_FLAGS_COVERAGE_INIT
"-Og -g3 -fno-omit-frame-pointer -fprofile-abs-path -fprofile-arcs -ftest-coverage --coverage"
CACHE STRING "C++ flags for code coverage builds")
set(CMAKE_C_FLAGS_COVERAGE
"${CMAKE_C_FLAGS_COVERAGE_INIT}"
CACHE STRING "C flags for code coverage builds")
set(CMAKE_CXX_FLAGS_COVERAGE
"${CMAKE_CXX_FLAGS_COVERAGE_INIT}"
CACHE STRING "C++ flags for code coverage builds")
# Build is not supported on Windows plaform
if(WIN32)
message(FATAL_ERROR "Windows build is not supported.")
endif()
# Set module name and project name.
set(ROCPROFILER_NAME "rocprofiler")
set(ROCPROFILER_TARGET "${ROCPROFILER_NAME}64")
set(ROCPROFILER_LIBRARY "lib${ROCPROFILER_TARGET}")
project(rocprofiler VERSION 2.0.0)
if(${ROCM_PATCH_VERSION})
set(PROJECT_VERSION_PATCH ${ROCM_PATCH_VERSION})
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
endif()
include(GNUInstallDirs)
# set default ROCM_PATH
if(NOT DEFINED ROCM_PATH)
set(ROCM_PATH
"/opt/rocm"
CACHE STRING "Default ROCM installation directory")
endif()
set(CMAKE_INSTALL_LIBDIR "lib" CACHE STRING "Library install directory")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(THREADS_PREFER_PTHREAD_FLAG ON)
set(CMAKE_BUILD_RPATH
"${PROJECT_BINARY_DIR}:${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_BUILD_RPATH_USE_ORIGIN ON)
set(CMAKE_SKIP_BUILD_RPATH OFF)
# Adding default path cmake modules
list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules")
# Set build environment
include(rocprofiler_options)
include(rocprofiler_utils)
include(rocprofiler_env)
include(rocprofiler_formatting)
include(rocprofiler_linting)
# Setup the package version.
rocprofiler_get_version("1.0.0")
message("-- LIB-VERSION: ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
set(BUILD_VERSION_MAJOR ${VERSION_MAJOR})
set(BUILD_VERSION_MINOR ${VERSION_MINOR})
set(BUILD_VERSION_PATCH ${VERSION_PATCH})
if(DEFINED VERSION_BUILD AND NOT ${VERSION_BUILD} STREQUAL "")
message("VERSION BUILD DEFINED ${VERSION_BUILD}")
set(BUILD_VERSION_PATCH "${BUILD_VERSION_PATCH}-${VERSION_BUILD}")
endif()
set(BUILD_VERSION_STRING
"${BUILD_VERSION_MAJOR}.${BUILD_VERSION_MINOR}.${BUILD_VERSION_PATCH}")
set(LIB_VERSION_MAJOR ${VERSION_MAJOR})
set(LIB_VERSION_MINOR ${VERSION_MINOR})
if(${ROCM_PATCH_VERSION})
set(LIB_VERSION_PATCH ${ROCM_PATCH_VERSION})
else()
set(LIB_VERSION_PATCH ${VERSION_PATCH})
endif()
set(LIB_VERSION_STRING "${LIB_VERSION_MAJOR}.${LIB_VERSION_MINOR}.${LIB_VERSION_PATCH}")
message("-- LIB-VERSION STRING: ${LIB_VERSION_STRING}")
# Set target and root/lib/test directory
set(TARGET_NAME "${ROCPROFILER_TARGET}")
set(ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(LIB_DIR "${ROOT_DIR}/src")
set(TEST_DIR "${ROOT_DIR}/test")
# To set additional RUNPATH in libraries installed in /opt/rocm-ver/lib/roctracer
set(ROCM_APPEND_PRIVLIB_RPATH "\$ORIGIN/..")
find_package(
amd_comgr REQUIRED CONFIG
HINTS ${CMAKE_INSTALL_PREFIX}
PATHS ${ROCM_PATH}
PATH_SUFFIXES lib/cmake/amd_comgr)
message(STATUS "Code Object Manager found at ${amd_comgr_DIR}.")
link_libraries(amd_comgr)
find_package(Threads REQUIRED)
find_package(
hsa-runtime64 REQUIRED CONFIG
HINTS ${CMAKE_INSTALL_PREFIX}
PATHS ${ROCM_PATH})
find_package(
HIP REQUIRED CONFIG
HINTS ${CMAKE_INSTALL_PREFIX}
PATHS ${ROCM_PATH})
find_package(
Clang CONFIG
PATHS "${ROCM_PATH}"
PATH_SUFFIXES "llvm/lib/cmake/clang")
if(NOT Clang_FOUND)
message(
FATAL_ERROR
"\
The \"rocm-llvm-dev\" package is not installed. \
Please install it for your OS distribution. e.g: for Ubuntu: \"sudo apt-get install rocm-llvm-dev\".\
")
endif()
find_library(NUMA NAME numa REQUIRED)
link_libraries(${NUMA})
get_property(
HSA_RUNTIME_INCLUDE_DIRECTORIES
TARGET hsa-runtime64::hsa-runtime64
PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
find_file(
HSA_H hsa.h
PATHS ${HSA_RUNTIME_INCLUDE_DIRECTORIES}
PATH_SUFFIXES hsa
NO_DEFAULT_PATH REQUIRED)
get_filename_component(HSA_RUNTIME_INC_PATH ${HSA_H} DIRECTORY)
include_directories(${HSA_RUNTIME_INC_PATH})
if(NOT DEFINED LIBRARY_TYPE)
set(LIBRARY_TYPE SHARED)
endif()
# Enable tracing API
if(NOT USE_PROF_API)
set(USE_PROF_API 1)
endif()
# Enable use of getROCmInstallPath() API
if(USE_GET_ROCM_PATH_API)
set(USE_GET_ROCM_PATH_API 1)
message(STATUS "Enabled Use of getROCmInstallPath() API" )
endif()
# Protocol header lookup
set(PROF_API_HEADER_NAME prof_protocol.h)
if(USE_PROF_API EQUAL 1)
find_path(
PROF_API_HEADER_DIR ${PROF_API_HEADER_NAME}
HINTS ${PROF_API_HEADER_PATH}
PATHS /opt/rocm/include
PATH_SUFFIXES roctracer/ext)
if(NOT PROF_API_HEADER_DIR)
message(
FATAL_ERROR
"Profiling API header not found. Tracer integration disabled. Use -DPROF_API_HEADER_PATH=<path to ${PROF_API_HEADER_NAME} header>"
)
else()
include_directories(${PROF_API_HEADER_DIR})
message(STATUS "Profiling API: ${PROF_API_HEADER_DIR}/${PROF_API_HEADER_NAME}")
endif()
endif()
enable_testing()
# Temporarily for CI to work
set(ROCPROFILER_BUILD_TESTS ON)
set(ROCPROFILER_BUILD_CI ON)
if(ROCPROFILER_BUILD_CI)
include(CTest)
endif()
# Build libraries
add_subdirectory(src)
# Build Plugins
add_subdirectory(plugin)
if(ROCPROFILER_BUILD_SAMPLES)
# Build samples
add_subdirectory(samples)
endif()
if(ROCPROFILER_BUILD_TESTS)
# Build tests
add_subdirectory(test)
add_subdirectory(tests-v2)
endif()
# Installation and packaging
set(DEST_NAME ${ROCPROFILER_NAME})
if(DEFINED CMAKE_INSTALL_PREFIX)
get_filename_component(prefix_name ${CMAKE_INSTALL_PREFIX} NAME)
get_filename_component(prefix_dir ${CMAKE_INSTALL_PREFIX} DIRECTORY)
if(prefix_name STREQUAL ${DEST_NAME})
set(CMAKE_INSTALL_PREFIX ${prefix_dir})
endif()
endif()
if(DEFINED CPACK_PACKAGING_INSTALL_PREFIX)
get_filename_component(prefix_name ${CPACK_PACKAGING_INSTALL_PREFIX} NAME)
get_filename_component(prefix_dir ${CPACK_PACKAGING_INSTALL_PREFIX} DIRECTORY)
if(prefix_name STREQUAL ${DEST_NAME})
set(CPACK_PACKAGING_INSTALL_PREFIX ${prefix_dir})
endif()
else()
set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
endif()
set(ORIGINAL_SCRIPT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin/tblextr.py)
set(OUTPUT_SCRIPT_PATH ${CMAKE_CURRENT_BINARY_DIR}/bin/tblextr.py)
configure_file(${ORIGINAL_SCRIPT_PATH} ${OUTPUT_SCRIPT_PATH} @ONLY)
message("CMake-install-prefix: ${CMAKE_INSTALL_PREFIX}")
message("CPack-install-prefix: ${CPACK_PACKAGING_INSTALL_PREFIX}")
message("-----------Dest-name: ${DEST_NAME}")
# Install headers
install(
FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/core/activity.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${ROCPROFILER_NAME}
COMPONENT dev)
# rpl_run.sh
install(
PROGRAMS ${CMAKE_CURRENT_SOURCE_DIR}/bin/rpl_run.sh
DESTINATION ${CMAKE_INSTALL_BINDIR}
RENAME rocprof
COMPONENT runtime)
configure_file(bin/rocprofv2 ${PROJECT_BINARY_DIR} COPYONLY)
install(
PROGRAMS ${PROJECT_SOURCE_DIR}/bin/rocprofv2
DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT runtime)
install(
PROGRAMS ${CMAKE_CURRENT_SOURCE_DIR}/bin/txt2xml.sh
${CMAKE_CURRENT_SOURCE_DIR}/bin/merge_traces.sh
DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/${ROCPROFILER_NAME}
COMPONENT runtime)
install(
FILES ${CMAKE_CURRENT_SOURCE_DIR}/bin/txt2params.py
${CMAKE_CURRENT_SOURCE_DIR}/bin/txt2params.py
${CMAKE_CURRENT_BINARY_DIR}/bin/tblextr.py
${CMAKE_CURRENT_SOURCE_DIR}/bin/dform.py
${CMAKE_CURRENT_SOURCE_DIR}/bin/mem_manager.py
${CMAKE_CURRENT_SOURCE_DIR}/bin/sqlitedb.py
DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/${ROCPROFILER_NAME}
COMPONENT runtime)
# gfx_metrics.xml metrics.xml
install(
FILES ${CMAKE_CURRENT_SOURCE_DIR}/test/tool/metrics.xml
${CMAKE_CURRENT_SOURCE_DIR}/test/tool/gfx_metrics.xml
DESTINATION ${CMAKE_INSTALL_LIBDIR}/${ROCPROFILER_NAME}
COMPONENT runtime)
if(${LIBRARY_TYPE} STREQUAL SHARED)
# Packaging directives
set(CPACK_GENERATOR "DEB" "RPM" "TGZ" CACHE STRING "CPACK GENERATOR DEB;RPM")
set(ENABLE_LDCONFIG
ON
CACHE BOOL "Set library links and caches using ldconfig.")
set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
set(CPACK_PACKAGE_VENDOR "Advanced Micro Devices, Inc.")
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
set(CPACK_PACKAGE_VERSION
"${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}"
)
set(CPACK_PACKAGE_CONTACT "ROCm Profiler Support <dl.ROCm-Profiler.support@amd.com>")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY
"ROCPROFILER library for AMD HSA runtime API extension support")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
if(DEFINED ENV{ROCM_LIBPATCH_VERSION})
set(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION}.$ENV{ROCM_LIBPATCH_VERSION}")
message("Using CPACK_PACKAGE_VERSION ${CPACK_PACKAGE_VERSION}")
endif()
# Debian package specific variable for ASAN
set(CPACK_DEBIAN_ASAN_PACKAGE_NAME "${ROCPROFILER_NAME}-asan")
set(CPACK_DEBIAN_ASAN_PACKAGE_DEPENDS "hsa-rocr-asan, rocm-core-asan")
# Install license file
install(
FILES ${CPACK_RESOURCE_FILE_LICENSE}
DESTINATION ${CMAKE_INSTALL_DOCDIR}
COMPONENT runtime)
install(
FILES ${CPACK_RESOURCE_FILE_LICENSE}
DESTINATION ${CMAKE_INSTALL_DOCDIR}-asan
COMPONENT asan)
# Debian package specific variables
if(DEFINED ENV{CPACK_DEBIAN_PACKAGE_RELEASE})
set(CPACK_DEBIAN_PACKAGE_RELEASE $ENV{CPACK_DEBIAN_PACKAGE_RELEASE})
else()
set(CPACK_DEBIAN_PACKAGE_RELEASE "local")
endif()
message("Using CPACK_DEBIAN_PACKAGE_RELEASE ${CPACK_DEBIAN_PACKAGE_RELEASE}")
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_DEBIAN_FILE_NAME "DEB-DEFAULT")
set(CPACK_DEBIAN_RUNTIME_PACKAGE_NAME "${PROJECT_NAME}")
set(CPACK_DEBIAN_RUNTIME_PACKAGE_DEPENDS
"rocminfo, hsa-rocr, rocm-core, libelf-dev, libnuma-dev, libxml2-dev"
)
set(CPACK_DEBIAN_DEV_PACKAGE_NAME "${PROJECT_NAME}-dev")
set(CPACK_DEBIAN_DEV_PACKAGE_DEPENDS "${PROJECT_NAME}, hsa-rocr-dev, rocm-core")
set(CPACK_DEBIAN_TESTS_PACKAGE_NAME "${PROJECT_NAME}-tests")
set(CPACK_DEBIAN_TESTS_PACKAGE_DEPENDS "${PROJECT_NAME}-dev, hsa-rocr-dev, rocm-core")
set(CPACK_DEBIAN_SAMPLES_PACKAGE_NAME "${PROJECT_NAME}-samples")
set(CPACK_DEBIAN_SAMPLES_PACKAGE_DEPENDS
"${PROJECT_NAME}-dev, hsa-rocr-dev, rocm-core")
set(CPACK_DEBIAN_DOCS_PACKAGE_NAME "${PROJECT_NAME}-docs")
set(CPACK_DEBIAN_DOCS_PACKAGE_DEPENDS "${PROJECT_NAME}-dev, hsa-rocr-dev, rocm-core")
set(CPACK_DEBIAN_PLUGINS_PACKAGE_NAME "${PROJECT_NAME}-plugins")
set(CPACK_DEBIAN_PLUGINS_PACKAGE_DEPENDS "${PROJECT_NAME}, hsa-rocr-dev, rocm-core")
set(CPACK_DEBIAN_CHANGELOG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/CHANGELOG.md")
# RPM package specific variables
if(DEFINED ENV{CPACK_RPM_PACKAGE_RELEASE})
set(CPACK_RPM_PACKAGE_RELEASE $ENV{CPACK_RPM_PACKAGE_RELEASE})
else()
set(CPACK_RPM_PACKAGE_RELEASE "local")
endif()
message("Using CPACK_RPM_PACKAGE_RELEASE ${CPACK_RPM_PACKAGE_RELEASE}")
set(CPACK_RPM_PACKAGE_LICENSE "MIT")
# 'dist' breaks manual builds on debian systems due to empty Provides
execute_process(
COMMAND rpm --eval %{?dist}
RESULT_VARIABLE PROC_RESULT
OUTPUT_VARIABLE EVAL_RESULT
OUTPUT_STRIP_TRAILING_WHITESPACE)
message("RESULT_VARIABLE ${PROC_RESULT} OUTPUT_VARIABLE: ${EVAL_RESULT}")
if(PROC_RESULT EQUAL "0" AND NOT EVAL_RESULT STREQUAL "")
string(APPEND CPACK_RPM_PACKAGE_RELEASE "%{?dist}")
endif()
set(CPACK_RPM_COMPONENT_INSTALL ON)
set(CPACK_RPM_FILE_NAME "RPM-DEFAULT")
set(CPACK_RPM_RUNTIME_PACKAGE_NAME "${PROJECT_NAME}")
set(CPACK_RPM_RUNTIME_PACKAGE_REQUIRES
"rocminfo, hsa-rocr, rocm-core, libxml2-devel")
set(CPACK_RPM_DEV_PACKAGE_NAME "${PROJECT_NAME}-devel")
set(CPACK_RPM_DEV_PACKAGE_REQUIRES "${PROJECT_NAME}, hsa-rocr-devel, rocm-core")
set(CPACK_RPM_DEV_PACKAGE_PROVIDES "${PROJECT_NAME}-dev")
set(CPACK_RPM_DEV_PACKAGE_OBSOLETES "${PROJECT_NAME}-dev")
set(CPACK_RPM_TESTS_PACKAGE_NAME "${PROJECT_NAME}-tests")
set(CPACK_RPM_TESTS_PACKAGE_REQUIRES "${PROJECT_NAME}-devel, hsa-rocr-devel, rocm-core")
set(CPACK_RPM_DOCS_PACKAGE_NAME "${PROJECT_NAME}-docs")
set(CPACK_RPM_DOCS_PACKAGE_REQUIRES "${PROJECT_NAME}-devel, hsa-rocr-devel, rocm-core")
set(CPACK_RPM_PLUGINS_PACKAGE_NAME "${PROJECT_NAME}-plugins")
set(CPACK_RPM_PLUGINS_PACKAGE_REQUIRES "${PROJECT_NAME}, hsa-rocr-devel, rocm-core")
set(CPACK_RPM_PACKAGE_AUTOREQ 0)
set(CPACK_RPM_SAMPLES_PACKAGE_NAME "${PROJECT_NAME}-samples")
set(CPACK_RPM_SAMPLES_PACKAGE_REQUIRES
"${PROJECT_NAME}-devel, hsa-rocr-devel, rocm-core, hip-runtime-amd")
message("CPACK_RPM_PACKAGE_RELEASE: ${CPACK_RPM_PACKAGE_RELEASE}")
# Disable build id for rocprofiler as its creating transaction error
set(CPACK_RPM_SPEC_MORE_DEFINE
"%define _build_id_links none
%global __strip ${CPACK_STRIP_EXECUTABLE}
%global __objdump ${CPACK_OBJDUMP_EXECUTABLE}
%global __objcopy ${CPACK_OBJCOPY_EXECUTABLE}
%global __readelf ${CPACK_READELF_EXECUTABLE}")
# RPM package specific variable for ASAN
set(CPACK_RPM_ASAN_PACKAGE_NAME "${ROCPROFILER_NAME}-asan")
set(CPACK_RPM_ASAN_PACKAGE_REQUIRES "hsa-rocr-asan, rocm-core-asan")
# set ( CPACK_RPM_CHANGELOG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/CHANGELOG.md" )
# Remove dependency on rocm-core if -DROCM_DEP_ROCMCORE=ON not given to cmake
if(NOT ROCM_DEP_ROCMCORE)
string(REGEX REPLACE ",? ?rocm-core" "" CPACK_RPM_RUNTIME_PACKAGE_REQUIRES
${CPACK_RPM_RUNTIME_PACKAGE_REQUIRES})
string(REGEX REPLACE ",? ?rocm-core" "" CPACK_RPM_DEV_PACKAGE_REQUIRES
${CPACK_RPM_DEV_PACKAGE_REQUIRES})
string(REGEX REPLACE ",? ?rocm-core-asan" "" CPACK_RPM_ASAN_PACKAGE_REQUIRES
${CPACK_RPM_ASAN_PACKAGE_REQUIRES})
string(REGEX REPLACE ",? ?rocm-core" "" CPACK_RPM_TESTS_PACKAGE_REQUIRES
${CPACK_RPM_TESTS_PACKAGE_REQUIRES})
string(REGEX REPLACE ",? ?rocm-core" "" CPACK_RPM_SAMPLES_PACKAGE_REQUIRES
${CPACK_RPM_SAMPLES_PACKAGE_REQUIRES})
string(REGEX REPLACE ",? ?rocm-core" "" CPACK_RPM_DOCS_PACKAGE_REQUIRES
${CPACK_RPM_DOCS_PACKAGE_REQUIRES})
string(REGEX REPLACE ",? ?rocm-core" "" CPACK_RPM_PLUGINS_PACKAGE_REQUIRES
${CPACK_RPM_PLUGINS_PACKAGE_REQUIRES})
string(REGEX REPLACE ",? ?rocm-core" "" CPACK_DEBIAN_RUNTIME_PACKAGE_DEPENDS
${CPACK_DEBIAN_RUNTIME_PACKAGE_DEPENDS})
string(REGEX REPLACE ",? ?rocm-core" "" CPACK_DEBIAN_DEV_PACKAGE_DEPENDS
${CPACK_DEBIAN_DEV_PACKAGE_DEPENDS})
string(REGEX REPLACE ",? ?rocm-core-asan" "" CPACK_DEBIAN_ASAN_PACKAGE_DEPENDS
${CPACK_DEBIAN_ASAN_PACKAGE_DEPENDS})
string(REGEX REPLACE ",? ?rocm-core" "" CPACK_DEBIAN_TESTS_PACKAGE_DEPENDS
${CPACK_DEBIAN_TESTS_PACKAGE_DEPENDS})
string(REGEX REPLACE ",? ?rocm-core" "" CPACK_DEBIAN_SAMPLES_PACKAGE_DEPENDS
${CPACK_DEBIAN_SAMPLES_PACKAGE_DEPENDS})
string(REGEX REPLACE ",? ?rocm-core" "" CPACK_DEBIAN_DOCS_PACKAGE_DEPENDS
${CPACK_DEBIAN_DOCS_PACKAGE_DEPENDS})
string(REGEX REPLACE ",? ?rocm-core" "" CPACK_DEBIAN_PLUGINS_PACKAGE_DEPENDS
${CPACK_DEBIAN_PLUGINS_PACKAGE_DEPENDS})
endif()
# set components
if(ENABLE_ASAN_PACKAGING)
# ASAN Package requires only asan component with libraries and license file
set(CPACK_COMPONENTS_ALL asan)
else()
set(CPACK_COMPONENTS_ALL runtime dev tests docs plugins samples)
endif()
include(CPack)
cpack_add_component(
runtime
DISPLAY_NAME "Runtime"
DESCRIPTION "Dynamic libraries for the ROCProfiler")
cpack_add_component(
dev
DISPLAY_NAME "Development"
DESCRIPTION "Development needed header files for ROCProfiler"
DEPENDS runtime)
cpack_add_component(
plugins
DISPLAY_NAME "ROCProfile Plugins"
DESCRIPTION "Plugins for handling ROCProfiler data output"
DEPENDS runtime)
cpack_add_component(
tests
DISPLAY_NAME "Tests"
DESCRIPTION "Tests for the ROCProfiler"
DEPENDS dev)
cpack_add_component(
samples
DISPLAY_NAME "Samples"
DESCRIPTION "Samples for the ROCProfiler"
DEPENDS dev)
cpack_add_component(
docs
DISPLAY_NAME "Documentation"
DESCRIPTION "Documentation for the ROCProfiler API"
DEPENDS dev)
cpack_add_component(
asan
DISPLAY_NAME "ASAN"
DESCRIPTION "ASAN libraries for the ROCPROFILER"
DEPENDS asan)
endif()
find_package(Doxygen)
if(DOXYGEN_FOUND)
# # Set input and output files for API Document
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile_API.in)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile_API)
# # Request to configure the file
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/doc/html/index.html
${CMAKE_CURRENT_BINARY_DIR}/doc/latex/refman.pdf
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
COMMAND make -C ${CMAKE_CURRENT_BINARY_DIR}/doc/latex pdf
MAIN_DEPENDENCY ${DOXYGEN_OUT}
${DOXYGEN_IN}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/include/rocprofiler/v2/rocprofiler_plugin.h
${CMAKE_CURRENT_SOURCE_DIR}/include/rocprofiler/v2/rocprofiler.h
COMMENT "Generating API documentation")
add_custom_target(doc DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/doc/html/index.html
${CMAKE_CURRENT_BINARY_DIR}/doc/latex/refman.pdf)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/doc/latex/refman.pdf"
DESTINATION ${CMAKE_INSTALL_DOCDIR}
RENAME "${PROJECT_NAME}v2_api_spec.pdf"
OPTIONAL
COMPONENT docs)
install(
DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/doc/html/"
DESTINATION ${CMAKE_INSTALL_DOCDIR}/html
OPTIONAL
COMPONENT docs)
# # Set input and output files for Tools Document
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile_Tool.in)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/tooldoc/Doxyfile_Tool)
# # Request to configure the file
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/tooldoc/html/index.html
${CMAKE_CURRENT_BINARY_DIR}/tooldoc/latex/refman.pdf
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
COMMAND make -C ${CMAKE_CURRENT_BINARY_DIR}/tooldoc/latex pdf
MAIN_DEPENDENCY ${DOXYGEN_OUT}
${DOXYGEN_IN}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/doc/rocprofv2_tool.md
COMMENT "Generating Tools documentation")
add_custom_target(
doc_tool DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/tooldoc/html/index.html
${CMAKE_CURRENT_BINARY_DIR}/tooldoc/latex/refman.pdf)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/tooldoc/latex/refman.pdf"
DESTINATION ${CMAKE_INSTALL_DOCDIR}
RENAME "${PROJECT_NAME}v2_tool.pdf"
OPTIONAL
COMPONENT docs)
install(
DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tooldoc/html/"
DESTINATION ${CMAKE_INSTALL_DOCDIR}/html
OPTIONAL
COMPONENT docs)
# # Set input and output files for changelog document
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile_ChangeLog.in)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/doc/changelog/Doxyfile_ChangeLog)
# # Request to configure the file
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/doc/changelog/latex/refman.pdf
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
COMMAND make -C ${CMAKE_CURRENT_BINARY_DIR}/doc/changelog/latex pdf
MAIN_DEPENDENCY ${DOXYGEN_OUT}
${DOXYGEN_IN}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/CHANGELOG.md
COMMENT "Generating changelog documentation")
add_custom_target(doc_changelog
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/doc/changelog/latex/refman.pdf)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/doc/changelog/latex/refman.pdf"
DESTINATION ${CMAKE_INSTALL_DOCDIR}
RENAME "${PROJECT_NAME}_ChangeLog.pdf"
OPTIONAL
COMPONENT docs)
add_dependencies(doc doc_changelog)
endif()
+1
View File
@@ -0,0 +1 @@
* @ammarwa @bgopesh
+23
View File
@@ -0,0 +1,23 @@
#!/bin/bash
set -e
do_ldconfig() {
# left-hand term originates from ENABLE_LDCONFIG = ON/OFF at package build
if [ "@ENABLE_LDCONFIG@" == "ON" ]; then
echo @CPACK_PACKAGING_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@ > /@CMAKE_INSTALL_SYSCONFDIR@/ld.so.conf.d/librocprofiler64.conf
ldconfig
fi
}
case "$1" in
( configure )
do_ldconfig
;;
( abort-upgrade | abort-remove | abort-deconfigure )
echo "$1"
;;
( * )
exit 0
;;
esac
+22
View File
@@ -0,0 +1,22 @@
#!/bin/bash
set -e
rm_ldconfig() {
# left-hand term originates from ENABLE_LDCONFIG = ON/OFF at package build
if [ "@ENABLE_LDCONFIG@" == "ON" ]; then
rm -f /@CMAKE_INSTALL_SYSCONFDIR@/ld.so.conf.d/librocprofiler64.conf
ldconfig
fi
}
case "$1" in
( remove | upgrade )
rm_ldconfig
;;
( purge )
;;
( * )
exit 0
;;
esac
+20
View File
@@ -0,0 +1,20 @@
Copyright (c) 2018-2025 Advanced Micro Devices, Inc. All rights reserved.
[MITx11 License]
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 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.
+598
View File
@@ -0,0 +1,598 @@
# ROCm Profiling Tools
> [!IMPORTANT]
We are phasing out development and support for roctracer/rocprofiler/rocprof/rocprofv2 in favor of rocprofiler-sdk/rocprofv3 in upcoming ROCm releases. Going forward, only critical defect fixes will be addressed for older versions of profiling tools and libraries. We encourage all users to upgrade to the latest version, rocprofiler-sdk library and rocprofv3 tool, to ensure continued support and access to new features.
> [!NOTE]
> The published documentation is available at [ROCProfiler documentation](https://rocm.docs.amd.com/projects/rocprofiler/en/latest/index.html) in an organized, easy-to-read format, with search and a table of contents.
[![Build Status](
https://dev.azure.com/ROCm-CI/ROCm-CI/_apis/build/status%2Frocprofiler?repoName=ROCm%2Frocprofiler&branchName=amd-staging)](https://dev.azure.com/ROCm-CI/ROCm-CI/_build/latest?definitionId=143&repoName=ROCm%2Frocprofiler&branchName=amd-staging)
## Introduction
ROCProfiler is AMDs tooling infrastructure that provides a hardware specific low level performance analysis interface for the profiling and the tracing of GPU compute applications.
## ROCProfiler V1
Profiling with metrics and traces based on perfcounters (PMC) and traces (SPM).
Implementation is based on AqlProfile HSA extension.
The last API library version for ROCProfiler v1 is 8.0.0
The library source tree:
- doc - Documentation
- include/rocprofiler/rocprofiler.h - Library public API
- include/rocprofiler/v2/rocprofiler.h - V2 Beta Library public API
- include/rocprofiler/v2/rocprofiler_plugins.h - V2 Beta Tool's Plugins Library public API
- src - Library sources
- core - Library API sources
- util - Library utils sources
- xml - XML parser
- test - Library test suite
- ctrl - Test controll
- util - Test utils
- simple_convolution - Simple convolution test kernel
## Build environment
Roctracer & Rocprofiler need to be installed in the same directory.
```bash
export CMAKE_PREFIX_PATH=<path_to_hsa-runtime_includes>:<path_to_hsa-runtime_library>
export CMAKE_BUILD_TYPE=<debug|release> # release by default
export CMAKE_DEBUG_TRACE=1 # 1 to enable debug tracing
```
To build with the current installed ROCM:
```bash
cd .../rocprofiler
./build.sh ## (for clean build use `-cb`)
```
To run the test:
```bash
cd .../rocprofiler/build
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH # paths to ROC profiler and oher libraries
export HSA_TOOLS_LIB=librocprofiler64.so.1 # ROC profiler library loaded by HSA runtime
export ROCP_TOOL_LIB=test/librocprof-tool.so # tool library loaded by ROC profiler
export LIBPCI_TOOL_LIB=libpciaccess.so # PCI access library loaded by ROC profiler
export ROCP_METRICS=metrics.xml # ROC profiler metrics config file
export ROCP_INPUT=input.xml # input file for the tool library
export ROCP_OUTPUT_DIR=./ # output directory for the tool library, for metrics results file 'results.txt' and trace files
./<your_test>
```
Internal 'simple_convolution' test run script:
```bash
cd .../rocprofiler/build
./run.sh
```
- To enabled error messages logging to '/tmp/rocprofiler_log.txt':
```bash
export ROCPROFILER_LOG=1
```
- To enable verbose tracing:
```bash
export ROCPROFILER_TRACE=1
```
## Supported AMD GPU Architectures (V1)
The following AMD GPU architectures are supported with ROCprofiler V1:
- gfx8 (Fiji/Ellesmere)
- gfx900 (AMD Vega 10)
- gfx906 (AMD Vega 7nm also referred to as AMD Vega 20)
- gfx908 (AMD Instinct™ MI100 accelerator)
- gfx90a (AMD Instinct™ MI200)
- gfx94x (AMD Instinct™ MI300)
***
Note: ROCProfiler V1 tool usage documentation is available at [Click Here](doc/rocprof_tool.md)
***
## ROCProfiler V2
The first API library version for ROCProfiler v2 is 9.0.0
***
Note: ROCProfilerV2 is currently considered a beta version and is subject to change in future releases
***
### ROCProfilerV2 Modules
- Counters
- Hardware
- Generic Buffer
- Session
- Filter
- Tools
- Plugins
- Samples
- Tests
## Getting started
### Requirements
- rocm-llvm-dev
- makecache
- Gtest Development Package (Ubuntu: libgtest-dev)
- libelf-dev, libnuma-dev, libpciaccess-dev on ubuntu or their corresponding packages on any other OS
- Cppheaderparser, websockets, matplotlib, lxml, barectf Python3 Packages
- Python packages can be installed using:
```bash
pip3 install -r requirements.txt
```
### Build
The user has two options for building:
- Option 1 (It will install in the path saved in ROCM_PATH environment variable or /opt/rocm if ROCM_PATH is empty):
- Run
Normal Build
```bash
./build.sh --build OR ./build.sh -b
```
Clean Build
```bash
./build.sh --clean-build OR ./build.sh -cb
```
- Option 2 (Where ROCM_PATH environment need to be set with the current installation directory of rocm), run the following:
- Creating the build directory
```bash
mkdir build && cd build
```
- Configuring the rocprofv2 build
```bash
cmake -DCMAKE_PREFIX_PATH=$ROCM_PATH -DCMAKE_MODULE_PATH=$ROCM_PATH/hip/cmake -DROCPROFILER_BUILD_TESTS=1 -DROCPROFILER_BUILD_SAMPLES=1 <CMAKE_OPTIONS> ..
```
- Building the main runtime of the rocprofv2 project
```bash
cmake --build . -- -j
```
- Optionally, for building API documentation
```bash
cmake --build . -- -j doc
```
- Optionally, for building packages (DEB, RPM, TGZ)
Note: Requires rpm package on ubuntu
```bash
cmake --build . -- -j package
```
### Install
- Optionally, run the following to install
```bash
cd build
cmake --build . -- -j install
```
## Features & Usage
### rocsys
A command line utility to control a session (launch/start/stop/exit), with the required application to be traced or profiled in a rocprofv2 context. Usage:
- Launch the application with the required profiling and tracing options with giving a session identifier to be used later
```bash
rocsys --session session_name launch mpiexec -n 2 rocprofv2 -i samples/input.txt Histogram
```
- Start a session with a given identifier created at launch
```bash
rocsys --session session_name start
```
- Stop a session with a given identifier created at launch
```bash
rocsys session session_name stop
```
- Exit a session with a given identifier created at launch
```bash
rocsys session session_name exit
```
### ROCProf Versioning Support
Currently, rocprof can support both versions, rocprof and rocprofv2, that can be done using `--tool-version`
```bash
rocprof --tool-version <VERSION_REQUIRED> <rocprof/v2_options> <app_relative_path>
```
- `--tool-version 1` means it will just use rocprof V1.
- `--tool-version 2` means it will just use rocprofv2.
To know what version you are using right now, along with more information about the rocm version, use the following:
```bash
rocprof --version
```
### Counters and Metric Collection
HW counters and derived metrics can be collected using following option:
```bash
rocprofv2 -i samples/input.txt <app_relative_path>
```
input.txt content Example (Details of what is needed inside input.txt will be mentioned with every feature):
`pmc: SQ_WAVES GRBM_COUNT GRBM_GUI_ACTIVE SQ_INSTS_VALU`
By default, rocprofv2 runs counter-collection mode with kernel serialization enabled. With serialization enabled, there is a possibility of deadlock occurring if a program that is being profiled attempts to launch two kernels that rely on each other to progress (e.g. kernel A waits in a while-loop until kernel B modifies a location in memory, but, due to kernel serialization, kernel B will not be launched until kernel A finishes executing). When encountering this issue, kernel serialization can be disabled with the following option:
```bash
rocprofv2 -ns -i samples/input.txt <app_relative_path>
```
or
```bash
rocprofv2 --no-serialization -i samples/input.txt <app_relative_path>
```
Alternatively, an environment variable can be set as follows:
```bash
export ROCPROFILER_NO_SERIALIZATION = 1
```
### Application Trace Support
Different trace options are available while profiling an app:
- HIP API & asynchronous activity tracing
```bash
rocprofv2 --hip-api <app_relative_path> ## For synchronous HIP API Activity tracing
rocprofv2 --hip-activity <app_relative_path> ## For both Synchronous & ASynchronous HIP API Activity tracing
rocprofv2 --hip-trace <app_relative_path> ## Same as --hip-activity, added for backward compatibility
```
- HSA API & asynchronous activity tracing
```bash
rocprofv2 --hsa-api <app_relative_path> ## For synchronous HSA API Activity tracing
rocprofv2 --hsa-activity <app_relative_path> ## For both Synchronous & ASynchronous HSA API Activity tracing
rocprofv2 --hsa-trace <app_relative_path> ## Same as --hsa-activity, added for backward compatibility
```
- Kernel dispatches tracing
```bash
rocprofv2 --kernel-trace <app_relative_path> ## Kernel Dispatch Tracing
```
- HIP & HSA API and asynchronous activity and kernel dispatches tracing
```bash
rocprofv2 --sys-trace <app_relative_path> ## Same as combining --hip-trace & --hsa-trace & ROCtx trace
```
- For complete usage options, please run rocprofv2 help
```bash
rocprofv2 --help
```
### Plugin Support
We have a template for adding new plugins. New plugins can be written on top of rocprofv2 to support the desired output format using include/rocprofiler/v2/rocprofiler_plugins.h header file. These plugins are modular in nature and can easily be decoupled from the code based on need. Installation files:
```string
rocprofiler-plugins_2.0.0-local_amd64.deb
rocprofiler-plugins-2.0.0-local.x86_64.rpm
```
Plugins may have multiple versions, the user can specify which version of the plugin to use by running the following command:
```bash
rocprofv2 --plugin <plugin_name> --plugin-version <plugin_version_required> <rocprofv2_options> <app_relative_path>
```
- File plugin: outputs the data in txt files. File plugin have two versions, by default version 2 is the current default.
Usage:
```bash
rocprofv2 --plugin file -i samples/input.txt -d output_dir <app_relative_path> # -d is optional, but can be used to define the directory output for output results
```
File plugin version 1 output header will be similar to the legacy rocprof v1 output:
```text
Index,KernelName,gpu-id,queue-id,queue-index,pid,tid,grd,wgr,lds,scr,arch_vgpr,accum_vgpr,sgpr,wave_size,sig,obj,DispatchNs,BeginNs,EndNs,CompleteNs,Counters
```
File plugin version 2 output header:
```text
Dispatch_ID,GPU_ID,Queue_ID,PID,TID,Grid_Size,Workgroup_Size,LDS_Per_Workgroup,Scratch_Per_Workitem,Arch_VGPR,Accum_VGPR,SGPR,Wave_Size,Kernel_Name,Start_Timestamp,End_Timestamp,Correlation_ID,Counters
```
- Perfetto plugin: outputs the data in protobuf format. Protobuf files can be viewed using ui.perfetto.dev or using trace_processor.
Usage:
```bash
rocprofv2 --plugin perfetto --hsa-trace -d output_dir <app_relative_path> # -d is optional, but can be used to define the directory output for output results
```
- CTF plugin: Outputs the data in ctf format(a binary trace format). CTF binary output can be viewed using TraceCompass or babeltrace.
Usage:
```bash
rocprofv2 --plugin ctf --hip-trace -d output_dir <app_relative_path> # -d is optional, but can be used to define the directory output for output results
```
- JSON plugin: Outputs `.json` file, the JSON file matches Google Trace Format, so it should be easily loaded to perfetto, chrome tracing or speedscope. For Speedscope, `--disable-json-data-flows` option will be needed as speedscope doesn't work with data flows.
Usage:
```bash
rocprofv2 --plugin json --hip-trace -d output_dir <app_relative_path>
```
### Flush Interval
Flush interval can be used to control the interval time in milliseconds between the buffers flush for the tool. However, if the buffers are full the flush will be called on its own. This can be used as in the next example:
```bash
rocprofv2 --flush-interval <TIME_INTERVAL_IN_MILLISECONDS> <rest_of_rocprofv2_arguments> <app_relative_path>
```
### Trace Period
Trace period can be used to control when the profiling or tracing is enabled using two arguments, the first one is the delay time, which is the time spent idle without tracing or profiling. The second argument is the profiling or the tracing time, which is the active time where the profiling and tracing are working, so basically, the session will work in the following timeline:
```string
<DELAY_TIME> => <PROFILING_OR_TRACING_SESSION_START> => <ACTIVE_PROFILING_OR_TRACING_TIME> => <PROFILING_OR_TRACING_SESSION_STOP>
```
This feature can be used using the following command:
```bash
rocprofv2 --trace-period <delay>:<active_time>:<interval> <rest_of_rocprofv2_arguments> <app_relative_path>
```
- delay: Time delay to start profiling (ms).
- active_time: How long to profile for (ms).
- interval: If set, profiling sessions will start (loop) every "interval", and run for "active_time", until the application ends. Must be higher than "active_time".
### Device Profiling
A device profiling session allows the user to profile the GPU device for counters irrespective of the running applications on the GPU. This is different from application profiling. device profiling session doesn't care about the host running processes and threads. It directly provides low level profiling information.
### Session Support
A session is a unique identifier for a profiling/tracing/pc-sampling task. A ROCProfilerV2 Session has enough information about what needs to be collected or traced and it allows the user to start/stop profiling/tracing whenever required. More details on the API can be found in the API specification documentation that can be installed using rocprofiler-doc package. Samples also can be found for how to use the API in samples directory.
### Kernel Filtration
rocprofv2 supports the ``kernel`` attribute in input files for only ``csv`` output format.
To enable kernel filtration with input file, the row beginning with the ``kernel:`` keyword should specify the names of kernels to be profiled.Filtering can also be enabled by the flag ``ROCPROFILER_KERNEL_FILTER``.
For a sample ``vecCopy`` kernel:
```CPP
__global__ void vecCopy(double *a, double *b, double *c, int n, int stride) {
// Get our global thread ID
int id = blockIdx.x*blockDim.x+threadIdx.x;
if (id < n)
c[id] = a[id];
}
```
To filter vecCopy kernel:
```bash
$ cat input.txt
pmc: SQ_WAVES
kernel: vecCopy
```
```bash
rocprofv2 -i input.txt -d out <application_path>
```
## Tests
We make use of the GoogleTest (Gtest) framework to automatically find and add test cases to the CMAKE testing environment. ROCProfilerV2 testing is categorized as following:
- unittests (Gtest Based) : These includes tests for core classes. Any newly added functionality should have a unit test written to it.
- featuretests (standalone and Gtest Based): These includes both API tests and tool tests. Tool is tested against different applications to make sure we have right output in every run.
- memorytests (standalone): This includes running address sanitizer for memory leaks, corruptions.
installation:
rocprofiler-tests_9.0.0-local_amd64.deb
rocprofiler-tests-9.0.0-local.x86_64.rpm
### List and Run tests
#### Run unit tests on the commandline
```bash
./build/tests/unittests/runUnitTests
```
#### Run profilerfeaturetests on the commandline
```bash
./build/tests/featuretests/profiler/runFeatureTests
```
#### Run tracer featuretests on the commandline
```bash
./build/tests/featuretests/tracer/runTracerFeatureTests
```
#### Run all tests
```bash
rocprofv2 -t
```
OR
```bash
ctest
```
### Guidelines for adding new tests
- Prefer to enhance an existing test as opposed to writing a new one. Tests have overhead to start and many small tests spend precious test time on startup and initialization issues.
- Make the test run standalone without requirement for command-line arguments. This makes it easier to debug since the name of the test is shown in the test report and if you know the name of the test you can the run the test.
## Logging
To enable error messages logging to '/tmp/rocprofiler_log.txt':
```bash
export ROCPROFILER_LOG=1
```
## Kernel Name Truncation
By default kernel names are not truncated. To enable truncation for readability:
```
export ROCPROFILER_TRUNCATE_KERNEL_PATH=1
```
## Documentation
We make use of doxygen to automatically generate API documentation. Generated document can be found in the following path:
ROCM_PATH by default is /opt/rocm
It can be set by the user in different location if needed.
<ROCM_PATH>/share/doc/rocprofv2
installation:
```string
rocprofiler-docs_9.0.0-local_amd64.deb
rocprofiler-docs-9.0.0-local.x86_64.rpm
```
## Samples
- Profiling: Profiling Samples depending on replay mode
- Tracing: Tracing Samples
installation:
```string
rocprofiler-samples_9.0.0-local_amd64.deb
rocprofiler-samples-9.0.0-local.x86_64.rpm
```
usage:
samples can be run as independent executables once installed
## Project Structure
- bin: ROCProf scripts along with V1 post processing scripts
- doc: Documentation settings for doxygen, V1 API Specifications pdf document.
- include:
- rocprofiler.h: V1 API Header File
- v2:
- rocprofiler.h: V2 API Header File
- rocprofiler_plugin.h: V2 Tool Plugins API
- plugin
- file: File Plugin
- perfetto: Perfetto Plugin
- ctf: CTF Plugin
- samples: Samples of how to use the API, and also input.txt input file samples for counter collection.
- script: Scripts needed for tracing
- src: Source files of the project
- api: API implementation for rocprofv2
- core: Core source files needed for the V1/V2 API
- counters: Basic and Derived Counters
- hardware: Hardware support
- hsa: Provides support for profiler and tracer to communicate with HSA
- queues: Intercepting HSA Queues
- packets: Packets Preparation for profiling
- memory: Memory Pool used in buffers that saves the output data
- session: Session Logic
- filter: Type of profiling or tracing and its properties
- tracer: Tracing support of the session
- profiler: Profiling support of the session
- tools: Tools needed to run profiling and tracing
- rocsys: Controlling Session from another CLI
- utils: Utilities needed by the project
- tests: Tests folder
- CMakeLists.txt: Handles cmake list for the whole project
- build.sh: To easily build and compile rocprofiler
- CHANGELOG.md: Changes that are happening per release
## Support
Please report in the Github Issues.
## Limitations
- Navi3x requires a stable power state for counter collection.
Currently, this state needs to be set by the user.
To do so, set "power_dpm_force_performance_level" to be writeable for non-root users, then set performance level to profile_standard:
```bash
sudo chmod 777 /sys/class/drm/card0/device/power_dpm_force_performance_level
echo profile_standard >> /sys/class/drm/card0/device/power_dpm_force_performance_level
```
Recommended: "profile_standard" for counter collection and "auto" for all other profiling. Use rocm-smi to verify the current power state. For multiGPU systems (includes integrated graphics), replace "card0" by the desired card.
- Timestamps may be incorrect with HIP_OPS when the system has been in sleep state.
- HIP_OPS are mutually exclusive with HSA_OPS.
- JSON Plugin as of now doesn't have an automated way to merge for multiple processes, a file will be generated per process (rank).
## Supported AMD GPU Architectures (V2)
The following AMD GPU architectures are supported with ROCprofiler V2:
- gfx900 (AMD Vega 10)
- gfx906 (AMD Vega 7nm also referred to as AMD Vega 20)
- gfx908 (AMD Instinct™ MI100 accelerator)
- gfx90a (AMD Instinct™ MI200)
- gfx94x (AMD Instinct™ MI300)
- gfx10xx ([Navi2x] AMD Radeon(TM) Graphics)
- gfx11xx ([Navi3x] AMD Radeon(TM) Graphics)
> [!WARNING]
> The latest mainline version of AQLprofile can be found at [https://repo.radeon.com/rocm/misc/aqlprofile/](https://repo.radeon.com/rocm/misc/aqlprofile/). However, it's important to note that updates to the public AQLProfile may not occur as frequently as updates to the rocprofiler. This discrepancy could lead to a potential mismatch between the AQLprofile binary and the rocprofiler source.
+5
View File
@@ -0,0 +1,5 @@
# left-hand term originates from ENABLE_LDCONFIG = ON/OFF at package build
if [ "@ENABLE_LDCONFIG@" == "ON" ]; then
echo @CPACK_PACKAGING_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@ > /@CMAKE_INSTALL_SYSCONFDIR@/ld.so.conf.d/librocprofiler64.conf
ldconfig
fi
+4
View File
@@ -0,0 +1,4 @@
# left-hand term originates from ENABLE_LDCONFIG = ON/OFF at package build
if [ "@ENABLE_LDCONFIG@" == "ON" ]; then
rm -f /@CMAKE_INSTALL_SYSCONFDIR@/ld.so.conf.d/librocprofiler64.conf && ldconfig
fi
+84
View File
@@ -0,0 +1,84 @@
#!/bin/sh -x
SO_EXT="hsaco"
TEST_NAME=$1
DST_DIR=$2
ROCM_DIR=$3
TGT_LIST=$4
if [ -z "$TEST_NAME" ] ; then
echo "Usage: $0 <test name> <dst dir>"
echo " Will look for <test name>.cl and will build <test name>.$SO_EXT dynamic code object library"
exit 1
fi
OBJ_NAME=$(echo "_$(basename $TEST_NAME)" | sed -e 's/_./\U&\E/g' -e 's/_//g')
if [ -z "$DST_DIR" ] ; then
DST_DIR=$(dirname TEST_NAME)
fi
if [ -z "$ROCM_DIR" ] ; then
ROCM_DIR=/opt/rocm
fi
if [ -z "$TGT_LIST" ] ; then
TGT_LIST=`$ROCM_DIR/bin/rocminfo | grep "amdgcn-amd-amdhsa--" | head -n 1 | sed -n "s/^.*amdgcn-amd-amdhsa--\(\w*\).*$/\1/p"`
fi
if [ -z "$TGT_LIST" ] ; then
echo "Error: GPU targets not found"
exit 1
fi
OCL_VER="2.0"
if [ -e $ROCM_DIR/llvm ] ; then
LLVM_DIR=$ROCM_DIR/llvm
LIB_DIR=$ROCM_DIR/lib
else
LLVM_DIR=$ROCM_DIR/hcc
LIB_DIR=$LLVM_DIR/lib
fi
# Determine whether using new or old device-libs layout
if [ -e $LIB_DIR/bitcode/opencl.amdgcn.bc ]; then
BC_DIR=$LIB_DIR/bitcode
elif [ -e $LIB_DIR/opencl.amdgcn.bc ]; then
BC_DIR=$LIB_DIR
elif [ -e $ROCM_DIR/amdgcn/bitcode/opencl.bc ]; then
BC_DIR=$ROCM_DIR/amdgcn/bitcode
else
echo "Error: Cannot find amdgcn bitcode directory"
exit 1
fi
CLANG_ROOT=$LLVM_DIR/lib/clang
CLANG_DIR=`ls -d $CLANG_ROOT/* | head -n 1`
if [ "$CLANG_DIR" = "" ] ; then
echo "Error: LLVM clang library was not found"
exit 1
fi
BIN_DIR=$LLVM_DIR/bin
INC_DIR=$CLANG_DIR/include
if [ -e $BC_DIR/opencl.amdgcn.bc ]; then
BITCODE_OPTS="-nogpulib \
-Xclang -mlink-bitcode-file -Xclang $BC_DIR/opencl.amdgcn.bc \
-Xclang -mlink-bitcode-file -Xclang $BC_DIR/ockl.amdgcn.bc \
-Xclang -mlink-bitcode-file -Xclang $BC_DIR/ocml.amdgcn.bc"
else
BITCODE_OPTS="--hip-device-lib-path=$BC_DIR"
fi
for GFXIP in $TGT_LIST ; do
OBJ_PREF=$GFXIP
OBJ_FILE="${OBJ_PREF}_${OBJ_NAME}.$SO_EXT"
# Kernels are currently compiled with code object version 4. In order to
# switch to code object version 5, kernel argument setup in
# rocprofiler/test/simple_convolution/simple_convolution needs to be updated
# according to https://llvm.org/docs/AMDGPUUsage.html#amdgpu-amdhsa-code-object-kernel-argument-metadata-map-table-v5
$BIN_DIR/clang -cl-std=CL$OCL_VER -include $INC_DIR/opencl-c.h $BITCODE_OPTS -target amdgcn-amd-amdhsa -mcpu=$GFXIP -mcode-object-version=4 $TEST_NAME.cl -o $DST_DIR/$OBJ_FILE
echo "'$OBJ_FILE' generated"
done
exit 0
+107
View File
@@ -0,0 +1,107 @@
################################################################################
# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
#
# 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 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.
################################################################################
import os
from sqlitedb import SQLiteDB
def gen_message(outfile):
if outfile != "":
print("File '" + outfile + "' is generating")
def post_process_data(db, table_name, outfile=""):
# db.add_data_column('A', 'DispDurNs', 'INTEGER', 'BeginNs - DispatchNs')
# db.add_data_column('A', 'ComplDurNs', 'INTEGER', 'CompleteNs - EndNs')
# db.add_data_column('A', 'TotalDurNs', 'INTEGER', 'CompleteNs - DispatchNs')
# db.add_data_column(table_name, 'TimeNs', 'INTEGER', 'BeginNs - %d' % start_ns)
db.add_data_column(table_name, "DurationNs", "INTEGER", "EndNs - BeginNs")
if outfile != "":
db.dump_csv(table_name, outfile)
gen_message(outfile)
def gen_data_bins(db, outfile):
db.execute(
"create view C as select Name, Calls, TotalDurationNs, TotalDurationNs/Calls as AverageNs, TotalDurationNs*100.0/(select sum(TotalDurationNs) from %s) as Percentage from %s order by TotalDurationNs desc;"
% ("B", "B")
)
db.dump_csv("C", outfile)
db.execute("DROP VIEW C")
def gen_table_bins(db, table, outfile, name_var, dur_ns_var):
db.execute(
"create view B as select (%s) as Name, count(%s) as Calls, sum(%s) as TotalDurationNs from %s group by %s"
% (name_var, name_var, dur_ns_var, table, name_var)
)
gen_data_bins(db, outfile)
db.execute("DROP VIEW B")
gen_message(outfile)
def gen_api_json_trace(db, table, start_ns, outfile):
db.execute(
'create view B as select "Index", Name as name, __section as pid, __lane as tid, ((BeginNs - %d)/1000) as ts, (DurationNs/1000) as dur from %s;'
% (start_ns, table)
)
db.dump_json("B", table, outfile)
db.execute("DROP VIEW B")
gen_message(outfile)
def gen_ext_json_trace(db, table, start_ns, outfile):
db.execute(
"create view B as select Name as name, __section as pid, __lane as tid, ((BeginNs - %d)/1000) as ts, ((EndNs - BeginNs)/1000) as dur from %s;"
% (start_ns, table)
)
db.dump_json("B", table, outfile)
db.execute("DROP VIEW B")
gen_message(outfile)
def gen_ops_json_trace(db, table, base_pid, start_ns, outfile):
db.execute(
'create view B as select "Index", "%s" as name, ("dev-id" + %d) as pid, __lane as tid, ((BeginNs - %d)/1000) as ts, (DurationNs/1000) as dur from %s;'
% (
"roctx-range" if "ROCP_RENAME_KERNEL" in os.environ else "Name",
base_pid,
start_ns,
table,
)
)
db.dump_json("B", table, outfile)
db.execute("DROP VIEW B")
gen_message(outfile)
def gen_kernel_json_trace(db, table, base_pid, start_ns, outfile):
db.execute(
'create view B as select "Index", KernelName as name, ("gpu-id" + %d) as pid, tid, ((BeginNs - %d)/1000) as ts, (DurationNs/1000) as dur from %s;'
% (base_pid, start_ns, table)
)
db.dump_json("B", table, outfile)
db.execute("DROP VIEW B")
gen_message(outfile)
##############################################################################################
+485
View File
@@ -0,0 +1,485 @@
################################################################################
# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
#
# 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 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.
################################################################################
import sys, os, re
from sqlitedb import SQLiteDB
pinned = ["hipMallocHost", "hipHostMalloc", "hipHostAlloc"]
ondevice = [
"hipMalloc",
"hipMallocPitch",
"hipMallocArray",
"hipMalloc3DArray",
"hsa_amd_memory_pool_allocate",
]
mm_table_descr = [
[
"BeginNs",
"EndNs",
"pid",
"tid",
"Name",
"Direction",
"SrcType",
"DstType",
"Size",
"Async",
],
{
"BeginNs": "INTEGER",
"EndNs": "INTEGER",
"pid": "INTEGER",
"tid": "INTEGER",
"Name": "TEXT",
"Direction": "TEXT",
"SrcType": "TEXT",
"DstType": "TEXT",
"Size": "INTEGER",
"Async": "TEXT",
},
]
def fatal(msg):
sys.stderr.write(sys.argv[0] + ": " + msg + "\n")
sys.exit(1)
DELIM = ","
# Mem copy manager class
class MemManager:
def __init__(self, db, indir):
self.db = db
self.allocations = {}
self.hsa_agent_types = {}
self.memcopies = {}
self.filename = ""
self.fd = 0
self.parse_hsa_handles(indir + "/" + "hsa_handles.txt")
def __del__(self):
if self.fd != 0:
self.fd.close()
# Parsing the mapping of HSA agent and memory pool handles
def parse_hsa_handles(self, infile):
if os.path.exists(infile):
inp = open(infile, "r")
cpu_agent_ptrn = re.compile(r"(0x[0-9a-fA-F]+) agent cpu")
gpu_agent_ptrn = re.compile(r"(0x[0-9a-fA-F]+) agent gpu")
for line in inp.readlines():
m_cpu = cpu_agent_ptrn.match(line)
if m_cpu:
self.hsa_agent_types[str(int(m_cpu.group(1), 16))] = 0 # "cpu"
m_gpu = gpu_agent_ptrn.match(line)
if m_gpu:
self.hsa_agent_types[str(int(m_gpu.group(1), 16))] = 1 # "gpu"
inp.close()
# register alloc and memcpy API calls
# ['BeginNs', 'EndNs', 'pid', 'tid', 'Name', 'args', 'Index', 'Data'],
def register_api(self, rec_vals):
res = ""
record_name = rec_vals[4] # 'Name'
record_args = rec_vals[5] # 'args'
malloc_ptrn = re.compile(r"hip.*Malloc|hsa_amd_memory_pool_allocate")
mcopy_ptrn = re.compile(r"hipMemcpy|hsa_amd_memory_async_copy")
if malloc_ptrn.match(record_name):
self.add_allocation(record_name, record_args)
elif mcopy_ptrn.match(record_name):
res = self.add_memcpy(rec_vals)
return res
# register memcpy asynchronous copy
# ['BeginNs', 'EndNs', 'Name', 'pid', 'tid', 'Index', ...
def register_copy(self, rec_vals):
data = ""
event = rec_vals[2] # 'Name'
procid = rec_vals[3] # 'pid'
recordid = rec_vals[5] # 'Index'
# query syncronous memcopy API record
key = (recordid, procid, 0)
if key in self.memcopies:
data = self.memcopies[key]
# query asyncronous memcopy API record
key = (recordid, procid, 1)
if key in self.memcopies:
if data != "":
fatal("register_copy: corrupted record sync/async")
async_copy_start_time = rec_vals[0]
async_copy_end_time = rec_vals[1]
tid = rec_vals[4]
copy_line_header = (
str(async_copy_start_time)
+ DELIM
+ str(async_copy_end_time)
+ DELIM
+ str(procid)
+ DELIM
+ str(tid)
)
copy_line_footer = "Async=" + str(1)
data = copy_line_header + self.memcopies[key] + copy_line_footer
self.memcopies[key] = data
return data
# register memcpy asynchronous activity
# rec_vals: ['BeginNs', 'EndNs', 'dev-id', 'queue-id', 'Name', 'pid', 'tid', 'Index', 'Data', ...
def register_activity(self, rec_vals):
data = ""
procid = rec_vals[5] # 'pid'
recordid = rec_vals[7] # 'Index'
# query syncronous memcopy API record
key = (recordid, procid, 0)
if key in self.memcopies:
data = self.memcopies[key]
# query asyncronous memcopy API record
key = (recordid, procid, 1)
if key in self.memcopies:
if data != "":
fatal("register_activity: corrupted record sync/async")
async_copy_start_time = rec_vals[0]
async_copy_end_time = rec_vals[1]
tid = rec_vals[6]
copy_line_header = (
str(async_copy_start_time)
+ DELIM
+ str(async_copy_end_time)
+ DELIM
+ str(procid)
+ DELIM
+ str(tid)
)
copy_line_footer = "Async=" + str(1)
data = copy_line_header + self.memcopies[key] + copy_line_footer
self.memcopies[key] = data
return data
# add allocation to map
def add_allocation(self, event, args):
choice = 0
if event == "hipMallocPitch":
malloc_args_ptrn = re.compile(r"\(ptr\((.*)\) width\((.*)\) height\((.*)\)\)")
choice = 1
elif event == "hipMallocArray":
malloc_args_ptrn = re.compile(
r"\(array\((.*)\) width\((.*)\) height\((.*)\)\)"
)
choice = 1
elif event == "hipMalloc3DArray":
malloc_args_ptrn = re.compile(
r"\(array\((.*)\) width\((.*)\) height\((.*)\) depth\((.*)\)\)"
)
choice = 2
elif event == "hsa_amd_memory_pool_allocate":
# ({handle=25291264}, 40, 0, 0x7ffc4c7bf1b0)
malloc_args_ptrn = re.compile(
r"\({handle=\d+}, (\d+), \d+, (0x[0-9a-fA-F]+)\)"
)
choice = 4
else:
# (ptr(0x7f3407000000) size(800000000) flags(0))
malloc_args_ptrn = re.compile(r"\(ptr\((.*)\) size\((.*)\) .*\)")
choice = 3
m = malloc_args_ptrn.match(args)
if m:
if choice == 4:
ptr = int(m.group(2), 16)
size = int(m.group(1))
elif choice == 3:
ptr = int(m.group(1), 16)
size = int(m.group(2))
elif choice == 1:
ptr = int(m.group(1), 16)
size = int(m.group(2)) * int(m.group(3))
else:
ptr = int(m.group(1), 16)
size = int(m.group(2)) * int(m.group(3)) * int(m.group(4))
self.allocations[ptr] = (size, event)
# get type of ptr
def get_ptr_type(self, ptr):
addr = int(ptr, 16)
addr_type = "unknown"
found = 0
for base, (size, event) in self.allocations.items():
if addr >= base and addr < base + size:
found = 1
break
if not found:
addr_type = "pageable"
elif event in pinned:
addr_type = "pinned"
elif event in ondevice:
addr_type = "device"
elif ptr in self.hsa_agent_types:
if self.hsa_agent_types[ptr] == 0:
addr_type = "pinned"
elif self.hsa_agent_types[ptr] == 1:
addr_type = "device"
else:
fatal("internal error: ptr(" + ptr + ") cannot be identified")
else:
fatal("internal error: ptr(" + ptr + ") cannot be identified")
return addr_type
# add memcpy to map
def add_memcpy(self, recvals):
recordid = recvals[6] # same as corrid
event = recvals[4]
start_time = recvals[0] # sync time stamp
end_time = recvals[1] # sync time stamp
args = recvals[5]
procid = int(recvals[2]) # used to query async entries
pid = recvals[2]
tid = recvals[3]
# hipMemcpy(void* dst, const void* src, size_t sizeBytes, hipMemcpyKind kind)
hip_memcpy_ptrn = re.compile(
r"\(\s*dst\((.*)\) src\((.*)\) sizeBytes\((\d+)\).*\)"
)
# hipMemcpy2D(void* dst, size_t dpitch, const void* src, size_t spitch, size_t width,
# size_t height, hipMemcpyKind kind);
hip_memcpy_ptrn2 = re.compile(
r"\(\s*dst\((.*)\) .* src\((.*)\) .* width\((\d+)\) height\((\d+)\).*\)"
)
# hipMemcpyToArray(hipArray* dst, size_t wOffset, size_t hOffset, const void* src,
# size_t count, hipMemcpyKind kind);
hip_memcpy_ptrn3 = re.compile(
r"\(\s*dst\((.*)\) .* src\((.*)\) count\((\d+)\).*\)"
)
# hipMemcpyToSymbol(const void* symbolName, const void* src, size_t sizeBytes,
# size_t offset = 0, hipMemcpyKind kind)
hip_memcpy_ptrn4 = re.compile(
r"\(\s*symbol\((.*)\) src\((.*)\) sizeBytes\((\d+)\).*\)"
)
# memcopy with kind argument
hip_memcpy_ptrn_kind = re.compile(r".* kind\((\d+)\)\s*.*")
# hsa_amd_memory_async_copy(void* dst, hsa_agent_t dst_agent, const void* src,
# hsa_agent_t src_agent, size_t size,
# uint32_t num_dep_signals,
# const hsa_signal_t* dep_signals,
# hsa_signal_t completion_signal);
# "(0x7f8ab6600000, 27064880, 0x7f8b16000000, 27059968, 800000000, 0, 0, 140240759809536) = 0"
# hsa_memcpy_ptrn_prev used to support format transition and will be cleaned up later.
hsa_memcpy_ptrn_prev = re.compile(
r"\((0x[0-9a-fA-F]+), (\d+), (0x[0-9a-fA-F]+), (\d+), (\d+), .*\) = \d"
)
# "(0x7fd83bc00000, {handle=16124864}, 0x7fd89b600000, {handle=16119808}, 800000000, 0, 0, {handle=140573877724672}) = 0"
hsa_memcpy_ptrn = re.compile(
r"\((0x[0-9a-fA-F]+), {handle=(\d+)}, (0x[0-9a-fA-F]+), {handle=(\d+)}, (\d+), .*\) = \d"
)
# "(0x7f9125cfe7b0, 0x7f9125cfe784, 0x7f9125cfe790, 0x7f9125cfe784, 0x7f9125cfe778, {handle=94324038652880}, 1, 0, 0, {handle=140261380710784}) = 0"
# dst, dst_offset, src, src_offset, range, copy_agent, dir, num_dep_signals, dep_signals, completion_signal
hsa_memcpy_ptrn2 = re.compile(
r"\((0x[0-9a-fA-F]+), 0x[0-9a-fA-F]+, (0x[0-9a-fA-F]+), 0x[0-9a-fA-F]+, 0x[0-9a-fA-F]+, {z=(\d+), y=(\d+), x=(\d+)}, {handle=(\d+)}, .*\) = \d"
)
# aysnc memcopy
async_event_ptrn = re.compile(r"Async|async")
m_basic_hip = hip_memcpy_ptrn.match(args)
m_basic_hsa3 = hip_memcpy_ptrn4.match(args)
m_basic_hsa_prev = hsa_memcpy_ptrn_prev.match(args)
m_basic_hsa = hsa_memcpy_ptrn.match(args)
m_basic_hsa2 = hsa_memcpy_ptrn2.match(args)
is_hip = True if not (m_basic_hsa_prev or m_basic_hsa or m_basic_hsa2) else False
m_2d = hip_memcpy_ptrn2.match(args)
m_array = hip_memcpy_ptrn3.match(args)
is_async = 1 if async_event_ptrn.search(event) else 0
copy_line = ""
size = 0
dstptr_type = "unknown"
srcptr_type = "unknown"
direction = "unknown"
kind_switcher = {
"0": "HtoH",
"1": "HtoD",
"2": "DtoH",
"3": "DtoD",
"4": "auto",
}
condition_matched = False
if m_basic_hip:
dstptr = m_basic_hip.group(1)
dstptr_type = self.get_ptr_type(dstptr)
srcptr = m_basic_hip.group(2)
srcptr_type = self.get_ptr_type(srcptr)
size = int(m_basic_hip.group(3))
condition_matched = True
if m_basic_hsa_prev:
dstptr = m_basic_hsa_prev.group(1)
dst_agent_ptr = m_basic_hsa_prev.group(2)
dstptr_type = self.get_ptr_type(dst_agent_ptr)
srcptr = m_basic_hsa_prev.group(3)
src_agent_ptr = m_basic_hsa_prev.group(4)
srcptr_type = self.get_ptr_type(src_agent_ptr)
size = int(m_basic_hsa_prev.group(5))
condition_matched = True
if m_basic_hsa:
dstptr = m_basic_hsa.group(1)
dst_agent_ptr = m_basic_hsa.group(2)
dstptr_type = self.get_ptr_type(dst_agent_ptr)
srcptr = m_basic_hsa.group(3)
src_agent_ptr = m_basic_hsa.group(4)
srcptr_type = self.get_ptr_type(src_agent_ptr)
size = int(m_basic_hsa.group(5))
condition_matched = True
if m_basic_hsa2:
dstptr = m_basic_hsa2.group(1)
dst_agent_ptr = m_basic_hsa2.group(6)
dstptr_type = self.get_ptr_type(dst_agent_ptr)
srcptr = m_basic_hsa2.group(2)
src_agent_ptr = m_basic_hsa2.group(6)
srcptr_type = self.get_ptr_type(src_agent_ptr)
z = int(m_basic_hsa2.group(3))
y = int(m_basic_hsa2.group(4))
x = int(m_basic_hsa2.group(5))
size = x * y * z
condition_matched = True
if m_basic_hsa3:
dstptr = m_basic_hsa3.group(1)
dstptr_type = self.get_ptr_type(dstptr)
srcptr = m_basic_hsa3.group(2)
srcptr_type = self.get_ptr_type(srcptr)
size = int(m_basic_hsa3.group(3))
condition_matched = True
if m_array:
dstptr = m_array.group(1)
dstptr_type = self.get_ptr_type(dstptr)
srcptr = m_array.group(2)
srcptr_type = self.get_ptr_type(srcptr)
size = m_array.group(3)
condition_matched = True
if m_2d:
dstptr = m_2d.group(1)
dstptr_type = self.get_ptr_type(dstptr)
srcptr = m_2d.group(2)
srcptr_type = self.get_ptr_type(srcptr)
size = int(m_2d.group(3)) * int(m_2d.group(4))
condition_matched = True
if not condition_matched:
fatal('Memcpy args "' + args + '" cannot be identified')
if not is_async:
start_time = recvals[0] # sync time stamp
end_time = recvals[1] # sync time stamp
duration = int(end_time) - int(start_time)
evt_switcher = {
"hipMemcpyDtoD": "DtoD",
"hipMemcpyDtoDAsync": "DtoD",
"hipMemcpyDtoH": "DtoH",
"hipMemcpyDtoHAsync": "DtoH",
"hipMemcpyHtoD": "HtoD",
"hipMemcpyHtoDAsync": "HtoD",
}
if is_hip:
m = hip_memcpy_ptrn_kind.match(args)
if m:
direction = kind_switcher.get(m.group(1), "unknown")
else:
direction = evt_switcher.get(event, "unknown")
else:
if (
dst_agent_ptr in self.hsa_agent_types
and src_agent_ptr in self.hsa_agent_types
):
if self.hsa_agent_types[src_agent_ptr] == 1:
direction = "D"
elif self.hsa_agent_types[src_agent_ptr] == 0:
direction = "H"
if direction != "unknown":
direction += "to"
if self.hsa_agent_types[dst_agent_ptr] == 1:
direction += "D"
elif self.hsa_agent_types[dst_agent_ptr] == 0:
direction += "H"
copy_line_header = ""
copy_line_footer = ""
copy_line_header = (
str(start_time) + DELIM + str(end_time) + DELIM + str(pid) + DELIM + str(tid)
)
copy_line_footer = "Async=" + str(is_async)
copy_line = (
copy_line_header
+ DELIM
+ event
+ DELIM
+ "Direction="
+ direction
+ DELIM
+ "SrcType="
+ srcptr_type
+ DELIM
+ "DstType="
+ dstptr_type
+ DELIM
+ "Size="
+ str(size)
+ DELIM
+ copy_line_footer
)
self.memcopies[(recordid, procid, is_async)] = copy_line
return copy_line
def dump_data(self, table_name, file_name):
# To create memcopy info table in DB
print("File '" + file_name + "' is generating")
table_handle = self.db.add_table(table_name, mm_table_descr)
fld_ptrn = re.compile(r"(.*)=(.*)")
for key, record in self.memcopies.items():
rec_vals_array = []
for rec in record.split(DELIM):
fld_ptrnm = fld_ptrn.match(rec)
if fld_ptrnm:
rec_vals_array.append(fld_ptrnm.group(2))
else:
rec_vals_array.append(rec)
self.db.insert_entry(table_handle, rec_vals_array)
# To dump the memcopy info table as CSV
self.db.dump_csv(table_name, file_name)
+102
View File
@@ -0,0 +1,102 @@
#!/bin/bash
################################################################################
# Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved.
#
# 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 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.
################################################################################
#A script to merge rocprof traces and then provide a results.json for the aggregate numbers.
BIN_DIR=$(dirname $(realpath ${BASH_SOURCE[0]}))
bin_name=`basename $0`
# usage method
usage() {
echo "Script for aggregating results from multiple rocprofiler out directries."
echo "Full path: $BIN_DIR/$bin_name"
echo "Usage: if running independently"
echo " $bin_name -o <outputdir> [<inputdir>...]"
echo ""
echo "Usage: if running with rocprof"
echo " rocprof --merge-traces -o <outputdir> [<inputdir>...]"
echo ""
echo "Options:"
echo " -o <outputdir> - output directory where the results will be aggregated."
echo " <inputdir>... - space separated list of rocprofiler directories. If not specified, CWD is used."
echo ""
exit 1
}
# read arguments
INPUT_DIRS=()
while getopts "o:h" opt; do
case $opt in
o) OUTPUT_DIR=$OPTARG ;;
h) usage ;;
\?) usage ;;
esac
done
shift $((OPTIND-1))
INPUT_DIRS=$@
if [ "${OUTPUT_DIR}" = "" ] ; then
echo "Missing output dir option"
usage
fi
for INPUT_DIR in ${INPUT_DIRS} ; do
if [[ ! -d "${INPUT_DIR}" ]] ; then
echo "Directory ${INPUT_DIR} does not exist."
exit 1
fi
done
if ! [ -d "${OUTPUT_DIR}" ] ; then
mkdir -p "${OUTPUT_DIR}"
fi
echo "Processing directories: $INPUT_DIRS"
for file in begin_ts_file hcc_ops_trace hsa_handles hip_api_trace roctx_trace hsa_api_trace results async_copy_trace; do
res=$(find ${INPUT_DIRS} -type f -regextype sed -regex ".*/[0-9]\{1,\}_${file}\.txt" \
-not -path "${OUTPUT_DIR}/*")
test -n "${res}" && cat ${res} > "${OUTPUT_DIR}/${file}.txt"
done
if ! [ -d "${BIN_DIR}" ] ; then
echo "Bin directory $BIN_DIR not found!"
exit 1
fi
if [ -z "$ROCP_PYTHON_VERSION" ] ; then
ROCP_PYTHON_VERSION=python3
fi
OUTPUT_LIST="$OUTPUT_DIR/results.txt"
db_output="$OUTPUT_DIR/results.db"
echo "$ROCP_PYTHON_VERSION $BIN_DIR/tblextr.py $db_output $OUTPUT_LIST"
$ROCP_PYTHON_VERSION $BIN_DIR/tblextr.py $db_output $OUTPUT_LIST
if [ "$?" -ne 0 ] ; then
echo "Profiling data corrupted: '$OUTPUT_LIST'"
exit 1
fi
+351
View File
@@ -0,0 +1,351 @@
#!/bin/bash -e
set -eo pipefail
# Define color code
GREEN='\033[0;32m'
GREY='\033[0;90m'
RED='\033[0;31m'
RESET='\033[0m'
# Function to display deprecation warning message
display_warning() {
echo -e "\n${RED}WARNING: We are phasing out development and support for roctracer/rocprofiler/rocprof/rocprofv2 in favor of \
rocprofiler-sdk/rocprofv3 in upcoming ROCm releases. Going forward, only critical defect fixes will be addressed for \
older versions of profiling tools and libraries. We encourage all users to upgrade to the latest version, \
rocprofiler-sdk library and rocprofv3 tool, to ensure continued support and access to new features.${RESET}\n"
}
# Display the warning message
display_warning
# LD_PRELOAD on script will not get propagated
if [ -n "${ROCP_PRELOAD}" ]; then LD_PRELOAD="${ROCP_PRELOAD}"; fi
CURRENT_DIR="$( dirname -- "$0"; )";
ROCPROFV2_DIR=$(dirname -- $(realpath ${BASH_SOURCE[0]}));
ROCM_DIR=$( dirname -- "$ROCPROFV2_DIR"; )
PLUGIN_LIST=("ctf" "perfetto" "file" "att" "cli" "json")
RUN_FROM_BUILD=0
if [[ $ROCPROFV2_DIR == *"/build"* ]]; then
RUN_FROM_BUILD=1
ROCM_DIR=$ROCPROFV2_DIR
fi
export ROCPROFILER_METRICS_PATH=$ROCM_DIR/libexec/rocprofiler/counters/derived_counters.xml
export LD_LIBRARY_PATH=$ROCM_DIR/lib:$LD_LIBRARY_PATH
export HSA_TOOLS_LIB=$ROCM_DIR/lib/librocprofiler64.so.2
usage() {
echo -e "${RESET}ROCProfilerV2 Run Script Usage:"
echo -e "${GREEN}-h | --help ${RESET} For showing this message"
echo -e "${GREEN}--list-counters ${RESET} For showing all available counters for the current GPUs"
if [ $RUN_FROM_BUILD == 1 ]; then
echo -e "${GREEN}-t | --test ${RESET} For Running the tests"
echo -e "${GREEN}-mt | --mem-test ${RESET} For Running the Memory Leak tests. This run requires building using -acb | --asan-clean-build option"
fi
echo -e "${GREEN}-m ${RESET} For providing an absolute path of a custom metrics file"
echo -e "${GREEN}--basenames ${RESET} For Truncating the kernel names"
echo -e "${GREEN}--hip-api ${RESET} For Collecting HIP API Traces"
echo -e "${GREEN}--hip-activity | --hip-trace ${RESET} For Collecting HIP API Activities Traces"
echo -e "${GREEN}--hsa-api ${RESET} For Collecting HSA API Traces"
echo -e "${GREEN}--hsa-activity | --hsa-trace ${RESET} For Collecting HSA API Activities Traces"
echo -e "${GREEN}--roctx-trace ${RESET} For Collecting ROCTx Traces"
echo -e "${GREEN}--kernel-trace ${RESET} For Collecting Kernel dispatch Traces"
echo -e "${GREEN}--sys-trace ${RESET} For Collecting HIP and HSA APIs and their Activities Traces along ROCTX\n"
echo -e "\t#${GREY}usage e.g: rocprofv2 --[hip-trace|hsa-trace|roctx-trace|kernel-trace|sys-trace] <executable>\n"${RESET}
echo -e "${GREEN}--plugin ${RESET} PLUGIN_NAME For enabling a plugin (cli/file/perfetto/ctf/speedscope)"
echo -e "\t#${GREY} usage(file/perfetto/ctf) e.g: rocprofv2 -i pmc.txt --plugin [file/perfetto/ctf/json] -d out_dir <executable>"
echo -e "\t# use \"rocprofv2 --plugin json --disable-json-data-flows ...\" for SpeedScope support as speedscope doesn't support data flows.${RESET}\n"
echo -e "${GREEN}--plugin-version ${RESET} <1|2> For selecting the version for the plugin (1/2)"
echo -e "\t#${GREY} 1 - Legacy output format, 2 - New output format (default)${RESET}\n"
echo -e "${GREEN}-i | --input ${RESET} For adding counters file path (every line in the text file represents a counter)"
echo -e "\t#${GREY} usage: rocprofv2 -i pmc.txt -d <out_dir> <executable>${RESET}\n"
echo -e "${GREEN}-o | --output-file-name ${RESET} For the output file name"
echo -e "\t#${GREY} usage e.g:(with current dir): rocprofv2 --hip-trace -o <file_name> <executable>"
echo -e "\t#${GREY} usage e.g:(with custom dir): rocprofv2 --hip-trace -d <out_dir> -o <file_name> <executable>${RESET}\n"
echo -e "${GREEN}-d | --output-directory ${RESET} For adding output path where the output files will be saved"
echo -e "\t#${GREY} usage e.g:(with custom dir): rocprofv2 --hip-trace -d <out_dir> <executable>${RESET}\n"
echo -e "${GREEN}-fi | --flush-interval ${RESET} For adding a flush interval in milliseconds, every \"flush interval\" the buffers will be flushed"
echo -e "\t#${GREY} usage e.g: rocprofv2 --hip-trace -fi 1000 <executable>${RESET}\n"
echo -e "${GREEN}-tp | --trace-period ${RESET} Specifies a trace period in milliseconds, with format \"-tp <DELAY>:<ACTIVE_TIME>:<LOOP_RESET_TIME>\"."
echo -e "\t#${GREY} usage e.g: rocprofv2 --hip-trace -tp 1000:2000:4000 <executable>${RESET}\n"
echo -e "${GREEN}-ns | --no-serialization ${RESET} For disabling serilization when running in counter-collection mode\"."
echo -e "\t#${GREY} usage e.g: rocprofv2 -i pmc.txt -ns${RESET}\n"
exit 1
}
if [ -z "$1" ]; then
usage
exit 1
fi
OUTPUT_PATH_INTERNAL="."
while [ 1 ]; do
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
usage
exit 1
elif [[ "$1" == "-t" || "$1" == "--test" ]]; then
if [ $RUN_FROM_BUILD == 1 ]; then
./run_tests.sh
exit 1
fi
elif [[ "$1" == "-mt" || "$1" == "--mem-test" ]]; then
if [ $RUN_FROM_BUILD == 1 ]; then
$ROCM_DIR/tests-v2/memorytests/run_asan_tests.sh $ROCM_DIR/tests-v2/featuretests/profiler/apps/hip_vectoradd $ROCM_DIR/memleaks.log
exit 1
fi
elif [[ "$1" == "--list-counters" ]]; then
export LD_PRELOAD=$LD_PRELOAD:$ROCM_DIR/lib/rocprofiler/librocprofiler_tool.so
eval $ROCM_DIR/libexec/rocprofiler/ctrl
exit 1
elif [[ "$1" == "-i" || "$1" == "--input" ]]; then
if [ $2 ] && [ -n $2 ] && [ -r $2 ]; then
export COUNTERS_PATH=$2
else
echo -e "Error: \"$2\" doesn't exist!"
usage
exit 1
fi
shift
shift
elif [[ "$1" == "-o" || "$1" == "--output-file-name" ]]; then
if [ $2 ]; then
export OUT_FILE_NAME=$2
else
usage
exit 1
fi
shift
shift
elif [[ "$1" == "-d" || "$1" == "--output-directory" ]]; then
if [ $2 ]; then
OUTPUT_PATH_INTERNAL=$2
export OUTPUT_PATH=$OUTPUT_PATH_INTERNAL
else
usage
exit 1
fi
shift
shift
elif [[ "$1" == "-fi" || "$1" == "--flush-interval" ]]; then
if [ $2 ] && [ $2 -gt 0 ]; then
export ROCPROFILER_FLUSH_INTERVAL=$2
else
echo -e "Wrong input \"$2\" for flush interval, it needs to be integer greater than zero!"
usage
exit 1
fi
shift
shift
elif [[ "$1" == "-tp" || "$1" == "--trace-period" ]]; then
if [ $2 ] && [[ "$2" == *":"* ]]; then
export ROCPROFILER_TRACE_PERIOD=$2
else
echo -e "Wrong input \"$2\" for trace period!"
usage
exit 1
fi
shift
shift
elif [ "$1" == "--hip-api" ]; then
export ROCPROFILER_HIP_API_TRACE=1
shift
elif [[ "$1" == "-ns" || "$1" == "--no-serialization" ]]; then
export ROCPROFILER_NO_SERIALIZATION=1
shift
elif [[ "$1" == "--hip-activity" || "$1" == "--hip-trace" ]]; then
export ROCPROFILER_HIP_API_TRACE=1
export ROCPROFILER_HIP_ACTIVITY_TRACE=1
shift
elif [ "$1" == "--hsa-api" ]; then
export ROCPROFILER_HSA_API_TRACE=1
shift
elif [[ "$1" == "--hsa-activity" || "$1" == "--hsa-trace" ]]; then
export ROCPROFILER_HSA_API_TRACE=1
export ROCPROFILER_HSA_ACTIVITY_TRACE=1
shift
elif [ "$1" == "--roctx-trace" ]; then
export ROCPROFILER_ROCTX_TRACE=1
shift
elif [ "$1" == "--kernel-trace" ]; then
export ROCPROFILER_KERNEL_TRACE=1
shift
elif [ "$1" == "--sys-trace" ]; then
export ROCPROFILER_HIP_API_TRACE=1
export ROCPROFILER_HIP_ACTIVITY_TRACE=1
export ROCPROFILER_HSA_API_TRACE=1
export ROCPROFILER_ROCTX_TRACE=1
shift
elif [ "$1" == "--roc-sys" ]; then
export ROCPROFILER_ENABLE_ROCSYS=$2
shift
shift
elif [ "$1" == "-m" ]; then
if [ $2 ] && [ -n $2 ] && [ -r $2 ]; then
export ROCPROFILER_METRICS_PATH=$2
else
echo -e "Error: \"$2\" path doesn't exist!"
usage
exit 1
fi
shift
shift
elif [ "$1" == "--version" ]; then
if [ -f "$ROCM_DIR/libexec/rocprofiler/rocprofiler-version" ]; then
ROCPROFILER_LIBRARY_VERSION=2 $ROCM_DIR/libexec/rocprofiler/rocprofiler-version
else
ROCM_VERSION=$(cat $ROCM_DIR/.info/version)
echo -e "ROCm version: $ROCM_VERSION"
echo -e "ROCProfiler version: 2.0"
fi
exit 0
elif [ "$1" == "--basenames" ]; then
export ROCPROFILER_TRUNCATE_KERNEL_PATH=1
shift
elif [ "$1" == "--tool-version" ]; then
shift
shift
elif [ "$1" == "--plugin-version" ]; then
export ROCPROFILER_PLUGIN_LIB_VERSION=".$2"
if [ ! -z $ROCPROFILER_PLUGIN_LIB ]; then
if [ -f $ROCM_DIR/lib/rocprofiler/$ROCPROFILER_PLUGIN_LIB$ROCPROFILER_PLUGIN_LIB_VERSION ]; then
export ROCPROFILER_PLUGIN_LIB=$ROCPROFILER_PLUGIN_LIB$ROCPROFILER_PLUGIN_LIB_VERSION
else
echo -e "Error: \"$ROCPROFILER_PLUGIN_LIB$ROCPROFILER_PLUGIN_LIB_VERSION\" doesn't exist!"
exit 1
fi
fi
shift
shift
elif [ "$1" == "--plugin" ]; then
if [ -n $2 ]; then
PLUGIN=$2
if [[ ! "${PLUGIN_LIST[*]}" =~ $PLUGIN ]]; then
echo -e "Wrong input \"$2\" for plugin!"
usage
exit 1
fi
export ROCPROFILER_PLUGIN_LIB=lib${PLUGIN}_plugin.so$ROCPROFILER_PLUGIN_LIB_VERSION
else
echo -e "Wrong input \"$2\" for plugin!"
usage
exit 1
fi
if [ "$2" = "att" ]; then
export ROCPROFILER_TRUNCATE_KERNEL_PATH=1
if [ $RUN_FROM_BUILD == 1 ]; then
ATT_PATH=$ROCM_DIR/plugin/att/att/att.py
else
ATT_PATH=$ROCM_DIR/libexec/rocprofiler/att/att.py
export ROCPROFV2_ATT_LIB_PATH=$ROCM_DIR/lib/hsa-amd-aqlprofile/librocprofv2_att.so
fi
ATT_ARGV=$3
shift
ATT_OPTIONS="Not done"
ATT_PYTHON3_ARG="python3 "
while [ "$ATT_OPTIONS" = "Not done" ]; do
if [[ "$3" = "--trace_file" ]]; then
ATT_ARGV="$ATT_ARGV $3 \"$4\""
shift
shift
elif [[ "$3" = "--mode" || "$3" = "--ports" || "$3" == "--att_kernel" ]]; then
ATT_ARGV="$ATT_ARGV $3 $4"
shift
shift
else
ATT_OPTIONS="Done"
fi
done
fi
shift
shift
elif [ "$1" == "--disable-json-data-flows" ]; then
export ROCPROFILER_DISABLE_JSON_DATA_FLOWS=1
shift
elif [[ "$1" == "-"* || "$1" == "--"* ]]; then
echo -e "Wrong option \"$1\", Please use the following options:\n"
usage
exit 1
else
break
fi
done
PMC_LINES=()
if [ -n "$COUNTERS_PATH" ]; then
input=$COUNTERS_PATH
while IFS= read -r line || [[ -n "$line" ]]; do
#skip empty lines
if [[ -z "$line" ]]; then
continue
fi
# if in att mode, only add the first line
if [[ ! -n "$PMC_LINES" ]] || [[ ! -n "$ATT_ARGV" ]]; then
PMC_LINES+=("$line")
fi
done <$input
fi
if [ -z $ROCPROFILER_PLUGIN_LIB ]; then
PLUGIN_TO_BE_USED=file_plugin
if [[ -z $OUTPUT_PATH && -z $OUT_FILE_NAME ]]; then
PLUGIN_TO_BE_USED=cli_plugin
fi
if [ -f $ROCM_DIR/lib/rocprofiler/$ROCPROFILER_PLUGIN_LIB$ROCPROFILER_PLUGIN_LIB_VERSION ]; then
export ROCPROFILER_PLUGIN_LIB=lib$PLUGIN_TO_BE_USED.so$ROCPROFILER_PLUGIN_LIB_VERSION
else
export ROCPROFILER_PLUGIN_LIB=lib$PLUGIN_TO_BE_USED.so
fi
fi
export LD_PRELOAD=$LD_PRELOAD:$ROCM_DIR/lib/rocprofiler/librocprofiler_tool.so
if [ -n "$PMC_LINES" ] && [ ! -n "$ATT_ARGV" ]; then
COUNTER=1
for i in ${!PMC_LINES[@]}; do
if [[ ${PMC_LINES[$i]} =~ "kernel:" ]]; then
echo ${PMC_LINES[$i]}
export ROCPROFILER_KERNEL_FILTER="${PMC_LINES[$i]}"
fi
done
for i in ${!PMC_LINES[@]}; do
export ROCPROFILER_COUNTERS="${PMC_LINES[$i]}"
if [[ ! ${PMC_LINES[$i]} =~ "pmc" ]]; then
continue
fi
FINAL_PATH="$OUTPUT_PATH_INTERNAL/pmc_$COUNTER"
if [ -n "$OUT_FILE_NAME" ] || [ -n "$OUTPUT_PATH" ]; then
export OUTPUT_PATH=$FINAL_PATH
fi
let COUNTER=COUNTER+1
LD_PRELOAD=$LD_PRELOAD "${@}"
if [ -n "$OUTPUT_PATH" ]; then
echo -e "\nThe output path for the following counters: $OUTPUT_PATH"
fi
done
else
LD_PRELOAD=$LD_PRELOAD "${@}"
fi
get_pmc_results_txt_path() {
for file_name in $(ls $1); do
if [[ $file_name == *results.txt ]]; then
echo "$1/$file_name"
fi
done
}
if [ -n "$ATT_PATH" ]; then
if [ -n "$ATT_ARGV" ]; then
eval "$ATT_PYTHON3_ARG $ATT_PATH $ATT_ARGV"
elif [ ! -n "$PMC_LINES" ]; then
echo "ATT File is required!"
fi
fi
+711
View File
@@ -0,0 +1,711 @@
#!/bin/bash
################################################################################
# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
#
# 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 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.
################################################################################
ROCPROF_ARGS="$*"
time_stamp=`date +%y%m%d_%H%M%S`
BIN_DIR=$(dirname $(realpath ${BASH_SOURCE[0]}))
ROOT_DIR=$(dirname $BIN_DIR)
RUN_DIR=`pwd`
TMP_DIR="/tmp"
DATA_DIR="rpl_data_${time_stamp}_$$"
RPL_PATH=$ROOT_DIR/lib
TLIB_PATH=$RPL_PATH/rocprofiler
TTLIB_PATH=$ROOT_DIR/lib/roctracer
ROCM_LIB_PATH=$ROOT_DIR/lib
PROF_BIN_DIR=$ROOT_DIR/libexec/rocprofiler
# Define color code
GREEN='\033[0;32m'
GREY='\033[0;90m'
RED='\033[0;31m'
RESET='\033[0m'
# Function to display deprecation warning message
display_warning() {
echo -e "\n${RED}WARNING: We are phasing out development and support for roctracer/rocprofiler/rocprof/rocprofv2 in favor of \
rocprofiler-sdk/rocprofv3 in upcoming ROCm releases. Going forward, only critical defect fixes will be addressed for \
older versions of profiling tools and libraries. We encourage all users to upgrade to the latest version, \
rocprofiler-sdk library and rocprofv3 tool, to ensure continued support and access to new features.${RESET}\n"
}
# Display the warning message
display_warning
# check if rocprof is supportd on this gpu arch
V1_SUPPORTED_GPU_ARCHS=("gfx80x","gfx90x","gfx94x")
IS_SUPPORTED="false"
if [ -f $BIN_DIR/rocm_agent_enumerator ]; then
CURRENT_AGENTS_LIST=$($BIN_DIR/rocm_agent_enumerator)
else
IS_SUPPORTED="false"
echo -e "Warning: Missing rocm_agent_enumerator binary"
exit 0
fi
if [ -z "$ROCP_PYTHON_VERSION" ] ; then
ROCP_PYTHON_VERSION=python3
fi
# runtime API trace
ROCTX_TRACE=0
HSA_TRACE=0
SYS_TRACE=0
HIP_TRACE=0
# Generate stats
GEN_STATS=0
# Quoting profiled cmd line
CMD_QTS=1
export PATH=.:$PATH
# enable error logging
export HSA_TOOLS_REPORT_LOAD_FAILURE=1
export HSA_VEN_AMD_AQLPROFILE_LOG=1
export ROCPROFILER_LOG=1
unset ROCPROFILER_SESS
# Profiler environment
# Loading of profiler library by HSA runtime
MY_HSA_TOOLS_LIB="$RPL_PATH/librocprofiler64.so.1"
# Tool internal Preloads
ROCPROFV1_LD_PRELOAD=$MY_HSA_TOOLS_LIB
ROCPROFV1_TOOL_PRELOAD=$MY_HSA_TOOLS_LIB
# Loading of the test tool by ROC Profiler
export ROCP_TOOL_LIB=$TLIB_PATH/librocprof-tool.so
# ROC Profiler metrics definition
export ROCP_METRICS=$TLIB_PATH/metrics.xml
# Enabling HSA dispatches intercepting by ROC PRofiler
export ROCP_HSA_INTERCEPT=1
# Disabling internal ROC Profiler proxy queue (simple version supported for testing purposes)
unset ROCP_PROXY_QUEUE
# Disable AQL-profile read API
export AQLPROFILE_READ_API=0
# ROC Profiler package path
export ROCP_PACKAGE_DIR=$ROOT_DIR
# enabled SPM KFD mode
export ROCP_SPM_KFD_MODE=1
# error handling
fatal() {
echo "$0: Error: $1"
echo ""
usage
exit 1
}
error() {
echo "$0: Error: $1"
echo ""
exit 1
}
error_message=""
errck() {
if [ -n "$error_message" ]; then
fatal "$1 : $error_message"
fi
}
# usage method
usage() {
bin_name=`basename $0`
echo "ROCm Profiling Library (RPL) run script, a part of ROCprofiler library package."
echo "Full path: $BIN_DIR/$bin_name"
echo "Metrics definition: $TLIB_PATH/metrics.xml"
echo ""
echo "Usage:"
echo " $bin_name [-h] [--list-basic] [--list-derived] [-i <input .txt/.xml file>] [-o <output CSV file>] <app command line>"
echo ""
echo "Options:"
echo " -h - this help"
echo " --tool-version <1|2> - to use specific version of rocprof tool, by default v1 is used"
echo " 1 - rocprofiler tool v1"
echo " 2 - rocprofiler tool v2"
echo " --verbose - verbose mode, dumping all base counters used in the input metrics"
echo " --list-basic - to print the list of basic HW counters"
echo " --list-derived - to print the list of derived metrics with formulas"
echo " --cmd-qts <on|off> - quoting profiled cmd line [on]"
echo ""
echo " -i <.txt|.xml file> - input file"
echo " Input file .txt format, automatically rerun application for every profiling features line:"
echo ""
echo " # Perf counters group 1"
echo " pmc : Wavefronts VALUInsts SALUInsts SFetchInsts FlatVMemInsts LDSInsts FlatLDSInsts GDSInsts VALUUtilization FetchSize"
echo " # Perf counters group 2"
echo " pmc : WriteSize L2CacheHit"
echo " # Filter by dispatches range, GPU index and kernel names"
echo " # supported range formats: \"3:9\", \"3:\", \"3\""
echo " range: 1 : 4"
echo " gpu: 0 1 2 3"
echo " kernel: simple Pass1 simpleConvolutionPass2"
echo ""
echo " Input file .xml format, for single profiling run:"
echo ""
echo " # Metrics list definition, also the form \"<block-name>:<event-id>\" can be used"
echo " # All defined metrics can be found in the 'metrics.xml'"
echo " # There are basic metrics for raw HW counters and high-level metrics for derived counters"
echo " <metric name=SQ:4,SQ_WAVES,VFetchInsts"
echo " ></metric>"
echo ""
echo " # Filter by dispatches range, GPU index and kernel names"
echo " <metric"
echo " # range formats: \"3:9\", \"3:\", \"3\""
echo " range=\"\""
echo " # list of gpu indexes \"0,1,2,3\""
echo " gpu_index=\"\""
echo " # list of matched sub-strings \"Simple1,Conv1,SimpleConvolution\""
echo " kernel=\"\""
echo " ></metric>"
echo ""
echo " -o <output file> - output CSV file [<input file base>.csv]"
echo " -d <data directory> - directory where profiler store profiling data including traces [/tmp]"
echo " The data directory is automatically removed if it is matching the default temporary directory."
echo " -t <temporary directory> - to change the temporary directory [/tmp]"
echo " By changing the temporary directory you can prevent removing the profiling data from /tmp or enable removing from not '/tmp' directory."
echo " -m <metric file> - file defining custom metrics to use in-place of defaults."
echo ""
echo " --basenames <on|off> - to turn on/off truncating of the kernel full function names till the base ones [off]"
echo " --timestamp <on|off> - to turn on/off the kernel dispatches timestamps, dispatch/begin/end/complete during kernel profiling [off]"
echo " --ctx-wait <on|off> - to wait for outstanding contexts on profiler exit [on]"
echo " --ctx-limit <max number> - maximum number of outstanding contexts [0 - unlimited]"
echo " --heartbeat <rate sec> - to print progress heartbeats [0 - disabled]"
echo " --obj-tracking <on|off> - to turn on/off kernels code objects tracking [on]"
echo " To support V3 code object"
echo ""
echo " --stats - generating kernel execution stats, file <output name>.stats.csv"
echo ""
echo " --roctx-trace - to enable rocTX application code annotation trace, \"Markers and Ranges\" JSON trace section."
echo " --hip-trace - to trace HIP, generates API execution stats and JSON file chrome-tracing compatible"
echo " --hsa-trace - to trace HSA, generates API execution stats and JSON file chrome-tracing compatible"
echo " --sys-trace - to trace HIP/HSA APIs and GPU activity, generates stats and JSON trace chrome-tracing compatible"
echo " '--hsa-trace' can be used in addition to select activity tracing from HSA (ROCr runtime) level"
echo " Generated files: <output name>.<domain>_stats.txt <output name>.json"
echo " Traced API list can be set by input .txt or .xml files."
echo " Input .txt:"
echo " hsa: hsa_queue_create hsa_amd_memory_pool_allocate"
echo " Input .xml:"
echo " <trace name=\"HSA\">"
echo " <parameters list=\"hsa_queue_create, hsa_amd_memory_pool_allocate\">"
echo " </parameters>"
echo " </trace>"
echo ""
echo " --roctx-rename - to rename kernels with their enclosing rocTX range's message."
echo ""
echo " --trace-start <on|off> - to enable tracing on start [on]"
echo " --trace-period <dealy:length:rate> - to enable trace with initial delay, with periodic sample length and rate"
echo " Supported time formats: <number(m|s|ms|us)>"
echo " --flush-rate <rate> - to enable trace flush rate (time period)"
echo " Supported time formats: <number(m|s|ms|us)>"
echo " --parallel-kernels - to enable concurrent kernels"
echo ""
echo "Configuration file:"
echo " You can set your parameters defaults preferences in the configuration file 'rpl_rc.xml'. The search path sequence: .:${HOME}:<installation directory>"
echo " First the configuration file is searched in the current directory, then in the current user's home directory, and then in the installation directory."
echo " Configurable options: 'basenames', 'timestamp', 'ctx-limit', 'heartbeat', 'obj-tracking'."
echo " An example of 'rpl_rc.xml':"
echo " <defaults"
echo " basenames=off"
echo " timestamp=off"
echo " ctx-limit=0"
echo " heartbeat=0"
echo " obj-tracking=off"
echo " ></defaults>"
echo ""
echo " --merge-traces - Script for aggregating results from multiple rocprofiler out directries."
echo " Usage: if running with rocprof"
echo " rocprof --merge-traces -o <outputdir> [<inputdir>...]"
echo ""
exit 1
}
# checking for availability of rocminfo utility
if !command -v rocminfo > /dev/null 2>&1 ; then
error "'rocminfo' utility is not found: please add ROCM bin path to PATH env var.";
fi
# setting ROCM_LIB_PATH
set_rocm_lib_path() {
for ROCM_LIB_PATH in "$ROOT_DIR/lib" "$ROOT_DIR/lib64" ; do
if [ -d "$ROCM_LIB_PATH" ]; then
return 0
fi
done
#error
return 255
}
# profiling run method
OUTPUT_LIST=""
run() {
if ! set_rocm_lib_path ; then
echo " Fatal could not find ROCm lib directory "
fatal
fi
# split the CURRENT_AGENTS_LIST array into individual elements.
current_gpus=(${CURRENT_AGENTS_LIST[@]})
for gpu in "${current_gpus[@]}"; do
# Check first 5 characters of gpu strings.
if [[ "${V1_SUPPORTED_GPU_ARCHS[@]:0:5}" =~ "${gpu:0:5}" ]]; then
IS_SUPPORTED="true"
fi
done
if [[ $IS_SUPPORTED == "false" ]]; then
echo ""
echo "------------ ------------ ------------"
echo "WARNING: rocprof(v1) is not supported on this device. Recommended use: rocprofv2"
echo "Please refer project's README for a list of supported architecures."
echo "------------ ------------ ------------"
echo ""
fi
export ROCP_INPUT="$1"
OUTPUT_DIR="$2"
shift
shift
APP_CMD=$*
if [ "$OUTPUT_DIR" = "-" ] ; then
input_tag=`echo $ROCP_INPUT | sed "s/\.xml//"`
export ROCP_OUTPUT_DIR=${input_tag}_results_${time_stamp}
elif [ "$OUTPUT_DIR" = "--" ] ; then
unset ROCP_OUTPUT_DIR
else
export ROCP_OUTPUT_DIR=$OUTPUT_DIR
fi
echo "RPL: result dir '$ROCP_OUTPUT_DIR'"
if [ ! -e "$ROCP_INPUT" ] ; then
error "Input file '$ROCP_INPUT' not found"
fi
if [ -n "$ROCP_OUTPUT_DIR" ] ; then
if [ "$OUTPUT_DIR" = "-" ] ; then
if [ -e "$ROCP_OUTPUT_DIR" ] ; then
error "generated dir '$ROCP_OUTPUT_DIR' exists"
fi
fi
mkdir -p "$ROCP_OUTPUT_DIR"
OUTPUT_LIST="$OUTPUT_LIST $ROCP_OUTPUT_DIR/results.txt"
fi
API_TRACE=""
MY_LD_PRELOAD=""
if [ "$ROCTX_TRACE" = 1 ] ; then
API_TRACE=${API_TRACE}":roctx"
MY_LD_PRELOAD="$ROCM_LIB_PATH/libroctx64.so"
fi
if [ "$HIP_TRACE" = 1 ] ; then
API_TRACE=${API_TRACE}":hip"
fi
if [ "$SYS_TRACE" = 1 ] ; then
API_TRACE=${API_TRACE}":sys"
fi
if [ "$HSA_TRACE" = 1 ] ; then
export ROCTRACER_DOMAIN=$API_TRACE":hsa"
ROCPROFV1_TOOL_PRELOAD="$MY_LD_PRELOAD $TTLIB_PATH/libroctracer_tool.so"
ROCPROFV1_LD_PRELOAD="$MY_HSA_TOOLS_LIB:$ROCM_LIB_PATH/libroctracer64.so.4"
elif [ -n "$API_TRACE" ] ; then
export ROCTRACER_DOMAIN=$API_TRACE
OUTPUT_LIST="$ROCP_OUTPUT_DIR/"
ROCPROFV1_TOOL_PRELOAD="$MY_LD_PRELOAD $TTLIB_PATH/libroctracer_tool.so"
ROCPROFV1_LD_PRELOAD="$ROCM_LIB_PATH/libroctracer64.so.4"
fi
if [ "$ROCP_STATS_OPT" = 1 ] ; then
if [ "$ROCTRACER_DOMAIN" = ":hip" ] ; then
ROCPROFV1_LD_PRELOAD="$ROCM_LIB_PATH/libroctracer64.so.4"
ROCPROFV1_TOOL_PRELOAD="$MY_LD_PRELOAD $TTLIB_PATH/libhip_stats.so"
else
error_message="ROCP_STATS_OPT is only available with --hip-trace option"
echo $error_message
exit 1
fi
fi
retval=1
if [ -n "$ROCP_OUTPUT_DIR" ] ; then
log_file="$ROCP_OUTPUT_DIR/log.txt"
exit_file="$ROCP_OUTPUT_DIR/exit.txt"
{
HSA_TOOLS_LIB="$ROCPROFV1_TOOL_PRELOAD" LD_PRELOAD=$LD_PRELOAD:"$ROCPROFV1_LD_PRELOAD" eval "$APP_CMD"
retval=$?
echo "exit($retval)" > $exit_file
} 2>&1 | tee "$log_file"
exitval=`cat "$exit_file" | sed -n "s/^.*exit(\([0-9]*\)).*$/\1/p"`
if [ -n "$exitval" ] ; then retval=$exitval; fi
else
HSA_TOOLS_LIB="$ROCPROFV1_TOOL_PRELOAD" LD_PRELOAD=$LD_PRELOAD:"$ROCPROFV1_LD_PRELOAD" eval "$APP_CMD"
retval=$?
fi
return $retval
}
merge_output() {
while [ -n "$1" ] ; do
output_dir=$(echo "$1" | sed "s/\/[^\/]*$//")
for file_name in `ls $output_dir` ; do
output_name=$(echo $file_name | sed -n "/\.txt$/ s/^[0-9]*_//p")
if [ -n "$output_name" ] ; then
trace_file=$output_dir/$file_name
output_file=$output_dir/$output_name
touch $output_file
cat $trace_file >> $output_file
fi
done
shift
done
}
convert_time_val() {
local time_maxumim_us=$((0xffffffff))
local __resultvar=$1
eval "local val=$"$__resultvar
val_m=`echo $val | sed -n "s/^\([0-9]*\)m$/\1/p"`
val_s=`echo $val | sed -n "s/^\([0-9]*\)s$/\1/p"`
val_ms=`echo $val | sed -n "s/^\([0-9]*\)ms$/\1/p"`
val_us=`echo $val | sed -n "s/^\([0-9]*\)us$/\1/p"`
if [ -n "$val_m" ] ; then val_us=$((val_m*60000000))
elif [ -n "$val_s" ] ; then val_us=$((val_s*1000000))
elif [ -n "$val_ms" ] ; then val_us=$((val_ms*1000))
fi
if [ -z "$val_us" ] ; then
error_message="invalid time value format ($val)"
elif [ "$val_us" -gt "$time_maxumim_us" ] ; then
error_message="time value exceeds maximum supported ($val > ${time_maxumim_us}us)"
else
eval $__resultvar="'$val_us'"
fi
}
unset_v1_envs() {
# enable error logging
unset HSA_TOOLS_REPORT_LOAD_FAILURE
unset HSA_VEN_AMD_AQLPROFILE_LOG
unset ROCPROFILER_LOG
unset ROCPROFILER_SESS
unset MY_HSA_TOOLS_LIB
unset ROCP_TOOL_LIB
unset ROCP_METRICS
unset ROCP_HSA_INTERCEPT
unset ROCP_PROXY_QUEUE
unset AQLPROFILE_READ_API
unset ROCP_PACKAGE_DIR
unset ROCP_SPM_KFD_MODE
}
################################################################################################
# main
echo "RPL: on '$time_stamp' from '$ROOT_DIR' in '$RUN_DIR'"
# Parsing arguments
if [ -z "$1" ] ; then
usage
fi
INPUT_FILE=""
DATA_PATH="-"
OUTPUT_DIR="-"
output=""
csv_output=""
ARG_IN=""
while [ 1 ] ; do
ARG_IN=$1
ARG_VAL=1
if [ "$1" = "--tool-version" ] ; then
if [ $2 = 1 ] ; then
:
elif [ $2 = 2 ] ; then
# unset all v1 variables
unset_v1_envs
eval $BIN_DIR/rocprofv2 $ROCPROF_ARGS
exit 0
else
echo "Wrong option '$1 $2'"
usage
fi
elif [ "$1" = "--version" ]; then
if [ -f "$BIN_DIR/../libexec/rocprofiler/rocprofiler-version" ]; then
ROCPROFILER_LIBRARY_VERSION=1 $BIN_DIR/../libexec/rocprofiler/rocprofiler-version
else
ROCM_VERSION=$(cat $BIN_DIR/../.info/version)
echo -e "ROCm version: $ROCM_VERSION"
echo -e "ROCProfiler version: 2.0"
fi
exit 0
elif [ "$1" = "-h" ] ; then
usage
elif [ "$1" = "-i" ] ; then
INPUT_FILE="$2"
elif [ "$1" = "-o" ] ; then
output="$2"
if [[ $output != *.csv ]]; then
echo "error: file name must have .CSV extension"
exit 1
fi
elif [ "$1" = "-d" ] ; then
DATA_PATH=$2
elif [ "$1" = "-t" ] ; then
TMP_DIR="$2"
if [ "$OUTPUT_DIR" = "-" ] ; then
DATA_PATH=$TMP_DIR
fi
elif [ "$1" = "-m" ] ; then
unset ROCP_METRICS
export ROCP_METRICS="$2"
elif [ "$1" = "--list-basic" ] ; then
export ROCP_INFO=b
HSA_TOOLS_LIB="$MY_HSA_TOOLS_LIB" eval "$TLIB_PATH/rocprof-ctrl"
exit 1
elif [ "$1" = "--list-derived" ] ; then
export ROCP_INFO=d
HSA_TOOLS_LIB="$MY_HSA_TOOLS_LIB" eval "$TLIB_PATH/rocprof-ctrl"
exit 1
elif [ "$1" = "--basenames" ] ; then
if [ "$2" = "on" ] ; then
export ROCP_TRUNCATE_NAMES=1
else
export ROCP_TRUNCATE_NAMES=0
fi
elif [ "$1" = "--timestamp" ] ; then
if [ "$2" = "on" ] ; then
export ROCP_TIMESTAMP_ON=1
else
export ROCP_TIMESTAMP_ON=0
fi
elif [ "$1" = "--ctx-wait" ] ; then
if [ "$2" = "on" ] ; then
export ROCP_OUTSTANDING_WAIT=1
else
export ROCP_OUTSTANDING_WAIT=0
fi
elif [ "$1" = "--ctx-limit" ] ; then
export ROCP_OUTSTANDING_MAX="$2"
elif [ "$1" = "--heartbeat" ] ; then
export ROCP_OUTSTANDING_MON="$2"
elif [ "$1" = "--stats" ] ; then
ARG_VAL=0
export ROCP_TIMESTAMP_ON=1
GEN_STATS=1
elif [ "$1" = "--roctx-trace" ] ; then
ARG_VAL=0
GEN_STATS=1
ROCTX_TRACE=1
elif [ "$1" = "--hsa-trace" ] ; then
ARG_VAL=0
export ROCP_TIMESTAMP_ON=1
GEN_STATS=1
HSA_TRACE=1
elif [ "$1" = "--sys-trace" ] ; then
ARG_VAL=0
export ROCP_TIMESTAMP_ON=1
GEN_STATS=1
SYS_TRACE=1
elif [ "$1" = "--hip-trace" ] ; then
ARG_VAL=0
export ROCP_TIMESTAMP_ON=1
GEN_STATS=1
HIP_TRACE=1
elif [ "$1" = "--roctx-rename" ] ; then
ARG_VAL=0
export ROCP_RENAME_KERNEL=1
elif [ "$1" = "--trace-start" ] ; then
if [ "$2" = "off" ] ; then
export ROCP_CTRL_RATE="-1:0:0"
fi
elif [ "$1" = "--trace-period" ] ; then
period_expr="^\([^:]*\):\([^:]*\):\([^:]*\)$"
period_ck=`echo "$2" | sed -n "s/"${period_expr}"/ok/p"`
if [ -z "$period_ck" ] ; then
fatal "Wrong option '$1 $2'"
fi
period_delay=`echo "$2" | sed -n "s/"${period_expr}"/\1/p"`
period_len=`echo "$2" | sed -n "s/"${period_expr}"/\2/p"`
period_rate=`echo "$2" | sed -n "s/"${period_expr}"/\3/p"`
convert_time_val period_delay
errck "Option '$ARG_IN', delay value"
convert_time_val period_len
errck "Option '$ARG_IN', length value"
convert_time_val period_rate
errck "Option '$ARG_IN', rate value"
export ROCP_CTRL_RATE="$period_delay:$period_len:$period_rate"
elif [ "$1" = "--flush-rate" ] ; then
period_rate=$2
convert_time_val period_rate
errck "Option '$ARG_IN', rate value"
export ROCP_FLUSH_RATE="$period_rate"
elif [ "$1" = "--obj-tracking" ] ; then
if [ "$2" = "off" ] ; then
export ROCP_OBJ_TRACKING=0
fi
elif [ "$1" = "--parallel-kernels" ] ; then
ARG_VAL=0
export ROCP_K_CONCURRENT=1
export AQLPROFILE_READ_API=1
elif [ "$1" = "--verbose" ] ; then
ARG_VAL=0
export ROCP_VERBOSE_MODE=1
elif [ "$1" = "--cmd-qts" ] ; then
if [ "$2" = "off" ] ; then
CMD_QTS=0
fi
elif [ "$1" = "--merge-traces" ] ; then
shift
echo "merging traces with $PROF_BIN_DIR/merge_traces.sh"
$PROF_BIN_DIR/merge_traces.sh $@
exit 0
else
break
fi
shift
if [ "$ARG_VAL" = 1 ] ; then shift; fi
done
ARG_CK=`echo $ARG_IN | sed "s/^-.*$/-/"`
if [ "$ARG_CK" = "-" ] ; then
fatal "Wrong option '$ARG_IN'"
fi
if [ "$GEN_STATS" = "1" -a "$ROCP_TIMESTAMP_ON" = "0" ] ; then
fatal "Wrong options, stats enabled with disabled timestamps"
fi
if [ -z "$INPUT_FILE" ] ; then
input_base="results"
input_type="none"
else
input_base=`echo "$INPUT_FILE" | sed "s/^\(.*\)\.\([^\.]*\)$/\1/"`
input_type=`echo "$INPUT_FILE" | sed "s/^\(.*\)\.\([^\.]*\)$/\2/"`
if [ -z "${input_base}" -o -z "${input_type}" ] ; then
fatal "Bad input file '$INPUT_FILE'"
fi
input_base=`basename $input_base`
fi
if [ "$DATA_PATH" = "-" ] ; then
DATA_PATH=$TMP_DIR
fi
if [ -n "$output" ] ; then
if [ "$output" = "--" ] ; then
OUTPUT_DIR="--"
else
csv_output=$output
fi
else
csv_output=$RUN_DIR/${input_base}.csv
fi
# Profiled cmd line string
APP_CMD=$*
if [ "$CMD_QTS" = 1 ] ; then
APP_CMD=""
for i in `seq 1 $#`; do
if [ -n "$APP_CMD" ] ; then
APP_CMD=$APP_CMD" "
fi
eval "arg=\${$i}"
APP_CMD=$APP_CMD\"$arg\"
done
fi
echo "RPL: profiling '$APP_CMD'"
echo "RPL: input file '$INPUT_FILE'"
input_list=""
RES_DIR=""
if [ "$input_type" = "xml" ] ; then
OUTPUT_DIR=$DATA_PATH
input_list=$INPUT_FILE
elif [ "$input_type" = "txt" -o "$input_type" = "none" ] ; then
RES_DIR=$DATA_PATH/$DATA_DIR
if [ -e $RES_DIR ] ; then
error "Rundir '$RES_DIR' exists"
fi
mkdir -p $RES_DIR
echo "RPL: output dir '$RES_DIR'"
if [ "$input_type" = "txt" ] ; then
$PROF_BIN_DIR/txt2xml.sh $INPUT_FILE $RES_DIR
else
echo "<metric></metric>" > $RES_DIR/input.xml
fi
input_list=`/bin/ls $RES_DIR/input*.xml`
export ROCPROFILER_SESS=$RES_DIR
else
fatal "Bad input file type '$INPUT_FILE'"
fi
if [ -n "$csv_output" ] ; then
rm -f $csv_output
fi
RET=1
export ROCP_MERGE_PIDS=1
for name in $input_list; do
run $name $OUTPUT_DIR $APP_CMD
RET=$?
if [ -n "$ROCPROFILER_SESS" -a -e "$ROCPROFILER_SESS/error" ] ; then
error_string=`cat $ROCPROFILER_SESS/error`
echo "Profiling error found: '$error_string'"
csv_output=""
RET=1
break
fi
done
if [ -n "$csv_output" ] ; then
merge_output $OUTPUT_LIST
if [ "$GEN_STATS" = "1" ] ; then
db_output=$(echo $csv_output | sed "s/\.csv/.db/")
$ROCP_PYTHON_VERSION $PROF_BIN_DIR/tblextr.py $db_output $OUTPUT_LIST
else
$ROCP_PYTHON_VERSION $PROF_BIN_DIR/tblextr.py $csv_output $OUTPUT_LIST
fi
if [ "$?" -ne 0 ] ; then
echo "Profiling data corrupted: '$OUTPUT_LIST'" | tee "$ROCPROFILER_SESS/error"
RET=1
fi
fi
if [ "$DATA_PATH" = "$TMP_DIR" ] ; then
if [ -e "$RES_DIR" ] ; then
rm -rf $RES_DIR
fi
fi
exit $RET
+34
View File
@@ -0,0 +1,34 @@
#!/bin/sh
BIN_DIR=`dirname $0`
BIN_DIR=`realpath $BIN_DIR`
PKG_DIR=${BIN_DIR%/bin}
if [ -z "$1" ] ; then
echo "Usage: $0 <cmd line>"
exit 1
fi
# profiler plugin library
test_app=$*
# ROC profiler library loaded by HSA runtime
export HSA_TOOLS_LIB=librocprofiler64.so.1
# tool library loaded by ROC profiler
if [ -z "$ROCP_TOOL_LIB" ] ; then
echo "ROCP_TOOL_LIB is not found"
exit 1
fi
# ROC profiler metrics config file
if [ -z "$ROCP_METRICS" ] ; then
echo "ROCP_METRICS is not found"
exit 1
fi
# enable error messages
export HSA_TOOLS_REPORT_LOAD_FAILURE=1
export HSA_VEN_AMD_AQLPROFILE_LOG=1
export ROCPROFILER_LOG=1
$test_app
+351
View File
@@ -0,0 +1,351 @@
################################################################################
# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
#
# 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 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.
################################################################################
import csv, sqlite3, re, sys
from functools import reduce
from txt2params import gen_params
# SQLite Database class
class SQLiteDB:
def __init__(self, file_name):
self.connection = sqlite3.connect(file_name)
self.tables = {}
self.section_index = 0
def __del__(self):
self.connection.close()
# add DB table
def add_table(self, name, descr, extra=()):
(field_list, field_dict) = descr
if name in self.tables:
raise Exception('table is already added: "' + name + '"')
# create DB table
table_descr = []
for field in field_list:
table_descr.append('"%s" %s' % (field, field_dict[field]))
for item in extra:
table_descr.append('"%s" %s' % (item[0], item[1]))
stm = "CREATE TABLE " + name + " (%s)" % ", ".join(table_descr)
cursor = self.connection.cursor()
cursor.execute(stm)
self.connection.commit()
# register table
fields_str = ",".join(map(lambda x: '"' + x + '"', field_list))
templ_str = ",".join("?" * len(field_list))
stm = "INSERT INTO " + name + "(" + fields_str + ") VALUES(" + templ_str + ");"
self.tables[name] = stm
return (cursor, stm)
# add columns to table
def add_columns(self, name, columns):
cursor = self.connection.cursor()
for item in columns:
stm = "ALTER TABLE " + name + ' ADD COLUMN "%s" %s' % (item[0], item[1])
cursor.execute(stm)
self.connection.commit()
# add columns with expression
def add_data_column(self, table_name, data_label, data_type, data_expr):
cursor = self.connection.cursor()
cursor.execute(
'ALTER TABLE %s ADD COLUMN "%s" %s' % (table_name, data_label, data_type)
)
cursor.execute("UPDATE %s SET %s = (%s);" % (table_name, data_label, data_expr))
def change_rec_name(self, table_name, rec_id, rec_name):
self.connection.execute(
"UPDATE " + table_name + ' SET Name = ? WHERE "Index" = ?', (rec_name, rec_id)
)
def change_rec_tid(self, table_name, rec_id, tid):
self.connection.execute(
"UPDATE " + table_name + ' SET tid = ? WHERE "Index" = ?', (tid, rec_id)
)
def change_rec_fld(self, table_name, fld_expr, rec_pat):
self.connection.execute(
"UPDATE " + table_name + " SET " + fld_expr + " WHERE " + rec_pat
)
def table_get_record(self, table_name, rec_pat):
cursor = self.connection.execute(
"SELECT * FROM " + table_name + " WHERE " + rec_pat
)
raws = cursor.fetchall()
if len(raws) != 1:
raise Exception(
"Record (" + rec_pat + ') is not unique, table "' + table_name + '"'
)
return list(raws[0])
# populate DB table entry
def insert_entry(self, table, val_list):
(cursor, stm) = table
cursor.execute(stm, val_list)
# populate DB table entry
def commit_entry(self, table, val_list):
self.insert_entry(table, val_list)
self.connection.commit()
# populate DB table data
def insert_table(self, table, reader):
for val_list in reader:
if not val_list[-1]:
val_list.pop()
self.insert_entry(table, val_list)
self.connection.commit()
# return table fields list
def _get_fields(self, table_name):
cursor = self.connection.execute("SELECT * FROM " + table_name)
return list(map(lambda x: '"%s"' % (x[0]), cursor.description))
# return table raws list
def _get_raws(self, table_name):
cursor = self.connection.execute("SELECT * FROM " + table_name)
return cursor.fetchall()
def _get_raws_indexed(self, table_name):
cursor = self.connection.execute(
"SELECT * FROM " + table_name + ' order by "Index" asc;'
)
return cursor.fetchall()
def _get_raw_by_id(self, table_name, rec_id):
cursor = self.connection.execute(
"SELECT * FROM " + table_name + ' WHERE "Index"=?', (rec_id,)
)
raws = cursor.fetchall()
if len(raws) != 1:
raise Exception('Index is not unique, table "' + table_name + '"')
return list(raws[0])
def table_get_raws(self, table_name):
return self._get_raws(table_name)
# dump CSV table
def dump_csv(self, table_name, file_name):
if not re.search(r"\.csv$", file_name):
raise Exception('wrong output file type: "' + file_name + '"')
fields = self._get_fields(table_name)
with open(file_name, mode="w") as fd:
fd.write(",".join(fields) + "\n")
for raw in self._get_raws(table_name):
tmp = list(raw)
for idx in range(len(tmp)):
if type(tmp[idx]) == str:
if not (tmp[idx][0] == tmp[idx][-1] == '"'):
tmp[idx] = '"' + tmp[idx] + '"'
raw = tuple(tmp)
fd.write(reduce(lambda a, b: str(a) + "," + str(b), raw) + "\n")
# dump JSON trace
def open_json(self, file_name):
if not re.search(r"\.json$", file_name):
raise Exception('wrong output file type: "' + file_name + '"')
with open(file_name, mode="w") as fd:
fd.write('{ "traceEvents":[{}\n')
def close_json(self, file_name):
if not re.search(r"\.json$", file_name):
raise Exception('wrong output file type: "' + file_name + '"')
with open(file_name, mode="a") as fd:
fd.write("}")
def label_json(self, pid, label, file_name):
if not re.search(r"\.json$", file_name):
raise Exception('wrong output file type: "' + file_name + '"')
with open(file_name, mode="a") as fd:
fd.write(
',{"args":{"name":"%s"},"ph":"M","pid":%s,"name":"process_name","sort_index":%d}\n'
% (label, pid, self.section_index)
)
self.section_index += 1
def flow_json(
self, base_id, from_pid, from_us_list, to_pid, to_us_dict, corr_id_list, file_name
):
if not re.search(r"\.json$", file_name):
raise Exception('wrong output file type: "' + file_name + '"')
with open(file_name, mode="a") as fd:
dep_id = base_id
for ind in range(len(from_us_list)):
corr_id = corr_id_list[ind] if (len(corr_id_list) != 0) else ind
if corr_id in to_us_dict:
(from_ts, stream_id, tid) = from_us_list[ind]
to_ts = to_us_dict[corr_id]
if from_ts > to_ts:
from_ts = to_ts
fd.write(
',{"ts":%d,"ph":"s","cat":"DataFlow","id":%d,"pid":%d,"tid":%d,"name":"dep"}\n'
% (from_ts, dep_id, from_pid, tid)
)
fd.write(
',{"ts":%d,"ph":"t","cat":"DataFlow","id":%d,"pid":%d,"tid":%d,"name":"dep"}\n'
% (to_ts, dep_id, to_pid, stream_id)
)
dep_id += 1
def metadata_json(self, jsonfile, sysinfo_file):
params = gen_params(sysinfo_file)
with open(jsonfile, mode="a") as fd:
cnt = 0
fd.write("],\n")
fd.write('"otherData": {\n')
for nkey in sorted(params.keys()):
key = nkey[1]
cnt = cnt + 1
if cnt == len(params):
fd.write(' "' + key + '": "' + params[nkey] + '"\n')
else:
fd.write(' "' + key + '": "' + params[nkey] + '",\n')
fd.write(" }\n")
def dump_json(self, table_name, data_name, file_name):
if not re.search(r"\.json$", file_name):
raise Exception('wrong output file type: "' + file_name + '"')
sub_ptrn = re.compile(r'(^"|"$)')
name_ptrn = re.compile(r"(name|Name)")
table_fields = self._get_fields(table_name)
table_raws = self._get_raws(table_name)
data_fields = self._get_fields(data_name)
data_raws = self._get_raws(data_name)
with open(file_name, mode="a") as fd:
table_raws_len = len(table_raws)
for raw_index in range(table_raws_len):
if (raw_index == table_raws_len - 1) or (raw_index % 1000 == 0):
sys.stdout.write(
"\rdump json "
+ str(raw_index)
+ ":"
+ str(len(table_raws))
+ " " * 100
)
vals_list = []
values = list(table_raws[raw_index])
for value_index in range(len(values)):
label = table_fields[value_index]
value = values[value_index]
if name_ptrn.search(label):
value = sub_ptrn.sub(r"", value)
if label != '"Index"':
if label == '"dur"' and value == 0:
vals_list.append('%s:"%s"' % (label, "1"))
elif label == '"pid"' or label == '"tid"':
vals_list.append('%s:%d' % (label, value))
else:
vals_list.append('%s:"%s"' % (label, value))
args_list = []
data = list(data_raws[raw_index])
for value_index in range(len(data)):
label = data_fields[value_index]
value = data[value_index]
if label[:3] == '"__':
continue
if name_ptrn.search(label):
value = sub_ptrn.sub(r"", value)
if label != '"Index"' and label != '"roctx-range"':
args_list.append('%s:"%s"' % (label, value))
fd.write(
',{"ph":"%s",%s,\n "args":{\n %s\n }\n}\n'
% ("X", ",".join(vals_list), ",\n ".join(args_list))
)
sys.stdout.write("\n")
# execute query on DB
def execute(self, cmd):
cursor = self.connection.cursor()
cursor.execute(cmd)
# commit DB
def commit(self):
self.connection.commit()
# close DB
def close(self):
self.connection.commit()
self.connection.close()
# access DB
def get_raws(self, table_name):
cur = self.connection.cursor()
cur.execute("SELECT * FROM %s" % table_name)
return cur.fetchall()
# return CSV descriptor
# list of fields and dictionaly for the fields types
def _get_csv_descr(self, table_name, fd):
reader = csv.DictReader(fd)
field_names = reader.fieldnames
if not field_names[-1]:
field_names.pop()
field_types = {}
for entry in reader:
fields_left = [f for f in field_names if f not in field_types.keys()]
# all fields processed
if not fields_left:
break
for field in fields_left:
data = entry[field]
# need data for the field to be processed
if len(data) == 0:
continue
if data.isdigit():
field_types[field] = "INTEGER"
else:
field_types[field] = "TEXT"
if len(fields_left) > 0:
raise Exception("types not found for fields: ", fields_left)
return (field_names, field_types)
# add CSV table
def add_csv_table(self, table_name, file_name, extra=()):
with open(file_name, mode="r") as fd:
# get CSV table descriptor
descr = self._get_csv_descr(table_name, fd)
# reader to populate the table
fd.seek(0)
reader = csv.reader(fd)
reader.next()
table = self.add_table(table_name, descr, extra)
self.insert_table(table, reader)
##############################################################################################
+1242
View File
File diff suppressed because it is too large Load Diff
+113
View File
@@ -0,0 +1,113 @@
################################################################################
# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
#
# 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 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.
################################################################################
import os, sys, re
# gen_params() takes a text file like the output of rocminfo cmd and parses it into a map {key,value}
# where key is the param and value is the value of this param
# for example: Threadmodel : "posix"
# it also processes encompasing sections to generate a full param name such as (section names separated by '_'):
# "Agent2_PoolInfo_ISAInfo_ISA1_WorkgroupMaxSizeperDimension_x": "1024(0x400)",
def gen_params(txtfile):
fields = {}
counter = 0
parent_field = ""
nbr_indent = 0
nbr_indent_prev = 0
check_for_dims = False
with open(txtfile) as fp:
for line in fp:
me = re.match(r"\*\*\* Done \*\*\*", line) # Marks the end of cmd
if me:
parent_field = ""
nbr_indent = 0
nbr_indent_prev = 0
check_for_dims = False
continue
mv = re.match(
r"HCC clang version\s+(.*)", line
) # outlier: only line with a version number and no ':', special case
if mv:
key = "HCCclangversion"
val = mv.group(1)
counter = counter + 1
fields[(counter, key)] = val
continue
# Variable 'check_for_dims' is True for text like this:
# Workgroup Max Size per Dimension:
# x 1024(0x400)
# y 1024(0x400)
# z 1024(0x400)
if check_for_dims == True:
mc = re.match(r"\s*([x|y|z])\s+(.*)", line)
if mc:
key_sav = mc.group(1)
if parent_field != "":
key = parent_field + "." + mc.group(1)
else:
key = mc.group(1)
val = re.sub(r"\s+", "", mc.group(2))
counter = counter + 1
fields[(counter, key)] = val
if key_sav == "z":
check_for_dims = False
nbr_indent_prev = nbr_indent
mi = re.search(r"^(\s+)\w+.*", line)
md = re.search(":", line)
if mi:
nbr_indent = int(len(mi.group(1)) / 2) # indentation cnt
else:
if not md:
tmp = re.sub(r"\s+", "", line)
if tmp.isalnum():
parent_field = tmp
if nbr_indent < nbr_indent_prev:
go_back_parent = nbr_indent_prev - nbr_indent
for i in range(go_back_parent): # decrease as many levels up as needed
pos = parent_field.rfind(".")
if pos != -1:
parent_field = parent_field[:pos]
# Process lines such as :
# Segment: GLOBAL; FLAGS: KERNARG, FINE GRAINED
# Size: 131897644(0x7dc992c) KB
for lin in line.split(";"):
lin = re.sub(r"\s+", "", lin)
m = re.match(r"(.*):(.*)", lin)
if m:
key, val = m.group(1), m.group(2)
if parent_field != "":
key = parent_field + "." + key
if val == "":
mk = re.match(r".*Dimension", key)
if mk: # expect x,y,z on next 3 lines
check_for_dims = True
parent_field = key
else:
counter = counter + 1
fields[(counter, key)] = val
else:
if nbr_indent != nbr_indent_prev and not check_for_dims:
parent_field = parent_field + "." + lin.replace(":", "")
return fields
+103
View File
@@ -0,0 +1,103 @@
#!/bin/bash
################################################################################
# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
#
# 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 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.
################################################################################
timestamp=`date +%y%m%d_%H%M%S`
if [ $# = 0 ] ; then
echo "Usage: $0 <input text file> [output dir]"
exit -1
fi
input=$1
outdir=$2
if [ -z "$outdir" ] ; then
outdir="."
fi
range=""
kernel=""
gpu_index=""
parse() {
scan="$1"
index=0
while read -r line || [[ -n "$line" ]] ; do
line=`echo $line | sed "s/\s*#.*$//"`
if [ -z "$line" ] ; then
continue
fi
feature=`echo $line | sed -n "s/^\s*\([a-z]*\)\s*:.*$/\1/p"`
line=`echo $line | sed "s/^[^:]*:\s*//"`
line=`echo "$line" | sed -e "s/\s*=\s*/=/g" -e "s/\s*:\s*/:/g" -e "s/,\{1,\}/ /g" -e "s/\s\{1,\}/ /g" -e "s/\s*$//"`
if [ "$scan" = 0 ] ; then
line=`echo "$line" | sed -e "s/ /,/g"`
if [ "$feature" == "range" ] ; then
range=$line
fi
if [ "$feature" == "kernel" ] ; then
kernel=$line
fi
if [ "$feature" == "gpu" ] ; then
gpu_index=$line
fi
else
found=$(echo $feature | sed -n "/^\(pmc\|hip\|hsa\)$/ p")
if [ -n "$found" ] ; then
output=$outdir/input${index}.xml
header="# $timestamp '$output' generated with '$0 $*'"
echo $header > $output
if [ "$feature" == "pmc" ] ; then
line=`echo "$line" | sed -e "s/ /,/g"`
cat >> $output <<EOF
<metric range="$range" kernel="$kernel" gpu_index="$gpu_index"></metric>
<metric name=$line ></metric>
EOF
fi
if [ "$feature" == "hip" ] ; then
cat >> $output <<EOF
<trace name="HIP"><parameters api="$line"></parameters></trace>
EOF
fi
if [ "$feature" == "hsa" ] ; then
cat >> $output <<EOF
<trace name="HSA"><parameters api="$line"></parameters></trace>
EOF
fi
fi
fi
index=$((index + 1))
done < $input
}
parse 0
parse 1
exit 0
+109
View File
@@ -0,0 +1,109 @@
#!/bin/bash -e
################################################################################
# Copyright (c) 2018-2022 Advanced Micro Devices, Inc.
#
# 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 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.
################################################################################
SRC_DIR=$(dirname "$0")
COMPONENT="rocprofiler"
ROCM_PATH="${ROCM_PATH:=/opt/rocm}"
LD_RUNPATH_FLAG=" -Wl,--enable-new-dtags -Wl,--rpath,$ROCM_PATH/lib"
usage() {
echo -e "ROCProfiler Build Script Usage:"
echo -e "\nTo run ./run.sh PARAMs, PARAMs can be the following:"
echo -e "-h | --help For showing this message"
echo -e "-b | --build For compiling"
echo -e "-cb | --clean-build For full clean build"
echo -e "-acb | --asan-clean-build For compiling with ASAN library attached"
exit 1
}
while [ 1 ] ; do
if [[ "$1" = "-h" || "$1" = "--help" ]] ; then
usage
exit 1
elif [[ "$1" = "-b" || "$1" = "--build" ]] ; then
TO_CLEAN=no
shift
elif [[ "$1" = "-acb" || "$1" = "--asan-clean-build" ]] ; then
ASAN=True TO_CLEAN=yes
shift
elif [[ "$1" = "-cb" || "$1" = "--clean-build" ]] ; then
TO_CLEAN=yes
shift
elif [ "$1" = "--" ] ; then
shift
EXTRA_BUILD_ARGS=$@
break
elif [[ "$1" = "-"* ]] ; then
echo -e "Wrong option \"$1\", Please use the following options:\n"
usage
exit 1
else
break
fi
done
umask 022
if [ -z "$ROCPROFILER_ROOT" ]; then ROCPROFILER_ROOT=$SRC_DIR; fi
if [ -z "$BUILD_DIR" ] ; then BUILD_DIR=build; fi
if [ -z "$BUILD_TYPE" ] ; then BUILD_TYPE="RelWithDebInfo"; fi
if [ -z "$PACKAGE_ROOT" ] ; then PACKAGE_ROOT=$ROCM_PATH; fi
if [ -z "$PREFIX_PATH" ] ; then PREFIX_PATH=$PACKAGE_ROOT; fi
if [ -z "$HIP_VDI" ] ; then HIP_VDI=0; fi
if [ -n "$ROCM_RPATH" ] ; then LD_RUNPATH_FLAG=" -Wl,--enable-new-dtags -Wl,--rpath,${ROCM_RPATH}"; fi
if [ -z "$TO_CLEAN" ] ; then TO_CLEAN=yes; fi
if [ -z "$RUN_TEST" ] ; then RUN_TEST=no; fi
if [ -z "$ASAN" ] ; then ASAN=False; fi
if [ -z "$GPU_LIST" ] ; then GPU_LIST="gfx900 gfx906 gfx908 gfx90a gfx940 gfx941 gfx942 gfx1030 gfx1100 gfx1101 gfx1102 gfx1031 gfx1150 gfx1151 gfx1200 gfx1201"; fi
ROCPROFILER_ROOT=$(cd $ROCPROFILER_ROOT && echo $PWD)
if [ "$TO_CLEAN" = "yes" ] ; then rm -rf $BUILD_DIR; fi
cmake -B ${BUILD_DIR} ${ROCPROFILER_ROOT} \
-DROCPROFILER_BUILD_CI=1 \
-DROCPROFILER_BUILD_TESTS=1 \
-DROCPROFILER_BUILD_SAMPLES=1 \
-DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE:-'RelWithDebInfo'} \
-DCMAKE_MODULE_PATH="${ROCM_PATH}/hip/cmake;${ROCM_PATH}/lib/cmake" \
-DCMAKE_PREFIX_PATH="${ROCM_INSTALL_PATH}/llvm;$PREFIX_PATH" \
-DCMAKE_INSTALL_PREFIX="$PACKAGE_ROOT" \
-DCMAKE_SHARED_LINKER_FLAGS="$LD_RUNPATH_FLAG" \
-DCMAKE_INSTALL_RPATH=${ROCM_RPATH} \
-DCMAKE_INSTALL_LIBDIR=lib \
-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=FALSE \
-DGPU_TARGETS="$GPU_LIST" \
-DCPACK_PACKAGING_INSTALL_PREFIX=$PACKAGE_ROOT \
-DCPACK_GENERATOR=${CPACKGEN:-'DEB;RPM'} \
-DCPACK_OBJCOPY_EXECUTABLE="${PACKAGE_ROOT}/llvm/bin/llvm-objcopy" \
-DCPACK_READELF_EXECUTABLE="${PACKAGE_ROOT}/llvm/bin/llvm-readelf" \
-DCPACK_STRIP_EXECUTABLE="${PACKAGE_ROOT}/llvm/bin/llvm-strip" \
-DCPACK_OBJDUMP_EXECUTABLE="${PACKAGE_ROOT}/llvm/bin/llvm-objdump" \
${EXTRA_BUILD_ARGS}
cmake --build "$BUILD_DIR" --target all --parallel $(nproc)
cmake --build "$BUILD_DIR" --target package --parallel $(nproc)
exit 0
@@ -0,0 +1,21 @@
# Try to find LIBDW
#
# Once found, this will define:
# - LIBDW_FOUND - system has libelf
# - LIBDW_INCLUDE_DIRS - the libelf include directory
# - LIBDW_LIBRARIES - Link these to use libelf
# - LIBDW_DEFINITIONS - Compiler switches required for using libelf
find_path(
FIND_LIBDW_INCLUDES
NAMES elfutils/libdw.h
PATHS /usr/include /usr/local/include)
find_library(FIND_LIBDW_LIBRARIES NAMES dw PATH /usr/lib /usr/local/lib)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LibDw DEFAULT_MSG FIND_LIBDW_INCLUDES
FIND_LIBDW_LIBRARIES)
mark_as_advanced(FIND_LIBDW_INCLUDES FIND_LIBDW_LIBRARIES)
set(LIBDW_INCLUDES ${FIND_LIBDW_INCLUDES})
set(LIBDW_LIBRARIES ${FIND_LIBDW_LIBRARIES})
@@ -0,0 +1,21 @@
# Try to find LIBELF
#
# Once found, this will define:
# - LIBELF_FOUND - system has libelf
# - LIBELF_INCLUDE_DIRS - the libelf include directory
# - LIBELF_LIBRARIES - Link these to use libelf
# - LIBELF_DEFINITIONS - Compiler switches required for using libelf
find_path(
FIND_LIBELF_INCLUDES
NAMES libelf.h
PATHS /usr/include /usr/include/libelf /usr/local/include /usr/local/include/libelf)
find_library(FIND_LIBELF_LIBRARIES NAMES elf PATH /usr/lib /usr/local/lib)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LibElf DEFAULT_MSG FIND_LIBELF_INCLUDES
FIND_LIBELF_LIBRARIES)
mark_as_advanced(FIND_LIBELF_INCLUDES FIND_LIBELF_LIBRARIES)
set(LIBELF_INCLUDES ${FIND_LIBELF_INCLUDES})
set(LIBELF_LIBRARIES ${FIND_LIBELF_LIBRARIES})
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.18.0 FATAL_ERROR)
if(NOT @GTEST_DISCOVER_TESTS_TARGET@_TESTS)
message(FATAL_ERROR "@GTEST_DISCOVER_TESTS_TARGET@_TESTS is not defined")
endif()
foreach(_TEST ${@GTEST_DISCOVER_TESTS_TARGET@_TESTS})
set_tests_properties(
${_TEST} PROPERTIES LABELS "@GTEST_DISCOVER_TESTS_LABELS@" ENVIRONMENT
"@GTEST_DISCOVER_TESTS_ENVIRONMENT@")
endforeach()
@@ -0,0 +1,76 @@
################################################################################
# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
#
# 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 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.
################################################################################
add_library(rocprofiler-build-flags INTERFACE)
add_library(rocprofiler::build-flags ALIAS rocprofiler-build-flags)
target_compile_options(
rocprofiler-build-flags
INTERFACE $<$<COMPILE_LANGUAGE:C,CXX>:-W -Wall -Wextra -Wno-unused-parameter>
$<$<COMPILE_LANGUAGE:CXX>:-fms-extensions>
$<$<COMPILE_LANGUAGE:CXX>:$<$<CXX_COMPILER_ID:Clang>:-ferror-limit=1000000>>
)
target_compile_definitions(rocprofiler-build-flags INTERFACE NEW_TRACE_API=1)
# Enable debug trace
if(ROCPROFILER_DEBUG_TRACE)
target_compile_definitions(rocprofiler-build-flags INTERFACE DEBUG_TRACE=1)
endif()
# Enable direct loading of AQL-profile HSA extension
if(ROCPROFILER_LD_AQLPROFILE)
target_compile_definitions(rocprofiler-build-flags INTERFACE ROCP_LD_AQLPROFILE=1)
endif()
if(NOT DEFINED ROCM_PATH)
set(ROCM_PATH "/opt/rocm" CACHE STRING "Default ROCM installation directory")
endif()
# Find hsa-runtime
find_package(
hsa-runtime64 CONFIG REQUIRED
HINTS ${CMAKE_PREFIX_PATH}
PATHS ${ROCM_PATH}
PATH_SUFFIXES lib/cmake/hsa-runtime64)
# Include path: /opt/rocm-ver/include. Go up one level to get ROCm path
get_filename_component(ROCM_ROOT_DIR ${ROCM_PATH}/include DIRECTORY)
# Basic Tool Chain Information
message("----------Build-Type: ${CMAKE_BUILD_TYPE}")
message("------------Compiler: ${CMAKE_CXX_COMPILER}")
message("----Compiler-Version: ${CMAKE_CXX_COMPILER_VERSION}")
message("-------ROCM_ROOT_DIR: ${ROCM_ROOT_DIR}")
message("-----CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
message("---CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
message("---------GPU_TARGETS: ${GPU_TARGETS}")
if("${ROCM_ROOT_DIR}" STREQUAL "")
message(FATAL_ERROR "ROCM_ROOT_DIR is not found.")
endif()
find_library(
HSA_AMD_AQLPROFILE_LIBRARY
NAMES hsa-amd-aqlprofile64
HINTS ${CMAKE_PREFIX_PATH}
PATHS ${ROCM_ROOT_DIR}
PATH_SUFFIXES lib REQUIRED)
@@ -0,0 +1,103 @@
# ------------------------------------------------------------------------------#
#
# creates following targets to format code:
# - format
# - format-source
# - format-cmake
# - format-python
# - format-rocprofiler-source
# - format-rocprofiler-cmake
# - format-rocprofiler-python
#
# ------------------------------------------------------------------------------#
include_guard(GLOBAL)
find_program(ROCPROFILER_CLANG_FORMAT_EXE NAMES clang-format-11 clang-format-mp-11)
find_program(ROCPROFILER_CMAKE_FORMAT_EXE NAMES cmake-format)
find_program(ROCPROFILER_BLACK_FORMAT_EXE NAMES black)
if(ROCPROFILER_CLANG_FORMAT_EXE
OR ROCPROFILER_BLACK_FORMAT_EXE
OR ROCPROFILER_CMAKE_FORMAT_EXE)
add_custom_target(format-rocprofiler)
if(NOT TARGET format)
add_custom_target(format)
endif()
foreach(_TYPE source python cmake)
if(NOT TARGET format-${_TYPE})
add_custom_target(format-${_TYPE})
endif()
endforeach()
set(rocp_sources)
set(rocp_headers)
set(rocp_cmake_files)
set(rocp_python_files)
foreach(_DIR include src plugin samples test tests-v2 script cmake_modules)
foreach(_TYPE headers sources cmake_files python_files)
set(${_TYPE})
endforeach()
file(GLOB_RECURSE headers ${PROJECT_SOURCE_DIR}/${_DIR}/*.h)
file(GLOB_RECURSE sources ${PROJECT_SOURCE_DIR}/${_DIR}/*.cpp)
file(GLOB_RECURSE cmake_files ${PROJECT_SOURCE_DIR}/${_DIR}/*CMakeLists.txt
${PROJECT_SOURCE_DIR}/${_DIR}/*.cmake)
file(GLOB_RECURSE python_files ${PROJECT_SOURCE_DIR}/${_DIR}/*.py)
foreach(_TYPE headers sources cmake_files python_files)
list(APPEND rocp_${_TYPE} ${${_TYPE}})
endforeach()
endforeach()
if(ROCPROFILER_CLANG_FORMAT_EXE)
add_custom_target(
format-rocprofiler-source
${ROCPROFILER_CLANG_FORMAT_EXE} -i ${rocp_sources} ${rocp_headers}
COMMENT
"[rocprofiler] Running source formatter ${ROCPROFILER_CLANG_FORMAT_EXE}..."
)
endif()
if(ROCPROFILER_BLACK_FORMAT_EXE)
add_custom_target(
format-rocprofiler-python
${ROCPROFILER_BLACK_FORMAT_EXE} -q ${rocp_python_files}
COMMENT
"[rocprofiler] Running Python formatter ${ROCPROFILER_BLACK_FORMAT_EXE}..."
)
if(NOT TARGET format-python)
add_custom_target(format-python)
endif()
endif()
if(ROCPROFILER_CMAKE_FORMAT_EXE)
add_custom_target(
format-rocprofiler-cmake
${ROCPROFILER_CMAKE_FORMAT_EXE} -i ${rocp_cmake_files}
COMMENT
"[rocprofiler] Running CMake formatter ${ROCPROFILER_CMAKE_FORMAT_EXE}..."
)
if(NOT TARGET format-cmake)
add_custom_target(format-cmake)
endif()
endif()
foreach(_TYPE source python cmake)
if(TARGET format-rocprofiler-${_TYPE})
add_dependencies(format-rocprofiler format-rocprofiler-${_TYPE})
add_dependencies(format-${_TYPE} format-rocprofiler-${_TYPE})
endif()
endforeach()
foreach(_TYPE source python)
if(TARGET format-rocprofiler-${_TYPE})
add_dependencies(format format-rocprofiler-${_TYPE})
endif()
endforeach()
else()
message(
STATUS
"no formatting tools (clang-format-11/black/cmake-format) could not be found. formatting build targets not available."
)
endif()
@@ -0,0 +1,30 @@
include_guard(DIRECTORY)
# ----------------------------------------------------------------------------------------#
#
# Clang Tidy
#
# ----------------------------------------------------------------------------------------#
if(ROCPROFILER_ENABLE_CLANG_TIDY)
find_program(ROCPROFILER_CLANG_TIDY_COMMAND NAMES clang-tidy)
if(NOT ROCPROFILER_CLANG_TIDY_COMMAND)
message(
WARNING "ROCPROFILER_ENABLE_CLANG_TIDY is ON but clang-tidy is not found!")
set(ROCPROFILER_ENABLE_CLANG_TIDY OFF)
else()
set(CMAKE_CXX_CLANG_TIDY ${ROCPROFILER_CLANG_TIDY_COMMAND}
-header-filter=${PROJECT_SOURCE_DIR}/.*)
# Create a preprocessor definition that depends on .clang-tidy content so the
# compile command will change when .clang-tidy changes. This ensures that a
# subsequent build re-runs clang-tidy on all sources even if they do not otherwise
# need to be recompiled. Nothing actually uses this definition. We add it to
# targets on which we run clang-tidy just to get the build dependency on the
# .clang-tidy file.
file(SHA1 ${PROJECT_SOURCE_DIR}/.clang-tidy clang_tidy_sha1)
set(CLANG_TIDY_DEFINITIONS "CLANG_TIDY_SHA1=${clang_tidy_sha1}")
unset(clang_tidy_sha1)
endif()
endif()
@@ -0,0 +1,139 @@
if("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}")
message(STATUS "")
message(STATUS "rocprofiler does not support in-source builds.")
message(STATUS "Delete CMakeCache.txt and CMakeFiles in ${PROJECT_SOURCE_DIR}")
message(STATUS "and run cmake with `-B <build-directory>`")
message(STATUS "")
message(FATAL_ERROR "In-source build detected.")
endif()
option(ROCPROFILER_BUILD_TESTS "Enable building the tests" OFF)
option(ROCPROFILER_BUILD_SAMPLES "Enable building the code samples" OFF)
# CLI and FILE plugins are always built
foreach(_PLUGIN "ATT" "CTF" "PERFETTO")
option(ROCPROFILER_BUILD_PLUGIN_${_PLUGIN} "Enable building the ${_PLUGIN} plugin" ON)
endforeach()
option(ROCPROFILER_DEBUG_TRACE "Enable debug tracing" OFF)
mark_as_advanced(ROCPROFILER_DEBUG_TRACE)
option(ROCPROFILER_LD_AQLPROFILE "Enable direct loading of AQL-profile HSA extension" OFF)
mark_as_advanced(ROCPROFILER_LD_AQLPROFILE)
option(ROCPROFILER_BUILD_CI "Enable continuous integration additions" OFF)
mark_as_advanced(ROCPROFILER_BUILD_CI)
option(ROCPROFILER_ENABLE_CLANG_TIDY "Enable clang-tidy checks" OFF)
mark_as_advanced(ROCPROFILER_ENABLE_CLANG_TIDY)
set(ROCPROFILER_BUILD_TYPES "Release" "RelWithDebInfo" "Debug" "MinSizeRel" "Coverage")
# export compile commands in the project
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE
"Release"
CACHE STRING "Build type" FORCE)
endif()
if(NOT CMAKE_BUILD_TYPE IN_LIST ROCPROFILER_BUILD_TYPES)
message(
FATAL_ERROR
"Unsupported build type '${CMAKE_BUILD_TYPE}'. Options: ${ROCPROFILER_BUILD_TYPES}"
)
endif()
if(ROCPROFILER_BUILD_CI)
foreach(_BUILD_TYPE ${ROCPROFILER_BUILD_TYPES})
string(TOUPPER "${_BUILD_TYPE}" _BUILD_TYPE)
# remove NDEBUG preprocessor def so that asserts are triggered
string(REGEX REPLACE ".DNDEBUG" "" CMAKE_C_FLAGS_${_BUILD_TYPE}
"${CMAKE_C_FLAGS_${_BUILD_TYPE}}")
string(REGEX REPLACE ".DNDEBUG" "" CMAKE_CXX_FLAGS_${_BUILD_TYPE}
"${CMAKE_CXX_FLAGS_${_BUILD_TYPE}}")
endforeach()
endif()
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${ROCPROFILER_BUILD_TYPES}")
endif()
set(ROCPROFILER_MEMCHECK
""
CACHE STRING "Memory checker type")
mark_as_advanced(ROCPROFILER_MEMCHECK)
# ASAN is defined by testing team on Jenkins
if(ASAN)
set(ROCPROFILER_MEMCHECK
"AddressSanitizer"
CACHE STRING "Memory checker type (forced by ASAN defined)" FORCE)
endif()
set(ROCPROFILER_MEMCHECK_TYPES "ThreadSanitizer" "AddressSanitizer" "LeakSanitizer"
"MemorySanitizer" "UndefinedBehaviorSanitizer")
if(ROCPROFILER_MEMCHECK AND NOT ROCPROFILER_MEMCHECK IN_LIST ROCPROFILER_MEMCHECK_TYPES)
message(
FATAL_ERROR
"Unsupported memcheck type '${ROCPROFILER_MEMCHECK}'. Options: ${ROCPROFILER_MEMCHECK_TYPES}"
)
endif()
set_property(CACHE ROCPROFILER_MEMCHECK PROPERTY STRINGS "${ROCPROFILER_MEMCHECK_TYPES}")
add_library(rocprofiler-memcheck INTERFACE)
add_library(rocprofiler::memcheck ALIAS rocprofiler-memcheck)
function(rocprofiler_add_memcheck_flags _TYPE)
target_compile_options(
rocprofiler-memcheck INTERFACE $<BUILD_INTERFACE:-g3 -Og -fno-omit-frame-pointer
-fsanitize=${_TYPE}>)
target_link_options(rocprofiler-memcheck INTERFACE
$<BUILD_INTERFACE:-fsanitize=${_TYPE} -Wl,--no-undefined>)
endfunction()
function(rocprofiler_set_memcheck_env _TYPE _LIB_BASE)
set(_LIBS ${_LIB_BASE})
foreach(_N 6 5 4 3 2 1 0)
list(
APPEND _LIBS
${CMAKE_SHARED_LIBRARY_PREFIX}${_LIB_BASE}${CMAKE_SHARED_LIBRARY_SUFFIX}.${_N}
)
endforeach()
foreach(_LIB ${_LIBS})
if(NOT ${_TYPE}_LIBRARY)
find_library(${_TYPE}_LIBRARY NAMES ${_LIB} ${ARGN})
endif()
endforeach()
target_link_libraries(rocprofiler-memcheck INTERFACE ${_LIB_BASE})
if(${_TYPE}_LIBRARY)
set(ROCPROFILER_MEMCHECK_PRELOAD_ENV
"LD_PRELOAD=${${_TYPE}_LIBRARY};LD_LIBRARY_PATH=${PROJECT_BINARY_DIR}/lib:$ENV{LD_LIBRARY_PATH}"
CACHE INTERNAL "LD_PRELOAD env variable for tests" FORCE)
endif()
endfunction()
# always unset so that it doesn't preload if memcheck disabled
unset(ROCPROFILER_MEMCHECK_PRELOAD_ENV CACHE)
if(ROCPROFILER_MEMCHECK STREQUAL "AddressSanitizer")
rocprofiler_add_memcheck_flags("address")
rocprofiler_set_memcheck_env("${ROCPROFILER_MEMCHECK}" "asan")
elseif(ROCPROFILER_MEMCHECK STREQUAL "LeakSanitizer")
rocprofiler_add_memcheck_flags("leak")
rocprofiler_set_memcheck_env("${ROCPROFILER_MEMCHECK}" "lsan")
elseif(ROCPROFILER_MEMCHECK STREQUAL "MemorySanitizer")
rocprofiler_add_memcheck_flags("memory")
elseif(ROCPROFILER_MEMCHECK STREQUAL "ThreadSanitizer")
rocprofiler_add_memcheck_flags("thread")
rocprofiler_set_memcheck_env("${ROCPROFILER_MEMCHECK}" "tsan")
elseif(ROCPROFILER_MEMCHECK STREQUAL "UndefinedBehaviorSanitizer")
rocprofiler_add_memcheck_flags("undefined")
rocprofiler_set_memcheck_env("${ROCPROFILER_MEMCHECK}" "ubsan")
endif()
@@ -0,0 +1,263 @@
################################################################################
# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
#
# 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 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.
################################################################################
# Parses the VERSION_STRING variable and places the first, second and third number values
# in the major, minor and patch variables.
function(rocprofiler_parse_version VERSION_STRING)
string(FIND ${VERSION_STRING} "-" STRING_INDEX)
if(${STRING_INDEX} GREATER -1)
math(EXPR STRING_INDEX "${STRING_INDEX} + 1")
string(SUBSTRING ${VERSION_STRING} ${STRING_INDEX} -1 VERSION_BUILD)
endif()
string(REGEX MATCHALL "[0123456789]+" VERSIONS ${VERSION_STRING})
list(LENGTH VERSIONS VERSION_COUNT)
if(${VERSION_COUNT} GREATER 0)
list(GET VERSIONS 0 MAJOR)
set(VERSION_MAJOR
${MAJOR}
PARENT_SCOPE)
set(TEMP_VERSION_STRING "${MAJOR}")
endif()
if(${VERSION_COUNT} GREATER 1)
list(GET VERSIONS 1 MINOR)
set(VERSION_MINOR
${MINOR}
PARENT_SCOPE)
set(TEMP_VERSION_STRING "${TEMP_VERSION_STRING}.${MINOR}")
endif()
if(${VERSION_COUNT} GREATER 2)
list(GET VERSIONS 2 PATCH)
set(VERSION_PATCH
${PATCH}
PARENT_SCOPE)
set(TEMP_VERSION_STRING "${TEMP_VERSION_STRING}.${PATCH}")
endif()
if(DEFINED VERSION_BUILD)
set(VERSION_BUILD
"${VERSION_BUILD}"
PARENT_SCOPE)
endif()
set(VERSION_STRING
"${TEMP_VERSION_STRING}"
PARENT_SCOPE)
endfunction()
# Gets the current version of the repository using versioning tags and git describe.
# Passes back a packaging version string and a library version string.
function(rocprofiler_get_version DEFAULT_VERSION_STRING)
rocprofiler_parse_version(${DEFAULT_VERSION_STRING})
find_program(GIT NAMES git)
if(GIT)
execute_process(
COMMAND "git describe --dirty --long --match [0-9]* 2>/dev/null"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_TAG_STRING
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE RESULT)
if(${RESULT} EQUAL 0)
rocprofiler_parse_version(${GIT_TAG_STRING})
endif()
endif()
set(VERSION_STRING
"${VERSION_STRING}"
PARENT_SCOPE)
set(VERSION_MAJOR
"${VERSION_MAJOR}"
PARENT_SCOPE)
set(VERSION_MINOR
"${VERSION_MINOR}"
PARENT_SCOPE)
set(VERSION_PATCH
"${VERSION_PATCH}"
PARENT_SCOPE)
set(VERSION_BUILD
"${VERSION_BUILD}"
PARENT_SCOPE)
endfunction()
# ----------------------------------------------------------------------------------------#
# macro rocprofiler_checkout_git_submodule()
#
# Run "git submodule update" if a file in a submodule does not exist
#
# ARGS: RECURSIVE (option) -- add "--recursive" flag RELATIVE_PATH (one value) --
# typically the relative path to submodule from PROJECT_SOURCE_DIR WORKING_DIRECTORY (one
# value) -- (default: PROJECT_SOURCE_DIR) TEST_FILE (one value) -- file to check for
# (default: CMakeLists.txt) ADDITIONAL_CMDS (many value) -- any addition commands to pass
#
function(ROCPROFILER_CHECKOUT_GIT_SUBMODULE)
# parse args
cmake_parse_arguments(
CHECKOUT "RECURSIVE"
"RELATIVE_PATH;WORKING_DIRECTORY;TEST_FILE;REPO_URL;REPO_BRANCH"
"ADDITIONAL_CMDS" ${ARGN})
if(NOT CHECKOUT_WORKING_DIRECTORY)
set(CHECKOUT_WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
endif()
if(NOT CHECKOUT_TEST_FILE)
set(CHECKOUT_TEST_FILE "CMakeLists.txt")
endif()
find_package(Git)
set(_DIR "${CHECKOUT_WORKING_DIRECTORY}/${CHECKOUT_RELATIVE_PATH}")
# ensure the (possibly empty) directory exists
if(NOT EXISTS "${_DIR}")
if(NOT CHECKOUT_REPO_URL)
message(FATAL_ERROR "submodule directory does not exist")
endif()
endif()
# if this file exists --> project has been checked out if not exists --> not been
# checked out
set(_TEST_FILE "${_DIR}/${CHECKOUT_TEST_FILE}")
# assuming a .gitmodules file exists
set(_SUBMODULE "${PROJECT_SOURCE_DIR}/.gitmodules")
set(_TEST_FILE_EXISTS OFF)
if(EXISTS "${_TEST_FILE}" AND NOT IS_DIRECTORY "${_TEST_FILE}")
set(_TEST_FILE_EXISTS ON)
endif()
if(_TEST_FILE_EXISTS)
return()
endif()
find_package(Git REQUIRED)
set(_SUBMODULE_EXISTS OFF)
if(EXISTS "${_SUBMODULE}" AND NOT IS_DIRECTORY "${_SUBMODULE}")
set(_SUBMODULE_EXISTS ON)
endif()
set(_HAS_REPO_URL OFF)
if(NOT "${CHECKOUT_REPO_URL}" STREQUAL "")
set(_HAS_REPO_URL ON)
endif()
# if the module has not been checked out
if(NOT _TEST_FILE_EXISTS AND _SUBMODULE_EXISTS)
# perform the checkout
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule update --init ${_RECURSE}
${CHECKOUT_ADDITIONAL_CMDS} ${CHECKOUT_RELATIVE_PATH}
WORKING_DIRECTORY ${CHECKOUT_WORKING_DIRECTORY}
RESULT_VARIABLE RET)
# check the return code
if(RET GREATER 0)
set(_CMD "${GIT_EXECUTABLE} submodule update --init ${_RECURSE}
${CHECKOUT_ADDITIONAL_CMDS} ${CHECKOUT_RELATIVE_PATH}")
message(STATUS "function(rocprofiler_checkout_git_submodule) failed.")
message(FATAL_ERROR "Command: \"${_CMD}\"")
else()
set(_TEST_FILE_EXISTS ON)
endif()
endif()
if(NOT _TEST_FILE_EXISTS AND _HAS_REPO_URL)
message(
STATUS
"Cloning '${CHECKOUT_REPO_URL}' into ${CHECKOUT_WORKING_DIRECTORY}/${CHECKOUT_RELATIVE_PATH}..."
)
# remove the existing directory
if(EXISTS "${_DIR}")
execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${_DIR})
endif()
# perform the checkout
execute_process(
COMMAND ${GIT_EXECUTABLE} clone ${CHECKOUT_ADDITIONAL_CMDS}
${CHECKOUT_REPO_URL} ${CHECKOUT_RELATIVE_PATH}
WORKING_DIRECTORY ${CHECKOUT_WORKING_DIRECTORY}
RESULT_VARIABLE RET_CLONE)
if(NOT RET_CLONE EQUAL 0)
message(
SEND_ERROR
"Failed to clone ${CHECKOUT_REPO_URL} into ${CHECKOUT_WORKING_DIRECTORY}/${CHECKOUT_RELATIVE_PATH}"
)
return()
endif()
if(CHECKOUT_REPO_BRANCH)
execute_process(
COMMAND ${GIT_EXECUTABLE} checkout ${CHECKOUT_REPO_BRANCH}
WORKING_DIRECTORY ${CHECKOUT_WORKING_DIRECTORY}/${CHECKOUT_RELATIVE_PATH}
RESULT_VARIABLE RET_BRANCH)
if(NOT RET_BRANCH EQUAL 0)
message(
SEND_ERROR
"Failed to checkout '${CHECKOUT_REPO_BRANCH}' for ${CHECKOUT_REPO_URL} in ${CHECKOUT_WORKING_DIRECTORY}/${CHECKOUT_RELATIVE_PATH}"
)
return()
endif()
endif()
# perform the submodule update
if(CHECKOUT_RECURSIVE
AND EXISTS "${_DIR}"
AND IS_DIRECTORY "${_DIR}")
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule update --init ${_RECURSE}
WORKING_DIRECTORY ${_DIR}
RESULT_VARIABLE RET_RECURSIVE)
if(NOT RET_RECURSIVE EQUAL 0)
message(
SEND_ERROR
"Failed to update submodules for ${CHECKOUT_REPO_URL} in ${CHECKOUT_WORKING_DIRECTORY}/${CHECKOUT_RELATIVE_PATH}"
)
return()
endif()
endif()
set(_TEST_FILE_EXISTS ON)
endif()
if(NOT EXISTS "${_TEST_FILE}" OR NOT _TEST_FILE_EXISTS)
message(
FATAL_ERROR
"Error checking out submodule: '${CHECKOUT_RELATIVE_PATH}' to '${_DIR}'")
endif()
endfunction()
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,837 @@
# ROC Profiler Library Specification
ROC Profiler API version 7
## 1. High level overview
```
The goal of the implementation is to provide a HW specific low-level performance analysis
interface for profiling of GPU compute applications. The profiling includes HW performance
counters with complex performance metrics and HW traces. The implementation distinguishes
two profiling features, metrics and traces. HW performance counters are treated as the basic
metrics and the formulas can be defined for derived complex metrics.
The library can be loaded by HSA runtime as a tool plugin and it can be loaded by higher
level HW independent performance analysis API like PAPI.
The library has C API and is based on AQLprofile AMD specific HSA extension.
1. The library provides methods to query the list of supported HW features.
2. The library provides profiling APIs to start, stop, read metrics results and tracing
data.
3. The library provides a intercepting API for collecting per-kernel profiling data for
the kernels
dispatched to HSA AQL queues.
4. The library provides mechanism to load profiling tool library plugin by env variable
ROCP_TOOL_LIB.
5. The library is responsible for allocation of the buffers for profiling and notifying
about output data buffer overflow for traces.
6. The library is implemented based on AMD specific AQLprofile HSA extension.
7. The library implementation is abstracted from the specific GFXIP.
8. The library implementation is extensible:
- Easy adding of counters and metrics
- Counters enumeration
- Counters and metrics can be dynamically configured using XML configuration files with
counters and metrics tables:
o Counters table entry, basic metric: counter name, block name, event id
o Complex metrics table entry: metric name, an expression for calculation the metric
from the counters
Metrics XML file example:
<gfx8>
<metric name=L1_CYCLES_COUNTER block=L1 event=0 >
<metric name=L1_MISS_COUNTER block=L1 event=33 >
. . .
</gfx8>
<gfx9>
. . .
</gfx9>
<global>
<metric name=L1_MISS_RATIO expr=L1_CYCLES_COUNT/ L1_MISS_COUNTER ></metric>
</global>
```
## 2. Environment
```
* HSA_TOOLS_LIB - required to be set to the name of rocprofiler library to be loaded by
HSA runtime
* ROCP_METRICS - path to the metrics XML file
* ROCP_TOOL_LIB - path to profiling tool library loaded by ROC Profiler
* ROCP_HSA_INTERCEPT - if set then HSA dispatches intercepting is enabled
```
## 3. General API
### 3.1. Description
```
The library supports method for getting the error number and error string of the last
failed library API call.
To check the conformance of used library APi header and the library binary the version
macros and API methods can be used.
Returning the error and error string methods:
- rocprofiler_error_string - method for returning the error string
Library version:
- ROCPROFILER_VERSION_MAJOR - API major version macro
- ROCPROFILER_VERSION_MINOR - API minor version macro
- rocprofiler_version_major - library major version
- rocprofiler_version_minor - library minor version
```
### 3.2. Returning the error and error string methods
```
hsa_status_t rocprofiler_error_string(const char** str);
```
### 3.3. Library version
```
The library provides back compatibility if the library major version is less or equal
then the API major version macro.
API version macros defined in the library API header 'rocprofiler.h':
ROCPROFILER_VERSION_MAJOR
ROCPROFILER_VERSION_MINOR
Methods to check library major and minor venison:
uint32_t rocprofiler_major_version();
uint32_t rocprofiler_minor_version();
```
## 4. Backend API
### 4.1. Description
```
The library provides the methods to open/close profiling context, to start, stop and read
HW performance counters and traces, to intercept kernel dispatches to collect per-kernel
profiling data. Also the library provides methods to calculate complex performance metrics
and to query the list of available metrics. The library distinguishes two profiling features,
metrics and traces, where HW performance counters are treated as the basic metrics. To check
if there was an error the library methods return HSA standard status code.
For a given context the profiling can be started/stopped and counters sampled in standalone
mode or profiling can be initiated by intercepting the kernel dispatches with registering
a dispatch callback.
For counters sampling, which is the usage model of higher level APIs like PAPI,
the start/stop/read APIs should be used.
For collecting per-kernel data for the submitted to HSA queues kernels the dispatch callback
API should be used.
The library provides back compatibility if the library major version is less or equal.
Returned API status:
- hsa_status_t - HSA status codes are used from hsa.h header
Loading and Configuring, loadable plugin on-load/unload methods:
- rocprofiler_settings_t global properties
- OnLoadTool
- OnLoadToolProp
- OnUnloadTool
Info API:
- rocprofiler_info_kind_t - profiling info kind
- rocprofiler_info_query_t - profiling info query
- rocprofiler_info_data_t - profiling info data
- rocprofiler_get_info - return the info for a given info kind
- rocprofiler_iterate_inf_ - iterate over the info for a given info kind
- rocprofiler_query_info - iterate over the info for a given info query
Context API:
- rocprofiler_t - profiling context handle
- rocprofiler_feature_kind_t - profiling feature kind
- rocprofiler_feature_parameter_t - profiling feature parameter
- rocprofiler_data_kind_t - profiling data kind
- rocprofiler_data_t - profiling data
- rocprofiler_feature_t - profiling feature
- rocprofiler_mode_t - profiling modes
- rocprofiler_properties_t - profiler properties
- rocprofiler_open - open new profiling context
- rocprofiler_close - close profiling context and release all allocated resources
- rocprofiler_group_count - return profiling groups count
- rocprofiler_get_group - return profiling group for a given index
- rocprofiler_get_metrics - method for calculating the metrics data
- rocprofiler_iterate_trace_data - method for iterating output trace data instances
- rocprofiler_time_id_t - supported time value ID enumeration
- rocprofiler_get_time return time for a given time ID and profiling timestamp value
Sampling API:
- rocprofiler_start - start profiling
- rocprofiler_stop - stop profiling
- rocprofiler_read - read profiling data to the profiling features objects
- rocprofiler_get_data - wait for profiling data
Group versions of start/stop/read/get_data methods:
o rocprofiler_group_start
o rocprofiler_group_stop
o rocprofiler_group_read
o rocprofiler_group_get_data
Intercepting API:
- rocprofiler_callback_t - profiling callback type
- rocprofiler_callback_data_t - profiling callback data type
- rocprofiler_dispatch_record_t dispatch record
- rocprofiler_queue_callbacks_t queue callbacks, dispatch/destroy
- rocprofiler_set_queue_callbacks - set queue kernel dispatch and queue destroy callbacks
- rocprofiler_remove_queue_callbacks - remove queue callbacks
Context pool API:
- rocprofiler_pool_t context pool handle
- rocprofiler_pool_entry_t context pool entry
- rocprofiler_pool_properties_t context pool properties
- rocprofiler_pool_handler_t context pool completion handler
- rocprofiler_pool_open - context pool open
- rocprofiler_pool_close - context pool close
- rocprofiler_pool_fetch fetch and empty context entry to pool
- rocprofiler_pool_release release a context entry
- rocprofiler_pool_iterate iterated fetched context entries
- rocprofiler_pool_flush flush completed context entries
```
### 4.2. Loading and Configuring
```
Loading and Configuring
The profiling properties can be set by profiler plugin on loading by ROC runtime.
The profiler library plugin can be set by ROCP_TOOL_LIB env var.
Global properties:
typedef struct {
uint32_t intercept_mode;
uint64_t timeout;
uint32_t timestamp_on;
} rocprofiler_settings_t;
On load/unload methods defined in profiling tool library loaded by ROCP_TOOL_LIB env var:
extern "C" void OnLoadTool();
extern "C" void OnLoadToolProp(rocprofiler_settings_t* settings);
extern "C" void OnUnloadTool();
```
### 4.3. Info API
```
The profiling metrics are defined by name and the traces are defined by name and parameters.
All supported features can be iterated using 'iterate_info/query_info' methods. The counter
names are defined in counters table configuration file, each counter has a unique name and
defined by block name and event id. The traces and trace parameters names are same as in
the hardware documentation and the parameters codes are rocprofiler_feature_parameter_t values,
see below in the "Context API" section.
Profiling info kind:
typedef enum {
ROCPROFILER_INFO_KIND_METRIC = 0, // metric info
ROCPROFILER_INFO_KIND_METRIC_COUNT = 1, // metrics count
ROCPROFILER_INFO_KIND_TRACE = 2, // trace info
ROCPROFILER_INFO_KIND_TRACE_COUNT = 3, // traces count
} rocprofiler_info_kind_t;
Profiling info data:
typedef struct {
rocprofiler_info_kind_t kind; // info data kind
union {
struct {
const char* name; // metric name
uint32_t instances; // instances number
const char* expr; // metric expression, NULL for basic counters
const char* description; // metric description
const char* block_name; // block name
uint32_t block_counters; // number of block counters
} metric;
struct {
const char* name; // trace name
const char* description; // trace description
uint32_t parameter_count; // supported by the trace number
// parameters
} trace;
};
} rocprofiler_info_data_t;
Return info for a given info kind:
has_status_t rocprofiler_get_info(
const hsa_agent_t* agent, // [in] GPU handle, NULL for all
// GPU agents
rocprofiler info_kind_t kind, // kind of iterated info
void *data); // data passed to callback
Iterate over the info for a given info kind, and invoke an application-defined callback on
every iteration:
has_status_t rocprofiler_iterate_info(
const hsa_agent_t* agent, // [in] GPU handle, NULL for all
// GPU agents
rocprofiler info_kind_t kind, // kind of iterated info
hsa_status_t (*callback)(const rocprofiler_info_data_t info, void *data), // callback
void *data);
Iterate over the info for a given info query, and invoke an application-defined callback on
every iteration. The query
fields set to NULL define the query wildcard:
has_status_t rocprofiler_query_info(
const hsa_agent_t* agent, // [in] GPU handle, NULL for all
// GPU agents
rocprofiler info_kind_t kind, // kind of iterated info
rocprofiler_info_data_t query, // info query
hsa_status_t (*callback)(const rocprofiler_info_data_t info, void *data), // callback
void *data); // data passed to callback
```
### 4.4. Context API
```
Profiling context is accumulating all profiling information including profiling features
which carry profiling data, required buffers for profiling command packets and output data.
The context can be created and deleted by the library open/close methods. By deleting
the context all accumulated by the library resources associated with this context will be
released. If it is required more than one run to collect all requested counters data then
data for all profiling groups should be collected and then the metrics can be calculated by
loading the saved groups' data to the profiling context. Saving and loading of the groups
data is responsibility of the tool. The groups are automatically identified on the profiling
context open and there is API to access them, see the "Profiling groups" section below.
Profiling context handle:
typename rocprofiler_t;
Profiling feature kind:
typedef enum {
ROCPROFILER_FEATURE_KIND_METRIC = 0, // metric
ROCPROFILER_FEATURE_KIND_TRACE = 1 // trace
} rocprofiler_feature_kind_t;
Profiling feature parameter:
typedef hsa_ven_amd_aqlprofile_parameter_t rocprofiler_feature_parameter_t;
Profiling data kind:
typedef enum {
ROCPROFILER_DATA_KIND_UNINIT = 0, // data uninitialized
ROCPROFILER_DATA_KIND_INT32 = 1, // 32bit integer
ROCPROFILER_DATA_KIND_INT64 = 2, // 64bit integer
ROCPROFILER_DATA_KIND_FLOAT = 3, // float single-precision result
ROCPROFILER_DATA_KIND_DOUBLE = 4, // float double-precision result
ROCPROFILER_DATA_KIND_BYTES = 5 // trace output as a bytes array
} rocprofiler_data_kind_t;
Profiling data:
typedef struct {
rocprofiler_data_kind_t kind; // result kind
union {
uint32_t result_int32; // 32bit integer result
uint64_t result_int64; // 64bit integer result
float result_float; // float single-precision result
double result_double; // float double-precision result
typedef struct {
void* ptr; // pointer
uint32_t size; // byte size
uint32_t instances; // number of trace instances
} result_bytes; // data by ptr and byte size
};
} rocprofiler_data_t;
Profiling feature:
typedef struct {
rocprofiler_feature_kind_t type; // feature type
const char* name; // feature name
const rocprofiler_feature_parameter_t* parameters; // feature parameters
uint32_t parameter_count; // feature parameter count
rocprofiler_data_t* data; // profiling data
} rocprofiler_feature_t;
Profiling mode masks:
There are several modes which can be specified for the profiling context.
STANDALONE mode can be used for the counters sampling in another then application context
to support statistical system wide profiling. In this mode the profiling context supports
its own queue which can be created on the context open if the CREATEQUEUE mode also specified.
See also "Profiler properties" section below for the standalone mode queue properties.
The profiler supports several profiling groups for collecting profiling data in several
runs and 'SINGLEGROUP' mode allows only one group and the context open will fail if more
groups are needed.
typedef enum {
ROCPROFILER_MODE_STANDALONE = 1, // standalone mode when ROC profiler
// supports own AQL queue
ROCPROFILER_MODE_CREATEQUEUE = 2, // profiler creates queue in STANDALONE mode
ROCPROFILER_MODE_SINGLEGROUP = 4 // profiler allows one group only and fails
// if more groups are needed
} rocprofiler_mode_t;
Context data readiness callback:
typedef void (*rocprofiler_context_callback_t)(
rocprofiler_group_t* group, // profiling group
void* arg); // callback arg
Profiler properties:
There are several properties which can be specified for the context. A callback can be
registered which will be called when the context data is ready. In standalone profiling mode
'ROCPROFILER_MODE_STANDALONE' the context supports its own queue and the queue can be set by
the property 'queue' or a queue will be created with the specified depth 'queue_depth' if mode
'ROCPROFILER_MODE_CREATEQUEUE' also specified.
typedef struct {
rocprofiler_context_callback_t callback; // callback on the context data readiness
void* callback_arg; // callback arg
has_queue_t* queue; // HSA queue for standalone mode
uint32_t queue_depth; // created queue depth,for create-queue mode
} rocprofiler_properties_t;
Open/close profiling context:
hsa_status_t rocprofiler_open(
hsa_agent_t agent, // GPU handle
rocprofiler_feature_t* features, // [in/out] profiling feature array
uint32_t feature_count, // profiling feature count
rocprofiler_t** context, // [out] profiling context handle
uint32_t mode, // profiling mode mask
rocprofiler_properties_t* properties); // profiler properties
hsa_status_t rocprofiler_close(
rocprofiler_t* context); // [in] profiling context
Profiling groups:
The profiler on the context open automatically identifies a required number of the application
runs to collect all data needed for all specified metrics and creates a metric group per each
run. Data for all profiling groups should be collected and then the metrics can be calculated
by loading the saved groups' data to the profiling context. Saving and loading of he groups
data is responsibility of the tool.
typedef struct {
uint32_t index; // profiling group index
rocprofiler_feature_t** features; // profiling features array
uint32_t feature_count; // profiling feature count
rocprofiler_t* context; // profiling context handle
} rocprofiler_group_t;
Return profiling groups count:
hsa_status_t rocprofiler_group_count(
rocprofiler_t* context); // [in/out] profiling context
uint32* count); // [out] profiling groups count
Return the profiling group for a given index:
hsa_status_t rocprofiler_get_group(
rocprofiler_t* context, // [in/out] profiling context,
// will be returned as
// a part of the group structure
uint32_t index, // [in] group index
rocprofiler_group_t* group); // [out] profiling group
Calculate metrics data. The data will be stored to the registered profiling features data fields:
After all profiling context data is ready the registered metrics can be calculated. The context
data readiness can be checked by 'get_data' API or using the context callback.
hsa_status_t rocprofiler_get_metrics(
rocprofiler_t* context); // [in/out] profiling context
Method for iterating trace data instances:
Trace data can have several instance, for example, one instance per Shader Engine.
hsa_status_t rocprofiler_iterate_trace_data(
const rocprofiler_t* contex, // [in] context object
hsa_ven_amd_aqlprofile_data_callback_t callback, // [in] callback to iterate
// the output data
void* callback_data); // [in/out] passed to callback data
Converting of profiling timestamp to time value for suported time ID.
Supported time value ID enumeration:
typedef enum {
ROCPROFILER_TIME_ID_CLOCK_REALTIME = 0, // Linux realtime clock time
ROCPROFILER_TIME_ID_CLOCK_MONOTONIC = 1, // Linux monotonic clock time
} rocprofiler_time_id_t;
Method for converting of profiling timestamp to time value for a given time ID:
hsa_status_t rocprofiler_get_time(
rocprofiler_time_id_t time_id, // identifier of the particular
// time to convert the timestamp
uint64_t timestamp, // profiling timestamp
uint64_t* value_ns); // [out] returned time ns value
```
### 4.5. Sampling API
```
The API supports the counters sampling usage model with start/read/stop methods and also lets
to wait for the profiling data in the intercepting usage model with get_data method.
Start/stop/read methods:
hsa_status_t rocprofiler_start(
rocprofiler_t* context, // [in/out] profiling context
uint32_t group_index = 0); // group index
hsa_status_t rocprofiler_stop(
rocprofiler_t* context, // [in/out] profiling context
uint32_t group_index = 0); // group index
hsa_status_t rocprofiler_read(
rocprofiler_t* context, // [in/out] profiling context
uint32_t group_index = 0); // group index
Wait for profiling data:
hsa_status_t rocprofiler_get_data(
rocprofiler_t* context, // [in/out] profiling context
uint32_t group_index = 0); // group index
Group versions of the above start/stop/read/get_data methods:
hsa_status_t rocprofiler_group_start(
rocprofiler_group_t* group); // [in/out] profiling group
hsa_status_t rocprofiler_group_stop(
rocprofiler_group_t* group); // [in/out] profiling group
hsa_status_t rocprofiler_group_read(
rocprofiler_group_t* group); // [in/out] profiling group
hsa_status_t rocprofiler_group_get_data(
rocprofiler_group_t* group); // [in/out] profiling group
```
### 4.6. Intercepting API
```
The library provides a callback API for enabling profiling for the kernels dispatched to
HSA AQL queues. The API enables per-kernel profiling data collection.
Currently implemented the option with serializing the kernels execution.
ROC profiler callback type:
hsa_status_t (*rocprofiler_callback_t)(
const rocprofiler_callback_data_t* callback_data, // callback data passed by HSA runtime
void* user_data, // [in/out] user data passed
// to the callback
rocprofiler_group** group); // [out] returned profiling group
Profiling callback data:
typedef struct {
uint64_t dispatch; // dispatch timestamp
uint64_t begin; // begin timestamp
uint64_t end; // end timestamp
uint64_t complete; // completion signal timestamp
} rocprofiler_dispatch_record_t;
typedef struct {
hsa_agent_t agent; // GPU agent handle
uint32_t agent_index; // GPU index
const hsa_queue_t* queue; // HSA queue
uint64_t queue_index; // Index in the queue
const hsa_kernel_dispatch_packet_t* packet; // HSA dispatch packet
const char* kernel_name; // Kernel name
const rocprofiler_dispatch_record_t* record; // Dispatch record
} rocprofiler_callback_data_t;
Queue callbacks:
typedef struct {
rocprofiler_callback_t dispatch; // kernel dispatch callback
hsa_status_t (*destroy)(hsa_queue_t* queue, void* data); // queue destroy callback
} rocprofiler_queue_callbacks_t;
Adding/removing kernel dispatch and queue destroy callbacks
hsa_status_t rocprofiler_set_intercepting(
rocprofiler_intercepting_t callbacks, // intercepting callbacks
void* data); // [in/out] passed callbacks data
hsa_status_t rocprofiler_remove_intercepting();
```
### 4.7. Profiling Context Pools
```
The API provide capability to create a context pool for a given agent and a set of features, to fetch/release a context entry, to register a callback for pools contexts completion.
Profiling pool handle:
typename rocprofiler_pool_t;
Profiling pool entry:
typedef struct {
rocprofiler_t* context; // context object
void* payload; // payload data object
} rocprofiler_pool_entry_t;
Profiling handler, calling on profiling completion:
typedef bool (*rocprofiler_pool_handler_t)(const rocprofiler_pool_entry_t* entry, void* arg);
Profiling properties:
typedef struct {
uint32_t num_entries; // pool size entries
uint32_t payload_bytes; // payload size bytes
rocprofiler_pool_handler_t handler; // handler on context completion
void* handler_arg; // the handler arg
} rocprofiler_pool_properties_t;
Open profiling pool:
hsa_status_t rocprofiler_pool_open(
hsa_agent_t agent, // GPU handle
rocprofiler_feature_t* features, // [in] profiling features array
uint32_t feature_count, // profiling info count
rocprofiler_pool_t** pool, // [out] context object
uint32_t mode, // profiling mode mask
rocprofiler_pool_properties_t*); // pool properties
Close profiling pool:
hsa_status_t rocprofiler_pool_close(
rocprofiler_pool_t* pool); // profiling pool handle
Fetch profiling pool entry:
hsa_status_t rocprofiler_pool_fetch(
rocprofiler_pool_t* pool, // profiling pool handle
rocprofiler_pool_entry_t* entry); // [out] empty profiling pool entry
Release profiling pool entry:
hsa_status_t rocprofiler_pool_release(
rocprofiler_pool_entry_t* entry); // released profiling pool entry
Iterate fetched profiling pool entries:
hsa_status_t rocprofiler_pool_iterate(
rocprofiler_pool_t* pool, // profiling pool handle
hsa_status_t (*callback)(rocprofiler_pool_entry_t* entry, void* data),
// callback
void *data); // [in/out] data passed to callback
Flush completed entries in profiling pool:
hsa_status_t rocprofiler_pool_flush(
rocprofiler_pool_t* pool); // profiling pool handle
```
## 5. Application code examples
### 5.1. Querying available metrics
```
Info data callback:
hsa_status_t info_data_callback(const rocprofiler_info_data_t info, void *data) {
switch (info.kind) {
case ROCPROFILER_INFO_KIND_METRIC: {
if (info.metric.expr != NULL) {
fprintf(stdout, "Derived counter: gpu-agent%d : %s : %s\n",
info.agent_index, info.metric.name, info.metric.description);
fprintf(stdout, " %s = %s\n", info.metric.name, info.metric.expr);
} else {
fprintf(stdout, "Basic counter: gpu-agent%d : %s",
info.agent_index, info.metric.name);
if (info.metric.instances > 1) {
fprintf(stdout, "[0-%u]", info.metric.instances - 1);
}
fprintf(stdout, " : %s\n", info.metric.description);
fprintf(stdout, " block %s has %u counters\n",
info.metric.block_name, info.metric.block_counters);
}
fflush(stdout);
break;
}
default:
printf("wrong info kind %u\n", kind);
return HSA_STATUS_ERROR;
}
return HSA_STATUS_SUCCESS;
}
Printing all available metrics:
hsa_status_t status = rocprofiler_iterate_info(
agent,
ROCPROFILER_INFO_KIND_METRIC,
info_data_callback,
NULL);
<check status>
```
### 5.2. Profiling code example
```
Profiling of L1 miss ratio, average memory bandwidth.
In the example below rocprofiler_group_get_data group APIs are used for the purpose of a usage
example but in SINGLEGROUP mode when only one group is allowed the context handle itself can be
saved and then direct context method rocprofiler_get_data with default group index equal to 0
can be used.
hsa_status_t dispatch_callback(
const rocprofiler_callback_data_t* callback_data,
void* user_data,
rocprofiler_group_t* group)
{
hsa_status_t status = HSA_STATUS_SUCCESS;
// Profiling context
rocprofiler_t* context;
// Profiling info objects
rocprofiler_feature_t features* = new rocprofiler_feature_t[2];
// Tracing parameters
rocprofiler_feature_parameter_t* parameters = new rocprofiler_feature_parameter_t[2];
// Setting profiling features
features[0].type = ROCPROFILER_METRIC;
features[0].name = "L1_MISS_RATIO";
features[1].type = ROCPROFILER_METRIC;
features[1].name = "DRAM_BANDWIDTH";
// Creating profiling context
status = rocprofiler_open(callback_data->dispatch.agent, features, 2, &context,
ROCPROFILER_MODE_SINGLEGROUP, NULL);
<check status>
// Get the profiling group
// For general case with many groups there is rocprofiler_group_count() API
const uint32_t group_index = 0
status = rocprofiler_get_group(context, group_index, group);
<check_status>
// In SINGLEGROUP mode the context handle itself can be saved, because there is just one group
<saving the callback data/profiling group/profiling features>
return status;
}
Profiling tool constructor is adding the dispatch callback:
void profiling_libary_constructor() {
// Defining callback data, no data in this simple example
void* callback_data = NULL;
// Adding observers
hsa_sttaus_t status = rocprofiler_add_dispatch_callback(dispatch_callback, callback_data);
<check status>
// Dispatching profiled kernel
<dispatching profiled kernels>
}
void profiling_libary_destructor() {
<for entry : <saved callbacks data>> {
// In SINGLEGROUP mode the rocprofiler_get_group() method with default zero group
// index can be used, if context handle would be saved
status = rocprofiler_group_get_data(entry->group);
<check status>
status = rocprofiler_get_metrics(entry->group->context);
<check status>
status = rocprofiler_close(entry->group->context);
<check status>
<tool_dump_data_method(entry->dispatch_data, entry->features, entry->features_count)>;
}
}
```
### 5.3. Option to use completion callback
```
Creating profiling context with completion callback:
. . .
rocprofiler_properties_t properties = {};
properties.callback = completion_callback;
properties.callback_arg = NULL; // no args defined
status = rocprofiler_open(agent, features, 3, &context,
ROCPROFILER_MODE_SINGLEGROUP, properties);
<check status>
. . .
Definition of completion callback:
void completion_callback(profiler_group_t group, void* arg) {
<tool_dump_data_method(group)>
hsa_status_t status = rocprofiler_close(group.context);
<check status>
}
```
### 5.4. Option to Use Context Pool
```
Code example of context pool usage.
Creating profiling contexts pool:
. . .
rocprofiler_pool_properties_t properties{};
properties.num_entries = 100;
properties.payload_bytes = sizeof(context_entry_t);
properties.handler = context_handler;
properties.handler_arg = handler_arg;
status = rocprofiler_pool_open(agent, features, 3, &context,
ROCPROFILER_MODE_SINGLEGROUP, properties);
<check status>
. . .
Fetching a context entry:
rocprofiler_pool_entry_t pool_entry{};
status = rocprofiler_pool_fetch(pool, &pool_entry);
<check status>
// Profiling context entry
rocprofiler_t* context = pool_entry.context;
context_entry_t* entry = reinterpret_cast <context_entry_t*>
(pool_entry.payload);
```
### 5.5. Standalone Sampling Usage Code Example
```
The profiling metrics are being read from separate standalone queue other than the application kernels are submitted to.
To enable the sampling mode, the profiling mode in all user queues should be enabled. It can be done by loading ROC-profiler
library to HSA runtime using the environment variable HSA_TOOLS_LIB for all shell sessions.
// Sampling rate
uint32_t sampling_rate = <some rate>;
// Sampling count
uint32_t sampling_count = <some count>;
// HSA status
hsa_status_t status = HSA_STATUS_ERROR;
// HSA agent
hsa_agent_t agent;
// Profiling context
rocprofiler_t* context = NULL;
// Profiling properties
rocprofiler_properties_t properties;
// Getting HSA agent
<query for HSA agent by hsa_iterate_agents()>
// Profiling feature objects
const unsigned feature_count = 2;
rocprofiler_feature_t feature[feature_count];
// Counters and metrics
feature[0].kind = ROCPROFILER_FEATURE_KIND_METRIC;
feature[0].name = "GPUBusy";
feature[1].kind = ROCPROFILER_FEATURE_KIND_METRIC;
feature[1].name = "SQ_WAVES";
// Creating profiling context with standalone queue
properties = {};
properties.queue_depth = 128;
status = rocprofiler_open(agent, feature, feature_count, &context,
ROCPROFILER_MODE_STANDALONE| ROCPROFILER_MODE_CREATEQUEUE|
ROCPROFILER_MODE_SINGLEGROUP, &properties);
<check status>
// Start counters and sample them in the loop with the sampling rate
status = rocprofiler_start(context, 0);
<check status>
for (unsigned ind = 0; ind < sampling_count; ++ind) {
sleep(sampling_rate);
status = rocprofiler_read(context, 0);
<check status>
status = rocprofiler_get_data(context, 0);
<check status>
status = rocprofiler_get_metrics(context);
<check status>
print_results(feature, feature_count);
}
// Stop counters
status = rocprofiler_stop(context, group_n);
<check status>
// Finishing cleanup
// Deleting profiling context will delete all allocated resources
status = rocprofiler_close(context);
<check status>
```
### 5.6. Printing Out Profiling Results
```
Below is a code example for printing out the profiling results from profiling features array:
void print_results(rocprofiler_feature_t* feature, uint32_t feature_count) {
for (rocprofiler_feature_t* p = feature; p < feature + feature_count; ++p)
{
std::cout << (p - feature) << ": " << p->name;
switch (p->data.kind) {
case ROCPROFILER_DATA_KIND_INT64:
std::cout << " result_int64 (" << p->data.result_int64 << ")"
<< std::endl;
break;
case ROCPROFILER_DATA_KIND_BYTES: {
std::cout << " result_bytes ptr(" << p->data.result_bytes.ptr <<
") " << " size(" << p->data.result_bytes.size << ")"
<< " instance_count(" << p->data.result_bytes.instance_count
<< ")";
break;
}
default:
std::cout << "bad result kind (" << p->data.kind << ")"
<< std::endl;
<abort>
}
}
}
```
Binary file not shown.
+393
View File
@@ -0,0 +1,393 @@
# rocprof
## 1. Overview
The rocProf is a command line tool implemented on the top of rocProfiler and rocTracer APIs. Source code for rocProf may be found here:
GitHub: https://github.com/ROCm-Developer-Tools/rocprofiler/blob/amd-master/bin/rocprof
This command line tool is implemented as a script which is setting up the environment for attaching the profiler and then run the provided application command line. The tool uses two profiling plugins loaded by ROC runtime and based on rocProfiler and rocTracer for collecting metrics/counters, HW traces and runtime API/activity traces. The tool consumes an input XML or text file with counters list or trace parameters and provides output profiling data and statistics in various formats as text, CSV and JSON traces. Google Chrome tracing can be used to visualize the JSON traces with runtime API/activity timelines and per kernel counters data.
## 2. Profiling Modes
rocprof can be used for GPU profiling using HW counters and application tracing
### 2.1. GPU profiling
GPU profiling is controlled with input file which defines a list of metrics/counters and a profiling scope. An input file is provided using option -i [input file]. Output CSV file with a line per submitted kernel is generated. Each line has kernel name, kernel parameters and counter values. By option ‘—stats the kernel execution stats can be generated in CSV format. Currently profiling has limitation of serializing submitted kernels.
An example of input file:
```
# Perf counters group 1
pmc : Wavefronts VALUInsts SALUInsts SFetchInsts
# Perf counters group 2
pmc : TCC_HIT[0], TCC_MISS[0]
# Filter by dispatches range, GPU index and kernel names
# supported range formats: "3:9", "3:", "3"
range: 1 : 4
gpu: 0 1 2 3
kernel: simple Pass1 simpleConvolutionPass2
```
An example of profiling command line for MatrixTranspose application
```
$ rocprof -i input.txt MatrixTranspose
RPL: on '191018_011134' from '/…./rocprofiler_pkg' in '/…./MatrixTranspose'
RPL: profiling '"./MatrixTranspose"'
RPL: input file 'input.txt'
RPL: output dir '/tmp/rpl_data_191018_011134_9695'
RPL: result dir '/tmp/rpl_data_191018_011134_9695/input0_results_191018_011134'
ROCProfiler: rc-file '/…./rpl_rc.xml'
ROCProfiler: input from "/tmp/rpl_data_191018_011134_9695/input0.xml"
gpu_index =
kernel =
range =
4 metrics
L2CacheHit, VFetchInsts, VWriteInsts, MemUnitStalled
0 traces
Device name Ellesmere [Radeon RX 470/480/570/570X/580/580X]
PASSED!
ROCprofiler: 1 contexts collected, output directory /tmp/rpl_data_191018_011134_9695/input0_results_191018_011134
RPL: '/…./MatrixTranspose/input.csv' is generated
```
#### 2.1.1. Counters and metrics
There are two profiling features, metrics and traces. Hardware performance counters are treated as the basic metrics and the formulas can be defined for derived metrics.
Counters and metrics can be dynamically configured using XML configuration files with counters and metrics tables:
- Counters table entry, basic metric: counter name, block name, event id
- Derived metrics table entry: metric name, an expression for calculation the metric from the counters
Metrics XML File Example:
```
<gfx8>
<metric name=L1_CYCLES_COUNTER block=L1 event=0 descr=”L1 cache cycles”></metric>
<metric name=L1_MISS_COUNTER block=L1 event=33 descr=”L1 cache misses”></metric>
. . .
</gfx8>
<gfx9>
. . .
</gfx9>
<global>
<metric
name=L1_MISS_RATIO
expr=L1_CYCLES_COUNT/L1_MISS_COUNTER
descry=”L1 miss rate metric”
></metric>
</global>
```
##### 2.1.1.1. Metrics query
Available counters and metrics can be queried by options ‘—list-basic for counters and ‘—list-derived for derived metrics. The output for counters indicates number of block instances and number of block counter registers. The output for derived metrics prints the metrics expressions.
Examples:
```
$ rocprof --list-basic
RPL: on '191018_014450' from '/opt/rocm/rocprofiler' in '/…./MatrixTranspose'
ROCProfiler: rc-file '/…./rpl_rc.xml'
Basic HW counters:
gpu-agent0 : GRBM_COUNT : Tie High - Count Number of Clocks
block GRBM has 2 counters
gpu-agent0 : GRBM_GUI_ACTIVE : The GUI is Active
block GRBM has 2 counters
. . .
gpu-agent0 : TCC_HIT[0-15] : Number of cache hits.
block TCC has 4 counters
gpu-agent0 : TCC_MISS[0-15] : Number of cache misses. UC reads count as misses.
block TCC has 4 counters
. . .
$ rocprof --list-derived
RPL: on '191018_015911' from '/opt/rocm/rocprofiler' in '/home/evgeny/work/BUILD/0_MatrixTranspose'
ROCProfiler: rc-file '/home/evgeny/rpl_rc.xml'
Derived metrics:
gpu-agent0 : TCC_HIT_sum : Number of cache hits. Sum over TCC instances.
TCC_HIT_sum = sum(TCC_HIT,16)
gpu-agent0 : TCC_MISS_sum : Number of cache misses. Sum over TCC instances.
TCC_MISS_sum = sum(TCC_MISS,16)
gpu-agent0 : TCC_MC_RDREQ_sum : Number of 32-byte reads. Sum over TCC instances.
TCC_MC_RDREQ_sum = sum(TCC_MC_RDREQ,16)
. . .
```
##### 2.1.1.2. Metrics collecting
Counters and metrics accumulated per kernel can be collected using input file with a list of metrics, see an example in 2.1.
Currently profiling has limitation of serializing submitted kernels.
The number of counters which can be dumped by one run is limited by GPU HW by number of counter registers per block. The number of counters can be different for different blocks and can be queried, see 2.1.1.1.
###### 2.1.1.2.1. Blocks instancing
GPU blocks are implemented as several identical instances. To dump counters of specific instance square brackets can be used, see an example in 2.1.
The number of block instances can be queried, see 2.1.1.1.
###### 2.1.1.2.2. HW limitations
The number of counters which can be dumped by one run is limited by GPU HW by number of counter registers per block. The number of counters can be different for different blocks and can be queried, see 2.1.1.1.
- Metrics groups
To dump a list of metrics exceeding HW limitations the metrics list can be split on groups.
The tool supports automatic splitting on optimal metric groups:
```
$ rocprof -i input.txt ./MatrixTranspose
RPL: on '191018_032645' from '/opt/rocm/rocprofiler' in '/…./MatrixTranspose'
RPL: profiling './MatrixTranspose'
RPL: input file 'input.txt'
RPL: output dir '/tmp/rpl_data_191018_032645_12106'
RPL: result dir '/tmp/rpl_data_191018_032645_12106/input0_results_191018_032645'
ROCProfiler: rc-file '/…./rpl_rc.xml'
ROCProfiler: input from "/tmp/rpl_data_191018_032645_12106/input0.xml"
gpu_index =
kernel =
range =
20 metrics
Wavefronts, VALUInsts, SALUInsts, SFetchInsts, FlatVMemInsts, LDSInsts, FlatLDSInsts, GDSInsts, VALUUtilization, FetchSize, WriteSize, L2CacheHit, VWriteInsts, GPUBusy, VALUBusy, SALUBusy, MemUnitStalled, WriteUnitStalled, LDSBankConflict, MemUnitBusy
0 traces
Device name Ellesmere [Radeon RX 470/480/570/570X/580/580X]
Input metrics out of HW limit. Proposed metrics group set:
group1: L2CacheHit VWriteInsts MemUnitStalled WriteUnitStalled MemUnitBusy FetchSize FlatVMemInsts LDSInsts VALUInsts SALUInsts SFetchInsts FlatLDSInsts GPUBusy Wavefronts
group2: WriteSize GDSInsts VALUUtilization VALUBusy SALUBusy LDSBankConflict
ERROR: rocprofiler_open(), Construct(), Metrics list exceeds HW limits
Aborted (core dumped)
Error found, profiling aborted.
```
- Collecting with multiple runs
To collect several metric groups a full application replay is used by defining several pmc: lines in the input file, see 2.1.
### 2.2. Application tracing
Supported application tracing includes runtime API and GPU activity tracing
Supported runtimes are: ROCr (HSA API) and HIP
Supported GPU activity: kernel execution, async memory copy, barrier packets.
The trace is generated in JSON format compatible with Chrome tracing.
The trace consists of several sections with timelines for API trace per thread and GPU activity. The timelines events show event name and parameters.
Supported options: ‘—hsa-trace, ‘—hip-trace, ‘—sys-trace, where sys trace is for HIP and HSA combined trace.
#### 2.2.1. HIP runtime trace
The trace is generated by option ‘—hip-trace and includes HIP API timelines and GPU activity at the runtime level.
#### 2.2.2. ROCr runtime trace
The trace is generated by option ‘—hsa-trace and includes ROCr API timelines and GPU activity at AQL queue level. Also, can provide counters per kernel.
#### 2.2.3. KFD driver trace
The trace is generated by option ‘—kfd-trace and includes KFD Thunk API timeline.
It is planned to add memory allocations/migration tracing.
#### 2.2.4. Code annotation
Support for application code annotation.
Start/stop API is supported to programmatically control the profiling.
A roctx library provides annotation API. Annotation is visualized in JSON trace as a separate "Markers and Ranges" timeline section.
##### 2.2.4.1. Start/stop API
```
// Tracing start API
void roctracer_start();
// Tracing stop API
void roctracer_stop();
```
##### 2.2.4.2. rocTX basic markers API
```
// A marker created by given ASCII massage
void roctxMark(const char* message);
// Returns the 0 based level of a nested range being started by given message associated to this range.
// A negative value is returned on the error.
int roctxRangePush(const char* message);
// Marks the end of a nested range.
// Returns the 0 based level the range.
// A negative value is returned on the error.
int roctxRangePop();
```
### 2.3. Multiple GPUs profiling
The profiler supports multiple GPUs profiling and provide GPI id for counters and kernels data in CSV output file. Also, GPU id is indicating for respective GPU activity timeline in JSON trace.
## 3. Profiling control
Profiling can be controlled by specifying a profiling scope, by filtering trace events and specifying interesting time intervals.
### 3.1. Profiling scope
Counters profiling scope can be specified by GPU id list, kernel name substrings list and dispatch range.
Supported range formats examples: "3:9", "3:", "3". You can see an example of input file in 2.1.
#### 3.2. Tracing control
Tracing can be filtered by events names using profiler input file and by enabling interesting time intervals by command line option.
#### 3.2.1. Filtering traced APIs
A list of traced API names can be specified in profiler input file.
An example of input file line for ROCr runtime trace (HAS API):
```
hsa: hsa_queue_create hsa_amd_memory_pool_allocate
```
#### 3.2.2. Tracing time period
Trace can be dumped periodically with initial delay, dumping period length and rate:
```
--trace-period <delay:length:rate>
```
### 3.3. Concurrent kernels
Currently concurrent kernels profiling is not supported which is a planned feature. Kernels are serialized.
### 3.4. Multi-processes profiling
Multi-processes profiling is not currently supported.
### 3.5. Errors logging
Profiler errors are logged to global logs:
```
/tmp/aql_profile_log.txt
/tmp/rocprofiler_log.txt
/tmp/roctracer_log.txt
```
## 4. 3rd party visualization tools
rocprof is producing JSON trace compatible with Chrome Tracing, which is an internal trace visualization tool in Google Chrome.
### 4.1. Chrome tracing
Good review can be found by the link: https://aras-p.info/blog/2017/01/23/Chrome-Tracing-as-Profiler-Frontend/
## 5. Command line options
The command line options can be printed with option -h:
```
$ rocprof -h
RPL: on '191018_023018' from '/opt/rocm/rocprofiler' in '/…./MatrixTranspose'
ROCm Profiling Library (RPL) run script, a part of ROCprofiler library package.
Full path: /opt/rocm/rocprofiler/bin/rocprof
Metrics definition: /opt/rocm/rocprofiler/lib/metrics.xml
Usage:
rocprof [-h] [--list-basic] [--list-derived] [-i <input .txt/.xml file>] [-o <output CSV file>] <app command line>
Options:
-h - this help
--verbose - verbose mode, dumping all base counters used in the input metrics
--list-basic - to print the list of basic HW counters
--list-derived - to print the list of derived metrics with formulas
--cmd-qts <on|off> - quoting profiled cmd-line [on]
-i <.txt|.xml file> - input file
Input file .txt format, automatically rerun application for every pmc line:
# Perf counters group 1
pmc : Wavefronts VALUInsts SALUInsts SFetchInsts FlatVMemInsts LDSInsts FlatLDSInsts GDSInsts VALUUtilization FetchSize
# Perf counters group 2
pmc : WriteSize L2CacheHit
# Filter by dispatches range, GPU index and kernel names
# supported range formats: "3:9", "3:", "3"
range: 1 : 4
gpu: 0 1 2 3
kernel: simple Pass1 simpleConvolutionPass2
Input file .xml format, for single profiling run:
# Metrics list definition, also the form "<block-name>:<event-id>" can be used
# All defined metrics can be found in the 'metrics.xml'
# There are basic metrics for raw HW counters and high-level metrics for derived counters
<metric name=SQ:4,SQ_WAVES,VFetchInsts
></metric>
# Filter by dispatches range, GPU index and kernel names
<metric
# range formats: "3:9", "3:", "3"
range=""
# list of gpu indexes "0,1,2,3"
gpu_index=""
# list of matched sub-strings "Simple1,Conv1,SimpleConvolution"
kernel=""
></metric>
-o <output file> - output CSV file [<input file base>.csv]
-d <data directory> - directory where profiler store profiling data including traces [/tmp]
The data directory is removed automatically if the directory is matching the temporary one, which is the default.
-t <temporary directory> - to change the temporary directory [/tmp]
By changing the temporary directory you can prevent removing the profiling data from /tmp or enable removing from not '/tmp' directory.
--basenames <on|off> - to turn on/off truncating of the kernel full function names till the base ones [off]
--timestamp <on|off> - to turn on/off the kernel dispatches timestamps, dispatch/begin/end/complete [off]
--ctx-wait <on|off> - to wait for outstanding contexts on profiler exit [on]
--ctx-limit <max number> - maximum number of outstanding contexts [0 - unlimited]
--heartbeat <rate sec> - to print progress heartbeats [0 - disabled]
--obj-tracking <on|off> - to turn on/off kernels code objects tracking [off]
--stats - generating kernel execution stats, file <output name>.stats.csv
--roctx-trace - to enable rocTX application code annotation trace, "Markers and Ranges" JSON trace section.
--sys-trace - to trace HIP/HSA APIs and GPU activity, generates stats and JSON trace chrome-tracing compatible
--hip-trace - to trace HIP, generates API execution stats and JSON file chrome-tracing compatible
--hsa-trace - to trace HSA, generates API execution stats and JSON file chrome-tracing compatible
--kfd-trace - to trace KFD, generates API execution stats and JSON file chrome-tracing compatible
Generated files: <output name>.<domain>_stats.txt <output name>.json
Traced API list can be set by input .txt or .xml files.
Input .txt:
hsa: hsa_queue_create hsa_amd_memory_pool_allocate
Input .xml:
<trace name="HSA">
<parameters list="hsa_queue_create, hsa_amd_memory_pool_allocate">
</parameters>
</trace>
--trace-start <on|off> - to enable tracing on start [on]
--trace-period <delay:length:rate> - to enable trace with initial delay, with periodic sample length and rate
Supported time formats: <number(m|s|ms|us)>
Configuration file:
You can set your parameters defaults preferences in the configuration file 'rpl_rc.xml'. The search path sequence: .:/home/user:<package path>
First the configuration file is looking in the current directory, then in your home, and then in the package directory.
Configurable options: 'basenames', 'timestamp', 'ctx-limit', 'heartbeat', 'obj-tracking'.
An example of 'rpl_rc.xml':
<defaults
basenames=off
timestamp=off
ctx-limit=0
heartbeat=0
obj-tracking=off
></defaults>
```
## 6. Publicly available counters and metrics
The following counters are publicly available for commercially available VEGA10/20 GPUs.
Counters:
```
• GRBM_COUNT : Tie High - Count Number of Clocks
• GRBM_GUI_ACTIVE : The GUI is Active
• SQ_WAVES : Count number of waves sent to SQs. (per-simd, emulated, global)
• SQ_INSTS_VALU : Number of VALU instructions issued. (per-simd, emulated)
• SQ_INSTS_VMEM_WR : Number of VMEM write instructions issued (including FLAT). (per-simd, emulated)
• SQ_INSTS_VMEM_RD : Number of VMEM read instructions issued (including FLAT). (per-simd, emulated)
• SQ_INSTS_SALU : Number of SALU instructions issued. (per-simd, emulated)
• SQ_INSTS_SMEM : Number of SMEM instructions issued. (per-simd, emulated)
• SQ_INSTS_FLAT : Number of FLAT instructions issued. (per-simd, emulated)
• SQ_INSTS_FLAT_LDS_ONLY : Number of FLAT instructions issued that read/wrote only from/to LDS (only works if EARLY_TA_DONE is enabled). (per-simd, emulated)
• SQ_INSTS_LDS : Number of LDS instructions issued (including FLAT). (per-simd, emulated)
• SQ_INSTS_GDS : Number of GDS instructions issued. (per-simd, emulated)
• SQ_WAIT_INST_LDS : Number of wave-cycles spent waiting for LDS instruction issue. In units of 4 cycles. (per-simd, nondeterministic)
• SQ_ACTIVE_INST_VALU : regspec 71? Number of cycles the SQ instruction arbiter is working on a VALU instruction. (per-simd, nondeterministic)
• SQ_INST_CYCLES_SALU : Number of cycles needed to execute non-memory read scalar operations. (per-simd, emulated)
• SQ_THREAD_CYCLES_VALU : Number of thread-cycles used to execute VALU operations (similar to INST_CYCLES_VALU but multiplied by # of active threads). (per-simd)
• SQ_LDS_BANK_CONFLICT : Number of cycles LDS is stalled by bank conflicts. (emulated)
• TA_TA_BUSY[0-15] : TA block is busy. Perf_Windowing not supported for this counter.
• TA_FLAT_READ_WAVEFRONTS[0-15] : Number of flat opcode reads processed by the TA.
• TA_FLAT_WRITE_WAVEFRONTS[0-15] : Number of flat opcode writes processed by the TA.
• TCC_HIT[0-15] : Number of cache hits.
• TCC_MISS[0-15] : Number of cache misses. UC reads count as misses.
• TCC_EA_WRREQ[0-15] : Number of transactions (either 32-byte or 64-byte) going over the TC_EA_wrreq interface. Atomics may travel over the same interface and are generally classified as write requests. This does not include probe commands.
• TCC_EA_WRREQ_64B[0-15] : Number of 64-byte transactions going (64-byte write or CMPSWAP) over the TC_EA_wrreq interface.
• TCC_EA_WRREQ_STALL[0-15] : Number of cycles a write request was stalled.
• TCC_EA_RDREQ[0-15] : Number of TCC/EA read requests (either 32-byte or 64-byte)
• TCC_EA_RDREQ_32B[0-15] : Number of 32-byte TCC/EA read requests
• TCP_TCP_TA_DATA_STALL_CYCLES[0-15] : TCP stalls TA data interface. Now Windowed.
```
The following derived metrics have been defined and the profiler metrics XML specification can be found at: https://github.com/ROCm-Developer-Tools/rocprofiler/blob/amd-master/test/tool/metrics.xml.
Metrics:
```
• TA_BUSY_avr : TA block is busy. Average over TA instances.
• TA_BUSY_max : TA block is busy. Max over TA instances.
• TA_BUSY_min : TA block is busy. Min over TA instances.
• TA_FLAT_READ_WAVEFRONTS_sum : Number of flat opcode reads processed by the TA. Sum over TA instances.
• TA_FLAT_WRITE_WAVEFRONTS_sum : Number of flat opcode writes processed by the TA. Sum over TA instances.
• TCC_HIT_sum : Number of cache hits. Sum over TCC instances.
• TCC_MISS_sum : Number of cache misses. Sum over TCC instances.
• TCC_EA_RDREQ_32B_sum : Number of 32-byte TCC/EA read requests. Sum over TCC instances.
• TCC_EA_RDREQ_sum : Number of TCC/EA read requests (either 32-byte or 64-byte). Sum over TCC instances.
• TCC_EA_WRREQ_sum : Number of transactions (either 32-byte or 64-byte) going over the TC_EA_wrreq interface. Sum over TCC instances.
• TCC_EA_WRREQ_64B_sum : Number of 64-byte transactions going (64-byte write or CMPSWAP) over the TC_EA_wrreq interface. Sum over TCC instances.
• TCC_WRREQ_STALL_max : Number of cycles a write request was stalled. Max over TCC instances.
• TCC_MC_WRREQ_sum : Number of 32-byte effective writes. Sum over TCC instaces.
• FETCH_SIZE : The total kilobytes fetched from the video memory. This is measured with all extra fetches and any cache or memory effects taken into account.
• WRITE_SIZE : The total kilobytes written to the video memory. This is measured with all extra fetches and any cache or memory effects taken into account.
• GPUBusy : The percentage of time GPU was busy.
• Wavefronts : Total wavefronts.
• VALUInsts : The average number of vector ALU instructions executed per work-item (affected by flow control).
• SALUInsts : The average number of scalar ALU instructions executed per work-item (affected by flow control).
• VFetchInsts : The average number of vector fetch instructions from the video memory executed per work-item (affected by flow control). Excludes FLAT instructions that fetch from video memory.
• SFetchInsts : The average number of scalar fetch instructions from the video memory executed per work-item (affected by flow control).
• VWriteInsts : The average number of vector write instructions to the video memory executed per work-item (affected by flow control). Excludes FLAT instructions that write to video memory.
• FlatVMemInsts : The average number of FLAT instructions that read from or write to the video memory executed per work item (affected by flow control). Includes FLAT instructions that read from or write to scratch.
• LDSInsts : The average number of LDS read or LDS write instructions executed per work item (affected by flow control). Excludes FLAT instructions that read from or write to LDS.
• FlatLDSInsts : The average number of FLAT instructions that read or write to LDS executed per work item (affected by flow control).
• GDSInsts : The average number of GDS read or GDS write instructions executed per work item (affected by flow control).
• VALUUtilization : The percentage of active vector ALU threads in a wave. A lower number can mean either more thread divergence in a wave or that the work-group size is not a multiple of 64. Value range: 0% (bad), 100% (ideal - no thread divergence).
• VALUBusy : The percentage of GPUTime vector ALU instructions are processed. Value range: 0% (bad) to 100% (optimal).
• SALUBusy : The percentage of GPUTime scalar ALU instructions are processed. Value range: 0% (bad) to 100% (optimal).
• Mem32Bwrites :
• FetchSize : The total kilobytes fetched from the video memory. This is measured with all extra fetches and any cache or memory effects taken into account.
• WriteSize : The total kilobytes written to the video memory. This is measured with all extra fetches and any cache or memory effects taken into account.
• L2CacheHit : The percentage of fetch, write, atomic, and other instructions that hit the data in L2 cache. Value range: 0% (no hit) to 100% (optimal).
• MemUnitBusy : The percentage of GPUTime the memory unit is active. The result includes the stall time (MemUnitStalled). This is measured with all extra fetches and writes and any cache or memory effects taken into account. Value range: 0% to 100% (fetch-bound).
• MemUnitStalled : The percentage of GPUTime the memory unit is stalled. Try reducing the number or size of fetches and writes if possible. Value range: 0% (optimal) to 100% (bad).
• WriteUnitStalled : The percentage of GPUTime the Write unit is stalled. Value range: 0% to 100% (bad).
• ALUStalledByLDS : The percentage of GPUTime ALU units are stalled by the LDS input queue being full or the output queue being not ready. If there are LDS bank conflicts, reduce them. Otherwise, try reducing the number of LDS accesses if possible. Value range: 0% (optimal) to 100% (bad).
• LDSBankConflict : The percentage of GPUTime LDS is stalled by bank conflicts. Value range: 0% (optimal) to 100% (bad).
```
+270
View File
@@ -0,0 +1,270 @@
# ROCProfiler v2
## DISCLAIMER
The information presented in this document is for informational purposes only and may contain technical inaccuracies, omissions, and typographical errors. The information contained herein is subject to change and may be rendered inaccurate for many reasons, including but not limited to product and roadmap changes, component and motherboard version changes, new model and/or product releases, product differences between differing manufacturers, software changes, BIOS flashes, firmware upgrades, or the like. Any computer system has risks of security vulnerabilities that cannot be completely prevented or mitigated. AMD assumes no obligation to update or otherwise correct or revise this information. However, AMD reserves the right to revise this information and to make changes from time to time to the content hereof without obligation of AMD to notify any person of such revisions or changes.THIS INFORMATION IS PROVIDED AS IS.” AMD MAKES NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE CONTENTS HEREOF AND ASSUMES NO RESPONSIBILITY FOR ANY INACCURACIES, ERRORS, OR OMISSIONS THAT MAY APPEAR IN THIS INFORMATION. AMD SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR ANY PARTICULAR PURPOSE. IN NO EVENT WILL AMD BE LIABLE TO ANY PERSON FOR ANY RELIANCE, DIRECT, INDIRECT, SPECIAL, OR OTHER CONSEQUENTIAL DAMAGES ARISING FROM THE USE OF ANY INFORMATION CONTAINED HEREIN, EVEN IF AMD IS EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. AMD, the AMD Arrow logo, and combinations thereof are trademarks of Advanced Micro Devices, Inc. Other product names used in this publication are for identification purposes only and may be trademarks of their respective companies.
© 2022 Advanced Micro Devices, Inc. All Rights Reserved.
## Introduction
ROCProfilerV2 is a newly developed design for AMDs tooling infrastructure that provides a hardware specific low level performance analysis interface for profiling of GPU compute applications.
The first API library version for ROCProfiler v2 is 9.0.0
**Note: ROCProfilerV2 is currently considered a beta version and is subject to change in future releases**
## Modules
- Counters
- Hardware
- Generic Buffer
- Session
- Filter
- Tools
- Plugins
- Samples
- Tests
## Getting started
### Requirements
- makecache
- Gtest Development Package (Ubuntu: libgtest-dev)
- libelf-dev, libnuma-dev on ubuntu or their corresponding packages on any other OS
- Cppheaderparser, websockets, matplotlib, lxml, barectf Python3 Packages
### Build
The user has two options for building:
- Option 1 (It will install in the path saved in ROCM_PATH environment variable or /opt/rocm if ROCM_PATH is empty):
- Run
```bash
# Normal Build
./build.sh --build OR ./build.sh -b
```
```bash
# Clean Build
./build.sh --clean-build OR ./build.sh -cb
```
- Option 2 (Where ROCM_PATH environment need to be set with the current installation directory of rocm), run the following:
```bash
# Creating the build directory
mkdir build && cd build
# Configuring the rocprofv2 build
cmake -DCMAKE_PREFIX_PATH=$ROCM_PATH -DCMAKE_MODULE_PATH=$ROCM_PATH/hip/cmake <CMAKE_OPTIONS> ..
# Building the main runtime of the rocprofv2 project
cmake --build . -- -j
# Optionally, for building API documentation
cmake --build . -- -j doc
# Optionally, for building ROCProfiler V2 samples
cmake --build . -- -j samples
# Optionally, for building packages (DEB, RPM, TGZ)
cmake --build . -- -j tests
# Optionally, for building packages (DEB, RPM, TGZ)
# Note: Requires rpm package on ubuntu
cmake --build . -- -j package
```
### Install
- Optionally, run the following to install
```bash
# Install rocprofv2 in the ROCM_PATH path
./rocprofv2 --install
```
OR, if you are using option 2 in building
```bash
cd build
# Install rocprofv2 in the ROCM_PATH path
cmake --build . -- -j install
```
## Tool Usage and Features
- rocsys: This is a frontend command line utility to launch/start/stop/exit a session with the required application to be traced or profiled in rocprofv2 context.
Usage:
```bash
# Launch the application with the required profiling and tracing options with giving a session identifier to be used later
rocsys --session session_name launch mpiexec -n 2 ./rocprofv2 -i samples/input.txt Histogram
# Start a session with a given identifier created at launch
rocsys --session session_name start
# Stop a session with a given identifier created at launch
rocsys session session_name stop
# Exit a session with a given identifier created at launch
rocsys session session_name exit
```
- rocprofv2:
- Counters and Metric Collection: HW counters and derived metrics can be collected using following option:
```bash
rocprofv2 -i samples/input.txt <app_relative_path>
input.txt
```
input.txt content Example (Details of what is needed inside input.txt will be mentioned with every feature):
```bash
pmc: SQ_WAVES GRBM_COUNT GRBM_GUI_ACTIVE SQ_INSTS_VALU
```
- Application Trace Support: Different trace options are available while profiling an app:
```bash
# HIP API & asynchronous activity tracing
rocprofv2 --hip-api <app_relative_path> ## For synchronous HIP API Activity tracing
rocprofv2 --hip-activity <app_relative_path> ## For both Synchronous & ASynchronous HIP API Activity tracing
rocprofv2 --hip-trace <app_relative_path> ## Same as --hip-activity, added for backward compatibility
# HSA API & asynchronous activity tracing
rocprofv2 --hsa-api <app_relative_path> ## For synchronous HSA API Activity tracing
rocprofv2 --hsa-activity <app_relative_path> ## For both Synchronous & ASynchronous HSA API Activity tracing
rocprofv2 --hsa-trace <app_relative_path> ## Same as --hsa-activity, added for backward compatibility
# Kernel dispatches tracing
rocprofv2 --kernel-trace <app_relative_path> ## Kernel Dispatch Tracing
# HIP & HSA API and asynchronous activity and kernel dispatches tracing
rocprofv2 --sys-trace <app_relative_path> ## Same as combining --hip-trace & --hsa-trace & --kernel-trace
```
For complete usage options, please run rocprofv2 help
```bash
rocprofv2 --help
```
- Plugin Support: We have a template for adding new plugins. New plugins can be written on top of rocprofv2 to support the desired output format using include/rocprofiler/v2/rocprofiler_plugins.h header file. These plugins are modular in nature and can easily be decoupled from the code based on need. E.g.
- file plugin: outputs the data in txt files.
- Perfetto plugin: outputs the data in protobuf format.
- Protobuf files can be viewed using ui.perfetto.dev or using trace_processor
- CTF plugin: Outputs the data in ctf format(a binary trace format)
- CTF binary output can be viewed using TraceCompass or babeltrace.
installation:
```bash
rocprofiler-plugins_9.0.0-local_amd64.deb
rocprofiler-plugins-9.0.0-local.x86_64.rpm
```
usage:
```bash
# plugin_name can be file, perfetto , ctf
./rocprofv2 --plugin plugin_name -i samples/input.txt -d output_dir <app_relative_path> # -d is optional, but can be used to define the directory output for output results
```
- Device Profiling: A device profiling session allows the user to profile the GPU device for counters irrespective of the running applications on the GPU. This is different from application profiling. device profiling session doesn't care about the host running processes and threads. It directly provides low level profiling information.
- Session Support: A session is a unique identifier for a profiling/tracing/pc-sampling task. A ROCProfilerV2 Session has enough information about what needs to be collected or traced and it allows the user to start/stop profiling/tracing whenever required. More details on the API can be found in the API specification documentation that can be installed using rocprofiler-doc package. Samples also can be found for how to use the API in samples directory.
## Tests
We make use of the GoogleTest (Gtest) framework to automatically find and add test cases to the CMAKE testing environment. ROCProfilerV2 testing is categorized as following:
- unittests (Gtest Based) : These includes tests for core classes. Any newly added functionality should have a unit test written to it.
- featuretests (standalone and Gtest Based): These includes both API tests and tool tests. Tool is tested against different applications to make sure we have right output in every run.
- memorytests (standalone): This includes running address sanitizer for memory leaks, corruptions.
installation:
```bash
rocprofiler-tests_9.0.0-local_amd64.deb
rocprofiler-tests-9.0.0-local.x86_64.rpm
```
- Optionally, for tests: run the following:
- Option 1, using rocprofv2 script:
```bash
cd build && ./rocprofv2 -t
```
- Option 2, using cmake directly:
```bash
cd build && cmake --build . -- -j check
```
## Documentation
We make use of doxygen to automatically generate API documentation. Generated document can be found in the following path:
```bash
# ROCM_PATH by default is /opt/rocm
# It can be set by the user in different location if needed.
<ROCM_PATH>/share/doc/rocprofv2
```
installation:
```bash
rocprofiler-docs_9.0.0-local_amd64.deb
rocprofiler-docs-9.0.0-local.x86_64.rpm
```
## Samples
- Profiling: Profiling Samples depending on replay mode
- Tracing: Tracing Samples
installation:
```bash
rocprofiler-samples_9.0.0-local_amd64.deb
rocprofiler-samples-9.0.0-local.x86_64.rpm
```
usage:
samples can be run as independent executables once installed
## Project Structure
- bin: ROCProf scripts along with V1 post processing scripts
- doc: Documentation settings for doxygen, V1 API Specifications pdf document.
- include:
- rocprofiler.h: V1 API Header File
- v2:
- rocprofiler.h: V2 API Header File
- rocprofiler_plugin.h: V2 Tool Plugins API
- plugin
- file: File Plugin
- perfetto: Perfetto Plugin
- ctf: CTF Plugin
- samples: Samples of how to use the API, and also input.txt input file samples for counter collection.
- script: Scripts needed for tracing
- src: Source files of the project
- api: API implementation for rocprofv2
- core: Core source files needed for the V1/V2 API
- counters: Basic and Derived Counters
- hardware: Hardware support
- hsa: Provides support for profiler and tracer to communicate with HSA
- queues: Intercepting HSA Queues
- packets: Packets Preparation for profiling
- memory: Memory Pool used in buffers that saves the output data
- session: Session Logic
- filter: Type of profiling or tracing and its properties
- tracer: Tracing support of the session
- profiler: Profiling support of the session
- spm: SPM support of the session
- tools: Tools needed to run profiling and tracing
- rocsys: Controlling Session from another CLI
- utils: Utilities needed by the project
- tests: Tests folder
- CMakeLists.txt: Handles cmake list for the whole project
- build.sh: To easily build and compile rocprofiler
- CHANGELOG.md: Changes that are happening per release
## Support
Please report in the Github Issues
## Limitations
@@ -0,0 +1,601 @@
/******************************************************************************
Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
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 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.
*******************************************************************************/
////////////////////////////////////////////////////////////////////////////////
//
// ROC Profiler API
//
// The goal of the implementation is to provide a HW specific low-level
// performance analysis interface for profiling of GPU compute applications.
// The profiling includes HW performance counters (PMC) with complex
// performance metrics and traces.
//
// The library can be used by a tool library loaded by HSA runtime or by
// higher level HW independent performance analysis API like PAPI.
//
// The library is written on C and will be based on AQLprofile AMD specific
// HSA extension. The library implementation requires HSA API intercepting and
// a profiling queue supporting a submit callback interface.
//
//
#ifndef INC_ROCPROFILER_H_
#define INC_ROCPROFILER_H_
/* Placeholder for calling convention and import/export macros */
#if !defined(ROCPROFILER_CALL)
#define ROCPROFILER_CALL
#endif /* !defined (ROCPROFILER_CALL) */
#if !defined(ROCPROFILER_EXPORT_DECORATOR)
#if defined(__GNUC__)
#define ROCPROFILER_EXPORT_DECORATOR __attribute__((visibility("default")))
#elif defined(_MSC_VER)
#define ROCPROFILER_EXPORT_DECORATOR __declspec(dllexport)
#endif /* defined (_MSC_VER) */
#endif /* !defined (ROCPROFILER_EXPORT_DECORATOR) */
#if !defined(ROCPROFILER_IMPORT_DECORATOR)
#if defined(__GNUC__)
#define ROCPROFILER_IMPORT_DECORATOR
#elif defined(_MSC_VER)
#define ROCPROFILER_IMPORT_DECORATOR __declspec(dllimport)
#endif /* defined (_MSC_VER) */
#endif /* !defined (ROCPROFILER_IMPORT_DECORATOR) */
#define ROCPROFILER_EXPORT ROCPROFILER_EXPORT_DECORATOR ROCPROFILER_CALL
#define ROCPROFILER_IMPORT ROCPROFILER_IMPORT_DECORATOR ROCPROFILER_CALL
#if !defined(ROCPROFILER)
#if defined(ROCPROFILER_EXPORTS)
#define ROCPROFILER_API ROCPROFILER_EXPORT
#else /* !defined (ROCPROFILER_EXPORTS) */
#define ROCPROFILER_API ROCPROFILER_IMPORT
#endif /* !defined (ROCPROFILER_EXPORTS) */
#endif /* !defined (ROCPROFILER) */
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include <hsa/amd_hsa_kernel_code.h>
#include <hsa/hsa.h>
#include <hsa/hsa_ext_amd.h>
#include <hsa/hsa_ven_amd_aqlprofile.h>
#include <stdint.h>
#define ROCPROFILER_VERSION_MAJOR 8
#define ROCPROFILER_VERSION_MINOR 0
////////////////////////////////////////////////////////////////////////////////
// Returning library version
uint32_t rocprofiler_version_major();
uint32_t rocprofiler_version_minor();
////////////////////////////////////////////////////////////////////////////////
// Global properties structure
typedef struct {
uint32_t intercept_mode;
uint32_t code_obj_tracking;
uint32_t memcopy_tracking;
uint32_t trace_size;
uint32_t trace_local;
uint64_t timeout;
uint32_t timestamp_on;
uint32_t hsa_intercepting;
uint32_t k_concurrent;
uint32_t opt_mode;
uint32_t obj_dumping;
} rocprofiler_settings_t;
////////////////////////////////////////////////////////////////////////////////
// Returning the error string method
hsa_status_t rocprofiler_error_string(
const char** str); // [out] the API error string pointer returning
////////////////////////////////////////////////////////////////////////////////
// Profiling features and data
//
// Profiling features objects have profiling feature info, type, parameters and data
// Also profiling data samplaes can be iterated using a callback
// Profiling feature kind
typedef enum {
ROCPROFILER_FEATURE_KIND_METRIC = 0,
ROCPROFILER_FEATURE_KIND_TRACE = 1,
ROCPROFILER_FEATURE_KIND_SPM_MOD = 2,
ROCPROFILER_FEATURE_KIND_PCSMP_MOD = 4
} rocprofiler_feature_kind_t;
// Profiling feture parameter
typedef hsa_ven_amd_aqlprofile_parameter_t rocprofiler_parameter_t;
// Profiling data kind
typedef enum {
ROCPROFILER_DATA_KIND_UNINIT = 0,
ROCPROFILER_DATA_KIND_INT32 = 1,
ROCPROFILER_DATA_KIND_INT64 = 2,
ROCPROFILER_DATA_KIND_FLOAT = 3,
ROCPROFILER_DATA_KIND_DOUBLE = 4,
ROCPROFILER_DATA_KIND_BYTES = 5
} rocprofiler_data_kind_t;
// Profiling data type
typedef struct {
rocprofiler_data_kind_t kind; // result kind
union {
uint32_t result_int32; // 32bit integer result
uint64_t result_int64; // 64bit integer result
float result_float; // float single-precision result
double result_double; // float double-precision result
struct {
void* ptr;
uint32_t size;
uint32_t instance_count;
bool copy;
} result_bytes; // data by ptr and byte size
};
} rocprofiler_data_t;
// Profiling feature type
typedef struct {
rocprofiler_feature_kind_t kind; // feature kind
union {
const char* name; // feature name
struct {
const char* block; // counter block name
uint32_t event; // counter event id
} counter;
};
const rocprofiler_parameter_t* parameters; // feature parameters array
uint32_t parameter_count; // feature parameters count
rocprofiler_data_t data; // profiling data
} rocprofiler_feature_t;
// Profiling features set type
typedef void rocprofiler_feature_set_t;
////////////////////////////////////////////////////////////////////////////////
// Profiling context
//
// Profiling context object accumuate all profiling information
// Profiling context object
typedef void rocprofiler_t;
// Profiling group object
typedef struct {
unsigned index; // group index
rocprofiler_feature_t** features; // profiling info array
uint32_t feature_count; // profiling info count
rocprofiler_t* context; // context object
} rocprofiler_group_t;
// Profiling mode mask
typedef enum {
ROCPROFILER_MODE_STANDALONE = 1, // standalone mode when ROC profiler supports a queue
ROCPROFILER_MODE_CREATEQUEUE = 2, // ROC profiler creates queue in standalone mode
ROCPROFILER_MODE_SINGLEGROUP = 4 // only one group is allowed, failed otherwise
} rocprofiler_mode_t;
// Profiling handler, calling on profiling completion
typedef bool (*rocprofiler_handler_t)(rocprofiler_group_t group, void* arg);
// Profiling preperties
typedef struct {
hsa_queue_t* queue; // queue for STANDALONE mode
// the queue is created and returned in CREATEQUEUE mode
uint32_t queue_depth; // created queue depth
rocprofiler_handler_t handler; // handler on completion
void* handler_arg; // the handler arg
} rocprofiler_properties_t;
// Create new profiling context
hsa_status_t rocprofiler_open(hsa_agent_t agent, // GPU handle
rocprofiler_feature_t* features, // [in] profiling features array
uint32_t feature_count, // profiling info count
rocprofiler_t** context, // [out] context object
uint32_t mode, // profiling mode mask
rocprofiler_properties_t* properties); // profiling properties
// Add feature to a features set
hsa_status_t rocprofiler_add_feature(
const rocprofiler_feature_t* feature, // [in]
rocprofiler_feature_set_t* features_set); // [in/out] profiling features set
// Create new profiling context
hsa_status_t rocprofiler_features_set_open(
hsa_agent_t agent, // GPU handle
rocprofiler_feature_set_t* features_set, // [in] profiling features set
rocprofiler_t** context, // [out] context object
uint32_t mode, // profiling mode mask
rocprofiler_properties_t* properties); // profiling properties
// Delete profiling info
hsa_status_t rocprofiler_close(rocprofiler_t* context); // [in] profiling context
// Context reset before reusing
hsa_status_t rocprofiler_reset(rocprofiler_t* context, // [in] profiling context
uint32_t group_index); // group index
// Return context agent
hsa_status_t rocprofiler_get_agent(rocprofiler_t* context, // [in] profiling context
hsa_agent_t* agent); // [out] GPU handle
// Supported time value ID
typedef enum {
ROCPROFILER_TIME_ID_CLOCK_REALTIME = 0, // Linux realtime clock time
ROCPROFILER_TIME_ID_CLOCK_REALTIME_COARSE = 1, // Linux realtime-coarse clock time
ROCPROFILER_TIME_ID_CLOCK_MONOTONIC = 2, // Linux monotonic clock time
ROCPROFILER_TIME_ID_CLOCK_MONOTONIC_COARSE = 3, // Linux monotonic-coarse clock time
ROCPROFILER_TIME_ID_CLOCK_MONOTONIC_RAW = 4, // Linux monotonic-raw clock time
} rocprofiler_time_id_t;
// Return time value for a given time ID and profiling timestamp
hsa_status_t rocprofiler_get_time(
rocprofiler_time_id_t time_id, // identifier of the particular time to convert the timesatmp
uint64_t timestamp, // profiling timestamp
uint64_t* value_ns, // [out] returned time 'ns' value, ignored if NULL
uint64_t* error_ns); // [out] returned time error 'ns' value, ignored if NULL
////////////////////////////////////////////////////////////////////////////////
// Queue callbacks
//
// Queue callbacks for initiating profiling per kernel dispatch and to wait
// the profiling data on the queue destroy.
// Dispatch record
typedef struct {
uint64_t dispatch; // dispatch timestamp, ns
uint64_t begin; // kernel begin timestamp, ns
uint64_t end; // kernel end timestamp, ns
uint64_t complete; // completion signal timestamp, ns
} rocprofiler_dispatch_record_t;
// Profiling callback data
typedef struct {
hsa_agent_t agent; // GPU agent handle
uint32_t agent_index; // GPU index (GPU Driver Node ID as reported in the sysfs topology)
const hsa_queue_t* queue; // HSA queue
uint64_t queue_index; // Index in the queue
uint32_t queue_id; // Queue id
hsa_signal_t completion_signal; // Completion signal
const hsa_kernel_dispatch_packet_t* packet; // HSA dispatch packet
const char* kernel_name; // Kernel name
uint64_t kernel_object; // Kernel object address
const amd_kernel_code_t* kernel_code; // Kernel code pointer
uint32_t thread_id; // Thread id
const rocprofiler_dispatch_record_t* record; // Dispatch record
} rocprofiler_callback_data_t;
// Profiling callback type
typedef hsa_status_t (*rocprofiler_callback_t)(
const rocprofiler_callback_data_t* callback_data, // [in] callback data
void* user_data, // [in/out] user data passed to the callback
rocprofiler_group_t* group); // [out] returned profiling group
// Queue callbacks
typedef struct {
rocprofiler_callback_t dispatch; // dispatch callback
hsa_status_t (*create)(hsa_queue_t* queue, void* data); // create callback
hsa_status_t (*destroy)(hsa_queue_t* queue, void* data); // destroy callback
} rocprofiler_queue_callbacks_t;
// Set queue callbacks
hsa_status_t rocprofiler_set_queue_callbacks(rocprofiler_queue_callbacks_t callbacks, // callbacks
void* data); // [in/out] passed callbacks data
// Remove queue callbacks
hsa_status_t rocprofiler_remove_queue_callbacks();
// Start/stop queue callbacks
hsa_status_t rocprofiler_start_queue_callbacks();
hsa_status_t rocprofiler_stop_queue_callbacks();
////////////////////////////////////////////////////////////////////////////////
// Start/stop profiling
//
// Start/stop the context profiling invocation, have to be as many as
// contect.invocations' to collect all profiling data
// Start profiling
hsa_status_t rocprofiler_start(rocprofiler_t* context, // [in/out] profiling context
uint32_t group_index); // group index
// Stop profiling
hsa_status_t rocprofiler_stop(rocprofiler_t* context, // [in/out] profiling context
uint32_t group_index); // group index
// Read profiling
hsa_status_t rocprofiler_read(rocprofiler_t* context, // [in/out] profiling context
uint32_t group_index); // group index
// Read profiling data
hsa_status_t rocprofiler_get_data(rocprofiler_t* context, // [in/out] profiling context
uint32_t group_index); // group index
// Get profiling groups count
hsa_status_t rocprofiler_group_count(const rocprofiler_t* context, // [in] profiling context
uint32_t* group_count); // [out] profiling groups count
// Get profiling group for a given index
hsa_status_t rocprofiler_get_group(rocprofiler_t* context, // [in] profiling context
uint32_t group_index, // profiling group index
rocprofiler_group_t* group); // [out] profiling group
// Start profiling
hsa_status_t rocprofiler_group_start(rocprofiler_group_t* group); // [in/out] profiling group
// Stop profiling
hsa_status_t rocprofiler_group_stop(rocprofiler_group_t* group); // [in/out] profiling group
// Read profiling
hsa_status_t rocprofiler_group_read(rocprofiler_group_t* group); // [in/out] profiling group
// Get profiling data
hsa_status_t rocprofiler_group_get_data(rocprofiler_group_t* group); // [in/out] profiling group
// Get metrics data
hsa_status_t rocprofiler_get_metrics(const rocprofiler_t* context); // [in/out] profiling context
// Definition of output data iterator callback
typedef hsa_ven_amd_aqlprofile_data_callback_t rocprofiler_trace_data_callback_t;
// Method for iterating the events output data
hsa_status_t rocprofiler_iterate_trace_data(
rocprofiler_t* context, // [in] profiling context
rocprofiler_trace_data_callback_t callback, // callback to iterate the output data
void* data); // [in/out] callback data
////////////////////////////////////////////////////////////////////////////////
// Profiling features and data
//
// Profiling features objects have profiling feature info, type, parameters and data
// Also profiling data samplaes can be iterated using a callback
// Profiling info kind
typedef enum {
ROCPROFILER_INFO_KIND_METRIC = 0, // metric info
ROCPROFILER_INFO_KIND_METRIC_COUNT = 1, // metric features count, int32
ROCPROFILER_INFO_KIND_TRACE = 2, // trace info
ROCPROFILER_INFO_KIND_TRACE_COUNT = 3, // trace features count, int32
ROCPROFILER_INFO_KIND_TRACE_PARAMETER = 4, // trace parameter info
ROCPROFILER_INFO_KIND_TRACE_PARAMETER_COUNT = 5 // trace parameter count, int32
} rocprofiler_info_kind_t;
// Profiling info query
typedef union {
rocprofiler_info_kind_t info_kind; // queried profiling info kind
struct {
const char* trace_name; // queried info trace name
} trace_parameter;
} rocprofiler_info_query_t;
// Profiling info data
typedef struct {
uint32_t
agent_index; // GPU HSA agent index (GPU Driver Node ID as reported in the sysfs topology)
rocprofiler_info_kind_t kind; // info data kind
union {
struct {
const char* name; // metric name
uint32_t instances; // instances number
const char* expr; // metric expression, NULL for basic counters
const char* description; // metric description
const char* block_name; // block name
uint32_t block_counters; // number of block counters
} metric;
struct {
const char* name; // trace name
const char* description; // trace description
uint32_t parameter_count; // supported by the trace number parameters
} trace;
struct {
uint32_t code; // parameter code
const char* trace_name; // trace name
const char* parameter_name; // parameter name
const char* description; // trace parameter description
} trace_parameter;
};
} rocprofiler_info_data_t;
// Return the info for a given info kind
hsa_status_t rocprofiler_get_info(const hsa_agent_t* agent, // [in] GFXIP handle
rocprofiler_info_kind_t kind, // kind of iterated info
void* data); // [in/out] returned data
// Iterate over the info for a given info kind, and invoke an application-defined callback on every
// iteration
hsa_status_t rocprofiler_iterate_info(const hsa_agent_t* agent, // [in] GFXIP handle
rocprofiler_info_kind_t kind, // kind of iterated info
hsa_status_t (*callback)(const rocprofiler_info_data_t info,
void* data), // callback
void* data); // [in/out] data passed to callback
// Iterate over the info for a given info query, and invoke an application-defined callback on every
// iteration
hsa_status_t rocprofiler_query_info(const hsa_agent_t* agent, // [in] GFXIP handle
rocprofiler_info_query_t query, // iterated info query
hsa_status_t (*callback)(const rocprofiler_info_data_t info,
void* data), // callback
void* data); // [in/out] data passed to callback
// Create a profiled queue. All dispatches on this queue will be profiled
hsa_status_t rocprofiler_queue_create_profiled(
hsa_agent_t agent_handle, uint32_t size, hsa_queue_type32_t type,
void (*callback)(hsa_status_t status, hsa_queue_t* source, void* data), void* data,
uint32_t private_segment_size, uint32_t group_segment_size, hsa_queue_t** queue);
////////////////////////////////////////////////////////////////////////////////
// Profiling pool
//
// Support for profiling contexts pool
// The API provide capability to create a contexts pool for a given agent and a set of features,
// to fetch/relase a context entry, to register a callback for the contexts completion.
// Profiling pool handle
typedef void rocprofiler_pool_t;
// Profiling pool entry
typedef struct {
rocprofiler_t* context; // context object
void* payload; // payload data object
} rocprofiler_pool_entry_t;
// Profiling handler, calling on profiling completion
typedef bool (*rocprofiler_pool_handler_t)(const rocprofiler_pool_entry_t* entry, void* arg);
// Profiling preperties
typedef struct {
uint32_t num_entries; // pool size entries
uint32_t payload_bytes; // payload size bytes
rocprofiler_pool_handler_t handler; // handler on context completion
void* handler_arg; // the handler arg
} rocprofiler_pool_properties_t;
// Open profiling pool
hsa_status_t rocprofiler_pool_open(
hsa_agent_t agent, // GPU handle
rocprofiler_feature_t* features, // [in] profiling features array
uint32_t feature_count, // profiling info count
rocprofiler_pool_t** pool, // [out] context object
uint32_t mode, // profiling mode mask
rocprofiler_pool_properties_t*); // pool properties
// Close profiling pool
hsa_status_t rocprofiler_pool_close(rocprofiler_pool_t* pool); // profiling pool handle
// Fetch profiling pool entry
hsa_status_t rocprofiler_pool_fetch(
rocprofiler_pool_t* pool, // profiling pool handle
rocprofiler_pool_entry_t* entry); // [out] empty profiling pool entry
// Release profiling pool entry
hsa_status_t rocprofiler_pool_release(
rocprofiler_pool_entry_t* entry); // released profiling pool entry
// Iterate fetched profiling pool entries
hsa_status_t rocprofiler_pool_iterate(rocprofiler_pool_t* pool, // profiling pool handle
hsa_status_t (*callback)(rocprofiler_pool_entry_t* entry,
void* data), // callback
void* data); // [in/out] data passed to callback
// Flush completed entries in profiling pool
hsa_status_t rocprofiler_pool_flush(rocprofiler_pool_t* pool); // profiling pool handle
////////////////////////////////////////////////////////////////////////////////
// HSA intercepting API
// HSA callbacks ID enumeration
typedef enum {
ROCPROFILER_HSA_CB_ID_ALLOCATE = 0, // Memory allocate callback
ROCPROFILER_HSA_CB_ID_DEVICE = 1, // Device assign callback
ROCPROFILER_HSA_CB_ID_MEMCOPY = 2, // Memcopy callback
ROCPROFILER_HSA_CB_ID_SUBMIT = 3, // Packet submit callback
ROCPROFILER_HSA_CB_ID_KSYMBOL = 4, // Loading/unloading of kernel symbol
ROCPROFILER_HSA_CB_ID_CODEOBJ = 5 // Loading/unloading of kernel symbol
} rocprofiler_hsa_cb_id_t;
// HSA callback data type
typedef struct {
union {
struct {
const void* ptr; // allocated area ptr
size_t size; // allocated area size, zero size means 'free' callback
hsa_amd_segment_t segment; // allocated area's memory segment type
hsa_amd_memory_pool_global_flag_t global_flag; // allocated area's memory global flag
int is_code; // equal to 1 if code is allocated
} allocate;
struct {
hsa_device_type_t type; // type of assigned device
uint32_t id; // id of assigned device
hsa_agent_t agent; // device HSA agent handle
const void* ptr; // ptr the device is assigned to
} device;
struct {
const void* dst; // memcopy dst ptr
const void* src; // memcopy src ptr
size_t size; // memcopy size bytes
} memcopy;
struct {
const void* packet; // submitted to GPU packet
const char* kernel_name; // kernel name, not NULL if dispatch
hsa_queue_t* queue; // HSA queue the kernel was submitted to
uint32_t device_type; // type of device the packed is submitted to
uint32_t device_id; // id of device the packed is submitted to
} submit;
struct {
uint64_t object; // kernel symbol object
const char* name; // kernel symbol name
uint32_t name_length; // kernel symbol name length
int unload; // symbol executable destroy
} ksymbol;
struct {
uint32_t storage_type; // code object storage type
int storage_file; // origin file descriptor
uint64_t memory_base; // origin memory base
uint64_t memory_size; // origin memory size
uint64_t load_base; // codeobj load base
uint64_t load_size; // codeobj load size
uint64_t load_delta; // codeobj load size
uint32_t uri_length; // URI string length
char* uri; // URI string
int unload; // unload flag
} codeobj;
};
} rocprofiler_hsa_callback_data_t;
// HSA callback function type
typedef hsa_status_t (*rocprofiler_hsa_callback_fun_t)(
rocprofiler_hsa_cb_id_t id, // callback id
const rocprofiler_hsa_callback_data_t* data, // [in] callback data
void* arg); // [in/out] user passed data
// HSA callbacks structure
typedef struct {
rocprofiler_hsa_callback_fun_t allocate; // memory allocate callback
rocprofiler_hsa_callback_fun_t device; // agent assign callback
rocprofiler_hsa_callback_fun_t memcopy; // memory copy callback
rocprofiler_hsa_callback_fun_t submit; // packet submit callback
rocprofiler_hsa_callback_fun_t ksymbol; // kernel symbol callback
rocprofiler_hsa_callback_fun_t codeobj; // codeobject load/unload callback
} rocprofiler_hsa_callbacks_t;
// Set callbacks. If the callback is NULL then it is disabled.
// If callback returns a value that is not HSA_STATUS_SUCCESS the callback
// will be unregistered.
hsa_status_t rocprofiler_set_hsa_callbacks(
const rocprofiler_hsa_callbacks_t callbacks, // HSA callback function
void* arg); // callback user data
#ifdef __cplusplus
} // extern "C" block
#endif // __cplusplus
#endif // INC_ROCPROFILER_H_
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,137 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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. */
/** \section rocprofiler_plugin_api ROCProfiler Plugin API
*
* The ROCProfiler Plugin API is used by the ROCProfiler Tool to output all
* profiling information. Different implementations of the ROCProfiler Plugin
* API can be developed that output the data in different formats. The
* ROCProfiler Tool can be configured to load a specific library that supports
* the user desired format.
*
* The API is not thread safe. It is the responsibility of the ROCProfiler Tool
* to ensure the operations are synchronized and not called concurrently. There
* is no requirement for the ROCProfiler Tool to report trace data in any
* specific order. If the format supported by plugin requires specific
* ordering, it is the responsibility of the plugin implementation to perform
* any necessary sorting.
*/
/**
* \file
* ROCProfiler Tool Plugin API interface.
*/
#ifndef ROCPROFILER_PLUGIN_H_
#define ROCPROFILER_PLUGIN_H_
#include <stdint.h>
#include "rocprofiler.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/** \defgroup rocprofiler_plugins ROCProfiler Plugin API Specification
* @{
*/
/** \defgroup initialization_group Initialization and Finalization
* \ingroup rocprofiler_plugins
*
* The ROCProfiler Plugin API must be initialized before using any of the
* operations to report trace data, and finalized after the last trace data has
* been reported.
*
* @{
*/
/**
* Initialize plugin.
* Must be called before any other operation.
*
* @param[in] rocprofiler_major_version The major version of the ROCProfiler API
* being used by the ROCProfiler Tool. An error is reported if this does not
* match the major version of the ROCProfiler API used to build the plugin
* library. This ensures compatibility of the trace data format.
* @param[in] rocprofiler_minor_version The minor version of the ROCProfiler API
* being used by the ROCProfiler Tool. An error is reported if the
* \p rocprofiler_major_version matches and this is greater than the minor
* version of the ROCProfiler API used to build the plugin library. This ensures
* compatibility of the trace data format.
* @param[in] data Pointer to the data passed to the ROCProfiler Plugin by the tool
* @return Returns 0 on success and -1 on error.
*/
ROCPROFILER_EXPORT int rocprofiler_plugin_initialize(uint32_t rocprofiler_major_version,
uint32_t rocprofiler_minor_version,
void* data);
/**
* Finalize plugin.
* This must be called after ::rocprofiler_plugin_initialize and after all
* profiling data has been reported by
* ::rocprofiler_plugin_write_kernel_records
*/
ROCPROFILER_EXPORT void rocprofiler_plugin_finalize();
/** @} */
/** \defgroup profiling_record_write_functions Profiling data reporting
* \ingroup rocprofiler_plugins
* Operations to output profiling data.
* @{
*/
// TODO(aelwazir): Recheck wording of the description
/**
* Report Buffer Records.
*
* @param[in] begin Pointer to the first record.
* @param[in] end Pointer to one past the last record.
* @param[in] session_id Session ID
* @param[in] buffer_id Buffer ID
* @return Returns 0 on success and -1 on error.
*/
ROCPROFILER_EXPORT int rocprofiler_plugin_write_buffer_records(
const rocprofiler_record_header_t* begin, const rocprofiler_record_header_t* end,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id);
/**
* Report Synchronous Record.
*
* @param[in] record Synchronous Tracer record.
* @param[in] data : api_data
* @param[in] tracer_data :Tracer record extra data such as function name and kernel name
* @return Returns 0 on success and -1 on error.
*/
ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(rocprofiler_record_tracer_t record);
/** @} */
/** @} */
#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */
#endif /* ROCPROFILER_PLUGIN_H_ */
@@ -0,0 +1,43 @@
################################################################################
# Copyright (c) 2022 Advanced Micro Devices, Inc.
#
# 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 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.
################################################################################
if(ROCPROFILER_BUILD_CODECOV)
set(CMAKE_BUILD_TYPE "Coverage")
endif()
# these two "native" plugins are always built
add_subdirectory(cli)
add_subdirectory(file)
if(ROCPROFILER_BUILD_PLUGIN_PERFETTO)
add_subdirectory(perfetto)
endif()
if(ROCPROFILER_BUILD_PLUGIN_ATT)
add_subdirectory(att)
endif()
if(ROCPROFILER_BUILD_PLUGIN_CTF)
add_subdirectory(ctf)
endif()
add_subdirectory(json)
@@ -0,0 +1,67 @@
# ##############################################################################
# # Copyright (c) 2022 Advanced Micro Devices, Inc. # # 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 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.
# ##############################################################################
find_library(
ROCPROFV2_ATT rocprofv2_att
HINTS ${CMAKE_INSTALL_PREFIX}
PATHS ${ROCM_PATH}
PATH_SUFFIXES hsa-amd-aqlprofile)
set(ENV{ROCPROFV2_ATT_LIB_PATH} $ROCPROFV2_ATT)
# Building att plugin library
file(GLOB ROCPROFILER_UTIL_SRC_FILES ${PROJECT_SOURCE_DIR}/src/utils/helper.cpp)
file(GLOB FILE_SOURCES att.cpp disassembly.cpp code_printing.cpp)
add_library(att_plugin SHARED ${FILE_SOURCES} ${ROCPROFILER_UTIL_SRC_FILES})
set_target_properties(
att_plugin
PROPERTIES CXX_VISIBILITY_PRESET hidden
LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib/rocprofiler
INSTALL_RPATH "${ROCM_APPEND_PRIVLIB_RPATH}")
target_compile_definitions(att_plugin PRIVATE HIP_PROF_HIP_API_STRING=1
__HIP_PLATFORM_AMD__=1)
target_include_directories(att_plugin PRIVATE ${PROJECT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR})
target_link_options(
att_plugin PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
-Wl,--no-undefined)
target_link_libraries(att_plugin PRIVATE rocprofiler-v2 hsa-runtime64::hsa-runtime64 stdc++fs dw elf amd_comgr)
install(TARGETS att_plugin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
COMPONENT asan)
install(TARGETS att_plugin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
COMPONENT runtime)
configure_file(att.py att/att.py COPYONLY)
configure_file(trace_view.py att/trace_view.py COPYONLY)
configure_file(stitch.py att/stitch.py COPYONLY)
configure_file(drawing.py att/drawing.py COPYONLY)
configure_file(att_to_csv.py att/att_to_csv.py COPYONLY)
configure_file(service.py att/service.py COPYONLY)
configure_file(ui/index.html att/ui/index.html COPYONLY)
configure_file(ui/logo.svg att/ui/logo.svg COPYONLY)
configure_file(ui/styles.css att/ui/styles.css COPYONLY)
configure_file(ui/httpserver.py att/ui/httpserver.py COPYONLY)
install(
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/att
DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/rocprofiler
COMPONENT runtime)
+309
View File
@@ -0,0 +1,309 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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 <cxxabi.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <memory>
#include <optional>
#include <ostream>
#include <sstream>
#include <string>
#include <hsa/hsa.h>
#include <mutex>
#include <sys/stat.h>
#include <regex>
#include "rocprofiler.h"
#include "rocprofiler_plugin.h"
#include "../utils.h"
#include "../../src/core/session/att/att_header.h"
#include "src/utils/filesystem.hpp"
#define ATT_FILENAME_MAXBYTES 90
#define TEST_INVALID_KERNEL size_t(-1)
static bool env_var_search(std::string& s) {
std::smatch m;
std::regex e("(.*)\\%\\q\\{([^}]+)\\}(.*)");
std::regex_match(s, m, e);
if (m.size() != 4) return false;
while (m.size() == 4) {
const char* envvar = getenv(m[2].str().c_str());
if (!envvar) envvar = "";
s = m[1].str() + envvar + m[3].str();
std::regex_match(s, m, e);
};
return true;
}
class att_plugin_t {
public:
att_plugin_t(void* data) {
std::vector<const char*> mpivars = {"MPI_RANK", "OMPI_COMM_WORLD_RANK", "MV2_COMM_WORLD_RANK"};
for (const char* envvar : mpivars)
if (const char* env = getenv(envvar)) {
MPI_RANK = atoi(env);
MPI_ENABLE = true;
break;
}
header.raw = reinterpret_cast<uint64_t>(data);
header.reserved = 0x11;
}
bool MPI_ENABLE = false;
int MPI_RANK = 0;
static std::mutex writing_lock;
bool is_valid_{true};
rocprofiler::att_header_packet_t header{.raw = 0};
std::string output_dir = ".";
bool CheckAddrMatches(uint64_t kernel_addr, uint64_t base_address, uint64_t size)
{
return (kernel_addr >= base_address) && (kernel_addr < base_address + size);
}
void InitOutputDir()
{
static bool bIsInit = false;
if (bIsInit) return;
bIsInit = true;
if (const char* env = getenv("OUTPUT_PATH")) output_dir = std::string(env);
env_var_search(output_dir);
if (!output_dir.size()) return;
try {
rocprofiler::common::filesystem::create_directories(output_dir);
} catch (...) {}
output_dir += '/';
}
inline bool att_file_exists(const std::string& name) {
struct stat buffer;
return stat(name.c_str(), &buffer) == 0;
}
bool IsValid() const { return is_valid_; }
int FlushATTRecord(const rocprofiler_record_att_tracer_t* att_tracer_record,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
if (!att_tracer_record) return ROCPROFILER_STATUS_ERROR;
InitOutputDir();
std::string kernel_name_mangled;
// Found problem with rocprofiler API for invalid kernel_ids;
if (att_tracer_record->kernel_id.handle != TEST_INVALID_KERNEL) {
size_t name_length;
CHECK_ROCPROFILER(rocprofiler_query_kernel_info_size(
ROCPROFILER_KERNEL_NAME, att_tracer_record->kernel_id, &name_length));
const char* kernel_name_c = nullptr;
CHECK_ROCPROFILER(rocprofiler_query_kernel_info(
ROCPROFILER_KERNEL_NAME, att_tracer_record->kernel_id, &kernel_name_c));
assert(kernel_name_c && "Rocprofv2 returned an invalid kernel name");
kernel_name_mangled = std::string(kernel_name_c);
free(const_cast<char*>(kernel_name_c));
} else { // Temporary. Adding a valid string.
kernel_name_mangled = "test_kernel";
}
std::string name_demangled =
rocprofiler::truncate_name(rocprofiler::cxx_demangle(kernel_name_mangled));
if (name_demangled.size() > ATT_FILENAME_MAXBYTES) // Limit filename size
name_demangled = name_demangled.substr(0, ATT_FILENAME_MAXBYTES);
std::string outfilepath = output_dir + '/' + name_demangled;
outfilepath.reserve(output_dir.size() + 128); // Max filename size
if (MPI_ENABLE) outfilepath += "_rank" + std::to_string(MPI_RANK);
outfilepath += "_v";
// Find if this filename already exists. If so, increment vname.
int file_iteration = 0;
while (att_file_exists(outfilepath + std::to_string(file_iteration) + "_kernel.txt"))
file_iteration += 1;
outfilepath += std::to_string(file_iteration);
auto writer_id = att_tracer_record->writer_id;
std::string fname = outfilepath + "_kernel.txt";
std::ofstream kernel_txt_file((outfilepath + "_kernel.txt").c_str());
kernel_txt_file << name_demangled << " dispatch[" << writer_id << "] GPU["
<< att_tracer_record->gpu_id.handle << "]: " << kernel_name_mangled
<< '\n';
// iterate over each shader engine att trace
header.navi = !(att_tracer_record->intercept_list.userdata & 0x1);
int se_num = att_tracer_record->shader_engine_data_count;
for (int i = 0; i < se_num; i++)
{
if (!att_tracer_record->shader_engine_data ||
!att_tracer_record->shader_engine_data[i].buffer_ptr)
continue;
printf("--------------collecting data for shader_engine %d---------------\n", i);
header.SEID = i;
rocprofiler_record_se_att_data_t* se_att_trace = &att_tracer_record->shader_engine_data[i];
char* data_buffer_ptr = reinterpret_cast<char*>(se_att_trace->buffer_ptr);
// dump data in binary format
std::ofstream out(outfilepath + "_se" + std::to_string(i) + ".att", std::ios::binary);
if (!out.is_open()) {
std::cerr << "ATT Failed to open file: " << outfilepath << "_se" << i << ".att\n";
return ROCPROFILER_STATUS_ERROR;
}
if (header.enable && !header.navi)
out.write((const char*)&header, sizeof(header.raw));
out.write(data_buffer_ptr, se_att_trace->buffer_size);
}
for (size_t i = 0; i < att_tracer_record->intercept_list.count; i++)
{
const auto& symbol = att_tracer_record->intercept_list.symbols[i];
if (!symbol.filepath) continue;
std::string sfilepath(symbol.filepath);
bool bCopiedData = symbol.data && symbol.data_size;
if (bCopiedData)
{
auto pos = sfilepath.find("://");
auto rpos = sfilepath.rfind('/');
if (pos == std::string::npos || pos+3 >= sfilepath.size()) continue;
std::string type(sfilepath.begin(), sfilepath.begin()+pos);
std::string cut(sfilepath.begin()+rpos+1, sfilepath.end());
sfilepath = type + cut + ".out";
}
kernel_txt_file << std::hex << "0x" << symbol.base_address << " 0x" << symbol.mem_size
<< ' ' << std::dec << symbol.att_marker_id << ' ' << sfilepath << '\n';
sfilepath = output_dir + '/' + sfilepath;
if (!bCopiedData || att_file_exists(sfilepath)) continue;
std::ofstream isafile(sfilepath, std::ios::binary);
if (!isafile.is_open()) {
std::cerr << "Could not open file: " << sfilepath << std::endl;
return ROCPROFILER_STATUS_ERROR;
}
isafile.write(symbol.data, symbol.data_size);
}
return ROCPROFILER_STATUS_SUCCESS;
}
int WriteBufferRecords(const rocprofiler_record_header_t* begin,
const rocprofiler_record_header_t* end,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
while (begin < end) {
if (!begin) return ROCPROFILER_STATUS_ERROR;
switch (begin->kind) {
case ROCPROFILER_PROFILER_RECORD:
case ROCPROFILER_TRACER_RECORD:
case ROCPROFILER_PC_SAMPLING_RECORD:
case ROCPROFILER_SPM_RECORD:
case ROCPROFILER_COUNTERS_SAMPLER_RECORD:
rocprofiler::warning("Invalid record Kind: %d\n", begin->kind);
break;
case ROCPROFILER_ATT_TRACER_RECORD: {
rocprofiler_record_att_tracer_t* att_record =
const_cast<rocprofiler_record_att_tracer_t*>(
reinterpret_cast<const rocprofiler_record_att_tracer_t*>(begin));
FlushATTRecord(att_record, session_id, buffer_id);
break;
}
}
int status = rocprofiler_next_record(begin, &begin, session_id, buffer_id);
if (status != ROCPROFILER_STATUS_SUCCESS) return status;
}
return ROCPROFILER_STATUS_SUCCESS;
}
private:
};
att_plugin_t* att_plugin = nullptr;
std::mutex att_plugin_t::writing_lock;
ROCPROFILER_EXPORT int rocprofiler_plugin_initialize(uint32_t rocprofiler_major_version,
uint32_t rocprofiler_minor_version,
void* data) {
if (rocprofiler_major_version != ROCPROFILER_VERSION_MAJOR ||
rocprofiler_minor_version < ROCPROFILER_VERSION_MINOR)
return ROCPROFILER_STATUS_ERROR;
std::lock_guard<std::mutex> lock(att_plugin_t::writing_lock);
if (att_plugin != nullptr) return ROCPROFILER_STATUS_ERROR;
att_plugin = new att_plugin_t(data);
if (att_plugin->IsValid()) return ROCPROFILER_STATUS_SUCCESS;
// The plugin failed to initialied, destroy it and return an error.
delete att_plugin;
att_plugin = nullptr;
return ROCPROFILER_STATUS_ERROR;
}
ROCPROFILER_EXPORT void rocprofiler_plugin_finalize() {
std::lock_guard<std::mutex> lock(att_plugin_t::writing_lock);
if (!att_plugin) return;
delete att_plugin;
att_plugin = nullptr;
}
ROCPROFILER_EXPORT int rocprofiler_plugin_write_buffer_records(
const rocprofiler_record_header_t* begin, const rocprofiler_record_header_t* end,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
std::lock_guard<std::mutex> lock(att_plugin_t::writing_lock);
if (!att_plugin || !att_plugin->IsValid()) return ROCPROFILER_STATUS_ERROR;
return att_plugin->WriteBufferRecords(begin, end, session_id, buffer_id);
}
ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(rocprofiler_record_tracer_t record) {
std::lock_guard<std::mutex> lock(att_plugin_t::writing_lock);
if (!att_plugin || !att_plugin->IsValid()) return ROCPROFILER_STATUS_ERROR;
if (record.header.id.handle == 0) return ROCPROFILER_STATUS_SUCCESS;
return ROCPROFILER_STATUS_SUCCESS;
}
+641
View File
@@ -0,0 +1,641 @@
#!/usr/bin/env python3
import sys
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3")
import os
import argparse
from pathlib import Path
from ctypes import *
import ctypes
from copy import deepcopy
from trace_view import view_trace
import sys
import glob
import numpy as np
import stitch
import gc
from collections import defaultdict
import service
ATT_VERSION = 5
class pcInfo(ctypes.Structure):
_fields_ = [
("addr", c_uint64),
("marker_id", c_uint64)
]
def to_v2_pc(self):
if self.marker_id == 0:
return self.addr
return self.addr | (self.marker_id<<service.ID_OFFSET) | (1<<service.HEADER_OFFSET)
class TraceData(ctypes.Structure):
_fields_ = [
("type", c_uint64, 8),
("hitcount", c_uint64, 56),
("latency", c_uint64),
("pc", pcInfo)
]
class TraceDataTranslated:
def __init__(self, inst):
self.type = inst.type
self.num_waves = inst.hitcount
self.cycles = inst.latency
if self.type == stitch.PCINFO:
self.cycles = inst.pc.to_v2_pc()
class Trace:
def __init__(self, traceid, tracesize, instructions_array):
self.instructions = [TraceDataTranslated(instructions_array[k]) for k in range(tracesize)]
self.traceid = traceid
class PerfEvent(ctypes.Structure):
_fields_ = [
("time", c_uint64),
("event0", c_uint16),
("event1", c_uint16),
("event2", c_uint16),
("event3", c_uint16),
("cu", c_uint8),
("bank", c_uint8),
]
def toTuple(self):
return (
int(self.time),
int(self.event0),
int(self.event1),
int(self.event2),
int(self.event3),
int(self.cu),
int(self.bank),
)
class CodeWrapped(ctypes.Structure):
""" Matches CodeWrapped on the python side """
_fields_ = [('line', ctypes.c_char_p),
('loc', ctypes.c_char_p),
('to_line', ctypes.c_int),
('value', ctypes.c_int),
('index', ctypes.c_int),
('line_num', ctypes.c_int),
('addr', ctypes.c_int64)]
class KvPair(ctypes.Structure):
""" Matches pair<int, int> = (key, value) on the python side """
_fields_ = [('key', ctypes.c_int),
('value', ctypes.c_int)]
class ReturnAssemblyInfo(ctypes.Structure):
""" Matches ReturnAssemblyInfo on the python side """
_fields_ = [('code', POINTER(CodeWrapped)),
('jumps', POINTER(KvPair)),
('code_len', ctypes.c_int),
('jumps_len', ctypes.c_int)]
class WaveState(ctypes.Structure):
_fields_ = [
("type", c_int32),
("duration", c_int32)
]
class WaveInstruction(ctypes.Structure):
_fields_ = [
("time", c_int64),
("duration", c_int64)
]
class Wave(ctypes.Structure):
_fields_ = [
("simd", ctypes.c_uint8),
("wave_id", ctypes.c_uint8),
("trap_status", ctypes.c_uint8),
("reserved", ctypes.c_uint8),
# total VMEM/FLAT/LDS/SMEM instructions issued
# VMEM Pipeline: instrs and stalls
("num_vmem_instrs", ctypes.c_int),
("num_vmem_stalls", ctypes.c_int),
# FLAT instrs and stalls
("num_flat_instrs", ctypes.c_int),
("num_flat_stalls", ctypes.c_int),
# LDS instr and stalls
("num_lds_instrs", ctypes.c_int),
("num_lds_stalls", ctypes.c_int),
# SCA instrs stalls
("num_salu_instrs", ctypes.c_int),
("num_smem_instrs", ctypes.c_int),
("num_salu_stalls", ctypes.c_int),
("num_smem_stalls", ctypes.c_int),
# Branch
("num_branch_instrs", ctypes.c_int),
("num_branch_taken_instrs", ctypes.c_int),
("num_branch_stalls", ctypes.c_int),
# total issued memory instructions
("num_mem_instrs", ctypes.c_int),
# total valus insts and stalls
("num_valu_stalls", ctypes.c_int),
("num_valu_instrs", ctypes.c_size_t),
# total issued instructions (compute + memory)
("num_issued_instrs", ctypes.c_size_t),
# Begin and end cycle
("begin_time", ctypes.c_int64),
("end_time", ctypes.c_int64),
("traceid", ctypes.c_int64),
("timeline_size", ctypes.c_size_t),
("instructions_size", ctypes.c_size_t),
("timeline_array", POINTER(WaveState)),
("instructions_array", POINTER(WaveInstruction)),
]
class PythonWave:
def __init__(self, sourcew):
for property, value in Wave._fields_:
try:
setattr(self, deepcopy(property), deepcopy(getattr(sourcew, property)))
except:
pass
self.timeline = [
(int(sourcew.timeline_array[k].type), int(sourcew.timeline_array[k].duration))
for k in range(self.timeline_size)
]
self.timeline_array = None
self.instructions = [
(int(sourcew.instructions_array[k].time), int(sourcew.instructions_array[k].duration))
for k in range(self.instructions_size)
]
self.instructions_array = None
class ReturnInfo(ctypes.Structure):
_fields_ = [
("flags", ctypes.c_uint64),
("binaryID", ctypes.c_uint64),
("num_traces", ctypes.c_uint64),
("tracesizes", POINTER(ctypes.c_uint64)),
("traceIDs", POINTER(ctypes.c_int64)),
("tracedata", POINTER(POINTER(TraceData))),
("num_events", ctypes.c_uint64),
("perfevents", POINTER(PerfEvent)),
("occupancy", POINTER(ctypes.c_uint64)),
("num_occupancy", ctypes.c_uint64),
("kernel_id_addr", POINTER(pcInfo)),
("num_kernel_ids", ctypes.c_uint64),
("wavedata", POINTER(Wave)),
("num_waves", ctypes.c_uint64),
]
rocprofv2_att_lib = os.getenv("ROCPROFV2_ATT_LIB_PATH")
if rocprofv2_att_lib is None:
print(
"ATT Lib path not set. Use export ROCPROFV2_ATT_LIB_PATH=/path/to/librocprofv2_att.so"
)
quit()
path_to_parser = os.path.abspath(rocprofv2_att_lib)
SO = CDLL(path_to_parser)
SO.AnalyseBinary.restype = ReturnInfo
SO.AnalyseBinary.argtypes = [ctypes.c_char_p]
SO.wrapped_parse_binary.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
SO.wrapped_parse_binary.restype = ReturnAssemblyInfo
SO.FreeBinary.argtypes = [ctypes.c_uint64]
def parse_binary(filename, kernel=None):
if kernel is None or kernel == "":
kernel = ctypes.c_char_p(0)
else:
with open(glob.glob(kernel)[0], "r") as file:
kernel = file.readlines()
kernel = kernel[0].split(": ")[1].split(".kd")[0]
kernel = str(kernel).encode("utf-8")
filename = os.path.abspath(str(filename))
info = SO.wrapped_parse_binary(str(filename).encode("utf-8"), kernel)
code = []
for k in range(info.code_len):
code_entry = info.code[k]
line = deepcopy(code_entry.line.decode("utf-8"))
loc = deepcopy(code_entry.loc.decode("utf-8"))
to_line = int(code_entry.to_line) if (code_entry.to_line >= 0) else None
loc = loc if len(loc) > 0 else None
# asm, inst_type, addr, loc, index, line_num, hitcount, cycles
code.append([line, int(code_entry.value), to_line, loc, int(code_entry.index),
int(code_entry.line_num), int(code_entry.addr), 0, 0])
jumps = {}
for k in range(info.jumps_len):
jumps[info.jumps[k].key] = info.jumps[k].value
return code, jumps
def getWaves_binary(name):
filename = os.path.abspath(str(name))
info = SO.AnalyseBinary(filename.encode("utf-8"))
isValid = info.flags & 0x1
if isValid == 0:
print('Invalid trace ', name)
return ([], [], [], [], None, [])
flags = "navi" if (info.flags & 0x2) else "vega"
kernel_addr = [info.kernel_id_addr[k].to_v2_pc() for k in range(info.num_kernel_ids)]
events = [deepcopy(info.perfevents[k]) for k in range(info.num_events)]
occupancy = [int(info.occupancy[k]) for k in range(int(info.num_occupancy))]
assert(((info.flags >> 3) & 0x1FFF == ATT_VERSION)) # Check ATT parser version
traces_python = {}
for T in range(info.num_traces):
if info.tracesizes[T] > 2:
id = info.traceIDs[T]
traces_python[id] = Trace(id, int(info.tracesizes[T]), info.tracedata[T])
waves_python = []
for k in range(info.num_waves):
if info.wavedata[k].instructions_size > 2:
waves_python.append(PythonWave(info.wavedata[k]))
SO.FreeBinary(info.binaryID)
return (traces_python, waves_python, events, occupancy, flags, kernel_addr, info.flags & 0x4)
def getWaves_stitch(traces, code, jumps, flags, latency_map, hitcount_map, bIsAuto, codeobjservice):
for id in traces.keys():
traces[id].instructions = stitch.stitch(traces[id].instructions, code, jumps, flags, bIsAuto, codeobjservice)
if len(code) > hitcount_map.size:
hitcount_map = np.pad(hitcount_map, [0,len(code)-hitcount_map.size])
latency_map = np.pad(latency_map, [0,len(code)-latency_map.size])
if traces[id].instructions is not None:
for inst in traces[id].instructions[0]:
hitcount_map[inst.asmline] += inst.num_waves
latency_map[inst.asmline] += inst.cycles
return hitcount_map, latency_map
def persist(trace_file, SIMD, traces):
trace = Path(trace_file).name
simds, waves = [], []
begin_time, end_time, timeline, instructions, trace_ids = [], [], [], [], []
mem_ins, issued_ins, valu_ins, valu_stalls = [], [], [], []
vmem_ins, vmem_stalls, flat_ins, flat_stalls = [], [], [], []
lds_ins, lds_stalls, salu_ins, salu_stalls = [], [], [], []
smem_ins, smem_stalls, br_ins, br_taken_ins, br_stalls = [], [], [], [], []
for wave in SIMD:
try:
if wave.instructions is None or traces[wave.traceid].instructions is None:
continue
except:
continue
simds.append(wave.simd)
waves.append(wave.wave_id)
begin_time.append(wave.begin_time)
end_time.append(wave.end_time)
trace_ids.append(wave.traceid)
mem_ins.append(wave.num_mem_instrs)
issued_ins.append(wave.num_issued_instrs)
valu_ins.append(wave.num_valu_instrs)
valu_stalls.append(wave.num_valu_stalls)
vmem_ins.append(wave.num_vmem_instrs)
vmem_stalls.append(wave.num_vmem_stalls)
flat_ins.append(wave.num_flat_instrs)
flat_stalls.append(wave.num_flat_stalls)
lds_ins.append(wave.num_lds_instrs)
lds_stalls.append(wave.num_lds_stalls)
salu_ins.append(wave.num_salu_instrs)
salu_stalls.append(wave.num_salu_stalls)
smem_ins.append(wave.num_smem_instrs)
smem_stalls.append(wave.num_smem_stalls)
br_ins.append(wave.num_branch_instrs)
br_taken_ins.append(wave.num_branch_taken_instrs)
br_stalls.append(wave.num_branch_stalls)
timeline.append(wave.timeline)
cc = 0
insts = []
skips = traces[wave.traceid].instructions[-1]
try:
for v in traces[wave.traceid].instructions[0]:
while cc in skips:
cc += 1
t = wave.instructions[cc]
insts.append((t[0], v.type, 0, t[1], v.asmline))
cc += 1
except:
pass # Incomplete waves
instructions.append((insts,) + traces[wave.traceid].instructions[1:-1])
df = {
"name": [trace for _ in range(len(begin_time))],
"id": [i for i in range(len(begin_time))],
"simd": simds,
"wave_slot": waves,
"begin_time": begin_time,
"end_time": end_time,
"mem_ins": mem_ins,
"issued_ins": issued_ins,
"valu_ins": valu_ins,
"valu_stalls": valu_stalls,
"vmem_ins": vmem_ins,
"vmem_stalls": vmem_stalls,
"flat_ins": flat_ins,
"flat_stalls": flat_stalls,
"lds_ins": lds_ins,
"lds_stalls": lds_stalls,
"salu_ins": salu_ins,
"salu_stalls": salu_stalls,
"smem_ins": smem_ins,
"smem_stalls": smem_stalls,
"br_ins": br_ins,
"br_taken_ins": br_taken_ins,
"br_stalls": br_stalls,
"timeline": timeline,
"instructions": instructions,
"traceids": trace_ids,
}
return df
def mem_max(array):
mem_dict = {}
for SE in array:
for wave in SE:
for inst in wave:
try:
mem_dict[inst[0]][0] = max(mem_dict[inst[0]][0], inst[1])
except:
mem_dict[inst[0]] = inst[1:]
assert mem_dict[inst[0]][1] == inst[2]
return mem_dict
def lgk(count):
return "lgkmcnt({0})".format(count)
def vmc(count):
return "vmcnt({0})".format(count)
def both_cnt(count):
return lgk(count) + " " + vmc(count)
def insert_waitcnt(flight_count, assembly_code):
flight_count = mem_max(flight_count)
for key in sorted(flight_count):
line_n = key
(
issue_amount,
waitcnt_amount,
) = flight_count[key]
if "vmcnt" in assembly_code[line_n] and "lgkmcnt" in assembly_code[line_n]:
counter_type = both_cnt
elif "vmcnt" in assembly_code[line_n]:
counter_type = vmc
elif "lgkmcnt" in assembly_code[line_n]:
counter_type = lgk
else:
print("Error: Line mismatch")
exit(-1)
for count in range(waitcnt_amount + 1, issue_amount):
print("Inserted line: " + str(line_n))
as_index = line_n - count / (issue_amount + 1)
assembly_code[as_index] = "\ts_waitcnt {0}\t\t; Timing analysis.".format(
counter_type(count)
)
as_index += 0.5 / (issue_amount + 1)
assembly_code[as_index] = "\ts_nop 0\t\t\t\t\t\t; Counters: " + str(
issue_amount
)
return assembly_code
def gen_timelines(DBFILES):
TIMELINES = [np.zeros(int(1E6), dtype=np.float32) for k in range(5)]
TIME_RESOLUTION = 16
for df in DBFILES:
for T in range(len(df["timeline"])):
timeline = df["timeline"][T]
time_acc = 0
tuples3 = [(0, df["begin_time"][T])] + [(int(t[0]), int(t[1])) for t in timeline]
for state in tuples3:
t_end = (time_acc + state[1])//TIME_RESOLUTION
if t_end > 1E8:
print("Warning: Time limit reached for ", state[0], state[1])
break
elif t_end > TIMELINES[state[0]].size:
TIMELINES[state[0]] = np.hstack(
[TIMELINES[state[0]], np.zeros_like(TIMELINES[state[0]])]
)
TIMELINES[state[0]][time_acc//TIME_RESOLUTION : t_end] += 1
time_acc += state[1]
return TIMELINES
if __name__ == "__main__":
pathenv = os.getenv("OUTPUT_PATH")
if pathenv is None:
pathenv = "."
parser = argparse.ArgumentParser()
parser.add_argument(
"assembly_code", help="Path to the assembly code. Must be the first parameter."
)
parser.add_argument(
"--trace_file", help="Filter for trace files", default=None, type=str
)
parser.add_argument(
"--att_kernel", help="Kernel file", type=str, default=os.path.join(pathenv, "*_kernel.txt")
)
parser.add_argument("--ports", help="Server and websocket ports, default: 8000,18000")
parser.add_argument(
"--mode",
help="""ATT analysis modes:\n
off: Only run ATT collection, disable analysis.\n
file: dump json files to disk.""",
type=str,
default="off",
)
args = parser.parse_args()
args.mode = args.mode.lower().split(',')
CSV_MODE = False
FILE_MODE = False
if 'csv' in args.mode:
CSV_MODE = True
if 'file' in args.mode:
FILE_MODE = True
if not CSV_MODE and not FILE_MODE:
print("Skipping analysis.")
quit()
if os.getenv("COUNTERS_PATH"):
with open(os.getenv("COUNTERS_PATH"), "r") as f:
lines = [l.split("//")[0] for l in f.readlines()]
EVENT_NAMES = []
clean = lambda x: x.split("=")[1].split(" ")[0].split("\n")[0]
for line in lines:
if "PERFCOUNTER_ID=" in line:
EVENT_NAMES += ["id: " + clean(line)]
for line in lines:
if "PERFCOUNTER=" in line:
EVENT_NAMES += [clean(line).split("SQ_")[1].lower()]
att_kernel_list = glob.glob(args.att_kernel)
if len(att_kernel_list) == 0:
print("Could not find att output kernel:", args.att_kernel)
quit()
for att_kernel in att_kernel_list:
print('Parsing:', att_kernel)
att_kernel_f = []
with open(att_kernel, 'r') as f:
for line in f:
att_kernel_f.append(line.split('\n')[0])
# returns element index of the file:// or memory:// filepath in the string
get_path_loc = lambda x: np.sum([len(m) for m in x.split(' ')[:3]])+3
# adds the OUTPUT_PATH env variable to a filepath if necessary
add_pathnv = lambda x: x[:get_path_loc(x)] + os.path.join(pathenv, x[get_path_loc(x):])
# returns the memory address in the string
get_addr = lambda x: int(x.split(' ')[0][2:], 16)
# Sets a preference for 'file' paths to be added before 'memory' paths. Sorts by addr.
get_addr_preference = lambda x: [0 if 'file' in x[get_path_loc(x):] else 1<<60][0]
# Get the GPU id in the string
gpu_id = int(att_kernel_f[0].split(' ')[2].split('GPU[')[1].split(']')[0])
# Eliminame first line in the att_kernel txt file and adds the OUTPUT_PATH as needed
att_kernel_f = [add_pathnv(p) if '.out' == p[-4:] else p for p in att_kernel_f[1:]]
# Sorts the list of codeobj by address, with 'file' given preference
att_kernel_f = sorted(att_kernel_f, key=lambda x: get_addr(x)+get_addr_preference(x))
assembly_code = deepcopy(args.assembly_code)
# Assembly parsing
bIsAuto = False
if assembly_code.lower().strip() == 'auto':
bIsAuto = True
code = [['; Begin ATT ASM', 100, 0, '', 0, 0, 0, 0, 0]]
jumps = []
elif not Path(assembly_code).is_file():
print("Invalid assembly_code('{0}')!".format(assembly_code))
sys.exit(1)
else:
code, jumps = parse_binary(assembly_code, att_kernel)
# Trace Parsing
trace_instance_name = att_kernel.split("_kernel.txt")[0]
if args.trace_file is None:
filenames = glob.glob(trace_instance_name + "_*.att")
else:
filenames = glob.glob(args.trace_file)
if len(filenames) == 0:
print("Could not find trace files for", att_kernel)
continue
print('Att kernel:', att_kernel)
DBFILES = []
EVENTS = []
OCCUPANCY = []
GFXV = []
analysed_filenames = []
occupancy_filenames = []
kernel_addr = {}
latency_map = np.zeros((len(code)), dtype=np.int64)
hitcount_map = np.zeros((len(code)), dtype=np.int32)
gc.collect()
if bIsAuto:
codeservice = service.CodeobjService(gpu_id, att_kernel_f, SO.classify_asm_line)
else:
codeservice = None
for name in filenames:
traces, waves, perfevents, occupancy, gfxv, addrs, ftrace = getWaves_binary(name)
if gfxv is None:
continue
if CSV_MODE == False and ftrace == 0:
print('Generating occupancy information')
for id, addr in enumerate(addrs):
kernel_addr[id] = addr
if len(occupancy) > 1:
OCCUPANCY.append( occupancy )
occupancy_filenames.append(name)
if np.sum([0]+[len(s.instructions) for id, s in traces.items()]) == 0:
print("No traces from", name)
continue
hitcount_map, latency_map = getWaves_stitch(traces, code, jumps, gfxv, latency_map, hitcount_map, bIsAuto, codeservice)
analysed_filenames.append(name)
EVENTS.append(perfevents)
DBFILES.append( persist(name, waves, traces) )
GFXV.append(gfxv)
gc.collect()
for k in range(len(code)):
code[k][-2] = int(hitcount_map[k])
code[k][-1] = int(latency_map[k])
try:
code[k][-3] = codeservice.ToRawPC(code[k][-3])
except:
pass
if k > 0 and code[k-1][-3] == 0:
code[k-1][-3] = code[k][-3]
if CSV_MODE:
from att_to_csv import dump_csv
dump_csv(code, trace_instance_name, bIsAuto)
if FILE_MODE:
try:
dispatchNames = {id: codeservice.getSymbolName(addr) for id, addr in kernel_addr.items()}
except:
dispatchNames = {id: "#addr"+str(addr) for id, addr in kernel_addr.items()}
drawinfo = {
"TIMELINES": gen_timelines(DBFILES),
"EVENTS": EVENTS,
"EVENT_NAMES": EVENT_NAMES,
"OCCUPANCY": OCCUPANCY,
"ShaderNames": occupancy_filenames,
"DispatchNames": dispatchNames
}
view_trace(
code,
DBFILES,
analysed_filenames,
0,
gfxv,
drawinfo,
trace_instance_name
)
+24
View File
@@ -0,0 +1,24 @@
#!/usr/bin/env python3
import numpy as np
import csv
import os
def dump_csv(code, trace_instance_name, bIsAuto):
outpath = os.getenv("OUT_FILE_NAME")
if outpath is None:
outpath = "att_output"
elif os.path.dirname(outpath) != '':
os.makedirs(os.path.dirname(outpath), exist_ok=True)
outpath += '_' + os.path.basename(trace_instance_name) + '.csv'
print('Generating', outpath)
with open(outpath, 'w') as f:
writer = csv.writer(f)
if bIsAuto:
writer.writerow(['Addr', 'Instruction', 'Hitcount', 'Cycles', 'C++ Reference'])
[writer.writerow([hex(m[6]), m[0], m[7], m[8], m[3]]) for m in code]
else:
writer.writerow(['Line', 'Instruction', 'Hitcount', 'Cycles', 'C++ Reference'])
[writer.writerow([m[5], m[0], m[7], m[8], m[3]]) for m in code]
@@ -0,0 +1,383 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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 "code_printing.hpp"
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <vector>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <hsa/amd_hsa_elf.h>
#include "../utils.h"
#include <cxxabi.h>
#include <elfutils/libdw.h>
#include <sys/mman.h>
#include <atomic>
#define C_API_BEGIN try {
#define C_API_END(returndata) \
} catch (std::exception& e) \
{ \
std::string s = e.what(); \
if (s.find("memory protocol not supported!") == std::string::npos) \
std::cerr << "Codeobj API lookup: " << e.what() << std::endl; \
return returndata; \
} \
catch (std::string& s) \
{ \
if (s.find("memory protocol not supported!") == std::string::npos) \
std::cerr << "Codeobj API lookup: " << s << std::endl; \
return returndata; \
} \
catch (...) \
{ \
return returndata; \
}
CodeObjDecoderComponent::CodeObjDecoderComponent(
const char* codeobj_data,
uint64_t codeobj_size,
uint64_t gpu_id
) {
if (
codeobj_size <= 4 ||
codeobj_data[0] != ELFMAG0 ||
codeobj_data[1] != ELFMAG1 ||
codeobj_data[2] != ELFMAG2 ||
codeobj_data[3] != ELFMAG3
)
throw std::invalid_argument("Invalid ELF file");
m_fd = -1;
#if defined(_GNU_SOURCE) && defined(MFD_ALLOW_SEALING) && defined(MFD_CLOEXEC)
m_fd = ::memfd_create(m_uri.c_str(), MFD_ALLOW_SEALING | MFD_CLOEXEC);
#endif
if (m_fd == -1) // If fail, attempt under /tmp
m_fd = ::open("/tmp", O_TMPFILE | O_RDWR, 0666);
if (m_fd == -1) {
printf("could not create a temporary file for code object\n");
return;
}
if (size_t size = ::write(m_fd, codeobj_data, codeobj_size); size != codeobj_size) {
printf("could not write to the temporary file\n");
return;
}
::lseek(m_fd, 0, SEEK_SET);
fsync(m_fd);
m_line_number_map = {};
std::unique_ptr<Dwarf, void (*)(Dwarf*)> dbg(dwarf_begin(m_fd, DWARF_C_READ),
[](Dwarf* dbg) { dwarf_end(dbg); });
/*if (!dbg) {
rocprofiler::warning("Error opening Dwarf!\n");
return;
} */
if (dbg) {
Dwarf_Off cu_offset{0}, next_offset;
size_t header_size;
std::map<uint64_t, std::string> used_addrs;
while (!dwarf_nextcu(dbg.get(), cu_offset, &next_offset, &header_size, nullptr, nullptr,
nullptr)) {
Dwarf_Die die;
if (!dwarf_offdie(dbg.get(), cu_offset + header_size, &die)) continue;
Dwarf_Lines* lines;
size_t line_count;
if (dwarf_getsrclines(&die, &lines, &line_count)) continue;
for (size_t i = 0; i < line_count; ++i) {
Dwarf_Addr addr;
int line_number;
Dwarf_Line* line = dwarf_onesrcline(lines, i);
if (line && !dwarf_lineaddr(line, &addr) && !dwarf_lineno(line, &line_number) && line_number)
{
std::string src = dwarf_linesrc(line, nullptr, nullptr);
auto dwarf_line = src + ':' + std::to_string(line_number);
if (used_addrs.find(addr) != used_addrs.end())
{
used_addrs.at(addr) += ' ' + dwarf_line;
continue;
}
used_addrs.emplace(addr, std::move(dwarf_line));
}
}
cu_offset = next_offset;
}
auto it = used_addrs.begin();
if(it != used_addrs.end())
{
while(std::next(it) != used_addrs.end())
{
uint64_t delta = std::next(it)->first - it->first;
auto segment = address_range_t{it->first, delta, 0};
m_line_number_map.emplace(segment, std::move(it->second));
it++;
}
auto segment = address_range_t{it->first, codeobj_size - it->first, 0};
m_line_number_map.emplace(segment, std::move(it->second));
}
}
// Can throw
disassembly = std::make_unique<DisassemblyInstance>(codeobj_data, codeobj_size, gpu_id);
try {
m_symbol_map = disassembly->GetKernelMap(); // Can throw
} catch(...) {}
//disassemble_kernels();
}
CodeObjDecoderComponent::~CodeObjDecoderComponent() {
if (m_fd) ::close(m_fd);
}
std::optional<SymbolInfo> CodeObjDecoderComponent::find_symbol(uint64_t vaddr) {
/* Load the symbol table. */
auto it = m_symbol_map.upper_bound(vaddr);
if (it == m_symbol_map.begin())
return std::nullopt;
auto&& [symbol_vaddr, symbol] = *std::prev(it);
if (vaddr >= symbol_vaddr + symbol.mem_size)
return std::nullopt;
std::string symbol_name = symbol.name;
int status = 0;
auto* demangled_name = abi::__cxa_demangle(symbol_name.c_str(), nullptr, nullptr, &status);
if (status == 0 && demangled_name)
{
symbol_name = demangled_name;
free(demangled_name);
}
return SymbolInfo{symbol_name, symbol.faddr, symbol.mem_size};
}
std::pair<instruction_instance_t, size_t>
CodeObjDecoderComponent::disassemble_instruction(uint64_t faddr, uint64_t vaddr)
{
if (!disassembly)
throw std::exception();
const char* cpp_line = nullptr;
auto it = m_line_number_map.find({vaddr, 0, 0});
if(it != m_line_number_map.end()) cpp_line = it->second.data();
size_t size = disassembly->ReadInstruction(faddr, vaddr, cpp_line);
return {disassembly->last_instruction, size};
}
void CodeObjDecoderComponent::disassemble_kernel(uint64_t faddr, uint64_t vaddr)
{
if (!disassembly) return;
auto symbol = find_symbol(vaddr);
if (!symbol)
{
std::cerr << "No symbol found at address 0x" << std::hex << faddr << std::endl;
return;
}
std::cout << "Dumping ISA for " << symbol->name << std::endl;
uint64_t end_addr = faddr + symbol->mem_size;
while (faddr < end_addr)
{
size_t size;
instruction_instance_t inst;
std::tie(inst, size) = this->disassemble_instruction(faddr, vaddr);
instructions.push_back(inst);
faddr += size;
vaddr += size;
}
}
void CodeObjDecoderComponent::disassemble_kernels() {
for (auto& [vaddr, v] : m_symbol_map) disassemble_kernel(v.faddr, vaddr);
}
void CodeObjDecoderComponent::disassemble_single_kernel(uint64_t kaddr) {
for (auto& [vaddr, v] : m_symbol_map)
if (kaddr >= vaddr && kaddr < vaddr + v.mem_size)
disassemble_kernel(v.faddr, vaddr);
}
CodeobjDecoder::CodeobjDecoder(
const char* filepath,
uint64_t loadbase,
uint64_t mem_size,
uint64_t gpu_id
): loadbase(loadbase), load_end(loadbase + mem_size)
{
if (!filepath) throw "Empty filepath.";
std::string_view fpath(filepath);
if (fpath.rfind(".out") + 4 == fpath.size())
{
std::ifstream file(filepath, std::ios::in | std::ios::binary);
if (!file.is_open())
throw "Invalid filename " + std::string(filepath);
std::vector<char> buffer;
file.seekg(0, file.end);
buffer.resize(file.tellg());
file.seekg(0, file.beg);
file.read(buffer.data(), buffer.size());
decoder = std::make_unique<CodeObjDecoderComponent>(buffer.data(), buffer.size(), gpu_id);
}
else
{
std::unique_ptr<CodeObjectBinary> binary = std::make_unique<CodeObjectBinary>(filepath);
auto& buffer = binary->buffer;
decoder = std::make_unique<CodeObjDecoderComponent>(buffer.data(), buffer.size(), gpu_id);
}
auto elf_segments = decoder->disassembly->getSegments();
}
bool CodeobjDecoder::add_to_map(uint64_t faddr, uint64_t vaddr, uint64_t voffset)
{
try
{
decoded_map[vaddr] = decoder->disassemble_instruction(faddr, voffset);
}
catch(std::exception& e)
{
return false;
}
return true;
}
bool CodeobjDecoder::decode_single_at_offset(uint64_t vaddr, uint64_t voffset)
{
auto faddr = decoder->disassembly->va2fo(voffset);
if (!faddr)
return false;
return add_to_map(*faddr, vaddr, voffset);
}
bool CodeobjDecoder::decode_single(uint64_t vaddr)
{
if (!decoder || vaddr < loadbase) return false;
return decode_single_at_offset(vaddr, vaddr-loadbase);
}
std::pair<instruction_instance_t, size_t>& CodeobjDecoder::getDecoded(uint64_t addr)
{
if (decoded_map.find(addr) != decoded_map.end())
return decoded_map[addr];
if (!decode_single(addr))
{
std::cerr << "Invalid addr: " << std::hex << addr << std::dec << std::endl;
throw std::exception();
}
return decoded_map[addr];
}
#define PUBLIC_API __attribute__((visibility("default")))
CodeobjTableTranslation table;
extern "C"
{
PUBLIC_API int addDecoder(
const char* filename,
uint32_t id,
uint64_t loadbase,
uint64_t memsize,
uint64_t gpu_id
) {
C_API_BEGIN
table.addDecoder(filename, id, loadbase, memsize, gpu_id);
return 0;
C_API_END(1)
}
PUBLIC_API int removeDecoder(uint32_t id, uint64_t loadbase)
{
return table.removeDecoder(id, loadbase) != false;
}
PUBLIC_API instruction_info_t getInstructionFromAddr(uint64_t vaddr)
{
static instruction_info_t default_info{nullptr, nullptr, 0};
C_API_BEGIN
return table.get(vaddr);
C_API_END(default_info)
}
PUBLIC_API instruction_info_t getInstructionFromID(uint32_t id, uint64_t offset)
{
static instruction_info_t default_info{nullptr, nullptr, 0};
C_API_BEGIN
return table.get(id, offset);
C_API_END(default_info)
}
PUBLIC_API const char* getSymbolName(uint64_t addr)
{
C_API_BEGIN
return table.getSymbolName(addr);
C_API_END(nullptr)
}
}
@@ -0,0 +1,206 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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. */
#pragma once
#include "rocprofiler.h"
#include <map>
#include <optional>
#include <string>
#include <vector>
#include <memory>
#include <unordered_map>
#include "disassembly.hpp"
#include "segment.hpp"
struct DSourceLine
{
uint64_t vaddr;
uint64_t size;
std::string str;
uint64_t begin() const { return vaddr; }
bool inrange(uint64_t addr) const { return addr >= vaddr && addr < vaddr+size; }
};
class CodeObjDecoderComponent
{
public:
std::optional<SymbolInfo> find_symbol(uint64_t address);
CodeObjDecoderComponent(const char* codeobj_data, uint64_t codeobj_size, uint64_t gpu_id);
~CodeObjDecoderComponent();
std::pair<instruction_instance_t, size_t>
disassemble_instruction(uint64_t faddr, uint64_t vaddr);
void disassemble_kernel(uint64_t faddr, uint64_t vaddr);
void disassemble_single_kernel(uint64_t kaddr);
void disassemble_kernels();
int m_fd;
std::map<address_range_t, std::string> m_line_number_map{};
std::map<uint64_t, SymbolInfo> m_symbol_map{};
std::string m_uri;
std::vector<instruction_instance_t> instructions{};
std::unique_ptr<DisassemblyInstance> disassembly{};
};
typedef struct {
const char* inst;
const char* cpp;
size_t size;
} instruction_info_t;
class CodeobjDecoder
{
public:
CodeobjDecoder(const char* filepath, uint64_t loadbase, uint64_t memsize, uint64_t gpu_id);
bool decode_single(uint64_t vaddr);
bool decode_single_at_offset(uint64_t vaddr, uint64_t voffset);
bool add_to_map(uint64_t faddr, uint64_t vaddr, uint64_t voffset);
std::pair<instruction_instance_t, size_t>& getDecoded(uint64_t addr);
const char* getInstruction(uint64_t addr) { return getDecoded(addr).first.instruction; }
const char* getCppref(uint64_t addr) { return getDecoded(addr).first.cpp_reference; }
size_t getSize(uint64_t addr) { return getDecoded(addr).second; }
instruction_info_t get(uint64_t addr) {
auto& inst = getDecoded(addr);
return {inst.first.instruction, inst.first.cpp_reference, inst.second};
}
uint64_t begin() const { return loadbase; };
uint64_t end() const { return load_end; }
uint64_t size() const { return load_end-loadbase; }
bool inrange(uint64_t addr) const { return addr >= begin() && addr < end(); }
const char* getSymbolName(uint64_t addr) const {
if (!decoder) return nullptr;
auto it = decoder->m_symbol_map.find(addr-loadbase);
if (it != decoder->m_symbol_map.end())
return it->second.name.data();
return nullptr;
}
std::vector<std::pair<uint64_t, uint64_t>> elf_segments{};
private:
const uint64_t loadbase;
uint64_t load_end = 0;
std::unordered_map<uint64_t, std::pair<instruction_instance_t, size_t>> decoded_map;
std::unique_ptr<CodeObjDecoderComponent> decoder{nullptr};
};
/**
* @brief Maps ID and offsets into instructions
*/
class CodeobjList
{
public:
CodeobjList() = default;
virtual void addDecoder(
const char* filepath,
uint32_t id,
uint64_t loadbase,
uint64_t memsize,
uint64_t gpu_id
)
{
decoders[id] = std::make_shared<CodeobjDecoder>(filepath, loadbase, memsize, gpu_id);
}
virtual bool removeDecoder(uint32_t id)
{
return decoders.erase(id) != 0;
}
instruction_info_t get(uint32_t id, uint64_t offset)
{
auto& decoder = decoders.at(id);
auto& inst = decoder->getDecoded(decoder->begin() + offset);
return {inst.first.instruction, inst.first.cpp_reference, inst.second};
}
const char* getSymbolName(uint32_t id, uint64_t offset)
{
auto& decoder = decoders.at(id);
uint64_t vaddr = decoder->begin() + offset;
if (decoder->inrange(vaddr))
return decoder->getSymbolName(vaddr);
return nullptr;
}
protected:
std::unordered_map<uint32_t, std::shared_ptr<CodeobjDecoder>> decoders{};
};
/**
* @brief Translates virtual addresses to elf file offsets
*/
class CodeobjTableTranslation : protected CodeobjList
{
using Super = CodeobjList;
public:
CodeobjTableTranslation() = default;
virtual void addDecoder(
const char* filepath,
uint32_t id,
uint64_t loadbase,
uint64_t memsize,
uint64_t gpu_id
) override
{
this->Super::addDecoder(filepath, id, loadbase, memsize, gpu_id);
auto ptr = decoders.at(id);
table.insert(address_range_t{ptr->begin(), static_cast<uint32_t>(ptr->size()), id});
}
virtual bool removeDecoder(uint32_t id, uint64_t loadbase)
{
return table.remove(loadbase) && this->Super::removeDecoder(id);
}
instruction_info_t get(uint64_t vaddr)
{
auto addr_range = table.find_codeobj_in_range(vaddr);
return get(addr_range.id, vaddr - addr_range.addr);
}
instruction_info_t get(uint32_t id, uint64_t offset) { return this->Super::get(id, offset); }
const char* getSymbolName(uint64_t vaddr)
{
for (auto& [_, decoder] : decoders)
{
if (!decoder->inrange(vaddr)) continue;
return decoder->getSymbolName(vaddr);
}
return nullptr;
}
private:
CodeobjTableTranslator table;
};
@@ -0,0 +1,386 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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. */
#if !defined(_GNU_SOURCE) || !defined(_XOPEN_SOURCE)
#define _XOPEN_SOURCE 700
#endif
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <elf.h>
#include <cxxabi.h>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <vector>
#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <elfutils/libdw.h>
#include "../utils.h"
#include "code_printing.hpp"
#include <hsa/amd_hsa_elf.h>
#define THROW_COMGR(call) \
if (amd_comgr_status_s status = call) { \
const char* reason = ""; \
amd_comgr_status_string(status, &reason); \
std::cerr << __FILE__ << ':' << __LINE__ << " code: " \
<< status << " failed: " << reason << std::endl; \
throw std::exception(); \
}
#define RETURN_COMGR(call) \
if (amd_comgr_status_s status = call) { \
const char* reason = ""; \
amd_comgr_status_string(status, &reason); \
std::cerr << __FILE__ << ':' << __LINE__ << " code: " \
<< status << " failed: " << reason << std::endl; \
return AMD_COMGR_STATUS_ERROR; \
}
std::unordered_map<uint64_t, std::string> DisassemblyInstance::agent_isa_name{};
CodeObjectBinary::CodeObjectBinary(const std::string& uri) : m_uri(uri) {
const std::string protocol_delim{"://"};
size_t protocol_end = m_uri.find(protocol_delim);
std::string protocol = m_uri.substr(0, protocol_end);
protocol_end += protocol_delim.length();
std::transform(protocol.begin(), protocol.end(), protocol.begin(),
[](unsigned char c) { return std::tolower(c); });
std::string path;
size_t path_end = m_uri.find_first_of("#?", protocol_end);
if (path_end != std::string::npos) {
path = m_uri.substr(protocol_end, path_end++ - protocol_end);
} else {
path = m_uri.substr(protocol_end);
}
/* %-decode the string. */
std::string decoded_path;
decoded_path.reserve(path.length());
for (size_t i = 0; i < path.length(); ++i)
{
if (path[i] == '%' && std::isxdigit(path[i + 1]) && std::isxdigit(path[i + 2])) {
decoded_path += std::stoi(path.substr(i + 1, 2), 0, 16);
i += 2;
} else {
decoded_path += path[i];
}
}
/* Tokenize the query/fragment. */
std::vector<std::string> tokens;
size_t pos, last = path_end;
while ((pos = m_uri.find('&', last)) != std::string::npos) {
tokens.emplace_back(m_uri.substr(last, pos - last));
last = pos + 1;
}
if (last != std::string::npos) {
tokens.emplace_back(m_uri.substr(last));
}
/* Create a tag-value map from the tokenized query/fragment. */
std::unordered_map<std::string, std::string> params;
std::for_each(tokens.begin(), tokens.end(), [&](std::string& token) {
size_t delim = token.find('=');
if (delim != std::string::npos) {
params.emplace(token.substr(0, delim), token.substr(delim + 1));
}
});
buffer = std::vector<char>{};
size_t offset{0}, size{0};
if (auto offset_it = params.find("offset"); offset_it != params.end()) {
offset = std::stoul(offset_it->second, nullptr, 0);
}
if (auto size_it = params.find("size"); size_it != params.end()) {
if (!(size = std::stoul(size_it->second, nullptr, 0))) return;
}
if (protocol != "file") throw protocol + " protocol not supported!";
std::ifstream file(decoded_path, std::ios::in | std::ios::binary);
if (!file || !file.is_open()) throw "could not open " + decoded_path;
if (!size) {
file.ignore(std::numeric_limits<std::streamsize>::max());
size_t bytes = file.gcount();
file.clear();
if (bytes < offset)
throw "invalid uri " + decoded_path + " (file size < offset)";
size = bytes - offset;
}
file.seekg(offset, std::ios_base::beg);
buffer.resize(size);
file.read(&buffer[0], size);
}
DisassemblyInstance::DisassemblyInstance(
const char* codeobj_data,
uint64_t codeobj_size,
uint64_t gpu_id
)
{
if (
codeobj_size <= 4 ||
codeobj_data[0] != ELFMAG0 ||
codeobj_data[1] != ELFMAG1 ||
codeobj_data[2] != ELFMAG2 ||
codeobj_data[3] != ELFMAG3
)
throw std::invalid_argument("Invalid ELF file");
buffer = std::vector<char>(codeobj_size, 0);
std::memcpy(buffer.data(), codeobj_data, codeobj_size);
THROW_COMGR(amd_comgr_create_data(AMD_COMGR_DATA_KIND_EXECUTABLE, &data));
THROW_COMGR(amd_comgr_set_data(data, buffer.size(), buffer.data()));
std::string input_isa{};
if (agent_isa_name.find(gpu_id) == agent_isa_name.end())
{
size_t isa_size = 128;
input_isa.resize(isa_size);
THROW_COMGR(amd_comgr_get_data_isa_name(data, &isa_size, input_isa.data()));
agent_isa_name[gpu_id] = input_isa;
}
else
{
input_isa = agent_isa_name.at(gpu_id);
}
THROW_COMGR(amd_comgr_create_disassembly_info(
input_isa.data(),
&DisassemblyInstance::memory_callback, &DisassemblyInstance::inst_callback,
[](uint64_t address, void* user_data) {}, &info));
}
static bool IsKernelType(amd_comgr_symbol_type_t type)
{
if (type == AMD_COMGR_SYMBOL_TYPE_FUNC)
return true;
#ifdef AMD_COMGR_SYMBOL_TYPE_AMDGPU_HSA_KERNEL // To be deprecated
if (type == AMD_COMGR_SYMBOL_TYPE_AMDGPU_HSA_KERNEL)
return true;
#endif
return false;
}
amd_comgr_status_t DisassemblyInstance::symbol_callback(amd_comgr_symbol_t symbol,
void* user_data) {
amd_comgr_symbol_type_t type;
RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_TYPE, &type));
if (!IsKernelType(type))
return AMD_COMGR_STATUS_SUCCESS;
uint64_t vaddr;
uint64_t mem_size;
uint64_t name_size;
RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_VALUE, &vaddr));
RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_SIZE, &mem_size));
RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_NAME_LENGTH, &name_size));
std::string name;
name.resize(name_size);
RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_NAME, name.data()));
DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
std::optional<uint64_t> faddr = instance.va2fo(vaddr);
if (faddr)
instance.symbol_map[vaddr] = {name, *faddr, mem_size};
return AMD_COMGR_STATUS_SUCCESS;
}
std::map<uint64_t, SymbolInfo>& DisassemblyInstance::GetKernelMap() {
symbol_map = {};
THROW_COMGR(amd_comgr_iterate_symbols(data, &DisassemblyInstance::symbol_callback, this));
return symbol_map;
}
DisassemblyInstance::~DisassemblyInstance() {
amd_comgr_release_data(data);
amd_comgr_destroy_disassembly_info(info);
}
uint64_t DisassemblyInstance::ReadInstruction(uint64_t faddr, uint64_t vaddr, const char* cpp_line)
{
uint64_t size_read;
uint64_t addr_in_buffer = reinterpret_cast<uint64_t>(buffer.data()) + faddr;
THROW_COMGR(amd_comgr_disassemble_instruction(info, addr_in_buffer, (void*)this, &size_read));
last_instruction.address = vaddr;
last_instruction.cpp_reference = cpp_line;
return size_read;
}
uint64_t DisassemblyInstance::memory_callback(uint64_t from, char* to, uint64_t size,
void* user_data) {
DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
int64_t copysize = reinterpret_cast<int64_t>(instance.buffer.data())
+ instance.buffer.size() - static_cast<int64_t>(from);
copysize = std::min<int64_t>(size, copysize);
std::memcpy(to, (char*)from, copysize);
return copysize;
}
void DisassemblyInstance::inst_callback(const char* instruction, void* user_data) {
DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
instance.last_instruction.instruction = strdup(instruction);
}
#define CHECK_VA2FO(x, msg) if (!(x)) { \
std::cerr << __FILE__ << ' ' << __LINE__ << ' ' << msg << std::endl; \
return std::nullopt; \
}
// mem - input argument, start of the elf
// va - input argument, virtual address
// return file offset, if found
std::optional<uint64_t> DisassemblyInstance::va2fo(uint64_t va)
{
CHECK_VA2FO(buffer.size(), "buffer is not large enough");
uint8_t *e_ident = (uint8_t*)buffer.data();
CHECK_VA2FO(e_ident, "e_ident is nullptr");
CHECK_VA2FO(
e_ident[EI_MAG0] == ELFMAG0 ||
e_ident[EI_MAG1] == ELFMAG1 ||
e_ident[EI_MAG2] == ELFMAG2 ||
e_ident[EI_MAG3] == ELFMAG3, "unexpected ei_mag");
CHECK_VA2FO(e_ident[EI_CLASS] == ELFCLASS64, "unexpected ei_class");
CHECK_VA2FO(e_ident[EI_DATA] == ELFDATA2LSB, "unexpected ei_data");
CHECK_VA2FO(e_ident[EI_VERSION] == EV_CURRENT, "unexpected ei_version");
CHECK_VA2FO(e_ident[EI_OSABI] == 64, "unexpected ei_osabi"); // ELFOSABI_AMDGPU_HSA
CHECK_VA2FO(
e_ident[EI_ABIVERSION] == 2 || // ELFABIVERSION_AMDGPU_HSA_V4
e_ident[EI_ABIVERSION] == 3 || // ELFABIVERSION_AMDGPU_HSA_V5
e_ident[EI_ABIVERSION] == 4, "unexpected ei_abiversion"); // ELFABIVERSION_AMDGPU_HSA_V6
Elf64_Ehdr *ehdr = (Elf64_Ehdr*)buffer.data();
CHECK_VA2FO(ehdr, "ehdr is nullptr");
CHECK_VA2FO(ehdr->e_type == ET_DYN, "unexpected e_type");
CHECK_VA2FO(ehdr->e_machine == ELF::EM_AMDGPU, "unexpected e_machine");
CHECK_VA2FO(buffer.size() > sizeof(Elf64_Ehdr), "buffer is not large enough");
CHECK_VA2FO(ehdr->e_phoff != 0, "unexpected e_phoff");
CHECK_VA2FO(buffer.size() > ehdr->e_phoff + sizeof(Elf64_Phdr), "buffer is not large enough");
Elf64_Phdr *phdr = (Elf64_Phdr*)((uint8_t*)buffer.data() + ehdr->e_phoff);
CHECK_VA2FO(phdr, "phdr is nullptr");
for (uint16_t i = 0; i < ehdr->e_phnum; ++i)
{
if (phdr[i].p_type != PT_LOAD)
continue;
if (va < phdr[i].p_vaddr || va >= (phdr[i].p_vaddr + phdr[i].p_memsz))
continue;
return va + phdr[i].p_offset - phdr[i].p_vaddr;
}
return std::nullopt;
}
#undef CHECK_VA2FO
#define CHECK_VA2FO(x, msg) if (!(x)) { \
std::cerr << __FILE__ << ' ' << __LINE__ << ' ' << msg << std::endl; \
return {}; \
}
std::vector<std::pair<uint64_t, uint64_t>> DisassemblyInstance::getSegments()
{
CHECK_VA2FO(buffer.size(), "buffer is not large enough");
uint8_t *e_ident = (uint8_t*)buffer.data();
CHECK_VA2FO(e_ident, "e_ident is nullptr");
CHECK_VA2FO(
e_ident[EI_MAG0] == ELFMAG0 ||
e_ident[EI_MAG1] == ELFMAG1 ||
e_ident[EI_MAG2] == ELFMAG2 ||
e_ident[EI_MAG3] == ELFMAG3, "unexpected ei_mag");
CHECK_VA2FO(e_ident[EI_CLASS] == ELFCLASS64, "unexpected ei_class");
CHECK_VA2FO(e_ident[EI_DATA] == ELFDATA2LSB, "unexpected ei_data");
CHECK_VA2FO(e_ident[EI_VERSION] == EV_CURRENT, "unexpected ei_version");
CHECK_VA2FO(e_ident[EI_OSABI] == 64, "unexpected ei_osabi"); // ELFOSABI_AMDGPU_HSA
CHECK_VA2FO(
e_ident[EI_ABIVERSION] == 2 || // ELFABIVERSION_AMDGPU_HSA_V4
e_ident[EI_ABIVERSION] == 3 || // ELFABIVERSION_AMDGPU_HSA_V5
e_ident[EI_ABIVERSION] == 4, "unexpected ei_abiversion"); // ELFABIVERSION_AMDGPU_HSA_V6
Elf64_Ehdr *ehdr = (Elf64_Ehdr*)buffer.data();
CHECK_VA2FO(ehdr, "ehdr is nullptr");
CHECK_VA2FO(ehdr->e_type == ET_DYN, "unexpected e_type");
CHECK_VA2FO(ehdr->e_machine == ELF::EM_AMDGPU, "unexpected e_machine");
CHECK_VA2FO(buffer.size() > sizeof(Elf64_Ehdr), "buffer is not large enough");
CHECK_VA2FO(ehdr->e_phoff != 0, "unexpected e_phoff");
CHECK_VA2FO(buffer.size() > ehdr->e_phoff + sizeof(Elf64_Phdr), "buffer is not large enough");
Elf64_Phdr *phdr = (Elf64_Phdr*)((uint8_t*)buffer.data() + ehdr->e_phoff);
CHECK_VA2FO(phdr, "phdr is nullptr");
std::vector<std::pair<uint64_t, uint64_t>> segments;
for (Elf64_Half i = 0; i < ehdr->e_phnum; ++i)
{
if (phdr[i].p_type != PT_LOAD)
continue;
segments.push_back({phdr[i].p_vaddr - phdr[i].p_offset, phdr[i].p_memsz});
}
return segments;
}
@@ -0,0 +1,75 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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. */
#pragma once
#include <amd_comgr/amd_comgr.h>
#include <string>
#include <vector>
#include <memory>
#include <limits>
typedef struct {
const char* instruction;
const char* cpp_reference;
uint64_t address;
} instruction_instance_t;
class CodeObjectBinary {
public:
CodeObjectBinary(const std::string& uri);
std::string m_uri;
std::vector<char> buffer;
};
struct SymbolInfo
{
std::string name;
uint64_t faddr;
uint64_t mem_size;
};
class DisassemblyInstance {
public:
DisassemblyInstance(
const char* codeobj_data,
uint64_t codeobj_size,
uint64_t gpu_id
);
~DisassemblyInstance();
uint64_t ReadInstruction(uint64_t faddr, uint64_t vaddr, const char* cpp_line);
std::map<uint64_t, SymbolInfo>& GetKernelMap();
static uint64_t memory_callback(uint64_t from, char* to, uint64_t size, void* user_data);
static void inst_callback(const char* instruction, void* user_data);
static amd_comgr_status_t symbol_callback(amd_comgr_symbol_t symbol, void* user_data);
// Per-gpu_id isa_name
static std::unordered_map<uint64_t, std::string> agent_isa_name;
std::optional<uint64_t> va2fo(uint64_t va);
std::vector<std::pair<uint64_t, uint64_t>> getSegments();
std::vector<char> buffer;
instruction_instance_t last_instruction;
amd_comgr_disassembly_info_t info;
amd_comgr_data_t data;
std::map<uint64_t, SymbolInfo> symbol_map;
};
+361
View File
@@ -0,0 +1,361 @@
#!/usr/bin/env python3
import sys
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3")
import numpy as np
from io import BytesIO
import matplotlib.pyplot as plt
from copy import deepcopy
import json
COUNTERS_MAX_CAPTURES = 1 << 12
class Readable:
def __init__(self, jsonstring):
self.jsonstr = json.dumps(jsonstring)
self.seek = 0
def read(self, length=0):
if length <= 0:
return self.jsonstr
else:
if self.seek >= len(self):
self.seek = 0
return None
response = self.jsonstr[self.seek : self.seek + length]
self.seek += length
return bytes(response, "utf-8")
def __len__(self):
return len(self.jsonstr)
class FileBytesIO:
def __init__(self, iobytes):
self.iobytes = deepcopy(iobytes)
self.seek = 0
def __len__(self):
return self.iobytes.getbuffer().nbytes
def read(self, length=0):
if length <= 0:
return bytes(self.iobytes.getbuffer())
else:
if self.seek >= self.iobytes.getbuffer().nbytes:
self.seek = 0
return None
response = self.iobytes.getbuffer()[self.seek : self.seek + length]
self.seek += length
return bytes(response)
def get_delta_time(events):
try:
CUS = [[e.time for e in events if e.cu == k and e.bank == 0] for k in range(16)]
CUS = [np.asarray(c).astype(np.int64) for c in CUS if len(c) > 2]
return np.min([np.min(abs(c[1:] - c[:-1])) for c in CUS])
except:
return 1
def draw_wave_metrics(selections, normalize, TIMELINES, EVENTS, EVENT_NAMES):
plt.figure(figsize=(15, 4))
delta_step = 8
quad_delta_time = max(
delta_step, int(0.5 + np.min([get_delta_time(events) for events in EVENTS]))
)
maxtime = (
np.max([np.max([e.time for e in events]) for events in EVENTS]) / quad_delta_time
+ 1
)
if maxtime * delta_step >= COUNTERS_MAX_CAPTURES:
delta_step = 1
while maxtime >= COUNTERS_MAX_CAPTURES:
quad_delta_time *= 2
maxtime /= 2
maxtime = int(min(maxtime * delta_step, COUNTERS_MAX_CAPTURES))
event_timeline = np.zeros((16, maxtime), dtype=np.int32)
print("Delta:", quad_delta_time)
print("Max_cycles:", maxtime * quad_delta_time * 4 // delta_step)
cycles = 4 * quad_delta_time // delta_step * np.arange(maxtime)
kernel = len(EVENTS) * quad_delta_time
for events in EVENTS:
for e in range(len(events) - 1):
bk = events[e].bank * 4
start = events[e].time // (quad_delta_time // delta_step)
end = start + delta_step
event_timeline[bk : bk + 4, start:end] += np.asarray(
events[e].toTuple()[1:5]
)[:, None]
start = events[-1].time
event_timeline[bk : bk + 4, start : start + delta_step] += np.asarray(
events[-1].toTuple()[1:5]
)[:, None]
event_timeline = [
np.convolve(e, [kernel for k in range(3)])[1:-1] for e in event_timeline
]
# event_timeline = [e/kernel for e in event_timeline]
if normalize:
event_timeline = [100 * e / max(e.max(), 1e-5) for e in event_timeline]
colors = [
"blue",
"green",
"gray",
"red",
"orange",
"cyan",
"black",
"darkviolet",
"yellow",
"darkred",
"pink",
"lime",
"gold",
"tan",
"aqua",
"olive",
]
[
plt.plot(cycles, e, "-", label=n, color=c)
for e, n, c, sel in zip(event_timeline, EVENT_NAMES, colors, selections)
if sel
]
plt.legend()
if normalize:
plt.ylabel("As % of maximum")
else:
plt.ylabel("Value")
plt.xlabel("Cycle")
plt.subplots_adjust(left=0.04, right=1, top=1, bottom=0.1)
figure_bytes = BytesIO()
plt.savefig(figure_bytes, dpi=150)
return EVENT_NAMES, FileBytesIO(figure_bytes)
def draw_wave_states(selections, normalize, TIMELINES):
plot_indices = [1, 2, 3, 4]
STATES = [["Empty", "Idle", "Exec", "Wait", "Stall"][k] for k in plot_indices]
colors = [["gray", "orange", "green", "red", "blue"][k] for k in plot_indices]
plt.figure(figsize=(15, 4))
maxtime = max([np.max((TIMELINES[k]!=0)*np.arange(0,TIMELINES[k].size)) for k in plot_indices])
maxtime = max(maxtime, 1)
timelines = [deepcopy(TIMELINES[k][:maxtime]) for k in plot_indices]
timelines = [np.pad(t, [0, maxtime - t.size]) for t in timelines]
if normalize:
timelines = np.array(timelines) / np.maximum(np.sum(timelines, 0) * 1e-2, 1e-7)
trim = max(maxtime // 5000, 1)
cycles = np.arange(0, timelines[0].size // trim, 1) * trim
timelines = [
time[: trim * (time.size // trim)].reshape((-1, trim)).mean(-1)
if len(time) > 0
else cycles * 0
for time in timelines
]
kernsize = 15
kernel = np.asarray([
np.exp(-abs(10 * k / kernsize)) for k in range(-kernsize // 2, kernsize // 2 + 1)
])
kernel /= np.sum(kernel)
timelines = [
np.convolve(time, kernel)[kernsize // 2 : -kernsize // 2]
for time in timelines if len(time) > 0
]
maxtime *= 16
cycles *= 16
[
plt.plot(cycles, t, label="State " + s, linewidth=1.1, color=c)
for t, s, c, sel in zip(timelines, STATES, colors, selections)
if sel
]
plt.legend()
if normalize:
plt.ylabel("Waves state %")
else:
plt.ylabel("Waves state total")
plt.xlabel("Cycle")
plt.ylim(-1)
plt.xlim(-maxtime // 200, maxtime + maxtime // 200 + 1)
plt.subplots_adjust(left=0.04, right=1, top=1, bottom=0.1)
figure_bytes = BytesIO()
plt.savefig(figure_bytes, dpi=150)
return STATES, FileBytesIO(figure_bytes)
def draw_occupancy_per_dispatch(selections, normalize, OCCUPANCY, dispatchnames):
plt.figure(figsize=(15, 4))
maxtime = 1
delta = 1
for k in range(len(OCCUPANCY)):
if len(OCCUPANCY[k]) <= 1:
continue
for ev in OCCUPANCY[k]:
maxtime = max(maxtime, ev[0])
NUM_DOTS = 1600 # number of points taken for graphing
delta = max(1, maxtime // NUM_DOTS) # Spacing between data points. Waves will be averaged over this interval.
# Holds occupancy data
chart = np.zeros((len(dispatchnames), maxtime // delta + 2), dtype=np.float32)
for occ in OCCUPANCY:
if len(occ) <= 1:
continue
# Number of waves multiplied by number of events
small_chart = np.zeros_like(chart)
# Holds number of events in that time period, for averaging.
norm_fact = np.zeros_like(chart)
norm_fact += 1E-5
# Holds last known state per dispatch
current_time = [0 for k in range(len(dispatchnames))]
# Holds occupancy per Dispatch
total_value = [0 for k in range(len(dispatchnames))]
for time, en, kid in occ:
b = current_time[kid]
e = max(b + 1, time // delta)
small_chart[kid][b:e] += total_value[kid]
norm_fact[kid][b:e] += 1
# Enable = 1 means a new wave started, enable = 0 means a wave has ended on that kernel ID.
total_value[kid] += 2*en - 1
current_time[kid] = time // delta
for small, norm, time, value in zip(small_chart, norm_fact, current_time, total_value):
small[time] += value
norm[time] += value
chart += small_chart/norm_fact # small_chart / norm_fact is the mean number of waves a tthat time point
for (id, name), occ in zip(dispatchnames.items(), chart):
plt.plot(np.arange(occ.size) * delta * 8, occ, label=str(id)+'#'+name, linewidth=1.1)
plt.legend()
if normalize:
plt.ylabel("Occupancy %")
else:
plt.ylabel("Occupancy total")
plt.xlabel("Cycle")
plt.ylim(-1)
plt.xlim(-maxtime // 200, maxtime + maxtime // 200 + delta + 1)
plt.subplots_adjust(left=0.04, right=1, top=1, bottom=0.1)
figure_bytes = BytesIO()
plt.savefig(figure_bytes, dpi=150)
return dispatchnames, FileBytesIO(figure_bytes)
def draw_occupancy(selections, normalize, OCCUPANCY, shadernames, numdispatchid):
plt.figure(figsize=(15, 4))
names = []
g_maxtime = 1
g_delta = 1
for name, occ in zip(shadernames, OCCUPANCY):
if len(occ) <= 1:
continue
occ_values = [0]
occ_times = [0]
for time, en, _ in occ:
occ_times.append(time)
occ_values.append(occ_values[-1] + 2*en - 1) # If enable = 1, increment. Else, decrement occupancy.
try:
names.append('SE'+name.split('_se')[1].split('.att')[0])
except:
names.append(name)
NUM_DOTS = 1500 # Number of points taken for graphing
maxtime = occ_times[-1]+1
delta = max(1, maxtime // NUM_DOTS)
g_maxtime = max(g_maxtime, maxtime)
g_delta = max(g_delta, delta)
chart = np.zeros((maxtime // delta + 1), dtype=np.float32)
norm_fact = np.zeros_like(chart)
norm_fact += 1E-6
for i in range(len(occ_times)-1):
b = occ_times[i] // delta
e = max(b + 1, occ_times[i + 1] // delta)
chart[b:e] += occ_values[i]
norm_fact[b:e] += 1
chart /= norm_fact
if normalize:
chart /= max(chart.max(), 1e-6)
plt.plot(np.arange(chart.size) * delta, chart, label=names[-1], linewidth=1.1)
plt.legend()
if normalize:
plt.ylabel("Occupancy %")
else:
plt.ylabel("Occupancy total")
plt.xlabel("Cycle")
plt.ylim(-1)
plt.xlim(-g_maxtime // 200, g_maxtime + g_maxtime // 200 + g_delta + 1)
plt.subplots_adjust(left=0.04, right=1, top=1, bottom=0.1)
figure_bytes = BytesIO()
plt.savefig(figure_bytes, dpi=150)
return names, FileBytesIO(figure_bytes)
def getocc(u):
# Parser struct occupancy_info_t
# Bits 23:63 Time= Time divided by 8
# Bit 18 = Enable (Wave start if 1, Wave end if 0)
# Bits 0:11 is the kernel ID running on that wave
return 8*int(u>>23), (u>>18) & 1, u&0xFFF
def GeneratePIC(drawinfo, selections=[True for k in range(16)], normalize=False):
EVENTS = drawinfo["EVENTS"]
response = {}
figures = {}
OCCUPANCY = drawinfo["OCCUPANCY"]
# Transforms returned data into a array of events with each event being a tuple (time, enable, kernel ID)
OCCUPANCY = [[getocc(u) for u in OCCUPANCY[k]] for k in range(len(OCCUPANCY))]
states, figure = draw_occupancy(selections, normalize, OCCUPANCY, drawinfo["ShaderNames"], len(drawinfo["DispatchNames"]))
response["occupancy.png"] = states
figures["occupancy.png"] = figure
states, figure = draw_occupancy_per_dispatch(selections, normalize, OCCUPANCY, drawinfo["DispatchNames"])
response["dispatches.png"] = states
figures["dispatches.png"] = figure
states, figure = draw_wave_states(selections, normalize, drawinfo["TIMELINES"])
response["timeline.png"] = states
figures["timeline.png"] = figure
if len(EVENTS) > 0 and np.sum([len(e) for e in EVENTS]) > 32:
EVENT_NAMES, figure = draw_wave_metrics(
selections, normalize, drawinfo["TIMELINES"], EVENTS, drawinfo["EVENT_NAMES"]
)
response["counters.png"] = EVENT_NAMES
figures["counters.png"] = figure
return Readable(response), figures
@@ -0,0 +1,77 @@
/* Copyright (c) 2023 Advanced Micro Devices, Inc.
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 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. */
#pragma once
#include <string>
#include <vector>
#include <iostream>
#include <random>
#include <set>
#include <algorithm>
struct address_range_t
{
uint64_t addr{0};
uint64_t size{0};
uint64_t id{0};
bool operator==(const address_range_t& other) const
{
return (addr >= other.addr && addr < other.addr + other.size) ||
(other.addr >= addr && other.addr < addr + size);
}
bool operator<(const address_range_t& other) const
{
if(*this == other) return false;
return addr < other.addr;
}
bool inrange(uint64_t _addr) const { return addr <= _addr && addr + size > _addr; };
};
/**
* @brief Finds a candidate codeobj for the given vaddr
*/
class CodeobjTableTranslator : public std::set<address_range_t>
{
using Super = std::set<address_range_t>;
public:
address_range_t find_codeobj_in_range(uint64_t addr)
{
if(!cached_segment.inrange(addr))
{
auto it = this->find(address_range_t{addr, 0, 0});
if(it == this->end()) throw std::exception();
cached_segment = *it;
}
return cached_segment;
}
void clear_cache() { cached_segment = {}; }
bool remove(const address_range_t& range)
{
clear_cache();
return this->erase(range) != 0;
}
bool remove(uint64_t addr) { return remove(address_range_t{addr, 0, 0}); }
private:
address_range_t cached_segment{};
};
+141
View File
@@ -0,0 +1,141 @@
#!/usr/bin/env python3
import sys
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3")
import ctypes
from ctypes import *
import os
HEADER_OFFSET = 62
HEADER_MASK = 0x3
ID_OFFSET = 34
ID_MASK = (1<<28)-1
OFFSET_MASK = (1<<ID_OFFSET)-1
pluginpath = '../../../lib/rocprofiler/libatt_plugin.so'
filedir = os.path.dirname(os.path.realpath(__file__))
attplugin = CDLL(os.path.join(filedir, pluginpath))
attplugin.getSymbolName.restype = c_char_p
attplugin.getSymbolName.argtypes = [c_uint64]
class instruction_info_t(ctypes.Structure):
_fields_ = [('inst', c_char_p),
('cpp', c_char_p),
('size', c_size_t)]
attplugin.getInstructionFromAddr.restype = instruction_info_t
attplugin.getInstructionFromAddr.argtypes = [c_uint64]
attplugin.getInstructionFromID.restype = instruction_info_t
attplugin.getInstructionFromID.argtypes = [c_uint32, c_uint64]
attplugin.addDecoder.restype = c_int
attplugin.addDecoder.argtypes = [c_char_p, c_uint32, c_uint64, c_uint64, c_uint64]
attplugin.removeDecoder.restype = c_int
attplugin.removeDecoder.argtypes = [c_uint32, c_uint64]
def IsRawPC(addr):
return addr >> HEADER_OFFSET == 0
def getID(addr):
return (addr >> ID_OFFSET) & ID_MASK
def getOffset(addr):
return addr & OFFSET_MASK
class CodeobjInstance:
def __init__(self, gpu_id, line):
tokens = line.split(' ')
self.load_base = int(tokens[0], 16)
self.memsize = int(tokens[1], 16)
self.att_id = int(tokens[2])
self.fpath = tokens[3]
path = self.fpath.encode('utf-8')
self.error = attplugin.addDecoder(path, self.att_id, self.load_base, self.memsize, gpu_id)
if self.error != 0:
print('Warning: Could not open', line)
raise
def release(self):
attplugin.removeDecoder(self.att_id, self.load_base)
class CodeobjService:
def __init__(self, gpu_id, att_kernel_txt, cfunc):
cfunc.restype = ctypes.c_int
cfunc.argtypes = [ctypes.c_char_p, ctypes.c_size_t]
self.classifier = cfunc
self.last_instance = None
self.services = {}
for line in att_kernel_txt:
try:
if 'memory://' == line[0:len('memory://')]:
continue
service = CodeobjInstance(gpu_id, line)
self.services[service.att_id] = service
except:
pass
def ToRawPC(self, addr):
if IsRawPC(addr):
return addr
return self.services[getID(addr)].load_base + getOffset(addr)
def release(self):
for _, instance in self.services.items():
instance.release()
def GetInstruction(self, addr):
if not IsRawPC(addr):
return self.GetInstructionFromID(getID(addr), getOffset(addr))
else:
return self.GetInstructionFromAddr(addr)
def GetInstructionFromAddr(self, addr):
info_inst = attplugin.getInstructionFromAddr(addr)
if info_inst.size == 0 or info_inst.inst is None:
return None
inst = info_inst.inst.decode()
cpp = info_inst.cpp
if cpp:
cpp = cpp.decode()
while len(inst) and (inst[0] == '\t' or inst[0] == ' '):
inst = inst[1:]
while len(inst) and (inst[-1] == '\t' or inst[-1] == ' '):
inst = inst[:-1]
return (self.classifier(info_inst.inst, len(inst)), inst, cpp, info_inst.size)
def GetInstructionFromID(self, id, offset):
info_inst = attplugin.getInstructionFromID(id, offset)
if info_inst.size == 0 or info_inst.inst is None:
return None
inst = info_inst.inst.decode()
cpp = info_inst.cpp
if cpp:
cpp = cpp.decode()
else:
cpp = ''
while len(inst) and (inst[0] == '\t' or inst[0] == ' '):
inst = inst[1:]
while len(inst) and (inst[-1] == '\t' or inst[-1] == ' '):
inst = inst[:-1]
return (self.classifier(info_inst.inst, len(inst)), inst, cpp, info_inst.size)
def getSymbolName(self, addr):
try:
name = attplugin.getSymbolName(self.ToRawPC(addr))
if name:
return name.decode()
return "Addr #"+hex(self.ToRawPC(addr))
except:
return "Addr #"+hex(addr)
+574
View File
@@ -0,0 +1,574 @@
#!/usr/bin/env python3
import sys
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3")
from collections import defaultdict
from copy import deepcopy
MAX_STITCHED_TOKENS = 200000000
MAX_FAILED_STITCHES = 256
SKIP = 0
SMEM = 1
SALU = 2
VMEM = 3
FLAT = 4
LDS = 5
VALU = 6
JUMP = 7
NEXT = 8
IMMED = 9
BRANCH = 10
GETPC = 11
SETPC = 12
SWAPPC = 13
LANEIO = 14
PCINFO = 15
WAVE_ENDED = 16
DONT_KNOW = 100
# Keeps track of register states for hipcc-generated assembly
class RegisterWatchList:
def __init__(self, labels, code, jump_map, insts):
self.registers = {"v" + str(k): [[] for m in range(64)] for k in range(64)}
for k in range(128):
self.registers["s" + str(k)] = []
self.labels = labels
self.code = code
self.jump_map = jump_map
self.insts = insts
def jump(self, as_line):
return self.jump_map[as_line[2]]
def getcode(self, line):
return self.code[line], 1
def getincrement(self, line):
return 1
def try_translate(self, tok):
if tok[0] in ["s"]:
return self.registers[self.range(tok)[0]]
elif "@" in tok:
return self.labels[tok.split("@")[0]] + 1
def range(self, r):
reg = r.split(":")
if len(reg) == 1:
return reg
else:
r0 = reg[0].split("[")
return [r0[0] + str(k) for k in range(int(r0[1]), int(reg[1][:-1]) + 1)]
def tokenize(self, line):
return [
u for u in [t.split(",")[0].strip() for t in line.split(" ")] if len(u) > 0
]
def getpc(self, line, next_line):
try:
dst = line.split(" ")[1].strip()
label_dests = []
try:
label_dests = next_line[0].split(", ")
except:
pass
try:
label_dests.append(next_line[0].split(", ")[-1].split("@")[0])
except:
pass
for label_dst in label_dests:
try:
cur_label = self.labels[label_dst]
for reg in self.range(dst):
self.registers[reg] = deepcopy(cur_label)
except:
pass
except:
pass
def swappc(self, line, line_num, inst_num):
try:
tokens = self.tokenize(line)
dst = tokens[1]
src = tokens[2]
popped = deepcopy(self.registers[self.range(src)[0]])
self.registers[self.range(dst)[0]] = line_num + 1
return popped
except:
return -1
def setpc(self, line, inst_num):
try:
src = line.split(' ')[1].strip()
return deepcopy(self.registers[self.range(src)[0]])
except:
return -1
def scratch(self, line):
try:
tokens = self.tokenize(line)
if "_load" in tokens[0]:
dst = tokens[1]
src = tokens[3] + tokens[4]
else:
src = tokens[2]
dst = tokens[3] + tokens[4]
self.registers[dst] = deepcopy(self.registers[src])
except:
pass
def move(self, line):
try:
tokens = self.tokenize(line)
if tokens[2][0] in ["s", "d"] and tokens[1][0] in ["s", "d"]:
self.registers[self.range(tokens[1])[0]] = deepcopy(
self.registers[self.range(tokens[2])[0]]
)
except:
pass
def updatelane(self, line):
tokens = self.tokenize(line)
try:
if "v_readlane" in tokens[0]:
self.registers[tokens[1]] = deepcopy(self.registers[tokens[2]][int(tokens[3])])
elif "v_writelane" in tokens[0]:
self.registers[tokens[1]][int(tokens[3])] = deepcopy(self.registers[tokens[2]])
except:
pass
# Matches tokens in reverse order
def try_match_swapped(self, i, line, increment):
try:
return self.insts[i + 1].type == self.code[line][1] and self.insts[i].type == self.code[line + 1][1]
except:
return False
# Translates PC values to instructions, for auto captured ISA
class PCTranslator:
def __init__(self, insts, code, raw_code, reverse_map, codeservice):
self.codeservice = codeservice
self.insts = insts
self.addrmap = {c[-3] : (c, self.codeservice.GetInstruction(c[-3])[3]) for c in code if c[-3] > 0}
self.code = code
self.raw_code = raw_code
self.reverse_map = reverse_map
self.jump_map = {c[-3] : self.getjump_loc(c) for c in code if c[1] == BRANCH}
def jump(self, as_line):
return self.jump_map[as_line[-3]]
def addsymbol(self, addr):
if addr in self.addrmap:
return
symbol = self.codeservice.getSymbolName(addr)
if symbol is None:
symbol = "Unkown symbol at 0x" + hex(addr)
last_line = self.raw_code[-1]
newline = ['; ' + symbol, DONT_KNOW, last_line[2], '', last_line[4], last_line[5], 0, 0, 0]
self.raw_code.append(newline)
def getcode(self, addr):
try:
return self.addrmap[addr]
except Exception as ex:
new_inst = self.codeservice.GetInstruction(addr)
if new_inst and new_inst[3]: # Check returned size > 0
last_line = self.raw_code[-1]
newline = [new_inst[1], new_inst[0], len(self.raw_code), new_inst[2], last_line[4]+1, last_line[5]+1, addr, 0, 0]
if new_inst[0] == BRANCH:
self.jump_map[addr] = self.getjump_loc(newline)
self.addrmap[addr] = (newline, new_inst[3])
next = len(self.code)
self.reverse_map[addr] = len(self.raw_code)
self.raw_code.append(newline)
self.code.append(newline)
return newline, new_inst[3]
else:
raise ex
def jump(self, asm_line):
try:
return self.jump_map[asm_line[-3]]
except:
loc = self.getjump_loc(asm_line)
self.jump_map[asm_line[-3]] = loc
return loc
def getjump_loc(self, asm_line):
try:
dest = int(asm_line[0].split(' ')[-1])
if dest >= 32768: dest -= 65536
return asm_line[-3] + 4*dest+4
except:
return -1
def getincrement(self, addr):
return self.getcode(addr)[1]
def try_translate(self, tok):
pass
def range(self, r):
pass
def tokenize(self, line):
pass
def getpc(self, line, next_line):
pass
def swappc(self, line, line_num, inst_index):
try:
inst_pos = inst_index+1
while self.insts[inst_pos].type != PCINFO:
inst_pos += 1
return self.getcode(self.insts[inst_pos].cycles)[0][-3]
except:
print('SWAPPC warning: Could not find addr for', inst_index, line)
return -1
def setpc(self, line, inst_index):
try:
inst_pos = inst_index+1
while self.insts[inst_pos].type != PCINFO:
inst_pos += 1
return self.getcode(self.insts[inst_pos].cycles)[0][-3]
except:
print('SETPC warning: Could not find addr for', inst_index, line)
return -1
def scratch(self, line):
pass
def move(self, line):
pass
def updatelane(self, line):
pass
# Matches tokens in reverse order
def try_match_swapped(self, i, addr, increment):
try:
return self.insts[i + 1].type == self.getcode(addr)[0][1] and \
self.insts[i].type == self.getcode(addr + increment)[0][1]
except Exception as e:
return False
def stitch(insts, raw_code, jumps, gfxv, bIsAuto, codeservice):
bGFX9 = gfxv == 'vega'
result, i, loopCount = [], 0, defaultdict(int)
SMEM_INST = [] # scalar memory
VLMEM_INST = [] # vector memory load
VSMEM_INST = [] # vector memory store
FLAT_INST = []
NUM_SMEM = 0
NUM_VLMEM = 0
NUM_VSMEM = 0
NUM_FLAT = 0
skipped_immed = 0
mem_unroll = []
flight_count = []
labels = {}
jump_map = [0]
# Clean the code and remove comments
code = [raw_code[0]]
for c in raw_code[1:]:
c = list(c)
c[0] = c[0].split(";")[0].split("//")[0].strip()
jump_map.append(len(code))
if c[1] != DONT_KNOW:
code.append(c)
elif ":" in c[0]:
labels[c[0].split(":")[0]] = len(code)
reverse_map = {}
if bIsAuto:
for k, v in enumerate(jump_map):
try:
reverse_map[code[v][-3]] = k
except:
pass
else:
for k, v in enumerate(jump_map):
reverse_map[v] = k
jumps = {jump_map[j] + 1: j for j in jumps}
# Checks if we have guaranteed ordering in memory operations
smem_ordering = 0
vlmem_ordering = 0
vsmem_ordering = 0
num_failed_stitches = 0
loops = 0
maxline = 0
pcskip = []
if bIsAuto:
try:
firstinst = insts[0]
if firstinst.type != PCINFO:
print('Warning: Waves without PCINFO')
return None
elif firstinst.cycles == 0:
print('Info: Some waves started before the trace')
return None
watchlist = PCTranslator(insts, code, raw_code, reverse_map, codeservice)
watchlist.addsymbol(firstinst.cycles)
line = firstinst.cycles
lineincrement = watchlist.getincrement(line)
except KeyError as e:
print('Warning: Waves from addr', hex(e.args[0]), 'have no codeobj info.')
for i in range(len(insts)):
insts[i].asmline = 0
return [i for k, i in enumerate(insts) if i.type != PCINFO], [], [], [], 1, 0, [k for k, i in enumerate(insts) if i.type == PCINFO]
except Exception as e:
print('Unknown error', e)
return None
else:
line = 0
lineincrement = 1
watchlist = RegisterWatchList(labels=labels, code=code, jump_map=jump_map, insts=insts)
N = len(insts)
while i < N and line >= 0 and loops < MAX_STITCHED_TOKENS:
if insts[i].type == PCINFO:
pcskip.append(i)
i += 1
continue
loops += 1
inst = insts[i]
try:
as_line, lineincrement = watchlist.getcode(line)
except:
break
matched = True
next = line + lineincrement
if not bIsAuto:
if '_mov_' in as_line[0]:
watchlist.move(as_line[0])
elif 'scratch_' in as_line[0]:
watchlist.scratch(as_line[0])
if as_line[1] == DONT_KNOW or (as_line[1] == SKIP and not bGFX9):
matched = False
elif as_line[1] == GETPC:
try:
watchlist.getpc(as_line[0], watchlist.getcode(next)[0])
matched = inst.type in [SALU, JUMP]
except:
matched = False
elif as_line[1] == LANEIO:
watchlist.updatelane(as_line[0])
matched = inst.type == VALU
elif as_line[1] == SETPC:
next = watchlist.setpc(as_line[0], i)
matched = inst.type in [SALU, JUMP]
i += 1
pcskip.append(i)
while bIsAuto and next < 0 and i+1 < len(insts):
i += 1
if insts[i].type == PCINFO:
pcskip.append(i)
next = watchlist.setpc(as_line[0], i-1)
else:
inst.cycles += insts[i].cycles
if next < 0:
print('Jump to unknown location in line', as_line[0])
break
elif as_line[1] == SWAPPC:
matched = inst.type in [SALU, JUMP]
next = watchlist.swappc(as_line[0], line, i)
i += 1
pcskip.append(i)
while bIsAuto and next < 0 and i+1 < len(insts):
i += 1
if insts[i].type == PCINFO:
next = watchlist.swappc(as_line[0], line, i-1)
pcskip.append(i)
else:
inst.cycles += insts[i].cycles
if next < 0:
print('Jump to unknown location in line', as_line[0])
break
elif inst.type == as_line[1]:
if line in jumps:
loopCount[jumps[line] - 1] += 1
num_inflight = NUM_FLAT + NUM_SMEM + NUM_VLMEM + NUM_VSMEM
if inst.type == SMEM or inst.type == LDS:
smem_ordering = 1 if inst.type == SMEM else smem_ordering
SMEM_INST.append([reverse_map[line], num_inflight])
NUM_SMEM += 1
elif inst.type == VMEM or (inst.type == FLAT and "global_" in as_line[0]):
inc_ordering = False
if "flat_" in as_line[0]:
inc_ordering = True
if not bGFX9 and "store" in as_line[0]:
VSMEM_INST.append([reverse_map[line], num_inflight])
NUM_VSMEM += 1
if inc_ordering:
vsmem_ordering = 1
else:
VLMEM_INST.append([reverse_map[line], num_inflight])
NUM_VLMEM += 1
if inc_ordering:
vlmem_ordering = 1
elif inst.type == FLAT:
smem_ordering = 1
vlmem_ordering = 1
vsmem_ordering = 1
FLAT_INST.append([reverse_map[line], num_inflight])
NUM_FLAT += 1
elif inst.type == IMMED and "s_wait" in as_line[0] and not "s_wait_alu" in as_line[0]:
if "lgkmcnt" in as_line[0] or "dscnt" in as_line[0] or "kmcnt" in as_line[0]:
try:
wait_N = int(as_line[0].split("lgkmcnt(")[1].split(")")[0])
except:
wait_N = 0
flight_count.append([as_line[5], num_inflight, wait_N])
if wait_N == 0:
smem_ordering = 0
if smem_ordering == 0:
offset = len(SMEM_INST) - wait_N
mem_unroll.append(
[reverse_map[line], SMEM_INST[:offset] + FLAT_INST]
)
SMEM_INST = SMEM_INST[offset:]
NUM_SMEM = len(SMEM_INST)
FLAT_INST = []
NUM_FLAT = 0
else:
NUM_SMEM = min(max(wait_N - NUM_FLAT, 0), NUM_SMEM)
NUM_FLAT = min(max(wait_N - NUM_SMEM, 0), NUM_FLAT)
num_inflight = NUM_FLAT + NUM_SMEM + NUM_VLMEM + NUM_VSMEM
if "vmcnt" in as_line[0] or "loadcnt" in as_line[0]:
try:
wait_N = int(as_line[0].split("vmcnt(")[1].split(")")[0])
except:
wait_N = 0
flight_count.append([as_line[5], num_inflight, wait_N])
if wait_N == 0:
vlmem_ordering = 0
if vlmem_ordering == 0:
offset = len(VLMEM_INST) - wait_N
mem_unroll.append(
[reverse_map[line], VLMEM_INST[:offset] + FLAT_INST]
)
VLMEM_INST = VLMEM_INST[offset:]
NUM_VLMEM = len(VLMEM_INST)
FLAT_INST = []
NUM_FLAT = 0
else:
NUM_VLMEM = min(max(wait_N - NUM_FLAT, 0), NUM_VLMEM)
NUM_FLAT = min(max(wait_N - NUM_VLMEM, 0), NUM_FLAT)
num_inflight = NUM_FLAT + NUM_SMEM + NUM_VLMEM + NUM_VSMEM
if "vscnt" in as_line[0] or (bGFX9 and "vmcnt" in as_line[0]) or "storecnt" in as_line[0]:
try:
wait_N = int(as_line[0].split('vscnt(')[1].split(')')[0])
except:
try:
wait_N = int(as_line[0].split('vmcnt(')[1].split(')')[0])
except:
wait_N = 0
flight_count.append([as_line[5], num_inflight, wait_N])
if wait_N == 0:
vsmem_ordering = 0
if vsmem_ordering == 0:
offset = len(VSMEM_INST) - wait_N
mem_unroll.append(
[reverse_map[line], VSMEM_INST[:offset] + FLAT_INST]
)
VSMEM_INST = VSMEM_INST[offset:]
NUM_VSMEM = len(VSMEM_INST)
FLAT_INST = []
NUM_FLAT = 0
else:
NUM_VSMEM = min(max(wait_N - NUM_FLAT, 0), NUM_VSMEM)
NUM_FLAT = min(max(wait_N - NUM_VSMEM, 0), NUM_FLAT)
num_inflight = NUM_FLAT + NUM_SMEM + NUM_VLMEM + NUM_VSMEM
elif inst.type == JUMP and as_line[1] == BRANCH:
next = watchlist.jump(as_line)
if next is None or next == 0:
print("Jump to unknown location!", as_line)
break
elif inst.type == NEXT and as_line[1] == BRANCH:
pass
else:
matched = False
if watchlist.try_match_swapped(i, line, lineincrement):
temp = insts[i]
insts[i] = insts[i + 1]
insts[i + 1] = temp
next = line
else:
hasWait = "s_wait" in as_line[0]
if hasWait or "_load_" in as_line[0]:
if skipped_immed > 0 and hasWait:
matched = True
skipped_immed -= 1
elif 's_waitcnt' in as_line[0] and 'scratch_' not in as_line[0]:
print('WARNING: Parsing terminated at:', as_line)
break
if matched or as_line[1] != DONT_KNOW:
if matched:
inst.asmline = reverse_map[line]
result.append(inst)
i += 1
num_failed_stitches = 0
elif not bGFX9 and inst.type == IMMED and line != next and as_line[1] != SKIP:
skipped_immed += 1
inst.asmline = reverse_map[line]
result.append(inst)
if 's_barrier' in as_line[0]:
next = line + lineincrement
i += 1
else:
num_failed_stitches += 1
maxline = max(reverse_map[line], maxline)
line = next
N = max(N, 1)
if i != N and (insts[i].type == WAVE_ENDED or i == N-1):
print('Warning - Wave ended.')
elif i < N:
print('Warning - Stitching rate: '+str(i * 100 / N)+'% matched', i, ' of ', N)
try:
print(line, code[line])
except:
pass
else:
while line < len(code):
if "s_endpgm" in code[line]:
mem_unroll.append(
[reverse_map[line], SMEM_INST + VLMEM_INST + VSMEM_INST + FLAT_INST]
)
break
line += 1
print('Success: Parsed', i, 'tokens')
return result, loopCount, mem_unroll, flight_count, maxline, len(result), pcskip
+270
View File
@@ -0,0 +1,270 @@
#!/usr/bin/env python3
import sys
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3")
import os
import sys
import time
import socket
from pathlib import Path
from collections import defaultdict
import http.server
import socketserver
import socket
import asyncio
import websockets
from multiprocessing import Process, Manager
import numpy as np
from http import HTTPStatus
from io import BytesIO
from drawing import Readable, GeneratePIC
from copy import deepcopy
from shutil import copy2
from glob import glob
JSON_GLOBAL_DICTIONARY = {}
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
s.connect(({IPAddr}, 1))
except Exception:
IPAddr = "127.0.0.1"
finally:
return IPAddr
IPAddr = get_ip()
PORT, WebSocketPort = 8000, 18000
SP = "\u00A0"
def get_top_n(code):
TOP_N = 10
top_n = sorted(deepcopy(code), key=lambda x: x[-1], reverse=True)[:TOP_N]
return [
(line_num, hitc, 0, run_time) for _, _, _, _, line_num, _, hitc, run_time in top_n
]
def wave_info(df, id):
dic = {
"Issue": df["issued_ins"][id],
"Valu": df["valu_ins"][id],
"Valu_stall": df["valu_stalls"][id],
"Salu": df["salu_ins"][id],
"Salu_stall": df["salu_stalls"][id],
"Vmem": df["vmem_ins"][id],
"Vmem_stall": df["vmem_stalls"][id],
"Smem": df["smem_ins"][id],
"Smem_stall": df["smem_stalls"][id],
"Flat": df["flat_ins"][id],
"Flat_stall": df["flat_stalls"][id],
"Lds": df["lds_ins"][id],
"Lds_stall": df["lds_stalls"][id],
"Br": df["br_ins"][id],
"Br_stall": df["br_stalls"][id],
}
dic["Issue_stall"] = int(np.sum([dic[key] for key in dic.keys() if "_STALL" in key]))
return dic
def extract_data(df, se_number):
if len(df["id"]) == 0 or len(df["instructions"]) == 0 or len(df["timeline"]) == 0:
return None
wave_filenames = []
flight_count = []
wave_slot_count = [
{df["wave_slot"][wave_id]: 0 for wave_id in df["id"]} for k in range(4)
]
print("Number of waves:", len(df["id"]))
allwaves_maxline = 0
for wave_id in df["id"]:
stitched, loopCount, mem_unroll, count, maxline, num_insts = df["instructions"][
wave_id
]
timeline = df["timeline"][wave_id]
if len(stitched) == 0 or len(timeline) == 0:
continue
allwaves_maxline = max(allwaves_maxline, maxline)
flight_count.append(count)
wave_entry = {
"id": int(df["id"][wave_id]),
"simd": int(df["simd"][wave_id]),
"slot": int(df["wave_slot"][wave_id]),
"begin": int(df["begin_time"][wave_id]),
"end": int(df["end_time"][wave_id]),
"info": wave_info(df, wave_id),
"instructions": stitched,
"timeline": timeline,
"waitcnt": mem_unroll,
}
data_obj = {
"name": "SE".format(se_number),
"duration": sum(dur for (_, dur) in timeline),
"wave": wave_entry,
"loop_count": loopCount,
"top_n": [],
"num_stitched": len(stitched),
"num_insts": num_insts,
"websocket_port": WebSocketPort,
"generation_time": time.ctime(),
}
simd_id = df["simd"][wave_id]
slot_id = df["wave_slot"][wave_id]
slot_count = wave_slot_count[simd_id][slot_id]
wave_slot_count[simd_id][slot_id] += 1
OUT = (
"se"
+ str(se_number)
+ "_sm"
+ str(simd_id)
+ "_sl"
+ str(slot_id)
+ "_wv"
+ str(slot_count)
+ ".json"
)
JSON_GLOBAL_DICTIONARY[OUT] = Readable(data_obj)
wave_filenames.append((OUT, df["begin_time"][wave_id], df["end_time"][wave_id]))
data_obj = {
"name": "SE".format(se_number),
"websocket_port": WebSocketPort,
"generation_time": time.ctime(),
}
se_filename = None
if len(wave_filenames) > 0:
se_filename = "se" + str(se_number) + "_info.json"
JSON_GLOBAL_DICTIONARY[se_filename] = Readable(data_obj)
return flight_count, wave_filenames, se_filename, allwaves_maxline
def call_picture_callback(return_dict, drawinfo):
response, imagebytes = GeneratePIC(drawinfo)
return_dict["graph_options.json"] = response
for k, v in imagebytes.items():
return_dict[k] = v
for n, m in enumerate(drawinfo["TIMELINES"]):
return_dict["wstates" + str(n) + ".json"] = Readable(
{"data": [int(n) for n in list(np.asarray(m))]}
)
for n, e in enumerate(drawinfo["EVENTS"]):
return_dict["se" + str(n) + "_perfcounter.json"] = Readable(
{"data": [v.toTuple() for v in e]}
)
def view_trace(
code,
dbnames,
att_filenames,
se_time_begin,
gfxv,
drawinfo,
trace_instance_name
):
global JSON_GLOBAL_DICTIONARY
pic_thread = None
manager = Manager()
return_dict = manager.dict()
occ_dict = {str(k): drawinfo["OCCUPANCY"][k] for k in range(len(drawinfo["OCCUPANCY"]))}
occ_dict['dispatches'] = {}
for id, name in drawinfo['DispatchNames'].items():
occ_dict['dispatches'][id] = name
occ_dict['names'] = drawinfo['ShaderNames']
JSON_GLOBAL_DICTIONARY["occupancy.json"] = Readable(occ_dict)
pic_thread = Process(target=call_picture_callback, args=(return_dict, drawinfo))
pic_thread.start()
att_filenames = [Path(f).name for f in att_filenames]
se_numbers = [int(a.split("_se")[1].split(".att")[0]) for a in att_filenames]
flight_count = []
simd_wave_filenames = {}
se_filenames = []
allse_maxline = 0
for se_number, dbname in zip(se_numbers, dbnames):
if len(dbname["id"]) == 0:
continue
count, wv_filenames, se_filename, maxline = extract_data(dbname, se_number)
if se_filename is None:
continue
allse_maxline = max(allse_maxline, maxline)
se_filenames.append(se_filename)
if count is not None:
flight_count.append(count)
simd_wave_filenames[se_number] = wv_filenames
code_sel = [c[:-3]+c[-2:] for c in code]
JSON_GLOBAL_DICTIONARY['code.json'] = Readable({"code": code_sel, "top_n": get_top_n(code_sel)})
for key in simd_wave_filenames.keys():
wv_array = [
[
int(s[0].split("_sm")[1].split("_sl")[0]),
int(s[0].split("_sl")[1].split("_wv")[0]),
int(s[0].split("_wv")[1].split(".")[0]),
s,
]
for s in simd_wave_filenames[key]
]
wv_dict = {}
for wv in wv_array:
try:
wv_dict[wv[0]][wv[1]][wv[2]] = wv[3]
except:
try:
wv_dict[wv[0]][wv[1]] = {wv[2]: wv[3]}
except:
try:
wv_dict[wv[0]] = {wv[1]: {wv[2]: wv[3]}}
except:
pass
simd_wave_filenames[key] = wv_dict
JSON_GLOBAL_DICTIONARY["filenames.json"] = Readable(
{
"wave_filenames": simd_wave_filenames,
"se_filenames": se_filenames,
"global_begin_time": int(se_time_begin),
"gfxv": gfxv,
}
)
if pic_thread is not None:
pic_thread.join()
for k, v in return_dict.items():
JSON_GLOBAL_DICTIONARY[k] = v
os.makedirs(trace_instance_name + "_ui/", exist_ok=True)
JSON_GLOBAL_DICTIONARY["live.json"] = Readable({"live": 0})
ui_dir_files = glob(os.path.join(os.path.abspath(os.path.dirname(__file__)), "ui")+"/*")
for f in ui_dir_files:
copy2(f, trace_instance_name + "_ui/")
for k, v in JSON_GLOBAL_DICTIONARY.items():
with open(os.path.join(trace_instance_name+"_ui", k), "w" if ".json" in k else "wb") as f:
f.write(v.read())
@@ -0,0 +1,70 @@
#!/usr/bin/env python3
import sys
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3")
import http.server
import socketserver
import socket
import os
import sys
class NoCacheHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_my_headers()
http.server.SimpleHTTPRequestHandler.end_headers(self)
def send_my_headers(self):
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
self.send_header("Pragma", "no-cache")
self.send_header("Expires", "0")
def do_GET(self):
if ".png?" in self.path:
self.path = self.path.split(".png?")[0] + ".png"
http.server.SimpleHTTPRequestHandler.do_GET(self)
class RocTCPServer(socketserver.TCPServer):
def server_bind(self):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.server_address)
def run_server():
Handler = NoCacheHTTPRequestHandler
os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), "."))
try:
with RocTCPServer((IPAddr, PORT), Handler) as httpd:
httpd.serve_forever()
except KeyboardInterrupt:
pass
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
s.connect(({IPAddr}, 1))
except Exception:
IPAddr = "127.0.0.1"
finally:
return IPAddr
IPAddr = get_ip()
PORT = 8000
if len(sys.argv) > 1:
PORT = int(sys.argv[1])
print("serving at port: {0}".format(PORT))
try:
run_server()
except KeyboardInterrupt:
print("Exitting.")
File diff suppressed because it is too large Load Diff
+132
View File
@@ -0,0 +1,132 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="240"
height="26"
viewBox="0 0 68.974872 7.4722778"
version="1.1"
id="svg5"
inkscape:version="1.2.1 (9c6d41e410, 2022-07-14)"
sodipodi:docname="logo.svg"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<sodipodi:namedview
id="namedview7"
pagecolor="#ffffff"
bordercolor="#000000"
borderopacity="0.25"
inkscape:showpageshadow="2"
inkscape:pageopacity="0.0"
inkscape:pagecheckerboard="0"
inkscape:deskcolor="#d1d1d1"
inkscape:document-units="mm"
showgrid="false"
inkscape:zoom="5.1659481"
inkscape:cx="147.89154"
inkscape:cy="75.10722"
inkscape:window-width="3840"
inkscape:window-height="2066"
inkscape:window-x="-11"
inkscape:window-y="-11"
inkscape:window-maximized="1"
inkscape:current-layer="layer1" />
<defs
id="defs2" />
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<rect
style="fill:#217844;stroke:#00000f;stroke-width:0.0610255;stroke-miterlimit:1"
id="rect864"
width="37.346996"
height="6.0453367"
x="26.672634"
y="0.91105449" />
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:6.35px;font-family:Calibri;-inkscape-font-specification:Calibri;fill:#165016;stroke:#00000f;stroke-width:0.0539999;stroke-miterlimit:1;stroke-dasharray:none"
x="82.180176"
y="95.625328"
id="text3303"><tspan
sodipodi:role="line"
id="tspan3301"
style="fill:#165016;stroke-width:0.054"
x="82.180176"
y="95.625328" /></text>
<!-- <g
id="g5730"
transform="matrix(0.57568164,0,0,0.53998363,-62.239348,-52.859622)">
<g
aria-label="AMD"
id="text954"
style="font-size:6.35px;font-family:Calibri;-inkscape-font-specification:Calibri;fill:#000080;stroke:#00000f;stroke-width:0.0539999;stroke-miterlimit:1"
transform="translate(27.503837,13.598552)">
<path
d="m 89.87666,89.436026 q 0.04779,0.05463 0.06348,0.09511 0.01746,0.03646 0.0022,0.06566 -0.01528,0.0292 -0.06797,0.05628 -0.0498,0.02596 -0.139412,0.0607 -0.08962,0.03475 -0.146787,0.05026 -0.05316,0.01729 -0.08543,0.01982 -0.0305,-0.0015 -0.05168,-0.01322 -0.01829,-0.01286 -0.03994,-0.0344 l -0.673965,-0.786215 -1.555301,0.60303 0.04216,1.017881 q 0.0014,0.02938 -0.0052,0.05522 -0.0077,0.02295 -0.03103,0.04861 -0.02154,0.02165 -0.07133,0.04761 -0.0469,0.02484 -0.127848,0.05622 -0.08384,0.0325 -0.142126,0.04513 -0.05428,0.01439 -0.08525,0.0031 -0.02808,-0.01239 -0.04265,-0.04997 -0.0128,-0.04159 -0.01433,-0.11416 l -0.09264,-3.961315 q -7.66e-4,-0.03628 0.0087,-0.06324 0.01239,-0.02808 0.0426,-0.0531 0.0331,-0.02613 0.08691,-0.05032 0.05558,-0.0282 0.142303,-0.06183 0.09251,-0.03587 0.155459,-0.05362 0.06183,-0.02065 0.103895,-0.02366 0.04206,-0.003 0.07127,0.01227 0.02808,0.01239 0.05198,0.03971 z m -2.799476,-2.442912 -0.0029,0.0011 0.07829,2.114582 1.300903,-0.504394 z"
style="stroke-width:0.054"
id="path4984" />
<path
d="m 94.99296,89.362526 q -2.68e-4,0.0248 -0.01287,0.04327 -0.0126,0.01847 -0.04374,0.03054 -0.0311,0.009 -0.08078,0.01463 -0.04661,0.0088 -0.124117,0.008 -0.07131,-7.69e-4 -0.123916,-0.01064 -0.04954,-0.0067 -0.08044,-0.01637 -0.02777,-0.0127 -0.03997,-0.03144 -0.0122,-0.01874 -0.01193,-0.04354 l 0.03766,-3.494157 -0.0062,-6.7e-5 -1.467119,3.494252 q -0.0095,0.0216 -0.0283,0.0369 -0.01567,0.01533 -0.04991,0.02737 -0.0311,0.009 -0.07764,0.01157 -0.04347,0.0057 -0.105481,0.0051 -0.06511,-7.02e-4 -0.111547,-0.0074 -0.04644,-0.0067 -0.07734,-0.01634 -0.03087,-0.01274 -0.04931,-0.02844 -0.01533,-0.01567 -0.02133,-0.03434 l -1.329447,-3.524398 -0.0031,-3.4e-5 -0.03767,3.494158 q -2.67e-4,0.0248 -0.01287,0.04327 -0.0126,0.01847 -0.04374,0.03054 -0.0311,0.009 -0.08078,0.01463 -0.04971,0.0088 -0.127216,0.0079 -0.07441,-8.02e-4 -0.123916,-0.01064 -0.04954,-0.0067 -0.08044,-0.01637 -0.02777,-0.0127 -0.03997,-0.03144 -0.0091,-0.0187 -0.0088,-0.04351 l 0.03977,-3.689482 q 0.0014,-0.130217 0.07022,-0.18529 0.06881,-0.05507 0.152521,-0.05417 l 0.322442,0.0035 q 0.09921,0.0011 0.173422,0.02047 0.07421,0.0194 0.129582,0.06031 0.05537,0.04091 0.09191,0.103316 0.03654,0.06241 0.06354,0.146421 l 1.131337,2.917613 0.0155,1.67e-4 1.240374,-2.882745 q 0.03511,-0.09265 0.07611,-0.157319 0.04101,-0.06467 0.08791,-0.101377 0.05004,-0.03977 0.109116,-0.05464 0.05911,-0.01797 0.136618,-0.01713 l 0.337944,0.0036 q 0.04651,5.01e-4 0.08665,0.01644 0.04327,0.01287 0.07084,0.04417 0.0307,0.02824 0.04881,0.07494 0.01813,0.04361 0.01743,0.108714 z"
style="stroke-width:0.054"
id="path4986" />
<path
d="m 99.688647,89.106481 q -0.202131,0.480082 -0.476607,0.788407 -0.273273,0.305468 -0.610318,0.442789 -0.332985,0.135666 -0.72523,0.10845 -0.392245,-0.02722 -0.866611,-0.22694 l -0.78299,-0.329666 q -0.06573,-0.02767 -0.107689,-0.0958 -0.0379,-0.06979 0.009,-0.181233 l 1.388446,-3.297704 q 0.04692,-0.111447 0.122123,-0.130248 0.07926,-0.02046 0.144986,0.0072 l 0.837285,0.352526 q 0.480082,0.202131 0.758628,0.467433 0.279749,0.262444 0.403985,0.590616 0.128297,0.326518 0.103035,0.706129 -0.0224,0.380814 -0.198064,0.798028 z m -0.525653,-0.197768 q 0.126331,-0.300051 0.160317,-0.58852 0.03519,-0.291326 -0.04543,-0.547307 -0.07776,-0.254777 -0.276143,-0.462778 -0.198382,-0.208 -0.581304,-0.369224 l -0.500085,-0.210553 -1.211581,2.877633 0.505801,0.212959 q 0.354346,0.149191 0.634542,0.162873 0.280196,0.01368 0.520083,-0.100626 0.239887,-0.114308 0.435404,-0.354952 0.199578,-0.242298 0.358396,-0.619505 z"
style="stroke-width:0.054"
id="path4988" />
</g>
<g
aria-label="Research"
id="text3463"
style="font-size:6.35px;font-family:Calibri;-inkscape-font-specification:Calibri;fill:#165016;stroke:#00000f;stroke-width:0.0539999;stroke-miterlimit:1"
transform="translate(28.562171,13.598552)">
<path
d="m 83.86747,96.36485 q 0,0.02481 -0.0093,0.04341 -0.0093,0.0186 -0.04031,0.03101 -0.031,0.0124 -0.08682,0.0186 -0.05581,0.0062 -0.148828,0.0062 -0.08062,0 -0.133325,-0.0062 -0.04961,-0.0062 -0.08062,-0.0186 -0.031,-0.0155 -0.04961,-0.04031 -0.0155,-0.0248 -0.0279,-0.06201 l -0.36897,-0.945679 q -0.06511,-0.161231 -0.136426,-0.294556 -0.06821,-0.136426 -0.164331,-0.232544 -0.09612,-0.09922 -0.226343,-0.151928 -0.130224,-0.05581 -0.313159,-0.05581 h -0.356567 v 1.708423 q 0,0.02481 -0.0155,0.04341 -0.0124,0.0186 -0.04341,0.03101 -0.02791,0.0093 -0.08062,0.0155 -0.04961,0.0093 -0.127124,0.0093 -0.07752,0 -0.130225,-0.0093 -0.04961,-0.0062 -0.08062,-0.0155 -0.03101,-0.0124 -0.04341,-0.03101 -0.0124,-0.0186 -0.0124,-0.04341 v -3.714502 q 0,-0.120923 0.06201,-0.167432 0.06511,-0.04961 0.136426,-0.04961 h 0.852661 q 0.151928,0 0.251147,0.0093 0.10232,0.0062 0.182935,0.0155 0.232544,0.04031 0.409277,0.127124 0.179834,0.08682 0.300757,0.220141 0.120923,0.133325 0.179834,0.306958 0.06201,0.170532 0.06201,0.378272 0,0.201538 -0.05581,0.362768 -0.05271,0.15813 -0.155029,0.282154 -0.10232,0.120922 -0.244947,0.210839 -0.142626,0.08992 -0.31936,0.151929 0.09922,0.04341 0.179834,0.111621 0.08061,0.06511 0.148828,0.15813 0.07131,0.09302 0.133325,0.213941 0.06201,0.120922 0.124024,0.272851 l 0.359668,0.883667 q 0.04341,0.111621 0.05581,0.15813 0.0124,0.04341 0.0124,0.06821 z m -0.803052,-2.818433 q 0,-0.235644 -0.10542,-0.396875 -0.10542,-0.164331 -0.353467,-0.235644 -0.07751,-0.0217 -0.176733,-0.03101 -0.09612,-0.0093 -0.254248,-0.0093 h -0.449585 v 1.351856 h 0.520898 q 0.21084,0 0.362769,-0.04961 0.155029,-0.05271 0.257348,-0.142627 0.10232,-0.09302 0.148829,-0.217041 0.04961,-0.124023 0.04961,-0.269751 z"
style="stroke-width:0.054"
id="path4967" />
<path
d="m 86.850233,94.851764 q 0,0.120923 -0.06201,0.173633 -0.05891,0.04961 -0.136426,0.04961 H 84.82245 q 0,0.232544 0.04651,0.418579 0.04651,0.186035 0.155029,0.319361 0.108521,0.133325 0.282154,0.204638 0.173633,0.07131 0.42478,0.07131 0.198437,0 0.353467,-0.03101 0.155029,-0.03411 0.26665,-0.07441 0.114722,-0.04031 0.186035,-0.07131 0.07441,-0.03411 0.111621,-0.03411 0.0217,0 0.03721,0.0124 0.0186,0.0093 0.02791,0.03101 0.0093,0.0217 0.0124,0.06201 0.0062,0.03721 0.0062,0.09302 0,0.04031 -0.0031,0.07131 -0.0031,0.02791 -0.0093,0.05271 -0.0031,0.0217 -0.0155,0.04031 -0.0093,0.0186 -0.0279,0.03721 -0.0155,0.0155 -0.09922,0.05581 -0.08372,0.03721 -0.217041,0.07441 -0.133325,0.03721 -0.310059,0.06511 -0.173633,0.03101 -0.37207,0.03101 -0.344165,0 -0.604614,-0.09612 -0.257349,-0.09612 -0.434082,-0.285254 -0.176734,-0.189136 -0.266651,-0.474389 -0.08992,-0.285254 -0.08992,-0.663526 0,-0.359668 0.09302,-0.644922 0.09302,-0.288354 0.26665,-0.486792 0.176734,-0.201538 0.424781,-0.306958 0.248046,-0.10852 0.555004,-0.10852 0.328663,0 0.558106,0.10542 0.232544,0.10542 0.381372,0.285254 0.148828,0.176733 0.217041,0.418579 0.07131,0.238745 0.07131,0.511596 z m -0.514697,-0.151929 q 0.0093,-0.403076 -0.179834,-0.632519 -0.186035,-0.229444 -0.555005,-0.229444 -0.189135,0 -0.331762,0.07131 -0.142627,0.07131 -0.238745,0.189136 -0.09612,0.117822 -0.148829,0.275952 -0.05271,0.155029 -0.05891,0.325561 z"
style="stroke-width:0.054"
id="path4969" />
<path
d="m 89.380312,95.608307 q 0,0.21394 -0.08062,0.381372 -0.07751,0.167432 -0.223242,0.282153 -0.145727,0.114722 -0.347265,0.173633 -0.201539,0.05891 -0.443384,0.05891 -0.148828,0 -0.285254,-0.0248 -0.133325,-0.02171 -0.241846,-0.05581 -0.10542,-0.03721 -0.179834,-0.07441 -0.07441,-0.04031 -0.10852,-0.07131 -0.03411,-0.031 -0.04961,-0.08682 -0.0155,-0.05581 -0.0155,-0.151929 0,-0.05891 0.0062,-0.09922 0.0062,-0.04031 0.0155,-0.06511 0.0093,-0.0248 0.02481,-0.03411 0.0186,-0.0124 0.04031,-0.0124 0.03411,0 0.09922,0.04341 0.06821,0.04031 0.164332,0.08992 0.09922,0.04961 0.232543,0.09302 0.133326,0.04031 0.306958,0.04031 0.130225,0 0.235645,-0.02791 0.10542,-0.02791 0.182935,-0.08062 0.07751,-0.05581 0.117822,-0.139526 0.04341,-0.08372 0.04341,-0.198437 0,-0.117823 -0.06201,-0.198438 -0.05891,-0.08061 -0.15813,-0.142627 -0.09922,-0.06201 -0.223242,-0.10852 -0.124023,-0.04961 -0.257348,-0.10232 -0.130225,-0.05271 -0.257349,-0.117822 -0.124023,-0.06821 -0.223242,-0.164331 -0.09922,-0.09612 -0.161231,-0.229443 -0.05891,-0.133326 -0.05891,-0.319361 0,-0.164331 0.06201,-0.313159 0.06511,-0.151929 0.192236,-0.26355 0.127124,-0.114721 0.31626,-0.182934 0.192236,-0.06821 0.446484,-0.06821 0.111621,0 0.223243,0.0186 0.111621,0.0186 0.201538,0.04651 0.08992,0.0279 0.151928,0.06201 0.06511,0.03101 0.09612,0.05581 0.03411,0.02481 0.04341,0.04341 0.0124,0.0186 0.0155,0.04341 0.0062,0.0217 0.0093,0.05581 0.0062,0.03411 0.0062,0.08372 0,0.05271 -0.0062,0.09302 -0.0031,0.03721 -0.0155,0.06201 -0.0093,0.02481 -0.0248,0.03721 -0.0155,0.0093 -0.03411,0.0093 -0.02791,0 -0.08062,-0.03411 -0.05271,-0.03411 -0.136426,-0.07131 -0.08371,-0.04031 -0.198437,-0.07441 -0.111621,-0.03411 -0.257349,-0.03411 -0.130224,0 -0.229443,0.03101 -0.09922,0.0279 -0.164331,0.08371 -0.06201,0.05271 -0.09612,0.127124 -0.03101,0.07442 -0.03101,0.161231 0,0.120923 0.06201,0.204639 0.06201,0.08061 0.16123,0.142627 0.09922,0.06201 0.226343,0.111621 0.127124,0.04961 0.257348,0.102319 0.133326,0.05271 0.26045,0.117822 0.130224,0.06511 0.229443,0.15813 0.09922,0.09302 0.15813,0.223242 0.06201,0.130225 0.06201,0.310059 z"
style="stroke-width:0.054"
id="path4971" />
<path
d="m 92.4933,94.851764 q 0,0.120923 -0.06201,0.173633 -0.05891,0.04961 -0.136426,0.04961 h -1.829346 q 0,0.232544 0.04651,0.418579 0.04651,0.186035 0.15503,0.319361 0.10852,0.133325 0.282153,0.204638 0.173633,0.07131 0.42478,0.07131 0.198438,0 0.353467,-0.03101 0.155029,-0.03411 0.26665,-0.07441 0.114722,-0.04031 0.186035,-0.07131 0.07442,-0.03411 0.111622,-0.03411 0.0217,0 0.03721,0.0124 0.0186,0.0093 0.02791,0.03101 0.0093,0.0217 0.0124,0.06201 0.0062,0.03721 0.0062,0.09302 0,0.04031 -0.0031,0.07131 -0.0031,0.02791 -0.0093,0.05271 -0.0031,0.0217 -0.0155,0.04031 -0.0093,0.0186 -0.02791,0.03721 -0.0155,0.0155 -0.09922,0.05581 -0.08372,0.03721 -0.217041,0.07441 -0.133325,0.03721 -0.310059,0.06511 -0.173632,0.03101 -0.37207,0.03101 -0.344165,0 -0.604614,-0.09612 -0.257349,-0.09612 -0.434082,-0.285254 -0.176734,-0.189136 -0.26665,-0.474389 -0.08992,-0.285254 -0.08992,-0.663526 0,-0.359668 0.09302,-0.644922 0.09302,-0.288354 0.26665,-0.486792 0.176734,-0.201538 0.424781,-0.306958 0.248047,-0.10852 0.555005,-0.10852 0.328662,0 0.558105,0.10542 0.232544,0.10542 0.381372,0.285254 0.148828,0.176733 0.217041,0.418579 0.07131,0.238745 0.07131,0.511596 z m -0.514698,-0.151929 q 0.0093,-0.403076 -0.179834,-0.632519 -0.186035,-0.229444 -0.555004,-0.229444 -0.189136,0 -0.331763,0.07131 -0.142627,0.07131 -0.238745,0.189136 -0.09612,0.117822 -0.148828,0.275952 -0.05271,0.155029 -0.05891,0.325561 z"
style="stroke-width:0.054"
id="path4973" />
<path
d="m 95.373744,96.371051 q 0,0.03721 -0.02481,0.05581 -0.0248,0.0186 -0.06821,0.02791 -0.04341,0.0093 -0.127124,0.0093 -0.08062,0 -0.130225,-0.0093 -0.04651,-0.0093 -0.06821,-0.02791 -0.0217,-0.0186 -0.0217,-0.05581 v -0.279053 q -0.182935,0.195337 -0.409277,0.303858 -0.223243,0.10852 -0.47439,0.10852 -0.220142,0 -0.399976,-0.05891 -0.176733,-0.05581 -0.303857,-0.164331 -0.124023,-0.10852 -0.195337,-0.26665 -0.06821,-0.15813 -0.06821,-0.359668 0,-0.235645 0.09612,-0.409278 0.09612,-0.173632 0.275952,-0.288354 0.179834,-0.114722 0.440284,-0.170532 0.260449,-0.05891 0.58601,-0.05891 h 0.384473 v -0.217042 q 0,-0.16123 -0.03411,-0.285253 -0.03411,-0.124024 -0.111621,-0.204639 -0.07442,-0.08372 -0.195337,-0.124024 -0.120923,-0.04341 -0.297657,-0.04341 -0.189135,0 -0.341064,0.04651 -0.148828,0.04341 -0.26355,0.09922 -0.111621,0.05271 -0.189136,0.09922 -0.07441,0.04341 -0.111621,0.04341 -0.0248,0 -0.04341,-0.0124 -0.0186,-0.0124 -0.03411,-0.03721 -0.0124,-0.0248 -0.0186,-0.06201 -0.0062,-0.04031 -0.0062,-0.08682 0,-0.07751 0.0093,-0.120923 0.0124,-0.04651 0.05271,-0.08682 0.04341,-0.04031 0.145727,-0.09302 0.10232,-0.05581 0.235645,-0.09922 0.133325,-0.04651 0.291455,-0.07441 0.15813,-0.03101 0.31936,-0.03101 0.300757,0 0.511597,0.06821 0.21084,0.06821 0.341064,0.201538 0.130225,0.130224 0.189136,0.325561 0.05891,0.195337 0.05891,0.455786 z M 94.865248,95.09671 h -0.437183 q -0.210839,0 -0.365869,0.03721 -0.155029,0.03411 -0.257348,0.10542 -0.10232,0.06821 -0.151929,0.167432 -0.04651,0.09612 -0.04651,0.223242 0,0.217041 0.136426,0.347266 0.139526,0.127124 0.387573,0.127124 0.201538,0 0.372071,-0.10232 0.173632,-0.102319 0.362768,-0.313159 z"
style="stroke-width:0.054"
id="path4975" />
<path
d="m 97.94413,93.760358 q 0,0.06821 -0.0031,0.114721 -0.0031,0.04651 -0.0124,0.07441 -0.0093,0.02481 -0.02481,0.04031 -0.0124,0.0124 -0.03721,0.0124 -0.02481,0 -0.06201,-0.0124 -0.03411,-0.0155 -0.08061,-0.02791 -0.04341,-0.0155 -0.09922,-0.0279 -0.05581,-0.0124 -0.120922,-0.0124 -0.07751,0 -0.151929,0.03101 -0.07441,0.03101 -0.15813,0.102319 -0.08061,0.07131 -0.170532,0.189136 -0.08992,0.117822 -0.198438,0.288355 v 1.835546 q 0,0.02481 -0.0124,0.04341 -0.0124,0.0155 -0.04031,0.0279 -0.02791,0.0124 -0.07751,0.0186 -0.04961,0.0062 -0.127124,0.0062 -0.07441,0 -0.124024,-0.0062 -0.04961,-0.0062 -0.08061,-0.0186 -0.02791,-0.0124 -0.04031,-0.0279 -0.0093,-0.0186 -0.0093,-0.04341 v -2.790527 q 0,-0.02481 0.0093,-0.04031 0.0093,-0.0186 0.03721,-0.03101 0.02791,-0.0155 0.07131,-0.0186 0.04341,-0.0062 0.114721,-0.0062 0.06821,0 0.111622,0.0062 0.04651,0.0031 0.07131,0.0186 0.02481,0.0124 0.03411,0.03101 0.0124,0.0155 0.0124,0.04031 V 93.9836 q 0.114722,-0.167432 0.21394,-0.272852 0.10232,-0.10542 0.192237,-0.164331 0.08992,-0.06201 0.176733,-0.08372 0.08992,-0.0248 0.179834,-0.0248 0.04031,0 0.08992,0.0062 0.05271,0.0031 0.10852,0.0155 0.05581,0.0124 0.09922,0.0279 0.04651,0.0155 0.06511,0.03101 0.0186,0.0155 0.0248,0.03101 0.0062,0.0124 0.0093,0.03411 0.0062,0.0217 0.0062,0.06511 0.0031,0.04031 0.0031,0.111621 z"
style="stroke-width:0.054"
id="path4977" />
<path
d="m 100.4401,95.94317 q 0,0.05271 -0.003,0.09302 -0.003,0.03721 -0.0124,0.06511 -0.006,0.02481 -0.0186,0.04651 -0.009,0.0186 -0.0496,0.05891 -0.0372,0.03721 -0.13023,0.09612 -0.093,0.05581 -0.21084,0.102319 -0.11472,0.04341 -0.251146,0.07131 -0.136426,0.0279 -0.282153,0.0279 -0.300757,0 -0.533301,-0.09922 -0.232544,-0.09922 -0.390674,-0.288354 -0.155029,-0.192236 -0.238745,-0.468188 -0.08061,-0.279053 -0.08061,-0.641822 0,-0.412378 0.09922,-0.706933 0.10232,-0.297657 0.275952,-0.486792 0.176734,-0.189136 0.412378,-0.279053 0.238746,-0.09302 0.514698,-0.09302 0.133325,0 0.257348,0.02481 0.127124,0.02481 0.232543,0.06511 0.10542,0.04031 0.18604,0.09302 0.0837,0.05271 0.12092,0.08992 0.0372,0.03721 0.0496,0.05891 0.0155,0.0217 0.0248,0.05271 0.009,0.02791 0.0124,0.06511 0.003,0.03721 0.003,0.09302 0,0.120923 -0.0279,0.170532 -0.0279,0.04651 -0.0682,0.04651 -0.0465,0 -0.10852,-0.04961 -0.0589,-0.05271 -0.15193,-0.114722 -0.09302,-0.06201 -0.226344,-0.111621 -0.130225,-0.05271 -0.310058,-0.05271 -0.36897,0 -0.567408,0.285254 -0.195337,0.282153 -0.195337,0.821655 0,0.269751 0.04961,0.47439 0.05271,0.201538 0.151928,0.337963 0.09922,0.136426 0.241846,0.204639 0.145728,0.06511 0.331763,0.06511 0.176733,0 0.310058,-0.05581 0.133326,-0.05581 0.229442,-0.120923 0.0992,-0.06821 0.16434,-0.120923 0.0682,-0.05581 0.10542,-0.05581 0.0217,0 0.0372,0.0124 0.0155,0.0124 0.0248,0.04341 0.0124,0.02791 0.0155,0.07441 0.006,0.04341 0.006,0.10542 z"
style="stroke-width:0.054"
id="path4979" />
<path
d="m 103.52208,96.36795 q 0,0.02481 -0.0124,0.04341 -0.0124,0.0155 -0.0403,0.0279 -0.0279,0.0124 -0.0775,0.0186 -0.0496,0.0062 -0.12402,0.0062 -0.0775,0 -0.12713,-0.0062 -0.0496,-0.0062 -0.0775,-0.0186 -0.0279,-0.0124 -0.0403,-0.0279 -0.0124,-0.0186 -0.0124,-0.04341 v -1.634008 q 0,-0.238745 -0.0372,-0.384473 -0.0372,-0.145728 -0.10852,-0.251147 -0.0713,-0.10542 -0.18603,-0.161231 -0.11163,-0.05581 -0.26045,-0.05581 -0.19224,0 -0.38448,0.136425 -0.19223,0.136426 -0.40307,0.399976 v 1.950268 q 0,0.02481 -0.0124,0.04341 -0.0124,0.0155 -0.0403,0.0279 -0.0279,0.0124 -0.0775,0.0186 -0.0496,0.0062 -0.12712,0.0062 -0.0744,0 -0.12402,-0.0062 -0.0496,-0.0062 -0.0806,-0.0186 -0.0279,-0.0124 -0.0403,-0.0279 -0.009,-0.0186 -0.009,-0.04341 v -4.142382 q 0,-0.02481 0.009,-0.04341 0.0124,-0.0186 0.0403,-0.031 0.031,-0.0124 0.0806,-0.0186 0.0496,-0.0062 0.12402,-0.0062 0.0775,0 0.12712,0.0062 0.0496,0.0062 0.0775,0.0186 0.0279,0.0124 0.0403,0.031 0.0124,0.0186 0.0124,0.04341 v 1.671215 q 0.22014,-0.232543 0.44338,-0.344165 0.22324,-0.114721 0.44959,-0.114721 0.27905,0 0.46818,0.09612 0.19224,0.09302 0.31006,0.251147 0.11783,0.15813 0.16743,0.372071 0.0527,0.21084 0.0527,0.511596 z"
style="stroke-width:0.054"
id="path4981" />
</g>
</g> -->
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:6.1311px;font-family:Calibri;-inkscape-font-specification:Calibri;fill:#f9f9f9;stroke:#00000f;stroke-width:0.0586557;stroke-miterlimit:1"
x="27.642879"
y="5.8916035"
id="text1590"><tspan
sodipodi:role="line"
id="tspan1588"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:6.1311px;font-family:Calibri;-inkscape-font-specification:Calibri;fill:#f9f9f9;stroke-width:0.0586558"
x="27.642879"
y="5.8916035">MI Trace View</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 20 KiB

@@ -0,0 +1,137 @@
h2,h3,h4 {
text-align: center;
font-family: Calibri, Candara, Optima, Arial, 'Trebuchet MS', sans-serif;
}
ul, ol {
list-style-type: none;
padding: 0;
margin: 20px 5px;
/*font-family: Calibri, Candara, Segoe, "Segoe UI", Optima, Arial, sans-serif; font-size: 15px; font-style: normal; font-variant: normal; */
font-family: 'Courier New', monospace; font-size: 15px; font-style: normal;
}
a {
color: royalblue;
cursor: pointer;
}
div#flexbox {
display: flex;
flex-wrap: wrap;
}
div#flexbox > div {
flex: 50%;
}
div#minimap {
position: absolute;
width: 350px;
top: 340px;
display: flex;
justify-content: right;
right: 5px;
}
div#wave, div#cu_wave {
overflow:scroll;
overflow-y:hidden;
}
div#ma_code {
height: calc(100vh - 180px);
overflow: auto;
overflow-x: hidden;
display: block;
margin: 20px;
}
nav {
padding-right: 8px;
position: absolute;
top: 1rem;
right: 1rem;
background: #FFFDE3;
z-index: 5;
margin-left: 10px;
width:330px;
}
.highlight {
background-color: lightgray;
font-weight: bold;
}
.clickable {
cursor: pointer;
font-weight: 180;
}
ul li {
border-bottom: 1px dotted black;
}
.tooltip {
display: none;
position: relative;
left: 10px;
z-index: 100;
background: #333;
border: 1px solid #c0c0c0;
opacity: 0.8;
color: white;
}
li:hover .tooltip {
display: inline-block; /*block;*/
}
.loop {
margin-left: 30px;
background: lightseagreen;
}
.btn {
border: 1px solid black;
background-color: #D7D7D7;
color: black;
padding: 3px 4px;
font-size: 14px;
cursor: pointer;
border-style: ridge;
border-radius: 6px;
}
.btn:hover {
color: blue;
}
.dropbtn {
border: 2px solid black;
background-color: #D7D7D7;
color: black;
padding: 3px 4px;
font-size: 15px;
cursor: pointer;
border-style: ridge;
border-radius: 4px;
}
.dropbtn:hover, .dropbtn:focus {
color: blue;
}
.dropdown {
position: relative;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #e0e0f0;
min-width: 10px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0.1,0.1);
z-index: 1;
}
.dropdown-content a:hover {background-color: #ddd;}
.show {display:inline-flex;}
@@ -0,0 +1,50 @@
# ###############################################################################
# # Copyright (c) 2022 Advanced Micro Devices, Inc.
# #
# # 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 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.
# ###############################################################################
file(GLOB ROCPROFILER_UTIL_SRC_FILES ${PROJECT_SOURCE_DIR}/src/utils/helper.cpp)
file(GLOB CLI_SOURCES "*.cpp")
add_library(cli_plugin SHARED ${CLI_SOURCES} ${ROCPROFILER_UTIL_SRC_FILES})
set_target_properties(
cli_plugin
PROPERTIES CXX_VISIBILITY_PRESET hidden
LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib/rocprofiler
INSTALL_RPATH "${ROCM_APPEND_PRIVLIB_RPATH}")
target_compile_definitions(cli_plugin PRIVATE HIP_PROF_HIP_API_STRING=1
__HIP_PLATFORM_AMD__=1)
target_include_directories(cli_plugin PRIVATE ${PROJECT_SOURCE_DIR})
target_link_options(
cli_plugin PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
-Wl,--no-undefined)
target_link_libraries(cli_plugin PRIVATE rocprofiler-v2 hsa-runtime64::hsa-runtime64
stdc++fs atomic amd_comgr dl)
install(TARGETS cli_plugin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
COMPONENT asan)
install(TARGETS cli_plugin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
COMPONENT runtime)
+418
View File
@@ -0,0 +1,418 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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 <cxxabi.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <atomic>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <ostream>
#include <sstream>
#include <string>
#include <hsa/hsa.h>
#include <mutex>
#include <unordered_map>
#include "rocprofiler.h"
#include "rocprofiler_plugin.h"
#include "../utils.h"
#include "src/utils/filesystem.hpp"
namespace fs = rocprofiler::common::filesystem;
namespace {
static std::string output_file_name;
class file_plugin_t {
private:
enum class output_type_t { COUNTER, TRACER, PC_SAMPLING };
class output_file_t {
public:
output_file_t(std::string name) : name_(std::move(name)) {}
std::string name() const { return name_; }
template <typename T> std::ostream& operator<<(T&& value) {
if (!is_open()) open();
return stream_ << std::forward<T>(value);
}
std::ostream& operator<<(std::ostream& (*func)(std::ostream&)) {
if (!is_open()) open();
return stream_ << func;
}
void open() {
// If the stream is already in the failed state, there's no need to try
// to open the file.
if (fail()) return;
const char* output_dir = getenv("OUTPUT_PATH");
output_file_name = getenv("OUT_FILE_NAME") ? std::string(getenv("OUT_FILE_NAME")) + "_" : "";
if (output_dir == nullptr && getenv("OUT_FILE_NAME") == nullptr) {
stream_.copyfmt(std::cout);
stream_.clear(std::cout.rdstate());
stream_.basic_ios<char>::rdbuf(std::cout.rdbuf());
return;
}
if (output_dir == nullptr) output_dir = "./";
fs::path output_prefix(output_dir);
if (!fs::is_directory(fs::status(output_prefix))) {
if (!stream_.fail()) rocprofiler::warning("Cannot open output directory '%s'", output_dir);
stream_.setstate(std::ios_base::failbit);
return;
}
std::stringstream ss;
ss << output_file_name << GetPid() << "_" << name_;
stream_.open(output_prefix / ss.str());
}
bool is_open() const { return stream_.is_open(); }
bool fail() const { return stream_.fail(); }
private:
const std::string name_;
std::ofstream stream_;
};
output_file_t* get_output_file(output_type_t output_type, uint32_t domain = 0) {
switch (output_type) {
case output_type_t::COUNTER:
return &output_file_;
case output_type_t::TRACER:
switch (domain) {
case ACTIVITY_DOMAIN_ROCTX:
return &roctx_file_;
case ACTIVITY_DOMAIN_HSA_API:
return &hsa_api_file_;
case ACTIVITY_DOMAIN_HIP_API:
return &hip_api_file_;
case ACTIVITY_DOMAIN_HIP_OPS:
return &hip_activity_file_;
case ACTIVITY_DOMAIN_HSA_OPS:
return &hsa_async_copy_file_;
default:
assert(!"domain/op not supported!");
break;
}
break;
case output_type_t::PC_SAMPLING:
return &pc_sample_file_;
}
return nullptr;
}
public:
file_plugin_t() {
valid_ = true;
kernel_filters_ = GetKernelFilters();
}
std::mutex writing_lock;
const char* GetDomainName(rocprofiler_tracer_activity_domain_t domain) {
switch (domain) {
case ACTIVITY_DOMAIN_ROCTX:
return "ROCTX_DOMAIN";
break;
case ACTIVITY_DOMAIN_HIP_API:
return "HIP_API_DOMAIN";
break;
case ACTIVITY_DOMAIN_HIP_OPS:
return "HIP_OPS_DOMAIN";
break;
case ACTIVITY_DOMAIN_HSA_API:
return "HSA_API_DOMAIN";
break;
case ACTIVITY_DOMAIN_HSA_OPS:
return "HSA_OPS_DOMAIN";
break;
case ACTIVITY_DOMAIN_HSA_EVT:
return "HSA_EVT_DOMAIN";
break;
default:
return "";
}
}
void FlushTracerRecord(rocprofiler_record_tracer_t tracer_record,
rocprofiler_session_id_t session_id,
rocprofiler_buffer_id_t buffer_id = rocprofiler_buffer_id_t{0}) {
std::lock_guard<std::mutex> lock(writing_lock);
if (tracer_record.timestamps.end.value <= 0 && tracer_record.domain != ACTIVITY_DOMAIN_ROCTX)
return;
const char* operation_name_c = nullptr;
// ROCTX domain Operation ID doesn't have a name
// It depends on the user input of the roctx functions.
// ROCTX message is the tracer_record.name
if (tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
CHECK_ROCPROFILER(rocprofiler_query_tracer_operation_name(
tracer_record.domain, tracer_record.operation_id, &operation_name_c));
}
output_file_t* output_file = get_output_file(output_type_t::TRACER, tracer_record.domain);
*output_file << "Domain(" << GetDomainName(tracer_record.domain) << "), ";
if (operation_name_c)
*output_file << ((tracer_record.domain == ACTIVITY_DOMAIN_HIP_API ||
tracer_record.domain == ACTIVITY_DOMAIN_HSA_API)
? "Function("
: "Operation_Name(")
<< operation_name_c << "), ";
if (tracer_record.name && tracer_record.domain != ACTIVITY_DOMAIN_ROCTX)
*output_file << "Kernel_Name("
<< rocprofiler::truncate_name(rocprofiler::cxx_demangle(tracer_record.name))
<< "), ";
if (tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
*output_file << "Start_Timestamp(" << tracer_record.timestamps.begin.value << "), "
<< "End_Timestamp(" << tracer_record.timestamps.end.value << "), "
<< "Correlation_ID(" << tracer_record.correlation_id.value << ")";
} else {
*output_file << "Timestamp(" << tracer_record.timestamps.begin.value << "), ";
}
if (tracer_record.domain == ACTIVITY_DOMAIN_ROCTX && tracer_record.external_id.id >= 0)
*output_file << "ROCTX_ID(" << tracer_record.external_id.id << "), ";
if (tracer_record.name && tracer_record.domain == ACTIVITY_DOMAIN_ROCTX)
*output_file << "ROCTX_Message(" << tracer_record.name << ")";
*output_file << std::endl;
}
void FlushProfilerRecord(const rocprofiler_record_profiler_t* profiler_record,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
auto check_filter_cache = [&](const char * k_name) {
auto str = std::string{k_name};
auto f = kernel_filter_cache_.find(k_name);
if (f == kernel_filter_cache_.end()) {
bool found_match = false;
for (const auto& filter : kernel_filters_) {
if (str.find(filter) != std::string::npos) {
found_match = true;
break;
}
}
f = kernel_filter_cache_.emplace(str, found_match).first;
}
return f->second;
};
std::lock_guard<std::mutex> lock(writing_lock);
size_t name_length = 0;
output_file_t* output_file{nullptr};
output_file = get_output_file(output_type_t::COUNTER);
CHECK_ROCPROFILER(rocprofiler_query_kernel_info_size(ROCPROFILER_KERNEL_NAME,
profiler_record->kernel_id, &name_length));
// Taken from rocprofiler: The size hasn't changed in recent past
static const uint32_t lds_block_size = 128 * 4;
const char* kernel_name_c = nullptr;
if (name_length > 1) {
CHECK_ROCPROFILER(rocprofiler_query_kernel_info(ROCPROFILER_KERNEL_NAME,
profiler_record->kernel_id, &kernel_name_c));
if (kernel_name_c != nullptr && !kernel_filters_.empty() && !check_filter_cache(kernel_name_c)) {
free(const_cast<char*>(kernel_name_c));
return;
}
}
*output_file << "Dispatch_ID(" << std::to_string(profiler_record->header.id.handle) << "), "
<< "GPU_ID(" << std::to_string(profiler_record->gpu_id.handle) << "), "
<< "Queue_ID(" << std::to_string(profiler_record->queue_id.handle) << "), "
<< "Process_ID(" << std::to_string(GetPid()) << "), "
<< "Thread_ID(" << std::to_string(profiler_record->thread_id.value) << "), "
<< "Grid_Size(" << std::to_string(profiler_record->kernel_properties.grid_size)
<< "), "
<< "Workgroup_Size("
<< std::to_string(profiler_record->kernel_properties.workgroup_size) << "), "
<< "LDS_Per_Workgroup("
<< std::to_string(
((profiler_record->kernel_properties.lds_size + (lds_block_size - 1)) &
~(lds_block_size - 1)))
<< "), "
<< "Scratch_Per_Workitem("
<< std::to_string(profiler_record->kernel_properties.scratch_size) << "), "
<< "Arch_VGPR("
<< std::to_string(profiler_record->kernel_properties.arch_vgpr_count) << "), "
<< "Accum_VGPR("
<< std::to_string(profiler_record->kernel_properties.accum_vgpr_count) << "), "
<< "SGPR(" << std::to_string(profiler_record->kernel_properties.sgpr_count)
<< "), "
<< "Wave_Size(" << std::to_string(profiler_record->kernel_properties.wave_size);
std::string kernel_name = "";
if (name_length > 1) {
kernel_name = rocprofiler::truncate_name(rocprofiler::cxx_demangle(kernel_name_c));
std::string key = "\"";
std::size_t found = kernel_name.rfind(key);
while (found != std::string::npos) {
kernel_name.replace(found, key.length(), "'");
found = kernel_name.rfind(key, found - 1);
}
}
*output_file << "), "
<< "Kernel_Name(\"" << kernel_name << "\"), "
<< "Begin_Timestamp(" << std::to_string(profiler_record->timestamps.begin.value)
<< "), "
<< "End_Timestamp(" << std::to_string(profiler_record->timestamps.end.value)
<< "), "
<< "Correlation_ID(" << std::to_string(profiler_record->correlation_id.value)
<< ")";
// For Counters
if (profiler_record->counters) {
*output_file << ", ";
for (uint64_t i = 0; i < profiler_record->counters_count.value; i++) {
if (profiler_record->counters[i].counter_handler.handle > 0) {
size_t counter_name_length = 0;
CHECK_ROCPROFILER(rocprofiler_query_counter_info_size(
session_id, ROCPROFILER_COUNTER_NAME, profiler_record->counters[i].counter_handler,
&counter_name_length));
if (counter_name_length > 1) {
const char* name_c = nullptr;
CHECK_ROCPROFILER(rocprofiler_query_counter_info(
session_id, ROCPROFILER_COUNTER_NAME, profiler_record->counters[i].counter_handler,
&name_c));
*output_file << name_c << "("
<< std::to_string(profiler_record->counters[i].value.value) << ")";
if (i < profiler_record->counters_count.value - 1) *output_file << ", ";
}
}
}
}
*output_file << std::endl;
if (kernel_name_c) {
free(const_cast<char*>(kernel_name_c));
}
}
void FlushPCSamplingRecord(const rocprofiler_record_pc_sample_t* pc_sampling_record) {
output_file_t* output_file{nullptr};
output_file = get_output_file(output_type_t::PC_SAMPLING);
const auto& sample = pc_sampling_record->pc_sample;
*output_file << "dispatch[" << sample.dispatch_id.value << "], "
<< "timestamp(" << sample.timestamp.value << "), "
<< "gpu_id(" << sample.gpu_id.handle << "), "
<< "pc-sample(" << std::hex << std::showbase << sample.pc << "), "
<< "se(" << sample.se << ')' << std::endl;
}
int WriteBufferRecords(const rocprofiler_record_header_t* begin,
const rocprofiler_record_header_t* end,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
while (begin < end) {
if (!begin) return 0;
switch (begin->kind) {
case ROCPROFILER_PROFILER_RECORD: {
const rocprofiler_record_profiler_t* profiler_record =
reinterpret_cast<const rocprofiler_record_profiler_t*>(begin);
FlushProfilerRecord(profiler_record, session_id, buffer_id);
break;
}
case ROCPROFILER_TRACER_RECORD: {
rocprofiler_record_tracer_t* tracer_record = const_cast<rocprofiler_record_tracer_t*>(
reinterpret_cast<const rocprofiler_record_tracer_t*>(begin));
FlushTracerRecord(*tracer_record, session_id, buffer_id);
break;
}
case ROCPROFILER_ATT_TRACER_RECORD: {
break;
}
case ROCPROFILER_PC_SAMPLING_RECORD: {
[[deprecated("PC Sampling is deprecated")]]
const rocprofiler_record_pc_sample_t* pc_sampling_record =
reinterpret_cast<const rocprofiler_record_pc_sample_t*>(begin);
FlushPCSamplingRecord(pc_sampling_record);
break;
}
default:
break;
}
rocprofiler_next_record(begin, &begin, session_id, buffer_id);
}
return 0;
}
bool is_valid() const { return valid_; }
private:
bool valid_{false};
std::set<std::string> kernel_filters_;
std::map<std::string, bool> kernel_filter_cache_;
std::atomic<bool> tracer_header_written_{false};
std::atomic<bool> profiler_header_written_{false};
output_file_t roctx_file_{"roctx_trace.txt"}, hsa_api_file_{"hsa_api_trace.txt"},
hip_api_file_{"hip_api_trace.txt"}, hip_activity_file_{"hcc_ops_trace.txt"},
hsa_async_copy_file_{"async_copy_trace.txt"}, pc_sample_file_{"pcs_trace.txt"},
output_file_{"results.txt"};
};
file_plugin_t* file_plugin = nullptr;
} // namespace
ROCPROFILER_EXPORT int rocprofiler_plugin_initialize(uint32_t rocprofiler_major_version,
uint32_t rocprofiler_minor_version,
void* data) {
if (rocprofiler_major_version != ROCPROFILER_VERSION_MAJOR ||
rocprofiler_minor_version < ROCPROFILER_VERSION_MINOR)
return -1;
if (file_plugin != nullptr) return -1;
file_plugin = new file_plugin_t();
if (file_plugin->is_valid()) return 0;
// The plugin failed to initialized, destroy it and return an error.
delete file_plugin;
file_plugin = nullptr;
return -1;
}
ROCPROFILER_EXPORT void rocprofiler_plugin_finalize() {
if (!file_plugin) return;
delete file_plugin;
file_plugin = nullptr;
}
ROCPROFILER_EXPORT int rocprofiler_plugin_write_buffer_records(
const rocprofiler_record_header_t* begin, const rocprofiler_record_header_t* end,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
if (!file_plugin || !file_plugin->is_valid()) return -1;
return file_plugin->WriteBufferRecords(begin, end, session_id, buffer_id);
}
ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(rocprofiler_record_tracer_t record) {
if (!file_plugin || !file_plugin->is_valid()) return -1;
if (record.header.id.handle == 0) return 0;
file_plugin->FlushTracerRecord(record, rocprofiler_session_id_t{0}, rocprofiler_buffer_id_t{0});
return 0;
}
@@ -0,0 +1 @@
README.html
@@ -0,0 +1,177 @@
################################################################################
# Copyright (c) 2022 Advanced Micro Devices, Inc.
#
# 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 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.
################################################################################
# Plugin shared object.
add_library(
ctf_plugin SHARED
ctf.cpp
plugin.cpp
barectf.c
"${CMAKE_CURRENT_BINARY_DIR}/barectf.h"
${PROJECT_SOURCE_DIR}/src/utils/helper.cpp
hsa_begin.cpp.i
hsa_end.cpp.i
hip_begin.cpp.i
hip_end.cpp.i)
set_target_properties(
ctf_plugin
PROPERTIES CXX_VISIBILITY_PRESET hidden
LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/../exportmap"
LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib/rocprofiler"
INSTALL_RPATH "${ROCM_APPEND_PRIVLIB_RPATH}")
set(METADATA_STREAM_FILE_DIR "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/plugin/ctf")
target_compile_definitions(
ctf_plugin
PUBLIC AMD_INTERNAL_BUILD
PRIVATE HIP_PROF_HIP_API_STRING=1 __HIP_PLATFORM_AMD__=1
CTF_PLUGIN_METADATA_FILE_PATH="${METADATA_STREAM_FILE_DIR}/metadata"
CTF_PLUGIN_INSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}")
# Disabling this as it causes CI to fail, we need to make sure rocm-core is installed
# before rocprofiler Add definitions for getROCmInstallPath() API
# if(USE_GET_ROCM_PATH_API) target_compile_definitions( ctf_plugin PRIVATE
# USE_GET_ROCM_PATH_API=1) endif()
target_include_directories(
ctf_plugin PRIVATE "${PROJECT_SOURCE_DIR}" "${CMAKE_BINARY_DIR}/src/api"
"${CMAKE_CURRENT_BINARY_DIR}")
target_link_options(
ctf_plugin PRIVATE "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exportmap"
-Wl,--no-undefined)
target_link_libraries(ctf_plugin PRIVATE rocprofiler-v2 hsa-runtime64::hsa-runtime64
stdc++fs dl)
# Disabling this as it causes CI to fail, we need to make sure rocm-core is installed
# before rocprofiler Add link dependencies for getROCmInstallPath() API
# if(USE_GET_ROCM_PATH_API) find_library(ROCM_CORE_LIB rocm-core HINTS ${ROCM_PATH}/lib)
# target_link_libraries(ctf_plugin PRIVATE ${ROCM_CORE_LIB}) endif()
install(TARGETS ctf_plugin LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}"
COMPONENT plugins)
# `gen_api_files.py` and `gen_env_yaml.py` require Python 3, CppHeaderParser, PyYAML, and
# barectf.
if(NOT Python3_EXECUTABLE)
find_package(
Python3
COMPONENTS Interpreter
REQUIRED)
endif()
message("Python: ${Python3_EXECUTABLE})")
execute_process(COMMAND Python3::Interpreter -c "print('hello')")
function(check_py3_pkg pkg_name)
execute_process(
COMMAND "${Python3_EXECUTABLE}" -c "import ${pkg_name}"
RESULT_VARIABLE PY3_IMPORT_RES
OUTPUT_QUIET)
if(NOT (${PY3_IMPORT_RES} EQUAL 0))
message(FATAL_ERROR "Cannot find Python 3 package `${pkg_name}`")
endif()
message(STATUS "Found Python 3 package `${pkg_name}`")
endfunction()
check_py3_pkg(CppHeaderParser)
check_py3_pkg(yaml)
find_program(BARECTF_RES barectf REQUIRED HINTS "$ENV{HOME}/.local/bin")
# Generate barectf YAML and C++ files for HSA API.
get_property(
HSA_RUNTIME_INCLUDE_DIRS
TARGET hsa-runtime64::hsa-runtime64
PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
find_file(
HSA_H hsa.h
PATHS ${HSA_RUNTIME_INCLUDE_DIRS}
PATH_SUFFIXES hsa
NO_DEFAULT_PATH REQUIRED)
get_filename_component(HSA_RUNTIME_INC_PATH "${HSA_H}" DIRECTORY)
add_custom_command(
OUTPUT hsa_erts.yaml hsa_begin.cpp.i hsa_end.cpp.i
COMMAND ${CMAKE_C_COMPILER} -E "${HSA_RUNTIME_INC_PATH}/hsa.h" -o hsa.h.i
COMMAND ${CMAKE_C_COMPILER} -E "${HSA_RUNTIME_INC_PATH}/hsa_ext_amd.h" -o
hsa_ext_amd.h.i
COMMAND ${CMAKE_COMMAND} -E cat hsa.h.i hsa_ext_amd.h.i
"${CMAKE_BINARY_DIR}/src/api/hsa_prof_str.h" > hsa_input.h
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/gen_api_files.py" hsa
hsa_input.h
BYPRODUCTS hsa.h.i hsa_ext_amd.h.i hsa_input.h
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/gen_api_files.py" "${HSA_RUNTIME_INC_PATH}/hsa.h"
"${HSA_RUNTIME_INC_PATH}/hsa_ext_amd.h"
"${CMAKE_BINARY_DIR}/src/api/hsa_prof_str.h"
COMMENT "Generating HSA API files for the `ctf` plugin...")
# Generate barectf YAML and C++ files for HIP API.
get_property(
HIP_INCLUDE_DIRS
TARGET hip::amdhip64
PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
find_file(
HIP_RUNTIME_API_H hip_runtime_api.h
PATHS ${HIP_INCLUDE_DIRS}
PATH_SUFFIXES hip
NO_DEFAULT_PATH REQUIRED)
find_file(
HIP_PROF_STR_H hip_prof_str.h
PATHS ${HIP_INCLUDE_DIRS}
PATH_SUFFIXES hip hip/amd_detail
NO_DEFAULT_PATH REQUIRED)
list(TRANSFORM HIP_INCLUDE_DIRS PREPEND -I)
add_custom_command(
OUTPUT hip_erts.yaml hip_begin.cpp.i hip_end.cpp.i
COMMAND
${CMAKE_C_COMPILER} ${HIP_INCLUDE_DIRS} -E
"${PROJECT_SOURCE_DIR}/src/core/session/tracer/src/hip_full_api.h"
-D__HIP_PLATFORM_AMD__=1 -D__HIP_ROCclr__=1 -o hip_runtime_api.h.i
COMMAND cat hip_runtime_api.h.i "${HIP_PROF_STR_H}" > hip_input.h
BYPRODUCTS hip_runtime_api.h.i hip_input.h
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/gen_api_files.py" hip
hip_input.h
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/gen_api_files.py"
"${PROJECT_SOURCE_DIR}/src/core/session/tracer/src/hip_full_api.h"
"${HIP_PROF_STR_H}"
COMMENT "Generating HIP API files for the `ctf` plugin...")
# Generate `env.yaml` (trace environment for barectf).
add_custom_command(
OUTPUT env.yaml
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/gen_env_yaml.py"
${PROJECT_VERSION}
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/gen_env_yaml.py"
COMMENT "Generating `env.yaml`...")
# Generate raw CTF tracer with barectf.
add_custom_command(
OUTPUT barectf.c barectf.h barectf-bitfield.h metadata
COMMAND "${BARECTF_RES}" gen "-I${CMAKE_CURRENT_BINARY_DIR}"
"-I${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/config.yaml"
DEPENDS hsa_erts.yaml hip_erts.yaml env.yaml "${CMAKE_CURRENT_SOURCE_DIR}/config.yaml"
"${CMAKE_CURRENT_SOURCE_DIR}/dst_base.yaml"
COMMENT "Generating raw CTF tracer with barectf...")
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/metadata"
DESTINATION "${METADATA_STREAM_FILE_DIR}"
COMPONENT plugins)
+260
View File
@@ -0,0 +1,260 @@
= CTF plugin for ROCProfiler
13 December 2022
Philippe Proulx
This plugin writes the received ROCProfiler tracer and profiler records to
a https://diamon.org/ctf/[CTF] trace.
== Build requirements
* Python ≥ 3.10
* barectf ≥ 3.1.1 (`pip3 install barectf`)
* PyYAML (`apt-get install python3-yaml`)
* CppHeaderParser (`pip3 install CppHeaderParser`)
== Usage
Once installed, you may load this plugin with `rocprofv2` using
the `--plugin ctf` command-line arguments.
This plugin honours the `OUTPUT_PATH` environment variable which
`rocprofv2` sets with the `-d` option. If you pass `-d my-dir` to
`rocprofv2`, then the plugin will write the CTF trace to the
`my-dir/trace` directory.
IMPORTANT: This plugin performs important cleanup tasks at finalization
time, so the resulting CTF trace could be corrupted if the plugin is
never finalized.
Once the plugin is finalized, open the resulting trace directory with
either https://babeltrace.org/[Babeltrace{nbsp}2] or
https://www.eclipse.org/tracecompass/[Trace Compass] to view or analyze
it.
=== Event record types
This plugin writes to different CTF data streams having different types.
On the file system, the prefix of a data stream file name indicates the
data stream type, that is:
`roctx_`::
rocTX messages.
+
Each CTF event record is named `roctx` and corresponds to a rocTX
tracer record.
+
The fields are:
+
--
[horizontal]
`thread_id`::
Thread ID.
`id`::
rocTX ID.
`msg`::
rocTX message.
--
`hsa_api_`::
HSA API beginning and end function calls.
+
All CTF event records have the following common fields:
+
--
[horizontal]
`thread_id`::
Thread ID.
`queue_id`::
Queue ID.
`agent_id`::
Agent ID.
`correlation_id`::
Correlation ID.
--
+
For each ROCProfiler HSA API tracer record for the HSA function named
`__name__`, this plugin writes two event records:
+
`__name___begin`:::
Beginning of the function call.
+
The event record contains fields which correspond to most of the
parameters of the HSA function.
`__name___end`:::
End of the function call.
`hip_api_`::
HIP API beginning and end function calls.
+
All CTF event records have the following common fields:
+
--
[horizontal]
`thread_id`::
Thread ID.
`queue_id`::
Queue ID.
`agent_id`::
Agent ID.
`correlation_id`::
Correlation ID.
`kernel_name`::
Kernel name (empty string if not available).
--
+
For each ROCProfiler HIP API tracer record for the HIP function named
`__name__`, this plugin writes two event records:
+
`__name__Begin`:::
Beginning of the function call.
+
The event record contains fields which correspond to most of the
parameters of the HIP function.
`__name__End`:::
End of the function call.
`api_ops_`::
HSA/HIP API beginning and end operations.
+
All CTF event records have the following common fields:
+
--
[horizontal]
`thread_id`::
Thread ID.
`queue_id`::
Queue ID.
`agent_id`::
Agent ID.
`correlation_id`::
Correlation ID.
--
+
The possible CTF event records are:
+
`hsa_op_begin`:::
HSA API operation beginning.
`hsa_op_end`:::
HSA API operation end.
`hip_op_begin`:::
HIP API operation beginning.
+
Such an event record also has the field `kernel_name` which is the
kernel name (empty string if not available).
`hip_op_end`:::
HIP API operation end.
`profiler_`::
Profiler records.
+
All CTF event records have the following common fields:
+
--
[horizontal]
`dispatch`::
Dispatch ID.
`gpu_id`::
GPU ID.
`queue_id`::
Queue ID.
`queue_index`::
Queue index.
`process_id`::
Process ID.
`thread_id`::
Thread ID.
`kernel_id`::
Kernel ID.
`kernel_name`::
Kernel name (empty string if not available).
`counter_names`::
Array of counter names, each one having a corresponding integral
value in the `counter_values` field.
`counter_values`::
Array of integers, each one being the value of a counter of which
the name is a corresponding string in the `counter_names` field.
--
+
The possible CTF event records are:
+
`profiler_record`:::
Profiler record.
`profiler_record_with_kernel_properties`:::
Profiler record with kernel properties.
+
Such an event record also has the following fields:
+
--
`grid_size`::
Grid size.
`workgroup_size`::
Workgroup size.
`lds_size`::
Local memory size.
`scratch_size`::
Scratch size.
`arch_vgpr_count`::
Architecture vector general purpose register count.
`accum_vgpr_count`::
Accum. vector general purpose register count
`sgpr_count`::
Scalar general purpose register count.
`wave_size`::
Wavefront size.
`signal_handle`::
Signal handle.
--
`hsa_handles_`::
HSA handle type mappings.
+
Each CTF event record is named `hsa_handle_type` and maps an HSA handle
to a processor unit type (CPU or GPU).
+
The clock value of those event records is irrelevant (always{nbsp}0).
+
The fields are:
+
--
[horizontal]
`handle`::
HSA handle.
`type`::
Processor unit type (`CPU` or `GPU` enumeration label).
--
@@ -0,0 +1,67 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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 PLUGIN_CTF_BARECTF_EVENT_RECORD_H
#define PLUGIN_CTF_BARECTF_EVENT_RECORD_H
#include <memory>
#include <cstdint>
struct barectf_default_ctx;
namespace rocm_ctf {
// Abstract base class of any barectf event record.
//
// A concrete event record class must implement Write() which must call
// a corresponding barectf tracing function.
//
// `CtxT` is the specific type of the barectf context which Write()
// receives.
template <typename CtxT> class BarectfEventRecord {
protected:
// Builds a barectf event record having the clock value `clock_val`.
explicit BarectfEventRecord(const std::uint64_t clock_val) noexcept : clock_val_{clock_val} {}
public:
// Shared pointer to const barectf event record.
using SP = std::shared_ptr<const BarectfEventRecord>;
virtual ~BarectfEventRecord() = default;
// Disabled copy operations to make this class simpler.
BarectfEventRecord(const BarectfEventRecord&) = delete;
BarectfEventRecord& operator=(const BarectfEventRecord&) = delete;
// Clock value of this event record.
std::uint64_t GetClockVal() const noexcept { return clock_val_; }
// Calls a corresponding barectf tracing function using the barectf
// context `barectf_ctx`.
virtual void Write(CtxT& barectf_ctx) const = 0;
private:
// Clock value.
std::uint64_t clock_val_;
};
} // namespace rocm_ctf
#endif // PLUGIN_CTF_BARECTF_EVENT_RECORD_H
@@ -0,0 +1,192 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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 PLUGIN_CTF_BARECTF_PLATFORM_H
#define PLUGIN_CTF_BARECTF_PLATFORM_H
#include <cstdlib>
#include <cstdint>
#include <fstream>
#include <vector>
#include <functional>
#include "src/utils/filesystem.hpp"
#include "barectf.h"
namespace rocm_ctf {
template <typename> class BarectfWriter;
// A barectf platform for any barectf writer.
//
// The user doesn't deal directly with such an object: it's closely
// coupled with a barectf writer.
//
// Each platform takes care of a single CTF data stream file.
//
// After building such a platform, get the raw barectf context with
// GetCtx() to call tracing functions. The platform must still exist
// when calling a tracing function.
//
// Such a platform opens the data stream file on construction and closes
// it on destruction.
//
// `DescrT` is the specific barectf platform descriptor. It must be a
// structure having:
//
// `Ctx`:
// Specific barectf context type.
//
// `static void OpenPacket(Ctx&)`:
// Packet opening function.
//
// `static void ClosePacket(Ctx&)`:
// Packet closing function.
template <typename DescrT> class BarectfPlatform final {
friend class BarectfWriter<DescrT>;
private:
// Builds a barectf platform.
//
// The platform writes CTF packets of size `packet_size` bytes to the
// CTF data stream file `data_stream_file_path`.
//
// For each event record to write, the platform reads `clock_val` to
// know the current timestamp.
explicit BarectfPlatform(const std::size_t packet_size,
const rocprofiler::common::filesystem::path& data_stream_file_path,
const std::uint64_t& clock_val)
: clock_val_{&clock_val}, buffer_(packet_size) {
// Initialize barectf callbacks.
barectf_platform_callbacks callbacks;
callbacks.default_clock_get_value = GetClockCb;
callbacks.is_backend_full = IsBackendFullCb;
callbacks.open_packet = OpenPacketCb;
callbacks.close_packet = ClosePacketCb;
// Configure exceptions so that stream operations throw instead of
// just setting flags on error.
output_.exceptions(std::ofstream::failbit | std::ofstream::badbit);
// Open CTF data stream output file in binary mode.
output_.open(data_stream_file_path, std::ios_base::out | std::ios_base::binary);
// Initialize the raw barectf context.
barectf_init(&ctx_, buffer_.data(), buffer_.size(), callbacks, this);
// Open the initial packet.
OpenPacketCb();
}
public:
// Disabled copy operations to make this class simpler.
BarectfPlatform(const BarectfPlatform&) = delete;
BarectfPlatform& operator=(const BarectfPlatform&) = delete;
// Closes/writes any last CTF packet and closes the data stream file.
~BarectfPlatform() {
if (barectf_packet_is_open(&ctx_) && !barectf_packet_is_empty(&ctx_)) {
// Close and write last CTF packet (not empty).
ClosePacketCb();
}
// Close data stream output file.
output_.close();
}
// Returns the raw barectf context of this platform.
const typename DescrT::Ctx& GetCtx() const noexcept { return ctx_; }
typename DescrT::Ctx& GetCtx() noexcept { return ctx_; }
private:
static BarectfPlatform& AsPlatform(void* const data) noexcept {
return *static_cast<BarectfPlatform*>(data);
}
// Four callbacks for barectf.
//
// Those four functions receive an instance of this class as `data`.
static std::uint64_t GetClockCb(void* const data) noexcept {
// Forward to instance method.
return AsPlatform(data).GetClockCb();
}
static int IsBackendFullCb(void* const data) noexcept {
// Forward to instance method.
return AsPlatform(data).IsBackendFullCb();
}
static void OpenPacketCb(void* const data) {
// Forward to instance method.
AsPlatform(data).OpenPacketCb();
}
static void ClosePacketCb(void* const data) {
// Forward to instance method.
AsPlatform(data).ClosePacketCb();
}
// Instance version of the "get clock value" callback.
std::uint64_t GetClockCb() noexcept { return *clock_val_; }
// Instance version of the "is the back end full?" callback.
int IsBackendFullCb() noexcept {
// Never full.
return 0;
}
// Instance version of the "open packet" callback.
void OpenPacketCb() {
// Forward to user (descriptor) function.
DescrT::OpenPacket(ctx_);
}
// Instance version of the "close packet" callback.
void ClosePacketCb() {
// Forward to user (descriptor) function to finalize the packet.
DescrT::ClosePacket(ctx_);
// Write to the data stream file.
WriteCurrentPacket();
}
// Writes the current CTF packet (`buffer_`) to the data stream file.
void WriteCurrentPacket() {
output_.write(reinterpret_cast<const char*>(buffer_.data()), buffer_.size());
}
// Clock value pointer.
const std::uint64_t* clock_val_;
// CTF data stream output file stream.
std::ofstream output_;
// Raw barectf context.
typename DescrT::Ctx ctx_;
// CTF packet buffer.
std::vector<std::uint8_t> buffer_;
};
} // namespace rocm_ctf
#endif // PLUGIN_CTF_BARECTF_PLATFORM_H
@@ -0,0 +1,124 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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 PLUGIN_CTF_BARECTF_TRACER_H
#define PLUGIN_CTF_BARECTF_TRACER_H
#include <cstdlib>
#include <memory>
#include <vector>
#include <string>
#include "src/utils/filesystem.hpp"
#include "barectf_event_record.h"
#include "barectf_writer.h"
namespace rocm_ctf {
// A barectf tracer offers the AddEventRecord() method to add an event
// record which it will ultimately write to some CTF data stream file
// within some specified CTF trace directory.
//
// One important feature of such a tracer is that you don't need to add
// event records in order of time. A barectf tracer manages one or more
// barectf writers, each one managing a single barectf platform/context
// (CTF data stream file).
//
// All the CTF data stream files which a barectf tracer indirectly
// manages share a common specified prefix. You must not use the same
// prefix for two barectf tracers writing to the same CTF trace
// directory.
//
// `PlatformDescrT` is the specific barectf platform descriptor (see the
// documentation of the `BarectfPlatform` class template).
template <typename PlatformDescrT> class BarectfTracer final {
public:
// Specific barectf event record type.
using EventRecord = typename BarectfWriter<PlatformDescrT>::EventRecord;
// Builds a barectf tracer to write CTF packets of size `packet_size`
// bytes to CTF data stream files having the prefix
// `data_stream_file_name_prefix` within the CTF trace directory
// `trace_dir`.
//
// The internal barectf writers manage event record queues having a
// maximum size of `max_writer_queue_size`. Increasing
// `max_writer_queue_size` increases the memory footprint of the
// tracer, but may reduce the number of required CTF data stream files
// to ensure time-ordered event records.
explicit BarectfTracer(const std::size_t packet_size,
rocprofiler::common::filesystem::path trace_dir,
const char* const data_stream_file_name_prefix,
const std::size_t max_writer_queue_size = 200)
: packet_size_{packet_size},
trace_dir_{std::move(trace_dir)},
data_stream_file_name_prefix_{data_stream_file_name_prefix},
max_writer_queue_size_{max_writer_queue_size} {}
// Disabled copy operations to make this class simpler.
BarectfTracer(const BarectfTracer&) = delete;
BarectfTracer& operator=(const BarectfTracer&) = delete;
// Adds the event record `event_record` to this tracer.
//
// The clock value of `event_record` may be less than the clock value
// of previously added event records.
void AddEventRecord(typename EventRecord::SP event_record) {
// Try to find a barectf writer to accept `event_record`.
for (auto& writer : writers_) {
if (writer->MayAddEventRecord(*event_record)) {
// Found: add the event record to this writer and return.
writer->AddEventRecord(std::move(event_record));
return;
}
}
// No barectf writer found: create a new one.
std::ostringstream ss;
ss << data_stream_file_name_prefix_ << writers_.size();
writers_.emplace_back(new BarectfWriter<PlatformDescrT>{packet_size_, trace_dir_ / ss.str(),
max_writer_queue_size_});
// Add the event record to this new barectf writer.
assert(writers_.back()->MayAddEventRecord(*event_record));
writers_.back()->AddEventRecord(std::move(event_record));
}
private:
// CTF packet size.
std::size_t packet_size_;
// CTF trace directory.
rocprofiler::common::filesystem::path trace_dir_;
// CTF data stream file name prefix.
std::string data_stream_file_name_prefix_;
// Maximum event record queue size of a barectf writer.
std::size_t max_writer_queue_size_;
// barectf writers.
std::vector<std::unique_ptr<BarectfWriter<PlatformDescrT>>> writers_;
};
} // namespace rocm_ctf
#endif // PLUGIN_CTF_BARECTF_TRACER_H
@@ -0,0 +1,178 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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 PLUGIN_CTF_BARECTF_WRITER_H
#define PLUGIN_CTF_BARECTF_WRITER_H
#include <cassert>
#include <cstdlib>
#include <cstdint>
#include <cassert>
#include <queue>
#include <utility>
#include "src/utils/filesystem.hpp"
#include "barectf_platform.h"
#include "barectf_event_record.h"
namespace rocm_ctf {
template <typename> class BarectfTracer;
// A barectf writer manages a queue of event records, writing them
// through barectf when needed.
//
// Such an object makes it possible to add some event record with a
// clock value V and then some other event record of which the clock
// value is less than V. The barectf writer ensures that actual barectf
// tracing functions are called chronologically, a requirement of CTF.
//
// A barectf writer keeps event records in memory until its queue is
// full (you provide the maximum queue size at construction time), in
// which case it writes the oldest event record to some current CTF
// packet through a barectf tracing function.
//
// Call MayAddEventRecord() to check whether or not you may add an event
// record to the barectf writer, and then AddEventRecord() if you may.
//
// A barectf writer writes all its remaining event records on
// destruction.
//
// `PlatformDescrT` is the specific barectf platform descriptor (see the
// documentation of the `BarectfPlatform` class template).
template <typename PlatformDescrT> class BarectfWriter final {
friend class BarectfTracer<PlatformDescrT>;
public:
// Specific barectf event record type.
using EventRecord = BarectfEventRecord<typename PlatformDescrT::Ctx>;
private:
// Builds a barectf writer to write CTF packets of size `packet_size`
// bytes to the CTF data stream file `data_stream_file_path`.
//
// The built barectf writer manages an event record queue having a
// maximum size of `max_queue_size`.
explicit BarectfWriter(const std::size_t packet_size,
const rocprofiler::common::filesystem::path& data_stream_file_path,
const std::size_t max_queue_size)
: platform_{packet_size, data_stream_file_path, clock_val_},
max_queue_size_{max_queue_size} {}
public:
// Writes all its remaining event records.
~BarectfWriter() {
// Write all the remaining event records from the oldest to the
// newest.
while (!queue_.empty()) {
WriteOldestEventRecord();
}
}
// Disabled copy operations to make this class simpler.
BarectfWriter(const BarectfWriter&) = delete;
BarectfWriter& operator=(const BarectfWriter&) = delete;
// Whether or not you may add the event record `event_record` to this
// writer with AddEventRecord().
bool MayAddEventRecord(const EventRecord& event_record) const noexcept {
if (queue_.empty()) {
return true;
}
// One may only add an event record if its clock value is greater
// than or equal to the clock value of the most recently written
// event record.
return event_record.GetClockVal() >= clock_val_;
}
// Adds the event record `event_record` to this writer.
//
// `MayAddEventRecord(*event_record)` must return `true`.
void AddEventRecord(typename EventRecord::SP event_record) {
assert(MayAddEventRecord(*event_record) && "May add event record");
// Add event record to queue.
queue_.emplace(std::move(event_record));
if (queue_.size() > max_queue_size_) {
// Queue is too large: write the oldest event record now to
// satisfy the requirement.
WriteOldestEventRecord();
}
}
private:
// Comparison type for `queue_`.
struct EventRecordQueueCompare final {
bool operator()(const typename EventRecord::SP& left,
const typename EventRecord::SP& right) const noexcept {
// "Greater than" so that the top element of the queue is the
// oldest event record.
return left->GetClockVal() > right->GetClockVal();
}
};
// Oldest event record within `queue_`.
//
// `queue_` must not be empty.
const EventRecord& GetOldestEventRecord() const noexcept {
assert(!queue_.empty() && "Queue isn't empty");
return *queue_.top();
}
// Writes the oldest event record through a barectf tracing function
// and removes it from the event record queue.
void WriteOldestEventRecord() {
auto& oldest_event_record = GetOldestEventRecord();
// When calling a barectf tracing function, it calls the clock value
// accessor callback of the platform, which itself reads from
// `clock_val_`.
clock_val_ = oldest_event_record.GetClockVal();
// Forward to a barectf tracing function.
oldest_event_record.Write(platform_.GetCtx());
// Remove from queue.
queue_.pop();
}
// barectf platform (manages file I/O).
BarectfPlatform<PlatformDescrT> platform_;
// Current clock value for `platform_`.
//
// This is also the clock value of the most recently written event
// record, therefore that MayAddEventRecord() can rely on this.
std::uint64_t clock_val_ = 0;
// Maximum size of `queue_` below.
std::size_t max_queue_size_;
// Event record queue.
std::priority_queue<typename EventRecord::SP, std::vector<typename EventRecord::SP>,
EventRecordQueueCompare>
queue_;
};
} // namespace rocm_ctf
#endif // PLUGIN_CTF_BARECTF_WRITER_H
+165
View File
@@ -0,0 +1,165 @@
################################################################################
# Copyright (c) 2022 Advanced Micro Devices, Inc.
#
# 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 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.
################################################################################
%YAML 1.2
--- !<tag:barectf.org,2020/3/config>
trace:
$include:
# Environment (generated file).
- env.yaml
type:
$include:
- stdint.yaml
- stdmisc.yaml
native-byte-order: little-endian
clock-types:
default:
origin-is-unix-epoch: true
$c-type: uint64_t
data-stream-types:
hsa_api:
event-record-common-context-field-type:
class: struct
members:
- _thread_id: uint32
- _queue_id: uint32
- _agent_id: uint32
- _correlation_id: uint64
$include:
# Base.
- dst_base.yaml
# HSA API event record types (generated file).
- hsa_erts.yaml
hip_api:
event-record-common-context-field-type:
class: struct
members:
- _thread_id: uint32
- _queue_id: uint32
- _agent_id: uint32
- _correlation_id: uint64
- _kernel_name: str
$include:
# Base.
- dst_base.yaml
# HIP API event record types (generated file).
- hip_erts.yaml
roctx:
$include:
# Base
- dst_base.yaml
event-record-common-context-field-type:
class: struct
members:
- _thread_id: uint32
event-record-types:
roctx:
payload-field-type:
class: struct
members:
- _id: sint64
- _msg: str
hsa_handles:
$include:
# Base.
- dst_base.yaml
event-record-types:
hsa_handle_type:
payload-field-type:
class: struct
members:
- _handle: uint64
- _type:
field-type:
class: uenum
size: 8
mappings:
CPU: [0]
GPU: [1]
api_ops:
$include:
# Base.
- dst_base.yaml
event-record-common-context-field-type:
class: struct
members:
- _thread_id: uint32
- _queue_id: uint32
- _agent_id: uint32
- _correlation_id: uint64
event-record-types:
hsa_op_begin:
payload-field-type:
class: struct
hsa_op_end:
payload-field-type:
class: struct
hip_op_begin:
payload-field-type:
class: struct
members:
- _kernel_name: str
hip_op_end:
payload-field-type:
class: struct
profiler:
$include:
# Base.
- dst_base.yaml
event-record-common-context-field-type:
class: struct
members:
- _dispatch: uint64
- _gpu_id: uint64
- _queue_id: uint64
- _queue_index: uint64
- _process_id: uint32
- _thread_id: uint32
- _kernel_id: uint64
- _kernel_name: str
- _counter_names:
field-type:
class: dynamic-array
element-field-type: str
- _counter_values:
field-type:
class: dynamic-array
element-field-type: uint64
event-record-types:
profiler_record:
payload-field-type:
class: struct
profiler_record_with_kernel_properties:
payload-field-type:
class: struct
members:
- _grid_size: uint64
- _workgroup_size: uint64
- _lds_size: uint64
- _scratch_size: uint64
- _arch_vgpr_count: uint64
- _accum_vgpr_count: uint64
- _sgpr_count: uint64
- _wave_size: uint64
- _signal_handle: uint64
+171
View File
@@ -0,0 +1,171 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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 <dlfcn.h>
#include <cassert>
#include <stdexcept>
#include <iostream>
#include <string>
#include <thread>
#include <link.h>
#include <chrono>
#include <regex>
#include <unistd.h>
#include "src/utils/filesystem.hpp"
#include <type_traits>
#include "rocprofiler.h"
#include "rocprofiler_plugin.h"
#include "plugin.h"
#ifdef USE_GET_ROCM_PATH_API
#include <rocm-core/rocm_getpath.h>
#endif
namespace fs = rocprofiler::common::filesystem;
namespace {
// Global plugin instance
rocm_ctf::Plugin* the_plugin = nullptr;
} // namespace
ROCPROFILER_EXPORT int rocprofiler_plugin_initialize(const uint32_t rocprofiler_major_version,
const uint32_t rocprofiler_minor_version,
void* data) {
if (rocprofiler_major_version != ROCPROFILER_VERSION_MAJOR ||
rocprofiler_minor_version < ROCPROFILER_VERSION_MINOR) {
return -1;
}
if (the_plugin) {
return -1;
}
auto output_dir = []() -> std::string {
if (const char* output_dir_internal = getenv("OUTPUT_PATH"); output_dir_internal != nullptr) {
return output_dir_internal;
}
return "./";
}();
auto output_file = []() -> std::string {
auto _v = getenv("OUTPUT_FILE");
return (_v) ? _v : "trace-{PID}";
}();
auto _replace = [&output_dir, &output_file](const char* _key, auto _value) {
using value_type = std::remove_cv_t<std::remove_reference_t<std::decay_t<decltype(_value)>>>;
auto _value_str = std::to_string(_value);
const auto _re = std::regex{_key, std::regex_constants::icase};
output_dir = std::regex_replace(output_dir, _re, _value_str);
output_file = std::regex_replace(output_file, _re, _value_str);
};
_replace("\\{PID\\}", getpid());
_replace("\\$ENV\\{PID\\}", getpid());
_replace("\\{PPID\\}", getppid());
_replace("\\$ENV\\{PPID\\}", getppid());
// Create the plugin instance.
#ifdef USE_GET_ROCM_PATH_API
char *installPath = nullptr;
unsigned int installPathLen = 0;
PathErrors_t retVal = PathSuccess;
auto metadata_path = std::string{CTF_PLUGIN_METADATA_FILE_PATH};
// Get ROCM install path
retVal = getROCmInstallPath( &installPath, &installPathLen );
if(PathSuccess == retVal){
metadata_path = fs::path(installPath) / fs::path{CTF_PLUGIN_METADATA_FILE_PATH};
}else {
std::cout << "Failed to get ROCm Install Path: " << retVal << std::endl;
}
// free allocated memory
if(nullptr != installPath) {
free(installPath);
}
#else
auto* this_plugin_handle = dlopen("libctf_plugin.so", RTLD_LAZY | RTLD_NOLOAD);
auto* librocprofiler_handle = dlopen("librocprofiler64.so", RTLD_LAZY | RTLD_NOLOAD);
auto metadata_path = std::string{CTF_PLUGIN_METADATA_FILE_PATH};
struct link_map* _link_map = nullptr;
if (this_plugin_handle && dlinfo(this_plugin_handle, RTLD_DI_LINKMAP, &_link_map) == 0) {
metadata_path = fs::path{_link_map->l_name}.parent_path() / fs::path{"../.."} /
CTF_PLUGIN_METADATA_FILE_PATH;
} else if (librocprofiler_handle &&
dlinfo(librocprofiler_handle, RTLD_DI_LINKMAP, &_link_map) == 0) {
metadata_path =
fs::path{_link_map->l_name}.parent_path() / ".." / CTF_PLUGIN_METADATA_FILE_PATH;
}
if (!fs::exists(metadata_path)) {
metadata_path = fs::path{CTF_PLUGIN_INSTALL_PREFIX} / CTF_PLUGIN_METADATA_FILE_PATH;
}
#endif // USE_GET_ROCM_PATH_API
try {
the_plugin = new rocm_ctf::Plugin{256 * 1024, fs::path{output_dir} / output_file,
fs::absolute(metadata_path)};
} catch (const std::exception& exc) {
std::cerr << "rocprofiler_plugin_initialize(): " << exc.what() << std::endl;
return -1;
}
return 0;
}
ROCPROFILER_EXPORT void rocprofiler_plugin_finalize() {
delete the_plugin;
the_plugin = nullptr;
}
ROCPROFILER_EXPORT int rocprofiler_plugin_write_buffer_records(
const rocprofiler_record_header_t* const begin, const rocprofiler_record_header_t* const end,
const rocprofiler_session_id_t session_id, const rocprofiler_buffer_id_t buffer_id) {
assert(the_plugin);
try {
the_plugin->HandleBufferRecords(begin, end, session_id, buffer_id);
} catch (const std::exception& exc) {
std::cerr << "rocprofiler_plugin_write_buffer_records(): " << exc.what() << std::endl;
return -1;
}
return 0;
}
ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(const rocprofiler_record_tracer_t record) {
assert(the_plugin);
if (record.header.id.handle == 0) {
return 0;
}
try {
the_plugin->HandleTracerRecord(record, rocprofiler_session_id_t{0});
} catch (const std::exception& exc) {
std::cerr << "rocprofiler_plugin_write_record(): " << exc.what() << std::endl;
return -1;
}
return 0;
}
@@ -0,0 +1,28 @@
################################################################################
# Copyright (c) 2022 Advanced Micro Devices, Inc.
#
# 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 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.
################################################################################
$default-clock-type-name: default
$features:
packet:
beginning-timestamp-field-type: false
discarded-event-records-counter-snapshot-field-type: false
end-timestamp-field-type: false
@@ -0,0 +1,718 @@
################################################################################
# Copyright (c) 2022 Advanced Micro Devices, Inc.
#
# 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 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.
################################################################################
import os
import os.path
import sys
import re
import yaml
import CppHeaderParser
# Numeric field type (abstract).
class _NumericFt:
# Returns the C++ expression to cast the expression `expr` to the C
# type of this field type.
def cast(self, expr):
return f"static_cast<{self.c_type}>({expr})"
# Integer field type (abstract).
class _IntFt(_NumericFt):
def __init__(self, size, pref_disp_base="dec"):
self._size = size
self._pref_disp_base = pref_disp_base
# Size (bits).
@property
def size(self):
return self._size
# Preferred display base (`dec` or `hex`).
@property
def pref_disp_base(self):
return self._pref_disp_base
# Equivalent barectf field type in YAML.
@property
def barectf_yaml(self):
return {
"size": self._size,
"preferred-display-base": self._pref_disp_base,
}
# Signed integer field type.
class _SIntFt(_IntFt):
# Equivalent barectf field type in YAML.
@property
def barectf_yaml(self):
ret = super().barectf_yaml
ret["class"] = "sint"
return ret
# Equivalent C type
@property
def c_type(self):
return f"std::int{self._size}_t"
# Unsigned integer field type.
class _UIntFt(_IntFt):
# Equivalent barectf field type in YAML.
@property
def barectf_yaml(self):
ret = super().barectf_yaml
ret["class"] = "uint"
return ret
# Equivalent C type.
@property
def c_type(self):
return f"std::uint{self._size}_t"
# Pointer field type.
class _PointerFt(_UIntFt):
def __init__(self):
super().__init__(64, "hex")
# Returns the C++ expression to cast the expression `expr` to the C
# type of this field type.
def cast(self, expr):
return f"static_cast<{self.c_type}>(reinterpret_cast<std::uintptr_t>({expr}))"
# Enumeration field type (abstract).
class _EnumFt(_IntFt):
def __init__(self, size, mappings):
super().__init__(size)
self._mappings = mappings.copy()
# Mappings (names to integers).
@property
def mappings(self):
return self._mappings
# Equivalent barectf field type in YAML.
@property
def barectf_yaml(self):
ret = super().barectf_yaml
mappings = {}
for name, val in self._mappings.items():
mappings[name] = [val]
ret["mappings"] = mappings
return ret
# Unsigned enumeration field type.
class _UEnumFt(_EnumFt, _UIntFt):
# Equivalent barectf field type in YAML.
@property
def barectf_yaml(self):
ret = super().barectf_yaml
ret["class"] = "uenum"
return ret
# Signed enumeration field type.
class _SEnumFt(_EnumFt, _UIntFt):
# Equivalent barectf field type in YAML.
@property
def barectf_yaml(self):
ret = super().barectf_yaml
ret["class"] = "senum"
return ret
# Optional string field type.
class _OptStrFt:
# Equivalent barectf field type in YAML.
@property
def barectf_yaml(self):
return {
"class": "str",
}
# String field type.
class _StrFt(_OptStrFt):
pass
# Floating-point number field type.
class _FloatFt(_NumericFt):
def __init__(self, size):
self._size = size
# Size (bits): 32 or 64.
@property
def size(self):
return self._size
# Equivalent barectf field type in YAML.
@property
def barectf_yaml(self):
return {
"class": "real",
"size": self._size,
}
# Equivalent C type.
@property
def c_type(self):
if self._size == 32:
return "float"
else:
assert self._size == 64
return "double"
# Event record type.
class _Ert:
def __init__(self, api_func_name, members):
self._api_func_name = api_func_name
self._members = members
# API function name
@property
def api_func_name(self):
return self._api_func_name
# Parameters of function (list of `_ErtMember`).
@property
def members(self):
return self._members
# Beginning event record type.
class _BeginErt(_Ert):
# Name of event record type depending on the API prefix.
def name(self, api_prefix):
suffix = "_begin" if api_prefix == "hsa" else "Begin"
return f"{self._api_func_name}{suffix}"
# End event record type.
class _EndErt(_Ert):
# Name of event record type depending on the API prefix.
def name(self, api_prefix):
suffix = "_end" if api_prefix == "hsa" else "End"
return f"{self._api_func_name}{suffix}"
# Event record type member.
class _ErtMember:
def __init__(self, access, member_names, ft):
self._access = access
self._member_names = member_names.copy()
self._ft = ft
# C++ access expression.
@property
def access(self):
return self._access
# List of member names.
@property
def member_names(self):
return self._member_names
# Equivalent field type.
@property
def ft(self):
return self._ft
# Makes sure some condition is satisfied, or prints the error message
# `error_msg` and quits with exit status 1 otherwise.
#
# This is an unconditional assertion.
def _make_sure(cond, error_msg):
if not cond:
print(f"Error: {error_msg}", file=sys.stderr)
sys.exit(1)
def _enumerator_effective_val(enum_val):
# Try the value, but this value may be a string (an
# enumerator/definition).
val = enum_val.get("value")
if type(val) is int:
return val
# Try the raw value.
val = enum_val.get("raw_value")
if val is not None:
if type(val) is int:
# Raw value is already an integer.
return val
else:
# Try to parse the raw value string as an integer.
try:
return int(val, 0)
except:
pass
_make_sure(False, f'Cannot get the integral value of enumerator `{enum_val["name"]}`')
# Returns the equivalent field type of the C type `c_type`.
def _number_ft_from_c_type(cpp_header, c_type):
# Check for known enumeration.
m = re.match(r"(?:enum\s+)?(\w+)", c_type)
if m:
size = 32
for enum_info in cpp_header.enums:
if m.group(1) == enum_info.get("name"):
# Fill enumeration field type mappings.
mappings = {
str(v["name"]): _enumerator_effective_val(v)
for v in enum_info["values"]
}
if len(mappings) == 0:
return _SIntFt(64)
if max(mappings.values()) >= 2**31 or min(mappings.values()) < -(
2**31
):
size = 64
_make_sure(
len(mappings) > 0, f'Enumeration `{enum_info["name"]}` is empty'
)
# Create corresponding enumeration field type.
return _SEnumFt(size, mappings)
# Find corresponding basic field type.
is_unsigned = "unsigned" in c_type
if "long" in c_type:
if is_unsigned:
return _UIntFt(64)
else:
return _SIntFt(64)
elif "short" in c_type:
if is_unsigned:
return _UIntFt(16)
else:
return _SIntFt(16)
elif "char" in c_type:
if is_unsigned:
return _UIntFt(8)
else:
return _SIntFt(8)
elif "float" in c_type:
return _FloatFt(32)
elif "double" in c_type:
return _FloatFt(64)
else:
# Assume `int` (often an unresolved C enumeration).
if is_unsigned:
return _UIntFt(32)
else:
return _SIntFt(32)
# Returns whether or not a property has a pointer type.
def _prop_is_pointer(prop, c_type):
if prop["pointer"] or prop["function_pointer"]:
return True
if prop["array"] and "array_size" in prop:
return True
if prop["unresolved"]:
# HSA API function pointers.
if prop["name"] in ("callback", "handler"):
return True
# HIP API function pointers.
if c_type.endswith("Fn_t"):
return True
# Check the C type itself.
if "*" in c_type or "*" in prop.get("raw_type", ""):
return True
return False
# Returns a list of event record type member objects for the structure
# `struct` considering the initial C++ access expression `access` and
# member names `member_names`.
def _get_ert_members_for_struct(cpp_header, struct, access, member_names):
members = []
member_names = member_names.copy()
member_names.append(None)
props = struct["properties"]["public"]
for index, prop in enumerate(props):
# Property name.
name = prop["name"]
# Member names, access, and C type.
member_names[-1] = str(name)
this_access = f"{access}.{name}"
c_type = prop["type"]
aliases = prop["aliases"]
# Skip no type.
if c_type == "":
continue
# Skip unnamed or union.
if name == "" or "union" in name or re.match(r"\bunion\b", c_type):
continue
# Check for known C type alias.
while True:
c_type_alias = cpp_header.typedefs.get(c_type)
if c_type_alias is None:
break
c_type = c_type_alias
# Check for C string.
if re.match(r"^((const\s+char)|(char\s+const)|char)\s*\*$", c_type.strip()):
members.append(_ErtMember(this_access, member_names, _OptStrFt()))
continue
# Check for pointer.
if _prop_is_pointer(prop, c_type):
# Pointer: use numeric value.
members.append(_ErtMember(this_access, member_names, _PointerFt()))
continue
# Check for substructure.
sub_struct = cpp_header.classes.get(c_type)
if sub_struct is None and len(aliases) == 1:
sub_struct = cpp_header.classes.get(aliases[0])
if sub_struct is not None:
members += _get_ert_members_for_struct(
cpp_header, sub_struct, this_access, member_names
)
continue
# Use a basic field type.
members.append(
_ErtMember(
this_access, member_names, _number_ft_from_c_type(cpp_header, c_type)
)
)
return members
# Returns the beginning and end event record type objects for the
# callback data structure `struct`.
def _erts_from_cb_data_struct(api_prefix, cpp_header, retval_info, struct):
# The location of the `args` union within the nested structures of
# `struct`.
args_nested_cls_index = 0
# Create return value members (to be used later).
if retval_info is not None:
args_nested_cls_index = 1
retval_members = {}
nested_classes = struct["nested_classes"]
_make_sure(
len(nested_classes) >= 1,
f"Return value union doesn't exist in `{struct['name']}`",
)
retval_union = nested_classes[0]
for prop in retval_union["properties"]["public"]:
name = str(prop["name"])
member = _ErtMember(
f"GetApiData().{name}",
["retval"],
_number_ft_from_c_type(cpp_header, prop["type"]),
)
retval_members[prop["name"]] = member
# Make sure we have everything we need.
for api_func_name, retval_name in retval_info.items():
if retval_name is not None:
_make_sure(
retval_name in retval_members,
f"Return value union member `{retval_name}` doesn't exist (function {api_func_name}())",
)
# Create beginning/end event record type objects.
begin_erts = []
end_erts = []
nested_classes = struct["nested_classes"][args_nested_cls_index]["nested_classes"]
props = struct["nested_classes"][args_nested_cls_index]["properties"]["public"]
_make_sure(
len(nested_classes) == len(props),
f'Mismatch between nested structure and member count in `{struct["name"]}`',
)
for index, prop in enumerate(props):
# API function name is the name of the member.
api_func_name = str(prop["name"])
# Get the parameters.
members = _get_ert_members_for_struct(
cpp_header, nested_classes[index], f"GetApiData().args.{api_func_name}", []
)
# Append new beginning event record type object.
begin_erts.append(_BeginErt(api_func_name, members))
# Append new end event record type object if possible.
ret_members = []
if retval_info is not None:
retval_type = retval_info.get(api_func_name)
if retval_type is not None:
ret_members.append(retval_members[retval_type])
end_erts.append(_EndErt(api_func_name, ret_members))
return begin_erts, end_erts
# Creates and returns the return value information dictionary.
#
# This dictionary maps API function names to the member to get within
# the callback data structure.
#
# This only applies to the HSA API: for other APIs, this function
# returns `None`.
def _get_retval_info(path):
if "hsa" not in os.path.basename(path):
return
retval_info = {}
cur_api_func_name = None
with open(path) as f:
for line in f:
if 'out << ")' in line and cur_api_func_name is not None:
m = re.search(r"api_data.(\w+_retval)", line)
retval_info[cur_api_func_name] = m.group(1) if m else None
else:
m = re.search(r'out << "(hsa_\w+)\(";', line)
if m:
cur_api_func_name = m.group(1)
return retval_info
# Returns a partial barectf data stream type in YAML with the event
# record types `erts`.
def _yaml_dst_from_erts(api_prefix, erts):
# Base.
yaml_erts = {}
yaml_dst = {
"event-record-types": yaml_erts,
}
# Create one event record type per API function.
for ert in erts:
# Base.
yaml_members = []
yaml_ert = {
"payload-field-type": {
"class": "struct",
"members": yaml_members,
},
}
# Create one structure field type member per member.
for member in ert.members:
# barectf doesn't support nested CTF structures, so join
# individual member names with `__` to flatten.
yaml_members.append(
{
"_"
+ "__".join(member.member_names): {
"field-type": member.ft.barectf_yaml,
},
}
)
# Add event record type.
yaml_erts[ert.name(api_prefix)] = yaml_ert
# Convert to YAML.
return yaml.dump(yaml_dst)
# Returns the C++ switch statement which calls the correct barectf
# tracing function depending on the API function operation ID.
def _cpp_switch_statement_from_erts(api_prefix, erts):
lines = []
lines.append("switch (GetOp()) {")
for ert in erts:
if api_prefix == 'hip' and 'R0600' in ert.api_func_name:
continue
lines.append(f" case {api_prefix.upper()}_API_ID_{ert.api_func_name}:")
lines.append(f" barectf_{api_prefix}_api_trace_{ert.name(api_prefix)}(")
lines.append(f" &barectf_ctx,")
lines.append(f" GetThreadId(),")
lines.append(f" GetQueueId(),")
lines.append(f" GetAgentId(),")
lines.append(f" GetCorrelationId(),")
if api_prefix == "hip":
lines.append(f" GetKernelName().c_str(),")
if len(ert.members) == 0:
# Remove last comma.
lines[-1] = lines[-1].replace(",", "")
for index, member in enumerate(ert.members):
if type(member.ft) is _OptStrFt:
# Only dereference C string if not null, otherwise use
# an empty string.
lines.append(f' {member.access} ? {member.access} : ""')
elif type(member.ft) is _StrFt:
lines.append(f" {member.access}")
else:
lines.append(f" {member.ft.cast(member.access)}")
if index + 1 < len(ert.members):
lines[-1] += ","
lines.append(" );")
lines.append(" break;")
lines.append("}")
return lines
# Returns a set of expected API function names based on the
# enumerators of the `*_api_id_t` enumeration.
def _api_func_names(api_prefix, cpp_header):
# Find the `*_api_id_t` enumeration.
for enum in cpp_header.enums:
if enum.get('name') == f'{api_prefix}_api_id_t':
break
# Create the set of API function names based on enumerators.
func_names = set()
pat = re.compile(rf'{api_prefix.upper()}_API_ID_(_*{api_prefix}.+)$')
for entry in enum['values']:
if type(entry['value']) is str and 'API_ID_NONE' in entry['value']:
# An enumerator may have the value `*_API_ID_NONE` which
# means the corresponding API function is not available.
continue
m = pat.match(entry['name'])
if m is not None:
func_names.add(m.group(1))
# Return API function names
return func_names
# Processes the complete API header file `path`.
def _process_file(api_prefix, path):
# Create `CppHeader` object.
try:
cpp_header = CppHeaderParser.CppHeader(path)
except CppHeaderParser.CppParseError as exc:
print(exc, file=sys.stderr)
sys.exit(1)
# Get return value information dictionary.
retval_info = _get_retval_info(path)
# add support for structures defined inside union.
new_items = []
for struct_name, struct in cpp_header.classes.items():
# Check if the struct_name starts with 'union '.
if re.match(r'^union \w+', struct_name) is not None:
parts = struct_name.split('::')
simplified = parts[-1]
if simplified != "union ":
new_items.append((simplified, cpp_header.classes.get(struct_name)))
for key, value in new_items:
cpp_header.classes[key] = value
# Find callback data structure.
for struct_name, struct in cpp_header.classes.items():
if re.match(r'^' + api_prefix + r'_api_data\w+$', struct_name) is not None:
break
# Process callback data structure.
begin_erts, end_erts = _erts_from_cb_data_struct(api_prefix,
cpp_header,
retval_info,
struct)
# API functions without parameters are not part of the callback data
# structure, but they have an ID in the `*_api_id_t` enumeration.
#
# Add missing event record types to `begin_erts` and `end_erts`
# considering the `*_api_id_t` enumeration.
processed_api_func_names = set([ert.api_func_name for ert in begin_erts])
for func_name in _api_func_names(api_prefix, cpp_header):
if func_name not in processed_api_func_names:
begin_erts.append(_BeginErt(func_name, []))
end_erts.append(_EndErt(func_name, []))
# Write barectf YAML file.
with open(f'{api_prefix}_erts.yaml', 'w') as f:
f.write(_yaml_dst_from_erts(api_prefix, begin_erts + end_erts))
# Write C++ code (beginning event record).
with open(f'{api_prefix}_begin.cpp.i', 'w') as f:
f.write('\n'.join(_cpp_switch_statement_from_erts(api_prefix,
begin_erts)))
# Write C++ code (end event record).
with open(f'{api_prefix}_end.cpp.i', 'w') as f:
f.write('\n'.join(_cpp_switch_statement_from_erts(api_prefix,
end_erts)))
if __name__ == "__main__":
# Disable `CppHeaderParser` printing to standard output.
CppHeaderParser.CppHeaderParser.print_warnings = 0
CppHeaderParser.CppHeaderParser.print_errors = 0
CppHeaderParser.CppHeaderParser.debug = 0
CppHeaderParser.CppHeaderParser.debug_trace = 0
# Process the complete API header file.
_process_file(sys.argv[1], sys.argv[2])
@@ -0,0 +1,37 @@
################################################################################
# Copyright (c) 2022 Advanced Micro Devices, Inc.
#
# 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 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.
################################################################################
import sys
import yaml
if __name__ == "__main__":
with open("env.yaml", "w") as f:
f.write(
yaml.dump(
{
"environment": {
"rocprofiler_version": sys.argv[1],
}
}
)
)
+813
View File
@@ -0,0 +1,813 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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 <cassert>
#include <cstdlib>
#include <sstream>
#include <stdexcept>
#include <iostream>
#include <utility>
#include <string>
#include <memory>
#include <limits>
#include <fstream>
#include <time.h>
#include <hsa/hsa.h>
#include <hsa/hsa_ext_amd.h>
#include "hsa_prof_str.h"
#include <hip/hip_runtime.h>
#include <hip/amd_detail/hip_prof_str.h>
#include "rocprofiler.h"
#include "rocprofiler_plugin.h"
#include "../utils.h"
#include "barectf.h"
#include "barectf_event_record.h"
#include "barectf_tracer.h"
#include "plugin.h"
#include "src/utils/filesystem.hpp"
namespace fs = rocprofiler::common::filesystem;
namespace rocm_ctf {
namespace {
// Abstract tracer event record using the barectf context type `CtxT`.
template <typename CtxT> class TracerEventRecord : public BarectfEventRecord<CtxT> {
protected:
explicit TracerEventRecord(const rocprofiler_record_tracer_t& record,
const std::uint64_t clock_val)
: BarectfEventRecord<CtxT>{clock_val},
op_{record.operation_id.id},
thread_id_{record.thread_id.value},
queue_id_{record.queue_id.handle},
agent_id_{record.agent_id.handle},
correlation_id_{record.correlation_id.value} {}
std::uint32_t GetOp() const noexcept { return op_; }
std::uint32_t GetThreadId() const noexcept { return thread_id_; }
std::uint64_t GetQueueId() const noexcept { return queue_id_; }
std::uint64_t GetAgentId() const noexcept { return agent_id_; }
std::uint64_t GetCorrelationId() const noexcept { return correlation_id_; }
private:
std::uint32_t op_;
std::uint32_t thread_id_;
std::uint64_t queue_id_;
std::uint64_t agent_id_;
std::uint64_t correlation_id_;
};
// Returns the beginning clock value of the tracer or profiler record
// `record`.
template <typename RecordT> std::uint64_t GetRecordBeginClockVal(const RecordT& record) {
return record.timestamps.begin.value;
}
// Returns the end clock value of the tracer or profiler record
// `record`.
template <typename RecordT> std::uint64_t GetRecordEndClockVal(const RecordT& record) {
return record.timestamps.end.value;
}
// Queries allocated string data using the size query function
// `query_size_func` and the data query function `query_data_func`,
// returning the corresponding string and freeing temporary allocated
// memory.
//
// Returns an empty string if anything goes wrong.
template <typename QuerySizeFuncT, typename QueryDataFuncT>
std::string QueryAllocStr(QuerySizeFuncT&& query_size_func, QueryDataFuncT&& query_data_func) {
// Query size first.
std::size_t size = 0;
[[maybe_unused]] auto ret = query_size_func(&size);
assert(ret == ROCPROFILER_STATUS_SUCCESS && "Query size");
if (size == 0) {
// No size: return empty string.
return {};
}
// Query data (allocated by query_data_func()).
char* alloc_str = nullptr;
ret = query_data_func(&alloc_str);
assert(ret == ROCPROFILER_STATUS_SUCCESS && "Query data");
if (!alloc_str) {
// No data: return empty string.
return {};
}
// Allocate return value.
std::string str_ret{alloc_str};
// Free allocated data.
std::free(alloc_str);
// Return string object.
return str_ret;
}
// rocTX event record.
class RocTxEventRecord final : public TracerEventRecord<barectf_roctx_ctx> {
public:
explicit RocTxEventRecord(const rocprofiler_record_tracer_t& record,
const rocprofiler_session_id_t session_id)
: TracerEventRecord<barectf_roctx_ctx>{record, GetRecordBeginClockVal(record)},
id_{record.external_id.id} {
msg_ = record.name ? record.name : "";
}
void Write(barectf_roctx_ctx& barectf_ctx) const override {
barectf_roctx_trace_roctx(&barectf_ctx, GetThreadId(), id_, msg_.c_str());
}
private:
std::uint64_t id_;
std::string msg_;
};
// Abstract HSA API event record.
class HsaApiEventRecord : public TracerEventRecord<barectf_hsa_api_ctx> {
protected:
explicit HsaApiEventRecord(const rocprofiler_record_tracer_t& record,
const rocprofiler_session_id_t session_id,
const std::uint64_t clock_val)
: TracerEventRecord<barectf_hsa_api_ctx>{record, clock_val} {
if (record.api_data.hsa) api_data_ = *(record.api_data.hsa);
}
explicit HsaApiEventRecord(const rocprofiler_record_tracer_t& record,
const std::uint64_t clock_val, hsa_api_data_t& api_data)
: TracerEventRecord<barectf_hsa_api_ctx>{record, clock_val}, api_data_(api_data) {}
const hsa_api_data_t& GetApiData() const noexcept { return api_data_; }
private:
hsa_api_data_t api_data_;
};
// HSA API event record (beginning).
class HsaApiEventRecordBegin final : public HsaApiEventRecord {
public:
explicit HsaApiEventRecordBegin(const rocprofiler_record_tracer_t& record,
const rocprofiler_session_id_t session_id)
: HsaApiEventRecord{record, session_id, GetRecordBeginClockVal(record)} {}
explicit HsaApiEventRecordBegin(const rocprofiler_record_tracer_t& record,
hsa_api_data_t& api_data)
: HsaApiEventRecord{record, GetRecordBeginClockVal(record), api_data} {}
void Write(barectf_hsa_api_ctx& barectf_ctx) const override {
// Include generated switch statement.
#include "hsa_begin.cpp.i"
}
};
// HSA API event record (end).
class HsaApiEventRecordEnd final : public HsaApiEventRecord {
public:
explicit HsaApiEventRecordEnd(const rocprofiler_record_tracer_t& record,
const rocprofiler_session_id_t session_id)
: HsaApiEventRecord{record, session_id, GetRecordEndClockVal(record)} {}
explicit HsaApiEventRecordEnd(const rocprofiler_record_tracer_t& record, hsa_api_data_t& api_data)
: HsaApiEventRecord{record, GetRecordBeginClockVal(record), api_data} {}
void Write(barectf_hsa_api_ctx& barectf_ctx) const override {
// Include generated switch statement.
#include "hsa_end.cpp.i"
}
};
// Abstract HIP API event record.
class HipApiEventRecord : public TracerEventRecord<barectf_hip_api_ctx> {
protected:
explicit HipApiEventRecord(const rocprofiler_record_tracer_t& record,
const rocprofiler_session_id_t session_id,
const std::uint64_t clock_val)
: TracerEventRecord<barectf_hip_api_ctx>{record, clock_val},
api_data_{record.api_data.hip ? *(record.api_data.hip) : hip_api_data_t{}},
kernel_name_{record.name ? record.name : std::string{}} {}
explicit HipApiEventRecord(const rocprofiler_record_tracer_t& record,
const std::uint64_t clock_val, hip_api_data_t& api_data,
std::string kernel_name)
: TracerEventRecord<barectf_hip_api_ctx>{record, clock_val},
api_data_{api_data},
kernel_name_{kernel_name} {}
const hip_api_data_t& GetApiData() const noexcept { return api_data_; }
const std::string& GetKernelName() const noexcept { return kernel_name_; }
private:
hip_api_data_t api_data_;
std::string kernel_name_;
};
// HIP API event record (beginning).
class HipApiEventRecordBegin final : public HipApiEventRecord {
public:
explicit HipApiEventRecordBegin(const rocprofiler_record_tracer_t& record,
const rocprofiler_session_id_t session_id)
: HipApiEventRecord{record, session_id, GetRecordBeginClockVal(record)} {}
explicit HipApiEventRecordBegin(const rocprofiler_record_tracer_t& record,
hip_api_data_t& api_data, std::string kernel_name)
: HipApiEventRecord{record, GetRecordBeginClockVal(record), api_data, kernel_name} {}
void Write(barectf_hip_api_ctx& barectf_ctx) const override {
// Include generated switch statement.
#include "hip_begin.cpp.i"
}
};
// HIP API event record (end).
class HipApiEventRecordEnd final : public HipApiEventRecord {
public:
explicit HipApiEventRecordEnd(const rocprofiler_record_tracer_t& record,
const rocprofiler_session_id_t session_id)
: HipApiEventRecord{record, session_id, GetRecordEndClockVal(record)} {}
explicit HipApiEventRecordEnd(const rocprofiler_record_tracer_t& record, hip_api_data_t& api_data,
std::string kernel_name)
: HipApiEventRecord{record, GetRecordBeginClockVal(record), api_data, kernel_name} {}
void Write(barectf_hip_api_ctx& barectf_ctx) const override {
// Include generated switch statement.
#include "hip_end.cpp.i"
}
};
// HSA API handle type event record.
class HsaHandleTypeEventRecord final : public BarectfEventRecord<barectf_hsa_handles_ctx> {
public:
enum class Type {
CPU = 0,
GPU = 1,
};
explicit HsaHandleTypeEventRecord(const std::uint64_t handle, const Type type)
: BarectfEventRecord<barectf_hsa_handles_ctx>{0}, handle_{handle}, type_{type} {}
void Write(barectf_hsa_handles_ctx& barectf_ctx) const override {
barectf_hsa_handles_trace_hsa_handle_type(&barectf_ctx, handle_,
static_cast<std::uint8_t>(type_));
}
private:
std::uint64_t handle_;
Type type_;
};
// Abstract API operation event record.
class ApiOpEventRecord : public TracerEventRecord<barectf_api_ops_ctx> {
protected:
explicit ApiOpEventRecord(const rocprofiler_record_tracer_t& record,
const std::uint64_t clock_val)
: TracerEventRecord<barectf_api_ops_ctx>{record, clock_val} {}
};
// HSA API operation event record (beginning).
class HsaOpEventRecordBegin final : public ApiOpEventRecord {
public:
explicit HsaOpEventRecordBegin(const rocprofiler_record_tracer_t& record)
: ApiOpEventRecord{record, GetRecordBeginClockVal(record)} {}
void Write(barectf_api_ops_ctx& barectf_ctx) const override {
barectf_api_ops_trace_hsa_op_begin(&barectf_ctx, GetThreadId(), GetQueueId(), GetAgentId(),
GetCorrelationId());
}
};
// HSA API operation event record (end).
class HsaOpEventRecordEnd final : public ApiOpEventRecord {
public:
explicit HsaOpEventRecordEnd(const rocprofiler_record_tracer_t& record)
: ApiOpEventRecord{record, GetRecordEndClockVal(record)} {}
void Write(barectf_api_ops_ctx& barectf_ctx) const override {
barectf_api_ops_trace_hsa_op_end(&barectf_ctx, GetThreadId(), GetQueueId(), GetAgentId(),
GetCorrelationId());
}
};
// HIP API operation event record (beginning).
class HipOpEventRecordBegin final : public ApiOpEventRecord {
public:
explicit HipOpEventRecordBegin(const rocprofiler_record_tracer_t& record)
: ApiOpEventRecord{record, GetRecordBeginClockVal(record)},
kernel_name_{QueryKernelName(record)} {}
void Write(barectf_api_ops_ctx& barectf_ctx) const override {
barectf_api_ops_trace_hip_op_begin(&barectf_ctx, GetThreadId(), GetQueueId(), GetAgentId(),
GetCorrelationId(), kernel_name_.c_str());
}
private:
// Queries and returns the kernel name of the record `record`.
//
// Returns an empty string if not available.
static std::string QueryKernelName(const rocprofiler_record_tracer_t& record) {
if (record.name) {
// Return demangled version.
return rocprofiler::cxx_demangle(record.name);
}
return {};
}
std::string kernel_name_;
};
// HIP API operation event record (end).
class HipOpEventRecordEnd final : public ApiOpEventRecord {
public:
explicit HipOpEventRecordEnd(const rocprofiler_record_tracer_t& record)
: ApiOpEventRecord{record, GetRecordEndClockVal(record)} {}
void Write(barectf_api_ops_ctx& barectf_ctx) const override {
barectf_api_ops_trace_hip_op_end(&barectf_ctx, GetThreadId(), GetQueueId(), GetAgentId(),
GetCorrelationId());
}
};
// Profiler record base.
class ProfilerEventRecord : public BarectfEventRecord<barectf_profiler_ctx> {
public:
explicit ProfilerEventRecord(const rocprofiler_record_profiler_t& record,
const rocprofiler_session_id_t session_id)
: BarectfEventRecord<barectf_profiler_ctx>{GetRecordBeginClockVal(record)},
dispatch_{record.header.id.handle},
gpu_id_{record.gpu_id.handle},
queue_id_{record.queue_id.handle},
queue_index_{record.queue_idx.value},
process_id_{GetPid()},
thread_id_{record.thread_id.value},
kernel_id_{record.kernel_id.handle},
kernel_name_{QueryKernelName(record)},
counter_infos_{QueryCounterInfos(record, session_id)} {}
void Write(barectf_profiler_ctx& barectf_ctx) const override {
barectf_profiler_trace_profiler_record(
&barectf_ctx, dispatch_, gpu_id_, queue_id_, queue_index_, process_id_, thread_id_,
kernel_id_, kernel_name_.c_str(), counter_infos_.names.size(), counter_infos_.names.data(),
counter_infos_.values.size(), counter_infos_.values.data());
}
protected:
// Counter infos.
//
// `names[i]` names the counter value `values[i]`.
struct CounterInfos final {
// `names_storage` owns the strings while the elements of `names`
// point to the internal C strings of `names_storage`.
//
// This is needed because barectf expects an array of contiguous
// C string pointers.
std::vector<std::string> names_storage;
std::vector<const char*> names;
// Counter values.
std::vector<std::uint64_t> values;
};
std::uint64_t GetDispatch() const noexcept { return dispatch_; }
std::uint64_t GetGpuId() const noexcept { return gpu_id_; }
std::uint64_t GetQueueId() const noexcept { return queue_id_; }
std::uint64_t GetQueueIndex() const noexcept { return queue_index_; }
std::uint32_t GetProcessId() const noexcept { return process_id_; }
std::uint32_t GetThreadId() const noexcept { return thread_id_; }
std::uint64_t GetKernelId() const noexcept { return kernel_id_; }
const std::string& GetKernelName() const noexcept { return kernel_name_; }
const CounterInfos& GetCounterInfos() const noexcept { return counter_infos_; }
private:
// Queries and returns the kernel name of the record `record`.
//
// Returns an empty string if not available.
static std::string QueryKernelName(const rocprofiler_record_profiler_t& record) {
const auto kernel_name = QueryAllocStr(
[&record](const auto size) {
return rocprofiler_query_kernel_info_size(ROCPROFILER_KERNEL_NAME, record.kernel_id,
size);
},
[&record](const auto str) {
return rocprofiler_query_kernel_info(ROCPROFILER_KERNEL_NAME, record.kernel_id,
const_cast<const char**>(str));
});
if (kernel_name.size() <= 1) {
return {};
}
// Return truncated and demangled version.
return rocprofiler::truncate_name(rocprofiler::cxx_demangle(kernel_name));
}
// Queries and returns the counter infos of the record `record` and
// session ID `session_id`.
static CounterInfos QueryCounterInfos(const rocprofiler_record_profiler_t& record,
const rocprofiler_session_id_t session_id) {
if (!record.counters) {
// No counters.
return {};
}
CounterInfos infos;
for (std::size_t i = 0; i < record.counters_count.value; ++i) {
auto& counter = record.counters[i];
if (counter.counter_handler.handle == 0) {
// Not available: continue.
continue;
}
// Query counter name size first
std::size_t counter_name_size = 0;
[[maybe_unused]] auto ret = rocprofiler_query_counter_info_size(
session_id, ROCPROFILER_COUNTER_NAME, counter.counter_handler, &counter_name_size);
assert(ret == ROCPROFILER_STATUS_SUCCESS && "Query counter name size");
if (counter_name_size == 0) {
// No size: continue.
continue;
}
// Query counter name (borrowed from `record`: no need to free).
const char* counter_name = nullptr;
ret = rocprofiler_query_counter_info(session_id, ROCPROFILER_COUNTER_NAME,
counter.counter_handler, &counter_name);
assert(ret == ROCPROFILER_STATUS_SUCCESS && "Query counter name");
if (!counter_name) {
// Not available: continue.
continue;
}
// Push back infos.
infos.names_storage.emplace_back(counter_name);
infos.names.push_back(infos.names_storage.back().c_str());
infos.values.push_back(counter.value.value);
}
return infos;
}
std::uint64_t dispatch_;
std::uint64_t gpu_id_;
std::uint64_t queue_id_;
std::uint64_t queue_index_;
std::uint32_t process_id_;
std::uint32_t thread_id_;
std::uint64_t kernel_id_;
std::string kernel_name_;
CounterInfos counter_infos_;
};
// Profiler record base.
class ProfilerWithKernelPropsEventRecord final : public ProfilerEventRecord {
private:
// According to `plugin/file/file.cpp`:
//
// > Taken from rocprofiler: The size hasn't changed in recent past
static constexpr std::uint32_t lds_block_size_ = 128 * 4;
public:
explicit ProfilerWithKernelPropsEventRecord(const rocprofiler_record_profiler_t& record,
const rocprofiler_session_id_t session_id)
: ProfilerEventRecord{record, session_id},
grid_size_{record.kernel_properties.grid_size},
workgroup_size_{record.kernel_properties.workgroup_size},
lds_size_{
((record.kernel_properties.lds_size + (lds_block_size_ - 1)) & ~(lds_block_size_ - 1))},
scratch_size_{record.kernel_properties.scratch_size},
arch_vgpr_count_{record.kernel_properties.arch_vgpr_count},
accum_vgpr_count_{record.kernel_properties.accum_vgpr_count},
sgpr_count_{record.kernel_properties.sgpr_count},
wave_size_{record.kernel_properties.wave_size},
signal_handle_{record.kernel_properties.signal_handle} {}
void Write(barectf_profiler_ctx& barectf_ctx) const override {
barectf_profiler_trace_profiler_record_with_kernel_properties(
&barectf_ctx, GetDispatch(), GetGpuId(), GetQueueId(), GetQueueIndex(), GetProcessId(),
GetThreadId(), GetKernelId(), GetKernelName().c_str(), GetCounterInfos().names.size(),
GetCounterInfos().names.data(), GetCounterInfos().values.size(),
GetCounterInfos().values.data(), grid_size_, workgroup_size_, lds_size_, scratch_size_,
arch_vgpr_count_, accum_vgpr_count_, sgpr_count_, wave_size_, signal_handle_);
}
private:
std::uint64_t grid_size_;
std::uint64_t workgroup_size_;
std::uint64_t lds_size_;
std::uint64_t scratch_size_;
std::uint64_t arch_vgpr_count_;
std::uint64_t accum_vgpr_count_;
std::uint64_t sgpr_count_;
std::uint64_t wave_size_;
std::uint64_t signal_handle_;
};
} // namespace
Plugin::Plugin(const std::size_t packet_size, const fs::path& trace_dir,
const fs::path& metadata_stream_path)
: roctx_tracer_{packet_size, trace_dir, "roctx_"},
hsa_api_tracer_{packet_size, trace_dir, "hsa_api_"},
hip_api_tracer_{packet_size, trace_dir, "hip_api_"},
api_ops_tracer_{packet_size, trace_dir, "api_ops_"},
hsa_handles_tracer_{packet_size, trace_dir, "hsa_handles_"},
profiler_tracer_{packet_size, trace_dir, "profiler_"} {
// Make sure the trace directory doesn't exist.
if (fs::exists(trace_dir)) {
std::ostringstream ss;
ss << "CTF trace directory `" << trace_dir.string() << "` already exists";
throw std::runtime_error{ss.str()};
}
// Make sure the metadata stream file exists.
if (!fs::exists(metadata_stream_path)) {
std::ostringstream ss;
ss << "CTF metadata stream file `" << metadata_stream_path.string() << "` doesn't exist";
throw std::runtime_error{ss.str()};
}
// Create trace directory.
if (!fs::create_directory(trace_dir)) {
std::ostringstream ss;
ss << "Cannot create the CTF trace directory `" << trace_dir.string() << "`";
throw std::runtime_error{ss.str()};
}
// Copy adjusted metadata stream file to trace directory.
try {
CopyAdjustedMetadataStreamFile(metadata_stream_path, trace_dir);
} catch (const std::exception& exc) {
std::ostringstream ss;
ss << "Cannot adjust and copy metadata stream file `" << metadata_stream_path.string()
<< "` to the CTF trace directory `" << trace_dir.string() << "`: " << exc.what();
throw std::runtime_error{ss.str()};
}
// Write HSA handle type event records.
WriteHsaHandleTypes();
}
void Plugin::HandleTracerRecord(const rocprofiler_record_tracer_t& record,
const rocprofiler_session_id_t session_id) {
std::lock_guard<std::mutex> lock{lock_};
// Depending on the domain, create and add an event record to the
// corresponding tracer.
switch (record.domain) {
case ACTIVITY_DOMAIN_ROCTX:
roctx_tracer_.AddEventRecord(std::make_shared<const RocTxEventRecord>(record, session_id));
break;
case ACTIVITY_DOMAIN_HSA_API: {
/*If data is nullptr then the call is asynchromous*/
if (record.api_data.hsa == nullptr) {
hsa_api_tracer_.AddEventRecord(
std::make_shared<const HsaApiEventRecordBegin>(record, session_id));
hsa_api_tracer_.AddEventRecord(
std::make_shared<const HsaApiEventRecordEnd>(record, session_id));
} else {
hsa_api_data_t hsa_api_data = *(record.api_data.hsa);
hsa_api_tracer_.AddEventRecord(
std::make_shared<const HsaApiEventRecordBegin>(record, hsa_api_data));
hsa_api_tracer_.AddEventRecord(
std::make_shared<const HsaApiEventRecordEnd>(record, hsa_api_data));
}
break;
}
case ACTIVITY_DOMAIN_HIP_API: {
/*If data is nullptr then the call is asynchromous*/
if (record.api_data.hip == nullptr) {
hip_api_tracer_.AddEventRecord(
std::make_shared<const HipApiEventRecordBegin>(record, session_id));
hip_api_tracer_.AddEventRecord(
std::make_shared<const HipApiEventRecordEnd>(record, session_id));
} else {
std::string kernel_name;
hip_api_data_t hip_api_data = *(record.api_data.hip);
if (record.name != nullptr)
kernel_name = rocprofiler::truncate_name(rocprofiler::cxx_demangle(std::string(record.name)));
else
kernel_name = "";
hip_api_tracer_.AddEventRecord(
std::make_shared<const HipApiEventRecordBegin>(record, hip_api_data, kernel_name));
hip_api_tracer_.AddEventRecord(
std::make_shared<const HipApiEventRecordEnd>(record, hip_api_data, kernel_name));
}
break;
}
case ACTIVITY_DOMAIN_HSA_OPS:
api_ops_tracer_.AddEventRecord(std::make_shared<const HsaOpEventRecordBegin>(record));
api_ops_tracer_.AddEventRecord(std::make_shared<const HsaOpEventRecordEnd>(record));
break;
case ACTIVITY_DOMAIN_HIP_OPS:
api_ops_tracer_.AddEventRecord(std::make_shared<const HipOpEventRecordBegin>(record));
api_ops_tracer_.AddEventRecord(std::make_shared<const HipOpEventRecordEnd>(record));
break;
default:
// Warn
std::cerr << "rocm_ctf::Plugin::HandleTracerRecord(): "
<< "ignoring record for unknown domain #" << record.domain << std::endl;
break;
}
}
void Plugin::HandleProfilerRecord(const rocprofiler_record_profiler_t& record,
const rocprofiler_session_id_t session_id) {
std::lock_guard<std::mutex> lock{lock_};
profiler_tracer_.AddEventRecord(
std::make_shared<const ProfilerWithKernelPropsEventRecord>(record, session_id));
}
void Plugin::HandleBufferRecords(const rocprofiler_record_header_t* begin,
const rocprofiler_record_header_t* const end,
const rocprofiler_session_id_t session_id,
const rocprofiler_buffer_id_t buffer_id) {
while (begin && begin < end) {
if (begin->kind == ROCPROFILER_TRACER_RECORD) {
HandleTracerRecord(*reinterpret_cast<const rocprofiler_record_tracer_t*>(begin), session_id);
} else {
assert(begin->kind == ROCPROFILER_PROFILER_RECORD);
HandleProfilerRecord(*reinterpret_cast<const rocprofiler_record_profiler_t*>(begin),
session_id);
}
rocprofiler_next_record(begin, &begin, session_id, buffer_id);
}
}
void Plugin::WriteHsaHandleTypes() {
[[maybe_unused]] const auto status = hsa_iterate_agents(
[](const auto agent, const auto user_data) {
auto& tracer = *static_cast<HsaHandlesTracer*>(user_data);
hsa_device_type_t type;
if (hsa_agent_get_info(agent, HSA_AGENT_INFO_DEVICE, &type) != HSA_STATUS_SUCCESS) {
return HSA_STATUS_ERROR;
}
using Type = HsaHandleTypeEventRecord::Type;
auto event_record = std::make_shared<HsaHandleTypeEventRecord>(
agent.handle, type == HSA_DEVICE_TYPE_CPU ? Type::CPU : Type::GPU);
tracer.AddEventRecord(std::move(event_record));
return HSA_STATUS_SUCCESS;
},
&hsa_handles_tracer_);
assert(status == HSA_STATUS_SUCCESS && "Iterate HSA agents");
}
namespace {
constexpr std::uint64_t ns_per_s = 1'000'000'000ULL;
// Samples the ROCProfiler clock and returns the value.
std::uint64_t GetClkVal() {
rocprofiler_timestamp_t ts;
[[maybe_unused]] const auto ret = rocprofiler_get_timestamp(&ts);
assert(ret == ROCPROFILER_STATUS_SUCCESS && "Get timestamp");
return ts.value;
}
// Updates `offset` and `delta`, if needed, to a more accurate clock
// class offset and a smaller ROCProfiler clock value delta.
//
// This function samples the ROCProfiler clock twice, also sampling the
// real-time clock in between, and uses the average ROCProfiler clock
// value to approximate the actual clock class offset.
//
// This strategy is based on the measure_single_clock_offset() function
// of the LTTng-tools project <https://lttng.org/>.
void UpdateClkClsOffsetAndDelta(std::uint64_t& offset, std::uint64_t& delta) {
// Sample ROCProfiler clock (first time).
const auto rocm_clk_val1 = GetClkVal();
// Sample real-time clock.
timespec realtime_spec = {0, 0};
[[maybe_unused]] const auto ret = clock_gettime(CLOCK_REALTIME, &realtime_spec);
assert(ret == 0);
// Sample ROCProfiler clock (second time).
const auto rocm_clk_val2 = GetClkVal();
// Compute the current ROCProfiler clock value delta.
const auto this_delta = rocm_clk_val2 - rocm_clk_val1;
if (this_delta > delta) {
// Discard larger delta.
return;
}
// Compute the average ROCProfiler clock value.
const auto rocm_clk_val_avg = (rocm_clk_val1 + rocm_clk_val2) >> 1;
// Compute the real-time clock value in nanoseconds.
const auto realtime_ns =
(static_cast<std::uint64_t>(realtime_spec.tv_sec) * ns_per_s) + realtime_spec.tv_nsec;
// Update clock class offset and delta.
assert(rocm_clk_val_avg < realtime_ns);
offset = realtime_ns - rocm_clk_val_avg;
delta = this_delta;
}
// Computes and returns the most possible accurate clock class offset.
std::uint64_t GetMetadataClkClsOffset() {
std::uint64_t offset = 0;
std::uint64_t delta = std::numeric_limits<std::uint64_t>::max();
// Best effort to find the most accurate offset.
for (auto i = 0U; i < 50U; ++i) {
UpdateClkClsOffsetAndDelta(offset, delta);
}
return offset;
}
} // namespace
static const char* LOOP_MPI_RANK(const std::vector<const char*>& mpivars) {
for (const char* env : mpivars)
if (const char* envvar = getenv(env)) return envvar;
return nullptr;
}
static void insert_meta_to_stream(std::stringstream& stream, const char* field, const char* value) {
if (!field || !value) return;
stream << "\n\t" << std::string(field) << " = " << std::string(value) << ';';
}
void Plugin::CopyAdjustedMetadataStreamFile(const fs::path& metadata_stream_path,
const fs::path& trace_dir) {
// Load installed metadata stream file contents.
std::string metadata;
std::getline(std::ifstream{metadata_stream_path}, metadata, '\0');
// Replace the original `offset` property.
{
static constexpr auto offset_term = "offset = 0;";
std::ostringstream ss;
ss << "offset = " << GetMetadataClkClsOffset() << ';';
metadata.replace(metadata.find(offset_term), std::strlen(offset_term), ss.str());
}
std::stringstream data_stream;
const char* rank = LOOP_MPI_RANK({"MPI_RANK", "OMPI_COMM_WORLD_RANK", "MV2_COMM_WORLD_RANK"});
// Add MPI information to metadata
if (rank) {
insert_meta_to_stream(data_stream, "rank", rank);
insert_meta_to_stream(data_stream, "node_rank", getenv("OMPI_COMM_WORLD_NODE_RANK"));
const char* local = LOOP_MPI_RANK({"OMPI_COMM_WORLD_LOCAL_RANK", "MV2_COMM_WORLD_LOCAL_RANK"});
insert_meta_to_stream(data_stream, "local_rank", local);
std::string data_ins = data_stream.str();
size_t env_pos = metadata.find("env {");
if (env_pos != std::string::npos)
metadata.insert(metadata.begin() + env_pos + 5, data_ins.begin(), data_ins.end());
else
std::cerr << "Failed to insert MPI metadata!" << std::endl;
}
// Write adjusted metadata stream to trace directory.
{
std::ofstream output{trace_dir / "metadata"};
output.write(metadata.data(), metadata.size());
}
}
} // namespace rocm_ctf
+148
View File
@@ -0,0 +1,148 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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 PLUGIN_CTF_PLUGIN_H
#define PLUGIN_CTF_PLUGIN_H
#include <mutex>
#include <cstdlib>
#include "src/utils/filesystem.hpp"
#include "rocprofiler.h"
#include "rocprofiler_plugin.h"
#include "barectf.h"
#include "barectf_tracer.h"
namespace rocm_ctf {
// CTF plugin.
//
// Build a plugin instance, and then call HandleTracerRecord(),
// HandleProfilerRecord(), and HandleBufferRecords() to add event
// records.
//
// A plugin instance performs important tasks at destruction time.
class Plugin final {
public:
// Builds a plugin instance to write a CTF trace in the `trace_dir`
// directory with packets of size `packet_size` bytes.
//
// `trace_dir` must not exist.
//
// This constructor immediately adjusts and copies the metadata stream
// file `metadata_stream_path` to the trace directory (`trace_dir`).
explicit Plugin(std::size_t packet_size, const rocprofiler::common::filesystem::path& trace_dir,
const rocprofiler::common::filesystem::path& metadata_stream_path);
// Handles a tracer record.
void HandleTracerRecord(const rocprofiler_record_tracer_t& record,
rocprofiler_session_id_t session_id);
// Handles a profiler record.
void HandleProfilerRecord(const rocprofiler_record_profiler_t& record,
rocprofiler_session_id_t session_id);
// Handles tracer or profiler records from `begin` to `end`
// (excluded).
void HandleBufferRecords(const rocprofiler_record_header_t* begin,
const rocprofiler_record_header_t* end,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id);
private:
// rocTX barectf platform descriptor.
struct RocTxPlatformDescr final {
using Ctx = barectf_roctx_ctx;
static void OpenPacket(Ctx& ctx) { barectf_roctx_open_packet(&ctx); }
static void ClosePacket(Ctx& ctx) { barectf_roctx_close_packet(&ctx); }
};
// HSA API barectf platform descriptor.
struct HsaApiPlatformDescr final {
using Ctx = barectf_hsa_api_ctx;
static void OpenPacket(Ctx& ctx) { barectf_hsa_api_open_packet(&ctx); }
static void ClosePacket(Ctx& ctx) { barectf_hsa_api_close_packet(&ctx); }
};
// HIP API barectf platform descriptor.
struct HipApiPlatformDescr final {
using Ctx = barectf_hip_api_ctx;
static void OpenPacket(Ctx& ctx) { barectf_hip_api_open_packet(&ctx); }
static void ClosePacket(Ctx& ctx) { barectf_hip_api_close_packet(&ctx); }
};
// HSA handles barectf platform descriptor.
struct HsaHandlesPlatformDescr final {
using Ctx = barectf_hsa_handles_ctx;
static void OpenPacket(Ctx& ctx) { barectf_hsa_handles_open_packet(&ctx); }
static void ClosePacket(Ctx& ctx) { barectf_hsa_handles_close_packet(&ctx); }
};
// API operations barectf platform descriptor.
struct ApiOpsPlatformDescr final {
using Ctx = barectf_api_ops_ctx;
static void OpenPacket(Ctx& ctx) { barectf_api_ops_open_packet(&ctx); }
static void ClosePacket(Ctx& ctx) { barectf_api_ops_close_packet(&ctx); }
};
// Profiler barectf platform descriptor.
struct ProfilerPlatformDescr final {
using Ctx = barectf_profiler_ctx;
static void OpenPacket(Ctx& ctx) { barectf_profiler_open_packet(&ctx); }
static void ClosePacket(Ctx& ctx) { barectf_profiler_close_packet(&ctx); }
};
// barectf tracer for HSA handle mappings.
using HsaHandlesTracer = BarectfTracer<HsaHandlesPlatformDescr>;
// Writes the HSA handle type mappings to a dedicated data stream
// file.
void WriteHsaHandleTypes();
// Loads the existing metadata stream file `metadata_stream_path`,
// adjusts the `offset` property of its single clock class, and writes
// the result to the `metadata` file within the `trace_dir` directory.
void CopyAdjustedMetadataStreamFile(
const rocprofiler::common::filesystem::path& metadata_stream_path,
const rocprofiler::common::filesystem::path& trace_dir);
// Dedicated tracers.
BarectfTracer<RocTxPlatformDescr> roctx_tracer_;
BarectfTracer<HsaApiPlatformDescr> hsa_api_tracer_;
BarectfTracer<HipApiPlatformDescr> hip_api_tracer_;
BarectfTracer<ApiOpsPlatformDescr> api_ops_tracer_;
HsaHandlesTracer hsa_handles_tracer_;
BarectfTracer<ProfilerPlatformDescr> profiler_tracer_;
// Locks any operation performed on the data of this.
std::mutex lock_;
};
} // namespace rocm_ctf
#endif // PLUGIN_CTF_PLUGIN_H
+12
View File
@@ -0,0 +1,12 @@
ROCPROFILER_PLUGINS_1.0.0{
global: rocprofiler_plugin_initialize;
rocprofiler_plugin_finalize;
rocprofiler_plugin_write_buffer_records;
rocprofiler_plugin_write_record;
addDecoder;
removeDecoder;
getInstructionFromAddr;
getInstructionFromID;
getSymbolName;
local: *;
};
@@ -0,0 +1,93 @@
# ###############################################################################
# # Copyright (c) 2022 Advanced Micro Devices, Inc.
# #
# # 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 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.
# ###############################################################################
file(GLOB ROCPROFILER_UTIL_SRC_FILES ${PROJECT_SOURCE_DIR}/src/utils/helper.cpp)
file(GLOB FILE_SOURCES_V1 "file_v1.cpp")
add_library(file_plugin_v1 SHARED ${FILE_SOURCES_V1} ${ROCPROFILER_UTIL_SRC_FILES})
set_target_properties(
file_plugin_v1
PROPERTIES CXX_VISIBILITY_PRESET hidden
DEFINE_SYMBOL "ROCPROFILER_EXPORTS"
LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib/rocprofiler/v1
INSTALL_RPATH "${ROCM_APPEND_PRIVLIB_RPATH}"
OUTPUT_NAME file_plugin
VERSION 1.0.0
SOVERSION 1)
target_compile_definitions(file_plugin_v1 PRIVATE HIP_PROF_HIP_API_STRING=1
__HIP_PLATFORM_HCC__=1)
target_include_directories(file_plugin_v1 PRIVATE ${PROJECT_SOURCE_DIR})
target_link_options(
file_plugin_v1 PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
-Wl,--no-undefined)
target_link_libraries(file_plugin_v1 PRIVATE rocprofiler-v2 hsa-runtime64::hsa-runtime64
stdc++fs amd_comgr dl)
# Prepare Name Link SO files for V1 file plugin Library
add_custom_command(
TARGET file_plugin_v1
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy
${PROJECT_BINARY_DIR}/lib/rocprofiler/v1/libfile_plugin.so.1*
${PROJECT_BINARY_DIR}/lib/rocprofiler/.)
install(TARGETS file_plugin_v1 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
COMPONENT asan NAMELINK_SKIP)
install(TARGETS file_plugin_v1 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
COMPONENT runtime NAMELINK_SKIP)
file(GLOB FILE_SOURCES "file.cpp")
add_library(file_plugin SHARED ${FILE_SOURCES} ${ROCPROFILER_UTIL_SRC_FILES})
set_target_properties(
file_plugin
PROPERTIES CXX_VISIBILITY_PRESET hidden
DEFINE_SYMBOL "ROCPROFILER_EXPORTS"
LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib/rocprofiler
INSTALL_RPATH "${ROCM_APPEND_PRIVLIB_RPATH}"
VERSION 2.0.0
SOVERSION 2)
target_compile_definitions(file_plugin PRIVATE HIP_PROF_HIP_API_STRING=1
__HIP_PLATFORM_AMD__=1)
target_include_directories(file_plugin PRIVATE ${PROJECT_SOURCE_DIR})
target_link_options(
file_plugin PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
-Wl,--no-undefined)
target_link_libraries(file_plugin PRIVATE rocprofiler-v2 hsa-runtime64::hsa-runtime64
stdc++fs amd_comgr dl)
install(TARGETS file_plugin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
COMPONENT asan)
install(TARGETS file_plugin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
COMPONENT runtime)
+545
View File
@@ -0,0 +1,545 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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 <cxxabi.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <atomic>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <ostream>
#include <sstream>
#include <string>
#include <hsa/hsa.h>
#include <mutex>
#include <unordered_map>
#include "rocprofiler.h"
#include "rocprofiler_plugin.h"
#include "../utils.h"
#include "src/utils/filesystem.hpp"
namespace fs = rocprofiler::common::filesystem;
namespace {
std::vector<std::string> GetCounterNames() {
std::vector<std::string> counters;
const char* line_c_str = getenv("ROCPROFILER_COUNTERS");
if (line_c_str) {
std::string line = line_c_str;
// skip commented lines
auto found = line.find_first_not_of(" \t");
if (found != std::string::npos) {
if (line[found] == '#') return {};
}
if (line.find("pmc") == std::string::npos) return counters;
char seperator = ' ';
std::string::size_type prev_pos = 0, pos = line.find(seperator, prev_pos);
prev_pos = ++pos;
if (pos != std::string::npos) {
while ((pos = line.find(seperator, pos)) != std::string::npos) {
std::string substring(line.substr(prev_pos, pos - prev_pos));
if (substring.length() > 0 && substring != ":") {
counters.push_back(substring);
}
prev_pos = ++pos;
}
if (!line.substr(prev_pos, pos - prev_pos).empty()) {
counters.push_back(line.substr(prev_pos, pos - prev_pos));
}
}
}
return counters;
}
class file_plugin_t {
private:
enum class output_type_t { COUNTER, TRACER, PC_SAMPLING };
class output_file_t {
public:
output_file_t(std::string name, bool bOpenOnInit = false) : name_(std::move(name)) {
if (bOpenOnInit) open();
}
std::string name() const { return name_; }
template <typename T> std::ostream& operator<<(T&& value) {
if (!is_open()) open();
return stream_ << std::forward<T>(value);
}
std::ostream& operator<<(std::ostream& (*func)(std::ostream&)) {
if (!is_open()) open();
return stream_ << func;
}
void open() {
// If the stream is already in the failed state, there's no need to try
// to open the file.
if (fail()) return;
const char* output_dir = getenv("OUTPUT_PATH");
const char* output_file_name_env = getenv("OUT_FILE_NAME");
std::string output_file_name = output_file_name_env ? output_file_name_env : "";
if (output_dir == nullptr && output_file_name.size() == 0) {
stream_.copyfmt(std::cout);
stream_.clear(std::cout.rdstate());
stream_.basic_ios<char>::rdbuf(std::cout.rdbuf());
bPrintToStdout = true;
return;
}
if (output_dir == nullptr) output_dir = ".";
fs::path output_prefix(output_dir);
if (!fs::is_directory(fs::status(output_prefix))) {
if (!stream_.fail()) rocprofiler::warning("Cannot open output directory '%s'", output_dir);
stream_.setstate(std::ios_base::failbit);
return;
}
std::stringstream ss;
ss << name_ << "_" << ((output_file_name.empty()) ? std::to_string(GetPid()) : "")
<< output_file_name << ".csv";
std::cout << "Results File: " << output_prefix / ss.str() << std::endl;
stream_.open(output_prefix / ss.str());
}
bool is_open() const { return stream_.is_open(); }
bool fail() const { return stream_.fail(); }
bool isStdOut() const { return bPrintToStdout; }
private:
const std::string name_;
std::ofstream stream_;
bool bPrintToStdout = false;
};
output_file_t* get_output_file(output_type_t output_type, uint32_t domain = 0) {
switch (output_type) {
case output_type_t::COUNTER:
return &output_file_;
case output_type_t::TRACER:
switch (domain) {
case ACTIVITY_DOMAIN_ROCTX:
return &roctx_file_;
case ACTIVITY_DOMAIN_HSA_API:
return &hsa_api_file_;
case ACTIVITY_DOMAIN_HIP_API:
return &hip_api_file_;
case ACTIVITY_DOMAIN_HIP_OPS:
return &hip_activity_file_;
case ACTIVITY_DOMAIN_HSA_OPS:
return &hsa_async_copy_file_;
default:
assert(!"domain/op not supported!");
break;
}
break;
case output_type_t::PC_SAMPLING:
return &pc_sample_file_;
}
return nullptr;
}
public:
file_plugin_t(void* data) {
if (data) counter_names_ = GetCounterNames();
kernel_filters_ = GetKernelFilters();
const char* str = getenv("ROCPROFILER_INDIVIDUAL_XCC_MODE");
if (str != NULL) is_individual_xcc_mode = (atol(str) > 0);
valid_ = true;
}
void WriteHeader(output_type_t type, rocprofiler_tracer_activity_domain_t domain) {
output_file_t* output_file;
std::stringstream ss;
switch (domain) {
case ACTIVITY_DOMAIN_HSA_API: {
if (hsa_api_header_written_.load(std::memory_order_relaxed)) return;
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HSA_API);
ss << "Domain,Function,Start_Timestamp,End_Timestamp,Correlation_ID\n\n";
*output_file << ss.str();
hsa_api_header_written_.exchange(true, std::memory_order_release);
return;
}
case ACTIVITY_DOMAIN_HIP_API: {
if (hip_api_header_written_.load(std::memory_order_relaxed)) return;
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HIP_API);
ss << "Domain,Function,Start_Timestamp,End_Timestamp,Correlation_ID\n\n";
*output_file << ss.str();
hip_api_header_written_.exchange(true, std::memory_order_release);
return;
}
case ACTIVITY_DOMAIN_ROCTX: {
if (roctx_header_written_.load(std::memory_order_relaxed)) return;
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_ROCTX);
ss << "Domain,ROCTX_ID,Message,Timestamp\n\n";
*output_file << ss.str();
roctx_header_written_.exchange(true, std::memory_order_release);
return;
}
case ACTIVITY_DOMAIN_HSA_OPS: {
if (hsa_async_copy_header_written_.load(std::memory_order_relaxed)) return;
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HSA_OPS);
ss << "Domain,Operation,Start_Timestamp,Stop_Timestamp,Correlation_ID\n\n";
*output_file << ss.str();
hsa_async_copy_header_written_.exchange(true, std::memory_order_release);
return;
}
case ACTIVITY_DOMAIN_HIP_OPS: {
if (hip_activity_header_written_.load(std::memory_order_relaxed)) return;
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HIP_OPS);
ss << "Domain,Operation,Kernel_Name,Start_Timestamp,Stop_Timestamp,"
"Correlation_ID\n\n";
*output_file << ss.str();
hip_activity_header_written_.exchange(true, std::memory_order_release);
return;
}
default: {
if (type == output_type_t::COUNTER) {
if (kernel_dispatches_header_written_.load(std::memory_order_relaxed)) return;
output_file = get_output_file(output_type_t::COUNTER);
ss << "Dispatch_ID,GPU_ID,Queue_ID,PID,TID,Grid_Size,Workgroup_Size,LDS_Per_Workgroup,"
"Scratch_Per_Workitem,Arch_VGPR,"
"Accum_VGPR,SGPR,Wave_Size,Kernel_Name,Start_Timestamp,End_Timestamp,"
"Correlation_ID";
*output_file << ss.str();
kernel_dispatches_header_written_.exchange(true, std::memory_order_release);
return;
} else if (type == output_type_t::PC_SAMPLING) {
if (pc_sample_header_written_.load(std::memory_order_relaxed)) return;
output_file = get_output_file(output_type_t::PC_SAMPLING);
ss << "Dispatch_ID,Timestamp,GPU_ID,PC_Sample,Shader_Engines\n\n";
*output_file << ss.str();
pc_sample_header_written_.exchange(true, std::memory_order_release);
return;
}
return;
}
}
}
std::mutex writing_lock;
const char* GetDomainName(rocprofiler_tracer_activity_domain_t domain) {
switch (domain) {
case ACTIVITY_DOMAIN_ROCTX:
return "ROCTX_DOMAIN";
break;
case ACTIVITY_DOMAIN_HIP_API:
return "HIP_API_DOMAIN";
break;
case ACTIVITY_DOMAIN_HIP_OPS:
return "HIP_OPS_DOMAIN";
break;
case ACTIVITY_DOMAIN_HSA_API:
return "HSA_API_DOMAIN";
break;
case ACTIVITY_DOMAIN_HSA_OPS:
return "HSA_OPS_DOMAIN";
break;
case ACTIVITY_DOMAIN_HSA_EVT:
return "HSA_EVT_DOMAIN";
break;
default:
return "";
}
}
void FlushTracerRecord(rocprofiler_record_tracer_t tracer_record,
rocprofiler_session_id_t session_id,
rocprofiler_buffer_id_t buffer_id = rocprofiler_buffer_id_t{0}) {
std::lock_guard<std::mutex> lock(writing_lock);
std::stringstream ss;
if (tracer_record.timestamps.end.value <= 0 && tracer_record.domain != ACTIVITY_DOMAIN_ROCTX)
return;
WriteHeader(output_type_t::TRACER, tracer_record.domain);
std::string roctx_message;
if (tracer_record.domain == ACTIVITY_DOMAIN_ROCTX && tracer_record.name) {
roctx_message = tracer_record.name;
}
const char* operation_name_c = nullptr;
// ROCTX domain Operation ID doesn't have a name
// It depends on the user input of the roctx functions.
// ROCTX message is the tracer_record.name
if (tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
CHECK_ROCPROFILER(rocprofiler_query_tracer_operation_name(
tracer_record.domain, tracer_record.operation_id, &operation_name_c));
}
output_file_t* output_file = get_output_file(output_type_t::TRACER, tracer_record.domain);
*output_file << GetDomainName(tracer_record.domain);
if (tracer_record.domain == ACTIVITY_DOMAIN_ROCTX && tracer_record.external_id.id >= 0)
ss << "," << tracer_record.external_id.id;
if (tracer_record.domain == ACTIVITY_DOMAIN_ROCTX) {
if (roctx_message.size() > 1)
ss << ",\"" << roctx_message << "\"";
else
ss << ",";
}
if (operation_name_c) ss << ",\"" << operation_name_c << "\"";
if (tracer_record.name && tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
ss << ",\"" << rocprofiler::truncate_name(rocprofiler::cxx_demangle(tracer_record.name)) << "\"";
} else if (tracer_record.domain == ACTIVITY_DOMAIN_HIP_OPS) {
ss << ",";
}
if (tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
ss << "," << tracer_record.timestamps.begin.value << ","
<< tracer_record.timestamps.end.value;
ss << "," << tracer_record.correlation_id.value;
} else {
ss << "," << tracer_record.timestamps.begin.value;
}
ss << "\n";
*output_file << ss.str();
}
void FlushProfilerRecord(const rocprofiler_record_profiler_t* profiler_record,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
auto check_filter_cache = [&](const char * k_name) {
auto str = std::string{k_name};
auto f = kernel_filter_cache_.find(k_name);
if (f == kernel_filter_cache_.end()) {
bool found_match = false;
for (const auto& filter : kernel_filters_) {
if (str.find(filter) != std::string::npos) {
found_match = true;
break;
}
}
f = kernel_filter_cache_.emplace(str, found_match).first;
}
return f->second;
};
std::lock_guard<std::mutex> lock(writing_lock);
std::stringstream ss;
WriteHeader(output_type_t::COUNTER, ACTIVITY_DOMAIN_NUMBER);
size_t name_length = 0;
output_file_t* output_file{nullptr};
output_file = get_output_file(output_type_t::COUNTER);
CHECK_ROCPROFILER(rocprofiler_query_kernel_info_size(ROCPROFILER_KERNEL_NAME,
profiler_record->kernel_id, &name_length));
// Taken from rocprofiler: The size hasn't changed in recent past
static const uint32_t lds_block_size = 128 * 4;
const char* kernel_name_c = nullptr;
if (name_length > 1) {
CHECK_ROCPROFILER(rocprofiler_query_kernel_info(ROCPROFILER_KERNEL_NAME,
profiler_record->kernel_id, &kernel_name_c));
if (kernel_name_c != nullptr && !kernel_filters_.empty() && !check_filter_cache(kernel_name_c)) {
free(const_cast<char*>(kernel_name_c));
return;
}
}
if (!counter_header_written_) {
if (profiler_record->counters) {
if(is_individual_xcc_mode) ss << ',' << "XCC_Index";
for (uint64_t i = 0; i < profiler_record->counters_count.value; i++) {
auto counter_handler = profiler_record->counters[i].counter_handler;
if (!counter_handler.handle) continue;
size_t counter_name_length = 0;
const char* name_c = nullptr;
CHECK_ROCPROFILER(rocprofiler_query_counter_info_size(
session_id, ROCPROFILER_COUNTER_NAME, counter_handler, &counter_name_length));
if (counter_name_length == 0) continue;
CHECK_ROCPROFILER(rocprofiler_query_counter_info(session_id, ROCPROFILER_COUNTER_NAME,
counter_handler, &name_c));
ss << ',' << name_c;
}
ss << '\n';
}
counter_header_written_ = true;
ss << '\n';
}
ss << std::to_string(profiler_record->header.id.handle) << ","
<< std::to_string(profiler_record->gpu_id.handle) << ","
<< std::to_string(profiler_record->queue_id.handle) << "," << std::to_string(GetPid()) << ","
<< std::to_string(profiler_record->thread_id.value);
ss << "," << std::to_string(profiler_record->kernel_properties.grid_size) << ","
<< std::to_string(profiler_record->kernel_properties.workgroup_size) << ","
<< std::to_string(((profiler_record->kernel_properties.lds_size + (lds_block_size - 1)) &
~(lds_block_size - 1)))
<< "," << std::to_string(profiler_record->kernel_properties.scratch_size) << ","
<< std::to_string(profiler_record->kernel_properties.arch_vgpr_count) << ","
<< std::to_string(profiler_record->kernel_properties.accum_vgpr_count) << ","
<< std::to_string(profiler_record->kernel_properties.sgpr_count) << ","
<< std::to_string(profiler_record->kernel_properties.wave_size);
std::string kernel_name = "";
if (name_length > 1) {
kernel_name = rocprofiler::truncate_name(rocprofiler::cxx_demangle(kernel_name_c));
std::string key = "\"";
std::size_t found = kernel_name.rfind(key);
while (found != std::string::npos) {
kernel_name.replace(found, key.length(), "'");
found = kernel_name.rfind(key, found - 1);
}
}
ss << ",\"" << kernel_name << "\"," << std::to_string(profiler_record->timestamps.begin.value)
<< "," << std::to_string(profiler_record->timestamps.end.value) << ","
<< std::to_string(profiler_record->correlation_id.value);
// For Counters
if (profiler_record->counters) {
if(is_individual_xcc_mode) ss << "," << profiler_record->xcc_index;
for (uint64_t i = 0; i < profiler_record->counters_count.value; i++) {
if (profiler_record->counters[i].counter_handler.handle > 0) {
ss << "," << std::to_string(profiler_record->counters[i].value.value);
}
}
}
ss << '\n';
*output_file << ss.str();
if (kernel_name_c) {
free(const_cast<char*>(kernel_name_c));
}
}
void FlushPCSamplingRecord(const rocprofiler_record_pc_sample_t* pc_sampling_record) {
WriteHeader(output_type_t::PC_SAMPLING, ACTIVITY_DOMAIN_NUMBER);
std::stringstream ss;
output_file_t* output_file{nullptr};
output_file = get_output_file(output_type_t::PC_SAMPLING);
const auto& sample = pc_sampling_record->pc_sample;
ss << sample.dispatch_id.value << "," << sample.timestamp.value << "," << sample.gpu_id.handle
<< "," << std::hex << std::showbase << sample.pc << "," << sample.se << "\n";
*output_file << ss.str();
}
int WriteBufferRecords(const rocprofiler_record_header_t* begin,
const rocprofiler_record_header_t* end,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
while (begin < end) {
if (!begin) return 0;
switch (begin->kind) {
case ROCPROFILER_PROFILER_RECORD: {
const rocprofiler_record_profiler_t* profiler_record =
reinterpret_cast<const rocprofiler_record_profiler_t*>(begin);
FlushProfilerRecord(profiler_record, session_id, buffer_id);
break;
}
case ROCPROFILER_TRACER_RECORD: {
rocprofiler_record_tracer_t* tracer_record = const_cast<rocprofiler_record_tracer_t*>(
reinterpret_cast<const rocprofiler_record_tracer_t*>(begin));
FlushTracerRecord(*tracer_record, session_id, buffer_id);
break;
}
case ROCPROFILER_ATT_TRACER_RECORD: {
break;
}
case ROCPROFILER_PC_SAMPLING_RECORD: {
[[deprecated("PC Sampling is deprecated")]]
const rocprofiler_record_pc_sample_t* pc_sampling_record =
reinterpret_cast<const rocprofiler_record_pc_sample_t*>(begin);
FlushPCSamplingRecord(pc_sampling_record);
break;
}
default:
break;
}
rocprofiler_next_record(begin, &begin, session_id, buffer_id);
}
return 0;
}
bool is_valid() const { return valid_; }
private:
bool valid_{false};
bool counter_header_written_ = false;
bool is_individual_xcc_mode=false;
std::vector<std::string> counter_names_;
std::set<std::string> kernel_filters_;
std::map<std::string, bool> kernel_filter_cache_;
std::atomic<bool> roctx_header_written_{false}, hsa_api_header_written_{false},
hip_api_header_written_{false}, hip_activity_header_written_{false},
hsa_async_copy_header_written_{false}, pc_sample_header_written_{false},
kernel_dispatches_header_written_{false};
output_file_t roctx_file_{"roctx_trace"}, hsa_api_file_{"hsa_api_trace"},
hip_api_file_{"hip_api_trace"}, hip_activity_file_{"hcc_ops_trace"},
hsa_async_copy_file_{"async_copy_trace"}, pc_sample_file_{"pcs_trace"},
output_file_{"results"};
};
file_plugin_t* file_plugin = nullptr;
} // namespace
ROCPROFILER_EXPORT int rocprofiler_plugin_initialize(uint32_t rocprofiler_major_version,
uint32_t rocprofiler_minor_version,
void* data) {
if (rocprofiler_major_version != ROCPROFILER_VERSION_MAJOR ||
rocprofiler_minor_version < ROCPROFILER_VERSION_MINOR)
return -1;
if (file_plugin != nullptr) return -1;
file_plugin = new file_plugin_t(data);
if (file_plugin->is_valid()) return 0;
// The plugin failed to initialized, destroy it and return an error.
delete file_plugin;
file_plugin = nullptr;
return -1;
}
ROCPROFILER_EXPORT void rocprofiler_plugin_finalize() {
if (!file_plugin) return;
delete file_plugin;
file_plugin = nullptr;
}
ROCPROFILER_EXPORT int rocprofiler_plugin_write_buffer_records(
const rocprofiler_record_header_t* begin, const rocprofiler_record_header_t* end,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
if (!file_plugin || !file_plugin->is_valid()) return -1;
return file_plugin->WriteBufferRecords(begin, end, session_id, buffer_id);
}
ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(rocprofiler_record_tracer_t record) {
if (!file_plugin || !file_plugin->is_valid()) return -1;
if (record.header.id.handle == 0) return 0;
file_plugin->FlushTracerRecord(record, rocprofiler_session_id_t{0}, rocprofiler_buffer_id_t{0});
return 0;
}
@@ -0,0 +1,516 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
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 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 <cxxabi.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <atomic>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <memory>
#include <optional>
#include <ostream>
#include <sstream>
#include <string>
#include <hsa/hsa.h>
#include <mutex>
#include <unordered_map>
#include "rocprofiler.h"
#include "rocprofiler_plugin.h"
#include "../utils.h"
#include "src/utils/filesystem.hpp"
namespace fs = rocprofiler::common::filesystem;
namespace {
std::vector<std::string> GetCounterNames() {
std::vector<std::string> counters;
const char* line_c_str = getenv("ROCPROFILER_COUNTERS");
if (line_c_str) {
std::string line = line_c_str;
// skip commented lines
auto found = line.find_first_not_of(" \t");
if (found != std::string::npos) {
if (line[found] == '#') return {};
}
if (line.find("pmc") == std::string::npos) return counters;
char seperator = ' ';
std::string::size_type prev_pos = 0, pos = line.find(seperator, prev_pos);
prev_pos = ++pos;
if (pos != std::string::npos) {
while ((pos = line.find(seperator, pos)) != std::string::npos) {
std::string substring(line.substr(prev_pos, pos - prev_pos));
if (substring.length() > 0 && substring != ":") {
counters.push_back(substring);
}
prev_pos = ++pos;
}
if (!line.substr(prev_pos, pos - prev_pos).empty()) {
counters.push_back(line.substr(prev_pos, pos - prev_pos));
}
}
}
return counters;
}
class file_plugin_t {
private:
enum class output_type_t { COUNTER, TRACER, PC_SAMPLING };
class output_file_t {
public:
output_file_t(std::string name, bool bOpenOnInit = false) : name_(std::move(name)) {
if (bOpenOnInit) open();
}
std::string name() const { return name_; }
template <typename T> std::ostream& operator<<(T&& value) {
if (!is_open()) open();
return stream_ << std::forward<T>(value);
}
std::ostream& operator<<(std::ostream& (*func)(std::ostream&)) {
if (!is_open()) open();
return stream_ << func;
}
void open() {
// If the stream is already in the failed state, there's no need to try
// to open the file.
if (fail()) return;
const char* output_dir = getenv("OUTPUT_PATH");
std::string output_file_name = getenv("OUT_FILE_NAME") ? getenv("OUT_FILE_NAME") : "";
if (output_dir == nullptr && getenv("OUT_FILE_NAME") == nullptr) {
stream_.copyfmt(std::cout);
stream_.clear(std::cout.rdstate());
stream_.basic_ios<char>::rdbuf(std::cout.rdbuf());
bPrintToStdout = true;
return;
}
if (output_dir == nullptr) output_dir = ".";
fs::path output_prefix(output_dir);
if (!fs::is_directory(fs::status(output_prefix))) {
if (!stream_.fail()) rocprofiler::warning("Cannot open output directory '%s'", output_dir);
stream_.setstate(std::ios_base::failbit);
return;
}
output_file_name = replace_MPI_macros(output_file_name);
std::stringstream ss;
ss << name_ << "_" << ((output_file_name.empty()) ? std::to_string(GetPid()) : "")
<< output_file_name << ".csv";
std::cout << "Results File: " << output_prefix / ss.str() << std::endl;
stream_.open(output_prefix / ss.str());
}
bool is_open() const { return stream_.is_open(); }
bool fail() const { return stream_.fail(); }
bool isStdOut() const { return bPrintToStdout; }
// Returns a string with the MPI %macro replaced with the corresponding envvar
std::string replace_MPI_macros(std::string output_file_name) {
std::unordered_map<const char*, const char*> MPI_BUILTINS = {
{"MPI_RANK", "%rank"},
{"OMPI_COMM_WORLD_RANK", "%rank"},
{"MV2_COMM_WORLD_RANK", "%rank"}};
for (const auto& [envvar, key] : MPI_BUILTINS) {
size_t key_find = output_file_name.rfind(key);
if (key_find == std::string::npos) continue; // Does not contain a %?rank var
const char* env_var_set = getenv(envvar);
if (env_var_set == nullptr) continue; // MPI_COMM_WORLD_x var is does not exist
int rank = atoi(env_var_set);
output_file_name = output_file_name.substr(0, key_find) + std::to_string(rank) +
output_file_name.substr(key_find + std::string(key).size());
}
return output_file_name;
}
private:
const std::string name_;
std::ofstream stream_;
bool bPrintToStdout = false;
};
output_file_t* get_output_file(output_type_t output_type, uint32_t domain = 0) {
switch (output_type) {
case output_type_t::COUNTER:
return &output_file_;
case output_type_t::TRACER:
switch (domain) {
case ACTIVITY_DOMAIN_ROCTX:
return &roctx_file_;
case ACTIVITY_DOMAIN_HSA_API:
return &hsa_api_file_;
case ACTIVITY_DOMAIN_HIP_API:
return &hip_api_file_;
case ACTIVITY_DOMAIN_HIP_OPS:
return &hip_activity_file_;
case ACTIVITY_DOMAIN_HSA_OPS:
return &hsa_async_copy_file_;
default:
assert(!"domain/op not supported!");
break;
}
break;
case output_type_t::PC_SAMPLING:
return &pc_sample_file_;
}
return nullptr;
}
public:
file_plugin_t(void* data) {
if (data) counter_names_ = GetCounterNames();
valid_ = true;
}
void WriteHeader(output_type_t type, rocprofiler_tracer_activity_domain_t domain) {
output_file_t* output_file;
switch (domain) {
case ACTIVITY_DOMAIN_HSA_API: {
if (hsa_api_header_written_.load(std::memory_order_relaxed)) return;
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HSA_API);
*output_file << "Domain,Function,Start_Timestamp,End_Timestamp,Correlation_ID" << std::endl;
*output_file << std::endl;
hsa_api_header_written_.exchange(true, std::memory_order_release);
return;
}
case ACTIVITY_DOMAIN_HIP_API: {
if (hip_api_header_written_.load(std::memory_order_relaxed)) return;
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HIP_API);
*output_file << "Domain,Function,Start_Timestamp,End_Timestamp,Correlation_ID" << std::endl;
*output_file << std::endl;
hip_api_header_written_.exchange(true, std::memory_order_release);
return;
}
case ACTIVITY_DOMAIN_ROCTX: {
if (roctx_header_written_.load(std::memory_order_relaxed)) return;
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_ROCTX);
*output_file << "Domain,ROCTX_ID,Message,Timestamp" << std::endl;
*output_file << std::endl;
roctx_header_written_.exchange(true, std::memory_order_release);
return;
}
case ACTIVITY_DOMAIN_HSA_OPS: {
if (hsa_async_copy_header_written_.load(std::memory_order_relaxed)) return;
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HSA_OPS);
*output_file << "Domain,Operation,Start_Timestamp,Stop_Timestamp,Correlation_ID"
<< std::endl;
*output_file << std::endl;
hsa_async_copy_header_written_.exchange(true, std::memory_order_release);
return;
}
case ACTIVITY_DOMAIN_HIP_OPS: {
if (hip_activity_header_written_.load(std::memory_order_relaxed)) return;
output_file = get_output_file(output_type_t::TRACER, ACTIVITY_DOMAIN_HIP_OPS);
*output_file << "Domain,Operation,Kernel_Name,Start_Timestamp,Stop_Timestamp,"
"Correlation_ID"
<< std::endl;
*output_file << std::endl;
hip_activity_header_written_.exchange(true, std::memory_order_release);
return;
}
default: {
if (type == output_type_t::COUNTER) {
if (kernel_dispatches_header_written_.load(std::memory_order_relaxed)) return;
output_file = get_output_file(output_type_t::COUNTER);
*output_file << "Index,KernelName,gpu-id,queue-id,queue-index,pid,tid,grd,wgr,lds,scr,"
"arch_vgpr,accum_vgpr,sgpr,wave_size";
if (counter_names_.size() > 0) {
for (uint32_t i = 0; i < counter_names_.size(); i++)
*output_file << "," << counter_names_[i];
}
*output_file << ",DispatchNs,BeginNs,EndNs,CompleteNs";
*output_file << std::endl;
*output_file << std::endl;
kernel_dispatches_header_written_.exchange(true, std::memory_order_release);
return;
} else if (type == output_type_t::PC_SAMPLING) {
if (pc_sample_header_written_.load(std::memory_order_relaxed)) return;
output_file = get_output_file(output_type_t::PC_SAMPLING);
*output_file << "Dispatch_ID,Timestamp,GPU_ID,PC_Sample,Shader_Engines" << std::endl;
*output_file << std::endl;
pc_sample_header_written_.exchange(true, std::memory_order_release);
return;
}
return;
}
}
}
std::mutex writing_lock;
const char* GetDomainName(rocprofiler_tracer_activity_domain_t domain) {
switch (domain) {
case ACTIVITY_DOMAIN_ROCTX:
return "ROCTX_DOMAIN";
break;
case ACTIVITY_DOMAIN_HIP_API:
return "HIP_API_DOMAIN";
break;
case ACTIVITY_DOMAIN_HIP_OPS:
return "HIP_OPS_DOMAIN";
break;
case ACTIVITY_DOMAIN_HSA_API:
return "HSA_API_DOMAIN";
break;
case ACTIVITY_DOMAIN_HSA_OPS:
return "HSA_OPS_DOMAIN";
break;
case ACTIVITY_DOMAIN_HSA_EVT:
return "HSA_EVT_DOMAIN";
break;
default:
return "";
}
}
void FlushTracerRecord(rocprofiler_record_tracer_t tracer_record,
rocprofiler_session_id_t session_id,
rocprofiler_buffer_id_t buffer_id = rocprofiler_buffer_id_t{0}) {
std::lock_guard<std::mutex> lock(writing_lock);
if (tracer_record.timestamps.end.value <= 0 && tracer_record.domain != ACTIVITY_DOMAIN_ROCTX)
return;
WriteHeader(output_type_t::TRACER, tracer_record.domain);
std::string roctx_message;
if (tracer_record.domain == ACTIVITY_DOMAIN_ROCTX && tracer_record.name) {
roctx_message = tracer_record.name;
}
const char* operation_name_c = nullptr;
// ROCTX domain Operation ID doesn't have a name
// It depends on the user input of the roctx functions.
// ROCTX message is the tracer_record.name
if (tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
CHECK_ROCPROFILER(rocprofiler_query_tracer_operation_name(
tracer_record.domain, tracer_record.operation_id, &operation_name_c));
}
output_file_t* output_file = get_output_file(output_type_t::TRACER, tracer_record.domain);
*output_file << GetDomainName(tracer_record.domain);
if (tracer_record.domain == ACTIVITY_DOMAIN_ROCTX && tracer_record.external_id.id >= 0)
*output_file << "," << tracer_record.external_id.id;
if (tracer_record.domain == ACTIVITY_DOMAIN_ROCTX) {
if (roctx_message.size() > 1)
*output_file << ",\"" << roctx_message << "\"";
else
*output_file << ",";
}
if (operation_name_c) *output_file << ",\"" << operation_name_c << "\"";
if (tracer_record.name && tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
*output_file << ",\"" << rocprofiler::truncate_name(rocprofiler::cxx_demangle(tracer_record.name)) << "\"";
} else if (tracer_record.domain == ACTIVITY_DOMAIN_HIP_OPS) {
*output_file << ",";
}
if (tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
*output_file << "," << tracer_record.timestamps.begin.value << ","
<< tracer_record.timestamps.end.value;
*output_file << "," << tracer_record.correlation_id.value;
} else {
*output_file << "," << tracer_record.timestamps.begin.value;
}
*output_file << std::endl;
}
void FlushProfilerRecord(const rocprofiler_record_profiler_t* profiler_record,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
std::lock_guard<std::mutex> lock(writing_lock);
WriteHeader(output_type_t::COUNTER, ACTIVITY_DOMAIN_NUMBER);
size_t name_length = 0;
output_file_t* output_file{nullptr};
output_file = get_output_file(output_type_t::COUNTER);
CHECK_ROCPROFILER(rocprofiler_query_kernel_info_size(ROCPROFILER_KERNEL_NAME,
profiler_record->kernel_id, &name_length));
// Taken from rocprofiler: The size hasn't changed in recent past
static const uint32_t lds_block_size = 128 * 4;
const char* kernel_name_c = nullptr;
if (name_length > 1) {
CHECK_ROCPROFILER(rocprofiler_query_kernel_info(ROCPROFILER_KERNEL_NAME,
profiler_record->kernel_id, &kernel_name_c));
}
*output_file << std::to_string(profiler_record->header.id.handle) << ",";
std::string kernel_name = "";
if (name_length > 1) {
kernel_name = rocprofiler::truncate_name(rocprofiler::cxx_demangle(kernel_name_c));
std::string key = "\"";
std::size_t found = kernel_name.rfind(key);
while (found != std::string::npos) {
kernel_name.replace(found, key.length(), "'");
found = kernel_name.rfind(key, found - 1);
}
}
*output_file << "\"" << kernel_name << "\",";
*output_file << std::to_string(profiler_record->gpu_id.handle) << ","
<< std::to_string(profiler_record->queue_id.handle) << ","
<< std::to_string(profiler_record->queue_idx.value) << ","
<< std::to_string(GetPid()) << ","
<< std::to_string(profiler_record->thread_id.value) << ","
<< std::to_string(profiler_record->kernel_properties.grid_size) << ","
<< std::to_string(profiler_record->kernel_properties.workgroup_size) << ","
<< std::to_string(
((profiler_record->kernel_properties.lds_size + (lds_block_size - 1)) &
~(lds_block_size - 1)))
<< "," << std::to_string(profiler_record->kernel_properties.scratch_size) << ","
<< std::to_string(profiler_record->kernel_properties.arch_vgpr_count) << ","
<< std::to_string(profiler_record->kernel_properties.accum_vgpr_count) << ","
<< std::to_string(profiler_record->kernel_properties.sgpr_count) << ","
<< std::to_string(profiler_record->kernel_properties.wave_size);
// For Counters
if (profiler_record->counters) {
for (uint64_t i = 0; i < profiler_record->counters_count.value; i++) {
if (profiler_record->counters[i].counter_handler.handle > 0) {
*output_file << "," << std::to_string(profiler_record->counters[i].value.value);
}
}
}
*output_file << ",0,"
<< std::to_string(profiler_record->timestamps.begin.value) << ","
<< std::to_string(profiler_record->timestamps.end.value) << ",0";
*output_file << '\n';
if (kernel_name_c) {
free(const_cast<char*>(kernel_name_c));
}
}
void FlushPCSamplingRecord(const rocprofiler_record_pc_sample_t* pc_sampling_record) {
WriteHeader(output_type_t::PC_SAMPLING, ACTIVITY_DOMAIN_NUMBER);
output_file_t* output_file{nullptr};
output_file = get_output_file(output_type_t::PC_SAMPLING);
const auto& sample = pc_sampling_record->pc_sample;
*output_file << sample.dispatch_id.value << "," << sample.timestamp.value << ","
<< sample.gpu_id.handle << "," << std::hex << std::showbase << sample.pc << ","
<< sample.se << std::endl;
}
int WriteBufferRecords(const rocprofiler_record_header_t* begin,
const rocprofiler_record_header_t* end,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
while (begin < end) {
if (!begin) return 0;
switch (begin->kind) {
case ROCPROFILER_PROFILER_RECORD: {
const rocprofiler_record_profiler_t* profiler_record =
reinterpret_cast<const rocprofiler_record_profiler_t*>(begin);
FlushProfilerRecord(profiler_record, session_id, buffer_id);
break;
}
case ROCPROFILER_TRACER_RECORD: {
rocprofiler_record_tracer_t* tracer_record = const_cast<rocprofiler_record_tracer_t*>(
reinterpret_cast<const rocprofiler_record_tracer_t*>(begin));
FlushTracerRecord(*tracer_record, session_id, buffer_id);
break;
}
case ROCPROFILER_ATT_TRACER_RECORD: {
break;
}
case ROCPROFILER_PC_SAMPLING_RECORD: {
[[deprecated("PC Sampling is deprecated")]]
const rocprofiler_record_pc_sample_t* pc_sampling_record =
reinterpret_cast<const rocprofiler_record_pc_sample_t*>(begin);
FlushPCSamplingRecord(pc_sampling_record);
break;
}
default:
break;
}
rocprofiler_next_record(begin, &begin, session_id, buffer_id);
}
return 0;
}
bool is_valid() const { return valid_; }
private:
bool valid_{false};
std::vector<std::string> counter_names_;
std::atomic<bool> roctx_header_written_{false}, hsa_api_header_written_{false},
hip_api_header_written_{false}, hip_activity_header_written_{false},
hsa_async_copy_header_written_{false}, pc_sample_header_written_{false},
kernel_dispatches_header_written_{false};
output_file_t roctx_file_{"roctx_trace"}, hsa_api_file_{"hsa_api_trace"},
hip_api_file_{"hip_api_trace"}, hip_activity_file_{"hcc_ops_trace"},
hsa_async_copy_file_{"async_copy_trace"}, pc_sample_file_{"pcs_trace"},
output_file_{"results"};
};
file_plugin_t* file_plugin = nullptr;
} // namespace
ROCPROFILER_EXPORT int rocprofiler_plugin_initialize(uint32_t rocprofiler_major_version,
uint32_t rocprofiler_minor_version,
void* data) {
if (rocprofiler_major_version != ROCPROFILER_VERSION_MAJOR ||
rocprofiler_minor_version < ROCPROFILER_VERSION_MINOR)
return -1;
if (file_plugin != nullptr) return -1;
file_plugin = new file_plugin_t(data);
if (file_plugin->is_valid()) return 0;
// The plugin failed to initialized, destroy it and return an error.
delete file_plugin;
file_plugin = nullptr;
return -1;
}
ROCPROFILER_EXPORT void rocprofiler_plugin_finalize() {
if (!file_plugin) return;
delete file_plugin;
file_plugin = nullptr;
}
ROCPROFILER_EXPORT int rocprofiler_plugin_write_buffer_records(
const rocprofiler_record_header_t* begin, const rocprofiler_record_header_t* end,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
if (!file_plugin || !file_plugin->is_valid()) return -1;
return file_plugin->WriteBufferRecords(begin, end, session_id, buffer_id);
}
ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(rocprofiler_record_tracer_t record) {
if (!file_plugin || !file_plugin->is_valid()) return -1;
if (record.header.id.handle == 0) return 0;
file_plugin->FlushTracerRecord(record, rocprofiler_session_id_t{0}, rocprofiler_buffer_id_t{0});
return 0;
}
@@ -0,0 +1,30 @@
include_directories(json/include)
file(GLOB ROCPROFILER_UTIL_SRC_FILES ${PROJECT_SOURCE_DIR}/src/utils/helper.cpp)
file(GLOB SPEEDSCOPE_HEADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/json/include/nlohmann/json.hpp)
add_library(json_plugin ${LIBRARY_TYPE} ${ROCPROFILER_UTIL_SRC_FILES}
${SPEEDSCOPE_HEADER_FILES} json.cpp)
set_target_properties(
json_plugin
PROPERTIES CXX_VISIBILITY_PRESET hidden
LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib/rocprofiler
INSTALL_RPATH "${ROCM_APPEND_PRIVLIB_RPATH}")
target_compile_definitions(json_plugin PRIVATE HIP_PROF_HIP_API_STRING=1
__HIP_PLATFORM_AMD__=1)
target_include_directories(json_plugin PRIVATE ${PROJECT_SOURCE_DIR})
target_link_options(
json_plugin PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
-Wl,--no-undefined)
target_link_libraries(json_plugin PRIVATE rocprofiler-v2 Threads::Threads stdc++fs
amd_comgr)
install(TARGETS json_plugin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}
COMPONENT plugins)
+765
View File
@@ -0,0 +1,765 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc. All rights reserved.
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 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 "rocprofiler.h"
#include <atomic>
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <string>
#include <iostream>
#include <mutex>
#include <string_view>
#include <utility>
#include <thread>
#include <unordered_set>
#include <cxxabi.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include "json/include/nlohmann/json.hpp"
#include "rocprofiler_plugin.h"
#include "../utils.h"
#include "src/utils/filesystem.hpp"
namespace fs = rocprofiler::common::filesystem;
namespace {
struct TraceCategoryArgs {
std::string name;
};
struct TraceActivity {
std::string phase;
uint64_t category;
std::string name;
std::string category_str;
std::string timestamp;
std::string duration;
uint64_t thread_id;
uint64_t correlation_id;
};
struct TraceCategory {
TraceCategoryArgs args;
std::string phase;
uint64_t category;
int sort_index;
};
enum TraceFlowCategory {
ROCPROFILER_DATA_FLOW_START = 0,
ROCPROFILER_DATA_FLOW_END = 1,
};
class TraceFlow {
public:
TraceFlow(uint64_t timestamp, std::string category_str, uint64_t category, uint64_t thread_id,
uint64_t id);
uint64_t getTimestamp();
std::string getCategoryStr();
uint64_t getID();
uint64_t getCategory();
uint64_t getThreadID();
void setTimestamp(uint64_t timestamp);
void setID(uint64_t id);
private:
uint64_t timestamp_ = 0;
std::string category_str_;
uint64_t id_;
uint64_t category_;
uint64_t thread_id_;
};
TraceFlow::TraceFlow(uint64_t timestamp, std::string category_str, uint64_t category,
uint64_t thread_id, uint64_t id)
: timestamp_(timestamp),
category_str_(std::move(category_str)),
category_(category),
thread_id_(thread_id),
id_(id) {}
uint64_t TraceFlow::getTimestamp() { return timestamp_; }
std::string TraceFlow::getCategoryStr() { return category_str_; }
uint64_t TraceFlow::getID() { return id_; }
uint64_t TraceFlow::getCategory() { return category_; }
uint64_t TraceFlow::getThreadID() { return thread_id_; }
void TraceFlow::setTimestamp(uint64_t timestamp) { timestamp_ = timestamp; }
void TraceFlow::setID(uint64_t id) { id_ = id; }
std::mutex writing_lock{};
std::string process_name;
std::string get_kernel_name(rocprofiler_record_profiler_t& profiler_record) {
std::string kernel_name = "";
size_t name_length = 1;
CHECK_ROCPROFILER(rocprofiler_query_kernel_info_size(ROCPROFILER_KERNEL_NAME,
profiler_record.kernel_id, &name_length));
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#pragma GCC diagnostic ignored "-Wstringop-overread"
if (name_length > 1) {
const char* kernel_name_c = nullptr;
CHECK_ROCPROFILER(rocprofiler_query_kernel_info(ROCPROFILER_KERNEL_NAME,
profiler_record.kernel_id, &kernel_name_c));
if (kernel_name_c && strlen(kernel_name_c) > 1)
kernel_name = rocprofiler::truncate_name(rocprofiler::cxx_demangle(kernel_name_c));
}
#pragma GCC diagnostic pop
return kernel_name;
}
class json_plugin_t {
public:
json_plugin_t() {
is_valid_ = true;
const char* rocprofiler_trace_period = getenv("ROCPROFILER_TRACE_PERIOD");
if (rocprofiler_trace_period) trace_period_enabled_ = true;
const char* enable_data_flow_str = getenv("ROCPROFILER_DISABLE_JSON_DATA_FLOWS");
if (enable_data_flow_str) {
if (std::string_view(enable_data_flow_str).find("ON") != std::string::npos) {
enable_data_flow_ = false;
}
if (std::string_view(enable_data_flow_str).find("1") != std::string::npos) {
enable_data_flow_ = false;
}
if (std::string_view(enable_data_flow_str).find("OFF") != std::string::npos) {
enable_data_flow_ = true;
}
if (std::string_view(enable_data_flow_str).find("0") != std::string::npos) {
enable_data_flow_ = true;
}
}
const char* output_dir = getenv("OUTPUT_PATH");
const char* temp_file_name = getenv("OUT_FILE_NAME");
std::string output_file_name = temp_file_name ? std::string(temp_file_name) + "_" : "";
if (output_dir == nullptr) output_dir = "./";
output_prefix_ = output_dir;
if (!fs::is_directory(fs::status(output_prefix_))) {
if (!stream_.fail()) rocprofiler::warning("Cannot open output directory '%s'", output_dir);
stream_.setstate(std::ios_base::failbit);
return;
}
txt_output_prefix_ = output_prefix_;
output_prefix_.append(output_file_name + std::to_string(GetPid()) + "_output.json");
txt_output_prefix_.append(output_file_name + std::to_string(GetPid()) + "_output");
// file.open(output_prefix_.string());
// This flush technique won't work we need to join JSON files
// flush_thread_check_.exchange(true, std::memory_order_acquire);
// char* flush_interval = getenv("ROCPROFILER_FLUSH_INTERVAL");
// uint64_t rocprofiler_flush_interval = 10;
// char* end;
// if (flush_interval) rocprofiler_flush_interval = std::stoi(flush_interval);
// flush_thread_ = std::thread(
// [&](uint64_t rocprofiler_flush_interval) {
// while (flush_thread_check_.load(std::memory_order_acquire)) {
// writing_lock.lock();
// ExportTraceEventsToJSON();
// writing_lock.unlock();
// usleep(rocprofiler_flush_interval);
// }
// },
// rocprofiler_flush_interval);
}
void delete_json_plugin() {
if (is_valid_) {
// flush_thread_check_.exchange(false, std::memory_order_acquire);
// flush_thread_.join();
ExportTraceEventsToJSON(output_prefix_.string());
const char* flame_graph_env = getenv("ROCPROFILER_ENABLE_FLAME_GRAPH");
if (flame_graph_env &&
(std::string_view(flame_graph_env).find("1") != std::string::npos ||
std::string_view(flame_graph_env).find("ON") != std::string::npos))
ExportTraceEventsForFlameGraph(txt_output_prefix_.string());
// if (file.is_open()) {
// file.close();
// }
}
}
void ExportTraceEventsToJSON(const std::string& path) {
nlohmann::json j;
j["traceEvents"] = nlohmann::json::array();
if (!trace_categories_check.load(std::memory_order_acquire)) {
for (const auto& event : trace_categories) {
nlohmann::json args;
args["name"] = event.args.name;
j["traceEvents"].push_back({{"args", args},
{"ph", event.phase},
{"pid", event.category},
{"name", "process_name"},
{"sort_index", event.sort_index}});
}
trace_categories_check.exchange(true, std::memory_order_acquire);
}
std::sort(trace_copy_activities_.begin(), trace_copy_activities_.end(),
[](auto& a, auto& b) { return (a.timestamp < b.timestamp); });
std::sort(trace_events_.begin(), trace_events_.end(),
[](auto& a, auto& b) { return (a.timestamp < b.timestamp); });
std::sort(trace_gpu_activities_.begin(), trace_gpu_activities_.end(),
[](auto& a, auto& b) { return (a.timestamp < b.timestamp); });
std::sort(trace_unknown_activities_.begin(), trace_unknown_activities_.end(),
[](auto& a, auto& b) { return (a.timestamp < b.timestamp); });
for (const auto& event : trace_copy_activities_) {
nlohmann::json args;
args["cid"] = event.correlation_id;
j["traceEvents"].push_back({{"name", event.name},
{"ph", event.phase},
{"ts", event.timestamp},
{"tid", event.thread_id},
{"pid", event.category},
{"dur", event.duration},
{"cat", event.category_str},
{"args", args}});
}
for (const auto& event : trace_unknown_activities_) {
nlohmann::json args;
args["cid"] = event.correlation_id;
j["traceEvents"].push_back({{"name", event.name},
{"ph", event.phase},
{"ts", event.timestamp},
{"tid", event.thread_id},
{"pid", event.category},
{"dur", event.duration},
{"cat", event.category_str},
{"args", args}});
}
for (const auto& event : trace_events_) {
nlohmann::json args;
args["cid"] = event.correlation_id;
j["traceEvents"].push_back({{"name", event.name},
{"ph", event.phase},
{"ts", event.timestamp},
{"tid", event.thread_id},
{"pid", event.category},
{"dur", event.duration},
{"cat", event.category_str},
{"args", args}});
}
for (const auto& event : trace_gpu_activities_) {
nlohmann::json args;
args["cid"] = event.correlation_id;
j["traceEvents"].push_back({{"name", event.name},
{"ph", event.phase},
{"ts", event.timestamp},
{"tid", event.thread_id},
{"pid", event.category},
{"dur", event.duration},
{"cat", event.category_str},
{"args", args}});
}
if (enable_data_flow_) {
for (auto data_flow : trace_data_flows_) {
if (data_flow.second.size() > 1) {
uint64_t id = trace_flow_counter.fetch_add(1, std::memory_order_acquire);
// The following workaround to overcome the timestamp clock issues in Mi300X
std::string df0 = "s";
std::string df1 = "t";
if (data_flow.second[0].getTimestamp() > data_flow.second[1].getTimestamp()) {
df1 = "s";
df0 = "t";
}
j["traceEvents"].push_back({{"id", id},
{"ph", df0},
{"ts", data_flow.second[0].getTimestamp()},
{"cat", "DataFlow"},
{"pid", data_flow.second[0].getCategory()},
{"tid", data_flow.second[0].getThreadID()},
{"name", "dep"}});
j["traceEvents"].push_back({{"id", id},
{"ph", df1},
{"ts", data_flow.second[1].getTimestamp()},
{"cat", "DataFlow"},
{"pid", data_flow.second[1].getCategory()},
{"tid", data_flow.second[1].getThreadID()},
{"name", "dep"}});
}
}
}
std::ofstream file(path);
if (file.is_open()) {
file << j.dump(2) << std::endl; // Pretty print with 4 spaces
file.close();
}
// trace_gpu_activities_.clear();
// trace_copy_activities_.clear();
// trace_events_.clear();
// trace_data_flows_.clear();
}
void ExportTraceEventsForFlameGraph(const std::string& file_path) {
uint64_t sample_rate = 10;
const char* flame_graph_sample_rate_env = getenv("ROCPROFILER_FLAME_GRAPH_SAMPLE_RATE");
if (flame_graph_sample_rate_env) sample_rate = std::stoull(flame_graph_sample_rate_env);
std::thread kernels_graph = std::thread([&]() {
const char* flame_graph_enable_kernels = getenv("ROCPROFILER_FLAME_GRAPH_ENABLE_KERNELS");
if (flame_graph_enable_kernels &&
(std::string_view(flame_graph_enable_kernels).find("0") != std::string::npos ||
std::string_view(flame_graph_enable_kernels).find("OFF") != std::string::npos))
return;
uint64_t kernels_sample_rate = sample_rate;
const char* flame_graph_sample_rate_kernels_env =
getenv("ROCPROFILER_FLAME_GRAPH_KERNELS_SAMPLE_RATE");
if (flame_graph_sample_rate_kernels_env)
kernels_sample_rate = std::stoull(flame_graph_sample_rate_kernels_env);
std::ofstream kernels_file(file_path + "_kernels.txt");
if (!kernels_file.is_open()) {
std::cerr << "Failed to open file for writing: " << file_path << std::endl;
return;
}
for (const auto& event : trace_gpu_activities_) {
// Convert duration to sample count (for simplicity, assume 1 sample per microsecond)
uint64_t duration = std::stoul(event.duration);
for (uint64_t i = 0; i < duration; i += kernels_sample_rate) {
kernels_file << event.name << ";" << event.name << i << " " << kernels_sample_rate
<< "\n"; // Simple example
}
}
kernels_file.close();
});
std::thread copy_graph = std::thread([&]() {
const char* flame_graph_enable_copy = getenv("ROCPROFILER_FLAME_GRAPH_ENABLE_MEM_COPY");
if (flame_graph_enable_copy &&
(std::string_view(flame_graph_enable_copy).find("0") != std::string::npos ||
std::string_view(flame_graph_enable_copy).find("OFF") != std::string::npos))
return;
uint64_t copy_sample_rate = sample_rate;
const char* flame_graph_sample_rate_copy_env =
getenv("ROCPROFILER_FLAME_GRAPH_MEM_COPY_SAMPLE_RATE");
if (flame_graph_sample_rate_copy_env)
copy_sample_rate = std::stoull(flame_graph_sample_rate_copy_env);
std::ofstream copy_file(file_path + "_mem_copies.txt");
if (!copy_file.is_open()) {
std::cerr << "Failed to open file for writing: " << file_path << std::endl;
return;
}
for (const auto& event : trace_copy_activities_) {
uint64_t duration = std::stoul(event.duration);
for (uint64_t i = 0; i < duration; i += copy_sample_rate) {
copy_file << event.name << ";" << event.name << i << " " << copy_sample_rate << "\n";
}
}
copy_file.close();
});
std::thread api_graph = std::thread([&]() {
const char* flame_graph_enable_api = getenv("ROCPROFILER_FLAME_GRAPH_ENABLE_API");
if (flame_graph_enable_api &&
(std::string_view(flame_graph_enable_api).find("0") != std::string::npos ||
std::string_view(flame_graph_enable_api).find("OFF") != std::string::npos))
return;
uint64_t api_sample_rate = sample_rate;
const char* flame_graph_sample_rate_api_env =
getenv("ROCPROFILER_FLAME_GRAPH_API_SAMPLE_RATE");
if (flame_graph_sample_rate_api_env)
api_sample_rate = std::stoull(flame_graph_sample_rate_api_env);
std::ofstream api_file(file_path + "_api.txt");
if (!api_file.is_open()) {
std::cerr << "Failed to open file for writing: " << file_path << std::endl;
return;
}
for (const auto& event : trace_events_) {
// Convert duration to sample count (for simplicity, assume 1 sample per microsecond)
uint64_t duration = std::stoul(event.duration);
for (uint64_t i = 0; i < duration; i += api_sample_rate) {
api_file << event.name << ";" << event.name << i << " " << api_sample_rate
<< "\n"; // Simple example
}
}
api_file.close();
});
kernels_graph.join();
copy_graph.join();
api_graph.join();
}
void LogGpuActivityTrace(const std::string& name, const std::string& category_str,
uint64_t timestamp, uint64_t thread_id, uint64_t category,
uint64_t duration, uint64_t correlation_id) {
if (duration == 0) duration = 1;
trace_gpu_activities_.push_back({"X", category, name, category_str, std::to_string(timestamp),
std::to_string(duration), thread_id, correlation_id});
}
void LogCopyActivityTrace(const std::string& name, const std::string& category_str,
uint64_t timestamp, uint64_t thread_id, uint64_t category,
uint64_t duration, uint64_t correlation_id) {
if (duration == 0) duration = 1;
trace_copy_activities_.push_back({"X", category, name, category_str, std::to_string(timestamp),
std::to_string(duration), thread_id, correlation_id});
}
void LogCopyUnknownTrace(const std::string& name, const std::string& category_str,
uint64_t timestamp, uint64_t thread_id, uint64_t category,
uint64_t duration, uint64_t correlation_id) {
if (duration == 0) duration = 1;
trace_unknown_activities_.push_back({"X", category, name, category_str,
std::to_string(timestamp), std::to_string(duration),
thread_id, correlation_id});
}
void LogAPITrace(const std::string& name, const std::string& category_str,
uint64_t start_timestamp, uint64_t end_timestamp, uint64_t thread_id,
uint64_t category, uint64_t correlation_id) {
uint64_t duration = (end_timestamp - start_timestamp);
if (duration == 0) duration = 1;
trace_events_.push_back({"X", category, name, category_str, std::to_string(start_timestamp),
std::to_string(duration), thread_id, correlation_id});
}
void AddDataFlowEvent(uint64_t timestamp, TraceFlowCategory type, uint64_t category,
uint64_t thread_id, uint64_t correlation_id) {
if (enable_data_flow_) {
if (type == ROCPROFILER_DATA_FLOW_START) {
trace_data_flows_[correlation_id].emplace_back(
TraceFlow(timestamp, "s", category, thread_id, correlation_id));
} else {
trace_data_flows_[correlation_id].emplace_back(
TraceFlow(timestamp, "t", category, thread_id, correlation_id));
}
}
}
// We may need to use this in the Args of the trace
const char* GetDomainName(rocprofiler_tracer_activity_domain_t domain) {
switch (domain) {
case ACTIVITY_DOMAIN_ROCTX:
return "ROCTX_DOMAIN";
break;
case ACTIVITY_DOMAIN_HIP_API:
return "HIP_API_DOMAIN";
break;
case ACTIVITY_DOMAIN_HIP_OPS:
return "HIP_OPS_DOMAIN";
break;
case ACTIVITY_DOMAIN_HSA_API:
return "HSA_API_DOMAIN";
break;
case ACTIVITY_DOMAIN_HSA_OPS:
return "HSA_OPS_DOMAIN";
break;
case ACTIVITY_DOMAIN_HSA_EVT:
return "HSA_EVT_DOMAIN";
break;
default:
return "";
}
}
int FlushProfilerRecord(rocprofiler_record_profiler_t profiler_record,
rocprofiler_session_id_t session_id) {
std::lock_guard<std::mutex> lock(writing_lock);
const uint64_t device_id = profiler_record.gpu_id.handle;
const uint64_t queue_id = profiler_record.queue_id.handle;
const uint64_t correlation_id = profiler_record.correlation_id.value;
// Taken from rocprofiler: The size hasn't changed in recent past
static const uint32_t lds_block_size = 128 * 4;
std::string full_kernel_name = get_kernel_name(profiler_record);
uint64_t start_timestamp = profiler_record.timestamps.begin.value / 1000;
uint64_t end_timestamp = profiler_record.timestamps.end.value / 1000;
uint64_t duration = end_timestamp - start_timestamp;
LogGpuActivityTrace(full_kernel_name, "gpu", start_timestamp, device_id + 1, 2, duration,
correlation_id);
AddDataFlowEvent(start_timestamp, ROCPROFILER_DATA_FLOW_END, 2, device_id + 1, correlation_id);
// We need to add the information below in the Args of the trace
// TRACE_EVENT_BEGIN("KERNELS", perfetto::DynamicString(full_kernel_name.c_str()), queue_track,
// profiler_record.timestamps.begin.value, "Full Kernel Name",
// full_kernel_name.c_str(), "Agent ID", device_id, "Queue ID",
// profiler_record.queue_id.handle, "GRD",
// profiler_record.kernel_properties.grid_size, "WGR",
// profiler_record.kernel_properties.workgroup_size, "LDS",
// (((profiler_record.kernel_properties.lds_size + (lds_block_size - 1)) &
// ~(lds_block_size - 1))),
// "SCR", profiler_record.kernel_properties.scratch_size, "Arch. VGPR",
// profiler_record.kernel_properties.arch_vgpr_count, "Accumulation Vgpr",
// profiler_record.kernel_properties.accum_vgpr_count, "SGPR",
// profiler_record.kernel_properties.sgpr_count, "Wave Size",
// profiler_record.kernel_properties.wave_size, "Signal",
// profiler_record.kernel_properties.signal_handle,
// perfetto::Flow::ProcessScoped(correlation_id));
// For Counters
if (!profiler_record.counters) return 0;
for (uint64_t i = 0; i < profiler_record.counters_count.value; i++) {
if (profiler_record.counters[i].counter_handler.handle == 0) continue;
// We need to add the counter values below in the Args of the trace
// TRACE_COUNTER("COUNTERS", counters_track, profiler_record.timestamps.begin.value,
// profiler_record.counters[i].value.value);
// // Added an extra zero event for maintaining start-end of the counter
// TRACE_COUNTER("COUNTERS", counters_track, profiler_record.timestamps.end.value, 0);
}
return 0;
}
int FlushTracerRecord(rocprofiler_record_tracer_t tracer_record,
rocprofiler_session_id_t session_id) {
std::lock_guard<std::mutex> lock(writing_lock);
uint64_t device_id = tracer_record.agent_id.handle;
const char* operation_name_c = nullptr;
// ROCTX domain Operation ID doesn't have a name
// It depends on the user input of the roctx functions.
// ROCTX message is the tracer_record.name
if (tracer_record.domain != ACTIVITY_DOMAIN_ROCTX) {
CHECK_ROCPROFILER(rocprofiler_query_tracer_operation_name(
tracer_record.domain, tracer_record.operation_id, &operation_name_c));
if (!operation_name_c) operation_name_c = "Unknown Operation";
}
uint64_t start_timestamp = tracer_record.timestamps.begin.value / 1000;
uint64_t end_timestamp = tracer_record.timestamps.end.value / 1000;
uint64_t duration = end_timestamp - start_timestamp;
uint64_t correlation_id = tracer_record.correlation_id.value;
std::string roctx_message;
uint64_t roctx_id = 0;
uint64_t thread_id = tracer_record.thread_id.value;
switch (tracer_record.domain) {
case ACTIVITY_DOMAIN_ROCTX: {
roctx_id = tracer_record.external_id.id;
roctx_message = tracer_record.name ? tracer_record.name : "";
LogAPITrace((!roctx_message.empty() ? roctx_message : ""), "CPU", start_timestamp,
end_timestamp, thread_id, 1, correlation_id);
break;
}
case ACTIVITY_DOMAIN_HSA_API: {
LogAPITrace(operation_name_c, "CPU", start_timestamp, end_timestamp, thread_id, 1,
correlation_id);
AddDataFlowEvent(start_timestamp, ROCPROFILER_DATA_FLOW_START, 1, thread_id,
correlation_id);
break;
}
case ACTIVITY_DOMAIN_HIP_API: {
LogAPITrace(operation_name_c, "CPU", start_timestamp, end_timestamp, thread_id, 1,
correlation_id);
AddDataFlowEvent(start_timestamp, ROCPROFILER_DATA_FLOW_START, 1, thread_id,
correlation_id);
if (trace_period_enabled_) found_correlation_ids_.insert(correlation_id);
break;
}
case ACTIVITY_DOMAIN_EXT_API: {
printf("Warning: External API is not supported!\n");
break;
}
case ACTIVITY_DOMAIN_HIP_OPS: {
if (trace_period_enabled_ &&
found_correlation_ids_.find(correlation_id) == found_correlation_ids_.end())
break;
std::size_t pos = std::string::npos;
if (tracer_record.name) {
auto kernel_name_it = kernel_names_map.find(tracer_record.name);
if (kernel_name_it == kernel_names_map.end()) {
kernel_name_it =
kernel_names_map
.emplace(
tracer_record.name,
rocprofiler::truncate_name(rocprofiler::cxx_demangle(tracer_record.name)))
.first;
}
LogGpuActivityTrace(kernel_name_it->second, "GPU", start_timestamp, device_id + 1, 2,
duration, correlation_id);
AddDataFlowEvent(start_timestamp, ROCPROFILER_DATA_FLOW_END, 2, device_id + 1,
tracer_record.correlation_id.value);
} else {
// MEM Copies are not correlated to GPUs, so they need a special track
pos = operation_name_c ? std::string_view(operation_name_c).find("Copy")
: std::string::npos;
std::string category_str = "COPY";
uint64_t category = 3;
uint64_t thread_id_json = 1;
if (std::string::npos == pos) {
LogCopyUnknownTrace(operation_name_c, "HIPBLITKERNELS", start_timestamp, device_id, 4,
duration, correlation_id);
AddDataFlowEvent(start_timestamp, ROCPROFILER_DATA_FLOW_END, 4, device_id,
correlation_id);
} else {
LogCopyActivityTrace(operation_name_c, category_str, start_timestamp, thread_id_json,
category, duration, correlation_id);
AddDataFlowEvent(start_timestamp, ROCPROFILER_DATA_FLOW_END, category, thread_id_json,
correlation_id);
}
}
break;
}
case ACTIVITY_DOMAIN_HSA_OPS: {
LogCopyActivityTrace(operation_name_c, "COPY", start_timestamp, 0, 3, duration,
correlation_id);
AddDataFlowEvent(start_timestamp, ROCPROFILER_DATA_FLOW_END, 3, 0, correlation_id);
break;
}
default: {
rocprofiler::warning("Ignored record for domain %d", tracer_record.domain);
break;
}
}
return 0;
}
int WriteBufferRecords(const rocprofiler_record_header_t* begin,
const rocprofiler_record_header_t* end,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
while (begin < end) {
if (!begin) return 0;
switch (begin->kind) {
case ROCPROFILER_PROFILER_RECORD: {
rocprofiler_record_profiler_t* profiler_record =
const_cast<rocprofiler_record_profiler_t*>(
reinterpret_cast<const rocprofiler_record_profiler_t*>(begin));
FlushProfilerRecord(*profiler_record, session_id);
break;
}
case ROCPROFILER_TRACER_RECORD: {
rocprofiler_record_tracer_t* tracer_record = const_cast<rocprofiler_record_tracer_t*>(
reinterpret_cast<const rocprofiler_record_tracer_t*>(begin));
FlushTracerRecord(*tracer_record, session_id);
break;
}
default:
break;
}
rocprofiler_next_record(begin, &begin, session_id, buffer_id);
}
return 0;
}
bool IsValid() const { return is_valid_; }
private:
bool is_valid_{false};
fs::path output_prefix_;
fs::path txt_output_prefix_;
std::atomic<bool> flush_thread_check_{false};
std::thread flush_thread_;
std::ofstream stream_;
std::unordered_map<std::string, std::string> kernel_names_map;
std::atomic<uint64_t> trace_flow_counter{0};
std::vector<TraceActivity> trace_events_;
std::vector<TraceActivity> trace_gpu_activities_;
std::vector<TraceActivity> trace_copy_activities_;
std::vector<TraceActivity> trace_unknown_activities_;
std::map<uint64_t, std::vector<TraceFlow>> trace_data_flows_;
TraceCategory trace_categories[4] = {{{"CPU"}, "M", 1, 0},
{{"GPU"}, "M", 2, 1},
{{"COPY"}, "M", 3, 2},
{{"HIPBLITKERNELS"}, "M", 4, 3}};
std::atomic<bool> trace_categories_check{false};
bool enable_data_flow_ = true;
std::unordered_set<uint64_t> found_correlation_ids_;
bool trace_period_enabled_ = false;
};
json_plugin_t* json_plugin = nullptr;
} // namespace
int rocprofiler_plugin_initialize(uint32_t rocprofiler_major_version,
uint32_t rocprofiler_minor_version, void* data) {
if (rocprofiler_major_version != ROCPROFILER_VERSION_MAJOR ||
rocprofiler_minor_version > ROCPROFILER_VERSION_MINOR)
return -1;
std::lock_guard<std::mutex> lock(writing_lock);
if (json_plugin != nullptr) return -1;
json_plugin = new json_plugin_t();
if (json_plugin->IsValid()) {
writing_lock.unlock();
return 0;
}
delete json_plugin;
json_plugin = nullptr;
return -1;
}
void rocprofiler_plugin_finalize() {
std::lock_guard<std::mutex> lock(writing_lock);
if (!json_plugin) return;
json_plugin->delete_json_plugin();
delete json_plugin;
json_plugin = nullptr;
}
ROCPROFILER_EXPORT int rocprofiler_plugin_write_buffer_records(
const rocprofiler_record_header_t* begin, const rocprofiler_record_header_t* end,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {
if (!json_plugin || !json_plugin->IsValid()) return -1;
return json_plugin->WriteBufferRecords(begin, end, session_id, buffer_id);
}
ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(rocprofiler_record_tracer_t record) {
if (record.header.id.handle == 0) return 0;
if (!json_plugin || !json_plugin->IsValid()) return -1;
return json_plugin->FlushTracerRecord(record, rocprofiler_session_id_t{0});
}
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2013-2022 Niels Lohmann
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 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.
@@ -0,0 +1,73 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
@@ -0,0 +1,9 @@
MIT License
Copyright (c) <year> <copyright holders>
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 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.
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,176 @@
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++
// | | |__ | | | | | | version 3.11.3
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT
#ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
#define INCLUDE_NLOHMANN_JSON_FWD_HPP_
#include <cstdint> // int64_t, uint64_t
#include <map> // map
#include <memory> // allocator
#include <string> // string
#include <vector> // vector
// #include <nlohmann/detail/abi_macros.hpp>
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++
// | | |__ | | | | | | version 3.11.3
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT
// This file contains all macro definitions affecting or depending on the ABI
#ifndef JSON_SKIP_LIBRARY_VERSION_CHECK
#if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH)
#if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 11 || NLOHMANN_JSON_VERSION_PATCH != 3
#warning "Already included a different version of the library!"
#endif
#endif
#endif
#define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum)
#define NLOHMANN_JSON_VERSION_MINOR 11 // NOLINT(modernize-macro-to-enum)
#define NLOHMANN_JSON_VERSION_PATCH 3 // NOLINT(modernize-macro-to-enum)
#ifndef JSON_DIAGNOSTICS
#define JSON_DIAGNOSTICS 0
#endif
#ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
#define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
#endif
#if JSON_DIAGNOSTICS
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag
#else
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS
#endif
#if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp
#else
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON
#endif
#ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION
#define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0
#endif
// Construct the namespace ABI tags component
#define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b) json_abi ## a ## b
#define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b) \
NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b)
#define NLOHMANN_JSON_ABI_TAGS \
NLOHMANN_JSON_ABI_TAGS_CONCAT( \
NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON)
// Construct the namespace version component
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \
_v ## major ## _ ## minor ## _ ## patch
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch)
#if NLOHMANN_JSON_NAMESPACE_NO_VERSION
#define NLOHMANN_JSON_NAMESPACE_VERSION
#else
#define NLOHMANN_JSON_NAMESPACE_VERSION \
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \
NLOHMANN_JSON_VERSION_MINOR, \
NLOHMANN_JSON_VERSION_PATCH)
#endif
// Combine namespace components
#define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b
#define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \
NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b)
#ifndef NLOHMANN_JSON_NAMESPACE
#define NLOHMANN_JSON_NAMESPACE \
nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \
NLOHMANN_JSON_ABI_TAGS, \
NLOHMANN_JSON_NAMESPACE_VERSION)
#endif
#ifndef NLOHMANN_JSON_NAMESPACE_BEGIN
#define NLOHMANN_JSON_NAMESPACE_BEGIN \
namespace nlohmann \
{ \
inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \
NLOHMANN_JSON_ABI_TAGS, \
NLOHMANN_JSON_NAMESPACE_VERSION) \
{
#endif
#ifndef NLOHMANN_JSON_NAMESPACE_END
#define NLOHMANN_JSON_NAMESPACE_END \
} /* namespace (inline namespace) NOLINT(readability/namespace) */ \
} // namespace nlohmann
#endif
/*!
@brief namespace for Niels Lohmann
@see https://github.com/nlohmann
@since version 1.0.0
*/
NLOHMANN_JSON_NAMESPACE_BEGIN
/*!
@brief default JSONSerializer template argument
This serializer ignores the template arguments and uses ADL
([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
for serialization.
*/
template<typename T = void, typename SFINAE = void>
struct adl_serializer;
/// a class to store JSON values
/// @sa https://json.nlohmann.me/api/basic_json/
template<template<typename U, typename V, typename... Args> class ObjectType =
std::map,
template<typename U, typename... Args> class ArrayType = std::vector,
class StringType = std::string, class BooleanType = bool,
class NumberIntegerType = std::int64_t,
class NumberUnsignedType = std::uint64_t,
class NumberFloatType = double,
template<typename U> class AllocatorType = std::allocator,
template<typename T, typename SFINAE = void> class JSONSerializer =
adl_serializer,
class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
class CustomBaseClass = void>
class basic_json;
/// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
/// @sa https://json.nlohmann.me/api/json_pointer/
template<typename RefStringType>
class json_pointer;
/*!
@brief default specialization
@sa https://json.nlohmann.me/api/json/
*/
using json = basic_json<>;
/// @brief a minimal map-like container that preserves insertion order
/// @sa https://json.nlohmann.me/api/ordered_map/
template<class Key, class T, class IgnoredLess, class Allocator>
struct ordered_map;
/// @brief specialization that maintains the insertion order of object keys
/// @sa https://json.nlohmann.me/api/ordered_json/
using ordered_json = basic_json<nlohmann::ordered_map>;
NLOHMANN_JSON_NAMESPACE_END
#endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
@@ -0,0 +1,35 @@
rocprofiler_checkout_git_submodule(
RECURSIVE
TEST_FILE "sdk"
RELATIVE_PATH perfetto
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
REPO_URL https://github.com/google/perfetto.git
REPO_BRANCH "releases/v44.x")
include_directories(perfetto/sdk)
file(GLOB ROCPROFILER_UTIL_SRC_FILES ${PROJECT_SOURCE_DIR}/src/utils/helper.cpp)
add_library(perfetto_plugin ${LIBRARY_TYPE} ${ROCPROFILER_UTIL_SRC_FILES} perfetto/sdk/perfetto.cc perfetto.cpp)
set_target_properties(
perfetto_plugin
PROPERTIES CXX_VISIBILITY_PRESET hidden
LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib/rocprofiler
INSTALL_RPATH "${ROCM_APPEND_PRIVLIB_RPATH}")
target_compile_definitions(perfetto_plugin PRIVATE HIP_PROF_HIP_API_STRING=1
__HIP_PLATFORM_AMD__=1)
target_include_directories(perfetto_plugin PRIVATE ${PROJECT_SOURCE_DIR})
target_link_options(
perfetto_plugin PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exportmap
-Wl,--no-undefined)
target_link_libraries(perfetto_plugin PRIVATE rocprofiler-v2
Threads::Threads stdc++fs amd_comgr)
install(TARGETS perfetto_plugin
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME} COMPONENT plugins)
Submodule projects/rocprofiler/plugin/perfetto/perfetto added at eb5ef24c58

Some files were not shown because too many files have changed in this diff Show More