PAPI 7.1.0.0
Loading...
Searching...
No Matches
gpu_metric_read.c
Go to the documentation of this file.
1/* Copyright (c) 2020 Intel Corp. All rights reserved
2 * Contributed by Peinan Zhang <peinan.zhang@intel.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 * of the Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
18 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
19 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22/*
23 * This test case tests time based data collection on ntel GPU performance metrics
24 *
25 * @ brief Collect metric data for a certain time interval, with one or more loops.
26 * By default, the metric data will aggregate overtime for each loop.
27 * When reset, each group reports metric data only for the sepecified time duration
28 *
29 * @option:
30 * [-d <time interval in second>][-l <number of loops>] [-s (reset)] [-m <metric list>]
31 */
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <unistd.h>
36#include <string.h>
37
38#include "papi.h"
39
40#include "gpu_common_utils.h"
41
42/*
43 * This test case tests:
44 * PAPI_start(), PAPI_read(), PAPI_reset(), PAPI_stop()
45 */
46
47int
48main(int argc, char ** argv) {
49
50 int i = 0;
51 int retVal = 0;
52 int cid = -1;
53 int total_metrics = 0;
54 int event_set = PAPI_NULL;
56 InParams param;
57
58 if (argc < 2) {
59 printf("usage: %s -d <duration> [ -l <loops>][-s][-m metric[:device=0][:tile=0]]\n", argv[0]);
60 return 0;
61 }
62 // unset variable ZET_ENABLE_API_TRACING_EXP to enable time base collection
63 retVal = putenv("ZET_ENABLE_API_TRACING_EXP=0");
64 retVal = parseInputParam(argc, argv, &param);
65 if (retVal) {
66 printf("Invalid input parameters.\n");
67 printf("usage: %s -d <duration> [ -l <loops>][-s][-m metric[:device=0][:tile=0]]\n", argv[0]);
68 return 0;
69 }
70 int num_metrics = param.num_metrics;
71 char **metric_names = (char **)(param.metric_names);
72
73 cid = initPAPIGPUComp();
74 if (cid < 0) {
75 return 1;
76 }
77
78 i = 0 | PAPI_NATIVE_MASK;
79 retVal=PAPI_enum_cmp_event( &i, PAPI_ENUM_FIRST, cid );
80 if (retVal != PAPI_OK) {
81 fprintf(stderr, "Error on enum_cmp_event for component[ %s ], abort.\n", COMP_NAME);
83 return 1;
84 }
85 total_metrics = 0;
86 do {
87 memset( &info, 0, sizeof ( info ) );
88 retVal = PAPI_get_event_info( i, &info );
89 if (retVal == PAPI_OK) {
91 retVal = PAPI_enum_cmp_event( &i, PAPI_ENUM_EVENTS, cid );
92 }
93 } while (retVal == PAPI_OK);
94
95 if ((!total_metrics)) {
96 fprintf(stderr, "Error on enum_cmp_event, abort.\n");
98 return 1;
99 }
100 retVal = initMetricSet(metric_names, num_metrics, &event_set);
101 if (retVal != PAPI_OK) {
103 return 1;
104 }
105 long long *metric_values = (long long *)calloc(num_metrics, sizeof(long long));
106 if (!metric_values) {
107 fprintf(stderr, "Memory allocation failed, abort.\n");
109 return 1;
110 }
111 retVal = PAPI_start(event_set);
112 if (retVal != PAPI_OK) {
113 fprintf(stderr, "Error on PAPI_start, retVal %d\n", retVal);
114 free(metric_values);
116 return 1;
117 }
118 //some work here
119 sleep(param.duration);
120 param.loops--;
121 for (uint32_t i=0; i<param.loops; i++) {
122 PAPI_read(event_set, metric_values);
123 for (int j=0; j<num_metrics; j++) {
124 printf("%-50s ...... %llu\n", metric_names[j], metric_values[j]);
125 }
126 printf("======\n");
127 if (param.reset) {
128 PAPI_reset(event_set);
129 }
130 //some work here
131 sleep(param.duration);
132 }
133 retVal = PAPI_stop(event_set, metric_values);
134 if (retVal != PAPI_OK) {
135 fprintf(stderr, "Error on PAPI_stop, retVal %d\n", retVal);
136 free(metric_values);
138 return 1;
139 }
140 for (int i=0; i<num_metrics; i++) {
141 printf("%-50s ...... %llu\n", metric_names[i], metric_values[i]);
142 }
143 free(metric_values);
145 return 0;
146}
int i
Enumerate PAPI preset or native events for a given component.
Get the event's name and description info.
Read hardware counters from an event set.
Reset the hardware event counts in an event set.
Finish using PAPI and free all related resources.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
#define PAPI_ENUM_EVENTS
Definition: f90papi.h:224
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_ENUM_FIRST
Definition: f90papi.h:85
#define PAPI_NULL
Definition: f90papi.h:78
int initPAPIGPUComp()
int parseInputParam(int argc, char **argv, InParams *param)
int initMetricSet(char **metric_names, int num_metrics, int *eventSet)
#define COMP_NAME
long long metric_values[MAX_NUM_METRICS]
static int total_metrics
#define PAPI_NATIVE_MASK
Return codes and api definitions.
FILE * stderr
int main()
Definition: pernode.c:20
uint32_t num_metrics
uint32_t duration
uint32_t loops
uint32_t reset
char ** metric_names