Skip to content

Commit 911417c

Browse files
Merge pull request #496 from Treece-Burgess/10-23-2025-further-cuda-quals-restrictions
cuda perfworks api: Search for valid qualifiers by name i.e. device or stat
2 parents 539d55e + 59d3e78 commit 911417c

File tree

1 file changed

+64
-20
lines changed

1 file changed

+64
-20
lines changed

src/components/cuda/cupti_profiler.c

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ static int evt_code_to_name(uint32_t event_code, char *name, int len);
210210
static int evt_name_to_basename(const char *name, char *base, int len);
211211
static int evt_name_to_device(const char *name, int *device, const char *base);
212212
static int evt_name_to_stat(const char *name, int *stat, const char *base);
213-
static int cuda_verify_no_repeated_qualifiers(const char *eventName);
214-
static int cuda_verify_qualifiers(int flag, char *qualifierName, int equalitySignPosition, int *qualifierValue);
213+
static int cuda_verify_qualifiers_are_not_repeated(const char *eventName);
214+
static int cuda_verify_qualifiers_are_valid(const char *qualifiers);
215+
static int cuda_verify_qualifiers_do_not_contain_junk(int flag, char *qualifierName, int equalitySignPosition, int *qualifierValue);
215216

216217
// Functions related to the stats qualifier
217218
static int restructure_event_name(const char *input, char *output, char *base, char *stat);
@@ -1661,21 +1662,34 @@ int cuptip_evt_name_to_code(const char *name, uint32_t *event_code)
16611662
char base[PAPI_MAX_STR_LEN] = { 0 };
16621663
SUBDBG("ENTER: name: %s, event_code: %p\n", name, event_code);
16631664

1664-
papi_errno = cuda_verify_no_repeated_qualifiers(name);
1665+
papi_errno = evt_name_to_basename(name, base, PAPI_MAX_STR_LEN);
16651666
if (papi_errno != PAPI_OK) {
16661667
goto fn_exit;
16671668
}
16681669

