├── README.md └── RunloopMonitorDemo ├── RunloopMonitorDemo.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── Cooteker.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── Cooteker.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── RunloopMonitorDemo.xcscheme │ └── xcschememanagement.plist ├── RunloopMonitorDemo ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist ├── MonitorController │ ├── MonitorController.h │ └── MonitorController.m ├── SeMonitorController │ ├── SeMonitorController.h │ └── SeMonitorController.m ├── ViewController.h ├── ViewController.m └── main.m ├── RunloopMonitorDemoTests ├── Info.plist └── RunloopMonitorDemoTests.m └── RunloopMonitorDemoUITests ├── Info.plist └── RunloopMonitorDemoUITests.m /README.md: -------------------------------------------------------------------------------- 1 | # RunloopMonitorDemo 2 | 3 | a demo for monitoring main thread 4 | use [plcrashreporter](https://github.com/plausiblelabs/plcrashreporter) to record current stack and heap 5 | 6 | 主线程卡顿的demo 7 | 可以使用 [plcrashreporter](https://github.com/plausiblelabs/plcrashreporter)去记录堆栈 8 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | AF4AE2D41CBEA68E00387E66 /* SeMonitorController.m in Sources */ = {isa = PBXBuildFile; fileRef = AF4AE2D31CBEA68E00387E66 /* SeMonitorController.m */; }; 11 | AFA803311CBE3AB100EBE42A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = AFA803301CBE3AB100EBE42A /* main.m */; }; 12 | AFA803341CBE3AB600EBE42A /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = AFA803331CBE3AB500EBE42A /* AppDelegate.m */; }; 13 | AFA803371CBE3AB700EBE42A /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = AFA803361CBE3AB700EBE42A /* ViewController.m */; }; 14 | AFA8033A1CBE3ABA00EBE42A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AFA803381CBE3ABA00EBE42A /* Main.storyboard */; }; 15 | AFA8033C1CBE3ABF00EBE42A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AFA8033B1CBE3ABF00EBE42A /* Assets.xcassets */; }; 16 | AFA8033F1CBE3AC200EBE42A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AFA8033D1CBE3AC200EBE42A /* LaunchScreen.storyboard */; }; 17 | AFA8034A1CBE3ADC00EBE42A /* RunloopMonitorDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AFA803491CBE3ADC00EBE42A /* RunloopMonitorDemoTests.m */; }; 18 | AFA803551CBE3ADF00EBE42A /* RunloopMonitorDemoUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = AFA803541CBE3ADF00EBE42A /* RunloopMonitorDemoUITests.m */; }; 19 | AFA803651CBE412800EBE42A /* MonitorController.m in Sources */ = {isa = PBXBuildFile; fileRef = AFA803641CBE412800EBE42A /* MonitorController.m */; }; 20 | /* End PBXBuildFile section */ 21 | 22 | /* Begin PBXContainerItemProxy section */ 23 | AFA803461CBE3ADB00EBE42A /* PBXContainerItemProxy */ = { 24 | isa = PBXContainerItemProxy; 25 | containerPortal = AFA803241CBE3AA500EBE42A /* Project object */; 26 | proxyType = 1; 27 | remoteGlobalIDString = AFA8032B1CBE3AA800EBE42A; 28 | remoteInfo = RunloopMonitorDemo; 29 | }; 30 | AFA803511CBE3ADE00EBE42A /* PBXContainerItemProxy */ = { 31 | isa = PBXContainerItemProxy; 32 | containerPortal = AFA803241CBE3AA500EBE42A /* Project object */; 33 | proxyType = 1; 34 | remoteGlobalIDString = AFA8032B1CBE3AA800EBE42A; 35 | remoteInfo = RunloopMonitorDemo; 36 | }; 37 | /* End PBXContainerItemProxy section */ 38 | 39 | /* Begin PBXFileReference section */ 40 | AF4AE2D21CBEA68E00387E66 /* SeMonitorController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SeMonitorController.h; sourceTree = ""; }; 41 | AF4AE2D31CBEA68E00387E66 /* SeMonitorController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SeMonitorController.m; sourceTree = ""; }; 42 | AFA8032C1CBE3AA900EBE42A /* RunloopMonitorDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RunloopMonitorDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 43 | AFA803301CBE3AB100EBE42A /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 44 | AFA803321CBE3AB200EBE42A /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 45 | AFA803331CBE3AB500EBE42A /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 46 | AFA803351CBE3AB600EBE42A /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 47 | AFA803361CBE3AB700EBE42A /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 48 | AFA803391CBE3ABA00EBE42A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 49 | AFA8033B1CBE3ABF00EBE42A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 50 | AFA8033E1CBE3AC200EBE42A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 51 | AFA803401CBE3AC300EBE42A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 52 | AFA803451CBE3ADB00EBE42A /* RunloopMonitorDemoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RunloopMonitorDemoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 53 | AFA803491CBE3ADC00EBE42A /* RunloopMonitorDemoTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RunloopMonitorDemoTests.m; sourceTree = ""; }; 54 | AFA8034B1CBE3ADD00EBE42A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 55 | AFA803501CBE3ADE00EBE42A /* RunloopMonitorDemoUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RunloopMonitorDemoUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 56 | AFA803541CBE3ADF00EBE42A /* RunloopMonitorDemoUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RunloopMonitorDemoUITests.m; sourceTree = ""; }; 57 | AFA803561CBE3AE000EBE42A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 58 | AFA803631CBE412800EBE42A /* MonitorController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MonitorController.h; sourceTree = ""; }; 59 | AFA803641CBE412800EBE42A /* MonitorController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MonitorController.m; sourceTree = ""; }; 60 | /* End PBXFileReference section */ 61 | 62 | /* Begin PBXFrameworksBuildPhase section */ 63 | AFA803291CBE3AA800EBE42A /* Frameworks */ = { 64 | isa = PBXFrameworksBuildPhase; 65 | buildActionMask = 2147483647; 66 | files = ( 67 | ); 68 | runOnlyForDeploymentPostprocessing = 0; 69 | }; 70 | AFA803421CBE3ADB00EBE42A /* Frameworks */ = { 71 | isa = PBXFrameworksBuildPhase; 72 | buildActionMask = 2147483647; 73 | files = ( 74 | ); 75 | runOnlyForDeploymentPostprocessing = 0; 76 | }; 77 | AFA8034D1CBE3ADD00EBE42A /* Frameworks */ = { 78 | isa = PBXFrameworksBuildPhase; 79 | buildActionMask = 2147483647; 80 | files = ( 81 | ); 82 | runOnlyForDeploymentPostprocessing = 0; 83 | }; 84 | /* End PBXFrameworksBuildPhase section */ 85 | 86 | /* Begin PBXGroup section */ 87 | AF4AE2D11CBEA64400387E66 /* SeMonitorController */ = { 88 | isa = PBXGroup; 89 | children = ( 90 | AF4AE2D21CBEA68E00387E66 /* SeMonitorController.h */, 91 | AF4AE2D31CBEA68E00387E66 /* SeMonitorController.m */, 92 | ); 93 | path = SeMonitorController; 94 | sourceTree = ""; 95 | }; 96 | AFA803231CBE3AA300EBE42A = { 97 | isa = PBXGroup; 98 | children = ( 99 | AFA8032E1CBE3AB000EBE42A /* RunloopMonitorDemo */, 100 | AFA803481CBE3ADB00EBE42A /* RunloopMonitorDemoTests */, 101 | AFA803531CBE3ADE00EBE42A /* RunloopMonitorDemoUITests */, 102 | AFA8032D1CBE3AA900EBE42A /* Products */, 103 | ); 104 | sourceTree = ""; 105 | }; 106 | AFA8032D1CBE3AA900EBE42A /* Products */ = { 107 | isa = PBXGroup; 108 | children = ( 109 | AFA8032C1CBE3AA900EBE42A /* RunloopMonitorDemo.app */, 110 | AFA803451CBE3ADB00EBE42A /* RunloopMonitorDemoTests.xctest */, 111 | AFA803501CBE3ADE00EBE42A /* RunloopMonitorDemoUITests.xctest */, 112 | ); 113 | name = Products; 114 | sourceTree = ""; 115 | }; 116 | AFA8032E1CBE3AB000EBE42A /* RunloopMonitorDemo */ = { 117 | isa = PBXGroup; 118 | children = ( 119 | AF4AE2D11CBEA64400387E66 /* SeMonitorController */, 120 | AFA803621CBE410400EBE42A /* MonitorController */, 121 | AFA803321CBE3AB200EBE42A /* AppDelegate.h */, 122 | AFA803331CBE3AB500EBE42A /* AppDelegate.m */, 123 | AFA803351CBE3AB600EBE42A /* ViewController.h */, 124 | AFA803361CBE3AB700EBE42A /* ViewController.m */, 125 | AFA803381CBE3ABA00EBE42A /* Main.storyboard */, 126 | AFA8033B1CBE3ABF00EBE42A /* Assets.xcassets */, 127 | AFA8033D1CBE3AC200EBE42A /* LaunchScreen.storyboard */, 128 | AFA803401CBE3AC300EBE42A /* Info.plist */, 129 | AFA8032F1CBE3AB100EBE42A /* Supporting Files */, 130 | ); 131 | path = RunloopMonitorDemo; 132 | sourceTree = ""; 133 | }; 134 | AFA8032F1CBE3AB100EBE42A /* Supporting Files */ = { 135 | isa = PBXGroup; 136 | children = ( 137 | AFA803301CBE3AB100EBE42A /* main.m */, 138 | ); 139 | name = "Supporting Files"; 140 | sourceTree = ""; 141 | }; 142 | AFA803481CBE3ADB00EBE42A /* RunloopMonitorDemoTests */ = { 143 | isa = PBXGroup; 144 | children = ( 145 | AFA803491CBE3ADC00EBE42A /* RunloopMonitorDemoTests.m */, 146 | AFA8034B1CBE3ADD00EBE42A /* Info.plist */, 147 | ); 148 | path = RunloopMonitorDemoTests; 149 | sourceTree = ""; 150 | }; 151 | AFA803531CBE3ADE00EBE42A /* RunloopMonitorDemoUITests */ = { 152 | isa = PBXGroup; 153 | children = ( 154 | AFA803541CBE3ADF00EBE42A /* RunloopMonitorDemoUITests.m */, 155 | AFA803561CBE3AE000EBE42A /* Info.plist */, 156 | ); 157 | path = RunloopMonitorDemoUITests; 158 | sourceTree = ""; 159 | }; 160 | AFA803621CBE410400EBE42A /* MonitorController */ = { 161 | isa = PBXGroup; 162 | children = ( 163 | AFA803631CBE412800EBE42A /* MonitorController.h */, 164 | AFA803641CBE412800EBE42A /* MonitorController.m */, 165 | ); 166 | path = MonitorController; 167 | sourceTree = ""; 168 | }; 169 | /* End PBXGroup section */ 170 | 171 | /* Begin PBXNativeTarget section */ 172 | AFA8032B1CBE3AA800EBE42A /* RunloopMonitorDemo */ = { 173 | isa = PBXNativeTarget; 174 | buildConfigurationList = AFA803591CBE3AE000EBE42A /* Build configuration list for PBXNativeTarget "RunloopMonitorDemo" */; 175 | buildPhases = ( 176 | AFA803281CBE3AA800EBE42A /* Sources */, 177 | AFA803291CBE3AA800EBE42A /* Frameworks */, 178 | AFA8032A1CBE3AA800EBE42A /* Resources */, 179 | ); 180 | buildRules = ( 181 | ); 182 | dependencies = ( 183 | ); 184 | name = RunloopMonitorDemo; 185 | productName = RunloopMonitorDemo; 186 | productReference = AFA8032C1CBE3AA900EBE42A /* RunloopMonitorDemo.app */; 187 | productType = "com.apple.product-type.application"; 188 | }; 189 | AFA803441CBE3ADB00EBE42A /* RunloopMonitorDemoTests */ = { 190 | isa = PBXNativeTarget; 191 | buildConfigurationList = AFA8035C1CBE3AE000EBE42A /* Build configuration list for PBXNativeTarget "RunloopMonitorDemoTests" */; 192 | buildPhases = ( 193 | AFA803411CBE3ADB00EBE42A /* Sources */, 194 | AFA803421CBE3ADB00EBE42A /* Frameworks */, 195 | AFA803431CBE3ADB00EBE42A /* Resources */, 196 | ); 197 | buildRules = ( 198 | ); 199 | dependencies = ( 200 | AFA803471CBE3ADB00EBE42A /* PBXTargetDependency */, 201 | ); 202 | name = RunloopMonitorDemoTests; 203 | productName = RunloopMonitorDemoTests; 204 | productReference = AFA803451CBE3ADB00EBE42A /* RunloopMonitorDemoTests.xctest */; 205 | productType = "com.apple.product-type.bundle.unit-test"; 206 | }; 207 | AFA8034F1CBE3ADD00EBE42A /* RunloopMonitorDemoUITests */ = { 208 | isa = PBXNativeTarget; 209 | buildConfigurationList = AFA8035F1CBE3AE000EBE42A /* Build configuration list for PBXNativeTarget "RunloopMonitorDemoUITests" */; 210 | buildPhases = ( 211 | AFA8034C1CBE3ADD00EBE42A /* Sources */, 212 | AFA8034D1CBE3ADD00EBE42A /* Frameworks */, 213 | AFA8034E1CBE3ADD00EBE42A /* Resources */, 214 | ); 215 | buildRules = ( 216 | ); 217 | dependencies = ( 218 | AFA803521CBE3ADE00EBE42A /* PBXTargetDependency */, 219 | ); 220 | name = RunloopMonitorDemoUITests; 221 | productName = RunloopMonitorDemoUITests; 222 | productReference = AFA803501CBE3ADE00EBE42A /* RunloopMonitorDemoUITests.xctest */; 223 | productType = "com.apple.product-type.bundle.ui-testing"; 224 | }; 225 | /* End PBXNativeTarget section */ 226 | 227 | /* Begin PBXProject section */ 228 | AFA803241CBE3AA500EBE42A /* Project object */ = { 229 | isa = PBXProject; 230 | attributes = { 231 | LastUpgradeCheck = 0730; 232 | ORGANIZATIONNAME = game3108; 233 | TargetAttributes = { 234 | AFA8032B1CBE3AA800EBE42A = { 235 | CreatedOnToolsVersion = 7.3; 236 | DevelopmentTeam = 2QKL8H8BDR; 237 | }; 238 | AFA803441CBE3ADB00EBE42A = { 239 | CreatedOnToolsVersion = 7.3; 240 | DevelopmentTeam = 2QKL8H8BDR; 241 | TestTargetID = AFA8032B1CBE3AA800EBE42A; 242 | }; 243 | AFA8034F1CBE3ADD00EBE42A = { 244 | CreatedOnToolsVersion = 7.3; 245 | DevelopmentTeam = 2QKL8H8BDR; 246 | TestTargetID = AFA8032B1CBE3AA800EBE42A; 247 | }; 248 | }; 249 | }; 250 | buildConfigurationList = AFA803271CBE3AA500EBE42A /* Build configuration list for PBXProject "RunloopMonitorDemo" */; 251 | compatibilityVersion = "Xcode 3.2"; 252 | developmentRegion = English; 253 | hasScannedForEncodings = 0; 254 | knownRegions = ( 255 | en, 256 | Base, 257 | ); 258 | mainGroup = AFA803231CBE3AA300EBE42A; 259 | productRefGroup = AFA8032D1CBE3AA900EBE42A /* Products */; 260 | projectDirPath = ""; 261 | projectRoot = ""; 262 | targets = ( 263 | AFA8032B1CBE3AA800EBE42A /* RunloopMonitorDemo */, 264 | AFA803441CBE3ADB00EBE42A /* RunloopMonitorDemoTests */, 265 | AFA8034F1CBE3ADD00EBE42A /* RunloopMonitorDemoUITests */, 266 | ); 267 | }; 268 | /* End PBXProject section */ 269 | 270 | /* Begin PBXResourcesBuildPhase section */ 271 | AFA8032A1CBE3AA800EBE42A /* Resources */ = { 272 | isa = PBXResourcesBuildPhase; 273 | buildActionMask = 2147483647; 274 | files = ( 275 | AFA8033F1CBE3AC200EBE42A /* LaunchScreen.storyboard in Resources */, 276 | AFA8033C1CBE3ABF00EBE42A /* Assets.xcassets in Resources */, 277 | AFA8033A1CBE3ABA00EBE42A /* Main.storyboard in Resources */, 278 | ); 279 | runOnlyForDeploymentPostprocessing = 0; 280 | }; 281 | AFA803431CBE3ADB00EBE42A /* Resources */ = { 282 | isa = PBXResourcesBuildPhase; 283 | buildActionMask = 2147483647; 284 | files = ( 285 | ); 286 | runOnlyForDeploymentPostprocessing = 0; 287 | }; 288 | AFA8034E1CBE3ADD00EBE42A /* Resources */ = { 289 | isa = PBXResourcesBuildPhase; 290 | buildActionMask = 2147483647; 291 | files = ( 292 | ); 293 | runOnlyForDeploymentPostprocessing = 0; 294 | }; 295 | /* End PBXResourcesBuildPhase section */ 296 | 297 | /* Begin PBXSourcesBuildPhase section */ 298 | AFA803281CBE3AA800EBE42A /* Sources */ = { 299 | isa = PBXSourcesBuildPhase; 300 | buildActionMask = 2147483647; 301 | files = ( 302 | AFA803371CBE3AB700EBE42A /* ViewController.m in Sources */, 303 | AFA803341CBE3AB600EBE42A /* AppDelegate.m in Sources */, 304 | AFA803651CBE412800EBE42A /* MonitorController.m in Sources */, 305 | AF4AE2D41CBEA68E00387E66 /* SeMonitorController.m in Sources */, 306 | AFA803311CBE3AB100EBE42A /* main.m in Sources */, 307 | ); 308 | runOnlyForDeploymentPostprocessing = 0; 309 | }; 310 | AFA803411CBE3ADB00EBE42A /* Sources */ = { 311 | isa = PBXSourcesBuildPhase; 312 | buildActionMask = 2147483647; 313 | files = ( 314 | AFA8034A1CBE3ADC00EBE42A /* RunloopMonitorDemoTests.m in Sources */, 315 | ); 316 | runOnlyForDeploymentPostprocessing = 0; 317 | }; 318 | AFA8034C1CBE3ADD00EBE42A /* Sources */ = { 319 | isa = PBXSourcesBuildPhase; 320 | buildActionMask = 2147483647; 321 | files = ( 322 | AFA803551CBE3ADF00EBE42A /* RunloopMonitorDemoUITests.m in Sources */, 323 | ); 324 | runOnlyForDeploymentPostprocessing = 0; 325 | }; 326 | /* End PBXSourcesBuildPhase section */ 327 | 328 | /* Begin PBXTargetDependency section */ 329 | AFA803471CBE3ADB00EBE42A /* PBXTargetDependency */ = { 330 | isa = PBXTargetDependency; 331 | target = AFA8032B1CBE3AA800EBE42A /* RunloopMonitorDemo */; 332 | targetProxy = AFA803461CBE3ADB00EBE42A /* PBXContainerItemProxy */; 333 | }; 334 | AFA803521CBE3ADE00EBE42A /* PBXTargetDependency */ = { 335 | isa = PBXTargetDependency; 336 | target = AFA8032B1CBE3AA800EBE42A /* RunloopMonitorDemo */; 337 | targetProxy = AFA803511CBE3ADE00EBE42A /* PBXContainerItemProxy */; 338 | }; 339 | /* End PBXTargetDependency section */ 340 | 341 | /* Begin PBXVariantGroup section */ 342 | AFA803381CBE3ABA00EBE42A /* Main.storyboard */ = { 343 | isa = PBXVariantGroup; 344 | children = ( 345 | AFA803391CBE3ABA00EBE42A /* Base */, 346 | ); 347 | name = Main.storyboard; 348 | sourceTree = ""; 349 | }; 350 | AFA8033D1CBE3AC200EBE42A /* LaunchScreen.storyboard */ = { 351 | isa = PBXVariantGroup; 352 | children = ( 353 | AFA8033E1CBE3AC200EBE42A /* Base */, 354 | ); 355 | name = LaunchScreen.storyboard; 356 | sourceTree = ""; 357 | }; 358 | /* End PBXVariantGroup section */ 359 | 360 | /* Begin XCBuildConfiguration section */ 361 | AFA803571CBE3AE000EBE42A /* Debug */ = { 362 | isa = XCBuildConfiguration; 363 | buildSettings = { 364 | ALWAYS_SEARCH_USER_PATHS = NO; 365 | CLANG_ANALYZER_NONNULL = YES; 366 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 367 | CLANG_CXX_LIBRARY = "libc++"; 368 | CLANG_ENABLE_MODULES = YES; 369 | CLANG_ENABLE_OBJC_ARC = YES; 370 | CLANG_WARN_BOOL_CONVERSION = YES; 371 | CLANG_WARN_CONSTANT_CONVERSION = YES; 372 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 373 | CLANG_WARN_EMPTY_BODY = YES; 374 | CLANG_WARN_ENUM_CONVERSION = YES; 375 | CLANG_WARN_INT_CONVERSION = YES; 376 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 377 | CLANG_WARN_UNREACHABLE_CODE = YES; 378 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 379 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 380 | COPY_PHASE_STRIP = NO; 381 | DEBUG_INFORMATION_FORMAT = dwarf; 382 | ENABLE_STRICT_OBJC_MSGSEND = YES; 383 | ENABLE_TESTABILITY = YES; 384 | GCC_C_LANGUAGE_STANDARD = gnu99; 385 | GCC_DYNAMIC_NO_PIC = NO; 386 | GCC_NO_COMMON_BLOCKS = YES; 387 | GCC_OPTIMIZATION_LEVEL = 0; 388 | GCC_PREPROCESSOR_DEFINITIONS = ( 389 | "DEBUG=1", 390 | "$(inherited)", 391 | ); 392 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 393 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 394 | GCC_WARN_UNDECLARED_SELECTOR = YES; 395 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 396 | GCC_WARN_UNUSED_FUNCTION = YES; 397 | GCC_WARN_UNUSED_VARIABLE = YES; 398 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 399 | MTL_ENABLE_DEBUG_INFO = YES; 400 | ONLY_ACTIVE_ARCH = YES; 401 | SDKROOT = iphoneos; 402 | TARGETED_DEVICE_FAMILY = "1,2"; 403 | }; 404 | name = Debug; 405 | }; 406 | AFA803581CBE3AE000EBE42A /* Release */ = { 407 | isa = XCBuildConfiguration; 408 | buildSettings = { 409 | ALWAYS_SEARCH_USER_PATHS = NO; 410 | CLANG_ANALYZER_NONNULL = YES; 411 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 412 | CLANG_CXX_LIBRARY = "libc++"; 413 | CLANG_ENABLE_MODULES = YES; 414 | CLANG_ENABLE_OBJC_ARC = YES; 415 | CLANG_WARN_BOOL_CONVERSION = YES; 416 | CLANG_WARN_CONSTANT_CONVERSION = YES; 417 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 418 | CLANG_WARN_EMPTY_BODY = YES; 419 | CLANG_WARN_ENUM_CONVERSION = YES; 420 | CLANG_WARN_INT_CONVERSION = YES; 421 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 422 | CLANG_WARN_UNREACHABLE_CODE = YES; 423 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 424 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 425 | COPY_PHASE_STRIP = NO; 426 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 427 | ENABLE_NS_ASSERTIONS = NO; 428 | ENABLE_STRICT_OBJC_MSGSEND = YES; 429 | GCC_C_LANGUAGE_STANDARD = gnu99; 430 | GCC_NO_COMMON_BLOCKS = YES; 431 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 432 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 433 | GCC_WARN_UNDECLARED_SELECTOR = YES; 434 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 435 | GCC_WARN_UNUSED_FUNCTION = YES; 436 | GCC_WARN_UNUSED_VARIABLE = YES; 437 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 438 | MTL_ENABLE_DEBUG_INFO = NO; 439 | SDKROOT = iphoneos; 440 | TARGETED_DEVICE_FAMILY = "1,2"; 441 | VALIDATE_PRODUCT = YES; 442 | }; 443 | name = Release; 444 | }; 445 | AFA8035A1CBE3AE000EBE42A /* Debug */ = { 446 | isa = XCBuildConfiguration; 447 | buildSettings = { 448 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 449 | CODE_SIGN_IDENTITY = "iPhone Developer"; 450 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 451 | INFOPLIST_FILE = RunloopMonitorDemo/Info.plist; 452 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 453 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 454 | PRODUCT_BUNDLE_IDENTIFIER = game3108.RunloopMonitorDemo; 455 | PRODUCT_NAME = "$(TARGET_NAME)"; 456 | PROVISIONING_PROFILE = ""; 457 | }; 458 | name = Debug; 459 | }; 460 | AFA8035B1CBE3AE000EBE42A /* Release */ = { 461 | isa = XCBuildConfiguration; 462 | buildSettings = { 463 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 464 | CODE_SIGN_IDENTITY = "iPhone Developer"; 465 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 466 | INFOPLIST_FILE = RunloopMonitorDemo/Info.plist; 467 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 468 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 469 | PRODUCT_BUNDLE_IDENTIFIER = game3108.RunloopMonitorDemo; 470 | PRODUCT_NAME = "$(TARGET_NAME)"; 471 | PROVISIONING_PROFILE = ""; 472 | }; 473 | name = Release; 474 | }; 475 | AFA8035D1CBE3AE000EBE42A /* Debug */ = { 476 | isa = XCBuildConfiguration; 477 | buildSettings = { 478 | BUNDLE_LOADER = "$(TEST_HOST)"; 479 | INFOPLIST_FILE = RunloopMonitorDemoTests/Info.plist; 480 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 481 | PRODUCT_BUNDLE_IDENTIFIER = game3108.RunloopMonitorDemoTests; 482 | PRODUCT_NAME = "$(TARGET_NAME)"; 483 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/RunloopMonitorDemo.app/RunloopMonitorDemo"; 484 | }; 485 | name = Debug; 486 | }; 487 | AFA8035E1CBE3AE000EBE42A /* Release */ = { 488 | isa = XCBuildConfiguration; 489 | buildSettings = { 490 | BUNDLE_LOADER = "$(TEST_HOST)"; 491 | INFOPLIST_FILE = RunloopMonitorDemoTests/Info.plist; 492 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 493 | PRODUCT_BUNDLE_IDENTIFIER = game3108.RunloopMonitorDemoTests; 494 | PRODUCT_NAME = "$(TARGET_NAME)"; 495 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/RunloopMonitorDemo.app/RunloopMonitorDemo"; 496 | }; 497 | name = Release; 498 | }; 499 | AFA803601CBE3AE000EBE42A /* Debug */ = { 500 | isa = XCBuildConfiguration; 501 | buildSettings = { 502 | INFOPLIST_FILE = RunloopMonitorDemoUITests/Info.plist; 503 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 504 | PRODUCT_BUNDLE_IDENTIFIER = game3108.RunloopMonitorDemoUITests; 505 | PRODUCT_NAME = "$(TARGET_NAME)"; 506 | TEST_TARGET_NAME = RunloopMonitorDemo; 507 | }; 508 | name = Debug; 509 | }; 510 | AFA803611CBE3AE000EBE42A /* Release */ = { 511 | isa = XCBuildConfiguration; 512 | buildSettings = { 513 | INFOPLIST_FILE = RunloopMonitorDemoUITests/Info.plist; 514 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 515 | PRODUCT_BUNDLE_IDENTIFIER = game3108.RunloopMonitorDemoUITests; 516 | PRODUCT_NAME = "$(TARGET_NAME)"; 517 | TEST_TARGET_NAME = RunloopMonitorDemo; 518 | }; 519 | name = Release; 520 | }; 521 | /* End XCBuildConfiguration section */ 522 | 523 | /* Begin XCConfigurationList section */ 524 | AFA803271CBE3AA500EBE42A /* Build configuration list for PBXProject "RunloopMonitorDemo" */ = { 525 | isa = XCConfigurationList; 526 | buildConfigurations = ( 527 | AFA803571CBE3AE000EBE42A /* Debug */, 528 | AFA803581CBE3AE000EBE42A /* Release */, 529 | ); 530 | defaultConfigurationIsVisible = 0; 531 | defaultConfigurationName = Release; 532 | }; 533 | AFA803591CBE3AE000EBE42A /* Build configuration list for PBXNativeTarget "RunloopMonitorDemo" */ = { 534 | isa = XCConfigurationList; 535 | buildConfigurations = ( 536 | AFA8035A1CBE3AE000EBE42A /* Debug */, 537 | AFA8035B1CBE3AE000EBE42A /* Release */, 538 | ); 539 | defaultConfigurationIsVisible = 0; 540 | defaultConfigurationName = Release; 541 | }; 542 | AFA8035C1CBE3AE000EBE42A /* Build configuration list for PBXNativeTarget "RunloopMonitorDemoTests" */ = { 543 | isa = XCConfigurationList; 544 | buildConfigurations = ( 545 | AFA8035D1CBE3AE000EBE42A /* Debug */, 546 | AFA8035E1CBE3AE000EBE42A /* Release */, 547 | ); 548 | defaultConfigurationIsVisible = 0; 549 | defaultConfigurationName = Release; 550 | }; 551 | AFA8035F1CBE3AE000EBE42A /* Build configuration list for PBXNativeTarget "RunloopMonitorDemoUITests" */ = { 552 | isa = XCConfigurationList; 553 | buildConfigurations = ( 554 | AFA803601CBE3AE000EBE42A /* Debug */, 555 | AFA803611CBE3AE000EBE42A /* Release */, 556 | ); 557 | defaultConfigurationIsVisible = 0; 558 | defaultConfigurationName = Release; 559 | }; 560 | /* End XCConfigurationList section */ 561 | }; 562 | rootObject = AFA803241CBE3AA500EBE42A /* Project object */; 563 | } 564 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo.xcodeproj/project.xcworkspace/xcuserdata/Cooteker.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/game3108/RunloopMonitorDemo/97ab063260f693a53af5feaef1cb851d6a45c952/RunloopMonitorDemo/RunloopMonitorDemo.xcodeproj/project.xcworkspace/xcuserdata/Cooteker.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo.xcodeproj/xcuserdata/Cooteker.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo.xcodeproj/xcuserdata/Cooteker.xcuserdatad/xcschemes/RunloopMonitorDemo.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 43 | 49 | 50 | 51 | 52 | 53 | 59 | 60 | 61 | 62 | 63 | 64 | 74 | 76 | 82 | 83 | 84 | 85 | 86 | 87 | 93 | 95 | 101 | 102 | 103 | 104 | 106 | 107 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo.xcodeproj/xcuserdata/Cooteker.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | RunloopMonitorDemo.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | AFA8032B1CBE3AA800EBE42A 16 | 17 | primary 18 | 19 | 20 | AFA803441CBE3ADB00EBE42A 21 | 22 | primary 23 | 24 | 25 | AFA8034F1CBE3ADD00EBE42A 26 | 27 | primary 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // RunloopMonitorDemo 4 | // 5 | // Created by game3108 on 16/4/13. 6 | // Copyright © 2016年 game3108. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // RunloopMonitorDemo 4 | // 5 | // Created by game3108 on 16/4/13. 6 | // Copyright © 2016年 game3108. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 25 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo/MonitorController/MonitorController.h: -------------------------------------------------------------------------------- 1 | // 2 | // MonitorController.h 3 | // RunloopMonitorDemo 4 | // 5 | // Created by game3108 on 16/4/13. 6 | // Copyright © 2016年 game3108. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MonitorController : NSObject 12 | + (instancetype) sharedInstance; 13 | - (void) startMonitor; 14 | - (void) endMonitor; 15 | - (void) printLogTrace; 16 | @end 17 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo/MonitorController/MonitorController.m: -------------------------------------------------------------------------------- 1 | // 2 | // MonitorController.m 3 | // RunloopMonitorDemo 4 | // 5 | // Created by game3108 on 16/4/13. 6 | // Copyright © 2016年 game3108. All rights reserved. 7 | // 8 | 9 | #import "MonitorController.h" 10 | #import 11 | #import 12 | 13 | @interface MonitorController(){ 14 | CFRunLoopObserverRef _observer; 15 | double _lastRecordTime; 16 | NSMutableArray *_backtrace; 17 | } 18 | 19 | @end 20 | 21 | @implementation MonitorController 22 | 23 | static double _waitStartTime; 24 | 25 | 26 | + (instancetype) sharedInstance{ 27 | static dispatch_once_t once; 28 | static id sharedInstance; 29 | dispatch_once(&once, ^{ 30 | sharedInstance = [[self alloc] init]; 31 | }); 32 | return sharedInstance; 33 | } 34 | 35 | - (void) startMonitor{ 36 | [self addMainThreadObserver]; 37 | [self addSecondaryThreadAndObserver]; 38 | } 39 | 40 | - (void) endMonitor{ 41 | if (!_observer) { 42 | return; 43 | } 44 | CFRunLoopRemoveObserver(CFRunLoopGetMain(), _observer, kCFRunLoopCommonModes); 45 | CFRelease(_observer); 46 | _observer = NULL; 47 | } 48 | 49 | #pragma mark printLogTrace 50 | - (void)printLogTrace{ 51 | NSLog(@"====================堆栈\n %@ \n",_backtrace); 52 | } 53 | 54 | 55 | #pragma mark addMainThreadObserver 56 | - (void) addMainThreadObserver { 57 | dispatch_async(dispatch_get_main_queue(), ^{ 58 | //建立自动释放池 59 | @autoreleasepool { 60 | //获得当前thread的Run loop 61 | NSRunLoop *myRunLoop = [NSRunLoop currentRunLoop]; 62 | 63 | //设置Run loop observer的运行环境 64 | CFRunLoopObserverContext context = {0, (__bridge void *)(self), NULL, NULL, NULL}; 65 | 66 | //创建Run loop observer对象 67 | //第一个参数用于分配observer对象的内存 68 | //第二个参数用以设置observer所要关注的事件,详见回调函数myRunLoopObserver中注释 69 | //第三个参数用于标识该observer是在第一次进入run loop时执行还是每次进入run loop处理时均执行 70 | //第四个参数用于设置该observer的优先级 71 | //第五个参数用于设置该observer的回调函数 72 | //第六个参数用于设置该observer的运行环境 73 | _observer = CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopAllActivities, YES, 0, &myRunLoopObserver, &context); 74 | 75 | if (_observer) { 76 | //将Cocoa的NSRunLoop类型转换成Core Foundation的CFRunLoopRef类型 77 | CFRunLoopRef cfRunLoop = [myRunLoop getCFRunLoop]; 78 | //将新建的observer加入到当前thread的run loop 79 | CFRunLoopAddObserver(cfRunLoop, _observer, kCFRunLoopDefaultMode); 80 | } 81 | } 82 | }); 83 | } 84 | 85 | void myRunLoopObserver(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info) { 86 | switch (activity) { 87 | //The entrance of the run loop, before entering the event processing loop. 88 | //This activity occurs once for each call to CFRunLoopRun and CFRunLoopRunInMode 89 | case kCFRunLoopEntry: 90 | NSLog(@"run loop entry"); 91 | break; 92 | //Inside the event processing loop before any timers are processed 93 | case kCFRunLoopBeforeTimers: 94 | NSLog(@"run loop before timers"); 95 | break; 96 | //Inside the event processing loop before any sources are processed 97 | case kCFRunLoopBeforeSources: 98 | NSLog(@"run loop before sources"); 99 | break; 100 | //Inside the event processing loop before the run loop sleeps, waiting for a source or timer to fire. 101 | //This activity does not occur if CFRunLoopRunInMode is called with a timeout of 0 seconds. 102 | //It also does not occur in a particular iteration of the event processing loop if a version 0 source fires 103 | case kCFRunLoopBeforeWaiting:{ 104 | _waitStartTime = 0; 105 | NSLog(@"run loop before waiting"); 106 | break; 107 | } 108 | //Inside the event processing loop after the run loop wakes up, but before processing the event that woke it up. 109 | //This activity occurs only if the run loop did in fact go to sleep during the current loop 110 | case kCFRunLoopAfterWaiting:{ 111 | _waitStartTime = [[NSDate date] timeIntervalSince1970]; 112 | NSLog(@"run loop after waiting"); 113 | break; 114 | } 115 | //The exit of the run loop, after exiting the event processing loop. 116 | //This activity occurs once for each call to CFRunLoopRun and CFRunLoopRunInMode 117 | case kCFRunLoopExit: 118 | NSLog(@"run loop exit"); 119 | break; 120 | /* 121 | A combination of all the preceding stages 122 | case kCFRunLoopAllActivities: 123 | break; 124 | */ 125 | default: 126 | break; 127 | } 128 | } 129 | 130 | #pragma mark addSecondaryThreadAndObserver 131 | - (void) addSecondaryThreadAndObserver{ 132 | NSThread *thread = [self secondaryThread]; 133 | [self performSelector:@selector(addSecondaryTimer) onThread:thread withObject:nil waitUntilDone:YES]; 134 | } 135 | 136 | - (NSThread *)secondaryThread { 137 | static NSThread *_secondaryThread = nil; 138 | static dispatch_once_t oncePredicate; 139 | dispatch_once(&oncePredicate, ^{ 140 | _secondaryThread = 141 | [[NSThread alloc] initWithTarget:self 142 | selector:@selector(networkRequestThreadEntryPoint:) 143 | object:nil]; 144 | [_secondaryThread start]; 145 | }); 146 | return _secondaryThread; 147 | } 148 | 149 | - (void)networkRequestThreadEntryPoint:(id)__unused object { 150 | @autoreleasepool { 151 | [[NSThread currentThread] setName:@"monitorControllerThread"]; 152 | NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; 153 | [runLoop addPort:[NSMachPort port] forMode:NSRunLoopCommonModes]; 154 | [runLoop run]; 155 | } 156 | } 157 | 158 | - (void) addSecondaryTimer{ 159 | NSTimer *myTimer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; 160 | [[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode]; 161 | } 162 | 163 | - (void)timerFired:(NSTimer *)timer{ 164 | if ( _waitStartTime < 1 ){ 165 | return; 166 | } 167 | double currentTime = [[NSDate date] timeIntervalSince1970]; 168 | double timeDiff = currentTime - _waitStartTime; 169 | if (timeDiff > 2.0){ 170 | if (_lastRecordTime - _waitStartTime < 0.001 && _lastRecordTime != 0){ 171 | NSLog(@"last time no :%f %f",timeDiff, _waitStartTime); 172 | return; 173 | } 174 | [self logStack]; 175 | _lastRecordTime = _waitStartTime; 176 | } 177 | } 178 | 179 | - (void)logStack{ 180 | void* callstack[128]; 181 | int frames = backtrace(callstack, 128); 182 | char **strs = backtrace_symbols(callstack, frames); 183 | int i; 184 | _backtrace = [NSMutableArray arrayWithCapacity:frames]; 185 | for ( i = 0 ; i < frames ; i++ ){ 186 | [_backtrace addObject:[NSString stringWithUTF8String:strs[i]]]; 187 | } 188 | free(strs); 189 | } 190 | 191 | @end 192 | 193 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo/SeMonitorController/SeMonitorController.h: -------------------------------------------------------------------------------- 1 | // 2 | // SeMonitorController.h 3 | // RunloopMonitorDemo 4 | // 5 | // Created by game3108 on 16/4/14. 6 | // Copyright © 2016年 game3108. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SeMonitorController : NSObject 12 | + (instancetype) sharedInstance; 13 | - (void) startMonitor; 14 | - (void) endMonitor; 15 | - (void) printLogTrace; 16 | @end 17 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo/SeMonitorController/SeMonitorController.m: -------------------------------------------------------------------------------- 1 | // 2 | // SeMonitorController.m 3 | // RunloopMonitorDemo 4 | // 5 | // Created by game3108 on 16/4/14. 6 | // Copyright © 2016年 game3108. All rights reserved. 7 | // 8 | 9 | #import "SeMonitorController.h" 10 | #import 11 | #import 12 | 13 | @interface SeMonitorController(){ 14 | CFRunLoopObserverRef _observer; 15 | dispatch_semaphore_t _semaphore; 16 | CFRunLoopActivity _activity; 17 | NSInteger _countTime; 18 | NSMutableArray *_backtrace; 19 | } 20 | 21 | @end 22 | 23 | @implementation SeMonitorController 24 | 25 | + (instancetype) sharedInstance{ 26 | static dispatch_once_t once; 27 | static id sharedInstance; 28 | dispatch_once(&once, ^{ 29 | sharedInstance = [[self alloc] init]; 30 | }); 31 | return sharedInstance; 32 | } 33 | 34 | - (void) startMonitor{ 35 | [self registerObserver]; 36 | } 37 | 38 | - (void) endMonitor{ 39 | if (!_observer) { 40 | return; 41 | } 42 | CFRunLoopRemoveObserver(CFRunLoopGetMain(), _observer, kCFRunLoopCommonModes); 43 | CFRelease(_observer); 44 | _observer = NULL; 45 | } 46 | 47 | - (void) printLogTrace{ 48 | NSLog(@"====================堆栈\n %@ \n",_backtrace); 49 | } 50 | 51 | static void runLoopObserverCallBack(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info) 52 | { 53 | SeMonitorController *instrance = [SeMonitorController sharedInstance]; 54 | instrance->_activity = activity; 55 | // 发送信号 56 | dispatch_semaphore_t semaphore = instrance->_semaphore; 57 | dispatch_semaphore_signal(semaphore); 58 | } 59 | 60 | - (void)registerObserver 61 | { 62 | CFRunLoopObserverContext context = {0,(__bridge void*)self,NULL,NULL}; 63 | _observer = CFRunLoopObserverCreate(kCFAllocatorDefault, 64 | kCFRunLoopAllActivities, 65 | YES, 66 | 0, 67 | &runLoopObserverCallBack, 68 | &context); 69 | CFRunLoopAddObserver(CFRunLoopGetMain(), _observer, kCFRunLoopCommonModes); 70 | 71 | // 创建信号 72 | _semaphore = dispatch_semaphore_create(0); 73 | 74 | // 在子线程监控时长 75 | dispatch_async(dispatch_get_global_queue(0, 0), ^{ 76 | while (YES) 77 | { 78 | // 假定连续5次超时50ms认为卡顿(当然也包含了单次超时250ms) 79 | long st = dispatch_semaphore_wait(_semaphore, dispatch_time(DISPATCH_TIME_NOW, 50*NSEC_PER_MSEC)); 80 | if (st != 0) 81 | { 82 | if (_activity==kCFRunLoopBeforeSources || _activity==kCFRunLoopAfterWaiting) 83 | { 84 | if (++_countTime < 5) 85 | continue; 86 | [self logStack]; 87 | NSLog(@"something lag"); 88 | } 89 | } 90 | _countTime = 0; 91 | } 92 | }); 93 | } 94 | 95 | - (void)logStack{ 96 | void* callstack[128]; 97 | int frames = backtrace(callstack, 128); 98 | char **strs = backtrace_symbols(callstack, frames); 99 | int i; 100 | _backtrace = [NSMutableArray arrayWithCapacity:frames]; 101 | for ( i = 0 ; i < frames ; i++ ){ 102 | [_backtrace addObject:[NSString stringWithUTF8String:strs[i]]]; 103 | } 104 | free(strs); 105 | } 106 | 107 | @end 108 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // RunloopMonitorDemo 4 | // 5 | // Created by game3108 on 16/4/13. 6 | // Copyright © 2016年 game3108. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // RunloopMonitorDemo 4 | // 5 | // Created by game3108 on 16/4/13. 6 | // Copyright © 2016年 game3108. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "MonitorController.h" 11 | #import "SeMonitorController.h" 12 | 13 | @interface ViewController () 14 | 15 | @end 16 | 17 | @implementation ViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | 22 | self.view.backgroundColor = [UIColor whiteColor]; 23 | 24 | [[SeMonitorController sharedInstance] startMonitor]; 25 | 26 | UIButton *longTimeButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 50, 100, 100)]; 27 | longTimeButton.backgroundColor = [UIColor blackColor]; 28 | [longTimeButton addTarget:self action:@selector(runLongTime) forControlEvents:UIControlEventTouchUpInside]; 29 | [self.view addSubview:longTimeButton]; 30 | 31 | UIButton *printLogButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 100)]; 32 | printLogButton.backgroundColor = [UIColor grayColor]; 33 | [printLogButton addTarget:self action:@selector(printLog) forControlEvents:UIControlEventTouchUpInside]; 34 | [self.view addSubview:printLogButton]; 35 | } 36 | 37 | - (void)runLongTime{ 38 | for ( int i = 0 ; i < 10000 ; i ++ ){ 39 | NSLog(@"%d",i); 40 | } 41 | } 42 | 43 | - (void)printLog{ 44 | [[SeMonitorController sharedInstance] printLogTrace]; 45 | } 46 | 47 | - (void)didReceiveMemoryWarning { 48 | [super didReceiveMemoryWarning]; 49 | // Dispose of any resources that can be recreated. 50 | } 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // RunloopMonitorDemo 4 | // 5 | // Created by game3108 on 16/4/13. 6 | // Copyright © 2016年 game3108. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemoTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemoTests/RunloopMonitorDemoTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // RunloopMonitorDemoTests.m 3 | // RunloopMonitorDemoTests 4 | // 5 | // Created by game3108 on 16/4/13. 6 | // Copyright © 2016年 game3108. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface RunloopMonitorDemoTests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation RunloopMonitorDemoTests 16 | 17 | - (void)setUp { 18 | [super setUp]; 19 | // Put setup code here. This method is called before the invocation of each test method in the class. 20 | } 21 | 22 | - (void)tearDown { 23 | // Put teardown code here. This method is called after the invocation of each test method in the class. 24 | [super tearDown]; 25 | } 26 | 27 | - (void)testExample { 28 | // This is an example of a functional test case. 29 | // Use XCTAssert and related functions to verify your tests produce the correct results. 30 | } 31 | 32 | - (void)testPerformanceExample { 33 | // This is an example of a performance test case. 34 | [self measureBlock:^{ 35 | // Put the code you want to measure the time of here. 36 | }]; 37 | } 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemoUITests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /RunloopMonitorDemo/RunloopMonitorDemoUITests/RunloopMonitorDemoUITests.m: -------------------------------------------------------------------------------- 1 | // 2 | // RunloopMonitorDemoUITests.m 3 | // RunloopMonitorDemoUITests 4 | // 5 | // Created by game3108 on 16/4/13. 6 | // Copyright © 2016年 game3108. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface RunloopMonitorDemoUITests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation RunloopMonitorDemoUITests 16 | 17 | - (void)setUp { 18 | [super setUp]; 19 | 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | 22 | // In UI tests it is usually best to stop immediately when a failure occurs. 23 | self.continueAfterFailure = NO; 24 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. 25 | [[[XCUIApplication alloc] init] launch]; 26 | 27 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. 28 | } 29 | 30 | - (void)tearDown { 31 | // Put teardown code here. This method is called after the invocation of each test method in the class. 32 | [super tearDown]; 33 | } 34 | 35 | - (void)testExample { 36 | // Use recording to get started writing UI tests. 37 | // Use XCTAssert and related functions to verify your tests produce the correct results. 38 | } 39 | 40 | @end 41 | --------------------------------------------------------------------------------