-
Notifications
You must be signed in to change notification settings - Fork 149
/
Friday Session 3. Instruments.srt
executable file
·3138 lines (2504 loc) · 64.5 KB
/
Friday Session 3. Instruments.srt
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1
00:00:00,401 --> 00:00:04,769
[MUSIC]
2
00:00:04,838 --> 00:00:09,307
Stanford University.
>> All right welcome class,
3
00:00:09,376 --> 00:00:12,244
everybody, to iPhone
section about instruments.
4
00:00:12,313 --> 00:00:14,212
We're gonna learn today about
instruments which is a tool
5
00:00:14,281 --> 00:00:17,382
that helps you with figuring
how to profile your code, and
6
00:00:17,451 --> 00:00:20,185
profiling means a few
different things. So we'll get
7
00:00:20,254 --> 00:00:22,387
started with a little bit of
background on what it means to
8
00:00:22,455 --> 00:00:24,422
even profile code in the first
place cuz that may not be
9
00:00:24,491 --> 00:00:27,726
something you've done before.
Even outside of Xcode in
10
00:00:27,794 --> 00:00:31,964
general with your programming
career. So, I want you to
11
00:00:32,032 --> 00:00:34,833
rewind back to kinda when
people were building OS' back
12
00:00:34,902 --> 00:00:37,702
in the day and people had all
kinds of questions that they
13
00:00:37,771 --> 00:00:40,806
wanted to ask about their code
as it was running in their OS.
14
00:00:40,874 --> 00:00:43,575
And those questions would
range across things from like
15
00:00:43,644 --> 00:00:46,210
the hard disk to main memory
to the CPU and just to try to
16
00:00:46,279 --> 00:00:49,280
figure out like how the OS was
allocating various resources.
17
00:00:49,349 --> 00:00:51,649
For the disk you might
ask something like, what,
18
00:00:51,718 --> 00:00:54,486
what processes are asking for
a given resource right now and
19
00:00:54,555 --> 00:00:56,989
which command requested that
resource from the OS so
20
00:00:57,057 --> 00:00:59,291
you could figure out whether
there are some bug or
21
00:00:59,360 --> 00:01:00,325
what was going on.
22
00:01:00,394 --> 00:01:02,661
You might have a problem in
memory like there's a leak or
23
00:01:02,730 --> 00:01:03,661
something like that and
24
00:01:03,730 --> 00:01:05,931
you wanna figure out how you
can trace down that leak.
25
00:01:05,999 --> 00:01:08,634
And then very importantly you
might wanna figure out how
26
00:01:08,702 --> 00:01:11,202
long certain processes are
taking on the CPU compared to
27
00:01:11,271 --> 00:01:13,972
other processes or how long a
process is taking in general.
28
00:01:14,040 --> 00:01:16,542
Or, when it spikes to 100%
usage or something like
29
00:01:16,610 --> 00:01:19,143
that so, these are all
really important questions.
30
00:01:19,212 --> 00:01:21,179
But, in order to figure it
out, you'd have to go in and
31
00:01:21,247 --> 00:01:23,915
manually write a bunch of
code, and that's not very fun.
32
00:01:23,984 --> 00:01:26,385
So if somebody came along
around the early 2000s, and
33
00:01:26,454 --> 00:01:29,054
said hey, there must
be a better way. And
34
00:01:29,122 --> 00:01:30,956
so I believe at
Sun Microsystems,
35
00:01:31,024 --> 00:01:33,958
they created this thing called
DTrace, which is a little
36
00:01:34,027 --> 00:01:36,962
lightweight scripting language
that looks kind of like C, so
37
00:01:37,030 --> 00:01:39,398
it fit right in with your
operating systems code.
38
00:01:39,466 --> 00:01:42,233
And DTrace would do is like
you would place this little
39
00:01:42,302 --> 00:01:45,369
things called probes which are
little of code inside your,
40
00:01:45,438 --> 00:01:46,872
inside your bigger system.
41
00:01:46,940 --> 00:01:49,507
And then they would trigger
output about how long it was
42
00:01:49,576 --> 00:01:52,110
taking to run various things
and you could figure how
43
00:01:52,179 --> 00:01:54,913
much memory was being used by
given resource and what not.
44
00:01:54,982 --> 00:01:57,249
And what is great is really,
really expressive which meant
45
00:01:57,318 --> 00:01:59,350
that you could do a lot of
different things with it,
46
00:01:59,419 --> 00:02:01,386
by writing very little code
and you could deploy it to
47
00:02:01,455 --> 00:02:03,422
production no problem because
when it was running in
48
00:02:03,490 --> 00:02:05,423
production mode. It wouldn't
take any hit on the CPU, so
49
00:02:05,492 --> 00:02:07,191
you can just leave
your code in there,
50
00:02:07,260 --> 00:02:10,595
in the live production system.
So, that's a super great step
51
00:02:10,664 --> 00:02:13,265
in the right direction, but I
had a little bit of a problem,
52
00:02:14,601 --> 00:02:16,435
which is that,
for example here,
53
00:02:16,503 --> 00:02:18,703
when you see this one that
compares process times,
54
00:02:18,772 --> 00:02:20,939
you see this one in
the center here is,
55
00:02:21,008 --> 00:02:25,043
is outputting a distributing
of processes as Atsy text. So
56
00:02:25,111 --> 00:02:27,178
it's showing you these little
at signs to say that that,
57
00:02:27,247 --> 00:02:29,447
that process in the center is
taking up half of the overall
58
00:02:29,516 --> 00:02:31,215
time and the other ones
are taking up the rest,
59
00:02:31,284 --> 00:02:32,884
it's not the best GUI. So
60
00:02:32,953 --> 00:02:37,823
you end up with this non-ideal
situation where you kinda got
61
00:02:37,891 --> 00:02:41,826
all this console of output and
we really didn't want that
62
00:02:41,895 --> 00:02:44,595
with Apple when we were
creating, you know Xcode and
63
00:02:44,664 --> 00:02:46,765
IOS because in most cases,
64
00:02:46,833 --> 00:02:48,467
people who are using it
are maybe programming for
65
00:02:48,535 --> 00:02:50,735
the first time. They've
never made apps before and
66
00:02:50,804 --> 00:02:53,204
this is just something that
you don't wanna have to go and
67
00:02:53,273 --> 00:02:57,108
deal with. So, This is kind
of the vision in my head
68
00:02:57,177 --> 00:02:59,644
of DTrace, it's like this very
powerful machine that has way
69
00:02:59,713 --> 00:03:00,645
too many knobs and, and
70
00:03:00,714 --> 00:03:01,946
buttons to get
used to easily and
71
00:03:02,015 --> 00:03:04,649
you have to kinda learn it up
front to be able to use it.
72
00:03:04,718 --> 00:03:07,652
And the solution instead, is
to have something happier and
73
00:03:07,721 --> 00:03:09,888
friendlier like our
friend Eve here,
74
00:03:09,957 --> 00:03:12,257
or Eva,
I forget how you say it. And
75
00:03:12,326 --> 00:03:17,228
the solution is called
Instruments, Instruments,
76
00:03:17,297 --> 00:03:21,800
Instruments. And so what
Instruments is, is it's a GUI,
77
00:03:21,869 --> 00:03:24,469
and it sits on top of DTrace,
that same little programming
78
00:03:24,538 --> 00:03:27,238
language, scripting language
that we just talked about.
79
00:03:27,307 --> 00:03:30,041
And it's built right into
Xcode and so it, it kinda
80
00:03:30,109 --> 00:03:33,078
democratized this idea that
you could do CPU analysis or
81
00:03:33,147 --> 00:03:35,079
memory analysis or
disc analysis and
82
00:03:35,148 --> 00:03:39,317
things like these. And one of
the interesting differences
83
00:03:39,386 --> 00:03:43,154
between instruments and using
something like DTrace is that.
84
00:03:43,223 --> 00:03:45,557
Since it's a GUI we now
can't do all of these,
85
00:03:45,626 --> 00:03:48,427
all of the D Trace tasks that
we would wanna do within one
86
00:03:48,495 --> 00:03:51,863
single window because there're
just too many of them. So
87
00:03:51,932 --> 00:03:54,900
what instruments does instead,
is it splits off
88
00:03:54,968 --> 00:03:58,003
all of all of the thing that
you'd possibly wanna do with
89
00:03:58,071 --> 00:04:00,838
an iOS app with profiling into
lots of different flavors.
90
00:04:00,907 --> 00:04:04,509
So, you see that we have these
things like the activity
91
00:04:04,578 --> 00:04:07,145
monitor, which is, you may be
used to from your actual OS,
92
00:04:07,214 --> 00:04:10,381
which shows you how
much stuff different,
93
00:04:10,450 --> 00:04:12,784
processes are taking up,
how many different resources.
94
00:04:12,852 --> 00:04:14,353
And you might have this thing
called the time profiler,
95
00:04:14,421 --> 00:04:17,489
which tells you how long
it takes for a given
96
00:04:17,557 --> 00:04:21,025
CPU process to do a certain
task or something like that.
97
00:04:21,094 --> 00:04:22,894
But, you can't have all of
them within one window, or
98
00:04:22,963 --> 00:04:24,729
it would just be too much
clutter and too much stuff.
99
00:04:24,798 --> 00:04:27,565
So, the way an instruments
works from a high level is,
100
00:04:27,634 --> 00:04:30,902
you're gonna launch a specific
subpar, sub module like this.
101
00:04:30,971 --> 00:04:32,870
And then you're going to
focus on only that for
102
00:04:32,939 --> 00:04:34,139
the duration of your app, and
103
00:04:34,207 --> 00:04:37,242
that way you get this nice
GUI, that's not cluttered up.
104
00:04:37,310 --> 00:04:40,445
And in particular with these
instruments what we'll focus
105
00:04:40,514 --> 00:04:43,681
on today are the ones that are
most useful for probably 95%
106
00:04:43,750 --> 00:04:46,484
of the use cases that you'd
ever use instruments for
107
00:04:46,553 --> 00:04:48,820
you'll be using these
four instruments.
108
00:04:48,888 --> 00:04:50,922
So you'll notice that four of
them are blue because they
109
00:04:50,991 --> 00:04:53,658
have more to do with CPU
stuff. And then two of them
110
00:04:53,727 --> 00:04:55,694
are orange because they have
more to do with RAM stuff
111
00:04:55,762 --> 00:04:58,162
with memory. So, we'll go
through these four today
112
00:04:58,231 --> 00:05:00,999
starting with the two CPU ones
and then we'll end up talking
113
00:05:01,067 --> 00:05:04,770
about memory stuff with
allocations and leaks. And
114
00:05:04,838 --> 00:05:06,737
the one big takeaway
from today, that I want
115
00:05:06,806 --> 00:05:08,407
you to have is that
instruments is not
116
00:05:08,475 --> 00:05:10,675
some kind of like, nice to
have optional thing that
117
00:05:10,743 --> 00:05:13,511
sometimes people go off and
use if they have a problem.
118
00:05:13,580 --> 00:05:15,780
It's actually very critical,
even if you think you've
119
00:05:15,849 --> 00:05:18,116
created your app properly and
you don't have any problems or
120
00:05:18,185 --> 00:05:20,252
memory leaks or anything
like that. Going through and
121
00:05:20,320 --> 00:05:22,487
profiling it before you deploy
a real production app and
122
00:05:22,556 --> 00:05:24,856
making sure that it's using
memory properly and that it's
123
00:05:24,925 --> 00:05:26,958
not taking too much CPU time
when it shouldn't be and
124
00:05:27,027 --> 00:05:28,560
things like that
that really matters.
125
00:05:28,628 --> 00:05:30,161
So, Instruments is
something that's
126
00:05:30,230 --> 00:05:33,098
a necessary thing to learn if
you're trying to become a real
127
00:05:33,166 --> 00:05:35,400
iOS programmer. So don't
consider this as like this
128
00:05:35,468 --> 00:05:38,269
optional thing that you
may not need some day. So
129
00:05:38,338 --> 00:05:40,371
we're gonna go into right
into a demo here and
130
00:05:40,440 --> 00:05:43,207
we're gonna show a little app,
a little mobile app and it's
131
00:05:43,276 --> 00:05:46,210
gonna have a few, few issues
with it that we're gonna track
132
00:05:46,279 --> 00:05:48,379
down like little detectives
inside of Instruments and
133
00:05:48,448 --> 00:05:50,181
it'll illustrate
how those four
134
00:05:50,250 --> 00:05:54,786
flavors of Instruments work
that I was speaking about. So,
135
00:05:57,724 --> 00:06:00,791
The first thing I'll do
is I'll run this app in
136
00:06:00,860 --> 00:06:03,428
the most basic version of
Instruments and we'll kinda
137
00:06:03,497 --> 00:06:07,232
just give you a little tour of
what this app is doing. And
138
00:06:07,301 --> 00:06:10,568
the idea with Instruments
is that you go up to this
139
00:06:10,637 --> 00:06:13,238
Product menu and
you click on Profile,
140
00:06:13,306 --> 00:06:16,708
which you can access with Cmd
+ I. And then when you run
141
00:06:16,777 --> 00:06:19,511
Profile it builds a version
of your app with certain
142
00:06:19,579 --> 00:06:22,147
debugging stuff built in and
pops up in the same so
143
00:06:22,215 --> 00:06:24,950
many flavors thing that I
showed you from earlier.
144
00:06:25,018 --> 00:06:27,252
And then we'll select
the Activity Monitor,
145
00:06:27,320 --> 00:06:29,587
which just as it says
monitor CPU memory disk and
146
00:06:29,656 --> 00:06:32,657
network usage statistics for
processes and the file system.
147
00:06:32,725 --> 00:06:36,895
So just general statistics
about stuff going
148
00:06:36,964 --> 00:06:40,732
on on your system, and
in this case we click Choose.
149
00:06:40,801 --> 00:06:43,734
This is the instruments GUI
that pops up that's gonna show
150
00:06:43,803 --> 00:06:45,236
us all of our statistics.
151
00:06:45,304 --> 00:06:48,172
And in order to run your app
while recording all of these
152
00:06:48,241 --> 00:06:51,342
statistics, you click the
Record button. It's kind of
153
00:06:51,411 --> 00:06:55,413
like using iTunes or something
but for debugging information,
154
00:06:55,482 --> 00:06:58,783
and then we'll pop open
our simulator here. And
155
00:06:58,852 --> 00:07:01,686
first thing I'll do is explain
what this app is just briefly,
156
00:07:01,754 --> 00:07:04,355
the idea is that we're
number theorists here.
157
00:07:04,424 --> 00:07:07,992
And we've got some big prime
number that you see right here
158
00:07:08,061 --> 00:07:10,895
at the top, and we want to
figure out what its prime
159
00:07:10,964 --> 00:07:12,763
factors are.
So since the number's so
160
00:07:12,832 --> 00:07:15,566
big that's gonna take a lot
of CPU time to compute so
161
00:07:15,635 --> 00:07:18,903
let's see what happens when we
try to compute it. And I also
162
00:07:18,972 --> 00:07:21,272
have two options here for
how we're gonna factor this,
163
00:07:21,341 --> 00:07:22,740
one is to do it
the main thread, and
164
00:07:22,809 --> 00:07:24,876
the other one is to do it
on the background thread.
165
00:07:24,944 --> 00:07:27,479
Now the first thing we'll
experience is what Paul talked
166
00:07:27,547 --> 00:07:28,446
about in lecture this
167
00:07:28,515 --> 00:07:31,049
week which is that when you do
things on the main thread, and
168
00:07:31,118 --> 00:07:33,685
they're really CPU intensive,
you actually block the main
169
00:07:33,753 --> 00:07:37,989
thread. So when we click on
this button here, We notice
170
00:07:38,057 --> 00:07:40,358
that the UI is completely
blocked I can't click anywhere
171
00:07:40,426 --> 00:07:42,460
else, because it's off
factoring this number,
172
00:07:42,528 --> 00:07:45,129
figuring out the prime
factors on the main thread.
173
00:07:45,198 --> 00:07:47,164
You'll also notice over here,
we have a list of all
174
00:07:47,233 --> 00:07:49,200
of the processes running
on my machine right now.
175
00:07:49,269 --> 00:07:52,370
Since we're in the simulator
this is my actual Mac Book all
176
00:07:52,439 --> 00:07:54,339
of the processes and
the name of this app
177
00:07:54,408 --> 00:07:56,974
as you can see up here
is instruments demo. So
178
00:07:57,043 --> 00:08:00,011
one of the first things we can
do is instruments demo here is
179
00:08:00,080 --> 00:08:02,847
taking up a lot of
the CPU time in fact,
180
00:08:02,916 --> 00:08:05,717
Twice as much as the total
percentage, because it's off
181
00:08:05,786 --> 00:08:08,352
finding the prime factors
of this large number. And
182
00:08:08,421 --> 00:08:11,656
if we want we can even filter
to get rid of all the noise,
183
00:08:11,724 --> 00:08:14,859
by typing in the name of the
process we want to inspect.
184
00:08:14,928 --> 00:08:18,763
You'll also notice
that there's other
185
00:08:18,831 --> 00:08:20,598
information displayed here
in the activity monitor
186
00:08:20,667 --> 00:08:22,299
like the number of threads
that are currently running.
187
00:08:22,368 --> 00:08:25,970
So since I have this. Forking
on a background thread options
188
00:08:26,039 --> 00:08:28,039
down here at the bottom.
To solve this problem,
189
00:08:28,107 --> 00:08:29,707
I'll go ahead and
click on this a few times,
190
00:08:29,776 --> 00:08:33,911
fork up a bunch of threads.
And
191
00:08:33,980 --> 00:08:36,481
you can see I hover over
here inside instruments for
192
00:08:36,550 --> 00:08:39,784
seeing the number of threads
pop up. Now we have a bunch of
193
00:08:39,852 --> 00:08:42,120
different threads all going
off in the background to try
194
00:08:42,189 --> 00:08:44,288
to figure out these prime
factors all on their own.
195
00:08:44,357 --> 00:08:46,691
And then they're gonna report
back when done. And we'll see
196
00:08:46,759 --> 00:08:48,359
these number of threads
go down when they finish.
197
00:08:48,428 --> 00:08:50,494
So this just kind of gives
you a general sense of what
198
00:08:50,563 --> 00:08:52,897
instruments is like. The
activity monitor you can see,
199
00:08:52,966 --> 00:08:55,132
doesn't give you superfine
grained information.
200
00:08:55,201 --> 00:08:57,402
It kinda just gives you
the high level, like. You know
201
00:08:57,470 --> 00:08:59,537
what the name of the process
is and how much CPU time and
202
00:08:59,606 --> 00:09:02,373
the number of threads and the
memory, just basic stats, but