summaryrefslogtreecommitdiff
path: root/common/JackEngineProfiling.cpp
blob: f70cc7e4ef5f7915675b8cd6311184e5e5dc0934 (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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
Copyright (C) 2008 Grame & RTL

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

#include "JackEngineProfiling.h"
#include "JackGraphManager.h"
#include "JackClientControl.h"
#include "JackEngineControl.h"
#include "JackClientInterface.h"
#include "JackGlobals.h"
#include "JackTime.h"

#include <iostream>
#include <fstream>

namespace Jack
{

JackEngineProfiling::JackEngineProfiling():fAudioCycle(0),fMeasuredClient(0)
{
    jack_info("Engine profiling activated, beware %ld MBytes are needed to record profiling points...", sizeof(fProfileTable) / (1024 * 1024));

    // Force memory page in
    memset(fProfileTable, 0, sizeof(fProfileTable));
}

JackEngineProfiling::~JackEngineProfiling()
{
    std::ofstream fStream("JackEngineProfiling.log", std::ios_base::ate);
    jack_info("Write server and clients timing data...");

    if (!fStream.is_open()) {
        jack_error("JackEngineProfiling::Save cannot open JackEngineProfiling.log file");
    } else {

        // For each measured point
        for (int i = 2; i < TIME_POINTS; i++) {

            // Driver timing values
            long d1 = long(fProfileTable[i].fCurCycleBegin - fProfileTable[i - 1].fCurCycleBegin);
            long d2 = long(fProfileTable[i].fPrevCycleEnd - fProfileTable[i - 1].fCurCycleBegin);

            if (d1 <= 0 || fProfileTable[i].fAudioCycle <= 0)
                continue; // Skip non valid cycles

            // Print driver delta and end cycle
            fStream << d1 << "\t" << d2 << "\t";

            // For each measured client
            for (unsigned int j = 0; j < fMeasuredClient; j++) {

                int ref = fIntervalTable[j].fRefNum;

                // Is valid client cycle
                if (fProfileTable[i].fClientTable[ref].fStatus != NotTriggered) {

                    long d5 = long(fProfileTable[i].fClientTable[ref].fSignaledAt - fProfileTable[i - 1].fCurCycleBegin);
                    long d6 = long(fProfileTable[i].fClientTable[ref].fAwakeAt - fProfileTable[i - 1].fCurCycleBegin);
                    long d7 = long(fProfileTable[i].fClientTable[ref].fFinishedAt - fProfileTable[i - 1].fCurCycleBegin);

                     fStream << ref << "\t" ;
                     fStream << ((d5 > 0) ? d5 : 0) << "\t";
                     fStream << ((d6 > 0) ? d6 : 0) << "\t" ;
                     fStream << ((d7 > 0) ? d7 : 0) << "\t";
                     fStream << ((d6 > 0 && d5 > 0) ? (d6 - d5) : 0) << "\t" ;
                     fStream << ((d7 > 0 && d6 > 0) ? (d7 - d6) : 0) << "\t" ;
                     fStream << fProfileTable[i].fClientTable[ref].fStatus << "\t" ;;

                } else { // Print tabs
                     fStream <<  "\t  \t  \t  \t  \t  \t \t";
                }
            }

            // Terminate line
            fStream << std::endl;
        }
    }

    // Driver period
     std::ofstream fStream1("Timing1.plot", std::ios_base::ate);

    if (!fStream1.is_open()) {
        jack_error("JackEngineProfiling::Save cannot open Timing1.plot file");
    } else {

        fStream1 << "set grid\n";
        fStream1 <<  "set title \"Audio driver timing\"\n";
        fStream1 <<  "set xlabel \"audio cycles\"\n";
        fStream1 <<  "set ylabel \"usec\"\n";
        fStream1 <<  "plot \"JackEngineProfiling.log\" using 1 title \"Audio period\" with lines \n";

        fStream1 <<  "set output 'Timing1.svg\n";
        fStream1 <<  "set terminal svg\n";

        fStream1 <<  "set grid\n";
        fStream1 <<  "set title \"Audio driver timing\"\n";
        fStream1 <<  "set xlabel \"audio cycles\"\n";
        fStream1 <<  "set ylabel \"usec\"\n";
        fStream1 <<  "plot \"JackEngineProfiling.log\" using 1 title \"Audio period\" with lines \n";
        fStream1 <<  "unset output\n";
    }

    // Driver end date
    std::ofstream fStream2("Timing2.plot", std::ios_base::ate);

    if (!fStream2.is_open()) {
        jack_error("JackEngineProfiling::Save cannot open Timing2.plot file");
    } else {

        fStream2 << "set grid\n";
        fStream2 <<  "set title \"Driver end date\"\n";
        fStream2 <<  "set xlabel \"audio cycles\"\n";
        fStream2 <<  "set ylabel \"usec\"\n";
        fStream2 <<  "plot  \"JackEngineProfiling.log\" using 2 title \"Driver end date\" with lines \n";

        fStream2 <<  "set output 'Timing2.svg\n";
        fStream2 <<  "set terminal svg\n";

        fStream2 <<  "set grid\n";
        fStream2 <<  "set title \"Driver end date\"\n";
        fStream2 <<  "set xlabel \"audio cycles\"\n";
        fStream2 <<  "set ylabel \"usec\"\n";
        fStream2 <<  "plot  \"JackEngineProfiling.log\" using 2 title \"Driver end date\" with lines \n";
        fStream2 <<  "unset output\n";
    }

    // Clients end date
    if (fMeasuredClient > 0) {
        std::ofstream fStream3("Timing3.plot", std::ios_base::ate);

        if (!fStream3.is_open()) {
            jack_error("JackEngineProfiling::Save cannot open Timing3.plot file");
        } else {

            fStream3 << "set multiplot\n";
            fStream3 << "set grid\n";
            fStream3 << "set title \"Clients end date\"\n";
            fStream3 << "set xlabel \"audio cycles\"\n";
            fStream3 << "set ylabel \"usec\"\n";
            fStream3 << "plot ";
            for (unsigned int i = 0; i < fMeasuredClient; i++) {
                if (i == 0) {
                    if (i + 1 == fMeasuredClient) { // Last client
                        fStream3 << "\"JackEngineProfiling.log\" using 1 title \"Audio period\" with lines,\"JackEngineProfiling.log\" using ";
                        fStream3 <<  ((i + 1) * 7) - 1;
                        fStream3 << " title \"" << fIntervalTable[i].fName << "\"with lines";
                     } else {
                        fStream3 << "\"JackEngineProfiling.log\" using 1 title \"Audio period\" with lines,\"JackEngineProfiling.log\" using ";
                        fStream3 <<  ((i + 1) * 7) - 1;
                        fStream3 << " title \"" << fIntervalTable[i].fName << "\"with lines,";
                    }
                } else if (i + 1 == fMeasuredClient) { // Last client
                    fStream3 << "\"JackEngineProfiling.log\" using " << ((i + 1) * 7) - 1  << " title \"" << fIntervalTable[i].fName << "\" with lines";
                } else {
                    fStream3 << "\"JackEngineProfiling.log\" using " << ((i + 1) * 7) - 1  << " title \"" << fIntervalTable[i].fName << "\" with lines,";
                }
            }

            fStream3 << "\n unset multiplot\n";
            fStream3 << "set output 'Timing3.svg\n";
            fStream3 << "set terminal svg\n";

            fStream3 << "set multiplot\n";
            fStream3 << "set grid\n";
            fStream3 << "set title \"Clients end date\"\n";
            fStream3 << "set xlabel \"audio cycles\"\n";
            fStream3 << "set ylabel \"usec\"\n";
            fStream3 << "plot ";
            for (unsigned int i = 0; i < fMeasuredClient; i++) {
                if (i == 0) {
                    if ((i + 1) == fMeasuredClient) { // Last client
                        fStream3 << "\"JackEngineProfiling.log\" using 1 title \"Audio period\" with lines,\"JackEngineProfiling.log\" using ";
                        fStream3 <<  ((i + 1) * 7) - 1;
                        fStream3 << " title \"" << fIntervalTable[i].fName << "\"with lines";
                    } else {
                        fStream3 << "\"JackEngineProfiling.log\" using 1 title \"Audio period\" with lines,\"JackEngineProfiling.log\" using ";
                        fStream3 <<  ((i + 1) * 7) - 1;
                        fStream3 << " title \"" << fIntervalTable[i].fName << "\"with lines,";
                    }
                } else if ((i + 1) == fMeasuredClient) { // Last client
                    fStream3 << "\"JackEngineProfiling.log\" using " << ((i + 1) * 7) - 1  << " title \"" << fIntervalTable[i].fName << "\" with lines";
                } else {
                    fStream3 << "\"JackEngineProfiling.log\" using " << ((i + 1) * 7) - 1  << " title \"" << fIntervalTable[i].fName << "\" with lines,";
                }
            }
            fStream3 << "\nunset multiplot\n";
            fStream3 << "unset output\n";
        }
    }

    // Clients scheduling
    if (fMeasuredClient > 0) {
        std::ofstream fStream4("Timing4.plot", std::ios_base::ate);

        if (!fStream4.is_open()) {
            jack_error("JackEngineProfiling::Save cannot open Timing4.plot file");
        } else {

            fStream4 << "set multiplot\n";
            fStream4 << "set grid\n";
            fStream4 << "set title \"Clients scheduling latency\"\n";
            fStream4 << "set xlabel \"audio cycles\"\n";
            fStream4 << "set ylabel \"usec\"\n";
            fStream4 << "plot ";
            for (unsigned int i = 0; i < fMeasuredClient; i++) {
                if ((i + 1) == fMeasuredClient) { // Last client
                    fStream4 << "\"JackEngineProfiling.log\" using " << ((i + 1) * 7)  << " title \"" << fIntervalTable[i].fName << "\" with lines";
                 } else {
                     fStream4 << "\"JackEngineProfiling.log\" using " << ((i + 1) * 7)  << " title \"" << fIntervalTable[i].fName << "\" with lines,";
                }
            }

            fStream4 << "\n unset multiplot\n";
            fStream4 << "set output 'Timing4.svg\n";
            fStream4 << "set terminal svg\n";

            fStream4 << "set multiplot\n";
            fStream4 << "set grid\n";
            fStream4 << "set title \"Clients scheduling latency\"\n";
            fStream4 << "set xlabel \"audio cycles\"\n";
            fStream4 << "set ylabel \"usec\"\n";
            fStream4 << "plot ";
            for (unsigned int i = 0; i < fMeasuredClient; i++) {
                if ((i + 1) == fMeasuredClient) { // Last client
                    fStream4 << "\"JackEngineProfiling.log\" using " << ((i + 1) * 7)  << " title \"" << fIntervalTable[i].fName << "\" with lines";
                } else {
                     fStream4 << "\"JackEngineProfiling.log\" using " << ((i + 1) * 7)  << " title \"" << fIntervalTable[i].fName << "\" with lines,";
                }
            }
            fStream4 << "\nunset multiplot\n";
            fStream4 << "unset output\n";
        }
    }

     // Clients duration
    if (fMeasuredClient > 0) {
        std::ofstream fStream5("Timing5.plot", std::ios_base::ate);

        if (!fStream5.is_open()) {
            jack_error("JackEngineProfiling::Save cannot open Timing5.plot file");
        } else {

            fStream5 << "set multiplot\n";
            fStream5 << "set grid\n";
            fStream5 << "set title \"Clients duration\"\n";
            fStream5 << "set xlabel \"audio cycles\"\n";
            fStream5 << "set ylabel \"usec\"\n";
            fStream5 << "plot ";
            for (unsigned int i = 0; i < fMeasuredClient; i++) {
                if ((i + 1) == fMeasuredClient) { // Last client
                    fStream5 << "\"JackEngineProfiling.log\" using " << ((i + 1) * 7) + 1  << " title \"" << fIntervalTable[i].fName << "\" with lines";
                } else {
                    fStream5 << "\"JackEngineProfiling.log\" using " << ((i + 1) * 7) + 1  << " title \"" << fIntervalTable[i].fName << "\" with lines,";
                }
            }

            fStream5 << "\n unset multiplot\n";
            fStream5 << "set output 'Timing5.svg\n";
            fStream5 << "set terminal svg\n";

            fStream5 << "set multiplot\n";
            fStream5 << "set grid\n";
            fStream5 << "set title \"Clients duration\"\n";
            fStream5 << "set xlabel \"audio cycles\"\n";
            fStream5 << "set ylabel \"usec\"\n";
            fStream5 << "plot ";
            for (unsigned int i = 0; i < fMeasuredClient; i++) {
                if ((i + 1) == fMeasuredClient) {// Last client
                    fStream5 << "\"JackEngineProfiling.log\" using " << ((i + 1) * 7) + 1  << " title \"" << fIntervalTable[i].fName << "\" with lines";
                } else {
                    fStream5 << "\"JackEngineProfiling.log\" using " << ((i + 1) * 7) + 1  << " title \"" << fIntervalTable[i].fName << "\" with lines,";
                }
            }
            fStream5 << "\nunset multiplot\n";
            fStream5 << "unset output\n";
        }
    }

    std::ofstream fStream6("Timings.html", std::ios_base::ate);
    if (!fStream6.is_open()) {
        jack_error("JackEngineProfiling::Save cannot open Timings.html file");
    } else {
        fStream6 << "<?xml version='1.0' encoding='utf-8'?>\n";
        fStream6 << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n";
        fStream6 << "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
        fStream6 << "<html xmlns='http://www.w3.org/1999/xhtml' lang='en'>\n";
        fStream6 << "  <head>\n";
        fStream6 << "    <title>JACK engine profiling</title>\n";
        fStream6 << "    <!-- assuming that images are 600px wide -->\n";
        fStream6 << "    <style media='all' type='text/css'>\n";
        fStream6 << "    .center { margin-left:auto ; margin-right: auto; width: 650px; height: 550px }\n";
        fStream6 << "    </style>\n";
        fStream6 << "  </head>\n";
        fStream6 << "  <body>\n";
        fStream6 << "    <h2 style='text-align:center'>JACK engine profiling</h2>\n";
        fStream6 << "    <div class='center'><object class='center' type='image/svg+xml' data='Timing1.svg'>Timing1</object></div>";
        fStream6 << "    <div class='center'><object class='center' type='image/svg+xml' data='Timing2.svg'>Timing2</object></div>";
        fStream6 << "    <div class='center'><object class='center' type='image/svg+xml' data='Timing3.svg'>Timing3</object></div>";
        fStream6 << "    <div class='center'><object class='center' type='image/svg+xml' data='Timing4.svg'>Timing4</object></div>";
        fStream6 << "    <div class='center'><object class='center' type='image/svg+xml' data='Timing5.svg'>Timing5</object></div>";
        fStream6 << "  </body>\n";
        fStream6 << "</html>\n";
    }

    std::ofstream fStream7("generate_timings", std::ios_base::ate);
    if (!fStream7.is_open()) {
        jack_error("JackEngineProfiling::Save cannot open generate_timings file");
    } else {
        fStream7 << "gnuplot -persist Timing1.plot \n";
        fStream7 << "gnuplot -persist Timing2.plot\n";
        fStream7 << "gnuplot -persist Timing3.plot\n";
        fStream7 << "gnuplot -persist Timing4.plot\n";
        fStream7 << "gnuplot -persist Timing5.plot\n";
    }
}

bool JackEngineProfiling::CheckClient(const char* name, int cur_point)
{
    for (int i = 0; i < MEASURED_CLIENTS; i++) {
       if (strcmp(fIntervalTable[i].fName, name) == 0) {
            fIntervalTable[i].fEndInterval = cur_point;
            return true;
        }
    }
    return false;
}

void JackEngineProfiling::Profile(JackClientInterface** table,
                                   JackGraphManager* manager,
                                   jack_time_t period_usecs,
                                   jack_time_t cur_cycle_begin,
                                   jack_time_t prev_cycle_end)
{
    fAudioCycle = (fAudioCycle + 1) % TIME_POINTS;

    // Keeps cycle data
    fProfileTable[fAudioCycle].fPeriodUsecs = period_usecs;
    fProfileTable[fAudioCycle].fCurCycleBegin = cur_cycle_begin;
    fProfileTable[fAudioCycle].fPrevCycleEnd = prev_cycle_end;
    fProfileTable[fAudioCycle].fAudioCycle = fAudioCycle;

    for (int i = GetEngineControl()->fDriverNum; i < CLIENT_NUM; i++) {
        JackClientInterface* client = table[i];
        JackClientTiming* timing = manager->GetClientTiming(i);
        if (client && client->GetClientControl()->fActive && client->GetClientControl()->fCallback[kRealTimeCallback]) {

            if (!CheckClient(client->GetClientControl()->fName, fAudioCycle)) {
                // Keep new measured client
                fIntervalTable[fMeasuredClient].fRefNum = i;
                strcpy(fIntervalTable[fMeasuredClient].fName, client->GetClientControl()->fName);
                fIntervalTable[fMeasuredClient].fBeginInterval = fAudioCycle;
                fIntervalTable[fMeasuredClient].fEndInterval = fAudioCycle;
                fMeasuredClient++;
            }
            fProfileTable[fAudioCycle].fClientTable[i].fRefNum = i;
            fProfileTable[fAudioCycle].fClientTable[i].fSignaledAt = timing->fSignaledAt;
            fProfileTable[fAudioCycle].fClientTable[i].fAwakeAt = timing->fAwakeAt;
            fProfileTable[fAudioCycle].fClientTable[i].fFinishedAt = timing->fFinishedAt;
            fProfileTable[fAudioCycle].fClientTable[i].fStatus = timing->fStatus;
        }
    }
}

JackTimingMeasure* JackEngineProfiling::GetCurMeasure()
{
    return &fProfileTable[fAudioCycle];
}

} // end of namespace