process.cc 6.81 KB
Newer Older
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * Copyright 2021 Max Planck Institute for Software Systems, and
 * National University of Singapore
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

25
26
#include "trace/process.h"

27
28
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
Antoine Kaufmann's avatar
Antoine Kaufmann committed
29
#include <iostream>
30
#include <memory>
31
#include <vector>
Antoine Kaufmann's avatar
Antoine Kaufmann committed
32

Antoine Kaufmann's avatar
Antoine Kaufmann committed
33
34
#include "trace/events.h"
#include "trace/parser.h"
Antoine Kaufmann's avatar
Antoine Kaufmann committed
35

36
37
38
39
40
template <typename T>
struct event_pair_cmp {
  bool operator()(const std::pair<T, std::shared_ptr<event>> l,
                  const std::pair<T, std::shared_ptr<event>> r) const {
    return l.second->ts < r.second->ts;
41
  }
42
43
};

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/** merge multiple event streams into one ordered by timestamp */
void MergeEvents(coro_t::push_type &sink,
                 std::set<coro_t::pull_type *> &all_parsers) {
  typedef std::pair<coro_t::pull_type *, std::shared_ptr<event>> itpair_t;

  // create set of pairs of source and next event, ordered by timestamp of next
  // event.
  std::set<itpair_t, event_pair_cmp<coro_t::pull_type *>> active;

  // initially populate the set
  for (auto p : all_parsers) {
    if (*p) {
      auto ev = p->get();
      (*p)();
      active.insert(std::make_pair(p, ev));
    }
  }

  // iterate until there are no more active sources
  while (!active.empty()) {
    // grab event with lowest timestamp
    auto i = active.begin();
    itpair_t p = *i;
    active.erase(i);

    // emit event
    sink(p.second);

    // check if there is another event in the source, if so, re-enqueue
    if (*p.first) {
      auto ev = p.first->get();
      (*p.first)();
      active.insert(std::make_pair(p.first, ev));
    }
  }
}

void Printer(coro_t::pull_type &source) {
  uint64_t ts_off = 0;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
83
  for (auto ev : source) {
84
    std::shared_ptr<EHostCall> hc;
85
86
87
88
    std::shared_ptr<EHostInstr> hi;
    if ((hi = std::dynamic_pointer_cast<EHostInstr>(ev))) {
      continue;
    } else if ((hc = std::dynamic_pointer_cast<EHostCall>(ev)) &&
Antoine Kaufmann's avatar
Antoine Kaufmann committed
89
               strcmp(ev->source->label, "C") && hc->fun == "__sys_sendto") {
90
91
92
93
94
95
96
97
98
99
100
101
      std::cout << "---------- REQ START:" << ev->ts << std::endl;
      ts_off = ev->ts;
    }

    std::cout << ev->source->label << " ";

    ev->ts -= ts_off;
    ev->ts /= 1000;
    ev->dump(std::cout);
  }
}

102
void Consumer(coro_t::pull_type &source) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
103
  for (auto ev : source) {
104
105
106
107
108
109
110
111
112
  }
}

struct InstStatsData {
  const char *label;
  uint64_t nInsts;
  uint64_t tMin;
  uint64_t tMax;
  uint64_t tMean;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
113
  std::vector<uint64_t> tAll;
114
115
116
};

void InstStats(coro_t::push_type &sink, coro_t::pull_type &source,
Antoine Kaufmann's avatar
Antoine Kaufmann committed
117
               struct InstStatsData &data) {
118
119
120
  uint64_t last_ts = 0;
  uint64_t first_ts = 0;
  data.nInsts = 0;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
121
  for (auto ev : source) {
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    std::shared_ptr<EHostInstr> hi;
    if ((hi = std::dynamic_pointer_cast<EHostInstr>(ev))) {
      if (!last_ts) {
        // first instruction
        first_ts = hi->ts;
        data.tMin = UINT64_MAX;
        data.tMax = 0;
      } else {
        uint64_t lat = hi->ts - last_ts;

        data.nInsts++;
        data.tAll.push_back(lat);
        if (lat < data.tMin)
          data.tMin = lat;
        if (lat > data.tMax)
          data.tMax = lat;

        /*if (lat > 4000)
          std::cout << "ILAT: " << lat  << " " << std::hex << hi->pc <<
              std::dec << "  " << hi->ts << std::endl;*/
Antoine Kaufmann's avatar
Antoine Kaufmann committed
142
      }
143
      last_ts = hi->ts;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
144
      // last_pc = hi->pc;
145
146
147
148
149
150
151
152
153
    }
    sink(ev);
  }

  if (data.nInsts != 0)
    data.tMean = (last_ts - first_ts) / data.nInsts;
  else
    data.tMean = 0;
}
154