1669-
papi_errno = evt_name_to_basename(name, base, PAPI_MAX_STR_LEN);
1670-
if (papi_errno != PAPI_OK) {
1671-
goto fn_exit;
1670+
char *userAddedQualifiers = strstr(name, ":");
1671+
// Detected user added qualifiers, verify their validity
1672+
// If no qualifiers are added, we handle default cases in
1673+
// evt_name_to_device and evt_name_to_stat respectively.
1674+
if (userAddedQualifiers != NULL) {
1675+
papi_errno = cuda_verify_qualifiers_are_valid(userAddedQualifiers);
1676+
if (papi_errno != PAPI_OK) {
1677+
goto fn_exit;
1678+
}
1679+
1680+
papi_errno = cuda_verify_qualifiers_are_not_repeated(userAddedQualifiers);
1681+
if (papi_errno != PAPI_OK) {
1682+
goto fn_exit;
1683+
}
16721684
}
16731685

1686+
// Handle device qualifier case
16741687
papi_errno = evt_name_to_device(name, &device, base);
16751688
if (papi_errno != PAPI_OK) {
16761689
goto fn_exit;
16771690
}
16781691

1692+
// Handle stat qualifier case
16791693
papi_errno = evt_name_to_stat(name, &stat, base);
16801694
if (papi_errno != PAPI_OK) {
16811695
goto fn_exit;
@@ -2036,23 +2050,23 @@ static int evt_name_to_basename(const char *name, char *base, int len)
20362050
return PAPI_OK;
20372051
}
20382052

2039-
/** @class cuda_verify_no_repeated_qualifiers
2053+
/** @class cuda_verify_qualifiers_are_not_repeated
20402054
* @brief Verify that a user has not added multiple device or stats qualifiers
20412055
* to an event name.
20422056
*
20432057
* @param *eventName
20442058
* User provided event name we need to verify.
20452059
*/
2046-
static int cuda_verify_no_repeated_qualifiers(const char *eventName)
2060+
static int cuda_verify_qualifiers_are_not_repeated(const char *qualifiers)
20472061
{
20482062
int numDeviceQualifiers = 0, numStatsQualifiers = 0;
2049-
char tmpEventName[PAPI_2MAX_STR_LEN];
2050-
int strLen = snprintf(tmpEventName, PAPI_2MAX_STR_LEN, "%s", eventName);
2063+
char tmpQualifiers[PAPI_2MAX_STR_LEN];
2064+
int strLen = snprintf(tmpQualifiers, PAPI_2MAX_STR_LEN, "%s", qualifiers);
20512065
if (strLen < 0 || strLen >= PAPI_2MAX_STR_LEN) {
2052-
ERRDBG("Failed to fully write eventName into tmpEventName.\n");
2066+
SUBDBG("Failed to fully write qualifiers into tmpQualifiers.\n");
20532067
return PAPI_EBUF;
20542068
}
2055-
char *token = strtok(tmpEventName, ":");
2069+
char *token = strtok(tmpQualifiers, ":");
20562070
while(token != NULL) {
20572071
if (strncmp(token, "device", 6) == 0) {
20582072
numDeviceQualifiers++;
@@ -2065,16 +2079,46 @@ static int cuda_verify_no_repeated_qualifiers(const char *eventName)
20652079
}
20662080

20672081
if (numDeviceQualifiers > 1 || numStatsQualifiers > 1) {
2068-
ERRDBG("Provided Cuda event has multiple device or stats qualifiers appended.\n");
2082+
SUBDBG("Provided Cuda event has multiple device or stats qualifiers appended.\n");
20692083
return PAPI_ENOEVNT;
20702084
}
20712085

20722086
return PAPI_OK;
20732087
}
20742088

2075-
/** @class cuda_verify_qualifiers
2089+
/** @class cuda_verify_qualifiers_are_valid
2090+
* @brief Verify that a user has added valid qualifiers i.e. stat or device.
2091+
*
2092+
* @param *qualifiers
2093+
* String of user added qualifiers.
2094+
*/
2095+
static int cuda_verify_qualifiers_are_valid(const char *qualifiers)
2096+
{
2097+
char tmpQualifiers[PAPI_2MAX_STR_LEN];
2098+
int strLen = snprintf(tmpQualifiers, PAPI_2MAX_STR_LEN, "%s", qualifiers);
2099+
if (strLen < 0 || strLen >= PAPI_2MAX_STR_LEN) {
2100+
SUBDBG("Failed to fully write qualifiers into tmpQualifiers.\n");
2101+
return PAPI_EBUF;
2102+
}
2103+
2104+
char *token = strtok(tmpQualifiers, ":");
2105+
while (token != NULL) {
2106+
if (strncmp(token, "device", 6) != 0 &&
2107+
strncmp(token, "stat", 4) != 0) {
2108+
SUBDBG("The appended qualifier is not supported: %s\n", token);
2109+
return PAPI_ENOEVNT;
2110+
}
2111+
2112+
token = strtok(NULL, ":");
2113+
}
2114+
2115+
return PAPI_OK;
2116+
}
2117+
2118+
/** @class cuda_verify_qualifiers_do_not_contain_junk
20762119
* @brief Verify that the device and/or stats qualifier provided by the user
2077-
* is valid. E.g. :device=# or :stat=avg.
2120+
* does not contain excess characters or does not have the proper
2121+
* equals sign.
20782122
*
20792123
* @param flag
20802124
* Device or stats flag define. Allows us to determine the case to enter for
@@ -2087,7 +2131,7 @@ static int cuda_verify_no_repeated_qualifiers(const char *eventName)
20872131
* Upon verifying the provided qualifier is valid. Store either a device index
20882132
* or a statistic index.
20892133
*/
2090-
static int cuda_verify_qualifiers(int flag, char *qualifierName, int equalitySignPosition, int *qualifierValue)
2134+
static int cuda_verify_qualifiers_do_not_contain_junk(int flag, char *qualifierName, int equalitySignPosition, int *qualifierValue)
20912135
{
20922136
int pos = equalitySignPosition;
20932137
// Verify that an equal sign was provided where it was suppose to be
@@ -2108,7 +2152,7 @@ static int cuda_verify_qualifiers(int flag, char *qualifierName, int equalitySig
21082152
return PAPI_ENOEVNT;
21092153
}
21102154

2111-
// Verify that only qualifiers have been appended
2155+
// Verify that no junk has been appended after the device qualifier
21122156
char *endPtr;
21132157
*qualifierValue = (int) strtol(qualifierName + strlen(":device="), &endPtr, 10);
21142158
// Check to make sure only qualifiers have been appended
@@ -2126,7 +2170,7 @@ static int cuda_verify_qualifiers(int flag, char *qualifierName, int equalitySig
21262170
for (i = 0; i < NUM_STATS_QUALS; i++) {
21272171
size_t token_len = strlen(stats[i]);
21282172
if (strncmp(qualifierName, stats[i], token_len) == 0) {
2129-
// Check to make sure only qualifiers have been appended
2173+
// Verify that no junk has been appended after the stats qualifier
21302174
char *no_excess_chars = qualifierName + token_len;
21312175
if (strlen(no_excess_chars) == 0 || strncmp(no_excess_chars, ":device", 7) == 0) {
21322176
*qualifierValue = i;
@@ -2157,7 +2201,7 @@ static int evt_name_to_device(const char *name, int *device, const char *base)
21572201
// User did provide :device=# qualifier
21582202
if (p != NULL) {
21592203
int equalitySignPos = 7;
2160-
int papi_errno = cuda_verify_qualifiers(DEVICE_FLAG, p, equalitySignPos, device);
2204+
int papi_errno = cuda_verify_qualifiers_do_not_contain_junk(DEVICE_FLAG, p, equalitySignPos, device);
21612205
if (papi_errno != PAPI_OK) {
21622206
return papi_errno;
21632207
}
@@ -2196,7 +2240,7 @@ static int evt_name_to_stat(const char *name, int *stat, const char *base)
21962240
char *p = strstr(name, ":stat");
21972241
if (p != NULL) {
21982242
int equalitySignPos = 5;
2199-
int papi_errno = cuda_verify_qualifiers(STAT_FLAG, p, equalitySignPos, stat);
2243+
int papi_errno = cuda_verify_qualifiers_do_not_contain_junk(STAT_FLAG, p, equalitySignPos, stat);
22002244
if (papi_errno != PAPI_OK) {
22012245
return papi_errno;
22022246
}

0 commit comments

Comments
 (0)