]>
Commit | Line | Data |
---|---|---|
813fb2f6 A |
1 | /* |
2 | * Copyright (c) 2016 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_APACHE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
7 | * you may not use this file except in compliance with the License. | |
8 | * You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, software | |
13 | * distributed under the License is distributed on an "AS IS" BASIS, | |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 | * See the License for the specific language governing permissions and | |
16 | * limitations under the License. | |
17 | * | |
18 | * @APPLE_APACHE_LICENSE_HEADER_END@ | |
19 | */ | |
20 | ||
21 | #ifndef __FIREHOSE_CHUNK_PRIVATE__ | |
22 | #define __FIREHOSE_CHUNK_PRIVATE__ | |
23 | ||
24 | #if KERNEL | |
25 | #include <machine/atomic.h> | |
26 | #endif | |
27 | #include <stdatomic.h> | |
28 | #include <sys/param.h> | |
29 | #include <os/base.h> | |
30 | #include "firehose_types_private.h" | |
31 | #include "tracepoint_private.h" | |
32 | ||
33 | __BEGIN_DECLS | |
34 | ||
35 | #define FIREHOSE_CHUNK_SIZE 4096ul | |
36 | ||
37 | #define FIREHOSE_CHUNK_POS_ENTRY_OFFS_INC (1ULL << 0) | |
38 | #define FIREHOSE_CHUNK_POS_PRIVATE_OFFS_INC (1ULL << 16) | |
39 | #define FIREHOSE_CHUNK_POS_REFCNT_INC (1ULL << 32) | |
40 | #define FIREHOSE_CHUNK_POS_FULL_BIT (1ULL << 56) | |
41 | #define FIREHOSE_CHUNK_POS_USABLE_FOR_STREAM(pos, stream) \ | |
42 | ((((pos).fcp_pos >> 48) & 0x1ff) == (uint16_t)stream) | |
43 | ||
44 | typedef union { | |
45 | _Atomic(uint64_t) fcp_atomic_pos; | |
46 | uint64_t fcp_pos; | |
47 | struct { | |
48 | uint16_t fcp_next_entry_offs; | |
49 | uint16_t fcp_private_offs; | |
50 | uint8_t fcp_refcnt; | |
51 | uint8_t fcp_qos; | |
52 | uint8_t fcp_stream; | |
53 | uint8_t fcp_flag_full : 1; | |
54 | uint8_t fcp_flag_io : 1; | |
55 | uint8_t _fcp_flag_unused : 6; | |
56 | }; | |
57 | } firehose_chunk_pos_u; | |
58 | ||
59 | typedef struct firehose_chunk_s { | |
60 | uint8_t fc_start[0]; | |
61 | firehose_chunk_pos_u fc_pos; | |
62 | uint64_t fc_timestamp; | |
63 | uint8_t fc_data[FIREHOSE_CHUNK_SIZE - 8 - 8]; | |
64 | } *firehose_chunk_t; | |
65 | ||
66 | typedef struct firehose_chunk_range_s { | |
67 | uint16_t fcr_offset; // offset from the start of the chunk | |
68 | uint16_t fcr_length; | |
69 | } *firehose_chunk_range_t; | |
70 | ||
71 | #if defined(KERNEL) || defined(OS_FIREHOSE_SPI) | |
72 | ||
73 | OS_ALWAYS_INLINE | |
74 | static inline bool | |
75 | firehose_chunk_pos_fits(firehose_chunk_pos_u pos, uint16_t size) | |
76 | { | |
77 | return pos.fcp_next_entry_offs + size <= pos.fcp_private_offs; | |
78 | } | |
79 | ||
80 | #define FIREHOSE_CHUNK_TRY_RESERVE_FAIL_ENQUEUE (-1) | |
81 | #define FIREHOSE_CHUNK_TRY_RESERVE_FAIL ( 0) | |
82 | ||
83 | OS_ALWAYS_INLINE | |
84 | static inline long | |
85 | firehose_chunk_tracepoint_try_reserve(firehose_chunk_t fc, uint64_t stamp, | |
86 | firehose_stream_t stream, uint8_t qos, uint16_t pubsize, | |
87 | uint16_t privsize, uint8_t **privptr) | |
88 | { | |
89 | const uint16_t ft_size = offsetof(struct firehose_tracepoint_s, ft_data); | |
90 | firehose_chunk_pos_u orig, pos; | |
91 | bool reservation_failed, stamp_delta_fits; | |
92 | ||
93 | stamp_delta_fits = ((stamp - fc->fc_timestamp) >> 48) == 0; | |
94 | ||
95 | // no acquire barrier because the returned space is written to only | |
96 | os_atomic_rmw_loop(&fc->fc_pos.fcp_atomic_pos, | |
97 | orig.fcp_pos, pos.fcp_pos, relaxed, { | |
98 | if (orig.fcp_pos == 0) { | |
99 | // we acquired a really really old reference, and we probably | |
100 | // just faulted in a new page | |
101 | os_atomic_rmw_loop_give_up(return FIREHOSE_CHUNK_TRY_RESERVE_FAIL); | |
102 | } | |
103 | if (!FIREHOSE_CHUNK_POS_USABLE_FOR_STREAM(orig, stream)) { | |
104 | // nothing to do if the chunk is full, or the stream doesn't match, | |
105 | // in which case the thread probably: | |
106 | // - loaded the chunk ref | |
107 | // - been suspended a long while | |
108 | // - read the chunk to find a very old thing | |
109 | os_atomic_rmw_loop_give_up(return FIREHOSE_CHUNK_TRY_RESERVE_FAIL); | |
110 | } | |
111 | pos = orig; | |
112 | if (!firehose_chunk_pos_fits(orig, | |
113 | ft_size + pubsize + privsize) || !stamp_delta_fits) { | |
114 | pos.fcp_flag_full = true; | |
115 | reservation_failed = true; | |
116 | } else { | |
117 | if (qos > pos.fcp_qos) pos.fcp_qos = qos; | |
118 | // using these *_INC macros is so that the compiler generates better | |
119 | // assembly: using the struct individual fields forces the compiler | |
120 | // to handle carry propagations, and we know it won't happen | |
121 | pos.fcp_pos += roundup(ft_size + pubsize, 8) * | |
122 | FIREHOSE_CHUNK_POS_ENTRY_OFFS_INC; | |
123 | pos.fcp_pos -= privsize * FIREHOSE_CHUNK_POS_PRIVATE_OFFS_INC; | |
124 | pos.fcp_pos += FIREHOSE_CHUNK_POS_REFCNT_INC; | |
125 | const uint16_t minimum_payload_size = 16; | |
126 | if (!firehose_chunk_pos_fits(pos, | |
127 | roundup(ft_size + minimum_payload_size , 8))) { | |
128 | // if we can't even have minimum_payload_size bytes of payload | |
129 | // for the next tracepoint, just flush right away | |
130 | pos.fcp_flag_full = true; | |
131 | } | |
132 | reservation_failed = false; | |
133 | } | |
134 | }); | |
135 | ||
136 | if (reservation_failed) { | |
137 | if (pos.fcp_refcnt) { | |
138 | // nothing to do, there is a thread writing that will pick up | |
139 | // the "FULL" flag on flush and push as a consequence | |
140 | return FIREHOSE_CHUNK_TRY_RESERVE_FAIL; | |
141 | } | |
142 | // caller must enqueue chunk | |
143 | return FIREHOSE_CHUNK_TRY_RESERVE_FAIL_ENQUEUE; | |
144 | } | |
145 | if (privptr) { | |
146 | *privptr = fc->fc_start + pos.fcp_private_offs; | |
147 | } | |
148 | return orig.fcp_next_entry_offs; | |
149 | } | |
150 | ||
151 | OS_ALWAYS_INLINE | |
152 | static inline firehose_tracepoint_t | |
153 | firehose_chunk_tracepoint_begin(firehose_chunk_t fc, uint64_t stamp, | |
154 | uint16_t pubsize, uint64_t thread_id, long offset) | |
155 | { | |
156 | firehose_tracepoint_t ft = (firehose_tracepoint_t) | |
157 | __builtin_assume_aligned(fc->fc_start + offset, 8); | |
158 | stamp -= fc->fc_timestamp; | |
159 | stamp |= (uint64_t)pubsize << 48; | |
160 | // The compiler barrier is needed for userland process death handling, see | |
161 | // (tracepoint-begin) in libdispatch's firehose_buffer_stream_chunk_install. | |
162 | atomic_store_explicit(&ft->ft_atomic_stamp_and_length, stamp, | |
163 | memory_order_relaxed); | |
164 | __asm__ __volatile__("" ::: "memory"); | |
165 | ft->ft_thread = thread_id; | |
166 | return ft; | |
167 | } | |
168 | ||
169 | OS_ALWAYS_INLINE | |
170 | static inline bool | |
171 | firehose_chunk_tracepoint_end(firehose_chunk_t fc, | |
172 | firehose_tracepoint_t ft, firehose_tracepoint_id_u ftid) | |
173 | { | |
174 | firehose_chunk_pos_u pos; | |
175 | ||
176 | atomic_store_explicit(&ft->ft_id.ftid_atomic_value, | |
177 | ftid.ftid_value, memory_order_release); | |
178 | pos.fcp_pos = atomic_fetch_sub_explicit(&fc->fc_pos.fcp_atomic_pos, | |
179 | FIREHOSE_CHUNK_POS_REFCNT_INC, memory_order_relaxed); | |
180 | return pos.fcp_refcnt == 1 && pos.fcp_flag_full; | |
181 | } | |
182 | ||
183 | #endif // defined(KERNEL) || defined(OS_FIREHOSE_SPI) | |
184 | ||
185 | __END_DECLS | |
186 | ||
187 | #endif // __FIREHOSE_CHUNK_PRIVATE__ |