multilate源代码解读0 mutilate.cc

Posted 银灯玉箫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了multilate源代码解读0 mutilate.cc相关的知识,希望对你有一定的参考价值。

mutilate.cc

The main logic for an agent.

The entry is the go Function at line 645.


At line 653, we prepare the options for the agent. The options specify the qps, number of threads and other configuration for the current agent.

complement the content of each thread data with td and ts at line 664.

ts accounts for the strings of servers.

    vector<string> ts[options.threads];


We leverage the pthread_create from pthread library to spawn mulitple threads.


At line 723, pthread_join is used to wait for the completion of threads.
At line 728, for master, we only use one thread.

do_mutilate

At line 769,
void do_mutilate(const vector& servers, options_t& options,
ConnectionStats& stats, bool master)

  struct event_base *base;
  struct evdns_base *evdns;
  struct bufferevent *bev;


We can see that evedns is built on top of the event_base.

Therefore, one thread map to a “do_mutliate” function.

One thread has mulitple connections. Multiple connections share a event_base.
Since a event_base is associated with a socket. Therefore, muliple connections shares a socket.

The following codes locate at Connection.cc.

  bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
  bufferevent_setcb(bev, bev_read_cb, bev_write_cb, bev_event_cb, this); // initialize the callback functions for read or write events
  bufferevent_enable(bev, EV_READ | EV_WRITE);

We can see that, the buffer_event is created from the libevent API bufferevent_socket_new.
Create a new socket bufferevent over an existing socket.

Create the Connection class instances

Choosing the first connection as the lead connection at Line 831.

At line 910,we initialize the event_base with loop_flag.

  int loop_flag =
    (options.blocking || args.blocking_given) ? EVLOOP_ONCE : EVLOOP_NONBLOCK;

No, the event_base_dispatch(base) call is equivalent to event_base_loop(base, 0), which means that it will neither stop after the first batch of events (like event_base_loop with EVLOOP_ONCE does) nor return immediately if there is no event ready (like event_base_loop with EVLOOP_NONBLOCK does).

You may want to read the great book on libevent written by Nick Mathewson : http://www.wangafu.net/~nickm/libevent-book/Ref3_eventloop.html

#### At line 906, we start each connection.

  // state commands
  void start()  drive_write_machine(); 

以上是关于multilate源代码解读0 mutilate.cc的主要内容,如果未能解决你的问题,请参考以下文章

multilate源代码解读3 AgentStats.h ConnectionStats.h LogHistorgramSampler.h 三者联合起来统计latency

Redis:多线程修改同一个Key使用watch+事务(mutil)实现乐观锁

用于 mutils 列的 pyspark udf

在 mutil 组中查找最接近的值

vue源码解读预热-0

Vue3.0 解读