aboutsummaryrefslogtreecommitdiffstats
path: root/common/report.cc
blob: 98ff14fb7718927023167cc4f2bc33b84cfe9011 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 *  nextpnr -- Next Generation Place and Route
 *
 *  Copyright (C) 2021  gatecat <gatecat@ds0.me>
 *
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include "json11.hpp"
#include "nextpnr.h"

NEXTPNR_NAMESPACE_BEGIN

using namespace json11;

namespace {
dict<IdString, std::pair<int, int>> get_utilization(const Context *ctx)
{
    // Sort by Bel type
    dict<IdString, std::pair<int, int>> result;
    for (auto &cell : ctx->cells) {
        result[ctx->getBelBucketName(ctx->getBelBucketForCellType(cell.second.get()->type))].first++;
    }
    for (auto bel : ctx->getBels()) {
        if (!ctx->getBelHidden(bel)) {
            result[ctx->getBelBucketName(ctx->getBelBucketForBel(bel))].second++;
        }
    }
    return result;
}
} // namespace

static std::string clock_event_name(const Context *ctx, const ClockEvent &e)
{
    std::string value;
    if (e.clock == ctx->id("$async$"))
        value = std::string("<async>");
    else
        value = (e.edge == FALLING_EDGE ? std::string("negedge ") : std::string("posedge ")) + e.clock.str(ctx);
    return value;
};

static Json::array report_critical_paths(const Context *ctx)
{

    auto report_critical_path = [ctx](const CriticalPath &report) {
        Json::array pathJson;

        for (const auto &segment : report.segments) {

            const auto &driver = ctx->cells.at(segment.from.first);
            const auto &sink = ctx->cells.at(segment.to.first);

            auto fromLoc = ctx->getBelLocation(driver->bel);
            auto toLoc = ctx->getBelLocation(sink->bel);

            auto fromJson = Json::object({{"cell", segment.from.first.c_str(ctx)},
                                          {"port", segment.from.second.c_str(ctx)},
                                          {"loc", Json::array({fromLoc.x, fromLoc.y})}});

            auto toJson = Json::object({{"cell", segment.to.first.c_str(ctx)},
                                        {"port", segment.to.second.c_str(ctx)},
                                        {"loc", Json::array({toLoc.x, toLoc.y})}});

            auto segmentJson = Json::object({
                    {"delay", ctx->getDelayNS(segment.delay)},
                    {"from", fromJson},
                    {"to", toJson},
            });

            if (segment.type == CriticalPath::Segment::Type::CLK_TO_Q) {
                segmentJson["type"] = "clk-to-q";
            } else if (segment.type == CriticalPath::Segment::Type::SOURCE) {
                segmentJson["type"] = "source";
            } else if (segment.type == CriticalPath::Segment::Type::LOGIC) {
                segmentJson["type"] = "logic";
            } else if (segment.type == CriticalPath::Segment::Type::SETUP) {
                segmentJson["type"] = "setup";
            } else if (segment.type == CriticalPath::Segment::Type::ROUTING) {
                segmentJson["type"] = "routing";
                segmentJson["net"] = segment.net.c_str(ctx);
                segmentJson["budget"] = ctx->getDelayNS(segment.budget);
            }

            pathJson.push_back(segmentJson);
        }

        return pathJson;
    };

    auto critPathsJson = Json::array();

    // Critical paths
    for (auto &report : ctx->timing_result.clock_paths) {

        critPathsJson.push_back(Json::object({{"from", clock_event_name(ctx, report.second.clock_pair.start)},
                                              {"to", clock_event_name(ctx, report.second.clock_pair.end)},
                                              {"path", report_critical_path(report.second)}}));
    }

    // Cross-domain paths
    for (auto &report : ctx->timing_result.xclock_paths) {
        critPathsJson.push_back(Json::object({{"from", clock_event_name(ctx, report.clock_pair.start)},
                                              {"to", clock_event_name(ctx, report.clock_pair.end)},
                                              {"path", report_critical_path(report)}}));
    }

    return critPathsJson;
}

