xref: /xnu-8019.80.24/osfmk/kern/perfmon.h (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
1 // Copyright (c) 2020 Apple Inc. All rights reserved.
2 //
3 // @APPLE_OSREFERENCE_LICENSE_HEADER_START@
4 //
5 // This file contains Original Code and/or Modifications of Original Code
6 // as defined in and that are subject to the Apple Public Source License
7 // Version 2.0 (the 'License'). You may not use this file except in
8 // compliance with the License. The rights granted to you under the License
9 // may not be used to create, or enable the creation or redistribution of,
10 // unlawful or unlicensed copies of an Apple operating system, or to
11 // circumvent, violate, or enable the circumvention or violation of, any
12 // terms of an Apple operating system software license agreement.
13 //
14 // Please obtain a copy of the License at
15 // http://www.opensource.apple.com/apsl/ and read it before using this file.
16 //
17 // The Original Code and all software distributed under the License are
18 // distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
19 // EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
20 // INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
22 // Please see the License for the specific language governing rights and
23 // limitations under the License.
24 //
25 // @APPLE_OSREFERENCE_LICENSE_HEADER_END@
26 
27 #ifndef KERN_PERFMON_H
28 #define KERN_PERFMON_H
29 
30 #include <stdbool.h>
31 #include <stddef.h>
32 #include <stdint.h>
33 #include <sys/cdefs.h>
34 #include <sys/perfmon_private.h>
35 
36 __BEGIN_DECLS
37 
38 enum perfmon_kind {
39 	perfmon_cpmu,
40 	perfmon_upmu,
41 	perfmon_kind_max,
42 };
43 
44 #if CONFIG_PERFMON
45 
46 // Allow other performance monitoring SPIs to take ownership of the hardware and
47 // prevent interference.
48 
49 // Returns whether the hardware could be acquired
50 __result_use_check bool perfmon_acquire(enum perfmon_kind kind,
51     const char *name);
52 // Returns whether the hardware is being used.
53 __result_use_check bool perfmon_in_use(enum perfmon_kind kind);
54 // Releases use of the hardware, or panics if it wasn't acquired.
55 void perfmon_release(enum perfmon_kind kind, const char *name);
56 
57 struct perfmon_source {
58 	const char *ps_name;
59 	const perfmon_name_t *ps_register_names;
60 	const perfmon_name_t *ps_attribute_names;
61 	struct perfmon_layout ps_layout;
62 	enum perfmon_kind ps_kind;
63 	bool ps_supported;
64 };
65 
66 extern struct perfmon_source perfmon_sources[perfmon_kind_max];
67 
68 struct perfmon_source *perfmon_source_reserve(enum perfmon_kind kind);
69 
70 void perfmon_source_sample_regs(struct perfmon_source *, uint64_t *, size_t);
71 
72 #define PERFMON_SPEC_MAX_EVENT_COUNT (16)
73 #define PERFMON_SPEC_MAX_ATTR_COUNT (32)
74 
75 typedef struct perfmon_config *perfmon_config_t;
76 
77 perfmon_config_t perfmon_config_create(struct perfmon_source *source);
78 
79 int perfmon_config_add_event(perfmon_config_t,
80     const struct perfmon_event *event);
81 int perfmon_config_set_attr(perfmon_config_t, const struct perfmon_attr *attr);
82 int perfmon_configure(perfmon_config_t);
83 struct perfmon_spec *perfmon_config_specify(perfmon_config_t);
84 void perfmon_config_destroy(perfmon_config_t);
85 
86 #else // CONFIG_PERFMON
87 
88 #define perfmon_acquire(...) true
89 #define perfmon_in_use(...) false
90 #define perfmon_release(...) do { } while (0)
91 
92 #endif // !CONFIG_PERMON
93 
94 #endif // !defined(KERN_PERFMON_H)
95