-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wuxianrong
committed
Dec 1, 2023
1 parent
05b603b
commit 6083056
Showing
46 changed files
with
0 additions
and
15,428 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,267 +0,0 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
|
||
|
||
<title>Pika命令耗时统计功能技术文档 | Hexo</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
<meta name="description" content="背景Pika 可以通过慢查询 (Slowlog) 来查询每个命令的执行时间"> | ||
<meta property="og:type" content="article"> | ||
<meta property="og:title" content="Pika命令耗时统计功能技术文档"> | ||
<meta property="og:url" content="http://example.com/2023/07/06/Pika%E5%91%BD%E4%BB%A4%E8%80%97%E6%97%B6%E7%BB%9F%E8%AE%A1%E5%8A%9F%E8%83%BD%E6%8A%80%E6%9C%AF%E6%96%87%E6%A1%A3/index.html"> | ||
<meta property="og:site_name" content="Hexo"> | ||
<meta property="og:description" content="背景Pika 可以通过慢查询 (Slowlog) 来查询每个命令的执行时间"> | ||
<meta property="og:locale" content="en_US"> | ||
<meta property="og:image" content="https://beijing.xstore.qihu.com/geelib-base-online/%E6%88%AA%E5%B1%8F2023-07-0518.16.25_64a542ff15f8a.png"> | ||
<meta property="og:image" content="https://beijing.xstore.qihu.com/geelib-base-online/%E6%88%AA%E5%B1%8F2023-07-0518.28.48_64a545e64ed75.png"> | ||
<meta property="og:image" content="https://beijing.xstore.qihu.com/geelib-base-online/%E6%88%AA%E5%B1%8F2023-07-0519.24.16_64a552e6699a8.png"> | ||
<meta property="og:image" content="https://beijing.xstore.qihu.com/geelib-base-online/%E6%88%AA%E5%B1%8F2023-07-0519.19.03_64a551abbb23d.png"> | ||
<meta property="og:image" content="https://beijing.xstore.qihu.com/geelib-base-online/%E6%88%AA%E5%B1%8F2023-07-0519.28.14_64a55430184ad.png"> | ||
<meta property="article:published_time" content="2023-07-06T04:40:26.000Z"> | ||
<meta property="article:modified_time" content="2023-08-08T08:43:15.853Z"> | ||
<meta property="article:author" content="John Doe"> | ||
<meta property="article:tag" content="Blog"> | ||
<meta name="twitter:card" content="summary"> | ||
<meta name="twitter:image" content="https://beijing.xstore.qihu.com/geelib-base-online/%E6%88%AA%E5%B1%8F2023-07-0518.16.25_64a542ff15f8a.png"> | ||
|
||
<link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml"> | ||
|
||
|
||
<link rel="shortcut icon" href="/favicon.png"> | ||
|
||
|
||
|
||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/index.min.css"> | ||
|
||
|
||
|
||
<link rel="stylesheet" href="/css/style.css"> | ||
|
||
|
||
|
||
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css"> | ||
|
||
|
||
<meta name="generator" content="Hexo 6.3.0"></head> | ||
|
||
<body> | ||
<div id="container"> | ||
<div id="wrap"> | ||
<header id="header"> | ||
<div id="banner"></div> | ||
<div id="header-outer" class="outer"> | ||
<div id="header-title" class="inner"> | ||
<h1 id="logo-wrap"> | ||
<a href="/" id="logo">Hexo</a> | ||
</h1> | ||
|
||
</div> | ||
<div id="header-inner" class="inner"> | ||
<nav id="main-nav"> | ||
<a id="main-nav-toggle" class="nav-icon"></a> | ||
|
||
<a class="main-nav-link" href="/">Home</a> | ||
|
||
<a class="main-nav-link" href="/archives">Archives</a> | ||
|
||
</nav> | ||
<nav id="sub-nav"> | ||
|
||
<a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a> | ||
|
||
<a id="nav-search-btn" class="nav-icon" title="Search"></a> | ||
</nav> | ||
<div id="search-form-wrap"> | ||
<form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit"></button><input type="hidden" name="sitesearch" value="http://example.com"></form> | ||
</div> | ||
</div> | ||
</div> | ||
</header> | ||
|
||
<div class="outer"> | ||
<section id="main"><article id="post-Pika命令耗时统计功能技术文档" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting"> | ||
<div class="article-meta"> | ||
<a href="/2023/07/06/Pika%E5%91%BD%E4%BB%A4%E8%80%97%E6%97%B6%E7%BB%9F%E8%AE%A1%E5%8A%9F%E8%83%BD%E6%8A%80%E6%9C%AF%E6%96%87%E6%A1%A3/" class="article-date"> | ||
<time class="dt-published" datetime="2023-07-06T04:40:26.000Z" itemprop="datePublished">2023-07-06</time> | ||
</a> | ||
|
||
</div> | ||
<div class="article-inner"> | ||
|
||
|
||
<header class="article-header"> | ||
|
||
|
||
<h1 class="p-name article-title" itemprop="headline name"> | ||
Pika命令耗时统计功能技术文档 | ||
</h1> | ||
|
||
|
||
</header> | ||
|
||
<div class="e-content article-entry" itemprop="articleBody"> | ||
|
||
<h3 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h3><p>Pika 可以通过慢查询 (Slowlog) 来查询每个命令的执行时间<span id="more"></span>,不过不能进行统计归类(例如多条set 命令的执行情况会依次展示出来,没有统计功能),并且 Pika 对各个命令的调用次数没有统计功能,只对总命令调用次数有统计功能。原因在于 Pika 没有实现 Redis 的 info Commandstats 命令,Redis 的这个命令可以统计各个命令的耗时情况以及调用次数(以微秒计),Pika 也需要支持这个命令</p> | ||
<p>以下是 Redis 的 Info Commandstats:</p> | ||
<p>其中 cmdstat_set 是命令名称,calls(命令调用次数),usec(命令耗时),usec_per_call(命令平均执行时间)</p> | ||
<p><img src="https://beijing.xstore.qihu.com/geelib-base-online/%E6%88%AA%E5%B1%8F2023-07-0518.16.25_64a542ff15f8a.png" alt="截屏2023-07-05 18.16.25.png"></p> | ||
<h3 id="解决方案"><a href="#解决方案" class="headerlink" title="解决方案"></a>解决方案</h3><p>Pika 目前已经支持慢查询命令只是没有统计的功能,我们可以用慢查询里面的对单个命令耗时统计功能函数,而Pika 所有的命令都装在Cmd_table 里面,我们可以用一个结构体放在 Cmd 基类中,里面封装 calls,usec 这两个变量来实现对命令的统计功能,执行 info Commandstats 命令时我们对相应的 cmd 判断,如果里面的 calls 不为0 则展示其信息,避免对未调用过的命令进行不必要的展示。</p> | ||
<h3 id="关键代码改动"><a href="#关键代码改动" class="headerlink" title="关键代码改动"></a>关键代码改动</h3><h5 id="include-x2F-pika-command-h"><a href="#include-x2F-pika-command-h" class="headerlink" title="include/pika_command.h"></a>include/pika_command.h</h5><p><img src="https://beijing.xstore.qihu.com/geelib-base-online/%E6%88%AA%E5%B1%8F2023-07-0518.28.48_64a545e64ed75.png" alt="截屏2023-07-05 18.28.48.png"></p> | ||
<p>这里用 cmd_count 和 cmd_time_consuming 来统计对各个命令的总耗时和调用次数并初始化为 0,利用原子变量避免多线程安全问题.</p> | ||
<h5 id="src-x2F-pika-client-conn-cc"><a href="#src-x2F-pika-client-conn-cc" class="headerlink" title="src/pika_client_conn.cc"></a>src/pika_client_conn.cc</h5><p><img src="https://beijing.xstore.qihu.com/geelib-base-online/%E6%88%AA%E5%B1%8F2023-07-0519.24.16_64a552e6699a8.png" alt="截屏2023-07-05 19.24.16.png"></p> | ||
<p>这里的 c_ptr->Execute 是命令的执行函数,在执行完这个函数后我们对时间进行统计,这里我用的慢查询那边的函数把结果存在 duration 中,然后从 CmdTable 中给相应的 cmd 的 count 和 time_consuming 修改值,从而实现功能.</p> | ||
<h5 id="src-x2F-pika-admin-cc"><a href="#src-x2F-pika-admin-cc" class="headerlink" title="src/pika_admin.cc"></a>src/pika_admin.cc</h5><p><img src="https://beijing.xstore.qihu.com/geelib-base-online/%E6%88%AA%E5%B1%8F2023-07-0519.19.03_64a551abbb23d.png" alt="截屏2023-07-05 19.19.03.png"></p> | ||
<p>这个是命令本身的实现逻辑,利用 g_pika_cmd_table_manager->GetCmdTable() 返回一个 <code><std::string, std::unique_ptrcmd></code>的指针,遍历它判断每个命令的调用情况是否为 0,是的话跳过不打印,否则打印出相应信息并留小数点后两位。</p> | ||
<h4 id="实现情况"><a href="#实现情况" class="headerlink" title="实现情况"></a>实现情况</h4><p><img src="https://beijing.xstore.qihu.com/geelib-base-online/%E6%88%AA%E5%B1%8F2023-07-0519.28.14_64a55430184ad.png" alt="截屏2023-07-05 19.28.14.png"></p> | ||
<p>我们将慢查询的参数 <code>slowlog-log-slower-than</code> 设置为 0,来对每个命令的耗时进行查询,可见图中两次 set 的耗时分别是 140us 和 444us 两个值相加与我统计出来的 usec 的值 584 一样,平均耗时则为总耗时除以调用次数.</p> | ||
<p>相关 PR: <a target="_blank" rel="noopener" href="https://github.com/OpenAtomFoundation/pika/pull/1660">#1660</a></p> | ||
<p>相关 Issue:<a target="_blank" rel="noopener" href="https://github.com/OpenAtomFoundation/pika/issues/977">#977</a></p> | ||
|
||
|
||
</div> | ||
<footer class="article-footer"> | ||
<a data-url="http://example.com/2023/07/06/Pika%E5%91%BD%E4%BB%A4%E8%80%97%E6%97%B6%E7%BB%9F%E8%AE%A1%E5%8A%9F%E8%83%BD%E6%8A%80%E6%9C%AF%E6%96%87%E6%A1%A3/" data-id="clpm8c0bb000lk5rf0nw8aqze" data-title="Pika命令耗时统计功能技术文档" class="article-share-link">Share</a> | ||
|
||
|
||
|
||
<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Blog/" rel="tag">Blog</a></li></ul> | ||
|
||
</footer> | ||
</div> | ||
|
||
|
||
<nav id="article-nav"> | ||
|
||
<a href="/2023/07/11/Blackwidow%E9%87%8D%E6%9E%84%E5%9B%BE/" id="article-nav-newer" class="article-nav-link-wrap"> | ||
<strong class="article-nav-caption">Newer</strong> | ||
<div class="article-nav-title"> | ||
|
||
Blackwidow代码解析 | ||
|
||
</div> | ||
</a> | ||
|
||
|
||
<a href="/2023/07/05/Pika%E5%91%BD%E4%BB%A4%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90/" id="article-nav-older" class="article-nav-link-wrap"> | ||
<strong class="article-nav-caption">Older</strong> | ||
<div class="article-nav-title">Pika命令处理流程源码分析</div> | ||
</a> | ||
|
||
</nav> | ||
|
||
|
||
</article> | ||
|
||
|
||
</section> | ||
|
||
<aside id="sidebar"> | ||
|
||
|
||
|
||
|
||
|
||
<div class="widget-wrap"> | ||
<h3 class="widget-title">Tags</h3> | ||
<div class="widget"> | ||
<ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Blog/" rel="tag">Blog</a></li></ul> | ||
</div> | ||
</div> | ||
|
||
|
||
|
||
|
||
<div class="widget-wrap"> | ||
<h3 class="widget-title">Tag Cloud</h3> | ||
<div class="widget tagcloud"> | ||
<a href="/tags/Blog/" style="font-size: 10px;">Blog</a> | ||
</div> | ||
</div> | ||
|
||
|
||
|
||
<div class="widget-wrap"> | ||
<h3 class="widget-title">Archives</h3> | ||
<div class="widget"> | ||
<ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/11/">November 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/10/">October 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/09/">September 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/08/">August 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/04/">April 2023</a></li></ul> | ||
</div> | ||
</div> | ||
|
||
|
||
|
||
|
||
<div class="widget-wrap"> | ||
<h3 class="widget-title">Recent Posts</h3> | ||
<div class="widget"> | ||
<ul> | ||
|
||
<li> | ||
<a href="/2023/11/30/Floyd%E5%A4%9Akey%E5%91%BD%E4%BB%A4%E5%9C%A8%E5%B9%B6%E5%8F%91%E5%9C%BA%E6%99%AF%E4%B8%8B%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95/">Floyd的多key命令在并发场景下的解决方案</a> | ||
</li> | ||
|
||
<li> | ||
<a href="/2023/11/27/Floyd%E8%AE%BE%E8%AE%A1%E6%96%B9%E6%A1%88%E8%A7%A3%E6%9E%90/">Pika的Floyd设计方案</a> | ||
</li> | ||
|
||
<li> | ||
<a href="/2023/11/21/Pika%E5%9C%A8K8s%E4%B8%8A%E7%9A%84%E6%89%A9%E5%AE%B9/">Pika在K8s上的扩容</a> | ||
</li> | ||
|
||
<li> | ||
<a href="/2023/10/30/Pika%E7%9A%84CacheStatus%E6%96%B9%E6%A1%88/">Pika的CacheStatus方案</a> | ||
</li> | ||
|
||
<li> | ||
<a href="/2023/10/26/Pika%E7%9A%84Cache%E6%96%B9%E6%A1%88/">Pika的Cache方案</a> | ||
</li> | ||
|
||
</ul> | ||
</div> | ||
</div> | ||
|
||
|
||
</aside> | ||
|
||
</div> | ||
<footer id="footer"> | ||
|
||
<div class="outer"> | ||
<div id="footer-info" class="inner"> | ||
|
||
© 2023 John Doe<br> | ||
Powered by <a href="https://hexo.io/" target="_blank">Hexo</a> | ||
</div> | ||
</div> | ||
</footer> | ||
|
||
</div> | ||
<nav id="mobile-nav"> | ||
|
||
<a href="/" class="mobile-nav-link">Home</a> | ||
|
||
<a href="/archives" class="mobile-nav-link">Archives</a> | ||
|
||
</nav> | ||
|
||
|
||
|
||
<script src="/js/jquery-3.4.1.min.js"></script> | ||
|
||
|
||
|
||
|
||
<script src="/fancybox/jquery.fancybox.min.js"></script> | ||
|
||
|
||
|
||
|
||
<script src="/js/script.js"></script> | ||
|
||
|
||
|
||
|
||
|
||
</div> | ||
</body> | ||
</html> | ||
Oops, something went wrong.