Skip to content

Commit 3100be1

Browse files
committed
[Runtime] Don't enable tracing in logd, diagnosticd, notifyd.
We can't use os_log functionality in logd, diagnosticd, or notifyd. Check for them and disable tracing in those processes. Add a new TracingCommon.h for common code shared between swiftCore and swift_Concurrency tracing. Add a single function that checks if tracing should be enabled, which now checks if os_signpost_enabled is available, and if the process is one of these. Modify the tracing code to check this before creating os_log objects. rdar://124226334
1 parent 761812a commit 3100be1

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

include/swift/Runtime/TracingCommon.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===--- TracingCommon.h - Common code for runtime/Concurrency -----*- 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 shared between swiftCore and swift_Concurrency.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_TRACING_COMMON_H
18+
#define SWIFT_TRACING_COMMON_H
19+
20+
#if SWIFT_STDLIB_TRACING
21+
22+
#include "swift/Runtime/Config.h"
23+
#include <os/signpost.h>
24+
25+
extern const char *__progname;
26+
27+
namespace swift {
28+
namespace runtime {
29+
namespace trace {
30+
31+
static inline bool shouldEnableTracing() {
32+
if (!SWIFT_RUNTIME_WEAK_CHECK(os_signpost_enabled))
33+
return false;
34+
if (__progname && (strcmp(__progname, "logd") == 0 ||
35+
strcmp(__progname, "diagnosticd") == 0 ||
36+
strcmp(__progname, "notifyd") == 0))
37+
return false;
38+
return true;
39+
}
40+
41+
} // namespace trace
42+
} // namespace runtime
43+
} // namespace swift
44+
45+
#endif
46+
47+
#endif // SWIFT_TRACING_H

stdlib/public/Concurrency/TracingSignpost.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#if SWIFT_STDLIB_CONCURRENCY_TRACING
1818

1919
#include "TracingSignpost.h"
20+
#include "swift/Runtime/TracingCommon.h"
2021
#include <stdio.h>
2122

2223
#define SWIFT_LOG_CONCURRENCY_SUBSYSTEM "com.apple.swift.concurrency"
@@ -30,8 +31,15 @@ namespace trace {
3031
os_log_t ActorLog;
3132
os_log_t TaskLog;
3233
swift::once_t LogsToken;
34+
bool TracingEnabled;
3335

3436
void setupLogs(void *unused) {
37+
if (!swift::runtime::trace::shouldEnableTracing()) {
38+
TracingEnabled = false;
39+
return;
40+
}
41+
42+
TracingEnabled = true;
3543
ActorLog = os_log_create(SWIFT_LOG_CONCURRENCY_SUBSYSTEM,
3644
SWIFT_LOG_ACTOR_CATEGORY);
3745
TaskLog = os_log_create(SWIFT_LOG_CONCURRENCY_SUBSYSTEM,

stdlib/public/Concurrency/TracingSignpost.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ namespace trace {
7070
extern os_log_t ActorLog;
7171
extern os_log_t TaskLog;
7272
extern swift::once_t LogsToken;
73+
extern bool TracingEnabled;
7374

7475
void setupLogs(void *unused);
7576

@@ -78,9 +79,9 @@ void setupLogs(void *unused);
7879
// optimized out.
7980
#define ENSURE_LOGS(...) \
8081
do { \
81-
if (!SWIFT_RUNTIME_WEAK_CHECK(os_signpost_enabled)) \
82-
return __VA_ARGS__; \
8382
swift::once(LogsToken, setupLogs, nullptr); \
83+
if (!TracingEnabled) \
84+
return __VA_ARGS__; \
8485
} while (0)
8586

8687
// Every function does ENSURE_LOGS() before making any os_signpost calls, so

stdlib/public/runtime/Tracing.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "Tracing.h"
18+
#include "swift/Runtime/TracingCommon.h"
1819

1920
#if SWIFT_STDLIB_TRACING
2021

@@ -27,8 +28,15 @@ namespace trace {
2728

2829
os_log_t ScanLog;
2930
swift::once_t LogsToken;
31+
bool TracingEnabled;
3032

3133
void setupLogs(void *unused) {
34+
if (!shouldEnableTracing()) {
35+
TracingEnabled = false;
36+
return;
37+
}
38+
39+
TracingEnabled = true;
3240
ScanLog = os_log_create(SWIFT_LOG_SUBSYSTEM, SWIFT_LOG_SECTION_SCAN_CATEGORY);
3341
}
3442

stdlib/public/runtime/Tracing.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace trace {
3434

3535
extern os_log_t ScanLog;
3636
extern swift::once_t LogsToken;
37+
extern bool TracingEnabled;
3738

3839
void setupLogs(void *unused);
3940

@@ -48,9 +49,9 @@ void setupLogs(void *unused);
4849
// optimized out.
4950
#define ENSURE_LOG(log) \
5051
do { \
51-
if (!SWIFT_RUNTIME_WEAK_CHECK(os_signpost_enabled)) \
52-
return {}; \
5352
swift::once(LogsToken, setupLogs, nullptr); \
53+
if (!TracingEnabled) \
54+
return {}; \
5455
} while (0)
5556

5657
/// A struct that captures the state of a scan tracing signpost. When the scan

0 commit comments

Comments
 (0)