155
int main(int argc, char *argv[]) {
156
157
  if (argc != 5) {
    std::cerr << "Usage: process CLIENT_HLOG CLIENT_NLOG SERVER_HLOG "
Antoine Kaufmann's avatar
Antoine Kaufmann committed
158
159
                 "SERVER_CLOG"
              << std::endl;
160
161
162
    return 1;
  }

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  sym_map syms;
  syms.add_filter("entry_SYSCALL_64");
  syms.add_filter("__do_sys_gettimeofday");
  syms.add_filter("__sys_sendto");
  syms.add_filter("i40e_lan_xmit_frame");
  syms.add_filter("syscall_return_via_sysret");
  syms.add_filter("__sys_recvfrom");
  syms.add_filter("deactivate_task");
  syms.add_filter("interrupt_entry");
  syms.add_filter("i40e_msix_clean_rings");
  syms.add_filter("napi_schedule_prep");
  syms.add_filter("__do_softirq");
  syms.add_filter("trace_napi_poll");
  syms.add_filter("net_rx_action");
  syms.add_filter("i40e_napi_poll");
  syms.add_filter("activate_task");
  syms.add_filter("copyout");
180

181
182
183
  syms.load_file("linux.dump", 0);
  syms.load_file("i40e.dump", 0xffffffffa0000000ULL);
  std::cerr << "map loaded" << std::endl;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
184

185
  std::set<log_parser *> all_parsers;
186

187
  gem5_parser ch(syms);
188
189
190
191
  ch.open(argv[1]);
  ch.label = "C";
  all_parsers.insert(&ch);

192
  gem5_parser sh(syms);
193
194
195
196
  sh.open(argv[3]);
  sh.label = "S";
  all_parsers.insert(&sh);

197
198
  nicbm_parser cn;
  cn.open(argv[2]);
199
  cn.label = "C";
200
  all_parsers.insert(&cn);
201
202
203
204

  nicbm_parser sn;
  sn.open(argv[4]);
  sn.label = "S";
205
  all_parsers.insert(&sn);
206

207
  std::cerr << "Opened all" << std::endl;
208

209
  std::set<coro_t::pull_type *> sources;
210
  std::set<InstStatsData *> isds;
211
  for (auto p : all_parsers) {
212
    coro_t::pull_type *src = new coro_t::pull_type(
Antoine Kaufmann's avatar
Antoine Kaufmann committed
213
214
215
        boost::bind(&log_parser::read_coro, boost::ref(*p), _1));
    InstStatsData *isd = new InstStatsData;
    isd->label = p->label;
216
    coro_t::pull_type *istat = new coro_t::pull_type(
Antoine Kaufmann's avatar
Antoine Kaufmann committed
217
        boost::bind(InstStats, _1, boost::ref(*src), boost::ref(*isd)));
218
219
    sources.insert(istat);
    isds.insert(isd);
220
  }
221

222
  coro_t::pull_type merged(boost::bind(MergeEvents, _1, boost::ref(sources)));
223
224
225
226
227
228
229
230
231
232
233
234
235
  Consumer(merged);

  std::cout << "Stats:" << std::endl;
  for (auto isd : isds) {
    if (!isd->nInsts)
      continue;

    std::sort(isd->tAll.begin(), isd->tAll.end());

    std::cout << " - " << isd->label << std::endl;
    std::cout << "    Instrs: " << isd->nInsts << std::endl;
    std::cout << "    Mean instr time: " << isd->tMean << std::endl;
    for (int i = 0; i <= 100; i += 1)
Antoine Kaufmann's avatar
Antoine Kaufmann committed
236
237
238
      std::cout << "    P[" << i << "] instr time: "
                << isd->tAll[isd->tAll.size() * i / 100 - (i == 100 ? 1 : 0)]
                << std::endl;
239
  }
Antoine Kaufmann's avatar
Antoine Kaufmann committed
240
}