1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
#include <stdio.h>
#include <stdlib.h>
#include "pmu.h"
#define MAX_SLOT_NUM 64 /* defined by ARMv8 SPEC*/
#define ARMV8_PMCR_N_SHIFT 11 /* Number of counters supported */
#define ARMV8_PMCR_N_MASK 0x1f
static __thread struct pmu_event_record * g_rec_ptr[MAX_SLOT_NUM];
static __thread int max_counter_slot;
/* start and stop counter */
static void stop_event_profile(struct pmu_event_record * p_record)
{
int slot=p_record->p_evt->slot;
p_record->p_evt->enabled=0;
stop_pmu_counter(slot);
}
static void init_pmu_event_record(struct pmu_event * p_evt, struct pmu_event_record * p_record)
{
struct prof_stat *p_stat;
int i=0;
p_record->p_evt=p_evt;
p_record->last_val=p_evt->init_val;
p_record->base_val=p_evt->init_val;
p_stat=p_record->prof_stat;
for(i=0;i<MAX_PROF_POINTS;i++)
{
p_stat[i].prof_seq=i;
p_stat[i].max_val=0;
p_stat[i].min_val=-1U;
p_stat[i].raw_val=0xdeadbeaf;
p_stat[i].cur_val=0;
p_stat[i].total_val=0;
p_stat[i].enter_count=0;
}
}
static void start_event_profile(struct pmu_event_record * p_record)
{
int slot=p_record->p_evt->slot;
struct prof_stat *p_stat;
int i;
p_record->p_evt->enabled=1;
p_stat=p_record->prof_stat;
for(i=0;i<MAX_PROF_POINTS;i++)
{
p_stat[i].prof_seq=i;
p_stat[i].max_val=0;
p_stat[i].min_val=-1U;
p_stat[i].raw_val=0xdeadbeaf;
p_stat[i].cur_val=0;
p_stat[i].total_val=0;
p_stat[i].enter_count=0;
}
write_pmu_counter(slot,p_record->p_evt->init_val);
start_pmu_counter(slot);
}
/* create event and profile */
int setup_event_counter(int slot, int event_id)
{
if(slot==31)
return 0;
if(event_id>1023)
return -1;
write_32bit_sysreg(PMSELR_EL0,slot);
write_32bit_sysreg(PMXEVTYPER_EL0,event_id);
return 0;
}
static struct pmu_event_record * create_pmu_event_record(char *name, int slot,
int event_id, uint32_t init_val, char * note)
{
struct pmu_event * p_evt;
struct pmu_event_record * p_record;
if(setup_event_counter(slot,event_id)<0)
return NULL;
p_evt=malloc(sizeof(struct pmu_event));
if(p_evt==NULL)
return NULL;
p_evt->name=name;
p_evt->slot=slot;
p_evt->event_id=event_id;
p_evt->init_val=init_val;
p_evt->note=note;
p_evt->enabled=0;
p_record=malloc(sizeof(struct pmu_event_record));
if(p_record==NULL)
{
free(p_evt);
return NULL;
}
p_record->p_evt=p_evt;
init_pmu_event_record(p_evt,p_record);
return p_record;
}
static void record_event_prof(struct pmu_event_record * p_record,
int prof_seq, int cal_offset, int update_last)
{
struct prof_stat * p_stat;
uint32_t evt_val;
evt_val=read_pmu_counter(p_record->p_evt->slot);
p_stat=&p_record->prof_stat[prof_seq];
p_stat->cal_offset=cal_offset;
p_stat->update_last=update_last;
p_stat->raw_val=evt_val;
if(cal_offset)
p_stat->cur_val=evt_val-p_record->last_val;
else
p_stat->cur_val=evt_val-p_record->base_val;
if(update_last)
p_record->last_val=evt_val;
p_stat->total_val+=p_stat->cur_val;
if(p_stat->cur_val>p_stat->max_val)
p_stat->max_val=p_stat->cur_val;
if(p_stat->cur_val<p_stat->min_val)
p_stat->min_val=p_stat->cur_val;
p_stat->enter_count++;
}
static void release_pmu_event_record(struct pmu_event_record * p_record)
{
struct pmu_event * p_evt;
p_evt=p_record->p_evt;
if(p_evt->enabled)
stop_pmu_counter(p_evt->slot);
free(p_evt);
free(p_record);
}
/* debugging */
static void dump_pmu_event(struct pmu_event * p_evt)
{
printf("event[%s/0x%x]: slot [%d] init_val[0x%x] enabled[%d]",
p_evt->name,p_evt->event_id,p_evt->slot,p_evt->init_val,
p_evt->enabled);
if(p_evt->note)
printf(" note[%s]\n",p_evt->note);
else
printf("\n");
}
static void dump_pmu_event_record(struct pmu_event_record * p_record)
{
int i;
struct prof_stat * p_stat;
uint64_t total_avg_val=0;
int count=0;
uint32_t avg;
printf("------------------------------------------------------------------------\n");
dump_pmu_event(p_record->p_evt);
p_stat=&p_record->prof_stat[0];
for(i=0;i<MAX_PROF_POINTS;i++)
{
if(p_stat[i].enter_count==0)
continue;
avg=(uint32_t)(p_stat[i].total_val/p_stat[i].enter_count);
printf("stat [%d]: max/min/avg [0x%x/0x%x/0x%x] total [0x%lx] count[%u]\n",
i,p_stat[i].max_val,p_stat[i].min_val,
avg,
p_stat[i].total_val,p_stat[i].enter_count);
printf(" raw_val[0x%x] cal_offset[%d] update_last[%d]\n",
p_stat[i].raw_val,p_stat[i].cal_offset,p_stat[i].update_last);
count++;
total_avg_val+=avg;
}
printf("total [%d] points, the sum of average number is: [0x%lx]\n\n",count,total_avg_val);
}
/* output interface */
void init_pmu_registers(void)
{
/* enabled PMU in PMCR*/
write_32bit_sysreg(PMCR_EL0,0x1);
max_counter_slot=(read_32bit_sysreg(PMCR_EL0) >> ARMV8_PMCR_N_SHIFT)&ARMV8_PMCR_N_MASK;
}
#define dump_32bit_sysreg(reg) \
printf(__stringify(reg) " is [0x%08x]\n",read_32bit_sysreg(reg))
#define dump_64bit_sysreg(reg) \
printf(__stringify(reg) " is [0x%016llx]\n",read_32bit_sysreg(reg))
void dump_pmu_registers(void)
{
dump_32bit_sysreg(PMCEID0_EL0);
dump_32bit_sysreg(PMCEID1_EL0);
dump_32bit_sysreg(PMOVSSET_EL0);
dump_32bit_sysreg(PMCR_EL0);
dump_32bit_sysreg(PMUSERENR_EL0);
dump_32bit_sysreg(PMCNTENSET_EL0);
}
struct pmu_event_record * get_pmu_event_record(int slot)
{
return g_rec_ptr[slot];
}
int create_pmu_event(char *name,int event_id,
uint32_t init_val, char * note)
{
int i;
struct pmu_event_record * p_record;
for(i=0;i<max_counter_slot;i++)
{
if(g_rec_ptr[i]==NULL)
break;
}
if(i==max_counter_slot)
return -1;
p_record=create_pmu_event_record(name,i,event_id,init_val,note);
if(p_record==NULL)
return -1;
g_rec_ptr[i]=p_record;
return i;
}
void release_pmu_event(int slot)
{
struct pmu_event_record * p_record;
p_record=g_rec_ptr[slot];
if(p_record)
release_pmu_event_record(p_record);
g_rec_ptr[slot]=NULL;
}
void start_pmu_event(int slot)
{
struct pmu_event_record * p_record;
p_record=g_rec_ptr[slot];
start_event_profile(p_record);
}
void stop_pmu_event(int slot)
{
struct pmu_event_record * p_record;
p_record=g_rec_ptr[slot];
stop_event_profile(p_record);
}
void record_pmu_event(int slot, int seq, int cal_offset, int update_last)
{
struct pmu_event_record * p_record;
p_record=g_rec_ptr[slot];
record_event_prof(p_record,seq,cal_offset,update_last);
}
void dump_pmu_event_stat(int slot)
{
struct pmu_event_record * p_record;
p_record=g_rec_ptr[slot];
dump_pmu_event_record(p_record);
}
uint32_t get_pmu_stat_avg(int slot)
{
struct pmu_event_record * p_record;
struct prof_stat * p_stat;
uint32_t total_avg=0;
uint32_t avg;
int i;
p_record=g_rec_ptr[slot];
for(i=0;i<MAX_PROF_POINTS;i++)
{
p_stat=&p_record->prof_stat[i];
if(p_stat->enter_count==0)
continue;
avg=p_stat->total_val/p_stat->enter_count;
total_avg+=avg;
}
return total_avg;
}
void set_pmu_event_base(int slot)
{
struct pmu_event_record * p_record;
uint32_t val;
p_record=g_rec_ptr[slot];
val=read_pmu_counter(slot);
p_record->last_val=val;
p_record->base_val=val;
}
|