1. Trang chủ
  2. » Công Nghệ Thông Tin

Lập trình Wrox Professional Xcode 3 cho Mac OS part 68 doc

24 118 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 24
Dung lượng 8,32 MB

Nội dung

Instruments ❘ 525 Learn More About Shark This introduction to Shark only scratches its surface. Full documentation for Shark is available within the application itself in the Help menu. There is a user ’ s guide, profi ling and data mining tutorials, as well as interactive processor instruction references. Shark can analyze multiple processes and applications that are no longer responding. You can start and stop its sampling from within your application, the keyboard, or using command - line tools. It can debug memory bandwidth issues, processor cache hits, and virtual memory paging. Read the documentation for a complete description of all of these features. If you still have questions, consider joining the Performance Analysis and Optimization mailing list (PerfOptimization - dev) at http://lists.apple.com/ . INSTRUMENTS Instruments is an application for doing performance analysis. Instruments replaces a hodgepodge collection of older utilities (Sampler, Big Top, Spin Control, ObjectAlloc, Thread Viewer, and others) with a uniform, fl exible, and coordinated set of modular testing tools. Using Instruments, you can quickly assemble a custom set of analysis modules that will target, and hopefully illuminate, exactly the problem you ’ re trying to solve. To use Instruments, you assemble a deck of customized data collection modules (called instruments ) that gather information about your application, the entire system, or both. The arrangement, as shown in Figure 19 - 8, comes out looking something like Garage Band. Each track records a different aspect of your application or the system. You can examine the details of an individual track, correlate different tracks, replay your application ’ s performance, compare the performance of different runs, and much more. FIGURE 19-8 c19.indd 525c19.indd 525 1/21/10 4:17:22 PM1/21/10 4:17:22 PM Download at getcoolebook.com 526 ❘ CHAPTER 19 PERFORMANCE ANALYSIS The Instruments User Guide is quite good. You can fi nd it by searching for “ Instruments ” in the Xcode documentation or online at http://developer.apple.com/mac/library/documentation/ DeveloperTools/Conceptual/InstrumentsUserGuide/ . The user guide, however, focuses mostly on what Instruments can do and how to use it as a standalone application. This chapter focuses more on how to use Instruments in an Xcode workfl ow, and demonstrates using Instruments to solve some typical development problems. The two are complementary, and I encourage you to read both. Terminology The following table defi nes the vocabulary used by Instruments: TERM DEFINITION Instruments The Instruments performance analysis application and testing suite instrument An individual data collection module track The data set collected by a single instrument Trace Document A document containing a set of instruments and the data they collected Template A document containing a confi guration of instruments playhead A position control that shows/selects a common point in time Recording Trace Data Running your application under the watchful gaze of Instruments is very simple: 1. Build your application (Command+B). 2. Choose an Instruments template from the Run ➪ Run With Performance Tool menu. The fi rst step is important. None of the commands that start performance analysis builds your project fi rst, so if you ’ ve made changes and want to test those changes, build your project fi rst. Xcode launches Instruments in the correct mode for your target application, confi gures it using the selected template, and starts your application running. Not all Instruments templates are available for every target. For example, iPhone OS 3.0 does not provide garbage collection, so the GC Monitor template is grayed out for iPhone apps. Here ’ s a list of the more popular Xcode - supplied templates and the kinds of issues you would use them to analyze: XCODE INSTRUMENTS TEMPLATE INSTRUMENTS USE Object Allocations ObjectAlloc, VM Tracker Look for Objective - C memory leaks, possibly ones that cause excessive VM memory usage. Leaks ObjectAlloc, Leaks Look for object, and non - object, memory leaks. c19.indd 526c19.indd 526 1/21/10 4:17:23 PM1/21/10 4:17:23 PM Download at getcoolebook.com Instruments ❘ 527 XCODE INSTRUMENTS TEMPLATE INSTRUMENTS USE Zombies ObjectAlloc with Zombies detection Find under - retained/over - released Objective - C objects. Time Profi ler Time Profi ler Analyze code performance, much like Shark. CPU Sampler Sampler, CPU Monitor Compare application CPU utilization with overall system CPU utilization. Threads Tread State Examine thread creation, switching, and state history. Multicore Thread State, Dispatch Examine thread states and Grand Central Dispatch queues looking for CPU utilization, bottlenecks, and stalls. GC Monitor Object Graph, ObjectAlloc, Garbage Collection Look at the high - level picture of object allocation, lifespan, and garbage collection activity. File Activity File Activity, Reads/ Writes, File Attributes, Directory I/O A combination of instruments that monitor all high - level fi le activity. Use it to look for fi le access problems, ordering, or ine cient I/O. Core Data Core Data Fetches, Core Data Cache Misses, Core Data Saves Track down poor Core Data performance looking for excessive fetches, poor caching, or slow saves. Instrument collection continues as long as the application is running. Quitting your application, or pressing the Stop button in Instruments, terminates data collection — and your application. Instruments ’ interface is live. You can browse, dig into, and analyze data as it’s being collected. When you ’ re done, either leave the trace document window open, save it, or close as you wish. Be careful of trying to do too much in Instruments while simultaneously gathering instrument data that involves system resources, like the CPU Sampler or Multicore templates. Instruments is, itself, using CPU, memory, and fi le system services to do its job. This can skew the results of your application ’ s performance. c19.indd 527c19.indd 527 1/21/10 4:17:23 PM1/21/10 4:17:23 PM Download at getcoolebook.com 528 ❘ CHAPTER 19 PERFORMANCE ANALYSIS Accumulating and Discarding Traces After terminating your test application, leave the trace document open and do one of two things: Press the Record button to restart the application. Start the application from Xcode again, using the same Instruments template. Instruments will, again, start your application and begin recording. The recording creates a second data set, called a run , in the trace document. Every time you restart your application in the same trace document, Instruments accumulates another run. Figure 19 - 9 shows four instruments with three runs of accumulated data. ➤ ➤ Expand Runs Select Current Run FIGURE 19-9 You have two ways of browsing runs: Expand an instrument Choose a run in the time control Expand an instrument to examine and compare the results of individual runs simultaneously. The current run is always shown on the instrument ’ s main track. You can also page through the runs using the time control in the toolbar. Click the arrows to select the current run. This run becomes the data that appears in the main track of every instrument. Use the run selector to review the entire results of a previous run. ➤ ➤ c19.indd 528c19.indd 528 1/21/10 4:17:26 PM1/21/10 4:17:26 PM Download at getcoolebook.com Instruments ❘ 529 Run data can consume a lot of fi le, memory, and screen space. If you have a run that you don ’ t fi nd interesting anymore, make it the current run using the time control and then choose Instrument ➪ Delete Run N. Instruments will delete the n th run of samples from all instruments. The Run Browser Another way to review multiple runs of analysis data is to use the run browser, shown in Figure 19 - 10. FIGURE 19-10 The run browser presents a cover fl ow style display of each run of instruments, along with some summary information. Here you can make comments about each run, delete a run, or click Promote Run to switch immediately to that run in the trace document window — equivalent to using the run selector. The return button, at the lower right, returns you to the trace document window. Analyzing Trace Data The data gathered from each instrument is organized into panes of successively more detailed information. You start by perusing the tracks, which compactly summarize the data gathered by that instrument. To examine the data of a single instrument in more detail, select the track and expand the details pane. The details pane typically provides numerous fi lter, analysis, and exploration tools. The details of individual samples can be explored even further in the extended details pane. Each of these is described in the following sections. c19.indd 529c19.indd 529 1/21/10 4:17:29 PM1/21/10 4:17:29 PM Download at getcoolebook.com 530 ❘ CHAPTER 19 PERFORMANCE ANALYSIS Tracks Tracks present a graphical summary of the trace data for each instrument. What information is displayed, and how, is controlled by the inspector tab of the instrument. You get to the inspector tab by clicking the small i button at the right edge of each instrument, or by choosing File ➪ Get Info, as shown on the left in Figure 19 - 11. Playhead FIGURE 19-11 The inspector for each instrument varies, but in general you can choose the target (normally all instruments target the same process, but that ’ s not a requirement), various instrument - specifi c settings, and the format of the track ’ s graph. The Style and Type controls determine what metric is displayed in the track and how it ’ s drawn. The Zoom control increases or decreases the vertical size of the track, setting the resolution of the graph. You can also zoom the scale of the selected track using the View ➪ Increase Deck Size (Command++) and View ➪ Decrease Deck Size (Command+ − ) commands. The Zoom control at the bottom of the window determines the horizontal magnifi cation of all of the tracks. Use the two zoom controls to zoom in on details, or zoom out to look for overall trends. You can peek at individual data points in the graph using the playhead control, as shown on the right in Figure 19 - 11. When you drag the playhead control across the track, datatips show the values of the data points in each track at that specifi c time. Depending on the horizontal zoom, a single pixel might represent several samples. You may have to zoom in to review discrete data points. More Detail After you ’ ve narrowed down the region of sample data you want to explore, it ’ s time to use the details pane that appears at the bottom of the trace document window, as shown in Figure 19 - 12. The details pane is normally already visible by default, but you can collapse and expand it using the details button in the lower - left corner of the window, or by choosing the View ➪ Detail (Command+D) command. c19.indd 530c19.indd 530 1/21/10 4:17:29 PM1/21/10 4:17:29 PM Download at getcoolebook.com Instruments ❘ 531 Each instrument has its own details pane format, but generally it displays a tabular view of the instrument ’ s individual samples and a structured view of its analysis. It may alternatively present graphs, other organizations of the same data, console messages, or even the source code associated with the samples. Each view is selected using the icons at the bottom of the details pane. If you ’ re trying to fi nd something specifi c, enter a search term in the search fi eld. The term might be a symbol name, address, or library name, depending on the type of information displayed. You can usually narrow the scope of the search using the search fi eld ’ s menu. Even More Detail When the detail in the details pane isn ’ t enough, you may need to open up the extended details pane either by clicking the extended details view button to the right of the other detail views, or by choosing the View ➪ Extended Detail (Command+E) command from the menu. The extended detail pane appears on the right side of the trace document window, as shown in Figure 19 - 13. FIGURE 19-12 FIGURE 19-13 c19.indd 531c19.indd 531 1/21/10 4:17:32 PM1/21/10 4:17:32 PM Download at getcoolebook.com 532 ❘ CHAPTER 19 PERFORMANCE ANALYSIS The extended details pane is about as close as you ’ re going to get to the raw data collected by the instrument. Samples may include a variety of information, depending on the instrument and how it ’ s confi gured. Some instruments don ’ t show any extended detail at all. Three types of extended data are very common: EXTENDED DATA TYPE DESCRIPTION Description A textual summary of the sample. This is where you ’ ll fi nd most of the simple properties of each sample. This might include the address of the object, the fi le system function called, the size of the memory allocation, the entity name that caused the cache miss, and so on. Stack Trace Many instruments capture a stack trace each time they take a sample. The stack trace group shows state of the call stack when that sample was taken. Timestamp Almost every sample includes a timestamp, which is shown in the timestamp group. Right/Control+clicking the stack trace presents a confi guration menu, shown on the right in Figure 19 - 13, where you can : Change the order in which stack frames are listed Color the functions by library Choose how much information about each stack frame function is displayed Jump to the API documentation for a function Timeline The timeline, shown in Figure 19 - 14, shows the temporal relationship of events in the tracks. You can use the timeline to : Peek at data points in a track Jump to samples at a particular time Mark locations with a fl ag Navigate between fl ags View and analyze a subset of the trace data ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ c19.indd 532c19.indd 532 1/21/10 4:17:41 PM1/21/10 4:17:41 PM Download at getcoolebook.com Instruments ❘ 533 Peeking and Navigating Using the Playhead You ’ ve already seen how you can peek at data points in the track by dragging the playhead to a time. In detail views that list data points by sample/time, the playhead and the detail list are usually synchronized; dragging the playhead to a time selects that sample in the details pane, and selecting a sample in the details pane moves the playhead to that location in the timeline. Using Flags You can mark interesting locations in the timeline using fl ags. Flags are set and removed at the current playhead location, and you can jump the playhead to a previously set fl ag. You can do this using the fl ag controls in the toolbar, or with the following commands: Edit ➪ Add Flag (Command - ↓ ) Edit ➪ Remove Flag (Command - ↑ ) View ➪ Next Flag (Command - → ) View ➪ Previous Flag (Command - ← ) View ➪ Inspect Flag The Next and Previous commands jump the playhead to the next or previous fl ag in the timeline. The Inspect Flag command opens an inspector panel that allows you to name the fl ag and record comments for future reference, as shown in Figure 19 - 15. ➤ ➤ ➤ ➤ ➤ Inspection Range Playhead Flags FIGURE 19-14 FIGURE 19-15 c19.indd 533c19.indd 533 1/21/10 4:17:41 PM1/21/10 4:17:41 PM Download at getcoolebook.com 534 ❘ CHAPTER 19 PERFORMANCE ANALYSIS Setting the Inspection Range Normally, instruments present and analyze all of the data in a run. If you ’ re interested in only a portion of that data — specifi cally, just the samples between two events — you can set the inspection range of the instruments using the Inspection Range controls in the toolbar. To set an inspection range: 1. Move the playhead to the fi rst sample to include in the analysis. 2. Click the left button in the Inspection Range control. 3. Move the playhead to the last sample to include. 4. Click the right button in the Inspection Range control. All of the details and analysis performed by all instruments will now be constrained to just the data collected between those two time periods, also shown in Figure 19 - 14. To reset the inspection range to include all samples again, click the center button in the Inspection Range control. Sample numbers are relative. That is, sample 0 is the fi rst sample in its data set. When you change the inspection range, you change the sample data set and sample 0 will be the fi rst sample in the new range. If you ’ re using the sample number to identify a data point, remember that its number will change if you alter the inspection range. Also note that while sample numbers change, time stamps don ’ t. Customizing Instruments Instruments can be customized in a myriad of ways. Most instruments are customized through their inspector pane, which you ’ ve already seen. Other analysis and view settings are usually found in the details pane containing the instrument ’ s data. When you edit instrument or display settings, those settings are part of the trace document. Settings are preserved when you save a trace document or continue to use a modifi ed trace document for future runs. Using Instruments via Xcode adds a wrinkle to changing instrument settings. When you launch Instruments from Xcode you do so by selecting a template. Normally, this creates a new trace document and initiates a recording. However, if Instruments already has a trace document open and that document was created with the same template selected in Xcode, Instruments will reuse the open document and accumulate another run. Thus, as long as you continue to select the same Instruments template in Xcode, Instruments will continue adding runs to the same trace document. Choose a different template and Instruments will create a new document — bypassing any changes you ’ ve made to instrument settings. This has implications if you customize your instruments or trace document after starting Instruments for the fi rst time. To customize instruments in an Xcode workfl ow, use one of the following workfl ows: c19.indd 534c19.indd 534 1/21/10 4:17:44 PM1/21/10 4:17:44 PM Download at getcoolebook.com [...]... trace document with the instruments and settings you want Choose the File ➪ Save As Template command and choose a location, description, and icon, as shown in Figure 19-17 Opening that template document creates a new trace document with your preconfigured set of instruments Download at getcoolebook.com c19.indd 536 1/21/10 4:17:48 PM Instruments ❘ 537 FIGURE 19-17 To add your customized template to Xcode, ... normally would 3 4 Launch Instruments, choose a template, and create a trace document 5 After a few moments, a second device appears in the menu — representing the second remote instruments server — as shown on the right in Figure 19-20 Choose this device as the target 6 7 Disconnect the USB cable, connecting whatever accessory you want to test While holding down the Option key, choose the Default... unresponsive You certainly don’t want to have to stop it, start it again in Xcode, and spend an hour reproducing the problem Instead, launch Instruments, configure some instruments, and choose your running application from the Attach to Process menu Once attached, choose File ➪ Record Trace (Command+R) to begin recording The other option is to choose Instrument Specific That setting enables the Target setting in... selected based on the position of the cursor, you can use it to start sampling multiple targets simultaneously Mini Mode Like the mini-debugger, Instruments has a mini mode too Switch to mini mode by choosing the View ➪ Mini Instruments command All open trace documents are reduced to a compact status display, as shown in Figure 19-19 FIGURE 19-19 Close the mini instruments window, or choose the command again,...Instruments ❘ 535 ➤ Start Instruments using an Xcode template Customize the instruments and leave the trace document open Start Instruments again from Xcode using the same template Instruments will reuse the already open trace document and accumulate another run using the customized settings ➤ Start Instruments using an Xcode template Customize the instruments and leave the trace document open Start... to a template, you can sample a running process using these steps: 1 2 Position the cursor over a window belonging to the target process This is important Press the quick start key combination for the template you want to use Download at getcoolebook.com c19.indd 538 1/21/10 4:17:52 PM Instruments 3 ❘ 539 To stop sampling, either position the cursor over a window belonging to the same process and press... how Instruments comes configured when you start it from Xcode From the Default Target control in the toolbar you can change the setting from Launch Executable to either Attach to Process or Instrument Specific, as shown in Figure 19-18 Download at getcoolebook.com c19.indd 537 1/21/10 4:17:51 PM 538 ❘ CHAPTER 19 PERFORMANCE ANALYSIS FIGURE 19-18 Choose Attach to Process when the process you want to analyze... Instruments from Xcode, then I may stay in Instruments, refi ning its settings and my tests, until I’ve found my problem, ultimately returning to Xcode to address it Here are a few ways in which Instruments can be used on its own and why you might want to do so Choosing the Target Process Each instrument has a target that describes the source process, or processes, that it’s going to sample Most instruments... change to Enable Device-Name — Wireless Choose the command and Instruments will start a second instruments server using the wireless network for communications Start the program and sample it using Instruments Download at getcoolebook.com c19.indd 539 1/21/10 4:17:52 PM 540 ❘ CHAPTER 19 PERFORMANCE ANALYSIS FIGURE 19-20 Typical Scenarios The following four scenarios are typical problems during application... times so that my sampling tools can get a statistically accurate picture of their performance Because this is purely a code performance analysis, I choose to do it in Shark I begin by following these steps: 1 2 3 4 Build the command-line test program Choose Run ➪ Run with Performance Tool ➪ Shark Click Start to start the test program running Let Shark sample the test program until it stops (about 8 . 19-12 FIGURE 19- 13 c19.indd 531 c19.indd 531 1/21/10 4:17 :32 PM1/21/10 4:17 :32 PM Download at getcoolebook.com 532 ❘ CHAPTER 19 PERFORMANCE ANALYSIS The extended details pane is about as close as you. settings, those settings are part of the trace document. Settings are preserved when you save a trace document or continue to use a modifi ed trace document for future runs. Using Instruments via Xcode. your application as you normally would. 3. Launch Instruments, choose a template, and create a trace document. 4. While holding down the Option key, choose the Default Target control, as shown

Ngày đăng: 04/07/2014, 06:20