summaryrefslogtreecommitdiff
path: root/rts/ProfilerReportJson.c
blob: 71a557b8de52d73cdc5f62189bded467513aa3dd (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
/* -----------------------------------------------------------------------------
 *
 * (c) The GHC Team, 1998-2017
 *
 * Generating cost-centre profiler JSON report
 *
 * ---------------------------------------------------------------------------*/

#if defined(PROFILING)

#include "rts/PosixSource.h"
#include "Rts.h"

#include "RtsUtils.h"
#include "ProfilerReportJson.h"
#include "Profiling.h"

#include <string.h>

// I don't think this code is all that perf critical.
// So we just allocate a new buffer each time around.
static void escapeString(char const* str, char **buf)
{
    char *out;
    size_t req_size; //Max required size for decoding.
    size_t in_size;  //Input size, including zero.

    in_size = strlen(str) + 1;
    // The strings are generally small and short
    // lived so should be ok to just double the size.
    req_size = in_size * 2;
    out = stgMallocBytes(req_size, "writeCCSReportJson");
    *buf = out;
    // We provide an outputbuffer twice the size of the input,
    // and at worse double the output size. So we can skip
    // length checks.
    for (; *str != '\0'; str++) {
        char c = *str;
        if (c == '\\') {
            *out = '\\'; out++;
            *out = '\\'; out++;
        } else if (c == '\n') {
            *out = '\\'; out++;
            *out = 'n';  out++;
        } else {
            *out = c; out++;
        }
    }
    *out = '\0';
}

static void
logCostCentres(FILE *prof_file)
{
    char* lbl;
    char* src_loc;
    bool needs_comma = false;
    fprintf(prof_file, "[\n");
    for (CostCentre *cc = CC_LIST; cc != NULL; cc = cc->link) {
        escapeString(cc->label, &lbl);
        escapeString(cc->srcloc, &src_loc);
        fprintf(prof_file,
                "%s"
                "{\"id\": %" FMT_Int ", "
                "\"label\": \"%s\", "
                "\"module\": \"%s\", "
                "\"src_loc\": \"%s\", "
                "\"is_caf\": %s}",
                needs_comma ? ", " : "",
                cc->ccID, lbl, cc->module, src_loc,
                cc->is_caf ? "true" : "false");
        needs_comma = true;
    }
    fprintf(prof_file, "]\n");
    stgFree(lbl);
    stgFree(src_loc);
}

static void
logCostCentreStack(FILE *prof_file, CostCentreStack const *ccs)
{
    fprintf(prof_file,
            "{\"id\": %" FMT_Int ", "
            "\"entries\": %" FMT_Word64 ", "
            "\"alloc\": %" FMT_Word64 ", "
            "\"ticks\": %" FMT_Word ", ",
            ccs->cc->ccID,
            ccs->scc_count,
            ccs->mem_alloc * sizeof(W_),
            ccs->time_ticks);

    bool need_comma = false;
    fprintf(prof_file, "\"children\": [");
    for (IndexTable *i = ccs->indexTable; i != 0; i = i->next) {
        if (!i->back_edge) {
            if (need_comma) {
                fprintf(prof_file, ",");
            }
            logCostCentreStack(prof_file, i->ccs);
            need_comma = true;
        }
    }
    fprintf(prof_file, "]}\n");
}

void
writeCCSReportJson(FILE *prof_file,
                   CostCentreStack const *stack,
                   ProfilerTotals totals)
{

    fprintf(prof_file, "{\n\"program\": \"%s\",\n", prog_name);
    fprintf(prof_file, "\"arguments\": [");
    for (int count = 0; prog_argv[count]; count++) {
        char* arg;
        escapeString(prog_argv[count], &arg);
        fprintf(prof_file, "%s\"%s\"",
                count == 0 ? "" : ", ", arg);
        stgFree(arg);
    }
    fprintf(prof_file, "],\n\"rts_arguments\": [");
    for (int count = 0; rts_argv[count]; count++) {
        char* arg;
        escapeString(rts_argv[count], &arg);
        fprintf(prof_file, "%s\"%s\"",
                count == 0 ? "" : ", ", arg);
        stgFree(arg);
    }
    fprintf(prof_file, "],\n");

    fprintf(prof_file, "\"end_time\": \"%s\",\n", time_str());
    fprintf(prof_file, "\"initial_capabilities\": %d,\n",
            RtsFlags.ParFlags.nCapabilities);
    fprintf(prof_file, "\"total_time\": %11.2f,\n",
            ((double) totals.total_prof_ticks *
             (double) RtsFlags.MiscFlags.tickInterval) / (TIME_RESOLUTION * n_capabilities));
    fprintf(prof_file, "\"total_ticks\": %lu,\n",
            (unsigned long) totals.total_prof_ticks);
    fprintf(prof_file, "\"tick_interval\": %d,\n",
            (int) TimeToUS(RtsFlags.MiscFlags.tickInterval));
    fprintf(prof_file, "\"total_alloc\":%" FMT_Word64 ",\n",
            totals.total_alloc * sizeof(W_));

    fprintf(prof_file, "\"cost_centres\": ");
    logCostCentres(prof_file);
    fprintf(prof_file, ",\n\"profile\": ");
    logCostCentreStack(prof_file, stack);
    fprintf(prof_file, "}\n");

}


#endif /* PROFILING */