Skip to content

Commit d5c0469

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.
1 parent 0045fe3 commit d5c0469

File tree

5 files changed

+53
-106
lines changed

5 files changed

+53
-106
lines changed

stdlib/public/Concurrency/Actor.cpp

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,7 @@ static ExecutorRef swift_task_getCurrentExecutorImpl() {
269269
auto currentTracking = ExecutorTrackingInfo::current();
270270
auto result = (currentTracking ? currentTracking->getActiveExecutor()
271271
: ExecutorRef::generic());
272-
#if SWIFT_TASK_PRINTF_DEBUG
273-
fprintf(stderr, "[%lu] getting current executor %p\n",
274-
_swift_get_thread_id(), result.getIdentity());
275-
#endif
272+
SWIFT_TASK_DEBUG_LOG("getting current executor %p", result.getIdentity());
276273
return result;
277274
}
278275

@@ -1172,10 +1169,7 @@ static Job *preprocessQueue(JobRef first,
11721169
}
11731170

11741171
void DefaultActorImpl::giveUpThread(RunningJobInfo runner) {
1175-
#if SWIFT_TASK_PRINTF_DEBUG
1176-
fprintf(stderr, "[%lu] giving up thread for actor %p\n",
1177-
_swift_get_thread_id(), this);
1178-
#endif
1172+
SWIFT_TASK_DEBUG_LOG("giving up thread for actor %p", this);
11791173
auto oldState = CurrentState.load(std::memory_order_acquire);
11801174
assert(oldState.Flags.isAnyRunningStatus());
11811175

@@ -1236,14 +1230,10 @@ void DefaultActorImpl::giveUpThread(RunningJobInfo runner) {
12361230
continue;
12371231
}
12381232

1239-
#if SWIFT_TASK_PRINTF_DEBUG
1240-
# define LOG_STATE_TRANSITION \
1241-
fprintf(stderr, "[%lu] actor %p transitioned from %zx to %zx (%s)\n", \
1242-
_swift_get_thread_id(), this, oldState.Flags.getOpaqueValue(), \
1243-
newState.Flags.getOpaqueValue(), __FUNCTION__)
1244-
#else
1245-
# define LOG_STATE_TRANSITION ((void)0)
1246-
#endif
1233+
#define LOG_STATE_TRANSITION \
1234+
SWIFT_TASK_DEBUG_LOG("actor %p transitioned from %zx to %zx (%s)\n", this, \
1235+
oldState.Flags.getOpaqueValue(), \
1236+
newState.Flags.getOpaqueValue(), __FUNCTION__)
12471237
LOG_STATE_TRANSITION;
12481238

12491239
// The priority of the remaining work.
@@ -1494,10 +1484,7 @@ static void swift_job_runImpl(Job *job, ExecutorRef executor) {
14941484
SWIFT_CC(swiftasync)
14951485
static void processDefaultActor(DefaultActorImpl *currentActor,
14961486
RunningJobInfo runner) {
1497-
#if SWIFT_TASK_PRINTF_DEBUG
1498-
fprintf(stderr, "[%lu] processDefaultActor %p\n",
1499-
_swift_get_thread_id(), currentActor);
1500-
#endif
1487+
SWIFT_TASK_DEBUG_LOG("processDefaultActor %p", currentActor);
15011488
DefaultActorImpl *actor = currentActor;
15021489

15031490
// If we actually have work to do, we'll need to set up tracking info.
@@ -1521,10 +1508,8 @@ static void processDefaultActor(DefaultActorImpl *currentActor,
15211508
auto job = currentActor->claimNextJobOrGiveUp(threadIsRunningActor,
15221509
runner);
15231510

1524-
#if SWIFT_TASK_PRINTF_DEBUG
1525-
fprintf(stderr, "[%lu] processDefaultActor %p claimed job %p\n",
1526-
_swift_get_thread_id(), currentActor, job);
1527-
#endif
1511+
SWIFT_TASK_DEBUG_LOG("processDefaultActor %p claimed job %p", currentActor,
1512+
job);
15281513

15291514
// If we failed to claim a job, we have nothing to do.
15301515
if (!job) {
@@ -1544,10 +1529,8 @@ static void processDefaultActor(DefaultActorImpl *currentActor,
15441529
// If it's become nil, or not a default actor, we have nothing to do.
15451530
auto currentExecutor = trackingInfo.getActiveExecutor();
15461531

1547-
#if SWIFT_TASK_PRINTF_DEBUG
1548-
fprintf(stderr, "[%lu] processDefaultActor %p current executor now %p\n",
1549-
_swift_get_thread_id(), currentActor, currentExecutor.getIdentity());
1550-
#endif
1532+
SWIFT_TASK_DEBUG_LOG("processDefaultActor %p current executor now %p",
1533+
currentActor, currentExecutor.getIdentity());
15511534

15521535
if (!currentExecutor.isDefaultActor()) {
15531536
// The job already gave up the thread for us.
@@ -1851,10 +1834,8 @@ static void runOnAssumedThread(AsyncTask *task, ExecutorRef executor,
18511834
executor = trackingInfo.getActiveExecutor();
18521835
trackingInfo.leave();
18531836

1854-
#if SWIFT_TASK_PRINTF_DEBUG
1855-
fprintf(stderr, "[%lu] leaving assumed thread, current executor is %p\n",
1856-
_swift_get_thread_id(), executor.getIdentity());
1857-
#endif
1837+
SWIFT_TASK_DEBUG_LOG("leaving assumed thread, current executor is %p",
1838+
executor.getIdentity());
18581839

18591840
if (executor.isDefaultActor())
18601841
asImpl(executor.getDefaultActor())->giveUpThread(runner);
@@ -1868,11 +1849,9 @@ static void swift_task_switchImpl(SWIFT_ASYNC_CONTEXT AsyncContext *resumeContex
18681849
auto currentExecutor =
18691850
(trackingInfo ? trackingInfo->getActiveExecutor()
18701851
: ExecutorRef::generic());
1871-
#if SWIFT_TASK_PRINTF_DEBUG
1872-
fprintf(stderr, "[%lu] trying to switch from executor %p to %p\n",
1873-
_swift_get_thread_id(), currentExecutor.getIdentity(),
1874-
newExecutor.getIdentity());
1875-
#endif
1852+
SWIFT_TASK_DEBUG_LOG("trying to switch from executor %p to %p",
1853+
currentExecutor.getIdentity(),
1854+
newExecutor.getIdentity());
18761855

18771856
// If the current executor is compatible with running the new executor,
18781857
// we can just immediately continue running with the resume function
@@ -1898,21 +1877,18 @@ static void swift_task_switchImpl(SWIFT_ASYNC_CONTEXT AsyncContext *resumeContex
18981877
if (canGiveUpThreadForSwitch(trackingInfo, currentExecutor) &&
18991878
!shouldYieldThread() &&
19001879
tryAssumeThreadForSwitch(newExecutor, runner)) {
1901-
#if SWIFT_TASK_PRINTF_DEBUG
1902-
fprintf(stderr, "[%lu] switch succeeded, task %p assumed thread for executor %p\n",
1903-
_swift_get_thread_id(), task, newExecutor.getIdentity());
1904-
#endif
1880+
SWIFT_TASK_DEBUG_LOG(
1881+
"switch succeeded, task %p assumed thread for executor %p", task,
1882+
newExecutor.getIdentity());
19051883
giveUpThreadForSwitch(currentExecutor, runner);
19061884
// 'return' forces tail call
19071885
return runOnAssumedThread(task, newExecutor, trackingInfo, runner);
19081886
}
19091887

19101888
// Otherwise, just asynchronously enqueue the task on the given
19111889
// executor.
1912-
#if SWIFT_TASK_PRINTF_DEBUG
1913-
fprintf(stderr, "[%lu] switch failed, task %p enqueued on executor %p\n",
1914-
_swift_get_thread_id(), task, newExecutor.getIdentity());
1915-
#endif
1890+
SWIFT_TASK_DEBUG_LOG("switch failed, task %p enqueued on executor %p", task,
1891+
newExecutor.getIdentity());
19161892
task->flagAsSuspended();
19171893
_swift_task_clearCurrent();
19181894
swift_task_enqueue(task, newExecutor);
@@ -1932,10 +1908,8 @@ void _swift_task_enqueueOnExecutor(Job *job, HeapObject *executor,
19321908

19331909
SWIFT_CC(swift)
19341910
static void swift_task_enqueueImpl(Job *job, ExecutorRef executor) {
1935-
#if SWIFT_TASK_PRINTF_DEBUG
1936-
fprintf(stderr, "[%lu] enqueue job %p on executor %p\n",
1937-
_swift_get_thread_id(), job, executor.getIdentity());
1938-
#endif
1911+
SWIFT_TASK_DEBUG_LOG("enqueue job %p on executor %p", job,
1912+
executor.getIdentity());
19391913

19401914
assert(job && "no job provided");
19411915

stdlib/public/Concurrency/AsyncLet.cpp

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

@@ -321,10 +318,7 @@ static void swift_asyncLet_endImpl(AsyncLet *alet) {
321318
AsyncTask *parent = swift_task_getCurrent();
322319
assert(parent && "async-let must have a parent task");
323320

324-
#if SWIFT_TASK_PRINTF_DEBUG
325-
fprintf(stderr, "[%lu] async let end of task %p, parent: %p\n",
326-
_swift_get_thread_id(), task, parent);
327-
#endif
321+
SWIFT_TASK_DEBUG_LOG("async let end of task %p, parent: %p", task, parent);
328322
_swift_task_dealloc_specific(parent, task);
329323
}
330324

@@ -349,10 +343,8 @@ static void asyncLet_finish_after_task_completion(SWIFT_ASYNC_CONTEXT AsyncConte
349343
// and finally, release the task and destroy the async-let
350344
assert(swift_task_getCurrent() && "async-let must have a parent task");
351345

352-
#if SWIFT_TASK_PRINTF_DEBUG
353-
fprintf(stderr, "[%lu] async let end of task %p, parent: %p\n",
354-
_swift_get_thread_id(), task, swift_task_getCurrent());
355-
#endif
346+
SWIFT_TASK_DEBUG_LOG("async let end of task %p, parent: %p", task,
347+
swift_task_getCurrent());
356348
// Destruct the task.
357349
task->~AsyncTask();
358350
// Deallocate it out of the parent, if it was allocated there.

stdlib/public/Concurrency/Task.cpp

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,16 @@ FutureFragment::Status AsyncTask::waitFuture(AsyncTask *waitingTask,
8484
switch (queueHead.getStatus()) {
8585
case Status::Error:
8686
case Status::Success:
87-
#if SWIFT_TASK_PRINTF_DEBUG
88-
fprintf(stderr, "[%lu] task %p waiting on task %p, completed immediately\n",
89-
_swift_get_thread_id(), waitingTask, this);
90-
#endif
87+
SWIFT_TASK_DEBUG_LOG("task %p waiting on task %p, completed immediately",
88+
waitingTask, this);
9189
_swift_tsan_acquire(static_cast<Job *>(this));
9290
if (contextIntialized) waitingTask->flagAsRunning();
9391
// The task is done; we don't need to wait.
9492
return queueHead.getStatus();
9593

9694
case Status::Executing:
97-
#if SWIFT_TASK_PRINTF_DEBUG
98-
fprintf(stderr, "[%lu] task %p waiting on task %p, going to sleep\n",
99-
_swift_get_thread_id(), waitingTask, this);
100-
#endif
95+
SWIFT_TASK_DEBUG_LOG("task %p waiting on task %p, going to sleep",
96+
waitingTask, this);
10197
_swift_tsan_release(static_cast<Job *>(waitingTask));
10298
// Task is not complete. We'll need to add ourselves to the queue.
10399
break;
@@ -183,21 +179,16 @@ void AsyncTask::completeFuture(AsyncContext *context) {
183179
// Schedule every waiting task on the executor.
184180
auto waitingTask = queueHead.getTask();
185181

186-
#if SWIFT_TASK_PRINTF_DEBUG
187182
if (!waitingTask)
188-
fprintf(stderr, "[%lu] task %p had no waiting tasks\n",
189-
_swift_get_thread_id(), this);
190-
#endif
183+
SWIFT_TASK_DEBUG_LOG("task %p had no waiting tasks", this);
191184

192185
while (waitingTask) {
193186
// Find the next waiting task before we invalidate it by resuming
194187
// the task.
195188
auto nextWaitingTask = waitingTask->getNextWaitingTask();
196189

197-
#if SWIFT_TASK_PRINTF_DEBUG
198-
fprintf(stderr, "[%lu] waking task %p from future of task %p\n",
199-
_swift_get_thread_id(), waitingTask, this);
200-
#endif
190+
SWIFT_TASK_DEBUG_LOG("waking task %p from future of task %p", waitingTask,
191+
this);
201192

202193
// Fill in the return context.
203194
auto waitingContext =
@@ -247,9 +238,7 @@ static void destroyTask(SWIFT_CONTEXT HeapObject *obj) {
247238
// the task-local allocator. There's actually nothing else to clean up
248239
// here.
249240

250-
#if SWIFT_TASK_PRINTF_DEBUG
251-
fprintf(stderr, "[%lu] destroy task %p\n", _swift_get_thread_id(), task);
252-
#endif
241+
SWIFT_TASK_DEBUG_LOG("destroy task %p", task);
253242
free(task);
254243
}
255244

@@ -322,9 +311,7 @@ static void completeTaskImpl(AsyncTask *task,
322311

323312
task->Private.complete(task);
324313

325-
#if SWIFT_TASK_PRINTF_DEBUG
326-
fprintf(stderr, "[%lu] task %p completed\n", _swift_get_thread_id(), task);
327-
#endif
314+
SWIFT_TASK_DEBUG_LOG("task %p completed", task);
328315

329316
// Complete the future.
330317
// Warning: This deallocates the task in case it's an async let task.
@@ -562,10 +549,8 @@ static AsyncTaskAndContext swift_task_create_commonImpl(
562549
} else {
563550
allocation = malloc(amountToAllocate);
564551
}
565-
#if SWIFT_TASK_PRINTF_DEBUG
566-
fprintf(stderr, "[%lu] allocate task %p, parent = %p, slab %u\n",
567-
_swift_get_thread_id(), allocation, parent, initialSlabSize);
568-
#endif
552+
SWIFT_TASK_DEBUG_LOG("allocate task %p, parent = %p, slab %u", allocation,
553+
parent, initialSlabSize);
569554

570555
AsyncContext *initialContext =
571556
reinterpret_cast<AsyncContext*>(
@@ -640,10 +625,7 @@ static AsyncTaskAndContext swift_task_create_commonImpl(
640625
futureAsyncContextPrefix->indirectResult = futureFragment->getStoragePtr();
641626
}
642627

643-
#if SWIFT_TASK_PRINTF_DEBUG
644-
fprintf(stderr, "[%lu] creating task %p with parent %p\n",
645-
_swift_get_thread_id(), task, parent);
646-
#endif
628+
SWIFT_TASK_DEBUG_LOG("creating task %p with parent %p", task, parent);
647629

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

stdlib/public/Concurrency/TaskPrivate.h

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

4242
namespace swift {
4343

44-
// Uncomment to enable helpful debug spew to stderr
45-
//#define SWIFT_TASK_PRINTF_DEBUG 1
44+
// Set to 1 to enable helpful debug spew to stderr
45+
#if 0
46+
#define SWIFT_TASK_DEBUG_LOG(fmt, ...) \
47+
fprintf(stderr, "[%lu] " fmt "\n", (unsigned long)_swift_get_thread_id(), \
48+
__VA_ARGS__)
49+
#else
50+
#define SWIFT_TASK_DEBUG_LOG(fmt, ...) (void)0
51+
#endif
4652

4753
#if defined(_WIN32)
4854
using ThreadID = decltype(GetCurrentThreadId());
@@ -405,10 +411,7 @@ inline void AsyncTask::flagAsSuspended() {
405411
// that can be used when debugging locally to instrument when a task actually is
406412
// dealloced.
407413
inline void AsyncTask::flagAsCompleted() {
408-
#if SWIFT_TASK_PRINTF_DEBUG
409-
fprintf(stderr, "[%lu] task completed %p\n",
410-
_swift_get_thread_id(), this);
411-
#endif
414+
SWIFT_TASK_DEBUG_LOG("task completed %p", this);
412415
}
413416

414417
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)