-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1778 lines (1502 loc) · 129 KB
/
index.html
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
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>For Fun and Logging</title>
<meta name="author" content="Jeff Ruan">
<meta name="description" content="">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="http://jeff66ruan.github.io">
<link href="/favicon.png" rel="icon">
<link href="/stylesheets/bootstrap/bootstrap.css" media="screen, projection" rel="stylesheet" type="text/css">
<link href="/stylesheets/bootstrap/responsive.css" media="screen, projection" rel="stylesheet" type="text/css">
<link href="/stylesheets/syntax/syntax.css" media="screen, projection" rel="stylesheet" type="text/css">
<style type="text/css">
body {
padding-bottom: 40px;
}
h1 {
margin-bottom: 15px;
}
img {
max-width: 100%;
}
.sharing, .meta, .pager {
margin: 20px 0px 20px 0px;
}
.page-footer p {
text-align: center;
}
</style>
<script src="/javascripts/libs/jquery.js"></script>
<script src="/javascripts/libs/modernizr-2.0.js"></script>
<script src="/javascripts/libs/bootstrap.js"></script>
<script src="/javascripts/octopress.js" type="text/javascript"></script>
<link href="/atom.xml" rel="alternate" title="For Fun and Logging" type="application/atom+xml">
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-48990941-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body >
<nav role="navigation"><div class="navbar navbar-inverse">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="/">For Fun and Logging</a>
<div class="nav-collapse">
<ul class="nav">
<li><a href="/">Blog</a></li>
<li><a href="/blog/archives">Archives</a></li>
</ul>
<ul class="nav pull-right" data-subscription="rss">
<li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
</ul>
<form class="pull-right navbar-search" action="http://google.com/search" method="get">
<fieldset role="search">
<input type="hidden" name="q" value="site:jeff66ruan.github.io" />
<input class="search-query" type="text" name="q" results="0" placeholder="Search"/>
</fieldset>
</form>
</div>
</div>
</div>
</div>
</nav>
<div class="container">
<div class="row-fluid">
<div class="span9">
<article>
<header class="page-header">
<h1 class="entry-title"><a href="/blog/2014/03/31/sigpnd-sigblk-sigign-sigcgt-in-proc-status-file/">SigPnd, ShdPnd, SigBlk, SigIgn and SigCgt Fields in Proc Information File System</a></h1>
<p class="meta">
<time datetime="2014-03-31T05:59:51-07:00" pubdate data-updated="true">Mar 31<span>st</span>, 2014</time>
</p>
</header>
<div class="entry-content"><p>The following is the field definition from Linux man proc(5).</p>
<blockquote><p>SigPnd, ShdPnd: Number of signals pending for thread and for process as a whole (see pthreads(7) and signal(7)).</p>
<p>SigBlk, SigIgn, SigCgt: Masks indicating signals being blocked, ignored, and caught (see signal(7)).</p></blockquote>
<p>But what they look like and how to interpret? Here is an output example:</p>
<blockquote><p>SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: fffffffe7ffbfeff
SigIgn: 0000000000000000
SigCgt: 0000000000000000</p></blockquote>
<p>They are displayed in hex format as 8 bytes. The right most 4 bytes represent stardard signals, the left is Linux real-time signal extension. Each bit in the 8 bytes represents one corresponding signal. If the bits index starts from zero(the right most bit in the above), The corresponding signal is represented by bit[signalValue-1]. An example is that the signal SIGHUP, whose value is 1, is represented the bit 0.</p>
<p>Take the above SigBlk as example, the first two bytes are</p>
<blockquote><p>0xfeff</p></blockquote>
<p>The binary format is</p>
<blockquote><p>1111,1110,1111,1111</p></blockquote>
<p>It means all signals from 1 to 16 are blocked except the signal 9 (SIGKILL). This is true because SIGKILL cannot be blocked or ignored.</p>
</div>
</article>
<article>
<header class="page-header">
<h1 class="entry-title"><a href="/blog/2014/03/23/oprofile-and-cache-an-experiment/">Oprofile and Memory Cache - an Experiment</a></h1>
<p class="meta">
<time datetime="2014-03-23T20:03:38-07:00" pubdate data-updated="true">Mar 23<span>rd</span>, 2014</time>
</p>
</header>
<div class="entry-content"><p>Last week, I did a small experiment of using oprofile to profile two piece of code. In this small experiment, I learned some basic ideas of using oprofile. In particular, I used oprofile to profile memory cache behaviour and the performance effect. In this experiment, I got ideas of how memory cache invalidation can affect the speed of running program. The following is the detail.</p>
<h3>The Experiment Configuration</h3>
<p>I ran the experiment in an Thinkpad T400 with a Ubuntu installation. The experiment needs to run on a multi-core processor because of profiling memory cache invalidation cost. In a single core processor, there is no such memory cache invalidation cost.</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$sudo lsb_release -a
</span><span class='line'>No LSB modules are available.
</span><span class='line'>Distributor ID: Ubuntu
</span><span class='line'>Description: Ubuntu 12.04 LTS
</span><span class='line'>Release: 12.04
</span><span class='line'>Codename: precise
</span><span class='line'>
</span><span class='line'>$ cat /proc/cpuinfo |grep "model name"
</span><span class='line'>model name : Intel(R) Core(TM)2 Duo CPU P8600 @ 2.40GHz
</span><span class='line'>model name : Intel(R) Core(TM)2 Duo CPU P8600 @ 2.40GHz</span></code></pre></td></tr></table></div></figure>
<h3>Source Code</h3>
<p>There are two pieces of programs, which are “no_alignment” and “alignment”. They are compiled with GNU GCC. Each program clones a child sharing the same memory address space, which means the parent and the child updating different fileds of the same global data. The difference is that the program “alignment” optimizing the fields of the shared_data to the cache line size alignment. In this way, the two fields are able to be fetched on different cache lines. Therefore, when the parent and the child runs on different cores, the “no_alignment” program has the cache invlidation costs between two cores, but the “alignment” program doesn’t.</p>
<p>The following are the source codes of the two programs. The only difference is the definition of struct shared_data.</p>
<figure class='code'><figcaption><span>no_alignment</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cp">#define _GNU_SOURCE</span>
</span><span class='line'><span class="cp">#include <sched.h></span>
</span><span class='line'><span class="cp">#include <stdio.h></span>
</span><span class='line'><span class="cp">#include <errno.h></span>
</span><span class='line'><span class="cp">#include <stdlib.h></span>
</span><span class='line'>
</span><span class='line'><span class="c1">// shared data</span>
</span><span class='line'><span class="k">struct</span> <span class="n">shared_data</span> <span class="p">{</span>
</span><span class='line'> <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">num_proc1</span><span class="p">;</span>
</span><span class='line'> <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">num_proc2</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="k">struct</span> <span class="n">shared_data</span> <span class="n">shared</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// define loops to run for a while</span>
</span><span class='line'><span class="kt">int</span> <span class="n">loop_i</span> <span class="o">=</span> <span class="mi">100000</span><span class="p">,</span> <span class="n">loop_j</span> <span class="o">=</span> <span class="mi">100000</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="kt">int</span> <span class="nf">child</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="cp">#define STACK_SIZE (8 * 1024)</span>
</span><span class='line'>
</span><span class='line'><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="n">pid_t</span> <span class="n">pid</span><span class="p">;</span>
</span><span class='line'> <span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="cm">/* Stack */</span>
</span><span class='line'> <span class="kt">char</span> <span class="o">*</span><span class="n">stack</span> <span class="o">=</span> <span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="n">STACK_SIZE</span><span class="p">);</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">stack</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="n">perror</span><span class="p">(</span><span class="s">"malloc"</span><span class="p">);</span>
</span><span class='line'> <span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">printf</span><span class="p">(</span><span class="s">"main: shared %p %p</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span>
</span><span class='line'> <span class="o">&</span><span class="n">shared</span><span class="p">.</span><span class="n">num_proc1</span><span class="p">,</span> <span class="o">&</span><span class="n">shared</span><span class="p">.</span><span class="n">num_proc2</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="cm">/* clone a thread sharing memory space with the parent process */</span>
</span><span class='line'> <span class="k">if</span> <span class="p">((</span><span class="n">pid</span> <span class="o">=</span> <span class="n">clone</span><span class="p">(</span><span class="n">child</span><span class="p">,</span> <span class="n">stack</span> <span class="o">+</span> <span class="n">STACK_SIZE</span><span class="p">,</span> <span class="n">CLONE_VM</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">))</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="n">perror</span><span class="p">(</span><span class="s">"clone"</span><span class="p">);</span>
</span><span class='line'> <span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">loop_i</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">for</span><span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o"><</span> <span class="n">loop_j</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="n">shared</span><span class="p">.</span><span class="n">num_proc1</span><span class="o">++</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kt">int</span> <span class="nf">child</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">arg</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="n">printf</span><span class="p">(</span><span class="s">"child: shared %p %p</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span>
</span><span class='line'> <span class="o">&</span><span class="n">shared</span><span class="p">.</span><span class="n">num_proc1</span><span class="p">,</span> <span class="o">&</span><span class="n">shared</span><span class="p">.</span><span class="n">num_proc2</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">loop_i</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o"><</span> <span class="n">loop_j</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="n">shared</span><span class="p">.</span><span class="n">num_proc2</span><span class="o">++</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span>alignment</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cp">#define _GNU_SOURCE</span>
</span><span class='line'><span class="cp">#include <sched.h></span>
</span><span class='line'><span class="cp">#include <stdio.h></span>
</span><span class='line'><span class="cp">#include <errno.h></span>
</span><span class='line'><span class="cp">#include <stdlib.h></span>
</span><span class='line'>
</span><span class='line'><span class="c1">// cache line size</span>
</span><span class='line'><span class="c1">// hardware dependent value</span>
</span><span class='line'><span class="c1">// it can checks from /proc/cpuinfo</span>
</span><span class='line'><span class="cp">#define CACHE_LINE_SIZE 64</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// shared data aligned with cache line size</span>
</span><span class='line'><span class="k">struct</span> <span class="n">shared_data</span> <span class="p">{</span>
</span><span class='line'> <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">__attribute__</span> <span class="p">((</span><span class="n">aligned</span> <span class="p">(</span><span class="n">CACHE_LINE_SIZE</span><span class="p">)))</span> <span class="n">num_proc1</span><span class="p">;</span>
</span><span class='line'> <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">__attribute__</span> <span class="p">((</span><span class="n">aligned</span> <span class="p">(</span><span class="n">CACHE_LINE_SIZE</span><span class="p">)))</span> <span class="n">num_proc2</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="k">struct</span> <span class="n">shared_data</span> <span class="n">shared</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// define loops to run for a while</span>
</span><span class='line'><span class="kt">int</span> <span class="n">loop_i</span> <span class="o">=</span> <span class="mi">100000</span><span class="p">,</span> <span class="n">loop_j</span> <span class="o">=</span> <span class="mi">100000</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="kt">int</span> <span class="nf">child</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="cp">#define STACK_SIZE (8 * 1024)</span>
</span><span class='line'>
</span><span class='line'><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="n">pid_t</span> <span class="n">pid</span><span class="p">;</span>
</span><span class='line'> <span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="cm">/* Stack */</span>
</span><span class='line'> <span class="kt">char</span> <span class="o">*</span><span class="n">stack</span> <span class="o">=</span> <span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="n">STACK_SIZE</span><span class="p">);</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">stack</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="n">perror</span><span class="p">(</span><span class="s">"malloc"</span><span class="p">);</span>
</span><span class='line'> <span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">printf</span><span class="p">(</span><span class="s">"main: shared %p %p</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span>
</span><span class='line'> <span class="o">&</span><span class="n">shared</span><span class="p">.</span><span class="n">num_proc1</span><span class="p">,</span> <span class="o">&</span><span class="n">shared</span><span class="p">.</span><span class="n">num_proc2</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="cm">/* clone a thread sharing memory space with the parent process */</span>
</span><span class='line'> <span class="k">if</span> <span class="p">((</span><span class="n">pid</span> <span class="o">=</span> <span class="n">clone</span><span class="p">(</span><span class="n">child</span><span class="p">,</span> <span class="n">stack</span> <span class="o">+</span> <span class="n">STACK_SIZE</span><span class="p">,</span> <span class="n">CLONE_VM</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">))</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="n">perror</span><span class="p">(</span><span class="s">"clone"</span><span class="p">);</span>
</span><span class='line'> <span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">loop_i</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">for</span><span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o"><</span> <span class="n">loop_j</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="n">shared</span><span class="p">.</span><span class="n">num_proc1</span><span class="o">++</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kt">int</span> <span class="nf">child</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">arg</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="n">printf</span><span class="p">(</span><span class="s">"child: shared %p %p</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span>
</span><span class='line'> <span class="o">&</span><span class="n">shared</span><span class="p">.</span><span class="n">num_proc1</span><span class="p">,</span> <span class="o">&</span><span class="n">shared</span><span class="p">.</span><span class="n">num_proc2</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">loop_i</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">for</span> <span class="p">(</span><span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o"><</span> <span class="n">loop_j</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="n">shared</span><span class="p">.</span><span class="n">num_proc2</span><span class="o">++</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<h3>Testing</h3>
<p>I was using the Oprofile 0.99 which has ‘operf’ program that allows non-root users to profile a specified individual process with less setup. Different CPUs have different supported events. The supported events, their meanings and accepted event format by operf can be checked from ophelp and the CPU’s hardware manual. In this experiment, the CLOCK event is CPU_CLK_UNHALTED and L2_LINES_IN is the number of allocated lines in L2 which is L2 cache missing number. As examples, the “CPU_CLK_UNHALTED:100000” tells operf to sample every 100000 unhalted clock with default mask(unhalted core cycles). The “L2_LINES_IN:100000:0xf0” tells operf to sample every 1000000 number of allocated lines in L2 on all cores with all prefetch inclusive.</p>
<p>The following is the experiment results.</p>
<figure class='code'><figcaption><span>profiling result of alignment</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$time</span> operf --event<span class="o">=</span>CPU_CLK_UNHALTED:100000,L2_LINES_IN:100000:0xf0 ./alignment
</span><span class='line'>
</span><span class='line'>operf: Profiler started
</span><span class='line'>main: shared 0x6010c0 0x601100
</span><span class='line'>child: shared 0x6010c0 0x601100
</span><span class='line'>
</span><span class='line'>profiled app exited with the following status: 160
</span><span class='line'>
</span><span class='line'>Profiling <span class="k">done</span>.
</span><span class='line'>
</span><span class='line'>real 0m41.373s
</span><span class='line'>user 0m54.015s
</span><span class='line'>sys 0m0.728s
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="nv">$opreport</span>
</span><span class='line'>Using /home/work/oprof_cache/oprofile_data/samples/ <span class="k">for </span>samples directory.
</span><span class='line'>CPU: Core 2, speed 2.401e+06 MHz <span class="o">(</span>estimated<span class="o">)</span>
</span><span class='line'>Counted CPU_CLK_UNHALTED events <span class="o">(</span>Clock cycles when not halted<span class="o">)</span> with a unit mask of 0x00 <span class="o">(</span>Unhalted core cycles<span class="o">)</span> count 100000
</span><span class='line'>Counted L2_LINES_IN events <span class="o">(</span>number of allocated lines in L2<span class="o">)</span> with a unit mask of 0xf0 <span class="o">(</span>multiple flags<span class="o">)</span> count 100000
</span><span class='line'>CPU_CLK_UNHALT...|L2_LINES_IN:10...|
</span><span class='line'> samples| %| samples| %|
</span><span class='line'>------------------------------------
</span><span class='line'> 939093 100.000 396933 100.000 alignment
</span><span class='line'> CPU_CLK_UNHALT...|L2_LINES_IN:10...|
</span><span class='line'> samples| %| samples| %|
</span><span class='line'> ------------------------------------
</span><span class='line'> 936430 99.7164 395920 99.7448 alignment
</span><span class='line'> 2657 0.2829 1013 0.2552 no-vmlinux
</span><span class='line'> 5 5.3e-04 0 0 ld-2.15.so
</span><span class='line'> 1 1.1e-04 0 0 libc-2.15.so
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span>profiling result of no_alignment</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$time</span> operf --event<span class="o">=</span>CPU_CLK_UNHALTED:100000,L2_LINES_IN:100000:0xf0 ./no_alignment
</span><span class='line'>operf: Profiler started
</span><span class='line'>main: shared 0x601058 0x60105c
</span><span class='line'>child: shared 0x601058 0x60105c
</span><span class='line'>profiled app exited with the following status: 160
</span><span class='line'>
</span><span class='line'>Profiling <span class="k">done</span>.
</span><span class='line'>
</span><span class='line'>real 1m24.739s
</span><span class='line'>user 1m59.167s
</span><span class='line'>sys 0m1.344s
</span><span class='line'>
</span><span class='line'><span class="nv">$opreport</span>
</span><span class='line'>Using /home/work/oprof_cache/oprofile_data/samples/ <span class="k">for </span>samples directory.
</span><span class='line'>CPU: Core 2, speed 2.401e+06 MHz <span class="o">(</span>estimated<span class="o">)</span>
</span><span class='line'>Counted CPU_CLK_UNHALTED events <span class="o">(</span>Clock cycles when not halted<span class="o">)</span> with a unit mask of 0x00 <span class="o">(</span>Unhalted core cycles<span class="o">)</span> count 100000
</span><span class='line'>Counted L2_LINES_IN events <span class="o">(</span>number of allocated lines in L2<span class="o">)</span> with a unit mask of 0xf0 <span class="o">(</span>multiple flags<span class="o">)</span> count 100000
</span><span class='line'>CPU_CLK_UNHALT...|L2_LINES_IN:10...|
</span><span class='line'> samples| %| samples| %|
</span><span class='line'>------------------------------------
</span><span class='line'> 1423030 100.000 1177262 100.000 no_alignment
</span><span class='line'> CPU_CLK_UNHALT...|L2_LINES_IN:10...|
</span><span class='line'> samples| %| samples| %|
</span><span class='line'> ------------------------------------
</span><span class='line'> 1419249 99.7343 1174038 99.7261 no_alignment
</span><span class='line'> 3778 0.2655 3224 0.2739 no-vmlinux
</span><span class='line'> 2 1.4e-04 0 0 ld-2.15.so
</span><span class='line'> 1 7.0e-05 0 0 libc-2.15.so
</span></code></pre></td></tr></table></div></figure>
<h3>Analysis and Conclusion</h3>
<p>I drawed the table for the analysising.</p>
<p><img src="media/Oprofile-and-Cache/Oprofile-and-Cache.png"></p>
<p>Here is the conclusion:</p>
<ul>
<li>The cache missing rate of no_alignment was 82.73%. It became 42.27% with alignment optimization. The cache missing rate was reduced to almost half.</li>
<li>The no_alignment ran 120.5 seconds and the alignment ran 55 seconds. It was a bit more than double faster with alignment optimization.</li>
</ul>
<blockquote><p><strong>NOTE:</strong> The real time is less than user + sys because the program runs on mutlecores parallel</p></blockquote>
</div>
</article>
<article>
<header class="page-header">
<h1 class="entry-title"><a href="/blog/2014/03/09/a-piece-of-code-refactoring/">一段代码的重构</a></h1>
<p class="meta">
<time datetime="2014-03-09T16:26:03-07:00" pubdate data-updated="true">Mar 9<span>th</span>, 2014</time>
</p>
</header>
<div class="entry-content"><h3>为什么要对这段代码重构</h3>
<p>上周对一段代码进行了重构,这段代码是参加一个小组的code review时读到的,这段代码并不长,只有100多行,我花了几个小时(也许两个小时),但是没有读明白这段代码是如何工作的。因为代码可读性不好,代码阅读者的时间就这样被浪费了而没有任何收获。而且代码的书写者在一段时间后也会面临同样的问题,他/她至少需要一点时间才能明白代码如何工作的。还有很重要的一点是,从长远来看,这样的代码可维护性也不高,引入新的改动会比较难或者比较容易引入新的bug。为了避免下一个代码阅读者掉入同样的一个“坑”,我提出需要对代码重构。</p>
<h3>为什么可读性不高</h3>
<p>原始代码请看后面,它的可读性不好主要体现在以下几点:</p>
<ul>
<li>变量的命名可读性。比如dummy命名为isdummy,peek_dummy命名为next_msg_isdummy的可读性会更好,sleep_entry实际上就是!q->msg_queue_pending_msgs变量有重复之嫌。关于如何命名可以参考<a href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/ref=sr_1_1?ie=UTF8&qid=1394416932&sr=8-1&keywords=clean+code" title="Clean Code: A Handbook of Agile Software Craftsmanship">clean code</a>。</li>
<li>do … while的循环条件是一个长条件(((*head == NULL) && !sleep_entry) || (dummy && sleep_entry) || unlikely(peek_dummy)),难读懂,这是造成可读性不好的主要原因,其实际体现出来的是代码的逻辑(或者流程)不清晰</li>
<li>函数的长度很长。并不是说长函数的可读性一定有问题,但是长函数往往是有问题,它通常反映代码书写者对代码没有进行很好的抽象。抽象是书写代码最终要的一种能力,如果我们打开备受<a href="http://norvig.com/" title="Peter Norvig">Peter Norvig</a>推崇的<a href="http://mitpress.mit.edu/sicp/full-text/book/book.html" title="Structure and Interpretation of Computer Programs">Structure and Interpretation of Computer Programs</a>一书,它的第一章就是Building Abstractions with Procedures</li>
<li>注释太少,一些需要注释的而没有,例如上面提到的do … while循环的三个条件</li>
</ul>
<p>通过与拥有这段代码的小组讨论之后,代码的意图实际上如下面的流程图所示:</p>
<p><img src="media/a-piece-of-code-refactoring/flowchart.png"></p>
<p>流程图没有经过简化,有一部分模块是重复的,但是可以让我们理解原代码的意图是什么。可以看到原代码将整个流程都在一个do … while中实现了,这就导致代码结构不清晰。</p>
<h3>代码重构中遵循的一些原则</h3>
<p>重构后的代码请看后面,那么重构中遵循了哪些原则呢?并不复杂,只要写代码过程中,留意下面几条原则,代码的可读性就可以得到极大的提高。</p>
<ul>
<li>给变量和函数一个可读性好的名字,正如我们在clean code中学到的</li>
<li>给if, when, switch等中的逻辑条件(condition)一个体现意图的可读性强的名字,这个condition可以是变量判断或者函数返回值实现的。</li>
<li>保持函数在一个合理的长度</li>
<li>在必要的时候画流程图可以解决混乱,注意这里流程图画的是我们希望代码做什么,而不是我们已经写的代码的流程图,这点很重要,因为我们要写出是执行我们意图的代码</li>
<li>DRY principle,即不要在不同的代码重复相同的代码和信息</li>
<li>就象写完文章之前最后再把文章读一边,在提交代码前把代码再读一边,通常我们都可以发现一些改进的点。不要担心这会导致无止无=尽的修改,最终我们会到达一个我们能接受的点,另外deadline也会迫使我们在适当的时候停下。</li>
</ul>
<h3>重构之后的一些反思</h3>
<ul>
<li>我始终认为代码的可读性是最重要的,尤其是现在代码的开发都是团队合作的结果。虽然重构后的代码有将近200多行,这是因为有更多的函数,注释和空格。但新的代码可读性更好,长远看更容易维护(意味着成本更低)</li>
<li>写可读性强的代码需要时间,所以在计划和估计任务时请考虑这点</li>
<li>可读性好的代码不是一蹴而就的,好的代码通常是通过反复的、慢慢的改进而来,我想这就是为什么需要经常性的代码重构的思想的来源</li>
<li>可读性好的代码让读代码的人获得乐趣,写代码的人也会因为写了一份好的代码而获得乐趣</li>
<li>也许一开始我们写可读性好的代码会有难度或者觉得花费时间,但是只要坚持上述提到的原则,早晚我们可以又快又好的写出漂亮的代码</li>
<li>保持stupid simple,就象功夫,花巧并不一定好,真正的高手只用基础招数的组合</li>
<li>没有UT也可以重构,也不影响写出一个好的代码,但是看下一条</li>
<li>当代码提交后,新代码review之后,碰到的一个问题是:是否应该将新代码提交,这样的犹豫是因为没有足够的UT和funtional测试。完全可以理解,所以这就是为什么需要UT和CI。当然我这里也有个疑惑就是Linux Kernel开发没有UT,那么在Linuxe Kernel的演化过程中,它的开发人员是如何获得这样的信心的</li>
</ul>
<h3>重构前的代码</h3>
<figure class='code'><figcaption><span>[原始代码] (recv_whole_msg_origin.c)</span> <a href='/downloads/code/a-piece-of-code-refactoring/recv_whole_msg_origin.c'>download</a></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
<span class='line-number'>80</span>
<span class='line-number'>81</span>
<span class='line-number'>82</span>
<span class='line-number'>83</span>
<span class='line-number'>84</span>
<span class='line-number'>85</span>
<span class='line-number'>86</span>
<span class='line-number'>87</span>
<span class='line-number'>88</span>
<span class='line-number'>89</span>
<span class='line-number'>90</span>
<span class='line-number'>91</span>
<span class='line-number'>92</span>
<span class='line-number'>93</span>
<span class='line-number'>94</span>
<span class='line-number'>95</span>
<span class='line-number'>96</span>
<span class='line-number'>97</span>
<span class='line-number'>98</span>
<span class='line-number'>99</span>
<span class='line-number'>100</span>
<span class='line-number'>101</span>
<span class='line-number'>102</span>
<span class='line-number'>103</span>
<span class='line-number'>104</span>
<span class='line-number'>105</span>
<span class='line-number'>106</span>
<span class='line-number'>107</span>
<span class='line-number'>108</span>
<span class='line-number'>109</span>
<span class='line-number'>110</span>
<span class='line-number'>111</span>
<span class='line-number'>112</span>
<span class='line-number'>113</span>
<span class='line-number'>114</span>
<span class='line-number'>115</span>
<span class='line-number'>116</span>
<span class='line-number'>117</span>
<span class='line-number'>118</span>
<span class='line-number'>119</span>
<span class='line-number'>120</span>
<span class='line-number'>121</span>
<span class='line-number'>122</span>
<span class='line-number'>123</span>
<span class='line-number'>124</span>
<span class='line-number'>125</span>
<span class='line-number'>126</span>
<span class='line-number'>127</span>
<span class='line-number'>128</span>
<span class='line-number'>129</span>
<span class='line-number'>130</span>
<span class='line-number'>131</span>
<span class='line-number'>132</span>
<span class='line-number'>133</span>
<span class='line-number'>134</span>
<span class='line-number'>135</span>
<span class='line-number'>136</span>
<span class='line-number'>137</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="kt">int</span> <span class="nf">msg_recv_whole_msg</span><span class="p">(</span><span class="k">struct</span> <span class="n">msg_client</span> <span class="o">*</span><span class="n">client</span><span class="p">,</span> <span class="k">struct</span> <span class="n">msg_frag</span> <span class="o">**</span><span class="n">head</span><span class="p">,</span> <span class="kt">int</span> <span class="n">nonblock</span><span class="p">,</span>
</span><span class='line'> <span class="k">struct</span> <span class="n">timespec</span> <span class="o">*</span><span class="n">ts</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="k">struct</span> <span class="n">msg_frag</span> <span class="o">*</span><span class="n">cur_msg</span><span class="p">;</span>
</span><span class='line'> <span class="k">struct</span> <span class="n">msg_queue</span> <span class="o">*</span><span class="n">q</span><span class="p">;</span>
</span><span class='line'> <span class="k">struct</span> <span class="n">ext_wait_queue</span> <span class="n">wait</span><span class="p">;</span>
</span><span class='line'> <span class="k">struct</span> <span class="n">timeval</span> <span class="n">tv</span><span class="p">;</span>
</span><span class='line'> <span class="kt">int</span> <span class="n">ret</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'> <span class="kt">long</span> <span class="n">timeout</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'> <span class="n">bool</span> <span class="n">dummy</span><span class="p">,</span> <span class="n">peek_dummy</span><span class="p">,</span> <span class="n">sleep_entry</span><span class="p">;</span>
</span><span class='line'> <span class="n">bool</span> <span class="n">first_entry</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
</span><span class='line'> <span class="n">buffer_bottom_t</span><span class="o">*</span> <span class="n">bb</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
</span><span class='line'> <span class="o">*</span><span class="n">head</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span><span class="p">(</span><span class="n">g_enable_timestamps</span><span class="p">)</span>
</span><span class='line'> <span class="n">do_gettimeofday</span><span class="p">(</span><span class="o">&</span><span class="n">tv</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="n">rcu_read_lock</span><span class="p">();</span> <span class="c1">// make sure q is not freed (in RCU call back)</span>
</span><span class='line'> <span class="n">q</span> <span class="o">=</span> <span class="n">client</span><span class="o">-></span><span class="n">q</span><span class="p">;</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">unlikely</span><span class="p">(</span><span class="o">!</span><span class="n">q</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'> <span class="n">rcu_read_unlock</span><span class="p">();</span>
</span><span class='line'> <span class="k">return</span> <span class="o">-</span><span class="n">ENOTCONN</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">prefetch</span><span class="p">(</span><span class="n">q</span><span class="p">);</span>
</span><span class='line'> <span class="n">timeout</span> <span class="o">=</span> <span class="n">__prepare_timeout</span><span class="p">(</span><span class="n">ts</span><span class="p">);</span>
</span><span class='line'> <span class="k">do</span> <span class="p">{</span>
</span><span class='line'> <span class="n">spin_lock_bh</span><span class="p">(</span><span class="o">&</span><span class="n">q</span><span class="o">-></span><span class="n">lock</span><span class="p">);</span>
</span><span class='line'> <span class="k">if</span><span class="p">(</span><span class="n">likely</span><span class="p">(</span><span class="n">first_entry</span><span class="p">)){</span>
</span><span class='line'> <span class="n">rcu_read_unlock</span><span class="p">();</span> <span class="c1">//we can unlock RCU because spin_lock_bh already disabled preempt</span>
</span><span class='line'> <span class="n">first_entry</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'> <span class="cm">/* Make sure next message is dummy because other threads may fetch it. */</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">__next_msg_is_dummy</span><span class="p">(</span><span class="n">q</span><span class="p">)</span> <span class="o">&&</span> <span class="p">((</span><span class="o">*</span><span class="n">head</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'> <span class="n">spin_unlock_bh</span><span class="p">(</span><span class="o">&</span><span class="n">q</span><span class="o">-></span><span class="n">lock</span><span class="p">);</span>
</span><span class='line'> <span class="k">break</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">q</span><span class="o">-></span><span class="n">msg_queue_pending_msgs</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="n">sleep_entry</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
</span><span class='line'> <span class="n">DTRACE</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="s">"msg_recv_whole_msg(): Msg priority queue not empty.</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="n">cur_msg</span> <span class="o">=</span> <span class="n">__get_first_msg</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="nb">false</span><span class="p">);</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">unlikely</span><span class="p">(</span><span class="n">cur_msg</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'> <span class="n">spin_unlock_bh</span><span class="p">(</span><span class="o">&</span><span class="n">q</span><span class="o">-></span><span class="n">lock</span><span class="p">);</span>
</span><span class='line'> <span class="n">EPRINTF</span><span class="p">(</span><span class="s">"Msg Q list empty while counter for msg is %u</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span>
</span><span class='line'> <span class="n">q</span><span class="o">-></span><span class="n">msg_queue_pending_msgs</span><span class="p">);</span>
</span><span class='line'> <span class="k">return</span> <span class="o">-</span><span class="n">ERANGE</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="n">dummy</span> <span class="o">=</span> <span class="n">MSG_IS_DUMMY_MSG</span><span class="p">(</span><span class="n">MSGHEADER</span><span class="p">(</span><span class="n">cur_msg</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'> <span class="n">bb</span> <span class="o">=</span> <span class="p">(</span><span class="n">buffer_bottom_t</span> <span class="o">*</span><span class="p">)(</span><span class="n">cur_msg</span><span class="o">-></span><span class="n">data</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="n">q</span><span class="o">-></span><span class="n">msg_queue_pending_msgs</span><span class="o">--</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">dummy</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="n">q</span><span class="o">-></span><span class="n">msg_queue_pending_dummy_msgs</span><span class="o">--</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">need_input_sync</span><span class="p">(</span><span class="n">client</span><span class="p">,</span> <span class="n">cur_msg</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'> <span class="n">client</span><span class="o">-></span><span class="n">q</span><span class="o">-></span><span class="n">msg_queue_pending_wosp_msgs</span><span class="o">--</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">MSG_IS_SYNC_MSG</span><span class="p">(</span><span class="n">bb</span><span class="p">)){</span>
</span><span class='line'> <span class="n">q</span><span class="o">-></span><span class="n">msg_queue_size</span> <span class="o">-=</span> <span class="n">cur_msg</span><span class="o">-></span><span class="n">n_bufs</span><span class="p">;</span>
</span><span class='line'> <span class="n">q</span><span class="o">-></span><span class="n">msg_queue_msgs</span><span class="o">--</span><span class="p">;</span>
</span><span class='line'> <span class="n">msg_pid_queue_stat_dec</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">bb</span><span class="o">-></span><span class="n">process_id</span><span class="p">,</span> <span class="n">cur_msg</span><span class="o">-></span><span class="n">n_bufs</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">recv_input_sync_prepare</span><span class="p">(</span><span class="n">client</span><span class="p">,</span> <span class="n">cur_msg</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// must put behind sync msg send out, for sync msg will use filler2.</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">dummy</span><span class="p">){</span>
</span><span class='line'> <span class="n">bb</span><span class="o">-></span><span class="n">filler2</span> <span class="o">=</span> <span class="p">(</span><span class="n">bb</span><span class="o">-></span><span class="n">filler2</span> <span class="o">&</span> <span class="mh">0xffff0000</span><span class="p">)</span><span class="o">|</span> <span class="n">get_and_inc_hand_seq</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">bb</span><span class="o">-></span><span class="n">process_id</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="cm">/* Take a peek whether the next message is a dummy message. */</span>
</span><span class='line'> <span class="n">peek_dummy</span> <span class="o">=</span> <span class="n">__next_msg_is_dummy</span><span class="p">(</span><span class="n">q</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="n">spin_unlock_bh</span><span class="p">(</span><span class="o">&</span><span class="n">q</span><span class="o">-></span><span class="n">lock</span><span class="p">);</span>
</span><span class='line'> <span class="n">recv_input_sync</span><span class="p">(</span><span class="n">client</span><span class="p">,</span> <span class="n">cur_msg</span><span class="p">);</span>
</span><span class='line'> <span class="n">ret</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'> <span class="n">cur_msg</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
</span><span class='line'> <span class="n">dummy</span> <span class="o">=</span> <span class="n">peek_dummy</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
</span><span class='line'> <span class="n">sleep_entry</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">unlikely</span><span class="p">(</span><span class="n">nonblock</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'> <span class="n">DPRINTF</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="s">"msg_recv_whole_msg(): nonblock, don't sleep</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
</span><span class='line'> <span class="n">spin_unlock_bh</span><span class="p">(</span><span class="o">&</span><span class="n">q</span><span class="o">-></span><span class="n">lock</span><span class="p">);</span>
</span><span class='line'> <span class="n">ret</span> <span class="o">=</span> <span class="o">-</span><span class="n">EAGAIN</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">unlikely</span><span class="p">(</span><span class="n">timeout</span> <span class="o"><</span> <span class="mi">0</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'> <span class="n">DPRINTF</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="s">"msg_recv_whole_msg(): timeout < 0: %ld</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">timeout</span><span class="p">);</span>
</span><span class='line'> <span class="n">spin_unlock_bh</span><span class="p">(</span><span class="o">&</span><span class="n">q</span><span class="o">-></span><span class="n">lock</span><span class="p">);</span>
</span><span class='line'> <span class="n">ret</span> <span class="o">=</span> <span class="o">-</span><span class="n">ETIMEDOUT</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'> <span class="n">wait</span><span class="p">.</span><span class="n">task</span> <span class="o">=</span> <span class="n">current</span><span class="p">;</span>
</span><span class='line'> <span class="n">wait</span><span class="p">.</span><span class="n">state</span> <span class="o">=</span> <span class="n">STATE_NONE</span><span class="p">;</span>
</span><span class='line'> <span class="n">DPRINTF</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="s">"msg_recv_whole_msg(): going to sleep.</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
</span><span class='line'> <span class="n">ret</span> <span class="o">=</span> <span class="n">__wq_sleep</span><span class="p">(</span><span class="o">&</span><span class="n">client</span><span class="o">-></span><span class="n">q</span><span class="p">,</span> <span class="o">&</span><span class="n">timeout</span><span class="p">,</span> <span class="o">&</span><span class="n">wait</span><span class="p">);</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">ret</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="n">cur_msg</span> <span class="o">=</span> <span class="n">wait</span><span class="p">.</span><span class="n">head</span><span class="p">;</span>
</span><span class='line'> <span class="n">recv_input_sync</span><span class="p">(</span><span class="n">client</span><span class="p">,</span> <span class="n">cur_msg</span><span class="p">);</span>
</span><span class='line'> <span class="k">if</span><span class="p">(</span><span class="n">unlikely</span><span class="p">(</span><span class="n">MSG_IS_DUMMY_MSG</span><span class="p">(</span><span class="n">MSGHEADER</span><span class="p">(</span><span class="n">cur_msg</span><span class="p">)))){</span>
</span><span class='line'> <span class="n">WPRINTF</span><span class="p">(</span><span class="s">"Unexpected dummy message received in wait task. From %x.%x.</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span>
</span><span class='line'> <span class="n">MSGHEADER</span><span class="p">(</span><span class="n">cur_msg</span><span class="p">)</span><span class="o">-></span><span class="n">computer</span><span class="p">,</span><span class="n">MSGHEADER</span><span class="p">(</span><span class="n">cur_msg</span><span class="p">)</span><span class="o">-></span><span class="n">family</span><span class="p">);</span>
</span><span class='line'> <span class="n">dummy</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="n">DTRACE</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="s">"msg_recv_whole_msg() ret: %d, msg:(%p)</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">ret</span><span class="p">,</span> <span class="n">cur_msg</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">dummy</span><span class="p">)</span>
</span><span class='line'> <span class="n">msg_free_msg</span><span class="p">(</span><span class="o">&</span><span class="n">cur_msg</span><span class="p">);</span>
</span><span class='line'> <span class="k">else</span> <span class="k">if</span> <span class="p">((</span><span class="o">*</span><span class="n">head</span><span class="p">)</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="c1">//Store the non-dummy message to receiver</span>
</span><span class='line'> <span class="o">*</span><span class="n">head</span> <span class="o">=</span> <span class="n">cur_msg</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span> <span class="k">while</span> <span class="p">(((</span><span class="o">*</span><span class="n">head</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="o">&&</span> <span class="o">!</span><span class="n">sleep_entry</span><span class="p">)</span> <span class="o">||</span> <span class="p">(</span><span class="n">dummy</span> <span class="o">&&</span> <span class="n">sleep_entry</span><span class="p">)</span> <span class="o">||</span> <span class="n">unlikely</span><span class="p">(</span><span class="n">peek_dummy</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'> <span class="cm">/* restore bb->phys_computer because all input-sync related logic is completed here</span>
</span><span class='line'><span class="cm"> restore it so that monitor tools sees compliant data as cIPA */</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="o">*</span><span class="n">head</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="n">bb</span> <span class="o">=</span> <span class="p">(</span><span class="n">buffer_bottom_t</span> <span class="o">*</span><span class="p">)(</span><span class="o">*</span><span class="n">head</span><span class="p">)</span><span class="o">-></span><span class="n">data</span><span class="p">;</span>
</span><span class='line'> <span class="n">bb</span><span class="o">-></span><span class="n">phys_computer</span> <span class="o">=</span> <span class="n">bb</span><span class="o">-></span><span class="n">next_phys_computer</span><span class="p">;</span>
</span><span class='line'> <span class="k">if</span><span class="p">(</span><span class="n">g_enable_timestamps</span><span class="p">){</span>
</span><span class='line'> <span class="n">set_time_rec</span><span class="p">(</span><span class="o">*</span><span class="n">head</span><span class="p">,</span> <span class="n">TIMEREC_ENTER_IOCTL</span><span class="p">,</span> <span class="o">&</span><span class="n">tv</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">__msg_monitor</span><span class="p">(</span><span class="o">*</span><span class="n">head</span><span class="p">,</span> <span class="n">MON_POINTS_T_RECEIVE_C</span><span class="p">);</span>