{"id":13722,"date":"2016-03-01T14:19:39","date_gmt":"2016-03-01T19:19:39","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=13722"},"modified":"2016-03-01T14:19:39","modified_gmt":"2016-03-01T19:19:39","slug":"tracking-tasks-with-stack-traces-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/03\/01\/tracking-tasks-with-stack-traces-in-swift\/","title":{"rendered":"Tracking Tasks With Stack Traces in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/www.cocoawithlove.com\/blog\/2016\/02\/28\/stack-traces-in-swift.html\">Matt Gallagher<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.cocoawithlove.com\/blog\/2016\/02\/28\/stack-traces-in-swift.html\">\n<p>Instead of using log files to track asynchronous tasks, I prefer to use a structure that I call a &ldquo;task journal&rdquo;.<\/p>\n<p>[&#8230;]<\/p>\n<p>We&rsquo;d have a bit more flexibility if we could use the C function <code>backtrace<\/code> but in Swift, we can&rsquo;t. The <code>backtrace<\/code> function comes from the &ldquo;execinfo.h&rdquo; header and is implemented as part of libSystem on OS X and iOS (which all Swift programs link against) but for whatever reason, the &ldquo;execinfo.h&rdquo; contents are not exposed to Swift programs.<\/p>\n<p>[&#8230;]<\/p>\n<p>The resulting <code>callStackReturnAddresses<\/code> function is about twice as fast as <code>NSThread.callStackReturnAddresses<\/code> on the basic tests I&rsquo;ve tried (roughly 2 million invocations per second per core on my Mac versus 1 million invocations per second for <code>NSThread.callStackReturnAddresses<\/code>). It&rsquo;s easily fast enough to gather lots of data in Debug builds &#8211; even for fairly intensive computational paths.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Matt Gallagher: Instead of using log files to track asynchronous tasks, I prefer to use a structure that I call a &ldquo;task journal&rdquo;. [&#8230;] We&rsquo;d have a bit more flexibility if we could use the C function backtrace but in Swift, we can&rsquo;t. The backtrace function comes from the &ldquo;execinfo.h&rdquo; header and is implemented as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,56,31,30,74,71,901],"class_list":["post-13722","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-debugging","tag-ios","tag-mac","tag-opensource","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=13722"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13722\/revisions"}],"predecessor-version":[{"id":13723,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13722\/revisions\/13723"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=13722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=13722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=13722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}