xref: /aosp_15_r20/external/oj-libjdwp/src/share/back/timing.c (revision e82f7db8c62aed3c168547abe4f9f4aeceaebfc7)
1 /*
2  * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 #include "timing.h"
27 
28 #include "error_messages.h"
29 #include "JDWP.h"
30 #include "outStream.h"
31 #include "util.h"
32 
33 // ANDROID-CHANGED: This whole file
34 
35 // This system stores cmd processing timing and sends them to the debugger
36 // to generate profiling stats.
37 
38 typedef struct Timing {
39     jlong start_ns;
40     jlong duration_ns;
41     jint id;
42     jint cmd_set;
43     jint cmd;
44 } Timing;
45 
46 static const jint MAX_TIMINGS = 500;
47 static Timing timings[MAX_TIMINGS];
48 static jint numTimings;
49 
timings_startCmd(jint id,jint cmd_set,jint cmd)50 void timings_startCmd(jint id, jint cmd_set, jint cmd) {
51   timings[numTimings].id = id;
52   timings[numTimings].cmd_set = cmd_set;
53   timings[numTimings].cmd = cmd;
54   timings[numTimings].start_ns = nsTime();
55 }
56 
timings_endCmd()57 void timings_endCmd() {
58   timings[numTimings].duration_ns = nsTime() - timings[numTimings].start_ns;
59   numTimings++;
60 
61   if (numTimings == MAX_TIMINGS) {
62     timings_flush();
63   }
64 }
65 
66 // Return the size of the ARTT chunk
getChunkSize()67 static jint getChunkSize() {
68   jint size = 0;
69   size += sizeof(jint); // version
70   size += sizeof(jint); // num timing entries.
71 
72   size += numTimings *  (sizeof(jint) * 3 + sizeof(jlong) * 2); // entries
73   return size;
74 }
75 
timings_flush()76 void timings_flush() {
77    // Don't even waste a packet if we know it will contain no payload.
78    if (numTimings == 0) {
79     return;
80    }
81 
82    PacketOutputStream packet;
83 
84    outStream_initCommand(&packet, uniqueID(), 0, JDWP_COMMAND_SET(DDM),  JDWP_COMMAND(DDM, Chunk));
85 
86    outStream_writeInt(&packet, 'A' << 24 | 'R' << 16 | 'T' << 8 | 'T');// DDM chunk type
87    outStream_writeInt(&packet,  getChunkSize()); // DDM chunk length
88 
89    outStream_writeInt(&packet, 1); //version
90    outStream_writeInt(&packet, numTimings); // num timing entries
91 
92    for(int i=0 ; i < numTimings ; i++) {
93      outStream_writeInt(&packet, timings[i].id);
94      outStream_writeInt(&packet, timings[i].cmd_set);
95      outStream_writeInt(&packet, timings[i].cmd);
96      outStream_writeLong(&packet, timings[i].start_ns);
97      outStream_writeLong(&packet, timings[i].duration_ns);
98    }
99    outStream_sendCommand(&packet);
100    outStream_destroy(&packet);
101 
102    numTimings = 0;
103 }
104 
105