summaryrefslogtreecommitdiffstats
path: root/llvm/llvm-soft-perfmon.cpp
blob: a5f9a568bd3fb59c0b60dfe578fcf165c399bea2 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 *  (C) 2010 by Computer System Laboratory, IIS, Academia Sinica, Taiwan.
 *      See COPYRIGHT in top-level directory.
 */

#include <iostream>
#include <sstream>
#include "tracer.h"
#include "utils.h"
#include "llvm.h"
#include "llvm-target.h"
#include "llvm-soft-perfmon.h"


extern LLVMEnv *LLEnv;
extern unsigned ProfileThreshold;
extern unsigned PredictThreshold;

/*
 * Software Performance Monitor (SPM)
 */
void SoftwarePerfmon::ParseProfileMode(std::string &ProfileLevel)
{
    static std::string profile_str[SPM_NUM] = {
        "none", "basic", "trace", "cache", "pass", "hpm", "exit", "hotspot", "all"
    };
    static uint64_t profile_enum[SPM_NUM] = {
        SPM_NONE, SPM_BASIC, SPM_TRACE, SPM_CACHE, SPM_PASS, SPM_HPM,
        SPM_EXIT, SPM_HOTSPOT, SPM_ALL,
    };

    if (ProfileLevel.empty())
        return;

    std::istringstream ss(ProfileLevel);
    std::string token;
    while(getline(ss, token, ',')) {
        for (int i = 0; i != SPM_NUM; ++i) {
            if (token == profile_str[i]) {
                Mode |= profile_enum[i];
                break;
            }
        }
    }
}

void SoftwarePerfmon::printProfile()
{
    if (!isEnabled())
        return;

    if (LLVMEnv::TransMode == TRANS_MODE_NONE ||
        LLVMEnv::TransMode == TRANS_MODE_INVALID)
        return;

    if (LLVMEnv::TransMode == TRANS_MODE_BLOCK)
        printBlockProfile();
    else
        printTraceProfile();
}

void SoftwarePerfmon::printBlockProfile()
{
    LLVMEnv::TransCodeList &TransCode = LLEnv->getTransCode();
    uint32_t GuestSize = 0, GuestICount = 0, HostSize = 0;
    uint64_t TransTime = 0, MaxTime = 0;

    for (auto TC : TransCode) {
        TraceInfo *Trace = TC->Trace;
        TranslationBlock *TB = TC->EntryTB;
        GuestSize += TB->size;
        GuestICount += TB->icount;
        HostSize += TC->Size;
        TransTime += Trace->TransTime;
        if (Trace->TransTime > MaxTime)
            MaxTime = Trace->TransTime;
    }

    auto &OS = DM.debug();
    OS << "\nBlock statistic:\n"
       << "Num of Blocks    : " << TransCode.size() << "\n"
       << "G/H Code Size    : " << GuestSize << "/" << HostSize << "bytes\n"
       << "Guest ICount     : " << GuestICount << "\n"
       << "Translation Time : " << format("%.6f", (double)TransTime * 1e-6)
                                << " seconds (max=" << MaxTime /1000 << " ms)\n";
}

static void printBasic(LLVMEnv::TransCodeList &TransCode)
{
    uint32_t GuestSize = 0, GuestICount = 0, HostSize = 0;
    uint32_t NumBlock = 0, NumLoop = 0, NumExit = 0, NumIndirectBr = 0;
    uint32_t MaxBlock = 0, MaxLoop = 0, MaxExit = 0, MaxIndirectBr = 0;
    uint64_t TransTime = 0, MaxTime = 0;
    unsigned NumTraces = TransCode.size();
    std::map<unsigned, unsigned> LenDist;

    for (auto TC : TransCode) {
        TraceInfo *Trace = TC->Trace;
        TBVec &TBs = Trace->TBs;
        for (unsigned i = 0, e = TBs.size(); i != e; ++i) {
            GuestSize += TBs[i]->size;
            GuestICount += TBs[i]->icount;
        }
        HostSize += TC->Size;

        NumBlock += TBs.size();
        NumLoop += Trace->NumLoop;
        NumExit += Trace->NumExit;
        NumIndirectBr += Trace->NumIndirectBr;
        TransTime += Trace->TransTime;

        if (TBs.size() > MaxBlock)
            MaxBlock = TBs.size();
        if (Trace->NumLoop > MaxLoop)
            MaxLoop = Trace->NumLoop;
        if (Trace->NumExit > MaxExit)
            MaxExit = Trace->NumExit;
        if (Trace->NumIndirectBr > MaxIndirectBr)
            MaxIndirectBr = Trace->NumIndirectBr;
        if (Trace->TransTime > MaxTime)
            MaxTime = Trace->TransTime;
        LenDist[TBs.size()]++;
    }

    auto &OS = DM.debug();
    OS << "Trace statistic:\n"
       << "Num of Traces    : " << NumTraces << "\n"
       << "Profile Thres.   : " << ProfileThreshold << "\n"
       << "Predict Thres.   : " << PredictThreshold << "\n"
       << "G/H Code Size    : " << GuestSize << "/" << HostSize << " bytes\n"
       << "Translation Time : " << format("%.6f", (double)TransTime * 1e-6)
                                << " seconds (max=" << MaxTime /1000 << " ms)\n"
       << "Average # Blocks : " << format("%.1f", (double)NumBlock / NumTraces)
                                << " (max=" << MaxBlock << ")\n"
       << "Average # Loops  : " << format("%.1f", (double)NumLoop / NumTraces)
                                << " (max=" << MaxLoop << ")\n"
       << "Average # Exits  : " << format("%.1f", (double)NumExit / NumTraces)
                                << " (max=" << MaxExit << ")\n"
       << "Average # IBs    : " << format("%.1f", (double)NumIndirectBr / NumTraces)
                                << " (max=" << MaxIndirectBr << ")\n"
       << "Flush Count      : " << LLEnv->getNumFlush() << "\n";

    OS << "Trace length distribution: (1-" << MaxBlock << ")\n    ";
    for (unsigned i = 1; i <= MaxBlock; i++)
        OS << LenDist[i] << " ";
    OS << "\n";
}

