xref: /linux/tools/perf/arch/x86/util/event.c (revision ad7ad6b5ddf63b436a5344fb686887f2d8b7cf3d)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
3 #include <linux/string.h>
4 #include <linux/zalloc.h>
5 #include <stdlib.h>
6 
7 #include "../../../util/event.h"
8 #include "../../../util/synthetic-events.h"
9 #include "../../../util/machine.h"
10 #include "../../../util/tool.h"
11 #include "../../../util/map.h"
12 #include "../../../util/debug.h"
13 
14 #if defined(__x86_64__)
15 
16 int perf_event__synthesize_extra_kmaps(struct perf_tool *tool,
17 				       perf_event__handler_t process,
18 				       struct machine *machine)
19 {
20 	int rc = 0;
21 	struct map *pos;
22 	struct maps *kmaps = machine__kernel_maps(machine);
23 	union perf_event *event = zalloc(sizeof(event->mmap) +
24 					 machine->id_hdr_size);
25 
26 	if (!event) {
27 		pr_debug("Not enough memory synthesizing mmap event "
28 			 "for extra kernel maps\n");
29 		return -1;
30 	}
31 
32 	maps__for_each_entry(kmaps, pos) {
33 		struct kmap *kmap;
34 		size_t size;
35 
36 		if (!__map__is_extra_kernel_map(pos))
37 			continue;
38 
39 		kmap = map__kmap(pos);
40 
41 		size = sizeof(event->mmap) - sizeof(event->mmap.filename) +
42 		       PERF_ALIGN(strlen(kmap->name) + 1, sizeof(u64)) +
43 		       machine->id_hdr_size;
44 
45 		memset(event, 0, size);
46 
47 		event->mmap.header.type = PERF_RECORD_MMAP;
48 
49 		/*
50 		 * kernel uses 0 for user space maps, see kernel/perf_event.c
51 		 * __perf_event_mmap
52 		 */
53 		if (machine__is_host(machine))
54 			event->header.misc = PERF_RECORD_MISC_KERNEL;
55 		else
56 			event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
57 
58 		event->mmap.header.size = size;
59 
60 		event->mmap.start = pos->start;
61 		event->mmap.len   = pos->end - pos->start;
62 		event->mmap.pgoff = pos->pgoff;
63 		event->mmap.pid   = machine->pid;
64 
65 		strlcpy(event->mmap.filename, kmap->name, PATH_MAX);
66 
67 		if (perf_tool__process_synth_event(tool, event, machine,
68 						   process) != 0) {
69 			rc = -1;
70 			break;
71 		}
72 	}
73 
74 	free(event);
75 	return rc;
76 }
77 
78 #endif
79 
80 void arch_perf_parse_sample_weight(struct perf_sample *data,
81 				   const __u64 *array, u64 type)
82 {
83 	union perf_sample_weight weight;
84 
85 	weight.full = *array;
86 	if (type & PERF_SAMPLE_WEIGHT)
87 		data->weight = weight.full;
88 	else {
89 		data->weight = weight.var1_dw;
90 		data->ins_lat = weight.var2_w;
91 	}
92 }
93 
94 void arch_perf_synthesize_sample_weight(const struct perf_sample *data,
95 					__u64 *array, u64 type)
96 {
97 	*array = data->weight;
98 
99 	if (type & PERF_SAMPLE_WEIGHT_STRUCT) {
100 		*array &= 0xffffffff;
101 		*array |= ((u64)data->ins_lat << 32);
102 	}
103 }
104