static Json::array report_detailed_net_timings(const Context *ctx)
{
    auto detailedNetTimingsJson = Json::array();

    // Detailed per-net timing analysis
    for (const auto &it : ctx->timing_result.detailed_net_timings) {

        const NetInfo *net = ctx->nets.at(it.first).get();
        ClockEvent start = it.second[0].clock_pair.start;

        Json::array endpointsJson;
        for (const auto &sink_timing : it.second) {

            // FIXME: Is it possible that there are multiple different start
            // events for a single net? It has a single driver
            NPNR_ASSERT(sink_timing.clock_pair.start == start);

            auto endpointJson = Json::object({{"cell", sink_timing.cell_port.first.c_str(ctx)},
                                              {"port", sink_timing.cell_port.second.c_str(ctx)},
                                              {"event", clock_event_name(ctx, sink_timing.clock_pair.end)},
                                              {"delay", ctx->getDelayNS(sink_timing.delay)},
                                              {"budget", ctx->getDelayNS(sink_timing.budget)}});
            endpointsJson.push_back(endpointJson);
        }

        auto netTimingJson = Json::object({{"net", net->name.c_str(ctx)},
                                           {"driver", net->driver.cell->name.c_str(ctx)},
                                           {"port", net->driver.port.c_str(ctx)},
                                           {"event", clock_event_name(ctx, start)},
                                           {"endpoints", endpointsJson}});

        detailedNetTimingsJson.push_back(netTimingJson);
    }

    return detailedNetTimingsJson;
}

/*
Report JSON structure:

{
  "utilization": {
    <BEL name>: {
      "available": <available count>,
      "used": <used count>
    },
    ...
  },
  "fmax" {
    <clock name>: {
      "achieved": <achieved fmax [MHz]>,
      "constraint": <target fmax [MHz]>
    },
    ...
  },
  "critical_paths": [
    {
      "from": <clock event edge and name>,
      "to": <clock event edge and name>,
      "path": [
        {
          "from": {
            "cell": <driver cell name>
            "port": <driver port name>
            "loc": [
              <grid x>,
              <grid y>
            ]
          },
          "to": {
            "cell": <sink cell name>
            "port": <sink port name>
            "loc": [
              <grid x>,
              <grid y>
            ]
          },
          "type": <path segment type "clk-to-q", "source", "logic", "routing" or "setup">,
          "net": <net name (for routing only!)>,
          "delay": <segment delay [ns]>,
          "budget": <segment delay budget [ns] (for routing only!)>,
        }
        ...
      ]
    },
    ...
  ],
  "detailed_net_timings": [
    {
      "driver": <driving cell name>,
      "port": <driving cell port name>,
      "event": <driver clock event name>,
      "net": <net name>,
      "endpoints": [
        {
          "cell": <sink cell name>,
          "port": <sink cell port name>,
          "event": <destination clock event name>,
          "delay": <delay [ns]>,
          "budget": <delay budget [ns]>,
        }
        ...
      ]
    }
    ...
  ]
}
*/

void Context::writeReport(std::ostream &out) const
{
    auto util = get_utilization(this);
    dict<std::string, Json> util_json;
    for (const auto &kv : util) {
        util_json[kv.first.str(this)] = Json::object{
                {"used", kv.second.first},
                {"available", kv.second.second},
        };
    }
    dict<std::string, Json> fmax_json;
    for (const auto &kv : timing_result.clock_fmax) {
        fmax_json[kv.first.str(this)] = Json::object{
                {"achieved", kv.second.achieved},
                {"constraint", kv.second.constraint},
        };
    }

    Json::object jsonRoot{
            {"utilization", util_json}, {"fmax", fmax_json}, {"critical_paths", report_critical_paths(this)}};

    if (detailed_timing_report) {
        jsonRoot["detailed_net_timings"] = report_detailed_net_timings(this);
    }

    out << Json(jsonRoot).dump() << std::endl;
}

NEXTPNR_NAMESPACE_END