{"id":400,"date":"2024-09-17T10:56:17","date_gmt":"2024-09-17T02:56:17","guid":{"rendered":"https:\/\/cococat.top\/?p=400"},"modified":"2024-09-17T10:56:17","modified_gmt":"2024-09-17T02:56:17","slug":"rust-note-8-random-note","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2024\/09\/17\/rust-note-8-random-note\/","title":{"rendered":"rust \u7b14\u8bb0 8\uff1arust &#038; tokio \u6742\u8bb0"},"content":{"rendered":"<h2>1. \u5728 <code>tokio::main<\/code> \u5b8f\u4e2d\u6307\u5b9a runtime \u7684\u7ebf\u7a0b\u4e2a\u6570<\/h2>\n<p>\u9ed8\u8ba4\u662f\u591a\u7ebf\u7a0b runtime\uff0cworker \u4e2a\u6570\u4e3a CPU \u7684\u6838\u5fc3\u6570\u3002\u53ef\u4ee5\u901a\u8fc7\u5728 <code>tokio::main<\/code> \u4e2d\u589e\u52a0\u5982\u4e0b\u5185\u5bb9\u6765\u5236\u5b9a worker \u4e2a\u6570\uff1a<\/p>\n<pre><code class=\"language-rust\">#[tokio::main(flavor = &quot;multi_thread&quot;, worker_threads = 10)]<\/code><\/pre>\n<p>\u6216\u8005\u5207\u6362\u6210\u5355\u7ebf\u7a0b runtime\uff1a<\/p>\n<pre><code class=\"language-rust\">#[tokio::main(flavor = &quot;current_thread&quot;)]<\/code><\/pre>\n<p>\u5176\u5b9e\u9f20\u6807 hover \u5230 <code>tokio::main<\/code> \u4e0a\u7136\u540e\u8bfb\u6ce8\u91ca\u5c31\u884c\u4e86\uff0c\u8fd9\u4e2a\u5728 tokio \u7684\u6ce8\u91ca\u91cc\u5199\u6e05\u695a\u4e86\u3002<\/p>\n<h2>2. \u5728 release \u7248\u672c\u91cc\u751f\u6210 debuginfo<\/h2>\n<p>\u5728 Cargo.toml \u91cc\u52a0\u4e0a\uff1a<\/p>\n<pre><code class=\"language-toml\">[profile.release]\ndebug = true\nstrip = false<\/code><\/pre>\n<p>\u7b2c\u4e00\u884c\u4f1a\u5728 release \u7248\u672c\u7684\u4e8c\u8fdb\u5236\u91cc\u6dfb\u52a0 debuginfo \uff08\u6587\u4ef6\u540d\u3001\u884c\u6570\u3001\u51fd\u6570\u540d\u7b49\uff09\uff1b\u7b2c\u4e8c\u884c\u4f1a\u5173\u95ed release \u7f16\u8bd1\u65f6\u5bf9\u7b26\u53f7\u7684\u88c1\u526a\u3002<br \/>\n\u4e8c\u8005\u7ed3\u5408\u8d77\u6765\uff0c\u53ef\u4ee5\u8ba9 release \u7248\u672c\u7684\u4e8c\u8fdb\u5236\u7a0b\u5e8f\u66f4\u5bb9\u6613\u901a\u8fc7 gdb \u89c2\u5bdf\u8c03\u7528\u6808\u7b49\u65b9\u5f0f debug\uff0c\u4e5f\u80fd\u591f\u901a\u8fc7 flamegraph \u7b49\u751f\u6210\u706b\u7130\u56fe\u3002<\/p>\n<h3>2.1 \u9898\u5916\u8bdd\u4e4b flamegraph \u4f7f\u7528<\/h3>\n<p><a href=\"https:\/\/github.com\/brendangregg\/FlameGraph\">https:\/\/github.com\/brendangregg\/FlameGraph<\/a><\/p>\n<pre><code class=\"language-bash\">sudo perf record -e cpu-clock -g --call-graph dwarf -p 28591\nperf script -i perf.data &amp;&gt; perf.unfold\n.\/stackcollapse-perf.pl perf.unfold &amp;&gt; perf.folded\n.\/flamegraph.pl perf.folded &gt; perf.svg<\/code><\/pre>\n<p>\u6ce8\u610f\u5982\u679c\u6293\u51fa\u6765\u7684\u6ca1\u6709 debuginfo \u53ef\u80fd\u662f\u56e0\u4e3a\u6ca1\u6709\u5e26 dwarf<\/p>\n<p>\u53c2\u8003\uff1a<br \/>\n<a href=\"https:\/\/www.cnblogs.com\/happyliu\/p\/6142929.html\">https:\/\/www.cnblogs.com\/happyliu\/p\/6142929.html<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/10933408\/how-can-i-get-perf-to-find-symbols-in-my-program\">https:\/\/stackoverflow.com\/questions\/10933408\/how-can-i-get-perf-to-find-symbols-in-my-program<\/a><\/p>\n<h2>3. AsyncRead &amp; AsyncWrite \u7edf\u8ba1\u95ee\u9898<\/h2>\n<p>\u5728\u4e1a\u52a1\u4ee3\u7801\u4e2d\u5bf9 TCP \u505a\u6536\u53d1\u5b57\u8282\u6570\u7684\u7edf\u8ba1\u65f6\uff0c\u4f1a\u53d1\u73b0\u4e00\u4e2a\u6bd4\u8f83\u8be1\u5f02\u7684\u95ee\u9898\uff1a<code>AsyncRead<\/code> \u6ca1\u6709\u8fd4\u56de\u8bfb\u4e86\u591a\u5c11\u5b57\u8282\u7684\u63a5\u53e3\uff0c\u4f46\u662f <code>AsyncWrite<\/code> \u5374\u662f\u539f\u751f\u652f\u6301\u7edf\u8ba1\u7684\u3002<\/p>\n<pre><code class=\"language-rust\">pub trait AsyncWrite {\n    \/\/ \u8fd4\u56de\u7684 Ok(usize) \u5c31\u4ee3\u8868\u6210\u529f\u5199\u5165\u4e86\u591a\u5c11\u5b57\u8282\u5230\u7f13\u51b2\u4e2d\n    fn poll_write(\n        self: Pin&lt;&amp;mut Self&gt;,\n        cx: &amp;mut Context&lt;&#039;_&gt;,\n        buf: &amp;[u8],\n    ) -&gt; Poll&lt;Result&lt;usize, Error&gt;&gt;;\n    \/\/ .....\n}\n\npub trait AsyncRead {\n    fn poll_read(\n        self: Pin&lt;&amp;mut Self&gt;,\n        cx: &amp;mut Context&lt;&#039;_&gt;,\n        buf: &amp;mut ReadBuf&lt;&#039;_&gt;,\n    ) -&gt; Poll&lt;Result&lt;()&gt;&gt;;\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u8bbe\u8ba1\u672c\u8eab\u662f\u5f88\u6b63\u5e38\u7684\uff0c\u6bd5\u7adf <code>poll_read<\/code> \u8981\u6c42\u6211\u4eec\u4f20\u5165\u4e00\u4e2a <code>ReadBuf<\/code> \uff0c<code>ReadBuf<\/code> \u7684\u6240\u6709\u65b9\u81ea\u7136\u53ef\u4ee5\u8fdb\u884c\u8bfb\u53d6\u5b57\u8282\u6570\u7684\u7edf\u8ba1\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u9ad8\u5ea6\u5c01\u88c5\u7684\u5e93\uff0c\u8fd9\u79cd\u7edf\u8ba1\u63a5\u53e3\u5e76\u6ca1\u6709\u66b4\u9732\u51fa\u6765\uff0c\u4f8b\u5982\u5148\u7528 tokio \u521b\u5efa <code>TcpStream<\/code>\uff0c\u7136\u540e\u7528 native-tls \u8fdb\u884c tls \u63e1\u624b\u7684\u573a\u666f\u3002<\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5c31\u53ea\u80fd\u5728 <code>TcpStream<\/code> \u4e0a\u9762\u518d\u5c01\u4e00\u5c42 Buffer \u4e86\uff0c\u9700\u8981\u4e3a\u8fd9\u4e2a\u65b0\u7684 Buffer \u5b9e\u73b0 <code>AsyncRead<\/code> \u548c <code>AsyncWrite<\/code>\u3002\u540e\u8005\u7684\u5b9e\u73b0\u5f88\u7b80\u5355\uff0c\u76f4\u63a5\u8c03\u7528\u5e95\u5c42 <code>TcpStream<\/code> \u7684\u63a5\u53e3\u5373\u53ef\uff1b\u524d\u8005\u7a0d\u5fae\u590d\u6742\u4e9b\uff0c\u9700\u8981\u5728\u8bfb\u5199\u524d\u540e\u5206\u522b\u8c03\u7528 <code>ReadBuf<\/code> \u7684 <code>filled<\/code> \u65b9\u6cd5\uff0c\u505a\u51cf\u6cd5\u6765\u5224\u65ad\u8bfb\u4e86\u591a\u5c11\u5b57\u8282\u3002<\/p>\n<h2>4. \u5bf9\u5f15\u7528\u7684\u6bd4\u8f83\u4e0e <code>std::ptr::eq<\/code><\/h2>\n<p>\u4f7f\u7528 <code>==<\/code> \u8fd0\u7b97\u7b26\uff0c\u5b9e\u9645\u4e0a\u4f1a\u8c03\u7528\u5bf9\u5e94\u7c7b\u578b\u7684 <code>PartialEq<\/code> \u5b9e\u73b0\u3002\u901a\u5e38\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528 <code>#[derive(PartialEq)]<\/code> \u6765\u4e3a\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u679a\u4e3e\u6216\u7ed3\u6784\u5b9e\u73b0 <code>PartialEq<\/code>\uff0c\u800c\u8fd9\u6837\u751f\u6210\u7684\u4ee3\u7801\u903b\u8f91\u4f1a\u662f\u9010\u4e2a\u6bd4\u8f83\u6210\u5458\u7684<strong>\u503c\u662f\u5426\u76f8\u7b49<\/strong>\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u60f3\u8981\u6bd4\u8f83\u4e24\u4e2a\u5f15\u7528\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5b9e\u4f8b\u65f6\uff0c\u53ef\u80fd\u4f1a\u60f3\u8981\u5c1d\u8bd5\u4f7f\u7528 <code>==<\/code> \u8fd0\u7b97\u7b26\u3002\u7136\u800c\u4e8b\u4e0e\u613f\u8fdd\uff0c\u8fd9\u79cd\u64cd\u4f5c\u5b9e\u9645\u4e0a\u4e5f\u4f1a\u53d8\u6210\u4f7f\u7528 <code>PartialEq<\/code> \u6765\u6bd4\u8f83\u5176\u503c\u662f\u5426\u76f8\u7b49\u3002\u4e3a\u4ec0\u4e48\u5462\uff1f\u770b\u770b <code>PartialEq<\/code> \u7684\u6e90\u7801\u5c31\u77e5\u9053\u4e86\uff1a<\/p>\n<pre><code class=\"language-rust\">pub trait PartialEq&lt;Rhs: ?Sized = Self&gt; {\n    \/\/\/ This method tests for `self` and `other` values to be equal, and is used by `==`.\n    #[must_use]\n    #[stable(feature = &quot;rust1&quot;, since = &quot;1.0.0&quot;)]\n    #[rustc_diagnostic_item = &quot;cmp_partialeq_eq&quot;]\n    fn eq(&amp;self, other: &amp;Rhs) -&gt; bool;\n    \/\/\/ \u4e0b\u9762\u7701\u7565\n}<\/code><\/pre>\n<p>\u53ef\u89c1\uff0c<code>eq<\/code> \u65b9\u6cd5\u63a5\u53d7\u7684 <code>self<\/code> \u548c <code>other<\/code> \u672c\u6765\u5c31\u662f\u5f15\u7528\u7c7b\u578b\uff0c\u56e0\u6b64\u5f15\u7528\u7c7b\u578b\u7684 <code>self<\/code> \u6b63\u4e2d\u5176\u4e0b\u6000\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u68c0\u67e5\u4e24\u4e2a\u5f15\u7528\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u573a\u666f\u5e94\u5f53\u4f7f\u7528 <code>std::ptr::eq<\/code> \u3002\u5176\u51fd\u6570\u539f\u578b\u662f\uff1a<\/p>\n<pre><code class=\"language-rust\">pub fn eq&lt;T: ?Sized&gt;(a: *const T, b: *const T) -&gt; bool {\n    a == b\n}<\/code><\/pre>\n<p>\u53ef\u89c1\u5176\u539f\u7406\u662f\u628a\u8fd0\u7b97\u5bf9\u8c61\u5f53\u6210\u4e86\u6307\u9488\u6765\u6bd4\u8f83\uff0c\u56e0\u6b64\u80fd\u591f\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u5728 tokio::main \u5b8f\u4e2d\u6307\u5b9a runtime \u7684\u7ebf\u7a0b\u4e2a\u6570 \u9ed8\u8ba4\u662f\u591a\u7ebf\u7a0b runtime\uff0cwork [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[24,26],"class_list":["post-400","post","type-post","status-publish","format-standard","hentry","category-rust","tag-rust","tag-tokio"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/comments?post=400"}],"version-history":[{"count":0,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/400\/revisions"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}