|
| 1 | +//===--- Tracing.h - Support code for runtime tracing --------------*- C++ -*-// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Support code for tracing events in the Swift runtime |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#ifndef SWIFT_TRACING_H |
| 18 | +#define SWIFT_TRACING_H |
| 19 | + |
| 20 | +#include "llvm/ADT/StringRef.h" |
| 21 | +#include "swift/ABI/Metadata.h" |
| 22 | +#include "swift/Demangling/Demangler.h" |
| 23 | + |
| 24 | +#if SWIFT_STDLIB_TRACING |
| 25 | +#include <os/signpost.h> |
| 26 | + |
| 27 | +#include "swift/Runtime/HeapObject.h" |
| 28 | + |
| 29 | +#define SWIFT_LOG_SECTION_SCAN "section_scan" |
| 30 | + |
| 31 | +namespace swift { |
| 32 | +namespace runtime { |
| 33 | +namespace trace { |
| 34 | + |
| 35 | +extern os_log_t ScanLog; |
| 36 | +extern swift::once_t LogsToken; |
| 37 | + |
| 38 | +void setupLogs(void *unused); |
| 39 | + |
| 40 | +// Every function does ENSURE_LOGS() before making any os_signpost calls, so |
| 41 | +// we can skip availability checking on all the individual calls. |
| 42 | +#pragma clang diagnostic push |
| 43 | +#pragma clang diagnostic ignored "-Wunguarded-availability" |
| 44 | +#pragma clang diagnostic ignored "-Wunguarded-availability-new" |
| 45 | + |
| 46 | +// Check a representative os_signpost function for NULL rather than doing a |
| 47 | +// standard availability check, for better performance if the check doesn't get |
| 48 | +// optimized out. |
| 49 | +#define ENSURE_LOG(log) \ |
| 50 | + do { \ |
| 51 | + if (!SWIFT_RUNTIME_WEAK_CHECK(os_signpost_enabled)) \ |
| 52 | + return {}; \ |
| 53 | + swift::once(LogsToken, setupLogs, nullptr); \ |
| 54 | + } while (0) |
| 55 | + |
| 56 | +/// A struct that captures the state of a scan tracing signpost. When the scan |
| 57 | +/// is complete, call end() with the result of the scan. If the state struct |
| 58 | +/// goes out of scope without calling end(), then it will automatically do the |
| 59 | +/// equivalent of end(nullptr). |
| 60 | +struct ScanTraceState { |
| 61 | + os_signpost_id_t signpostID; |
| 62 | + |
| 63 | + bool ended = false; |
| 64 | + |
| 65 | + template <typename T> |
| 66 | + T *end(T *result) { |
| 67 | + ended = true; |
| 68 | + os_signpost_interval_end(ScanLog, signpostID, SWIFT_LOG_SECTION_SCAN, |
| 69 | + "result=%p", result); |
| 70 | + return result; |
| 71 | + } |
| 72 | + |
| 73 | + ~ScanTraceState() { |
| 74 | + if (!ended) |
| 75 | + end((void *)nullptr); |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +static inline ScanTraceState |
| 80 | +accessible_function_scan_begin(llvm::StringRef name) { |
| 81 | + ENSURE_LOG(ScanLog); |
| 82 | + |
| 83 | + auto id = os_signpost_id_generate(ScanLog); |
| 84 | + os_signpost_interval_begin(ScanLog, id, SWIFT_LOG_SECTION_SCAN, |
| 85 | + "accessible function scan for '%.*s'", |
| 86 | + (int)name.size(), name.data()); |
| 87 | + return {id}; |
| 88 | +} |
| 89 | + |
| 90 | +static inline ScanTraceState metadata_scan_begin(Demangle::NodePointer node) { |
| 91 | + ENSURE_LOG(ScanLog); |
| 92 | + |
| 93 | + auto id = os_signpost_id_generate(ScanLog); |
| 94 | + os_signpost_interval_begin(ScanLog, id, SWIFT_LOG_SECTION_SCAN, |
| 95 | + "metadata scan for %s", |
| 96 | + node ? nodeToString(node).c_str() : "<null>"); |
| 97 | + return {id}; |
| 98 | +} |
| 99 | + |
| 100 | +static inline ScanTraceState |
| 101 | +protocol_conformance_scan_begin(Demangle::NodePointer node) { |
| 102 | + ENSURE_LOG(ScanLog); |
| 103 | + |
| 104 | + auto id = os_signpost_id_generate(ScanLog); |
| 105 | + os_signpost_interval_begin(ScanLog, id, SWIFT_LOG_SECTION_SCAN, |
| 106 | + "protocol conformance scan for %s", |
| 107 | + node ? nodeToString(node).c_str() : "<null>"); |
| 108 | + return {id}; |
| 109 | +} |
| 110 | + |
| 111 | +static inline ScanTraceState |
| 112 | +protocol_conformance_scan_begin(const Metadata *type, |
| 113 | + const ProtocolDescriptor *protocol) { |
| 114 | + ENSURE_LOG(ScanLog); |
| 115 | + |
| 116 | + auto id = os_signpost_id_generate(ScanLog); |
| 117 | + |
| 118 | + // Check for enablement separately to avoid the potentially expensive |
| 119 | + // swift_getTypeName call when tracing is not enabled. |
| 120 | + if (os_signpost_enabled(ScanLog)) { |
| 121 | + auto typeName = swift_getTypeName(type, /*qualified*/ true); |
| 122 | + auto protoName = protocol ? protocol->Name.get() : "<null>"; |
| 123 | + os_signpost_interval_begin(ScanLog, id, SWIFT_LOG_SECTION_SCAN, |
| 124 | + "protocol conformance scan for %.*s(%p): %s(%p)", |
| 125 | + (int)typeName.length, typeName.data, type, |
| 126 | + protoName, protocol); |
| 127 | + } |
| 128 | + return {id}; |
| 129 | +} |
| 130 | + |
| 131 | +static inline ScanTraceState protocol_scan_begin(Demangle::NodePointer node) { |
| 132 | + ENSURE_LOG(ScanLog); |
| 133 | + |
| 134 | + auto id = os_signpost_id_generate(ScanLog); |
| 135 | + os_signpost_interval_begin(ScanLog, id, SWIFT_LOG_SECTION_SCAN, |
| 136 | + "protocol scan for '%s'", |
| 137 | + node ? nodeToString(node).c_str() : "<null>"); |
| 138 | + return {id}; |
| 139 | +} |
| 140 | + |
| 141 | +#pragma clang diagnostic pop |
| 142 | + |
| 143 | +} // namespace trace |
| 144 | +} // namespace runtime |
| 145 | +} // namespace swift |
| 146 | + |
| 147 | +#else |
| 148 | + |
| 149 | +namespace swift { |
| 150 | +namespace runtime { |
| 151 | +namespace trace { |
| 152 | + |
| 153 | +struct ScanTraceState { |
| 154 | + template <typename T> |
| 155 | + T *end(T *result) { |
| 156 | + return result; |
| 157 | + } |
| 158 | +}; |
| 159 | + |
| 160 | +static inline ScanTraceState |
| 161 | +accessible_function_scan_begin(llvm::StringRef name) { |
| 162 | + return {}; |
| 163 | +} |
| 164 | + |
| 165 | +static inline ScanTraceState metadata_scan_begin(Demangle::NodePointer node) { |
| 166 | + return {}; |
| 167 | +} |
| 168 | + |
| 169 | +static inline ScanTraceState |
| 170 | +protocol_conformance_scan_begin(Demangle::NodePointer node) { |
| 171 | + return {}; |
| 172 | +} |
| 173 | + |
| 174 | +static inline ScanTraceState |
| 175 | +protocol_conformance_scan_begin(const Metadata *type, |
| 176 | + const ProtocolDescriptor *protocol) { |
| 177 | + return {}; |
| 178 | +} |
| 179 | + |
| 180 | +static inline ScanTraceState protocol_scan_begin(Demangle::NodePointer node) { |
| 181 | + return {}; |
| 182 | +} |
| 183 | + |
| 184 | +} // namespace trace |
| 185 | +} // namespace runtime |
| 186 | +} // namespace swift |
| 187 | + |
| 188 | +#endif |
| 189 | + |
| 190 | +#endif // SWIFT_TRACING_H |
0 commit comments