Skip to content

Commit bc99b81

Browse files
committed
[Concurrency] Replace SWIFT_TASK_PRINTF_DEBUG with a SWIFT_TASK_DEBUG_LOG macro.
This macro takes the string and parameters directly, and is conditionally defined to either call fprintf or ignore its arguments. This makes the call sites a little more pleasant (no #if scattered about) and ensures every log includes the thread ID and a newline automatically. (cherry picked from commit d5c0469)
1 parent 670944c commit bc99b81

File tree

5 files changed

+54
-88
lines changed

5 files changed

+54
-88
lines changed

stdlib/public/Concurrency/Actor.cpp

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,7 @@ static ExecutorRef swift_task_getCurrentExecutorImpl() {
260260
auto currentTracking = ExecutorTrackingInfo::current();
261261
auto result = (currentTracking ? currentTracking->getActiveExecutor()
262262
: ExecutorRef::generic());
263-
#if SWIFT_TASK_PRINTF_DEBUG
264-
fprintf(stderr, "[%p] getting current executor %p\n", pthread_self(), result.getIdentity());
265-
#endif
263+
SWIFT_TASK_DEBUG_LOG("getting current executor %p", result.getIdentity());
266264
return result;
267265
}
268266

@@ -1158,9 +1156,7 @@ static Job *preprocessQueue(JobRef first,
11581156
}
11591157

11601158
void DefaultActorImpl::giveUpThread(RunningJobInfo runner) {
1161-
#if SWIFT_TASK_PRINTF_DEBUG
1162-
fprintf(stderr, "[%p] giving up thread for actor %p\n", pthread_self(), this);
1163-
#endif
1159+
SWIFT_TASK_DEBUG_LOG("giving up thread for actor %p", this);
11641160
auto oldState = CurrentState.load(std::memory_order_acquire);
11651161
assert(oldState.Flags.isAnyRunningStatus());
11661162

@@ -1220,13 +1216,11 @@ void DefaultActorImpl::giveUpThread(RunningJobInfo runner) {
12201216
// Try again.
12211217
continue;
12221218
}
1223-
1224-
#if SWIFT_TASK_PRINTF_DEBUG
1225-
# define LOG_STATE_TRANSITION fprintf(stderr, "[%p] actor %p transitioned from %zx to %zx (%s)\n", \
1226-
pthread_self(), this, oldState.Flags.getOpaqueValue(), newState.Flags.getOpaqueValue(), __FUNCTION__)
1227-
#else
1228-
# define LOG_STATE_TRANSITION ((void)0)
1229-
#endif
1219+
1220+
#define LOG_STATE_TRANSITION \
1221+
SWIFT_TASK_DEBUG_LOG("actor %p transitioned from %zx to %zx (%s)\n", this, \
1222+
oldState.Flags.getOpaqueValue(), \
1223+
newState.Flags.getOpaqueValue(), __FUNCTION__)
12301224
LOG_STATE_TRANSITION;
12311225

12321226
// The priority of the remaining work.
@@ -1478,9 +1472,7 @@ static void swift_job_runImpl(Job *job, ExecutorRef executor) {
14781472
SWIFT_CC(swiftasync)
14791473
static void processDefaultActor(DefaultActorImpl *currentActor,
14801474
RunningJobInfo runner) {
1481-
#if SWIFT_TASK_PRINTF_DEBUG
1482-
fprintf(stderr, "[%p] processDefaultActor %p\n", pthread_self(), currentActor);
1483-
#endif
1475+
SWIFT_TASK_DEBUG_LOG("processDefaultActor %p", currentActor);
14841476
DefaultActorImpl *actor = currentActor;
14851477

14861478
// If we actually have work to do, we'll need to set up tracking info.
@@ -1504,9 +1496,8 @@ static void processDefaultActor(DefaultActorImpl *currentActor,
15041496
auto job = currentActor->claimNextJobOrGiveUp(threadIsRunningActor,
15051497
runner);
15061498

1507-
#if SWIFT_TASK_PRINTF_DEBUG
1508-
fprintf(stderr, "[%p] processDefaultActor %p claimed job %p\n", pthread_self(), currentActor, job);
1509-
#endif
1499+
SWIFT_TASK_DEBUG_LOG("processDefaultActor %p claimed job %p", currentActor,
1500+
job);
15101501

15111502
// If we failed to claim a job, we have nothing to do.
15121503
if (!job) {
@@ -1526,9 +1517,8 @@ static void processDefaultActor(DefaultActorImpl *currentActor,
15261517
// If it's become nil, or not a default actor, we have nothing to do.
15271518
auto currentExecutor = trackingInfo.getActiveExecutor();
15281519

1529-
#if SWIFT_TASK_PRINTF_DEBUG
1530-
fprintf(stderr, "[%p] processDefaultActor %p current executor now %p\n", pthread_self(), currentActor, currentExecutor.getIdentity());
1531-
#endif
1520+
SWIFT_TASK_DEBUG_LOG("processDefaultActor %p current executor now %p",
1521+
currentActor, currentExecutor.getIdentity());
15321522

15331523
if (!currentExecutor.isDefaultActor()) {
15341524
// The job already gave up the thread for us.
@@ -1827,9 +1817,8 @@ static void runOnAssumedThread(AsyncTask *task, ExecutorRef executor,
18271817
executor = trackingInfo.getActiveExecutor();
18281818
trackingInfo.leave();
18291819

1830-
#if SWIFT_TASK_PRINTF_DEBUG
1831-
fprintf(stderr, "[%p] leaving assumed thread, current executor is %p\n", pthread_self(), executor.getIdentity());
1832-
#endif
1820+
SWIFT_TASK_DEBUG_LOG("leaving assumed thread, current executor is %p",
1821+
executor.getIdentity());
18331822

18341823
if (executor.isDefaultActor())
18351824
asImpl(executor.getDefaultActor())->giveUpThread(runner);
@@ -1843,9 +1832,9 @@ static void swift_task_switchImpl(SWIFT_ASYNC_CONTEXT AsyncContext *resumeContex
18431832
auto currentExecutor =
18441833
(trackingInfo ? trackingInfo->getActiveExecutor()
18451834
: ExecutorRef::generic());
1846-
#if SWIFT_TASK_PRINTF_DEBUG
1847-
fprintf(stderr, "[%p] trying to switch from executor %p to %p\n", pthread_self(), currentExecutor.getIdentity(), newExecutor.getIdentity());
1848-
#endif
1835+
SWIFT_TASK_DEBUG_LOG("trying to switch from executor %p to %p",
1836+
currentExecutor.getIdentity(),
1837+
newExecutor.getIdentity());
18491838

18501839
// If the current executor is compatible with running the new executor,
18511840
// we can just immediately continue running with the resume function
@@ -1871,19 +1860,18 @@ static void swift_task_switchImpl(SWIFT_ASYNC_CONTEXT AsyncContext *resumeContex
18711860
if (canGiveUpThreadForSwitch(trackingInfo, currentExecutor) &&
18721861
!shouldYieldThread() &&
18731862
tryAssumeThreadForSwitch(newExecutor, runner)) {
1874-
#if SWIFT_TASK_PRINTF_DEBUG
1875-
fprintf(stderr, "[%p] switch succeeded, task %p assumed thread for executor %p\n", pthread_self(), task, newExecutor.getIdentity());
1876-
#endif
1863+
SWIFT_TASK_DEBUG_LOG(
1864+
"switch succeeded, task %p assumed thread for executor %p", task,
1865+
newExecutor.getIdentity());
18771866
giveUpThreadForSwitch(currentExecutor, runner);
18781867
// 'return' forces tail call
18791868
return runOnAssumedThread(task, newExecutor, trackingInfo, runner);
18801869
}
18811870

18821871
// Otherwise, just asynchronously enqueue the task on the given
18831872
// executor.
1884-
#if SWIFT_TASK_PRINTF_DEBUG
1885-
fprintf(stderr, "[%p] switch failed, task %p enqueued on executor %p\n", pthread_self(), task, newExecutor.getIdentity());
1886-
#endif
1873+
SWIFT_TASK_DEBUG_LOG("switch failed, task %p enqueued on executor %p", task,
1874+
newExecutor.getIdentity());
18871875
task->flagAsSuspended();
18881876
swift_task_enqueue(task, newExecutor);
18891877
}
@@ -1902,9 +1890,8 @@ void _swift_task_enqueueOnExecutor(Job *job, HeapObject *executor,
19021890

19031891
SWIFT_CC(swift)
19041892
static void swift_task_enqueueImpl(Job *job, ExecutorRef executor) {
1905-
#if SWIFT_TASK_PRINTF_DEBUG
1906-
fprintf(stderr, "[%p] enqueue job %p on executor %p\n", pthread_self(), job, executor.getIdentity());
1907-
#endif
1893+
SWIFT_TASK_DEBUG_LOG("enqueue job %p on executor %p", job,
1894+
executor.getIdentity());
19081895

19091896
assert(job && "no job provided");
19101897

stdlib/public/Concurrency/AsyncLet.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,10 @@ void swift::swift_asyncLet_begin(AsyncLet *alet,
178178
void *closureEntryPoint,
179179
HeapObject *closureContext,
180180
void *resultBuffer) {
181-
#if SWIFT_TASK_PRINTF_DEBUG
182-
fprintf(stderr, "[%lu] creating async let buffer of type %s at %p\n",
183-
_swift_get_thread_id(),
184-
swift_getTypeName(futureResultType, true).data,
185-
resultBuffer);
186-
#endif
187-
181+
SWIFT_TASK_DEBUG_LOG("creating async let buffer of type %s at %p",
182+
swift_getTypeName(futureResultType, true).data,
183+
resultBuffer);
184+
188185
auto flags = TaskCreateFlags();
189186
flags.setEnqueueJob(true);
190187

@@ -323,9 +320,7 @@ static void swift_asyncLet_endImpl(AsyncLet *alet) {
323320
AsyncTask *parent = swift_task_getCurrent();
324321
assert(parent && "async-let must have a parent task");
325322

326-
#if SWIFT_TASK_PRINTF_DEBUG
327-
fprintf(stderr, "[%p] async let end of task %p, parent: %p\n", pthread_self(), task, parent);
328-
#endif
323+
SWIFT_TASK_DEBUG_LOG("async let end of task %p, parent: %p", task, parent);
329324
_swift_task_dealloc_specific(parent, task);
330325
}
331326

@@ -350,10 +345,8 @@ static void asyncLet_finish_after_task_completion(SWIFT_ASYNC_CONTEXT AsyncConte
350345
// and finally, release the task and destroy the async-let
351346
assert(swift_task_getCurrent() && "async-let must have a parent task");
352347

353-
#if SWIFT_TASK_PRINTF_DEBUG
354-
fprintf(stderr, "[%lu] async let end of task %p, parent: %p\n",
355-
_swift_get_thread_id(), task, swift_task_getCurrent());
356-
#endif
348+
SWIFT_TASK_DEBUG_LOG("async let end of task %p, parent: %p", task,
349+
swift_task_getCurrent());
357350
// Destruct the task.
358351
task->~AsyncTask();
359352
// Deallocate it out of the parent, if it was allocated there.

stdlib/public/Concurrency/Task.cpp

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,16 @@ FutureFragment::Status AsyncTask::waitFuture(AsyncTask *waitingTask,
8383
switch (queueHead.getStatus()) {
8484
case Status::Error:
8585
case Status::Success:
86-
#if SWIFT_TASK_PRINTF_DEBUG
87-
fprintf(stderr, "[%p] task %p waiting on task %p, completed immediately\n", pthread_self(), waitingTask, this);
88-
#endif
86+
SWIFT_TASK_DEBUG_LOG("task %p waiting on task %p, completed immediately",
87+
waitingTask, this);
8988
_swift_tsan_acquire(static_cast<Job *>(this));
9089
if (contextIntialized) waitingTask->flagAsRunning();
9190
// The task is done; we don't need to wait.
9291
return queueHead.getStatus();
9392

9493
case Status::Executing:
95-
#if SWIFT_TASK_PRINTF_DEBUG
96-
fprintf(stderr, "[%p] task %p waiting on task %p, going to sleep\n", pthread_self(), waitingTask, this);
97-
#endif
94+
SWIFT_TASK_DEBUG_LOG("task %p waiting on task %p, going to sleep",
95+
waitingTask, this);
9896
_swift_tsan_release(static_cast<Job *>(waitingTask));
9997
// Task is not complete. We'll need to add ourselves to the queue.
10098
break;
@@ -179,19 +177,16 @@ void AsyncTask::completeFuture(AsyncContext *context) {
179177
// Schedule every waiting task on the executor.
180178
auto waitingTask = queueHead.getTask();
181179

182-
#if SWIFT_TASK_PRINTF_DEBUG
183180
if (!waitingTask)
184-
fprintf(stderr, "[%p] task %p had no waiting tasks\n", pthread_self(), this);
185-
#endif
181+
SWIFT_TASK_DEBUG_LOG("task %p had no waiting tasks", this);
186182

187183
while (waitingTask) {
188184
// Find the next waiting task before we invalidate it by resuming
189185
// the task.
190186
auto nextWaitingTask = waitingTask->getNextWaitingTask();
191187

192-
#if SWIFT_TASK_PRINTF_DEBUG
193-
fprintf(stderr, "[%p] waking task %p from future of task %p\n", pthread_self(), waitingTask, this);
194-
#endif
188+
SWIFT_TASK_DEBUG_LOG("waking task %p from future of task %p", waitingTask,
189+
this);
195190

196191
// Fill in the return context.
197192
auto waitingContext =
@@ -241,9 +236,7 @@ static void destroyTask(SWIFT_CONTEXT HeapObject *obj) {
241236
// the task-local allocator. There's actually nothing else to clean up
242237
// here.
243238

244-
#if SWIFT_TASK_PRINTF_DEBUG
245-
fprintf(stderr, "[%p] destroy task %p\n", pthread_self(), task);
246-
#endif
239+
SWIFT_TASK_DEBUG_LOG("destroy task %p", task);
247240
free(task);
248241
}
249242

@@ -316,9 +309,7 @@ static void completeTaskImpl(AsyncTask *task,
316309

317310
task->Private.complete(task);
318311

319-
#if SWIFT_TASK_PRINTF_DEBUG
320-
fprintf(stderr, "[%p] task %p completed\n", pthread_self(), task);
321-
#endif
312+
SWIFT_TASK_DEBUG_LOG("task %p completed", task);
322313

323314
// Complete the future.
324315
// Warning: This deallocates the task in case it's an async let task.
@@ -556,10 +547,8 @@ static AsyncTaskAndContext swift_task_create_commonImpl(
556547
} else {
557548
allocation = malloc(amountToAllocate);
558549
}
559-
#if SWIFT_TASK_PRINTF_DEBUG
560-
fprintf(stderr, "[%lu] allocate task %p, parent = %p, slab %u\n",
561-
pthread_self(), allocation, parent, initialSlabSize);
562-
#endif
550+
SWIFT_TASK_DEBUG_LOG("allocate task %p, parent = %p, slab %u", allocation,
551+
parent, initialSlabSize);
563552

564553
AsyncContext *initialContext =
565554
reinterpret_cast<AsyncContext*>(
@@ -638,9 +627,7 @@ static AsyncTaskAndContext swift_task_create_commonImpl(
638627
futureAsyncContextPrefix->indirectResult = futureFragment->getStoragePtr();
639628
}
640629

641-
#if SWIFT_TASK_PRINTF_DEBUG
642-
fprintf(stderr, "[%p] creating task %p with parent %p\n", pthread_self(), task, parent);
643-
#endif
630+
SWIFT_TASK_DEBUG_LOG("creating task %p with parent %p", task, parent);
644631

645632
// Initialize the task-local allocator.
646633
initialContext->ResumeParent = reinterpret_cast<TaskContinuationFunction *>(

stdlib/public/Concurrency/TaskPrivate.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@
2929

3030
namespace swift {
3131

32-
// Uncomment to enable helpful debug spew to stderr
33-
//#define SWIFT_TASK_PRINTF_DEBUG 1
32+
// Set to 1 to enable helpful debug spew to stderr
33+
#if 0
34+
#define SWIFT_TASK_DEBUG_LOG(fmt, ...) \
35+
fprintf(stderr, "[%lu] " fmt "\n", (unsigned long)_swift_get_thread_id(), \
36+
__VA_ARGS__)
37+
#else
38+
#define SWIFT_TASK_DEBUG_LOG(fmt, ...) (void)0
39+
#endif
3440

3541
class AsyncTask;
3642
class TaskGroup;
@@ -393,10 +399,7 @@ inline void AsyncTask::flagAsSuspended() {
393399
// that can be used when debugging locally to instrument when a task actually is
394400
// dealloced.
395401
inline void AsyncTask::flagAsCompleted() {
396-
#if SWIFT_TASK_PRINTF_DEBUG
397-
fprintf(stderr, "[%lu] task completed %p\n",
398-
_swift_get_thread_id(), this);
399-
#endif
402+
SWIFT_TASK_DEBUG_LOG("task completed %p", this);
400403
}
401404

402405
inline void AsyncTask::localValuePush(const HeapObject *key,

stdlib/public/Concurrency/ThreadSanitizer.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,14 @@ TSanFunc *tsan_acquire, *tsan_release;
3131
void swift::_swift_tsan_acquire(void *addr) {
3232
if (tsan_acquire) {
3333
tsan_acquire(addr);
34-
#if SWIFT_TASK_PRINTF_DEBUG
35-
fprintf(stderr, "[%lu] tsan_acquire on %p\n", _swift_get_thread_id(), addr);
36-
#endif
34+
SWIFT_TASK_DEBUG_LOG("tsan_acquire on %p", addr);
3735
}
3836
}
3937

4038
void swift::_swift_tsan_release(void *addr) {
4139
if (tsan_release) {
4240
tsan_release(addr);
43-
#if SWIFT_TASK_PRINTF_DEBUG
44-
fprintf(stderr, "[%lu] tsan_release on %p\n", _swift_get_thread_id(), addr);
45-
#endif
41+
SWIFT_TASK_DEBUG_LOG("tsan_release on %p", addr);
4642
}
4743
}
4844

0 commit comments

Comments
 (0)