Skip to content

Commit 45eaaa2

Browse files
authored
Merge pull request swiftlang#39042 from mikeash/swift-task-debug-printf-cleanup
2 parents 37ee480 + d5c0469 commit 45eaaa2

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
@@ -175,13 +175,10 @@ void swift::swift_asyncLet_begin(AsyncLet *alet,
175175
void *closureEntryPoint,
176176
HeapObject *closureContext,
177177
void *resultBuffer) {
178-
#if SWIFT_TASK_PRINTF_DEBUG
179-
fprintf(stderr, "[%lu] creating async let buffer of type %s at %p\n",
180-
_swift_get_thread_id(),
181-
swift_getTypeName(futureResultType, true).data,
182-
resultBuffer);
183-
#endif
184-
178+
SWIFT_TASK_DEBUG_LOG("creating async let buffer of type %s at %p",
179+
swift_getTypeName(futureResultType, true).data,
180+
resultBuffer);
181+
185182
auto flags = TaskCreateFlags();
186183
flags.setEnqueueJob(true);
187184

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

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

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

351-
#if SWIFT_TASK_PRINTF_DEBUG
352-
fprintf(stderr, "[%lu] async let end of task %p, parent: %p\n",
353-
_swift_get_thread_id(), task, swift_task_getCurrent());
354-
#endif
345+
SWIFT_TASK_DEBUG_LOG("async let end of task %p, parent: %p", task,
346+
swift_task_getCurrent());
355347
// Destruct the task.
356348
task->~AsyncTask();
357349
// 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
@@ -83,20 +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, "[%lu] task %p waiting on task %p, completed immediately\n",
88-
_swift_get_thread_id(), waitingTask, this);
89-
#endif
86+
SWIFT_TASK_DEBUG_LOG("task %p waiting on task %p, completed immediately",
87+
waitingTask, this);
9088
_swift_tsan_acquire(static_cast<Job *>(this));
9189
if (contextIntialized) waitingTask->flagAsRunning();
9290
// The task is done; we don't need to wait.
9391
return queueHead.getStatus();
9492

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

185-
#if SWIFT_TASK_PRINTF_DEBUG
186181
if (!waitingTask)
187-
fprintf(stderr, "[%lu] task %p had no waiting tasks\n",
188-
_swift_get_thread_id(), this);
189-
#endif
182+
SWIFT_TASK_DEBUG_LOG("task %p had no waiting tasks", this);
190183

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

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

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

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

@@ -321,9 +310,7 @@ static void completeTaskImpl(AsyncTask *task,
321310

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

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

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

569554
AsyncContext *initialContext =
570555
reinterpret_cast<AsyncContext*>(
@@ -639,10 +624,7 @@ static AsyncTaskAndContext swift_task_create_commonImpl(
639624
futureAsyncContextPrefix->indirectResult = futureFragment->getStoragePtr();
640625
}
641626

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

647629
// Initialize the task-local allocator.
648630
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)