static void printTraceExec(LLVMEnv::TransCodeList &TransCode)
{
    unsigned NumThread = 0;
    for (auto next_cpu = first_cpu; next_cpu != nullptr;
         next_cpu = CPU_NEXT(next_cpu))
        NumThread++;

    /* Detailed trace information and runtime counters. */
    auto &OS = DM.debug();
    OS << "----------------------------\n"
       << "Trace execution information:\n";

    unsigned NumTraces = TransCode.size();
    for (unsigned i = 0; i != NumThread; ++i) {
        unsigned TraceUsed = 0;

        OS << ">\n"
           << "Thread " << i << ":\n"
           << "                                   dynamic exec count\n"
           << "  id      pc      #loop:#exit      loop      ibtc      exit\n";
        for (unsigned j = 0; j != NumTraces; ++j) {
            TraceInfo *Trace = TransCode[j]->Trace;
            uint64_t *Counter = Trace->ExecCount[i];
            if (Counter[0] + Counter[1] + Counter[2] == 0)
                continue;
            TraceUsed++;
            OS << format("%4d", j) << ") "
               << format("0x%08" PRIx, Trace->getEntryPC()) << "    "
               << format("%2d", Trace->NumLoop)   << "    "
               << format("%2d", Trace->NumExit)   << "   "
               << format("%8" PRId64, Counter[0]) << "  "
               << format("%8" PRId64, Counter[1]) << "  "
               << format("%8" PRId64, Counter[2]) << "\n";
        }
        OS << "Trace used: " << TraceUsed << "/" << NumTraces <<"\n";
    }
}

static void printHPM()
{
    auto &OS = DM.debug();
    OS << "Num of Insns     : " << SP->NumInsns << "\n"
       << "Num of Loads     : " << SP->NumLoads << "\n"
       << "Num of Stores    : " << SP->NumStores << "\n"
       << "Num of Branches  : " << SP->NumBranches << "\n"
       << "Sample Time      : " << format("%.6f seconds", (double)SP->SampleTime * 1e-6)
       << "\n";
}

