-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpmu.c
More file actions
726 lines (654 loc) · 25.9 KB
/
Copy pathpmu.c
File metadata and controls
726 lines (654 loc) · 25.9 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2026 MoatLab, Virginia Tech. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/types.h>
#include <asm/unistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>
#include <linux/perf_event.h>
#include <assert.h>
#include <dirent.h>
#include <numa.h>
#include <numaif.h>
#include <time.h>
#include "constants.h"
#include "pact.h"
#include "pmu.h"
#include "perf.h"
#include "error.h"
/* Event configuration table for core/thread counting events.
* Populated at runtime by pmu_platform_init(). */
event_config_t core_event_configs[CORE_EVENT_COUNT];
int validate_hardware_access(void)
{
/* Initialize NUMA */
if (numa_available() < 0) {
log_warning("validate_hardware_access", "NUMA not available, migrations will be simulated");
return 0;
}
/* Check if we have the necessary permissions for hardware counters */
if (geteuid() != 0) {
log_warning("validate_hardware_access",
"Running without root privileges - hardware counters may not be accessible");
return 0; /* Not fatal, but degraded functionality */
}
/* Check if /proc/sys/kernel/perf_event_paranoid allows access */
FILE *paranoid_file = fopen("/proc/sys/kernel/perf_event_paranoid", "r");
if (paranoid_file) {
int paranoid_level;
if (fscanf(paranoid_file, "%d", ¶noid_level) == 1) {
if (paranoid_level > 1) {
log_warning("validate_hardware_access",
"perf_event_paranoid level is high - some counters may be restricted");
}
}
fclose(paranoid_file);
}
return 1; /* Success */
}
long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd,
unsigned long flags)
{
return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags);
}
/* Function to discover CHA PMUs for target CPUs only */
typedef struct {
int cha_id;
int pmu_type;
char device_name[64];
} cha_discovery_t;
/* Zero-init every cha_pmu_info_t slot to "unused" sentinels (-1 IDs, -1 fds). */
static void cha_pmus_clear_slots(cha_pmu_info_t *cha_pmus)
{
for (int i = 0; i < MAX_CHAS; i++) {
cha_pmus[i].cha_id = -1;
cha_pmus[i].pmu_type = -1;
cha_pmus[i].core_id = -1;
cha_pmus[i].device_name[0] = '\0';
for (int j = 0; j < 4; j++) {
cha_pmus[i].group_fast.fds[j] = -1;
cha_pmus[i].group_slow.fds[j] = -1;
cha_pmus[i].group_fast.ids[j] = -1;
cha_pmus[i].group_slow.ids[j] = -1;
}
}
}
/* Read PMU type for a single uncore_cha_* device and record it into the
* discovered[] buffer. Returns true on success. */
static bool record_cha_device(const char *dev_name, int cha_id, cha_discovery_t *out)
{
char path[256];
int ret = snprintf(path, sizeof(path), "/sys/devices/%s/type", dev_name);
if (ret >= (int)sizeof(path)) {
log_warning("detect_cha_pmus", "Path too long for device %s, skipping", dev_name);
return false;
}
FILE *fp = fopen(path, "r");
if (!fp) {
exit(EXIT_FAILURE); /* preserved: legacy behavior on /sys read failure */
}
int pmu_type;
bool ok = (fscanf(fp, "%d", &pmu_type) == 1);
fclose(fp);
if (!ok) {
return false;
}
out->cha_id = cha_id;
out->pmu_type = pmu_type;
strncpy(out->device_name, dev_name, sizeof(out->device_name) - 1);
out->device_name[sizeof(out->device_name) - 1] = '\0';
return true;
}
/* Discover all uncore_cha_* devices under /sys/devices. Fills the discovered[]
* buffer and returns count. */
static int discover_all_cha_devices(cha_discovery_t *discovered)
{
DIR *dir = opendir("/sys/devices");
if (!dir) {
perror("Failed to open /sys/devices");
return -1;
}
int n = 0;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (strncmp(entry->d_name, "uncore_cha_", 11) != 0) {
continue;
}
int cha_id = atoi(entry->d_name + 11);
if (cha_id < 0 || cha_id >= MAX_CHAS) {
log_warning("detect_cha_pmus", "CHA ID %d is out of bounds (0-%d), skipping", cha_id,
MAX_CHAS - 1);
exit(EXIT_FAILURE);
}
if (record_cha_device(entry->d_name, cha_id, &discovered[n])) {
n++;
}
}
closedir(dir);
return n;
}
/* Find the smallest pmu_type — used as the origin for CHA-offset → core
* mapping in g_pmu_platform.cha_to_core_map. */
static int find_base_pmu_type(const cha_discovery_t *discovered, int n)
{
int base = INT_MAX;
for (int i = 0; i < n; i++) {
if (discovered[i].pmu_type < base) {
base = discovered[i].pmu_type;
}
}
return base;
}
/* Filter the discovered CHAs by cpu_mask and populate cha_pmus[] for the
* active set. Returns count of active CHAs and writes count of skipped. */
static int filter_active_chas(const cha_discovery_t *discovered, int n_discovered,
int base_pmu_type, uint64_t cpu_mask, cha_pmu_info_t *cha_pmus,
int *out_skipped)
{
int active = 0, skipped = 0;
for (int i = 0; i < n_discovered; i++) {
int cha_id = discovered[i].cha_id;
int pmu_type = discovered[i].pmu_type;
int cha_offset = pmu_type - base_pmu_type;
if (cha_offset < 0 || cha_offset >= g_pmu_platform.nr_cha_mapping) {
log_debug("detect_cha_pmus",
"CHA %d: offset %d out of range (only %d mapped), skipping", cha_id,
cha_offset, g_pmu_platform.nr_cha_mapping);
skipped++;
continue;
}
int core_id = g_pmu_platform.cha_to_core_map[cha_offset];
if (core_id >= 0 && cpu_mask != 0 && !(cpu_mask & (1ULL << core_id))) {
log_debug("detect_cha_pmus", "Skipping CHA %d (core %d not in target CPU mask 0x%lx)",
cha_id, core_id, cpu_mask);
skipped++;
continue;
}
cha_pmus[active].cha_id = cha_id;
cha_pmus[active].pmu_type = pmu_type;
cha_pmus[active].core_id = core_id;
strncpy(cha_pmus[active].device_name, discovered[i].device_name,
sizeof(cha_pmus[active].device_name) - 1);
cha_pmus[active].device_name[sizeof(cha_pmus[active].device_name) - 1] = '\0';
log_info("detect_cha_pmus", "Added CHA %d: %s (PMU type %d, core %d) at index %d", cha_id,
discovered[i].device_name, pmu_type, core_id, active);
active++;
}
*out_skipped = skipped;
return active;
}
int discover_cha_pmus(cha_pmu_info_t *cha_pmus, int *nr_cha, uint64_t cpu_mask)
{
cha_pmus_clear_slots(cha_pmus);
cha_discovery_t discovered[MAX_CHAS];
int n_discovered = discover_all_cha_devices(discovered);
if (n_discovered < 0) {
return -1;
}
if (n_discovered == 0) {
log_info("detect_cha_pmus", "No CHA PMUs discovered");
*nr_cha = 0;
return 0;
}
log_info("detect_cha_pmus", "Total CHA PMUs discovered: %d", n_discovered);
int base = find_base_pmu_type(discovered, n_discovered);
int skipped = 0;
int active = filter_active_chas(discovered, n_discovered, base, cpu_mask, cha_pmus, &skipped);
*nr_cha = active;
if (skipped > 0) {
log_info("detect_cha_pmus", "Active CHA PMUs: %d (skipped %d CHAs not in target CPU mask)",
active, skipped);
} else {
log_info("detect_cha_pmus", "Active CHA PMUs: %d", active);
}
return active;
}
int setup_tor_events(event_group_t *event_group, int pmu_type, int tier, int core_id)
{
const pmu_platform_t *plat = &g_pmu_platform;
static const char *event_names[CHA_EVENT_COUNT] = {"TOR_OCCUPANCY", "TOR_CYCLES"};
int leader_fd = -1;
for (int i = 0; i < CHA_EVENT_COUNT; i++) {
struct perf_event_attr pe;
memset(&pe, 0, sizeof(pe));
pe.size = sizeof(pe);
pe.type = pmu_type;
pe.sample_type = PERF_SAMPLE_IDENTIFIER;
pe.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING |
PERF_FORMAT_GROUP | PERF_FORMAT_ID;
/* All config composition delegated to the platform */
tor_pe_config_t cfg;
plat->fill_tor_config(plat, &cfg, i, tier, core_id);
pe.config = cfg.config;
pe.config1 = cfg.config1;
pe.config2 = cfg.config2;
pe.disabled = (i == CHA_TOR_OCCUPANCY) ? 1 : 0;
pe.exclude_kernel = 0;
pe.exclude_hv = 0;
pe.exclude_idle = 0;
pe.inherit = 1;
int group_fd = (i == CHA_TOR_OCCUPANCY) ? -1 : leader_fd;
int fd = perf_event_open(&pe, -1, 0, group_fd, 0);
if (fd == -1) {
log_warning("setup_tor_events", "Error opening %s event (tier %d): %s", event_names[i],
tier, strerror(errno));
return -1;
}
ioctl(fd, PERF_EVENT_IOC_ID, &event_group->ids[i]);
event_group->fds[i] = fd;
event_group->counters_used++;
if (i == CHA_TOR_OCCUPANCY) {
leader_fd = fd;
}
}
return 0;
}
/* Setup perf events for active CHAs (already filtered by discover_cha_pmus) */
static void close_event_group_fds(event_group_t *g)
{
for (int j = 0; j < g->counters_used; j++) {
if (g->fds[j] != -1) {
close(g->fds[j]);
g->fds[j] = -1;
}
}
}
/* Cleanup partially-opened TOR event fds for one CHA after setup failure.
* Fixes a prior bug where both loops indexed group_slow.fds, leaking the
* group_fast fds when slow-group setup failed after fast-group success. */
static void cleanup_partial_cha_setup(cha_pmu_info_t *cha)
{
close_event_group_fds(&cha->group_fast);
close_event_group_fds(&cha->group_slow);
}
/* Open the fast (tier 0) and slow (tier 1) TOR event groups for one CHA.
* On any group failure, both groups' fds are closed via cleanup helper.
* Returns the number of events successfully opened (0 on failure). */
static int setup_one_cha_tor_groups(cha_pmu_info_t *cha)
{
int fast_ok = setup_tor_events(&cha->group_fast, cha->pmu_type, 0, cha->core_id) == 0;
int slow_ok =
fast_ok && setup_tor_events(&cha->group_slow, cha->pmu_type, 1, cha->core_id) == 0;
if (slow_ok) {
return cha->group_fast.counters_used + cha->group_slow.counters_used;
}
fprintf(stderr, " Failed to setup TOR events for CHA %d, skipping\n", cha->cha_id);
cleanup_partial_cha_setup(cha);
return 0;
}
int setup_pmu_cha_perf_events(cha_pmu_info_t *cha_pmus, int *nr_cha)
{
int total_events = 0;
int active_cha_count = 0;
for (int i = 0; i < *nr_cha; i++) {
log_info("setup_uncore_events", "Setting up CHA %d (%s, PMU type %d, core %d)",
cha_pmus[i].cha_id, cha_pmus[i].device_name, cha_pmus[i].pmu_type,
cha_pmus[i].core_id);
int n = setup_one_cha_tor_groups(&cha_pmus[i]);
if (n > 0) {
active_cha_count++;
total_events += n;
}
}
log_info("setup_uncore_events", "Successfully opened %d individual events across %d CHAs",
total_events, active_cha_count);
return total_events;
}
/* manipulate CHA PMU counters */
void ioctl_pmu_cha_perf_events(cha_pmu_info_t *cha_pmus, int nr_cha, int request)
{
for (int i = 0; i < nr_cha; i++) {
ioctl(cha_pmus[i].group_fast.fds[0], request, PERF_IOC_FLAG_GROUP);
ioctl(cha_pmus[i].group_slow.fds[0], request, PERF_IOC_FLAG_GROUP);
}
}
/* manipulate per-cpu PMU counters, includes fast/slow tier pebs, and counting events if no pid is specified */
void ioctl_pmu_core_perf_events(per_cpu_state_t *cpu_states, int nr_target_cpus, int request)
{
for (int i = 0; i < nr_target_cpus; i++) {
ioctl(cpu_states[i].leader.fd, request, PERF_IOC_FLAG_GROUP);
}
}
void start_pmu_perf_events(pact_context_t *ctx)
{
pact_workload_t *wl = ctx->workload;
ioctl_pmu_core_perf_events(ctx->cpu_states, ctx->nr_all_cpus, PERF_EVENT_IOC_RESET);
ioctl_pmu_cha_perf_events(wl->cha_pmus, wl->nr_cha, PERF_EVENT_IOC_RESET);
if (wl->counting_leader.fd >= 0) {
ioctl(wl->counting_leader.fd, PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP);
}
ioctl_pmu_cha_perf_events(wl->cha_pmus, wl->nr_cha, PERF_EVENT_IOC_ENABLE);
if (wl->counting_leader.fd >= 0) {
ioctl(wl->counting_leader.fd, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP);
}
ioctl_pmu_core_perf_events(ctx->cpu_states, ctx->nr_all_cpus, PERF_EVENT_IOC_ENABLE);
}
void stop_pmu_perf_events(pact_context_t *ctx)
{
pact_workload_t *wl = ctx->workload;
ioctl_pmu_cha_perf_events(wl->cha_pmus, wl->nr_cha, PERF_EVENT_IOC_DISABLE);
if (wl->counting_leader.fd >= 0) {
ioctl(wl->counting_leader.fd, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP);
}
ioctl_pmu_core_perf_events(ctx->cpu_states, ctx->nr_all_cpus, PERF_EVENT_IOC_DISABLE);
}
void read_pmu_cha_perf_events(cha_pmu_info_t *cha_pmus, int nr_cha)
{
for (int i = 0; i < nr_cha; i++) {
read_pmu_event_group(&cha_pmus[i].group_fast);
read_pmu_event_group(&cha_pmus[i].group_slow);
/* scale readings with time_enabled / time_running */
scale_multiplexed_events(&cha_pmus[i].group_fast);
scale_multiplexed_events(&cha_pmus[i].group_slow);
}
}
/* Read a perf event group via leader_fd. Matches the N values returned by
* the kernel to the caller's expected event IDs and writes them into
* values[]; unmatched slots are left zeroed. Returns bytes read (>0 on
* success, 0 on empty, <0 on error). */
static int read_perf_event_group_raw(int leader_fd, const uint64_t *ids, uint64_t *values, int n,
uint64_t *out_time_enabled, uint64_t *out_time_running)
{
char buf[4096];
read_format_t *rf = (read_format_t *)buf;
int bytes_read = read(leader_fd, buf, sizeof(buf));
if (bytes_read <= 0) {
return bytes_read;
}
for (int j = 0; j < n; j++) {
values[j] = 0;
}
for (uint64_t i = 0; i < rf->nr; i++) {
for (int j = 0; j < n; j++) {
if (rf->values[i].id == ids[j]) {
values[j] = rf->values[i].value;
break;
}
}
}
if (out_time_enabled) {
*out_time_enabled = rf->time_enabled;
}
if (out_time_running) {
*out_time_running = rf->time_running;
}
return bytes_read;
}
/* Read a perf_event_t[N] group (per_cpu_state_t / pact_workload_t shape):
* gather ids, call the raw helper, scatter values back. */
static int read_perf_event_array(perf_event_t *leader, perf_event_t *events, int n)
{
if (leader->fd < 0) {
return -1;
}
uint64_t ids[CORE_EVENT_COUNT];
uint64_t values[CORE_EVENT_COUNT];
for (int j = 0; j < n; j++) {
ids[j] = events[j].id;
}
int bytes_read = read_perf_event_group_raw(leader->fd, ids, values, n, &leader->time_enabled,
&leader->time_running);
if (bytes_read <= 0) {
return bytes_read;
}
for (int j = 0; j < n; j++) {
events[j].value = (events[j].fd >= 0) ? values[j] : 0;
}
return bytes_read;
}
/* Read the workload's per-PID counting group (single fd, all events in
* one read). Counts are kernel-aggregated across all threads of the
* workload via inherit=1. */
static void read_workload_counting_events(pact_workload_t *wl)
{
int bytes_read =
read_perf_event_array(&wl->counting_leader, wl->counting_events, CORE_EVENT_COUNT);
if (bytes_read <= 0 && wl->counting_leader.fd >= 0) {
log_warning("read_workload_counting_events",
"Failed to read workload PID %d: bytes_read=%d, errno=%s", wl->target_pid,
bytes_read, strerror(errno));
}
}
int setup_dummy_leader_event(perf_event_t *perf_event, pid_t pid, int cpu)
{
struct perf_event_attr pe;
memset(&pe, 0, sizeof(pe));
pe.type = PERF_TYPE_SOFTWARE;
pe.config = PERF_COUNT_SW_DUMMY;
pe.size = sizeof(pe);
pe.disabled = 1; /* ONLY group leader start disabled*/
pe.inherit = 1;
pe.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING |
PERF_FORMAT_GROUP | PERF_FORMAT_ID;
perf_event->fd = perf_event_open(&pe, pid, cpu, -1, 0);
if (perf_event->fd == -1) {
log_error("setup_dummy_leader_event", "perf_event_open(pid=%d, cpu=%d) failed: %s", pid,
cpu, strerror(errno));
return -1;
}
return 0;
}
/* Setup PEBS for LLC miss sampling, for the target CPUs */
int setup_pebs_event(per_cpu_state_t *cpu_state, pid_t pid, int cpu)
{
struct perf_event_attr pe;
memset(&pe, 0, sizeof(pe));
pe.type = PERF_TYPE_RAW;
pe.size = sizeof(pe);
pe.sample_period = cpu_state->pebs_sampling_period;
pe.sample_type = PERF_SAMPLE_ADDR | PERF_SAMPLE_TID;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;
pe.exclude_idle = 1;
pe.mmap = 1;
pe.precise_ip = 2; /* Request PEBS */
pe.inherit = 1;
pe.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING |
PERF_FORMAT_GROUP | PERF_FORMAT_ID;
/* MEM_LOAD_L3_MISS_RETIRED.REMOTE_DRAM — slow tier only */
pe.config = g_pmu_platform.event_llc_miss_remote;
cpu_state->fd_pebs = perf_event_open(&pe, pid, cpu, cpu_state->leader.fd, 0);
if (cpu_state->fd_pebs < 0) {
if (errno == EACCES) {
log_error("setup_pebs_sampling",
"Permission denied for PEBS - run as root or adjust perf_event_paranoid");
} else if (errno == ENODEV) {
log_error("setup_pebs_sampling", "PEBS not supported on this hardware");
} else {
log_error("setup_pebs_sampling", "Failed to open PEBS event");
}
return -1;
}
log_info("setup_pebs_sampling", "CPU [%d] PEBS event fd:%d (REMOTE_DRAM)", cpu,
cpu_state->fd_pebs);
/* Map the buffer */
size_t mmap_size = (1 + PERF_BUFFER_PAGES) * PAGE_SIZE;
cpu_state->pebs_mmap =
mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, cpu_state->fd_pebs, 0);
if (cpu_state->pebs_mmap == MAP_FAILED) {
log_error("setup_pebs_sampling", "Failed to mmap PEBS buffer");
safe_close(cpu_state->fd_pebs, "setup_pebs_sampling");
cpu_state->fd_pebs = -1;
return -1;
}
return 0;
}
int setup_counting_event(perf_event_t *perf_event, pid_t pid, int cpu, perf_event_t *leader,
uint64_t config, const char *name)
{
struct perf_event_attr pe;
memset(&pe, 0, sizeof(pe));
pe.type = PERF_TYPE_RAW;
pe.size = sizeof(pe);
pe.config = config; /* event config */
pe.sample_type = PERF_SAMPLE_IDENTIFIER; /* key for counting mode */
pe.sample_period = 0;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;
pe.inherit = 1;
pe.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING |
PERF_FORMAT_GROUP | PERF_FORMAT_ID;
/* CRITICAL: Leader must start disabled, members inherit this */
if (leader == NULL) {
pe.disabled = 1; /* This is the leader - start disabled */
} else {
pe.disabled = 0; /* Group member - will follow leader's state */
}
perf_event->fd = perf_event_open(&pe, pid, cpu, (leader == NULL ? -1 : leader->fd), 0x8);
if (perf_event->fd < 0) {
log_error("setup_counting_event", "Failed to open counting event config=0x%llx", config);
perf_event->fd = -1;
exit(EXIT_FAILURE);
} else {
log_info("setup_counting_event", "thread [%d] counting event %s (config=0x%llx) fd:%d", pid,
name, config, perf_event->fd);
}
ioctl(perf_event->fd, PERF_EVENT_IOC_ID, &perf_event->id);
return 0;
}
/* Read an event group */
int read_pmu_event_group(event_group_t *event_group)
{
if (event_group->fds[0] < 0) {
return -1; /* Group not available */
}
uint64_t te = event_group->time_enabled, tr = event_group->time_running;
int bytes_read =
read_perf_event_group_raw(event_group->fds[0], event_group->ids, event_group->values,
event_group->counters_used, &te, &tr);
if (bytes_read > 0) {
event_group->last_time_enabled = event_group->time_enabled;
event_group->last_time_running = event_group->time_running;
event_group->time_enabled = te;
event_group->time_running = tr;
}
return 0;
}
void scale_multiplexed_events(event_group_t *event_group)
{
double scale_factor = 1.0;
if (event_group->time_running - event_group->last_time_running == 0) {
return; /* No change in time_running, no scaling needed */
}
scale_factor = (double)(event_group->time_enabled - event_group->last_time_enabled) /
(double)(event_group->time_running - event_group->last_time_running);
for (int i = 0; i < event_group->counters_used; i++) {
event_group->values[i] = (uint64_t)((double)event_group->values[i] * scale_factor);
}
}
/*
* Per-tier MLP from CHA TOR counters (Algorithm 1): MLP = ΔT1/ΔT2, where
* T1 accumulates TOR occupancy and T2 counts cycles with at least one
* outstanding TOR entry. Occupancy and cycle deltas are summed across all
* of the workload's CHAs before dividing, so each tier yields one ratio
* per sampling window. Counters are IOC_RESET at window start, so group
* values are true window deltas; each group's values were already
* multiplex-corrected by scale_multiplexed_events(), so summing across
* groups with different schedule fractions is sound.
*/
static double calculate_tier_mlp(pact_workload_t *wl, int tier)
{
uint64_t sum_occupancy = 0;
uint64_t sum_cycles = 0;
int valid_groups = 0;
for (int cha = 0; cha < wl->nr_cha; cha++) {
event_group_t *group =
(tier == 0) ? &wl->cha_pmus[cha].group_fast : &wl->cha_pmus[cha].group_slow;
/* Skip groups scheduled for under 1 ms of THIS window; their
* scaled-up readings are noise. time_running is cumulative since
* open (IOC_RESET clears only counter values), so the window's
* share is the delta against the previous read. */
if (group->time_running - group->last_time_running < 1000000) {
continue;
}
valid_groups++;
sum_occupancy += group->values[CHA_TOR_OCCUPANCY];
sum_cycles += group->values[CHA_TOR_CYCLES];
}
if (valid_groups == 0) {
return -1.0; /* no valid measurement this window (all multiplexed out) */
}
if (sum_occupancy == 0 || sum_cycles == 0) {
return g_pmu_platform.mlp_min; /* measured, but no traffic to this tier */
}
double mlp = (double)sum_occupancy / (double)sum_cycles;
if (mlp < g_pmu_platform.mlp_min) {
mlp = g_pmu_platform.mlp_min;
}
if (mlp > g_pmu_platform.mlp_max) {
mlp = g_pmu_platform.mlp_max;
}
return mlp;
}
/* Fresh per-tier MLP ratios for this window; no cross-window smoothing.
* A window with no valid measurement (every CHA group multiplexed out)
* keeps the previous window's value instead of fabricating the minimum,
* which would over-attribute stalls for that window. */
static void calculate_workload_mlp(pact_workload_t *wl)
{
double fast = calculate_tier_mlp(wl, 0);
double slow = calculate_tier_mlp(wl, 1);
if (fast >= 0.0) {
wl->workload_mlp_fast = fast;
}
if (slow >= 0.0) {
wl->workload_mlp_slow = slow;
}
}
/* Read the per-PID counting group (single fd with inherit=1, aggregating
* across all child threads of the workload) and roll up into workload
* stats. MLP is computed from CHA TOR ratios (Algorithm 1). */
void read_pmu_counting_events(pact_context_t *ctx)
{
pact_workload_t *wl = ctx->workload;
read_workload_counting_events(wl);
uint64_t wl_fast = wl->counting_events[CORE_EVENT_LLC_MISS_FAST].value;
uint64_t wl_slow = wl->counting_events[CORE_EVENT_LLC_MISS_SLOW].value;
log_trace("read_pmu_counting_events", "Workload LLC misses fast=%lu, slow=%lu", wl_fast,
wl_slow);
wl->stats.llc_misses_fast = wl_fast;
wl->stats.llc_misses_slow = wl_slow;
if (wl->counting_leader.fd >= 0) {
wl->stats.time_running = wl->counting_leader.time_running - wl->stats.last_time_running;
wl->stats.last_time_running = wl->counting_leader.time_running;
}
read_pmu_cha_perf_events(wl->cha_pmus, wl->nr_cha);
calculate_workload_mlp(wl);
log_debug("read_pmu_counting_events", "Workload (PID %d) MLP: fast=%.2f, slow=%.2f",
wl->target_pid, wl->workload_mlp_fast, wl->workload_mlp_slow);
}
/* Open one counting group per workload: dummy SW leader + LLC-miss events,
* all on (pid=target_pid, cpu=-1, inherit=1, mmap=0). Kernel auto-attributes
* counts across every thread of the workload via inherit. Replaces the
* per-TID setup that needed /proc/<pid>/task polling. */
void setup_workload_counting_events(pact_workload_t *wl)
{
init_perf_event(&wl->counting_leader);
for (int j = 0; j < CORE_EVENT_COUNT; j++) {
init_perf_event(&wl->counting_events[j]);
}
if (setup_dummy_leader_event(&wl->counting_leader, wl->target_pid, -1) < 0) {
log_error("setup_workload_counting_events",
"Failed to create counting leader for workload PID %d", wl->target_pid);
return;
}
for (int j = 0; j < CORE_EVENT_COUNT; j++) {
if (setup_counting_event(&wl->counting_events[j], wl->target_pid, -1, &wl->counting_leader,
core_event_configs[j].config, core_event_configs[j].name) < 0) {
log_error("setup_workload_counting_events",
"Failed to setup %s event for workload PID %d", core_event_configs[j].name,
wl->target_pid);
return;
}
}
log_info("setup_workload_counting_events",
"Workload PID %d: per-workload counting events setup (1 fd per event, inherit=1)",
wl->target_pid);
}