Skip to content

Commit 6d72bc0

Browse files
cuda perfworks api: Update internal string handling
1 parent 656f5a9 commit 6d72bc0

File tree

3 files changed

+59
-49
lines changed

3 files changed

+59
-49
lines changed

src/components/cuda/cupti_profiler.c

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -681,13 +681,13 @@ int verify_user_added_events(uint32_t *events_id, int num_events, cuptip_control
681681
// Reconstructing event name. Append the basename, stat, and sub-metric.
682682
size_t basename_len = stat_position - cuptiu_table_p->events[info.nameid].basenameWithStatReplaced;
683683
char reconstructedEventName[PAPI_HUGE_STR_LEN]="";
684-
strLen = snprintf(reconstructedEventName, PAPI_MAX_STR_LEN, "%.*s%s%s",
684+
strLen = snprintf(reconstructedEventName, PAPI_HUGE_STR_LEN, "%.*s%s%s",
685685
(int)basename_len,
686686
cuptiu_table_p->events[info.nameid].basenameWithStatReplaced,
687687
stat,
688688
stat_position + 4);
689-
if (strLen < 0 || strLen >= PAPI_MAX_STR_LEN) {
690-
SUBDBG("Failed to fully write reconstructed event name.\n");
689+
if (strLen < 0 || strLen >= PAPI_HUGE_STR_LEN) {
690+
SUBDBG("Failed to add the CUPTI metric name as reconstruction of the basename and stat exceeded the buffer size.\n");
691691
return PAPI_EBUF;
692692
}
693693

@@ -705,9 +705,9 @@ int verify_user_added_events(uint32_t *events_id, int num_events, cuptip_control
705705
int idx = state->gpu_ctl[info.device].added_events->count;
706706
// Store metadata
707707
strLen = snprintf(state->gpu_ctl[info.device].added_events->cuda_evts[idx],
708-
PAPI_MAX_STR_LEN, "%s", reconstructedEventName);
709-
if (strLen < 0 || strLen >= PAPI_MAX_STR_LEN) {
710-
SUBDBG("Failed to fully write reconstructed Cuda event name to array of added events.\n");
708+
PAPI_HUGE_STR_LEN, "%s", reconstructedEventName);
709+
if (strLen < 0 || strLen >= PAPI_HUGE_STR_LEN) {
710+
SUBDBG("Failed to fully write the reconstructed CUPTI metric name to array of added events.\n");
711711
return PAPI_EBUF;
712712
}
713713
state->gpu_ctl[info.device].added_events->cuda_devs[idx] = info.device;
@@ -1475,15 +1475,15 @@ static int get_ntv_events(cuptiu_event_table_t *evt_table, const char *evt_name,
14751475
// Increment event count
14761476
(*count)++;
14771477

1478-
strLen = snprintf(event->name, PAPI_2MAX_STR_LEN, "%s", name_no_stat);
1479-
if (strLen < 0 || strLen >= PAPI_2MAX_STR_LEN) {
1480-
ERRDBG("Failed to fully write name with no stat.\n");
1478+
strLen = snprintf(event->name, PAPI_HUGE_STR_LEN, "%s", name_no_stat);
1479+
if (strLen < 0 || strLen >= PAPI_HUGE_STR_LEN) {
1480+
ERRDBG("Failed to fully write CUPTI metric name with no 'stat'.\n");
14811481
return PAPI_EBUF;
14821482
}
14831483

1484-
strLen = snprintf(event->basenameWithStatReplaced, sizeof(event->basenameWithStatReplaced), "%s", name_restruct);
1484+
strLen = snprintf(event->basenameWithStatReplaced, PAPI_HUGE_STR_LEN, "%s", name_restruct);
14851485
if (strLen < 0 || strLen >= PAPI_HUGE_STR_LEN) {
1486-
ERRDBG("String larger than PAPI_HUGE_STR_LEN");
1486+
ERRDBG("Failed to fully write CUPTI metric name with the stat value replaced with 'stat'.\n");
14871487
return PAPI_EBUF;
14881488
}
14891489

@@ -1659,10 +1659,10 @@ int cuptip_evt_name_to_code(const char *name, uint32_t *event_code)
16591659
{
16601660
int htable_errno, device, stat, flags, nameid, papi_errno = PAPI_OK;
16611661
cuptiu_event_t *event;
1662-
char base[PAPI_MAX_STR_LEN] = { 0 };
1662+
char base[PAPI_HUGE_STR_LEN] = { 0 };
16631663
SUBDBG("ENTER: name: %s, event_code: %p\n", name, event_code);
16641664

1665-
papi_errno = evt_name_to_basename(name, base, PAPI_MAX_STR_LEN);
1665+
papi_errno = evt_name_to_basename(name, base, PAPI_HUGE_STR_LEN);
16661666
if (papi_errno != PAPI_OK) {
16671667
goto fn_exit;
16681668
}
@@ -1775,11 +1775,11 @@ static int evt_code_to_name(uint32_t event_code, char *name, int len)
17751775
}
17761776

17771777
int str_len;
1778-
char stat[PAPI_HUGE_STR_LEN] = "";
1778+
char stat[PAPI_MIN_STR_LEN] = "";
17791779
if (info.stat < NUM_STATS_QUALS){
1780-
str_len = snprintf(stat, sizeof(stat), "%s", stats[info.stat]);
1781-
if (str_len < 0 || str_len >= PAPI_HUGE_STR_LEN) {
1782-
ERRDBG("String larger than PAPI_HUGE_STR_LEN");
1780+
str_len = snprintf(stat, PAPI_MIN_STR_LEN, "%s", stats[info.stat]);
1781+
if (str_len < 0 || str_len >= PAPI_MIN_STR_LEN) {
1782+
ERRDBG("Failed to fully write statistic qualifier name.\n");
17831783
return PAPI_EBUF;
17841784
}
17851785
}
@@ -1794,7 +1794,7 @@ static int evt_code_to_name(uint32_t event_code, char *name, int len)
17941794
break;
17951795
case (STAT_FLAG):
17961796
str_len = snprintf(name, len, "%s:stat=%s", cuptiu_table_p->events[info.nameid].name, stat);
1797-
if (str_len < 0 || str_len >= PAPI_HUGE_STR_LEN) {
1797+
if (str_len < 0 || str_len >= len) {
17981798
ERRDBG("String formatting exceeded max string length.\n");
17991799
return PAPI_EBUF;
18001800
}
@@ -1841,11 +1841,15 @@ int cuptip_evt_code_to_info(uint32_t event_code, PAPI_event_info_t *info)
18411841
}
18421842
size_t basename_len = stat_position - cuptiu_table_p->events[inf.nameid].basenameWithStatReplaced;
18431843
char reconstructedEventName[PAPI_HUGE_STR_LEN]="";
1844-
int strLen = snprintf(reconstructedEventName, PAPI_MAX_STR_LEN, "%.*s%s%s",
1844+
int strLen = snprintf(reconstructedEventName, PAPI_HUGE_STR_LEN, "%.*s%s%s",
18451845
(int)basename_len,
18461846
cuptiu_table_p->events[inf.nameid].basenameWithStatReplaced,
18471847
cuptiu_table_p->events[inf.nameid].stat->arrayMetricStatistics[0],
18481848
stat_position + 4);
1849+
if (strLen < 0 || strLen >= PAPI_HUGE_STR_LEN) {
1850+
SUBDBG("Failed to reconstruct CUPTI metric name with the basename and stat.\n");
1851+
return PAPI_EBUF;
1852+
}
18491853

18501854
int i;
18511855
// For a Cuda event collect the description, units, and number of passes
@@ -1890,7 +1894,7 @@ int cuptip_evt_code_to_info(uint32_t event_code, PAPI_event_info_t *info)
18901894
}
18911895
case DEVICE_FLAG:
18921896
{
1893-
char devices[PAPI_MAX_STR_LEN] = { 0 };
1897+
char devices[PAPI_2MAX_STR_LEN] = { 0 };
18941898
int init_metric_dev_id;
18951899
for (i = 0; i < numDevicesOnMachine; ++i) {
18961900
if (cuptiu_dev_check(cuptiu_table_p->events[inf.nameid].device_map, i)) {
@@ -1900,9 +1904,10 @@ int cuptip_evt_code_to_info(uint32_t event_code, PAPI_event_info_t *info)
19001904
init_metric_dev_id = i;
19011905

19021906
}
1903-
int strLen = snprintf(devices + strlen(devices), PAPI_MAX_STR_LEN, "%i,", i);
1904-
if (strLen < 0 || strLen >= PAPI_MAX_STR_LEN) {
1905-
ERRDBG("Failed to fully write device qualifiers.\n");
1907+
int strLen = snprintf(devices + strlen(devices), PAPI_2MAX_STR_LEN - strlen(devices), "%i,", i);
1908+
if (strLen < 0 || strLen >= PAPI_2MAX_STR_LEN - strlen(devices)) {
1909+
SUBDBG("Failed to write device %d into devices in DEVICE_FLAG case.\n", i);
1910+
return PAPI_EBUF;
19061911
}
19071912

19081913
}
@@ -1964,7 +1969,7 @@ int cuptip_evt_code_to_info(uint32_t event_code, PAPI_event_info_t *info)
19641969
case (STAT_FLAG | DEVICE_FLAG):
19651970
{
19661971
int init_metric_dev_id;
1967-
char devices[PAPI_MAX_STR_LEN] = { 0 };
1972+
char devices[PAPI_2MAX_STR_LEN] = { 0 };
19681973
for (i = 0; i < numDevicesOnMachine; ++i) {
19691974
if (cuptiu_dev_check(cuptiu_table_p->events[inf.nameid].device_map, i)) {
19701975
/* for an event, store the first device found to use with :device=#,
@@ -1973,7 +1978,11 @@ int cuptip_evt_code_to_info(uint32_t event_code, PAPI_event_info_t *info)
19731978
init_metric_dev_id = i;
19741979
}
19751980

1976-
sprintf(devices + strlen(devices), "%i,", i);
1981+
strLen = snprintf(devices + strlen(devices), PAPI_2MAX_STR_LEN - strlen(devices), "%i,", i);
1982+
if (strLen < 0 || strLen >= PAPI_2MAX_STR_LEN - strlen(devices)) {
1983+
SUBDBG("Failed to write device %d into devices in STAT_FLAG | DEVICE_FLAG case.\n", i);
1984+
return PAPI_EBUF;
1985+
}
19771986
}
19781987
}
19791988
*(devices + strlen(devices) - 1) = 0;
@@ -2060,9 +2069,9 @@ static int evt_name_to_basename(const char *name, char *base, int len)
20602069
static int cuda_verify_qualifiers_are_not_repeated(const char *qualifiers)
20612070
{
20622071
int numDeviceQualifiers = 0, numStatsQualifiers = 0;
2063-
char tmpQualifiers[PAPI_2MAX_STR_LEN];
2064-
int strLen = snprintf(tmpQualifiers, PAPI_2MAX_STR_LEN, "%s", qualifiers);
2065-
if (strLen < 0 || strLen >= PAPI_2MAX_STR_LEN) {
2072+
char tmpQualifiers[PAPI_HUGE_STR_LEN];
2073+
int strLen = snprintf(tmpQualifiers, PAPI_HUGE_STR_LEN, "%s", qualifiers);
2074+
if (strLen < 0 || strLen >= PAPI_HUGE_STR_LEN) {
20662075
SUBDBG("Failed to fully write qualifiers into tmpQualifiers.\n");
20672076
return PAPI_EBUF;
20682077
}
@@ -2094,9 +2103,9 @@ static int cuda_verify_qualifiers_are_not_repeated(const char *qualifiers)
20942103
*/
20952104
static int cuda_verify_qualifiers_are_valid(const char *qualifiers)
20962105
{
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) {
2106+
char tmpQualifiers[PAPI_HUGE_STR_LEN];
2107+
int strLen = snprintf(tmpQualifiers, PAPI_HUGE_STR_LEN, "%s", qualifiers);
2108+
if (strLen < 0 || strLen >= PAPI_HUGE_STR_LEN) {
21002109
SUBDBG("Failed to fully write qualifiers into tmpQualifiers.\n");
21012110
return PAPI_EBUF;
21022111
}
@@ -2355,9 +2364,9 @@ static int enumerate_metrics_for_unique_devices(const char *pChipName, int *tota
23552364
size_t metricNameBeginIndex = getMetricNamesParams.pMetricNameBeginIndices[metricIdx];
23562365
const char *baseMetricName = &getMetricNamesParams.pMetricNames[metricNameBeginIndex];
23572366

2358-
char fullMetricName[PAPI_2MAX_STR_LEN];
2359-
int strLen = snprintf(fullMetricName, PAPI_2MAX_STR_LEN, "%s", baseMetricName);
2360-
if (strLen < 0 || strLen >= PAPI_2MAX_STR_LEN) {
2367+
char fullMetricName[PAPI_HUGE_STR_LEN];
2368+
int strLen = snprintf(fullMetricName, PAPI_HUGE_STR_LEN, "%s", baseMetricName);
2369+
if (strLen < 0 || strLen >= PAPI_HUGE_STR_LEN) {
23612370
SUBDBG("Failed to fully append the base metric name.\n");
23622371
return PAPI_EBUF;
23632372
}
@@ -2375,8 +2384,8 @@ static int enumerate_metrics_for_unique_devices(const char *pChipName, int *tota
23752384
return papi_errno;
23762385
}
23772386

2378-
strLen = snprintf(fullMetricName + offsetForMetricName, PAPI_2MAX_STR_LEN - offsetForMetricName, "%s", rollupMetricName);
2379-
if (strLen < 0 || strLen >= PAPI_2MAX_STR_LEN) {
2387+
strLen = snprintf(fullMetricName + offsetForMetricName, PAPI_HUGE_STR_LEN - offsetForMetricName, "%s", rollupMetricName);
2388+
if (strLen < 0 || strLen >= PAPI_HUGE_STR_LEN - offsetForMetricName) {
23802389
SUBDBG("Failed to fully append rollup metric name.\n");
23812390
return PAPI_EBUF;
23822391
}
@@ -2402,8 +2411,8 @@ static int enumerate_metrics_for_unique_devices(const char *pChipName, int *tota
24022411
}
24032412

24042413
if (supportedSubMetrics.pSupportedSubmetrics[subMetricIdx] != NVPW_SUBMETRIC_NONE) {
2405-
strLen = snprintf(fullMetricName + offsetForMetricName, PAPI_2MAX_STR_LEN - offsetForMetricName, "%s", subMetricName);
2406-
if (strLen < 0 || strLen >= PAPI_2MAX_STR_LEN) {
2414+
strLen = snprintf(fullMetricName + offsetForMetricName, PAPI_HUGE_STR_LEN - offsetForMetricName, "%s", subMetricName);
2415+
if (strLen < 0 || strLen >= PAPI_HUGE_STR_LEN - offsetForMetricName) {
24072416
SUBDBG("Failed to fully append submetric names.\n");
24082417
return PAPI_EBUF;
24092418
}
@@ -2414,15 +2423,15 @@ static int enumerate_metrics_for_unique_devices(const char *pChipName, int *tota
24142423
SUBDBG("Failed to allocate memory for metricNames.\n");
24152424
return PAPI_ENOMEM;
24162425
}
2417-
metricNames[metricCount] = (char *) malloc(PAPI_2MAX_STR_LEN * sizeof(char));
2426+
metricNames[metricCount] = (char *) malloc(PAPI_HUGE_STR_LEN * sizeof(char));
24182427
if (metricNames[metricCount] == NULL) {
24192428
SUBDBG("Failed to allocate memory for the index %d in the array metricNames.\n", metricCount);
24202429
return PAPI_ENOMEM;
24212430
}
24222431

24232432
// Store the constructed metric name
2424-
strLen = snprintf(metricNames[metricCount], PAPI_2MAX_STR_LEN, "%s", fullMetricName);
2425-
if (strLen < 0 || strLen >= PAPI_2MAX_STR_LEN) {
2433+
strLen = snprintf(metricNames[metricCount], PAPI_HUGE_STR_LEN, "%s", fullMetricName);
2434+
if (strLen < 0 || strLen >= PAPI_HUGE_STR_LEN) {
24262435
SUBDBG("Failed to fully write constructued metric name: %s\n", fullMetricName);
24272436
return PAPI_EBUF;
24282437
}
@@ -2655,8 +2664,8 @@ static int get_metric_properties(const char *pChipName, const char *metricName,
26552664
dimUnitsParams.pDimUnits = dimUnitsFactor;
26562665
nvpwCheckErrors( NVPW_MetricsEvaluator_GetMetricDimUnitsPtr(&dimUnitsParams), return PAPI_EMISC );
26572666

2658-
char tmpMetricUnits[PAPI_MAX_STR_LEN] = { 0 };
2659-
int i;
2667+
char tmpMetricUnits[PAPI_HUGE_STR_LEN] = { 0 };
2668+
int i, offsetMetricUnits = 0;
26602669
for (i = 0; i < dimUnitsParams.numDimUnits; i++) {
26612670
NVPW_MetricsEvaluator_DimUnitToString_Params dimUnitToStringParams = {NVPW_MetricsEvaluator_DimUnitToString_Params_STRUCT_SIZE};
26622671
dimUnitToStringParams.pMetricsEvaluator = pMetricsEvaluator;
@@ -2665,11 +2674,12 @@ static int get_metric_properties(const char *pChipName, const char *metricName,
26652674
nvpwCheckErrors( NVPW_MetricsEvaluator_DimUnitToStringPtr(&dimUnitToStringParams), return PAPI_EMISC );
26662675

26672676
char *unitsFormat = (i == 0) ? "%s" : "/%s";
2668-
strLen = snprintf(tmpMetricUnits + strlen(tmpMetricUnits), PAPI_MAX_STR_LEN - strlen(tmpMetricUnits), unitsFormat, dimUnitToStringParams.pPluralName);
2669-
if (strLen < 0 || strLen >= PAPI_MAX_STR_LEN) {
2677+
strLen = snprintf(tmpMetricUnits + offsetMetricUnits, PAPI_HUGE_STR_LEN - offsetMetricUnits, unitsFormat, dimUnitToStringParams.pPluralName);
2678+
if (strLen < 0 || strLen >= PAPI_HUGE_STR_LEN - offsetMetricUnits) {
26702679
SUBDBG("Failed to fully write dimensional units for a metric.\n");
26712680
return PAPI_EBUF;
26722681
}
2682+
offsetMetricUnits = strlen(tmpMetricUnits);
26732683
}
26742684
free(dimUnitsFactor);
26752685
metricUnits = tmpMetricUnits;

src/components/cuda/cupti_utils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ typedef struct {
3131
} StringVector;
3232

3333
typedef struct event_record_s {
34-
char name[PAPI_2MAX_STR_LEN];
35-
char basenameWithStatReplaced[PAPI_2MAX_STR_LEN];
34+
char name[PAPI_HUGE_STR_LEN];
35+
char basenameWithStatReplaced[PAPI_HUGE_STR_LEN];
3636
char desc[PAPI_HUGE_STR_LEN];
3737
StringVector * stat;
3838
cuptiu_bitmap_t device_map;
@@ -42,7 +42,7 @@ typedef struct event_table_s {
4242
unsigned int count;
4343
unsigned int event_stats_count;
4444
unsigned int capacity;
45-
char cuda_evts[30][PAPI_2MAX_STR_LEN];
45+
char cuda_evts[30][PAPI_HUGE_STR_LEN];
4646
int cuda_devs[30];
4747
int evt_pos[30];
4848
gpu_record_t *avail_gpu_info;

src/components/cuda/linux-cuda.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ static int cuda_init_comp_presets(void)
371371
char *cname = _cuda_vector.cmp_info.name;
372372

373373
/* Setup presets. */
374-
char arch_name[PAPI_2MAX_STR_LEN];
374+
char arch_name[PAPI_MAX_STR_LEN];
375375
int devIdx = -1;
376376
int numDevices = 0;
377377

0 commit comments

Comments
 (0)