static void printHotspot(unsigned &CoverSet,
                         std::vector<std::vector<uint64_t> *> &SampleListVec)
{
    auto &OS = DM.debug();
    auto &TransCode = LLEnv->getTransCode();
    auto &SortedCode = LLEnv->getSortedCode();
    uint64_t BlockCacheStart = (uintptr_t)tcg_ctx_global.code_gen_buffer;
    uint64_t BlockCacheEnd = BlockCacheStart + tcg_ctx_global.code_gen_buffer_size;
    uint64_t TraceCacheStart = (uintptr_t)LLVMEnv::TraceCache;
    uint64_t TraceCacheEnd = TraceCacheStart + LLVMEnv::TraceCacheSize;
    uint64_t TotalSamples = 0;
    uint64_t NumBlockCache = 0, NumTraceCache = 0, NumOther = 0;

    for (auto *L : SampleListVec) {
        for (uint64_t IP : *L) {
            if (IP >= BlockCacheStart && IP < BlockCacheEnd)
                NumBlockCache++;
            else if (IP >= TraceCacheStart && IP < TraceCacheEnd)
                NumTraceCache++;
            else
                NumOther++;

            auto IT = SortedCode.upper_bound(IP);
            if (IT == SortedCode.begin())
                continue;
            auto TC = (--IT)->second;
            if (IP < (uint64_t)TC->Code + TC->Size)
                TC->SampleCount++;;
        }
        delete L;
    }

    TotalSamples = NumBlockCache + NumTraceCache + NumOther;
    if (TotalSamples == 0 || TransCode.empty()) {
        OS << CoverSet << "% CoverSet     : 0\n";
        return;
    }

    /* Print the time breakdown of block cache, trace cache and other. */
    char buf[128] = {'\0'};
    double RatioBlockCache = (double)NumBlockCache * 100 / TotalSamples;
    double RatioTraceCache = (double)NumTraceCache * 100 / TotalSamples;
    sprintf(buf, "block (%.1f%%) trace (%.1f%%) other (%.1f%%)", RatioBlockCache,
            RatioTraceCache, 100.0f - RatioBlockCache - RatioTraceCache);
    OS << "Breakdown        : " << buf << "\n";

    /* Print the amount of traces in the cover set. */
    std::map<TranslatedCode *, unsigned> IndexMap;
    for (unsigned i = 0, e = TransCode.size(); i != e; ++i)
        IndexMap[TransCode[i]] = i;

    LLVMEnv::TransCodeList Covered(TransCode.begin(), TransCode.end());
    std::sort(Covered.begin(), Covered.end(),
            [](const TranslatedCode *a, const TranslatedCode *b) {
                return a->SampleCount > b->SampleCount;
            });

    uint64_t CoverSamples = TotalSamples * CoverSet / 100;
    uint64_t AccuSamples = 0;
    unsigned NumTracesInCoverSet = 0;
    for (TranslatedCode *TC : Covered) {
        if (AccuSamples >= CoverSamples || TC->SampleCount == 0)
            break;
        NumTracesInCoverSet++;
        AccuSamples += TC->SampleCount;
    }

    OS << CoverSet << "% CoverSet     : " << NumTracesInCoverSet << "\n";

    if (NumTracesInCoverSet == 0)
        return;

    /* Print the percentage of time of the traces in the cover set. */
    if (DM.getDebugMode() & DEBUG_IR_OPT) {
        OS << "Traces of CoverSet:\n";
        for (unsigned i = 0; i < NumTracesInCoverSet; ++i) {
            TranslatedCode *TC = Covered[i];
            sprintf(buf, "%4d (%.1f%%): ", IndexMap[TC],
                    (double)TC->SampleCount * 100 / TotalSamples);
            OS << buf;
            int j = 0;
            for (auto *TB: TC->Trace->TBs) {
                std::stringstream ss;
                ss << std::hex << TB->pc;
                OS << (j++ == 0 ? "" : ",") << ss.str();
            }
            OS << "\n";
        }
    } else {
        unsigned top = 10;

        OS << "Percentage of CoverSet (top 10): ";
        if (NumTracesInCoverSet < top)
            top = NumTracesInCoverSet;
        for (unsigned i = 0; i < top; ++i) {
            TranslatedCode *TC = Covered[i];
            sprintf(buf, "%.1f%%", (double)TC->SampleCount * 100 / TotalSamples);
            OS << (i == 0 ? "" : " ") << buf;
        }
        OS << "\n";
    }
}

void SoftwarePerfmon::printTraceProfile()
{
    auto &OS = DM.debug();
    unsigned NumTraces = LLEnv->getTransCode().size();

    OS << "\n";
    if (NumTraces == 0) {
        OS << "Trace statistic:\n"
           << "Num of Traces  : " << NumTraces << "\n\n";
        return;
    }

    /* Static information */
    if (Mode & SPM_BASIC)
        printBasic(LLEnv->getTransCode());
    if (Mode & SPM_EXIT)
        OS << "Num of TraceExit : " << NumTraceExits << "\n";
    if (Mode & SPM_HPM)
        printHPM();
    if (Mode & SPM_HOTSPOT)
        printHotspot(CoverSet, SP->SampleListVec);

    /* Code cache infomation - start address and size */
    if (Mode & SPM_CACHE) {
        size_t BlockSize = (uintptr_t)tcg_ctx_global.code_gen_ptr -
                           (uintptr_t)tcg_ctx_global.code_gen_buffer;
        size_t TraceSize = LLEnv->getMemoryManager()->getCodeSize();

        OS << "-------------------------\n"
           << "Block/Trace Cache information:\n";
        OS << "Block: start=" << tcg_ctx_global.code_gen_buffer
           << " size=" << tcg_ctx_global.code_gen_buffer_size
           << " code=" << format("%8d", BlockSize) << " (ratio="
           << format("%.2f", (double)BlockSize * 100 / tcg_ctx_global.code_gen_buffer_size)
           << "%)\n";
        OS << "Trace: start=" << LLVMEnv::TraceCache
           << " size=" << LLVMEnv::TraceCacheSize
           << " code=" << format("%8d", TraceSize) << " (ratio="
           << format("%.2f", (double)TraceSize * 100 / LLVMEnv::TraceCacheSize)
           << "%)\n\n";
    }

    if (Mode & SPM_TRACE)
        printTraceExec(LLEnv->getTransCode());

    if ((Mode & SPM_PASS) && !ExitFunc.empty()) {
        OS << "\n-------------------------\n"
           << "Pass information:\n";
        for (unsigned i = 0, e = ExitFunc.size(); i != e; ++i)
            (*ExitFunc[i])();
    }
}

/*
 * vim: ts=8 sts=4 sw=4 expandtab
 */

OpenPOWER on IntegriCloud