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(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值