<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>(self)</title>
  <icon>https://www.gravatar.com/avatar/cf5cad9b475fccd28fe9f0824327bc49</icon>
  <subtitle>WWVzLCBpdCBpcyBCYXNlNjQgZW5jb2Rl</subtitle>
  <link href="https://presentzeng.github.io/atom.xml" rel="self"/>
  
  <link href="https://presentzeng.github.io/"/>
  <updated>2023-01-25T03:22:04.985Z</updated>
  <id>https://presentzeng.github.io/</id>
  
  <author>
    <name>Presentzeng</name>
    <email>presentzeng@gmail.com</email>
  </author>
  
  <generator uri="https://hexo.io/">Hexo</generator>
  
  <entry>
    <title>Macbook网络设置</title>
    <link href="https://presentzeng.github.io/2023/01/21/Proxy%20Setting%20For%20Macbook/"/>
    <id>https://presentzeng.github.io/2023/01/21/Proxy%20Setting%20For%20Macbook/</id>
    <published>2023-01-20T16:00:00.000Z</published>
    <updated>2023-01-25T03:22:04.985Z</updated>
    
    <content type="html"><![CDATA[<p><strong>Mac Book Proxy Setting</strong></p><h1 id="Introduction"><a href="#Introduction" class="headerlink" title="Introduction"></a>Introduction</h1><p>公司发了macbook，用zscaler，用家里网很慢，当然在公司也没快到哪里去。macbook被公司远程管理的，很多限制，这篇文章就是关于我怎么在限制下提高macbook使用体验。</p><h1 id="Debug"><a href="#Debug" class="headerlink" title="Debug"></a>Debug</h1><p>zscaler会显示一个IP,查IP是香港的，直接Ping，至少25%的掉包。200ms以上的延迟。间断性的甚至100%掉包。<br>挂了梯子测试，但是ping无法用proxy。这时就考虑是否挂梯子试试。</p><h1 id="遇到的问题"><a href="#遇到的问题" class="headerlink" title="遇到的问题"></a>遇到的问题</h1><p>梯子有三种</p><ol><li>电脑直接装应用，例如clash，这个会出现unverified app的警告，由于电脑被远程控制了，所以没办法安装上去</li><li>用路由，但是这种路由的价格都比较贵，我手上的并不能翻，需要买新的</li><li>用网关，macbook和我自己的电脑连接同一个wifi,我自己电脑上clash打开”allow Lan”然后用macbook连IP+clash port就行</li></ol><p>目前看最合适的是第三种，但是第三种遇到了一点问题：macbook里面固定使用pac文件了，无法改成web或者socket的系统代理。<br>退一步想看看macbook的input output端口做一个端口转发，例如macbook是走496端口访问外网，那么我用脚本做一个端口转发让macbook访问496的时候，其实访问的是123端口<br>查詢目前使用中的 port 及 process id<br>lsof -n -i | grep LISTEN<br>然后用ssh隧道转发，没有走通<br>退而求其次，准备改Chrome本身的代理，Chrome本身是跟着系统proxy走的，但是可以添加一个Extension叫做policyswitchyOmega，然后在里面设置socket5代理就行了</p><h1 id="效果"><a href="#效果" class="headerlink" title="效果"></a>效果</h1><p>效果非常好，比在公司的速度都要快上很多，也就是证明了zscaler是因为服务器设在香港被墙了导致了网速慢，但是用网关梯子这个前提是在一个局域网里面，这时就考虑是否能够在其他地方也加速</p><h1 id="设计"><a href="#设计" class="headerlink" title="设计"></a>设计</h1><p>macbook -&gt; 腾讯云服务器 -&gt; nginx -&gt; (clash on tencent) </p><ul><li><p>为什么要用nginx代理一次<br>因为clash只接局域网的数据，所以用nginx代理一次</p></li><li><p>参考文档 &amp; 具体方法</p></li></ul><p>用docker运行clash on linux<br><a href="https://blog.zzsqwq.cn/posts/how-to-use-clash-on-linux/">https://blog.zzsqwq.cn/posts/how-to-use-clash-on-linux/</a></p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">config.yaml</span><br><span class="line">secret: &#x27;12345678&#x27;</span><br><span class="line">mixed-port: 7890</span><br><span class="line">allow-lan: true</span><br><span class="line">bind-address: &#x27;*&#x27;</span><br><span class="line">mode: rule</span><br><span class="line">log-level: info</span><br><span class="line">external-controller: 0.0.0.0:9090</span><br></pre></td></tr></table></figure><p>其中wget -O config.yaml $your-proxy-url，这里是订阅链接，catnet有问题，返回的是一串String, 用其他的梯子</p><p>运行nginx</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">docker-compose logs | tail -n 20</span><br><span class="line">docker run -it -d -p 1234:1234 -v /data:/etc/nginx/conf.d nginx</span><br><span class="line"></span><br><span class="line">stream &#123;</span><br><span class="line">    server &#123;</span><br><span class="line">       listen 80;</span><br><span class="line">       proxy_connect_timeout 1s;</span><br><span class="line">       proxy_timeout 3s;</span><br><span class="line">       proxy_pass 172.17.0.2:7890;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>在macbook设置好代理后，整条通路没有通，查Log发现是访问到了Nginx但是没有访问到clash docker,怀疑Nginx访问时依旧带了真实地址，或者地址不清晰，被clash拒绝了<br>在nginx的配置中加一个set_header_IP x_read_IP 类似的东西，并设置为192.168.1.1，依然不可以。</p><p>换条路，用其他的转发工具</p><p>ncat –sh-exec “ncat 172.17.0.2 7890” -l 80  –keep-open<br>将80端口收到的流量转发到clash docker上，然后设置macbook访问IP:80端口，这时可以访问到了，连YouTube都能打开<br>但是有个问题，很慢，一查，发现公司将腾讯云IP地址也做了过滤，导致很慢很慢，也就是搞了半天，路通了，但是很烂</p><p>问题不大， 在此记下，方便以后.</p>]]></content>
    
    
      
      
    <summary type="html">&lt;p&gt;&lt;strong&gt;Mac Book Proxy Setting&lt;/strong&gt;&lt;/p&gt;
&lt;h1 id=&quot;Introduction&quot;&gt;&lt;a href=&quot;#Introduction&quot; class=&quot;headerlink&quot; title=&quot;Introduction&quot;&gt;&lt;/a&gt;Int</summary>
      
    
    
    
    
    <category term="Fun" scheme="https://presentzeng.github.io/tags/Fun/"/>
    
  </entry>
  
  <entry>
    <title>MIT 6.null Lecture Notes</title>
    <link href="https://presentzeng.github.io/2022/12/27/Null_Lecture_Notes/"/>
    <id>https://presentzeng.github.io/2022/12/27/Null_Lecture_Notes/</id>
    <published>2022-12-26T16:00:00.000Z</published>
    <updated>2023-01-15T13:37:11.665Z</updated>
    
    <content type="html"><![CDATA[<p><a href="https://missing.csail.mit.edu/2020/course-shell/">Link of Lecture</a></p><h1 id="Lecture-1-Shell"><a href="#Lecture-1-Shell" class="headerlink" title="Lecture 1 Shell"></a>Lecture 1 Shell</h1><h3 id="PATH"><a href="#PATH" class="headerlink" title="$PATH"></a>$PATH</h3><ol><li>all of the path in the machine the shell will look for program</li><li>which echo: will tell me which echo is gonna run</li></ol><h3 id="Permission"><a href="#Permission" class="headerlink" title="Permission"></a>Permission</h3><ul><li><p>Read Permission on directory<br>To list the content of directory</p></li><li><p>Write Permission on directory<br>rename create or remove files within this directory<br>if you have write permission on file but not on it’s directory<br>you can empty the file, but you can’t delete it</p></li><li><p>Execute permission on directory<br>it is known as search permission, are you allow to enter this directory<br>If you want to open/read/write a file, you must have execute permisison on all it’s parent directories</p></li><li><p>Example one<br>if shell didn’t have write permission on file abc<br>then even if you run the “sudo echo 500 &gt; abc” you will still get permission denied<br>and you can run it as: “echo 500 | sudo tee abc”</p></li></ul><h3 id="single-quote-amp-double-quote"><a href="#single-quote-amp-double-quote" class="headerlink" title="single quote &amp; double quote"></a>single quote &amp; double quote</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">foo=bar</span><br><span class="line">echo &quot;Value is $foo&quot; //foo will be replaced</span><br><span class="line">echo &#x27;Value is $foo&#x27; //foo won&#x27;t be replaced&#x27;</span><br></pre></td></tr></table></figure><h1 id="Lecture-2-Scripting"><a href="#Lecture-2-Scripting" class="headerlink" title="Lecture 2 Scripting"></a>Lecture 2 Scripting</h1><h3 id="parameter-of-shell"><a href="#parameter-of-shell" class="headerlink" title="parameter of shell"></a>parameter of shell</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">mcd()&#123;</span><br><span class="line">  mkdir -p &quot;$1&quot;</span><br><span class="line">  cd &quot;$1&quot;</span><br><span class="line">&#125;</span><br><span class="line">// $0 is the name of file</span><br><span class="line">// $? will get the error code from previous command, and zero means ok</span><br><span class="line">// $_ will get the last argument of the previous command</span><br><span class="line">// $# the number of parameters</span><br><span class="line">// $$ the process id of this command</span><br><span class="line">// $@ is the whole parameter stored</span><br><span class="line">for file in &quot;$@&quot;; do</span><br><span class="line">  grep foobar &quot;$file&quot; &gt; /dev/null</span><br><span class="line">  if [[&quot;$?&quot; -ne 0 ]]; then</span><br><span class="line">    echo &quot;File $file does not have any foobar, adding one&quot;</span><br><span class="line">    echo &quot;# foobar&quot; &gt;&gt; &quot;$file&quot;</span><br><span class="line">  fi </span><br><span class="line">done</span><br></pre></td></tr></table></figure><h3 id="get-output-of-command-into-variable"><a href="#get-output-of-command-into-variable" class="headerlink" title="get output of command into variable"></a>get output of command into variable</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">foo=$(pwd)</span><br></pre></td></tr></table></figure><h3 id="file-evaluation"><a href="#file-evaluation" class="headerlink" title="file evaluation"></a>file evaluation</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">-b file True if file exists and is a block special file</span><br><span class="line">-c file True if file exists and is a character special file</span><br><span class="line">-d file True if file exists and is a drectory</span><br><span class="line">-e file True if file exists and regardless of type </span><br><span class="line">-f file True if file exists and is a regular file</span><br><span class="line">-g file True if file exists and its set group ID flag is set </span><br><span class="line">-h file True if file exists and is a symbolic link</span><br></pre></td></tr></table></figure><h3 id="globbing"><a href="#globbing" class="headerlink" title="globbing"></a>globbing</h3><p>project* will match project1 project12 project 123 and so on<br>,and project? will only expand one char</p><h3 id="first-line-of-shell"><a href="#first-line-of-shell" class="headerlink" title="first line of shell"></a>first line of shell</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">!/usr/bin/env python</span></span><br></pre></td></tr></table></figure><p>to tell the shell what env to trigger</p><h3 id="operator"><a href="#operator" class="headerlink" title="operator"></a>operator</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">false || echo &quot;this will be printed&quot;</span><br><span class="line">true || echo &quot;this won&#x27;t be printed&quot;</span><br><span class="line">// the second one will be excuted only if the first one is false</span><br><span class="line">// and &amp;&amp; is the opposite</span><br><span class="line">// use ; it will always print </span><br></pre></td></tr></table></figure><h3 id="Find"><a href="#Find" class="headerlink" title="Find"></a>Find</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">find . -path &#x27;**/test/*.py&#x27; -type f</span><br><span class="line">find . -mtime -1 // been modified in the last day</span><br><span class="line">find . -name &quot;*.tmp&quot; -exec rm &#123;&#125; \</span><br></pre></td></tr></table></figure><h3 id="reverse-searching"><a href="#reverse-searching" class="headerlink" title="reverse searching"></a>reverse searching</h3><p>click Ctrl+R and type the command you want to search for,in the same time you can click Ctrl+R too<br>and prints all the command that matches</p><h3 id="string-operation"><a href="#string-operation" class="headerlink" title="string operation"></a>string operation</h3><ol><li><p>Get Length</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">string=&quot;abcd&quot;</span><br><span class="line">echo $&#123;#string&#125; #print 4</span><br><span class="line">length=$&#123;array_name[@]&#125; # print length of array_name</span><br></pre></td></tr></table></figure></li><li><p>Extraction</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">string=&quot;runoob is a site&quot;</span><br><span class="line">echo $&#123;string:1:4&#125; # print unoo</span><br></pre></td></tr></table></figure></li></ol><h3 id="equation"><a href="#equation" class="headerlink" title="equation"></a>equation</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">-eq检测两个数是否相等，相等则返回true</span><br><span class="line">-ne检测两个数是否不等，不等则返回true</span><br><span class="line">-gt检测左边的是否大于右边的，大于则返回true</span><br><span class="line">-lt检测左边的是否小于右边的，小于则返回true</span><br><span class="line">-ge检测左边的是否大于等于右边的，大于等于则返回true</span><br><span class="line">-le检测左边的是否小于等于右边的，小于等于则返回true</span><br><span class="line">!非运算</span><br><span class="line">-o或运算</span><br><span class="line">-a与运算</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 数值测试</span></span><br><span class="line">num1=100</span><br><span class="line">num2=200</span><br><span class="line">if test $[num1] -eq $[num2]</span><br><span class="line">then</span><br><span class="line">echo &#x27;两个数相等&#x27;</span><br><span class="line">else</span><br><span class="line">echo &#x27;两个数不相等&#x27;</span><br><span class="line">fi</span><br><span class="line"><span class="meta">  </span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 文件测试</span></span><br><span class="line">cd /bin</span><br><span class="line">if test -e ./bash</span><br><span class="line">then </span><br><span class="line">echo &#x27;文件存在&#x27;</span><br><span class="line">else</span><br><span class="line">echo &#x27;文件不存在&#x27;</span><br><span class="line">fi</span><br></pre></td></tr></table></figure><h3 id="flow-control"><a href="#flow-control" class="headerlink" title="flow control"></a>flow control</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">for var in item1 item2 ... itemN</span><br><span class="line">do</span><br><span class="line">command1</span><br><span class="line">command2</span><br><span class="line">commandN</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">int=1</span><br><span class="line">while [ $int -lt 5 ]</span><br><span class="line">do</span><br><span class="line">echo $int</span><br><span class="line">let &quot;int++&quot;  # let是bash中用于计算的工具，变量计算中不需要加上$表示变量</span><br><span class="line">done</span><br><span class="line"><span class="meta">#</span><span class="bash"> 运行结果</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 1</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 2</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 3</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 4</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 5</span></span><br><span class="line"></span><br><span class="line">echo &#x27;输入1到4之间的数字&#x27;</span><br><span class="line">echo &#x27;你输入的数字为&#x27;</span><br><span class="line">read aNum</span><br><span class="line">case $aNum in</span><br><span class="line">1) echo &#x27;你选择了1&#x27;</span><br><span class="line">;;</span><br><span class="line">2) echo &#x27;你选择了2&#x27;</span><br><span class="line">;;</span><br><span class="line">3) echo &#x27;你选择了3&#x27;</span><br><span class="line">;;</span><br><span class="line">4) echo &#x27;你选择了4&#x27;</span><br><span class="line">;;</span><br><span class="line">*) echo &#x27;你没有输入1到4之间的任何数字&#x27;</span><br><span class="line">;;</span><br><span class="line">esac</span><br><span class="line"></span><br><span class="line">while true</span><br><span class="line">do</span><br><span class="line">echo -n &quot;输入1到5之间的数字&quot;</span><br><span class="line">read aNum</span><br><span class="line">case $aNum in</span><br><span class="line">1|2|3|4|5) echo &quot;你输入的数字为$aNum!&quot;</span><br><span class="line">;;</span><br><span class="line">*) echo &quot;你输入的数字不是1到5之间的！结束&quot;</span><br><span class="line">break</span><br><span class="line">        ;;</span><br><span class="line">    esac</span><br><span class="line">done</span><br></pre></td></tr></table></figure>]]></content>
    
    
      
      
    <summary type="html">&lt;p&gt;&lt;a href=&quot;https://missing.csail.mit.edu/2020/course-shell/&quot;&gt;Link of Lecture&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&quot;Lecture-1-Shell&quot;&gt;&lt;a href=&quot;#Lecture-1-Shell&quot; cl</summary>
      
    
    
    
    
    <category term="Tools" scheme="https://presentzeng.github.io/tags/Tools/"/>
    
  </entry>
  
  <entry>
    <title>Introduction to Computer Networking CS144</title>
    <link href="https://presentzeng.github.io/2022/11/19/Introduction_to_Computer_Networking_CS144/"/>
    <id>https://presentzeng.github.io/2022/11/19/Introduction_to_Computer_Networking_CS144/</id>
    <published>2022-11-18T16:00:00.000Z</published>
    <updated>2023-01-15T13:37:11.664Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Introduction"><a href="#Introduction" class="headerlink" title="Introduction"></a>Introduction</h1><p>This page is about how to build a router, a network interface, and<br>the TCP protocol</p><h1 id="structure"><a href="#structure" class="headerlink" title="structure"></a>structure</h1><p>4 Layer<br>Application / Transport / Network / Link</p><ol><li><p>Link(Ethernet / WIFI)</p></li><li><p>Network Layer(IP)<br>and it is the only Thin waist<br>it is the logical smallest packets which internet break up data into<br>and it is format is very easy Three parts: “To” “From” “Data”<br>Use Internet Protocol, it makes a best effor to deliver datagrams,<br>they can be delivered out of order and corrupted</p></li><li><p>Transport Layer(TCP / UDP)<br>transmission control protocol<br>user datagram protocol</p></li><li><p>Application<br>HTTP<br>ascii text / human readable</p></li></ol><h1 id="Lab1-Warm-Up"><a href="#Lab1-Warm-Up" class="headerlink" title="Lab1 Warm Up"></a>Lab1 Warm Up</h1><h2 id="Fetch-a-Web-Page"><a href="#Fetch-a-Web-Page" class="headerlink" title="Fetch a Web Page"></a>Fetch a Web Page</h2><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">telnet cs144.keithw.org http</span><br><span class="line">GET /hello HTTP/1.1</span><br><span class="line">Host: cs144.keithw.org</span><br><span class="line">Connection: close</span><br><span class="line">one more enter</span><br></pre></td></tr></table></figure><h2 id="Send-yourself-an-email"><a href="#Send-yourself-an-email" class="headerlink" title="Send yourself an email"></a>Send yourself an email</h2><p>pass, it requires standford network</p><h2 id="Listening-and-connecting"><a href="#Listening-and-connecting" class="headerlink" title="Listening and connecting"></a>Listening and connecting</h2><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">netcat -v -l -p 9090</span><br><span class="line">telnet localhost 9090</span><br></pre></td></tr></table></figure><p>Lecture 03</p>]]></content>
    
    
      
      
    <summary type="html">&lt;h1 id=&quot;Introduction&quot;&gt;&lt;a href=&quot;#Introduction&quot; class=&quot;headerlink&quot; title=&quot;Introduction&quot;&gt;&lt;/a&gt;Introduction&lt;/h1&gt;&lt;p&gt;This page is about how to buil</summary>
      
    
    
    
    
    <category term="Network" scheme="https://presentzeng.github.io/tags/Network/"/>
    
  </entry>
  
  <entry>
    <title>Microservices_Monitor(Draft)</title>
    <link href="https://presentzeng.github.io/2022/11/13/Microservices_Monitor/"/>
    <id>https://presentzeng.github.io/2022/11/13/Microservices_Monitor/</id>
    <published>2022-11-12T16:00:00.000Z</published>
    <updated>2023-01-15T13:37:11.665Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Introduction"><a href="#Introduction" class="headerlink" title="Introduction"></a>Introduction</h1><p>This is a page about Microservice and monitoring </p><h1 id="Prometheus"><a href="#Prometheus" class="headerlink" title="Prometheus"></a>Prometheus</h1><p>A tool for monitoring,install monitor application,it will send out the log metrics,<br>and in the UI it can search anything based on rate / frequence and so on<br>take node for example</p><ol><li>it monitor the machine side</li><li>it using job config in yml file to add target</li></ol><h1 id="Microservices"><a href="#Microservices" class="headerlink" title="Microservices"></a>Microservices</h1><ol><li>break down application into microservices based on the business functionalities</li><li>keep one service doing one specifical jobs</li><li>self-contained and independent from each other , can be developed deployed scaled separately</li><li>it is loosely coupled</li><li>communication use api call / message broker / service mesh</li></ol><h2 id="downside-of-MicroSerivces"><a href="#downside-of-MicroSerivces" class="headerlink" title="downside of MicroSerivces"></a>downside of MicroSerivces</h2><ol><li>communication complexity</li><li>how to monitor so many microservice when it is down</li></ol><h2 id="repo"><a href="#repo" class="headerlink" title="repo"></a>repo</h2><p>when use microservice you might choose below two Repo type</p><ul><li>monorepo</li><li>polyreop</li></ul><h2 id="Near-Zero-DownTime"><a href="#Near-Zero-DownTime" class="headerlink" title="Near Zero DownTime"></a>Near Zero DownTime</h2><ol><li><p>Blue/Green deployment strategy<br>running two server and use load balance</p></li><li><p>Canary release strategy<br>slowly roll out the new version to a small group of users</p></li></ol><h1 id="k8s-AutoScaling"><a href="#k8s-AutoScaling" class="headerlink" title="k8s AutoScaling"></a>k8s AutoScaling</h1><p>K8s has three types of autoscaling</p><h2 id="HPA-Horizontal-Pod-Autoscaler"><a href="#HPA-Horizontal-Pod-Autoscaler" class="headerlink" title="HPA(Horizontal Pod Autoscaler)"></a>HPA(Horizontal Pod Autoscaler)</h2><p>Pod number autoscaling, depends on metrics-server,adjust to CPU/memory/QPS</p><h2 id="VPA-Vertical-Pod-Autoscaler"><a href="#VPA-Vertical-Pod-Autoscaler" class="headerlink" title="VPA(Vertical Pod Autoscaler)"></a>VPA(Vertical Pod Autoscaler)</h2><p>Inside a Pod</p><h2 id="CA-cluster-Autoscaler"><a href="#CA-cluster-Autoscaler" class="headerlink" title="CA(cluster Autoscaler)"></a>CA(cluster Autoscaler)</h2><p>Node number autoscaling</p><p><a href="https://github.com/kubernetes/autoscaler/tree/master/cluster-autoscaler">Source code link</a></p><h3 id="folder-structure"><a href="#folder-structure" class="headerlink" title="folder structure"></a>folder structure</h3><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">- core #the core part of CA</span><br><span class="line">  - autoscaler.go #interface of the autoscaling</span><br><span class="line">  - scale_down.go            </span><br><span class="line">  - scale_up.go              </span><br><span class="line"></span><br><span class="line">- cloudprovider #interface with all cloud provider</span><br><span class="line">  - alicloud</span><br><span class="line">  - aws</span><br><span class="line">  - azure</span><br><span class="line">  - baidu</span><br><span class="line"></span><br><span class="line">- expander # different strategy of expand the node</span><br><span class="line">  - factory # design pattern of factory</span><br><span class="line">  - mostpods # use the most pods</span><br><span class="line">  - price # the lowest price</span><br><span class="line">  - priority # according to NodeGroup priority</span><br><span class="line">  - random # random pick</span><br><span class="line">  - waste # the most usage percentage</span><br><span class="line"></span><br><span class="line">- simulator #</span><br></pre></td></tr></table></figure><h3 id="How-k8s-manage-to-increase-Node-AWS"><a href="#How-k8s-manage-to-increase-Node-AWS" class="headerlink" title="How k8s manage to increase Node(AWS)?"></a>How k8s manage to increase Node(AWS)?</h3><p>use awsmanager to increase ASG, the same we increase ASG inside aws console:<br><a href="https://github.com/kubernetes/autoscaler/blob/46d7964132ba7c252663b23283a9e17a15dccc09/cluster-autoscaler/cloudprovider/aws/aws_cloud_provider.go">IncreaseSize .231</a></p><h3 id="which-strategy-k8s-is-using"><a href="#which-strategy-k8s-is-using" class="headerlink" title="which strategy k8s is using?"></a>which strategy k8s is using?</h3><p><a href="https://github.com/kubernetes/autoscaler/blob/62c3b26a8b6f7386a1ffab96b50a3ebf983f56e8/cluster-autoscaler/core/scale_up.go">scale up 475</a></p>]]></content>
    
    
      
      
    <summary type="html">&lt;h1 id=&quot;Introduction&quot;&gt;&lt;a href=&quot;#Introduction&quot; class=&quot;headerlink&quot; title=&quot;Introduction&quot;&gt;&lt;/a&gt;Introduction&lt;/h1&gt;&lt;p&gt;This is a page about Microserv</summary>
      
    
    
    
    
    <category term="Tools" scheme="https://presentzeng.github.io/tags/Tools/"/>
    
  </entry>
  
  <entry>
    <title>Learn_Java</title>
    <link href="https://presentzeng.github.io/2022/11/10/Learn_java/"/>
    <id>https://presentzeng.github.io/2022/11/10/Learn_java/</id>
    <published>2022-11-09T16:00:00.000Z</published>
    <updated>2023-01-15T13:37:11.662Z</updated>
    
    <content type="html"><![CDATA[<p><strong>Simple Introduction</strong><br>let’s Begin</p><h2 id="quick-trying"><a href="#quick-trying" class="headerlink" title="quick trying"></a>quick trying</h2><ol><li>apt install default-jdk</li><li>write First.java with System(Class).out(field).println(function) Hello World</li><li>javac First.java,it will generate First.class, it is actually java Byte Code, will been translated by JVM to native code</li><li>java First to invoke JVM execute the Class file </li></ol><h2 id="different-Editions"><a href="#different-Editions" class="headerlink" title="different Editions"></a>different Editions</h2><ol><li>Standard Edition(SE) : it is core java platform</li><li>Enterprise Edition (EE): for very large scale and distributed systems</li><li>Micro Edition(ME): subset of SE and for mobile</li></ol><h2 id="Most-important-feature"><a href="#Most-important-feature" class="headerlink" title="Most important feature"></a>Most important feature</h2><ol><li>Object-Oriented</li><li>Core Java APIs</li><li>String / Threads / Database Program</li></ol><h2 id="Grammer"><a href="#Grammer" class="headerlink" title="Grammer"></a>Grammer</h2><h3 id="Variables-Primitive-and-Reference-Types"><a href="#Variables-Primitive-and-Reference-Types" class="headerlink" title="Variables Primitive and Reference Types"></a>Variables Primitive and Reference Types</h3><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">int x = 1;</span><br><span class="line">int y = x;</span><br><span class="line">x = 2;</span><br><span class="line">// y still equal 1</span><br></pre></td></tr></table></figure><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Point p1 = new Point(1, 1);</span><br><span class="line">Point p2 = p1;</span><br><span class="line"></span><br></pre></td></tr></table></figure><h3 id="Casting"><a href="#Casting" class="headerlink" title="Casting"></a>Casting</h3><h3 id="Numbers-Strings-and-Arrays"><a href="#Numbers-Strings-and-Arrays" class="headerlink" title="Numbers Strings and Arrays"></a>Numbers Strings and Arrays</h3><h3 id="Read-Input"><a href="#Read-Input" class="headerlink" title="Read Input"></a>Read Input</h3>]]></content>
    
    
      
      
    <summary type="html">&lt;p&gt;&lt;strong&gt;Simple Introduction&lt;/strong&gt;&lt;br&gt;let’s Begin&lt;/p&gt;
&lt;h2 id=&quot;quick-trying&quot;&gt;&lt;a href=&quot;#quick-trying&quot; class=&quot;headerlink&quot; title=&quot;quick try</summary>
      
    
    
    
    
    <category term="Learn" scheme="https://presentzeng.github.io/tags/Learn/"/>
    
  </entry>
  
  <entry>
    <title>Process</title>
    <link href="https://presentzeng.github.io/2022/10/01/Process/"/>
    <id>https://presentzeng.github.io/2022/10/01/Process/</id>
    <published>2022-09-30T16:00:00.000Z</published>
    <updated>2023-01-15T13:37:11.666Z</updated>
    
    <content type="html"><![CDATA[<h1 id="introduction"><a href="#introduction" class="headerlink" title="introduction"></a>introduction</h1><p>Knowing the Process, it is the very basic thing of all.</p><h2 id="Handy-Command"><a href="#Handy-Command" class="headerlink" title="Handy Command"></a>Handy Command</h2><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">1. ps</span><br><span class="line">2. kill  </span><br><span class="line">3. pgrep # get process name and return pid</span><br><span class="line">4. top # monitor </span><br><span class="line">5. jobs # show the table that been stopped by Contrl+Z</span><br><span class="line">6. nice -n -20 cmd # run cmd at it&#x27;s highest priority </span><br><span class="line">7. renice -n priority -p pid # reset the nice of process</span><br><span class="line">8. lsof | grep filename # check which process hold this file</span><br></pre></td></tr></table></figure><h2 id="proc-filesystem-Explanation"><a href="#proc-filesystem-Explanation" class="headerlink" title="/proc/ filesystem Explanation"></a>/proc/ filesystem Explanation</h2><ol><li><p>fd<br>how many file descriptor has been opened by a process</p></li><li><p>maps<br>what is the mem map of process</p></li><li><p>status<br>check the process state: Sleeping / Running / Stopped / Zombie</p></li><li><p>cwd<br>The link the process currently is operating from</p></li><li><p>cmd<br>the current cmd process is executing</p></li><li><p>cmdline<br>how the process is called</p></li><li><p>environ<br>environment variable</p></li></ol>]]></content>
    
    
      
      
    <summary type="html">&lt;h1 id=&quot;introduction&quot;&gt;&lt;a href=&quot;#introduction&quot; class=&quot;headerlink&quot; title=&quot;introduction&quot;&gt;&lt;/a&gt;introduction&lt;/h1&gt;&lt;p&gt;Knowing the Process, it is the</summary>
      
    
    
    
    
    <category term="Tools" scheme="https://presentzeng.github.io/tags/Tools/"/>
    
  </entry>
  
  <entry>
    <title>6S081 Study Notes(Chinese)</title>
    <link href="https://presentzeng.github.io/2022/09/19/6s081_chinese/"/>
    <id>https://presentzeng.github.io/2022/09/19/6s081_chinese/</id>
    <published>2022-09-18T16:00:00.000Z</published>
    <updated>2023-01-15T13:37:11.663Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Process"><a href="#Process" class="headerlink" title="Process"></a>Process</h1><p>一个process本质上就是一段代码加一段数据。</p><p>对于process最重要的三个是 page table, kernel stack, run state，<br>其他两个都好理解，这个kernel stack是process enters kernel（system call or interrupt)的时候，kernel code就在这个stack里面运行，<br>这个stack user process是无法访问的。</p><h1 id="pagetable"><a href="#pagetable" class="headerlink" title="pagetable"></a>pagetable</h1><h2 id="地址映射"><a href="#地址映射" class="headerlink" title="地址映射"></a>地址映射</h2><p>系统将地址分成了一个一个page大小，一个page大小是2的12次方，也就是4096 bytes<br>page table是一个柜子，每个柜子512个抽屉，每个抽屉最大空间64bit 也就是8个byte.<br>也就是一个page table其实也是一个page的大小</p><p>在这个系统中，一个process拥有一个page table.也就是它可以拥有512*4096 bytes（2G）大小的虚拟空间</p><h2 id="虚拟地址转物理地址"><a href="#虚拟地址转物理地址" class="headerlink" title="虚拟地址转物理地址"></a>虚拟地址转物理地址</h2><h3 id="做个比喻"><a href="#做个比喻" class="headerlink" title="做个比喻"></a>做个比喻</h3><p>每一个pagetable就像一个有着512个抽屉的柜子，第一个柜子就是root pagetable</p><p>每个抽屉的编号就是PTE，盒子里有两张纸条，一张写的是下一个柜子地址编号(ppn)，一张写的是当前这个抽屉的权限，例如这个柜子里面的纸条是否有效，哪些应用能打开看等等(flag)。<br>有个叫做”satp”的寄存器存了第一个root柜子的地址</p><h3 id="整个地址转换流程"><a href="#整个地址转换流程" class="headerlink" title="整个地址转换流程"></a>整个地址转换流程</h3><p>你会拿着四张纸条，第一张是一个二进制9bit数，首先根据satp的寄存器地址找到到底是哪个柜子<br>然后呢根据第一个9bit数算一下是第几个抽屉，例如0x03是第三个抽屉，打开第三个抽屉，看看flag纸条这个抽屉是否<br>有效，有效的话呢，拿出ppn纸条，找下一个柜子在哪。一直找到第三个柜子。</p><p>当我们找到第三个柜子的时候，拿到ppn纸条，和最开始拿到的四张纸条的最后一张拼一起<br>就是一个真正的物理地址</p><h2 id="walk"><a href="#walk" class="headerlink" title="walk"></a>walk</h2><p>这是pagetable里面最重要的函数。</p><p>它的核心本质上就是走了一遍上面的地址转换，先给你四张纸，你拿四张纸，一个一个去找。<br>如果没找到就创建一个新的page</p><h2 id="mappages"><a href="#mappages" class="headerlink" title="mappages"></a>mappages</h2><p>这是pagetable里第二重要的函数</p><p>它调用walk将va的地址存进walk返回的PTE里面，也就是你手上有一个物理地址，有一个虚拟地址。<br>简单比喻就是：你首先用walk函数将va转换，最后返回第三个柜子的一个抽屉，然后你将物理地址放进去</p><h2 id="A-kernel-page-table-per-process-HARD"><a href="#A-kernel-page-table-per-process-HARD" class="headerlink" title="A kernel page table per process(HARD)"></a>A kernel page table per process(HARD)</h2><h3 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h3><p>Kernel的虚拟地址和物理地址一一对应，也就是Kernel是整块映射的。但是每一个process只有page table，<br>用于映射user space，如果process调用了System Call，传递了一个user address过去，问题是这个address只有<br>user page table可以翻译，所以要user page table先翻译成物理地址才可以</p><h3 id="做法"><a href="#做法" class="headerlink" title="做法"></a>做法</h3><ol><li>每一个user process多保存一个kernel_pagetable</li><li>allocproc会给每一个process分配空间，在这里面帮kernel_pagetable分配空间</li><li>kvminit调用了mappages将kernel的虚拟和物理一一对应</li><li>procinit专门用来给一个一个的kernel stack初始化，这里初始化就是个地址</li><li>这样做和之前的区别是什么？不用进入supervisor mode?<br>原因可能因为:”Each process has two stacks: a user stack and a kernel stack (p-&gt;kstack). When the process is<br>executing user instructions, only its user stack is in use, and its kernel stack is empty,When the<br>process enters the kernel (for a system call or interrupt), the kernel code executes on the process’s<br>kernel stack; while a process is in the kernel, its user stack still contains saved data, but isn’t actively used.”<br>也就是说kernel stack和user stack不能同时使用</li><li>整个核心流程就是每个proc保存多一个kernel_pagetable.但是这个page table用于存什么东西呢？</li><li>每一个process的kernel_pagetable要和global的一样，那么是怎么copy的呢？好像只需要copy root table就行了。用什么copy呢？memmove?<br>在哪copy呢？scheduler()?</li><li>process的kernel_pagetable要留一分map给p-&gt;kstack，这个kstack在procinit已经赋值成VA了，那么如果要再映射一次，用walkaddr拿到pa,提供一个新VA然后用mappages<br>建立新映射? 这一步失败了，因为walkaddr只能用于拿user space的。</li></ol><h3 id="惊天大BUG"><a href="#惊天大BUG" class="headerlink" title="惊天大BUG!!!!!!!!"></a>惊天大BUG!!!!!!!!</h3><p>我写的时候一直出现 panic(‘remap’),重新梳理了好多次，陷入深深自我怀疑。<br>拖了半天终于对比他人做法发现我是：</p><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mappages(kpt, UART0, UART0, PGSIZE, PTE_R | PTE_W);</span><br></pre></td></tr></table></figure><p>而mappages的参数是</p><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mappages(pagetable, va, sz, pa, perm)</span><br></pre></td></tr></table></figure><p>我参数都摆错顺序了呀！！！！！！！！！！！ 什么低级错误缠了了我这么久！！！！！！</p><h3 id="函数使用错误"><a href="#函数使用错误" class="headerlink" title="函数使用错误"></a>函数使用错误</h3><p>我尝试用kalloc()为proc-&gt;kernel_pagetable开辟空间，uvmcreate其实也是调用这个函数为UserSpace开辟空间，但是它有memset()，这一步漏掉可不行</p><h3 id="理解错误"><a href="#理解错误" class="headerlink" title="理解错误"></a>理解错误</h3><ol><li>each per-process kernel page table should be identical to the existing global kernel page table</li></ol><p>我一直想的是，user_kernel_table 和 kernel_table 是不是需要用memcpy拷贝，这是因为我对walk并没有真的理解，虽然我将它用非递归的方式重写了，<br>当我为user_kernel_table 写了kvminit2并调用，这时候后,user_kernel_table和kernel_table都存了一样的信息，因为我用的是一样的va去存储pa</p><ol start="2"><li>Make sure that each process’s kernel page table has a mapping for that process’s kernel stack</li></ol><p>has a mapping，意思就是将kernel stack记在user_kernel_table上，我之前就有一个问题，在procinit里面，每个process的kernel stack<br>记在kernel_page上，lab要我全部拷贝到allocproc，并且给user_kernel_table记，那么还需不需要给kernel_page记了。实际上不需要了，<br>因为调用kernel stack时，都是用的process自己的user_kernel_table</p><ol start="3"><li>You’ll need a way to free a page table without also freeing the leaf physical memory pages</li></ol><p>leaf physical memory pages, 不是指第三个page table,而是指第三个page指向的physical page!!!<br>每个user kernel page table本质上指向的是同一个地址，你肯定不能free掉啊！ </p><p>非常重要：kfree和kalloc都是以一个page为最小单位，而且是对齐的，也就是随便拿一个PTE，里面的地址被释放，这个地址所在的整个page都会被释放<br>所以根本不需要va的最后offset，walk返回的pte通过pte2pa拿到的就是那一块page的开头地址</p><p>在xv6里面，先用uvmunmap,调用walk找到所有的mapping释放掉，然后再调用freewalk释放掉所有的pagetable</p><h3 id="复盘"><a href="#复盘" class="headerlink" title="复盘"></a>复盘</h3><p>9/1完成speedup开始做2020的per process kernel page，每个星期算两个小时，也做了近十个小时了。<br>实际操作中，我被很多Debug卡住了，例如我尝试写kvminit，用mappages的时候，没看到和kvmmap的参数的顺序有差别，再比方说，我全部写完后，<br>一直有kvmap的panic错误，但是我只是去检查我自己的代码，其实这时需要改动kvmap那里，将kernel_pagetable改为user_kernel_pagetable就行了</p><p>为什么我会遇到这种坎坷呢，核心还是我没有完全的熟透这个pagetable的每一个细节，具体行动上，我并没有严格按照Lab的instruction 将要求的vm.c全部代码看一遍，<br>过一遍，其实如果我将每个代码重新写一下，就像写walk()函数一样，后面就会清晰很多，而不是遇到问题又重新去看。</p><p>遇到问题再查资料也可以，但问题是，会更痛苦，更曲折，如果已经有instruction，是可以尝试follow一下的。</p><p>很多时候，book里面也已经给了所有信息，例如我遇到的问题书上其实已经说了“Functions starting with kvm manipulate<br>the kernel page table; functions starting with uvm manipulate a user page table”。 但是当时看的时候并没当回事，所以也不用妄自菲薄，<br>学而时习之，过了一遍再来复习，肯定又有完全不一样的理解。</p><h2 id="Simplify-HARD"><a href="#Simplify-HARD" class="headerlink" title="Simplify (HARD)"></a>Simplify (HARD)</h2><h3 id="背景-1"><a href="#背景-1" class="headerlink" title="背景"></a>背景</h3><p>上一个task已经创建了一个user kernel page table,但是依旧没有能够做到调用kernel直接dereference user pointers，因为这时user_kernel_page_table<br>只存了kernel的map，还有user data的map没做</p><p>task已经提供了copyin_new，这里去掉了将va转为pa，然后再memmove，直接从src(va)拷贝到dest(pa)，</p><p>以下为错误理解！！！<br>等于说src(va)这里就是真正的物理地址。user_data和kernel一样都是va=pa，map在user_kernel_pgtb上</p><p>所有的user_data都存在了user page table里面，那么要”add user mappings to process’s kernel page table” 应该要遍历user_page_table<br>拿到所有的物理地址，并且映射到user_kernel_page_table(va=pa)<br>于是函数逻辑就是：先遍历user page table,拿到了物理地址，然后将物理地址和一样数值的va一对一map在user kernel page table</p><p>！！！以上为错误理解</p><h3 id="解析"><a href="#解析" class="headerlink" title="解析"></a>解析</h3><p>参考了其他人的解法，发现是将user_page_table从0到proc-&gt;sz全部遍历一边，取出里面的pa，映射到user_kernel_pgtb.<br>copyin_new()后，kernel拿到的是user_kernel_pgtb可以解析的虚拟地址。之前拿到的是copyin用walk解析后的实际地址。</p><h3 id="我最大的疑问是，kernel是什么做dereference呢，walk-？"><a href="#我最大的疑问是，kernel是什么做dereference呢，walk-？" class="headerlink" title="我最大的疑问是，kernel是什么做dereference呢，walk()？"></a>我最大的疑问是，kernel是什么做dereference呢，walk()？</h3><p>是paging hardware做的解析，你只要提供pagetable它就能解析，有一句很重要的话没看到：walk mimics the RISC-V paging hardware<br>walk只是用来模拟page hardware的</p><h3 id="但依旧有个疑问-kernel在什么情况下做的"><a href="#但依旧有个疑问-kernel在什么情况下做的" class="headerlink" title="但依旧有个疑问,kernel在什么情况下做的"></a>但依旧有个疑问,kernel在什么情况下做的</h3><p>读到一句话：copyout and copyin copy data to and from user virtual addresses provided as<br>system call arguments; they are in vm.c because they need to explicitly translate those addresses<br>in order to find the corresponding physical memory<br>也就是copyin的explicitly translate是不应该的，也只要satp握着的pgtb含有这个va，它就会自动转换</p><p>参考下面这一段<br>Early in the boot sequence, main calls kvminit (kernel/vm.c:22) to create the kernel’s page<br>table. This call occurs before xv6 has enabled paging on the RISC-V, so addresses refer directly to<br>physical memory</p><h3 id="对于user-page-table来说，它的虚拟地址是连续的"><a href="#对于user-page-table来说，它的虚拟地址是连续的" class="headerlink" title="对于user page table来说，它的虚拟地址是连续的"></a>对于user page table来说，它的虚拟地址是连续的</h3><p>uvmalloc代码揭示了，每一次growproc，都是从oldsz -&gt; newsz, 地址是连续的</p><h2 id="Detecting-which-pages-have-been-accessed-HARD"><a href="#Detecting-which-pages-have-been-accessed-HARD" class="headerlink" title="Detecting which pages have been accessed (HARD)"></a>Detecting which pages have been accessed (HARD)</h2><h3 id="几个问题："><a href="#几个问题：" class="headerlink" title="几个问题："></a>几个问题：</h3><ol><li><p>怎么从Manual里选到底用哪个数值作为PTE_A?<br>这里在riscv-privileged里面有图表</p><img src="/img/6s081/1.png" /></li><li><p>在哪里设置PTE_A?<br>Risc-V自己设置的 Each leaf PTE contains an accessed (A) and dirty (D) bit. The A bit indicates the virtual page has<br>been read, written, or fetched from since the last time the A bit was cleared. The D bit indicates<br>the virtual page has been written since the last time the D bit was cleared</p></li></ol><h1 id="Trap"><a href="#Trap" class="headerlink" title="Trap"></a>Trap</h1><h2 id="只有三种情况会触发Trap"><a href="#只有三种情况会触发Trap" class="headerlink" title="只有三种情况会触发Trap"></a>只有三种情况会触发Trap</h2><ol><li>System call, user executes the ecall</li><li>Exception</li><li>Device interrupt </li></ol><h2 id="Trap的特点"><a href="#Trap的特点" class="headerlink" title="Trap的特点"></a>Trap的特点</h2><ol><li>transparent</li><li>Three distinct case trap: user space / kernel space /timer interrupt</li></ol><h2 id="Trap-流程"><a href="#Trap-流程" class="headerlink" title="Trap 流程"></a>Trap 流程</h2><ol><li>hardware action by cpu, Vector Assembly</li><li>C Trap handler</li><li>system call &amp; driver</li></ol><h2 id="Kernel-will-do-instead-of-CPU"><a href="#Kernel-will-do-instead-of-CPU" class="headerlink" title="Kernel will do instead of CPU"></a>Kernel will do instead of CPU</h2><ol><li>switch to kernel page table</li><li>switch to a stack in the kernel</li><li>save register</li></ol><h2 id="寄存器说明"><a href="#寄存器说明" class="headerlink" title="寄存器说明"></a>寄存器说明</h2><p>这些寄存器只能在supervisor mode读取和存储</p><ul><li>stvec: Trap handler的地址</li><li>sepc: 存PC指针的地方，trap完后，调用sret重新将spec写入pc</li><li>scause: 存放为什么调用trap</li><li>sscratch: ?</li><li>sstatus: SIE为1才会允许device interrupts, SSP用于指明trap来自哪个Mode</li><li>stval: 存储无法被translate的地址</li></ul><h2 id="Trap-from-user-space流程-with-register"><a href="#Trap-from-user-space流程-with-register" class="headerlink" title="Trap from user space流程(with register)"></a>Trap from user space流程(with register)</h2><ol><li>如果是device interrupt 且 sstatus的SIE未set，Stop</li><li>清掉SIE bit</li><li>copy pc sepc 存储pc指针</li><li>存储当前mode在sstatus</li><li>set scause number</li><li>换到supervisor mode</li><li>pc = stvec</li><li>开始执行</li></ol><h2 id="Trap-from-user-space流程（with-code"><a href="#Trap-from-user-space流程（with-code" class="headerlink" title="Trap from user space流程（with code)"></a>Trap from user space流程（with code)</h2><ol><li><p>trampoline.S/uservec<br>汇编，stvec指向处，这个user trap和kernel trap都会用到，但是RISC-V的机制不会改satp从user_pgtb到kernel_pgtb<br>所以uservec需要同时map同一地址的user_pgtb和kernel_pgtb<br>开头这段代码会将寄存器存在trampframe这片地址（每一个user_pgtb都存了，在地址TRAPEFRAME）<br>然后从tramframe的最前面提取存好的kernel_pgtb等数据进入下一个函数</p></li><li><p>trap.c/usertrap<br>首先就将stvec换成kernelvec，也就是这个trap可以继续被打断？<br>判断是哪种trap System Call / Devices / execption<br>其中exception会杀掉错误process</p></li><li><p>trap.c/usertrapret<br>这个就是重新恢复环境，stvec恢复uservec，sepc恢复到pc等等，然后userret回去，这里注意userret会switch page table</p></li><li><p>trampoline.S/userret<br>和uservec对比完全反过来，上一层调用的时候传入两个参数分别是Trapframe和pagetable</p></li></ol><h2 id="Trap-from-kernel流程（with-code"><a href="#Trap-from-kernel流程（with-code" class="headerlink" title="Trap from kernel流程（with code)"></a>Trap from kernel流程（with code)</h2><ol><li><p>kernelvec.S<br>存一些寄存器之类的</p></li><li><p>trap.c/kerneltrap<br>如果是device就call devintr,如果是exeption就直接panic<br>如果是timer interrupt，就call yield让CPU</p></li></ol><h2 id="System-Call-with-Code"><a href="#System-Call-with-Code" class="headerlink" title="System Call(with Code)"></a>System Call(with Code)</h2><p>前面的lab已经添加过system call了，现在来真实了解一下system call怎么调用的</p><ol><li>kernel/syscall.c<br>有一个数组，存储各个syscall地址,从a7寄存器取offset，然后直接调用了对应的sys_function.<br>然后将返回值放在p-&gt;trapframe-&gt;a0</li></ol><h2 id="stvec-是什么寄存器，为什么很重要？"><a href="#stvec-是什么寄存器，为什么很重要？" class="headerlink" title="stvec 是什么寄存器，为什么很重要？"></a>stvec 是什么寄存器，为什么很重要？</h2><p>The kernel writes the address of its trap handler here; the RISC-V jumps here to<br>handle a trap。只要进入trap，就会调用这个地址的函数。<br>如果不这么做，那么Then a trap could switch to supervisor mode while still running user instructions</p><h2 id="trapline是什么-trapframe又是什么"><a href="#trapline是什么-trapframe又是什么" class="headerlink" title="trapline是什么,trapframe又是什么"></a>trapline是什么,trapframe又是什么</h2><p>trampline是一段汇编代码，里面用于将硬件的Trap信号导到C代码上，trampoline这个page在每个pagetable都有，而且都是同样的virtual address<br>方便切换时好继续执行<br>trapframe是一段内存空间，process拥有的，用于在Trap的时候存寄存器等值<br>这些东西的产生，核心是因为user_page里面没有kernel memory，如果用Simplify (HARD)的方法，就可以去掉这些东西，并且更高效</p><h2 id="COW-copy-on-write"><a href="#COW-copy-on-write" class="headerlink" title="COW(copy on write)"></a>COW(copy on write)</h2><p>Fork后child不会复制parent的PA，而是指向它，到真的要用的时候，才通过page fault触发并创建新的page,<br>为了区分COW和非法访问，PTE里面存了一个bit用来区分。同时要有一个ref count,这个存在内存里，用来说明此page是否没人引用了。</p><h2 id="lazy-allocation"><a href="#lazy-allocation" class="headerlink" title="lazy allocation"></a>lazy allocation</h2><p>sbrk()用于开辟内存，而且是eager allocation,但是呢：applications often ask for more memory than they need<br>所以真实情况是，最开始并不给application真实的空间，等真的开始访问对应地址的时候，会触发page fault trap，这时kernel才帮忙创建并分配<br>page table地址</p><h2 id="zero-fill-on-demand"><a href="#zero-fill-on-demand" class="headerlink" title="zero fill on demand"></a>zero fill on demand</h2><p>全局变量都置为0，这些变量其实都指向了同一个pa,然后都只有只读权限，当尝试写的时候，会触发page fault并且开新空间</p><h2 id="Demand-paging"><a href="#Demand-paging" class="headerlink" title="Demand paging"></a>Demand paging</h2><p>这个是在Exec的时候，做lazy allocation</p><h2 id="Lab-Trap-Backtrace-moderate"><a href="#Lab-Trap-Backtrace-moderate" class="headerlink" title="Lab Trap : Backtrace(moderate)"></a>Lab Trap : Backtrace(moderate)</h2><p>问题1：offset(-8)，这个负八单位是多少？1Byte? 1bit? 4Byte?<br>这里是一个Byte，系统都是一个Byte一个Byte的操作。</p><p>问题2：怎么判断什么时候停止往回回溯backtrace?<br>每个kernel stack都是一页,所以拿到第一个framepointer的地址，然后通过PGROUNDDOWN和UP分别拿到栈头和栈尾，通过判断地址是否是在这个page下就可知道什么时候停下了。</p>]]></content>
    
    
      
      
    <summary type="html">&lt;h1 id=&quot;Process&quot;&gt;&lt;a href=&quot;#Process&quot; class=&quot;headerlink&quot; title=&quot;Process&quot;&gt;&lt;/a&gt;Process&lt;/h1&gt;&lt;p&gt;一个process本质上就是一段代码加一段数据。&lt;/p&gt;
&lt;p&gt;对于process最重要的三个是 p</summary>
      
    
    
    
    
    <category term="Linux" scheme="https://presentzeng.github.io/tags/Linux/"/>
    
    <category term="System" scheme="https://presentzeng.github.io/tags/System/"/>
    
    <category term="OS" scheme="https://presentzeng.github.io/tags/OS/"/>
    
  </entry>
  
  <entry>
    <title>Handy_Command</title>
    <link href="https://presentzeng.github.io/2022/08/24/Handy_Command/"/>
    <id>https://presentzeng.github.io/2022/08/24/Handy_Command/</id>
    <published>2022-08-23T16:00:00.000Z</published>
    <updated>2023-01-15T13:37:11.664Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Handy-Command"><a href="#Handy-Command" class="headerlink" title="Handy Command"></a>Handy Command</h1><blockquote><p>just copy and use it</p></blockquote><h3 id="word-counts"><a href="#word-counts" class="headerlink" title="word counts"></a>word counts</h3><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">grep  filename | wc -l</span><br></pre></td></tr></table></figure><h3 id="shell-if"><a href="#shell-if" class="headerlink" title="shell if"></a>shell if</h3><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">aflag=$(grep filename | wc -l)</span><br><span class="line"><span class="meta">if[[$</span><span class="bash">aflag -eq 1]]</span></span><br><span class="line">then</span><br><span class="line">sed xxxx</span><br><span class="line">else</span><br><span class="line">echo &quot;done&quot;</span><br><span class="line">fi</span><br></pre></td></tr></table></figure><h3 id="bash-Grep-amp-find-amp-env-amp-str"><a href="#bash-Grep-amp-find-amp-env-amp-str" class="headerlink" title="bash Grep &amp; find &amp; env &amp; str"></a>bash Grep &amp; find &amp; env &amp; str</h3><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">for pat in `find . -name &quot;log4j*&quot;` ; do unzip -p $package META-INF/MANIFEST.MF | grep Implementation-Version; done; // dump and grep key value</span><br><span class="line"><span class="meta">$</span><span class="bash">&#123;str::-n&#125; // delete last n byte</span></span><br><span class="line">kubectl get pvc -n pt | grep zookeeper | head -n 100 | awk &#x27;&#123;print $1&#125;&#x27; | xargs kubectl delte pvc -n pt</span><br><span class="line">bash JAVA_HOME=xxx scripts // activate env for certain command</span><br><span class="line">aws --profile=saml cloudtrail lookup-events --lookup-aatributes xxx | grep -o -P &#x27;.&#123;1,3&#125;xxx.&#123;0,200&#125;&#x27; // grep and print certain length of content</span><br></pre></td></tr></table></figure><h3 id="run-docker-as-a-root-user"><a href="#run-docker-as-a-root-user" class="headerlink" title="run docker as a root user"></a>run docker as a root user</h3><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker container exec -u 0 -it mycontainer bash</span><br></pre></td></tr></table></figure><h3 id="pip-install-with-Cert"><a href="#pip-install-with-Cert" class="headerlink" title="pip install with Cert"></a>pip install with Cert</h3><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install requests --cert *.pem --trusted-host *.xxx.com --index-url https://username:password@repolink</span><br></pre></td></tr></table></figure><h3 id="CPU-usage"><a href="#CPU-usage" class="headerlink" title="CPU usage"></a>CPU usage</h3><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">println &quot;uptime&quot;.execute().text</span><br><span class="line">println &quot;mpstat -P ALL&quot;.execute</span><br></pre></td></tr></table></figure><h3 id="Get-current-available-physical-on-windows"><a href="#Get-current-available-physical-on-windows" class="headerlink" title="Get current available physical on windows"></a>Get current available physical on windows</h3><p>systeminfo | find “Available Physical Memory”</p><h3 id="k8s-command"><a href="#k8s-command" class="headerlink" title="k8s command"></a>k8s command</h3><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">export http_proxy=</span><br><span class="line">unset http_proxy</span><br><span class="line"></span><br><span class="line">helm list -n namespace</span><br><span class="line">helm install --dry-run --debug ./foldername --generate-name</span><br><span class="line">helm install ./foldername --create-namespace -n present-test --generate-name </span><br><span class="line">helm delete xxx -n namespace</span><br><span class="line"></span><br><span class="line">kubectl get pods -n namespace</span><br><span class="line">kubectl describe pod pod_name -n namespace</span><br><span class="line">kubectl exec -it xxx -n namespace -- sh</span><br><span class="line">kubectl get pvc</span><br><span class="line">kubectl delete pvc xxx</span><br><span class="line">kubectl create secret generic xxx_secret --from-literal=DB_USER=*** --from-literal=DB_PASSWORD=&#x27;***&#x27; -n namespace</span><br><span class="line">kubectl get sa(service Account)</span><br><span class="line">kubectl describe sa</span><br></pre></td></tr></table></figure><h3 id="special-char"><a href="#special-char" class="headerlink" title="special char"></a>special char</h3><p>u”\u00b0” = °</p><h3 id="C-Pointer"><a href="#C-Pointer" class="headerlink" title="C Pointer"></a>C Pointer</h3><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">int a[10];</span><br><span class="line">int *pa;</span><br><span class="line">pa = &amp;a[0];</span><br><span class="line">pa == a;</span><br><span class="line">a[i] == *(a+i)</span><br><span class="line">pa[i] == *(pa+i)</span><br><span class="line">pa++ legal</span><br><span class="line">a++ illegal</span><br><span class="line">The</span><br><span class="line">meaning of ``adding 1 to a pointer,&#x27;&#x27; and by extension, all pointer arithmetic, is that pa+1 points</span><br><span class="line">to the next object</span><br></pre></td></tr></table></figure>]]></content>
    
    
      
      
    <summary type="html">&lt;h1 id=&quot;Handy-Command&quot;&gt;&lt;a href=&quot;#Handy-Command&quot; class=&quot;headerlink&quot; title=&quot;Handy Command&quot;&gt;&lt;/a&gt;Handy Command&lt;/h1&gt;&lt;blockquote&gt;
&lt;p&gt;just copy and</summary>
      
    
    
    
    
    <category term="Tools" scheme="https://presentzeng.github.io/tags/Tools/"/>
    
  </entry>
  
  <entry>
    <title>Tricky Bug</title>
    <link href="https://presentzeng.github.io/2022/08/19/Tricky_Bug/"/>
    <id>https://presentzeng.github.io/2022/08/19/Tricky_Bug/</id>
    <published>2022-08-18T16:00:00.000Z</published>
    <updated>2023-01-15T13:37:11.666Z</updated>
    
    <content type="html"><![CDATA[<p><strong>Tricky Bug</strong></p><blockquote><p>Sometimes when using the Opening source tools, i will encounter weird bug or feature, most of it because i am using it in a unique way.</p></blockquote><h2 id="1-NPM-CONFIG-Auth"><a href="#1-NPM-CONFIG-Auth" class="headerlink" title="1. NPM CONFIG Auth"></a>1. NPM CONFIG Auth</h2><p>when npm is using the private registry, like the Nexus, it will require Token for npm to access it</p><p>generally we can use in this way</p><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">export NPM_CONFIG_STRICT_SSL=false</span><br><span class="line">export NPM_CONFIG_ALWAYS_AUTH=true</span><br><span class="line">export NPM_CONFIG__AUTH=xxxx</span><br><span class="line">npm ping --registry https://xxx.xxx.xxx/</span><br></pre></td></tr></table></figure><p>you might have noticed that for the AUTH env it has two underscore instead of one<br>actually it is a feature of NPM, and it is merged into the tool since 2014<br>refer link: <a href="https://github.com/npm/cli/issues/3985">https://github.com/npm/cli/issues/3985</a></p><p>Solution:<br>Use NPM_CONFIG__AUTH</p><h2 id="2-Docker-Config"><a href="#2-Docker-Config" class="headerlink" title="2. Docker Config"></a>2. Docker Config</h2><p>When many users are using docker on Jenkins， we can restrict user by locking the Global Config</p><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">touch /var/log/gitconfig</span><br><span class="line">ln -s var/log/gitconfig /home/user/.gitconfig</span><br><span class="line"></span><br><span class="line">touch /var/log/dockerconfig.json</span><br><span class="line">mkdir /home/user/.docker</span><br><span class="line">chmod 600 /home/jenbld/.docker </span><br><span class="line">ln -s /var/log/dockerconfig.json /home/user/.docker/config.json</span><br></pre></td></tr></table></figure><p>and we can export home=${workspace}, to use the local config under workspace<br>There comes with a plugin problem:</p><p>The “Docker Build and Publish plugin” will fail on “Failed to deserialize Rejected”.</p><p>see the changes:<br><a href="https://github.com/moby/moby/commit/18c9b6c6455f116ae59cde8544413b3d7d294a5e">https://github.com/moby/moby/commit/18c9b6c6455f116ae59cde8544413b3d7d294a5e</a><br>change from .dockercfg to config.json</p><p>Solution:<br>upgrade your plugin or use the deprecated config .dockercfg</p><h2 id="3-Terraform-init"><a href="#3-Terraform-init" class="headerlink" title="3. Terraform init"></a>3. Terraform init</h2><p>when you run terraform init on laptop you might encounter</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">╷</span><br><span class="line">│ Error: Failed to query available provider packages</span><br><span class="line">│</span><br><span class="line">│ Could not retrieve the list of available versions for provider</span><br><span class="line">│ hashicorp/google: could not connect to registry.terraform.io: Failed to</span><br><span class="line">│ request discovery document: Get</span><br><span class="line">│ &quot;https://registry.terraform.io/.well-known/terraform.json&quot;: proxyconnect</span><br><span class="line">│ tcp: EOF</span><br></pre></td></tr></table></figure><p>it might because the proxy issue, set on the vpn and set proxy<br>export https_proxy=’<a href="http://127.0.0.1:7890&#39;">http://127.0.0.1:7890&#39;</a> then will be fine</p><p>when you encounter “Error: storage.NewClient() failed: dialing: google: could not find default credentials”<br>during terraform init, just add the credentials = “../key.json” to backend.tf to fix it</p>]]></content>
    
    
      
      
    <summary type="html">&lt;p&gt;&lt;strong&gt;Tricky Bug&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Sometimes when using the Opening source tools, i will encounter weird bug or feature, mos</summary>
      
    
    
    
    
    <category term="Debug" scheme="https://presentzeng.github.io/tags/Debug/"/>
    
  </entry>
  
  <entry>
    <title>外汇投机指南(All about Forex)</title>
    <link href="https://presentzeng.github.io/2022/04/17/Speculation_on_forex/"/>
    <id>https://presentzeng.github.io/2022/04/17/Speculation_on_forex/</id>
    <published>2022-04-16T16:00:00.000Z</published>
    <updated>2022-12-03T12:42:08.533Z</updated>
    
    <content type="html"><![CDATA[<p><strong>外汇投机指南</strong></p><blockquote><p>Editing</p></blockquote><h4 id="为什么选外汇"><a href="#为什么选外汇" class="headerlink" title="为什么选外汇"></a>为什么选外汇</h4><ol><li>它超级大盘，EURUSD这种货币对几乎无人能操纵</li><li>外汇的信息获取非常充足</li><li>极小的入市成本，0.01手，一美金都能做交易</li></ol><h4 id="Why-Forex"><a href="#Why-Forex" class="headerlink" title="Why Forex"></a>Why Forex</h4><ol><li>It is Huge, No one can manipulate it Such as EURUSD</li><li>It is all open information for Forex</li><li>extremely Low investment cost, 0.01lot you can start your journey</li></ol><h4 id="平台和工具"><a href="#平台和工具" class="headerlink" title="平台和工具"></a>平台和工具</h4><ul><li>Python 3.8.8</li><li>MetaTrade5</li><li>MetaTrade的Python Module</li><li>一个外汇佣商的账号</li></ul><h4 id="Platform-and-Tools"><a href="#Platform-and-Tools" class="headerlink" title="Platform and Tools"></a>Platform and Tools</h4><ul><li>Python 3.8.8</li><li>MetaTrade5</li><li>MetaTrade的Python Module</li><li>A Forex Account Such as FTXM</li></ul><h4 id="背景知识"><a href="#背景知识" class="headerlink" title="背景知识"></a>背景知识</h4><p>这里以EURUSD为介绍<br>这个货币对叫做欧元/美元，在交易系统上有两个价格 卖价（bid）买价（ask）<br>这个都是对于交易者来说的，也就是ask就是市场上你能买到的最低价,bid就是市场上你能卖的最高价<br>ask &gt;= bid，点差就是ask-bid，点差是可以为0的，例如EURUSD行情比较平缓时，点差为0</p><h4 id="Background"><a href="#Background" class="headerlink" title="Background"></a>Background</h4><p>Take EURUSD as introduction<br>It is called EUR/USD pair, on market, it has two price, bid and ask<br>it is all refer to trader, ask is the lowest price you can ask to buy<br>bid is the highest price, you can sell<br>ask &gt;= bid, Spread = ask - bid, it can be zero</p><h4 id="怎么赚钱"><a href="#怎么赚钱" class="headerlink" title="怎么赚钱"></a>怎么赚钱</h4><p>外汇的波动非常小，需要用杠杆放大，交易所会借钱给你，帮你放大波动用于赚钱<br>例如 你有10美元，用2000倍杠杆，此时你如果在1.01254这个价位做空0.01手，交易所会借给你1000美元，你需要用1000/2000 = 0.5美元作为保证金<br>此时如果价格从1.01254波动到1.01200,那么你就用了0.5美金赚到了(1.01254 - 1.01200)*1000 = 0.54美金<br>(这里没有计算点差费用和手续费，用于做一个简单说明)</p><h4 id="这个赚钱吗风险怎样"><a href="#这个赚钱吗风险怎样" class="headerlink" title="这个赚钱吗风险怎样"></a>这个赚钱吗风险怎样</h4><p>依旧以EURUSD为例，它的每小时波动标准差为0.000705641339641201,也就是如果你用2000的杠杆，用0.5美元做保证金，如果赌对了，平均能赚到0.7美元<br>以三个月内的小时线做了分布图</p><img src="/img/forex/forex_pic.png" />大概率都落在0.002的波动范围内，也就是用2000倍杠杆，0.5美金能赚2美金风险有多大呢，如果汇率往另外一个方向波动0.0005不到，你的0.5美金全部消失<h4 id="常见交易方法"><a href="#常见交易方法" class="headerlink" title="常见交易方法"></a>常见交易方法</h4><ol><li><p>MACD/RSI/Bollinger Bands/EMA<br>这一部分我用Python和EA都跑过，模拟盘没有赚到钱</p></li><li><p>滚仓<br>先开一个单，如果盈利了就继续加仓，这个有赚到过一点点</p></li><li><p>马丁策略<br>这个是非常著名的策略，胜率非常高，但是一旦失败，就爆仓，网上有非常多的教程也有现成的<br>我贴一下我的模拟盘净值,分别是失败和成功的</p></li></ol><img src="/img/forex/martin_failed.png" /><img src="/img/forex/martin_success.png" /><h4 id="参考代码-Demo-code"><a href="#参考代码-Demo-code" class="headerlink" title="参考代码(Demo code)"></a>参考代码(Demo code)</h4><p><a href="https://github.com/presentzeng/forex">https://github.com/presentzeng/forex</a></p><h4 id="参考文档-Demo-code"><a href="#参考文档-Demo-code" class="headerlink" title="参考文档(Demo code)"></a>参考文档(Demo code)</h4><p><a href="https://www.mql5.com/zh/docs/constants/structures/mqltraderequest">https://www.mql5.com/zh/docs/constants/structures/mqltraderequest</a></p>]]></content>
    
    
      
      
    <summary type="html">&lt;p&gt;&lt;strong&gt;外汇投机指南&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Editing&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4 id=&quot;为什么选外汇&quot;&gt;&lt;a href=&quot;#为什么选外汇&quot; class=&quot;headerlink&quot; title=&quot;为什么选外汇&quot;</summary>
      
    
    
    
    
    <category term="Fun" scheme="https://presentzeng.github.io/tags/Fun/"/>
    
  </entry>
  
  <entry>
    <title>6S081 Study Notes(ENGLISH)</title>
    <link href="https://presentzeng.github.io/2022/02/01/6S081/"/>
    <id>https://presentzeng.github.io/2022/02/01/6S081/</id>
    <published>2022-01-31T16:00:00.000Z</published>
    <updated>2023-01-15T13:37:11.662Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Introduction-amp-amp-Link"><a href="#Introduction-amp-amp-Link" class="headerlink" title="Introduction &amp;&amp; Link:"></a>Introduction &amp;&amp; Link:</h1><p>It is a Note about The MIT course 6.S081. arranged by following it’s lab order<br><a href="https://pdos.csail.mit.edu/6.S081/2021/index.html">https://pdos.csail.mit.edu/6.S081/2021/index.html</a><br><a href="https://pdos.csail.mit.edu/6.S081/2021/labs/util.html">https://pdos.csail.mit.edu/6.S081/2021/labs/util.html</a></p><h1 id="Lab1-Utilities"><a href="#Lab1-Utilities" class="headerlink" title="Lab1 Utilities"></a>Lab1 Utilities</h1><h2 id="Key-Points"><a href="#Key-Points" class="headerlink" title="Key Points"></a>Key Points</h2><ol><li>The kernel uses the hardware protection mechanisms to ensure that each process executing user space can access only its own memory</li><li>Fork creates a new process, called the child process, with exactly the same memory contents as the calling process</li></ol><h3 id="pingpong"><a href="#pingpong" class="headerlink" title="pingpong"></a>pingpong</h3><ul><li><p>what is the difference of two code block</p><figure class="highlight abnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">int fd[<span class="number">2</span>]<span class="comment">;</span></span><br><span class="line"><span class="attribute">pid</span>=fork()<span class="comment">;</span></span><br><span class="line">pipe(fd)<span class="comment">;</span></span><br></pre></td></tr></table></figure><figure class="highlight abnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">pid</span>=fork()<span class="comment">;</span></span><br><span class="line">int fd[<span class="number">2</span>]<span class="comment">;</span></span><br><span class="line">pipe(fd)<span class="comment">;</span></span><br></pre></td></tr></table></figure><p>all have 4 fd genereated<br>but the first one two process’s fd have no connection.<br>and the second one has</p></li><li><p>remember to exit at every child process, otherwise it will execute the code below</p></li></ul><h3 id="xargs"><a href="#xargs" class="headerlink" title="xargs"></a>xargs</h3><ul><li>what is char* a[10]?<blockquote><p>format a =  {char* char* …. char*}</p></blockquote></li><li>exec(command, argv)<blockquote><p>the argv[0] should be not be filled with parameter but with command name</p></blockquote></li><li>there are two types format: “123\n456” and 123 456, how we deal with it.<blockquote><p>we can devide with space of \n and treate “ as one of the char, delete it at the last step</p></blockquote></li><li>we should clear all the uninitial string otherwise we will get random value like “&quot; which will cause a lot of confusion to us<br><img src="/img/1650726024.png" alt="random value"></li><li>if you wanna pass char par[10][10] as a parameter in function you should write prototype like below<blockquote><p>func(char * par[10][10]), that means you should write out the dimension of it</p></blockquote></li><li>what is the difference <blockquote><p>char *par[] = {}; char * par[10] = {}; char *par[10],  the former two are empty array, the last one is the right defination</p></blockquote></li><li>why we should fork and call exec <blockquote><p>exec system call will replace the the calling process, that is why</p></blockquote></li><li>what is the difference between strlen and sizeof<br>pls Do use sizeof with memset</li><li>we can compare char with == but we should compare string by using strcmp</li><li>“” is a string ‘’ is a char</li><li>how to use pointer array properly<figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">char</span> *pass_par[10]; </span><br><span class="line"><span class="keyword">char</span> * <span class="keyword">test</span> = <span class="string">&quot;xargs&quot;</span>;</span><br><span class="line">pass_par[0] = <span class="keyword">test</span>;</span><br><span class="line">printf(<span class="string">&quot;%s\n&quot;</span>, pass_par[0]);</span><br><span class="line"> </span><br></pre></td></tr></table></figure></li></ul><h1 id="Lab2-System-calls"><a href="#Lab2-System-calls" class="headerlink" title="Lab2 System calls"></a>Lab2 System calls</h1><h2 id="Mode"><a href="#Mode" class="headerlink" title="Mode"></a>Mode</h2><blockquote><p>mode</p></blockquote><p>Machine<br>mode is mostly intended for configuring a computer</p><p>supervisor mode is for executing privileged instructions: for example, enabling and disabling interrupts, reading and writing the register that holds the address of a page<br>table</p><p>CPUs provide a special instruction that switches the CPU from user mode<br>to supervisor mode. (RISC-V provides<br>the ecall)</p><p>xv6 is monolithic kernel, with all kernel in one mode</p><blockquote><p>Kernel</p></blockquote><p>the hardware only uses the low 39 bits when looking up virtual addresses in page tables; and<br>xv6 only uses 38 of those 39 bits. Thus, the maximum address is 2<br>38 − 1 = 0x3fffffffff</p><p>Each process has two stacks: a user stack and a kernel stack</p><p>In Entry.S it’s _entry set up a stack so that xv6 can run C code start.c</p><h3 id="System-call-tracing"><a href="#System-call-tracing" class="headerlink" title="System call tracing"></a>System call tracing</h3><p>Preparation:</p><ol><li>read at least twice the instructions</li><li>add print command to check the execute order of each function</li><li>you can check how the kill pass all the argument</li></ol><p>Note:</p><ol><li>The most important part is how to pass the argument to the system call</li><li>To read the document, you will know the argument is stored in which Register, and you can use function argint<br>to “Fetch the nth 32-bit system call argument”</li></ol><h3 id="Sysinfo"><a href="#Sysinfo" class="headerlink" title="Sysinfo"></a>Sysinfo</h3><p>Preparation:</p><ol><li>the trick part about the compilation is pls use sys_sysinfo instead of sys_info</li><li>A process’s most important pieces of kernel state are its page table, its kernel<br>stack, and its run state.</li><li>sysinfotest -&gt; kernel/sysproc.c -&gt; kernel/kalloc*proc</li></ol><p>Note:</p><ol><li>The most important part is to understand the function copyout</li><li>for counting proc, we should caculate all the proc that not in UNUSED mode</li><li>for counting mem, we should times the page size</li></ol><h1 id="Lab3-Page-Table"><a href="#Lab3-Page-Table" class="headerlink" title="Lab3  Page Table"></a>Lab3  Page Table</h1><p>Note:</p><ol><li>VA and PA is translated by map</li><li>MMU won’t store any map</li><li>kernel will write the map value into the satp register</li><li>page is 4 KB in the Risk V, which required</li><li>for satp, it is divided into three pieces 25+27+12, 12 is the byte within the page, index is for the map to translate to physical page</li><li>every process keeps it’s own page table</li><li>Kernel is using “direct mapping”</li></ol><p>The Steps to translate the virtual to physical</p><ol><li>the virtual address is divided into PTE-selector and offset, for example 9+9+9(PTE-selector) + 12(Offset)</li><li>for the first page table it is satp + value(first 9 bit), in it we can get the PPN of next page(it’s physical address)</li></ol><h3 id="concepts-of-each-key-defination"><a href="#concepts-of-each-key-defination" class="headerlink" title="concepts of each key defination"></a>concepts of each key defination</h3><ol><li>pte = page table entries, it is actually an address </li><li>ppn = physical table number</li><li>pagetable is an array string like below<br>[*pte1, *pte2, *pte3 …. *pte512]</li><li> Each PTE contains a 44-bit physical page<br>number (PPN) and some flags</li><li>Sv39 Risc-V only uses bottom 27 bit for virtual address, that means 27 power of 2 is the max number of PTES</li><li>each pte actually has 64 bit(8 byte), but it only uses the bottom 54 bits</li><li>each page table has 512 ptes, in total it is 4096 byte</li><li>*pte = ppn(44) + flag(12), pte is an address it is value is ppn+flag</li><li>physical address is 56 bit<br>10 last physical address(56) = ppn(44) + Virtual offset(12)</li><li>the middle physical(56)= ppn(44)+ empty zero(12)</li><li>every VA will has a unique three level pagetable chain</li><li>walk function will create a link from VA to pte based on one certain first level pagetable</li><li>mappages will store the pa to the last pagetable pte</li></ol><h3 id="rewrite-Walk-in-non-recursive-way"><a href="#rewrite-Walk-in-non-recursive-way" class="headerlink" title="rewrite Walk in non recursive way"></a>rewrite Walk in non recursive way</h3><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">define BIT2OFFSET(level, va)  ((va &gt;&gt; 12) &gt;&gt; level*9) &amp; 0x1ff</span></span><br><span class="line"><span class="meta">#</span><span class="bash">define PTETOPA(pte)  ((pte &gt;&gt; 10) &lt;&lt; <span class="string">12)</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash"><span class="string">define PATOPTE(pa) (((uint64)pa &gt;&gt; 12</span>) &lt;&lt; <span class="string">10)</span></span></span><br><span class="line">pte_t *</span><br><span class="line">walk(pagetable_t pagetable, uint64 va, int alloc)</span><br><span class="line">&#123;</span><br><span class="line">    if(va &gt; MAXVA)</span><br><span class="line">        panic(&quot;walk&quot;);</span><br><span class="line"></span><br><span class="line">    // get pte from first level page</span><br><span class="line">    uint64 *first_pte = &amp;pagetable[BIT2OFFSET(2, va)];</span><br><span class="line">    // if valid first page</span><br><span class="line">    if((*first_pte &amp; 0x1) != 0) //valid</span><br><span class="line">    &#123;</span><br><span class="line">        //get next pagetable address</span><br><span class="line">        pagetable = (uint64 *)PTETOPA(*first_pte);</span><br><span class="line">        uint64 *second_pte = &amp;pagetable[BIT2OFFSET(1, va)];</span><br><span class="line">        if((*second_pte &amp; 0x1) != 0)</span><br><span class="line">        &#123;</span><br><span class="line">            pagetable = (uint64 *)PTETOPA(*second_pte);</span><br><span class="line">            return &amp;pagetable[PX(0, va)];</span><br><span class="line">        &#125;</span><br><span class="line">        else //second lvl page create</span><br><span class="line">        &#123;</span><br><span class="line">            //uint64 *second_pte = (uint64*)pagetable[BIT2OFFSET(1, va)];</span><br><span class="line">            if(!alloc || ((pagetable = kalloc()) == 0))</span><br><span class="line">            &#123;</span><br><span class="line">               return 0;</span><br><span class="line">            &#125;</span><br><span class="line">            memset(pagetable, 0, PGSIZE);</span><br><span class="line">            *second_pte = PA2PTE(pagetable) | PTE_V;</span><br><span class="line">            // once created, go to next lvl</span><br><span class="line">            pagetable = (uint64 *)PTETOPA(*second_pte);</span><br><span class="line">            //uint64 *third_pte = &amp;pagetable[BIT2OFFSET(0, va)];</span><br><span class="line">            return (pte_t*)PTETOPA(*second_pte);</span><br><span class="line">            //return &amp;pagetable[PX(0, va)];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    else //if first is not valid</span><br><span class="line">    &#123;</span><br><span class="line">        if(!alloc || ((pagetable = kalloc()) == 0))</span><br><span class="line">        &#123;</span><br><span class="line">           return 0;</span><br><span class="line">        &#125;</span><br><span class="line">        else</span><br><span class="line">        &#123;</span><br><span class="line">            memset(pagetable, 0, PGSIZE);</span><br><span class="line">            *first_pte = PA2PTE(pagetable) | PTE_V;</span><br><span class="line">        &#125;</span><br><span class="line">        pagetable = (uint64 *)PTETOPA(*first_pte);</span><br><span class="line">        uint64 *second_pte = &amp;pagetable[BIT2OFFSET(1, va)];</span><br><span class="line">        if((*second_pte &amp; 0x1) != 0)</span><br><span class="line">        &#123;</span><br><span class="line">            pagetable = (uint64 *)PTETOPA(*second_pte);</span><br><span class="line">            //uint64 *third_pte = &amp;pagetable[BIT2OFFSET(0, va)];</span><br><span class="line">            return &amp;pagetable[PX(0, va)];</span><br><span class="line">        &#125;</span><br><span class="line">        else //second lvl page create</span><br><span class="line">        &#123;</span><br><span class="line">            uint64 *second_pte = &amp;pagetable[BIT2OFFSET(1, va)];</span><br><span class="line">            if(!alloc || ((pagetable = kalloc()) == 0))</span><br><span class="line">            &#123;</span><br><span class="line">               return 0;</span><br><span class="line">            &#125;</span><br><span class="line">            else</span><br><span class="line">            &#123;</span><br><span class="line">                memset(pagetable, 0, PGSIZE);</span><br><span class="line">                *second_pte = PA2PTE(pagetable) | PTE_V;</span><br><span class="line">            &#125;</span><br><span class="line">            // once created, go to next lvl</span><br><span class="line">            pagetable = (uint64 *)PTETOPA(*second_pte);</span><br><span class="line">            //uint64 *third_pte = &amp;pagetable[BIT2OFFSET(0, va)];</span><br><span class="line">            return &amp;pagetable[PX(0, va)];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure><h3 id="Free-process’s-memory"><a href="#Free-process’s-memory" class="headerlink" title="Free process’s memory"></a>Free process’s memory</h3><ol><li>it should free it’s page table and the physical memory it refers to</li><li>Is only the leaf pagetable has the PTE_R PTE_W flag?</li></ol><h3 id="Print-a-page-table-Task"><a href="#Print-a-page-table-Task" class="headerlink" title="Print a page table(Task)"></a>Print a page table(Task)</h3><ol><li>pretty easy, we can just copy and modify freewalk to print</li></ol><h3 id="Speed-up-system-calls-Task"><a href="#Speed-up-system-calls-Task" class="headerlink" title="Speed up system calls(Task)"></a>Speed up system calls(Task)</h3><h4 id="preparation"><a href="#preparation" class="headerlink" title="preparation"></a>preparation</h4><ol><li><p>check the user side of ugetpid, to understand how the app gonna use the interface</p><figure class="highlight shell"><figcaption><span>script</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">int ugetpid(void)</span><br><span class="line">&#123;</span><br><span class="line">  struct usyscall *u = (struct usyscall *)USYSCALL;</span><br><span class="line">  return u-&gt;pid;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>the function directly convert the USYSCALL Address to struct usyscall, and return the stored value of pid</p></li><li><p>To do this, we can kalloc a page and set it readable to userspace, Then in Kernel space we can directly store the pid into it</p></li><li><p>USYSCALL is a virtual address</p></li><li><p>mappages’s main purpose is to map va to pa, it use walk() to create a pte for the va, and store the pa into that pte</p></li><li><p>Each process has a separate page table(First Level), and when xv6 switches between processes, it also changes<br>page tables</p></li><li><p>we should create a new pagetable specify for this task, that means each process has two pagetable,<br>one is the original, other is the store pid one</p></li></ol><h4 id="Plan"><a href="#Plan" class="headerlink" title="Plan"></a>Plan</h4><ol><li>you know the Physical Address</li><li>you know the VA address</li><li>you can use mappages to map va to physical</li><li>then you can init this value all done</li></ol><h3 id="A-kernel-page-table-per-process-Task"><a href="#A-kernel-page-table-per-process-Task" class="headerlink" title="A kernel page table per process(Task)"></a>A kernel page table per process(Task)</h3><ol><li>modify struct proc add kernel field</li><li>every User process will keep an extra identical kernel_pagetable</li><li>at before, all the process share the same kernel page table</li></ol><h2 id="Simplify-HARD"><a href="#Simplify-HARD" class="headerlink" title="Simplify (HARD)"></a>Simplify (HARD)</h2><ol><li>this feature should use the User_kernel_page_table</li></ol><blockquote><p>when did it translated</p></blockquote><p>copyout and copyin copy data to and from user virtual addresses provided as<br>system call arguments; they are in vm.c because they need to explicitly translate those addresses in order to find the corresponding physical memory<br>and after the user_kernel_page_table setup, it will translate by hardware</p><blockquote><p>how this Virtual address range?</p></blockquote><p>From 0 to bottom</p><h2 id="Detecting-which-pages-have-been-accessed-HARD"><a href="#Detecting-which-pages-have-been-accessed-HARD" class="headerlink" title="Detecting which pages have been accessed (HARD)"></a>Detecting which pages have been accessed (HARD)</h2><blockquote><p> how to determine PTE_A?</p></blockquote><p>Inside riscv-privileged.pdf<br><img src="/img/6s081/1.png" /></p><blockquote><p>where to set PTE_A</p></blockquote><p>Risc-V done by itself: Each leaf PTE contains an accessed (A) and dirty (D) bit. The A bit indicates the virtual page has<br>been read, written, or fetched from since the last time the A bit was cleared. The D bit indicates<br>the virtual page has been written since the last time the D bit was cleared</p><h1 id="Trap"><a href="#Trap" class="headerlink" title="Trap"></a>Trap</h1><blockquote><p>Three main way to trigger Trap</p></blockquote><p>1.System call, user executes the ecall</p><p>2.Exception</p><p>3.Device interrupt </p><blockquote><p>Trap’s feature</p></blockquote><p>1.transparent</p><p>2.Three distinct case trap: user space / kernel space /timer interrupt</p><blockquote><p>Trap procedure</p></blockquote><p>1.hardware action, Vector Assembly</p><p>2.C Trap handler</p><p>3.system call &amp; drver</p><blockquote><p>Kernel will do instead of CPU</p></blockquote><ol><li>switch to kernel page table</li></ol><p>2.switch to a stack in the kernel</p><ol start="3"><li>save register</li></ol><blockquote><p>What is stvec</p></blockquote><p>The kernel writes the address of its trap handler here; the RISC-V jumps here to<br>handle a trap<br>Without stvec Then a trap could switch to supervisor mode while still running user instructions</p><blockquote><p>What is trampoline</p></blockquote><p>as the name it is, it is a trampline code for user process, it has all the important function such as uservec.<br>and it is stored at every pages</p>]]></content>
    
    
      
      
    <summary type="html">&lt;h1 id=&quot;Introduction-amp-amp-Link&quot;&gt;&lt;a href=&quot;#Introduction-amp-amp-Link&quot; class=&quot;headerlink&quot; title=&quot;Introduction &amp;amp;&amp;amp; Link:&quot;&gt;&lt;/a&gt;Introdu</summary>
      
    
    
    
    
    <category term="Linux" scheme="https://presentzeng.github.io/tags/Linux/"/>
    
    <category term="System" scheme="https://presentzeng.github.io/tags/System/"/>
    
    <category term="OS" scheme="https://presentzeng.github.io/tags/OS/"/>
    
  </entry>
  
  <entry>
    <title>Thread and Process</title>
    <link href="https://presentzeng.github.io/2020/11/19/Thread/"/>
    <id>https://presentzeng.github.io/2020/11/19/Thread/</id>
    <published>2020-11-18T16:00:00.000Z</published>
    <updated>2022-12-03T12:42:08.535Z</updated>
    
    <content type="html"><![CDATA[<blockquote><p>先给结论：网络瓶颈用线程，cpu瓶颈用进程</p></blockquote><h1 id="运行条件"><a href="#运行条件" class="headerlink" title="运行条件"></a>运行条件</h1><p>16核 ubuntu python3+</p><h1 id="闲置情况"><a href="#闲置情况" class="headerlink" title="闲置情况"></a>闲置情况</h1><img src="/img/Thread/1.png" />top-1命令，可以看到16核基本上没负载(us值）<h1 id="Python单核跑满cpu"><a href="#Python单核跑满cpu" class="headerlink" title="Python单核跑满cpu"></a>Python单核跑满cpu</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">lp</span>():</span> </span><br><span class="line">    <span class="keyword">while</span> <span class="number">1</span>: </span><br><span class="line">        a = <span class="number">1</span> </span><br><span class="line">        b = <span class="number">2</span> </span><br><span class="line">        c = b + a </span><br><span class="line">lp()</span><br></pre></td></tr></table></figure><img src="/img/Thread/2.png" />单核代码，占满了cpu5<h1 id="三线程Python"><a href="#三线程Python" class="headerlink" title="三线程Python"></a>三线程Python</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> time </span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">add_num</span>():</span> </span><br><span class="line">    <span class="keyword">while</span> <span class="number">1</span>: </span><br><span class="line">        a = <span class="number">1</span> </span><br><span class="line">        b = <span class="number">2</span> </span><br><span class="line">        c = b + a </span><br><span class="line"> </span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>: </span><br><span class="line">    t1 = Thread(target=add_num) </span><br><span class="line">    t2 = Thread(target=add_num) </span><br><span class="line">    t3 = Thread(target=add_num) </span><br><span class="line">    t3.start() </span><br><span class="line">    t1.start() </span><br><span class="line">    t2.start() </span><br><span class="line">    time.sleep(<span class="number">100</span>)</span><br></pre></td></tr></table></figure><img src="/img/Thread/3.png" />可以看到是0 4 7三个核在负载，而且无法跑满，连50%都跑不到，但是34+37+40 = 111 这时就有个猜想，是否线程本质只占一个cpu<p>试试开一个线程，是可以跑满100的</p><img src="/img/Thread/4.png" /><h1 id="两线程"><a href="#两线程" class="headerlink" title="两线程"></a>两线程</h1><img src="/img/Thread/5.png" /><p>相加为100%</p><h1 id="三进程"><a href="#三进程" class="headerlink" title="三进程"></a>三进程</h1><img src="/img/Thread/6.png" /><p>可以看到是07 11 13三个核在负载，全部跑满100%</p><h1 id="两线程C代码"><a href="#两线程C代码" class="headerlink" title="两线程C代码"></a>两线程C代码</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">#include &lt;stdio.h&gt; </span></span><br><span class="line"><span class="comment">#include &lt;stdlib.h&gt; </span></span><br><span class="line"><span class="comment">#include &lt;pthread.h&gt; </span></span><br><span class="line"> </span><br><span class="line">void print_message_function (void *ptr); </span><br><span class="line"> </span><br><span class="line"><span class="built_in">int</span> main() </span><br><span class="line">&#123; </span><br><span class="line">    <span class="built_in">int</span> tmp1, tmp2; </span><br><span class="line">    void *retval; </span><br><span class="line">    pthread_t thread1, thread2; </span><br><span class="line">    char *message1 = <span class="string">&quot;thread1&quot;</span>; </span><br><span class="line">    char *message2 = <span class="string">&quot;thread2&quot;</span>; </span><br><span class="line"> </span><br><span class="line">    <span class="built_in">int</span> ret_thrd1, ret_thrd2; </span><br><span class="line"> </span><br><span class="line">    ret_thrd1 = pthread_create(&amp;thread1, NULL, (void *)&amp;print_message_function, (void *) message1); </span><br><span class="line">    ret_thrd2 = pthread_create(&amp;thread2, NULL, (void *)&amp;print_message_function, (void *) message2); </span><br><span class="line"> </span><br><span class="line">    // 线程创建成功，返回<span class="number">0</span>,失败返回失败号 </span><br><span class="line">    <span class="keyword">if</span> (ret_thrd1 != <span class="number">0</span>) &#123; </span><br><span class="line">        printf(<span class="string">&quot;线程1创建失败\n&quot;</span>); </span><br><span class="line">    &#125; <span class="keyword">else</span> &#123; </span><br><span class="line">        printf(<span class="string">&quot;线程1创建成功\n&quot;</span>); </span><br><span class="line">    &#125; </span><br><span class="line"> </span><br><span class="line">    <span class="keyword">if</span> (ret_thrd2 != <span class="number">0</span>) &#123; </span><br><span class="line">        printf(<span class="string">&quot;线程2创建失败\n&quot;</span>); </span><br><span class="line">    &#125; <span class="keyword">else</span> &#123; </span><br><span class="line">        printf(<span class="string">&quot;线程2创建成功\n&quot;</span>); </span><br><span class="line">    &#125; </span><br><span class="line"> </span><br><span class="line">    //同样，pthread_join的返回值成功为<span class="number">0</span> </span><br><span class="line">    tmp1 = pthread_join(thread1, &amp;retval); </span><br><span class="line">    printf(<span class="string">&quot;thread1 return value(retval) is %d\n&quot;</span>, (<span class="built_in">int</span>)retval); </span><br><span class="line">    printf(<span class="string">&quot;thread1 return value(tmp) is %d\n&quot;</span>, tmp1); </span><br><span class="line">    <span class="keyword">if</span> (tmp1 != <span class="number">0</span>) &#123; </span><br><span class="line">        printf(<span class="string">&quot;cannot join with thread1\n&quot;</span>); </span><br><span class="line">    &#125; </span><br><span class="line">    printf(<span class="string">&quot;thread1 end\n&quot;</span>); </span><br><span class="line"> </span><br><span class="line">    tmp2 = pthread_join(thread1, &amp;retval); </span><br><span class="line">    printf(<span class="string">&quot;thread2 return value(retval) is %d\n&quot;</span>, (<span class="built_in">int</span>)retval); </span><br><span class="line">    printf(<span class="string">&quot;thread2 return value(tmp) is %d\n&quot;</span>, tmp1); </span><br><span class="line">    <span class="keyword">if</span> (tmp2 != <span class="number">0</span>) &#123; </span><br><span class="line">        printf(<span class="string">&quot;cannot join with thread2\n&quot;</span>); </span><br><span class="line">    &#125; </span><br><span class="line">    printf(<span class="string">&quot;thread2 end\n&quot;</span>); </span><br><span class="line"> </span><br><span class="line">&#125; </span><br><span class="line"> </span><br><span class="line">void print_message_function( void *ptr ) &#123; </span><br><span class="line">    <span class="built_in">int</span> i = <span class="number">0</span>; </span><br><span class="line">    <span class="keyword">while</span>(<span class="number">1</span>) </span><br><span class="line">    &#123; </span><br><span class="line">        i++; </span><br><span class="line">    &#125; </span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure><p>C的多线程没有这个问题</p><p>​<img src="/img/Thread/7.png" /></p><h1 id="现象："><a href="#现象：" class="headerlink" title="现象："></a>现象：</h1><p>python的线程只能用到一个核。</p><h1 id="结论"><a href="#结论" class="headerlink" title="结论"></a>结论</h1><p>个人查资料后的判断结论是：python用到了GIL锁导致python线程只能用到一个核<br>Reference :<br><a href="https://timber.io/blog/multiprocessing-vs-multithreading-in-python-what-you-need-to-know/">https://timber.io/blog/multiprocessing-vs-multithreading-in-python-what-you-need-to-know/</a></p>]]></content>
    
    
      
      
    <summary type="html">&lt;blockquote&gt;
&lt;p&gt;先给结论：网络瓶颈用线程，cpu瓶颈用进程&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1 id=&quot;运行条件&quot;&gt;&lt;a href=&quot;#运行条件&quot; class=&quot;headerlink&quot; title=&quot;运行条件&quot;&gt;&lt;/a&gt;运行条件&lt;/h1&gt;&lt;p&gt;16核 ub</summary>
      
    
    
    
    
    <category term="Common" scheme="https://presentzeng.github.io/tags/Common/"/>
    
  </entry>
  
  <entry>
    <title>Common Weakness</title>
    <link href="https://presentzeng.github.io/2020/02/21/Common_Weakness/"/>
    <id>https://presentzeng.github.io/2020/02/21/Common_Weakness/</id>
    <published>2020-02-20T16:00:00.000Z</published>
    <updated>2022-12-03T12:42:08.532Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Common-Weakness"><a href="#Common-Weakness" class="headerlink" title="Common Weakness"></a>Common Weakness</h1><blockquote><p>Editing</p></blockquote><h3 id="Input-validation"><a href="#Input-validation" class="headerlink" title="Input validation"></a>Input validation</h3><p>If we don’t validate input properly, Hacker can alter control flow,  change the code execution, or get the Important file informations</p><p>e.g:<br><a href="https://cwe.mitre.org/data/definitions/790.html">CWE-790</a><br><a href="https://cwe.mitre.org/data/definitions/116.html">CWE-116</a><br><a href="https://cwe.mitre.org/data/definitions/138.html">CWE-138</a></p><h3 id="Defense"><a href="#Defense" class="headerlink" title="Defense"></a>Defense</h3><ol><li>Consider all the aspect:Value Type /  Length / Format / Expired Time / Effect Scope and so on</li><li>Assume all input is malicious, Only accept the thing that we know, That means we only set the whiltlist</li></ol>]]></content>
    
    
      
      
    <summary type="html">&lt;h1 id=&quot;Common-Weakness&quot;&gt;&lt;a href=&quot;#Common-Weakness&quot; class=&quot;headerlink&quot; title=&quot;Common Weakness&quot;&gt;&lt;/a&gt;Common Weakness&lt;/h1&gt;&lt;blockquote&gt;
&lt;p&gt;Editi</summary>
      
    
    
    
    
    <category term="Security" scheme="https://presentzeng.github.io/tags/Security/"/>
    
  </entry>
  
  <entry>
    <title>Born</title>
    <link href="https://presentzeng.github.io/2000/01/01/Born/"/>
    <id>https://presentzeng.github.io/2000/01/01/Born/</id>
    <published>1999-12-31T16:00:00.000Z</published>
    <updated>2022-12-03T12:42:08.532Z</updated>
    
    <content type="html"><![CDATA[<p><strong>Simple Introduction</strong></p><h4 id="Initial"><a href="#Initial" class="headerlink" title="Initial"></a>Initial</h4><p>“Right at this moment, I was born” – Millennium Bug</p>]]></content>
    
    
      
      
    <summary type="html">&lt;p&gt;&lt;strong&gt;Simple Introduction&lt;/strong&gt;&lt;/p&gt;
&lt;h4 id=&quot;Initial&quot;&gt;&lt;a href=&quot;#Initial&quot; class=&quot;headerlink&quot; title=&quot;Initial&quot;&gt;&lt;/a&gt;Initial&lt;/h4&gt;&lt;p&gt;“Right</summary>
      
    
    
    
    
    <category term="Fun" scheme="https://presentzeng.github.io/tags/Fun/"/>
    
  </entry>
  
</feed>
