1LIBPFM(3) Linux Programmer's Manual LIBPFM(3)
2
3
4
6 pfm_dispatch_events - determine PMC registers values for a set of
7 events to measure
8
10 #include <perfmon/pfmlib.h>
11
12 int pfm_dispatch_events(pfmlib_input_param_t *p, void *mod_in, pfmlib_output_param_t *q,void *mod_out);
13
14
16 This function is the central piece of the library. It is important to
17 understand that the library does not effectively program the PMU, i.e.,
18 it does not make the operating system calls. The PMU is never actually
19 accessed by the library. Instead, the library helps applications pre‐
20 pare the arguments to pass to the kernel. In particular, it sets up the
21 values to program into the PMU configuration registers (PMC). The list
22 of used data registers (PMD) is also returned.
23
24 The input argument are divided into two categories: the generic argu‐
25 ments in p and the optional PMU model specific arguments in mod_in.
26 The same applies for the output arguments: q contains the generic out‐
27 put arguments and mod_out the optional PMU model specific arguments.
28
29 An application describes what it wants to measure in the in and if it
30 uses some model specific features, such as opcode matching on Itanium 2
31 processors, it must pass a pointer to the relevant model-specific input
32 parameters in mod_in. The generic output parameters contains the regis‐
33 ter index and values for the PMC and PMD registers needed to make the
34 measurement. The index mapping is guaranteed to match the mapping used
35 by the Linux perfmon2 interface. In case the library is not used on
36 this system, the hardware register addresses or indexes can also be
37 retrieved from the output structure.
38
39 The pfmlib_input_param_t structure is defined as follows:
40
41 typedef struct
42 int event;
43 unsigned int plm;
44 unsigned long flags;
45 unsigned int unit_masks[PFMLIB_MAX_MASKS_PER_EVENT];
46 unsigned int num_masks;
47 } pfmlib_event_t;
48
49 typedef struct {
50 unsigned int pfp_event_count;
51 unsigned int pfp_dfl_plm;
52 unsigned int pfp_flags;
53 pfmlib_event_t pfp_events[PFMLIB_MAX_PMCS];
54 pfmlib_regmask_t pfp_unavail_pmcs;
55 } pfmlib_input_param_t;
56
57 The structure mostly contains one table, called pfp_events which
58 describes the events to be measured. The number of submitted events is
59 indicated by pfp_event_count.
60
61 Each event is described in the pfp_events table by an opaque descriptor
62 stored in the event field. This descriptor is obtained with the
63 pfm_find_full_event() or derivative functions. For some events, it may
64 be necessary to specify at least one unit mask in the unit_masks table.
65 A unit mask is yet another opaque descriptor obtained via
66 pfm_find_event_mask() or pfm_find_full_event. Typically, if an event
67 supports multiple unit masks, they can be combined in which case more
68 than one entry in unit_masks must be specified. The actual number of
69 unit mask descriptors passed must be indicated in num_masks. When no
70 unit mask is used, this field must be set to 0.
71
72 A privilege level mask for the event can be provided in plm. This is a
73 bitmask where each bit indicates a privilege level at which to monitor,
74 more than one bit can be set. The library supports up to four levels,
75 but depending on the PMU model, some levels may not be available. The
76 levels are as follows:
77
78 PFM_PLM0
79 monitor at the privilege level 0. For many architectures, this
80 means kernel level
81
82 PFM_PLM1
83 monitor at privilege level 1
84
85 PFM_PLM2
86 monitor at privilege level 2
87
88 PFM_PLM3
89 monitor at the privilege level 3. For many architectures, this
90 means user level
91
92 Events with a plm value of 0 will use the default privilege level mask
93 as indicated by pfp_dfl_plm which must be set to any combinations of
94 values described above. It is illegal to have a value of 0 for this
95 field.
96
97 The pfp_flags field contains a set of flags that affect the whole set
98 of events to be monitored. The currently defined flags are:
99
100 PFMLIB_PFP_SYSTEMWIDE
101 indicates that the monitors are to be used in a system-wide mon‐
102 itoring session. This could influence the way the library sets
103 up some register values.
104
105
106 The pfp_unavail_pmcs bitmask can be used by applications to communicate
107 to the library the list of PMC registers which are not available on the
108 system. Some kernels may allocate certain PMC registers (and associ‐
109 ated data registers) for other purposes. Those registers must not be
110 used by the library otherwise the assignement of events to PMC regis‐
111 ters may be rejected by the kernel. Applications must figure out which
112 registers are available using a kernel interface at their disposal, the
113 library does not provide this service. The library expect the restric‐
114 tions to be expressed using the Linux perfmon2 PMC register mapping.
115
116 Refer to the PMU specific manual for a description of the model-spe‐
117 cific input parameters to be passed in mod_in.
118
119 The generic output parameters are contained in the fBpfmlib_out‐
120 put_param_t structure which is defined as:
121
122 typedef struct {
123 unsigned long long reg_value;
124 unsigned int reg_num;
125 unsigned long reg_addr;
126 } pfmlib_reg_t;
127
128 typedef struct {
129 unsigned int pfp_pmc_count;
130 unsigned int pfp_pmd_count;
131 pfmlib_reg_t pfp_pmcs[PFMLIB_MAX_PMCS];
132 pfmlib_reg_t pfp_pmds[PFMLIB_MAX_PMDS];
133 } pfmlib_output_param_t;
134
135 The number of valid entries in the pfp_pmcs table is indicated by
136 pfp_pmc_count. The number of valid entries in the pfp_pmds table is
137 indicated by pfp_pmd_count. Each entry in both tables is of type pfm‐
138 lib_reg_t.
139
140 In the pfp_pmcs table, the reg_num contains the PMC register index
141 (perfmon2 mapping), and the reg_value contains a 64-bit value to be
142 used to program the PMC register. The reg_addr indicates the hardware
143 address or index for the PMC register.
144
145 In the pfp_pmds table, the reg_num contains the PMD register index
146 (perfmon2 mapping). the reg_value is ignored. The reg_addr indicates
147 the hardware address or index for the PMC register.
148
149 Refer to the PMU specific manual for a description of the model-spe‐
150 cific output parameters to be returned in mod_out.
151
152 The current implementation of the pfm_dispatch_events completely over‐
153 writes the pfmlib_output_param structure. In other words, results do
154 not accumulate into the pfp_pmcs table across multiple calls. Unused
155 fields are guaranteed to be zeroed upon successful return.
156
157 Depending on the PMU model, there may not always be a one to one map‐
158 ping between a PMC register and a data register. Register dependencies
159 may be more intricate. However the pfm_dispatch_events guarantees cer‐
160 tain ordering between the pfp_pmcs and pfp_pmds tables. In particular,
161 it guarantees that the pfp_pmds table always starts with the counters
162 corresponding, in the same order, to the events as provided in the
163 pfp_event table on input. There is always one counter per event. Addi‐
164 tional PMD registers, if any, come after.
165
167 Here is a typical sequence using the perfmon2 interface:
168 #include <perfmon/pfmlib.h>
169 ...
170 pfmlib_input_param_t inp;
171 pfmlib_output_param_t outp;
172 pfarg_ctx_t ctx;
173 pfarg_pmd_t pd[1];
174 pfarg_pmc_t pc[1];
175 pfarg_load_t load_arg;
176 int fd, i;
177 int ret;
178
179 if (pfm_initialize() != PFMLIB_SUCCESS) {
180 fprintf(stderr, "can't initialize library\n");
181 exit(1);
182 }
183 memset(&ctx,0, sizeof(ctx));
184 memset(&inp,0, sizeof(inp));
185 memset(&outp,0, sizeof(outp));
186 memset(pd, 0, sizeof(pd));
187 memset(pc, 0, sizeof(pc));
188 memset(&load_arg, 0, sizeof(load_arg));
189
190 ret = pfm_get_cycle_event(&inp.pfp_events[0]);
191 if (ret != PFMLIB_SUCCESS) {
192 fprintf(stderr, "cannot find cycle event\n");
193 exit(1);
194 }
195 inp.pfp_dfl_plm = PFM_PLM3;
196 inp.pfp_event_count = 1;
197
198 ret = pfm_dispatch_events(&inp, NULL, &outp, NULL);
199 if (ret != PFMLIB_SUCCESS) {
200 fprintf(stderr, "cannot dispatch events: %s\n", pfm_strerror(ret));
201 exit(1);
202 }
203 /* propagate pmc value to perfmon2 structures */
204 for(i=0; i < outp.pfp_pmc_count; i++) {
205 pc[i].reg_num = outp.pfp_pmcs[i].reg_num;
206 pc[i].reg_value = outp.pfp_pmcs[i].reg_value;
207 }
208 for(i=0; i < outp.pfp_pmd_count; i++) {
209 pd[i].reg_num = outp.pfp_pmds[i].reg_num;
210 pd[i].reg_value = 0;
211 }
212 ...
213 if (pfm_create_context(&ctx, NULL, 0) == -1 ) {
214 ...
215 }
216 fd = ctx.ctx_fd;
217
218 if (pfm_write_pmcs(fd, pc, outp.pfp_pmc_count) == -1) {
219 ...
220 }
221 if (pfm_write_pmds(fd, pd, outp.pfp_pmd_count) == -1) {
222 ...
223 }
224
225 load_arg.load_pid = getpid();
226 if (pfm_load_context(fd, &load_arg) == -1) {
227 ...
228 }
229
230 pfm_start(fd, NULL);
231 /* code to monitor */
232 pfm_stop(fd);
233
234 if (pfm_read_pmds(fd, pd, evt.pfp_event_count) == -1) {
235 ...
236 }
237 printf("results: %llu0, pd[0].reg_value);
238 ...
239 close(fd);
240 ...
241
242
244 The function returns whether or not the call was successful. A return
245 value of PFMLIB_SUCCESS indicates sucess, otherwise the value is the
246 error code.
247
249 PFMLIB_ERR_NOINIT The library has not been initialized properly.
250
251 PFMLIB_ERR_INVAL
252 Some arguments were invalid. For instance the value of *count is
253 zero. This can also be due to he content of the pfmlib_param_t
254 structure.
255
256 PFMLIB_ERR_NOTFOUND
257 No matching event was found.
258
259 PFMLIB_ERR_TOOMANY
260 The number of events to monitor exceed the number of implemented
261 counters.
262
263 PFMLIB_ERR_NOASSIGN
264 The events cannot be dispatched to the PMC because events have
265 conflicting constraints.
266
267 PFMLIB_ERR_MAGIC
268 The model specific extension does not have the right magic num‐
269 ber.
270
271 PFMLIB_ERR_FEATCOMB
272 The set of events and features cannot be combined.
273
274 PFMLIB_ERR_EVTMANY
275 An event has been supplied more than once and is causing
276 resource (PMC) conflicts.
277
278 PFMLIB_ERR_IRRINVAL
279 Invalid code range restriction (Itanium, Itanium 2).
280
281 PFMLIB_ERR_IRRALIGN
282 Code range has invalid alignment (Itanium, Itanium 2).
283
284 PFMLIB_ERR_IRRTOOMANY
285 Cannot satisfy all the code ranges (Itanium, Itanium 2).
286
287 PFMLIB_ERR_DRRTOOMANY
288 Cannot satisfy all the data ranges (Itanium, Itanium 2).
289
290 PFMLIB_ERR_DRRINVAL
291 Invalid data range restriction (Itanium, Itanium 2).
292
293 PFMLIB_ERR_EVTSET
294 Some events belong to incompatible sets (Itanium 2).
295
296 PFMLIB_ERR_EVTINCOMP
297 Some events cannot be measured at the same time (Itanium 2).
298
299 PFMLIB_ERR_IRRTOOBIG
300 Code range is too big (Itanium 2).
301
302 PFMLIB_ERR_UMASK
303 Invalid or missing unit mask.
304
306 libpfm_itanium(3), libpfm_itanium2(3), pfm_regmask_set(3), pfm_reg‐
307 mask_clr(3), pfm_find_event_code_mask(3)
308
310 Stephane Eranian <eranian@hpl.hp.com>
311
312 July , 2003 LIBPFM(3)