jdk/src/jdk.hprof.agent/share/native/libhprof/hprof_trace.c
author sla
Tue, 26 Aug 2014 07:55:08 +0200
changeset 26201 40a873d21081
parent 25859 jdk/src/demo/share/jvmti/hprof/hprof_trace.c@3317bb8137f4
permissions -rw-r--r--
8043936: Drop HPROF as demo, keep as HPROF agent shipped with JDK Reviewed-by: erikj, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10292
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/* Trace table. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * A trace is an optional thread serial number plus N frames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The thread serial number is added to the key only if the user asks for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *    threads in traces, which will cause many more traces to be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *    Without it all threads share the traces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This is a variable length Key, depending on the number of frames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *   The frames are FrameIndex values into the frame table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * It is important that the thread serial number is used and not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *    TlsIndex, threads come and go, and TlsIndex values are re-used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *    but the thread serial number is unique per thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * The cpu=times and cpu=samples dumps rely heavily on traces, the trace
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   dump preceeds the cpu information and uses the trace information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   Depending on the cpu= request, different sorts are applied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   traces that are dumped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
#include "hprof.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
typedef struct TraceKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    SerialNumber thread_serial_num; /* Thread serial number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    short        n_frames;          /* Number of frames that follow. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    jvmtiPhase   phase : 8;         /* Makes some traces unique */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    FrameIndex   frames[1];         /* Variable length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
} TraceKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
typedef struct TraceInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    SerialNumber serial_num;        /* Trace serial number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    jint         num_hits;          /* Number of hits this trace has */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    jlong        total_cost;        /* Total cost associated with trace */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    jlong        self_cost;         /* Total cost without children cost */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    jint         status;            /* Status of dump of trace */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
} TraceInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
typedef struct IterateInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    TraceIndex* traces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    int         count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    jlong       grand_total_cost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
} IterateInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
/* Private internal functions. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
static TraceKey*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
get_pkey(TraceIndex index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    void *      pkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    int         key_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    table_get_key(gdata->trace_table, index, &pkey, &key_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    HPROF_ASSERT(pkey!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    HPROF_ASSERT(key_len>=(int)sizeof(TraceKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    HPROF_ASSERT(((TraceKey*)pkey)->n_frames<=1?key_len==(int)sizeof(TraceKey) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
             key_len==(int)sizeof(TraceKey)+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                      (int)sizeof(FrameIndex)*(((TraceKey*)pkey)->n_frames-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    return (TraceKey*)pkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
static TraceInfo *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
get_info(TraceIndex index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    TraceInfo *         info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    info        = (TraceInfo*)table_get_info(gdata->trace_table, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
static TraceIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
find_or_create(SerialNumber thread_serial_num, jint n_frames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            FrameIndex *frames, jvmtiPhase phase, TraceKey *trace_key_buffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    TraceInfo * info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    TraceKey *  pkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    int         key_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    TraceIndex  index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    jboolean    new_one;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    static TraceKey empty_key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    HPROF_ASSERT(frames!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    HPROF_ASSERT(trace_key_buffer!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    key_len = (int)sizeof(TraceKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    if ( n_frames > 1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        key_len += (int)((n_frames-1)*(int)sizeof(FrameIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    pkey = trace_key_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    *pkey = empty_key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    pkey->thread_serial_num = (gdata->thread_in_traces ? thread_serial_num : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    pkey->n_frames = (short)n_frames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    pkey->phase = phase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    if ( n_frames > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        (void)memcpy(pkey->frames, frames, (n_frames*(int)sizeof(FrameIndex)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    new_one = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    index = table_find_or_create_entry(gdata->trace_table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                pkey, key_len, &new_one, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    if ( new_one ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        info = get_info(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        info->serial_num = gdata->trace_serial_number_counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
list_item(TableIndex index, void *key_ptr, int key_len, void *info_ptr, void *arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    TraceInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    TraceKey         *key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    int               i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    HPROF_ASSERT(key_ptr!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    HPROF_ASSERT(key_len>0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    HPROF_ASSERT(info_ptr!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    key = (TraceKey*)key_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    info = (TraceInfo *)info_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    debug_message( "Trace 0x%08x: SN=%u, threadSN=%u, n_frames=%d, frames=(",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
             index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
             info->serial_num,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
             key->thread_serial_num,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
             key->n_frames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    for ( i = 0 ; i < key->n_frames ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        debug_message( "0x%08x, ", key->frames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    debug_message( "), traceSN=%u, num_hits=%d, self_cost=(%d,%d), "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        "total_cost=(%d,%d), status=0x%08x\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        info->serial_num,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        info->num_hits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        jlong_high(info->self_cost),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        jlong_low(info->self_cost),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        jlong_high(info->total_cost),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        jlong_low(info->total_cost),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        info->status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
clear_cost(TableIndex i, void *key_ptr, int key_len, void *info_ptr, void *arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    TraceInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    HPROF_ASSERT(key_ptr!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    HPROF_ASSERT(key_len>0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    HPROF_ASSERT(info_ptr!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    info = (TraceInfo *)info_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    info->num_hits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    info->total_cost = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    info->self_cost = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
/* Get the names for a frame in order to dump it. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
get_frame_details(JNIEnv *env, FrameIndex frame_index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                SerialNumber *frame_serial_num, char **pcsig, ClassIndex *pcnum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                char **pmname, char **pmsig, char **psname, jint *plineno)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    jmethodID method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    jlocation location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    jint      lineno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    HPROF_ASSERT(frame_index!=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    *pmname = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    *pmsig = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    *pcsig = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    if ( psname != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        *psname = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    if ( plineno != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        *plineno = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    if ( pcnum != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        *pcnum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    frame_get_location(frame_index, frame_serial_num, &method, &location, &lineno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    if ( plineno != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        *plineno = lineno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    WITH_LOCAL_REFS(env, 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        jclass klass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        getMethodClass(method, &klass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        getClassSignature(klass, pcsig, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if ( pcnum != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            LoaderIndex loader_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            jobject     loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            loader = getClassLoader(klass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            loader_index = loader_find_or_create(env, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            *pcnum = class_find_or_create(*pcsig, loader_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
             (void)class_new_classref(env, *pcnum, klass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if ( psname != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            getSourceFileName(klass, psname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    } END_WITH_LOCAL_REFS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    getMethodName(method, pmname, pmsig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
/* Write out a stack trace.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
output_trace(TableIndex index, void *key_ptr, int key_len, void *info_ptr, void *arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    TraceKey *key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    TraceInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    SerialNumber serial_num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    SerialNumber thread_serial_num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    jint n_frames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    JNIEnv *env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    char *phase_str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    struct FrameNames {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        SerialNumber serial_num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        char * sname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        char * csig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        char * mname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        int    lineno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    } *finfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    info = (TraceInfo*)info_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    if ( info->status != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    env = (JNIEnv*)arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    key = (TraceKey*)key_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    thread_serial_num = key->thread_serial_num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    serial_num = info->serial_num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    info->status = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    finfo = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    n_frames = (jint)key->n_frames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    if ( n_frames > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        finfo = (struct FrameNames *)HPROF_MALLOC(n_frames*(int)sizeof(struct FrameNames));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        /* Write frames, but save information for trace later */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        for (i = 0; i < n_frames; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            FrameIndex frame_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            char *msig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            ClassIndex cnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            frame_index = key->frames[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            get_frame_details(env, frame_index, &finfo[i].serial_num,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        &finfo[i].csig, &cnum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        &finfo[i].mname, &msig, &finfo[i].sname, &finfo[i].lineno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (frame_get_status(frame_index) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                io_write_frame(frame_index, finfo[i].serial_num,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                               finfo[i].mname, msig,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                               finfo[i].sname, class_get_serial_number(cnum),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                               finfo[i].lineno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                frame_set_status(frame_index, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            jvmtiDeallocate(msig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /* Find phase string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    if ( key->phase == JVMTI_PHASE_LIVE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        phase_str = NULL; /* Normal trace, no phase annotation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        phase_str =  phaseString(key->phase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    io_write_trace_header(serial_num, thread_serial_num, n_frames, phase_str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    for (i = 0; i < n_frames; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        io_write_trace_elem(serial_num, key->frames[i], finfo[i].serial_num,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                            finfo[i].csig,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                            finfo[i].mname, finfo[i].sname, finfo[i].lineno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        jvmtiDeallocate(finfo[i].csig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        jvmtiDeallocate(finfo[i].mname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        jvmtiDeallocate(finfo[i].sname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    io_write_trace_footer(serial_num, thread_serial_num, n_frames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    if ( finfo != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        HPROF_FREE(finfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
/* Output a specific list of traces. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
output_list(JNIEnv *env, TraceIndex *list, jint count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    rawMonitorEnter(gdata->data_access_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        for ( i = 0; i < count ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            TraceIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            TraceInfo  *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            void *      pkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            int         key_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            index = list[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            table_get_key(gdata->trace_table, index, &pkey, &key_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            info = get_info(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            output_trace(index, pkey, key_len, info, (void*)env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    } rawMonitorExit(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
collect_iterator(TableIndex index, void *key_ptr, int key_len, void *info_ptr, void *arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    TraceInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    IterateInfo      *iterate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    HPROF_ASSERT(key_ptr!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    HPROF_ASSERT(key_len>0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    HPROF_ASSERT(arg!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    HPROF_ASSERT(info_ptr!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    iterate = (IterateInfo *)arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    info = (TraceInfo *)info_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    iterate->traces[iterate->count++] = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    iterate->grand_total_cost += info->self_cost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
qsort_compare_cost(const void *p_trace1, const void *p_trace2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    TraceIndex          trace1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    TraceIndex          trace2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    TraceInfo * info1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    TraceInfo * info2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    HPROF_ASSERT(p_trace1!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    HPROF_ASSERT(p_trace2!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    trace1 = *(TraceIndex *)p_trace1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    trace2 = *(TraceIndex *)p_trace2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    info1 = get_info(trace1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    info2 = get_info(trace2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    return (int)(info2->self_cost - info1->self_cost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
qsort_compare_num_hits(const void *p_trace1, const void *p_trace2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    TraceIndex          trace1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    TraceIndex          trace2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    TraceInfo * info1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    TraceInfo * info2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    HPROF_ASSERT(p_trace1!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    HPROF_ASSERT(p_trace2!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    trace1 = *(TraceIndex *)p_trace1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    trace2 = *(TraceIndex *)p_trace2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    info1 = get_info(trace1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    info2 = get_info(trace2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    return info2->num_hits - info1->num_hits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
/* External interfaces. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
trace_init(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    gdata->trace_table = table_initialize("Trace",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                            256, 256, 511, (int)sizeof(TraceInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
trace_list(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    debug_message(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        "--------------------- Trace Table ------------------------\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    table_walk_items(gdata->trace_table, &list_item, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    debug_message(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        "----------------------------------------------------------\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
trace_cleanup(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    table_cleanup(gdata->trace_table, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    gdata->trace_table = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
SerialNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
trace_get_serial_number(TraceIndex index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    TraceInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    if ( index == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    info = get_info(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    return info->serial_num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
trace_increment_cost(TraceIndex index, jint num_hits, jlong self_cost, jlong total_cost)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    TraceInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    table_lock_enter(gdata->trace_table); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        info              = get_info(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        info->num_hits   += num_hits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        info->self_cost  += self_cost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        info->total_cost += total_cost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    } table_lock_exit(gdata->trace_table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
TraceIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
trace_find_or_create(SerialNumber thread_serial_num, jint n_frames, FrameIndex *frames, jvmtiFrameInfo *jframes_buffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    return find_or_create(thread_serial_num, n_frames, frames, getPhase(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                                (TraceKey*)jframes_buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
/* We may need to ask for more frames than the user asked for */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
get_real_depth(int depth, jboolean skip_init)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    int extra_frames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    extra_frames = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /* This is only needed if we are doing BCI */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    if ( gdata->bci && depth > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        /* Account for Java and native Tracker methods */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        extra_frames = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if ( skip_init ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            /* Also allow for ignoring the java.lang.Object.<init> method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            extra_frames += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    return depth + extra_frames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
/* Fill in FrameIndex array from jvmtiFrameInfo array, return n_frames */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
fill_frame_buffer(int depth, int real_depth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                 int frame_count, jboolean skip_init,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                 jvmtiFrameInfo *jframes_buffer, FrameIndex *frames_buffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    int  n_frames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    jint topframe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /* If real_depth is 0, just return 0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    if ( real_depth == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /* Assume top frame index is 0 for now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    topframe = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /* Possible top frames belong to the hprof Tracker class, remove them */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    if ( gdata->bci ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        while ( ( ( frame_count - topframe ) > 0 ) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                ( topframe < (real_depth-depth) ) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                ( tracker_method(jframes_buffer[topframe].method) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                  ( skip_init
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    && jframes_buffer[topframe].method==gdata->object_init_method ) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
             ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            topframe++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /* Adjust count to match depth request */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    if ( ( frame_count - topframe ) > depth ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        frame_count =  depth + topframe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    /* The actual frame count we will process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    n_frames = frame_count - topframe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    if ( n_frames > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        for (i = 0; i < n_frames; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            jmethodID method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            jlocation location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            method = jframes_buffer[i+topframe].method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            location = jframes_buffer[i+topframe].location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            frames_buffer[i] = frame_find_or_create(method, location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    return n_frames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
/* Get the trace for the supplied thread */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
TraceIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
trace_get_current(jthread thread, SerialNumber thread_serial_num,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                        int depth, jboolean skip_init,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                        FrameIndex *frames_buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                        jvmtiFrameInfo *jframes_buffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    TraceIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    jint       frame_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    int        real_depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    int        n_frames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    HPROF_ASSERT(thread!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    HPROF_ASSERT(frames_buffer!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    HPROF_ASSERT(jframes_buffer!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    /* We may need to ask for more frames than the user asked for */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    real_depth = get_real_depth(depth, skip_init);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /* Get the stack trace for this one thread */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    frame_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    if ( real_depth > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        getStackTrace(thread, jframes_buffer, real_depth, &frame_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    /* Create FrameIndex's */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    n_frames = fill_frame_buffer(depth, real_depth, frame_count, skip_init,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                 jframes_buffer, frames_buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    /* Lookup or create new TraceIndex */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    index = find_or_create(thread_serial_num, n_frames, frames_buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                getPhase(), (TraceKey*)jframes_buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
/* Get traces for all threads in list (traces[i]==0 if thread not running) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
trace_get_all_current(jint thread_count, jthread *threads,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                      SerialNumber *thread_serial_nums,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                      int depth, jboolean skip_init,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                      TraceIndex *traces, jboolean always_care)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    jvmtiStackInfo *stack_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    int             nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    int             real_depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    int             i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    FrameIndex     *frames_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    TraceKey       *trace_key_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    jvmtiPhase      phase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    HPROF_ASSERT(threads!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    HPROF_ASSERT(thread_serial_nums!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    HPROF_ASSERT(traces!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    HPROF_ASSERT(thread_count > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /* Find out what the phase is for all these traces */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    phase = getPhase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /* We may need to ask for more frames than the user asked for */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    real_depth = get_real_depth(depth, skip_init);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /* Get the stack traces for all the threads */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    getThreadListStackTraces(thread_count, threads, real_depth, &stack_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /* Allocate a frames_buffer and trace key buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    nbytes = (int)sizeof(FrameIndex)*real_depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    frames_buffer = (FrameIndex*)HPROF_MALLOC(nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    nbytes += (int)sizeof(TraceKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    trace_key_buffer = (TraceKey*)HPROF_MALLOC(nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /* Loop over the stack traces we have for these 'thread_count' threads */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    for ( i = 0 ; i < thread_count ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        int n_frames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        /* Assume 0 at first (no trace) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        traces[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        /* If thread has frames, is runnable, and isn't suspended, we care */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if ( always_care ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
             ( stack_info[i].frame_count > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
               && (stack_info[i].state & JVMTI_THREAD_STATE_RUNNABLE)!=0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
               && (stack_info[i].state & JVMTI_THREAD_STATE_SUSPENDED)==0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
               && (stack_info[i].state & JVMTI_THREAD_STATE_INTERRUPTED)==0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            /* Create FrameIndex's */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            n_frames = fill_frame_buffer(depth, real_depth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                                         stack_info[i].frame_count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                                         skip_init,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                                         stack_info[i].frame_buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                                         frames_buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            /* Lookup or create new TraceIndex */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            traces[i] = find_or_create(thread_serial_nums[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                           n_frames, frames_buffer, phase, trace_key_buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    /* Make sure we free the space */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    HPROF_FREE(frames_buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    HPROF_FREE(trace_key_buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    jvmtiDeallocate(stack_info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
/* Increment the trace costs for all the threads (for cpu=samples) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
trace_increment_all_sample_costs(jint thread_count, jthread *threads,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                      SerialNumber *thread_serial_nums,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                      int depth, jboolean skip_init)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    TraceIndex *traces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    int         nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    HPROF_ASSERT(threads!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    HPROF_ASSERT(thread_serial_nums!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    HPROF_ASSERT(thread_count > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    HPROF_ASSERT(depth >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    if ( depth == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    /* Allocate a traces array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    nbytes = (int)sizeof(TraceIndex)*thread_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    traces = (TraceIndex*)HPROF_MALLOC(nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /* Get all the current traces for these threads */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    trace_get_all_current(thread_count, threads, thread_serial_nums,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                      depth, skip_init, traces, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /* Increment the cpu=samples cost on these traces */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    table_lock_enter(gdata->trace_table); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        for ( i = 0 ; i < thread_count ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            /* Each trace gets a hit and an increment of it's total cost */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            if ( traces[i] != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                TraceInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                info              = get_info(traces[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                info->num_hits   += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                info->self_cost  += (jlong)1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                info->total_cost += (jlong)1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    } table_lock_exit(gdata->trace_table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    /* Free up the memory allocated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    HPROF_FREE(traces);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
trace_output_unmarked(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    rawMonitorEnter(gdata->data_access_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        table_walk_items(gdata->trace_table, &output_trace, (void*)env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    } rawMonitorExit(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
/* output info on the cost associated with traces  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
trace_output_cost(JNIEnv *env, double cutoff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    IterateInfo iterate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    int i, trace_table_size, n_items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    double accum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    int n_entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    rawMonitorEnter(gdata->data_access_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        n_entries = table_element_count(gdata->trace_table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        iterate.traces = HPROF_MALLOC(n_entries*(int)sizeof(TraceIndex)+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        iterate.count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        iterate.grand_total_cost = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        table_walk_items(gdata->trace_table, &collect_iterator, &iterate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        trace_table_size = iterate.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        /* sort all the traces according to the cost */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        qsort(iterate.traces, trace_table_size, sizeof(TraceIndex),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    &qsort_compare_cost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        n_items = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        for (i = 0; i < trace_table_size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            TraceInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            TraceIndex trace_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            double percent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            trace_index = iterate.traces[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            info = get_info(trace_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            /* As soon as a trace with zero hits is seen, we need no others */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            if (info->num_hits == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            percent = (double)info->self_cost / (double)iterate.grand_total_cost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            if (percent < cutoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            n_items++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        /* Now write all trace we might refer to. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        output_list(env, iterate.traces, n_items);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        io_write_cpu_samples_header(iterate.grand_total_cost, n_items);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        accum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        for (i = 0; i < n_items; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            SerialNumber frame_serial_num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            TraceInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            TraceKey *key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            TraceIndex trace_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            double percent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            char *csig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            char *mname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            char *msig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            trace_index = iterate.traces[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            info = get_info(trace_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            key = get_pkey(trace_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            percent = ((double)info->self_cost / (double)iterate.grand_total_cost) * 100.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            accum += percent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            csig = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            mname = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            msig  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            if (key->n_frames > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                get_frame_details(env, key->frames[0], &frame_serial_num,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                        &csig, NULL, &mname, &msig, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            io_write_cpu_samples_elem(i+1, percent, accum, info->num_hits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                        (jint)info->self_cost, info->serial_num,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                        key->n_frames, csig, mname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            jvmtiDeallocate(csig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            jvmtiDeallocate(mname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            jvmtiDeallocate(msig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        io_write_cpu_samples_footer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        HPROF_FREE(iterate.traces);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    } rawMonitorExit(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
/* output the trace cost in old prof format */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
trace_output_cost_in_prof_format(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    IterateInfo iterate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    int i, trace_table_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    int n_entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    rawMonitorEnter(gdata->data_access_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        n_entries = table_element_count(gdata->trace_table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        iterate.traces = HPROF_MALLOC(n_entries*(int)sizeof(TraceIndex)+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        iterate.count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        iterate.grand_total_cost = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        table_walk_items(gdata->trace_table, &collect_iterator, &iterate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        trace_table_size = iterate.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        /* sort all the traces according to the number of hits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        qsort(iterate.traces, trace_table_size, sizeof(TraceIndex),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                    &qsort_compare_num_hits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        io_write_oldprof_header();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        for (i = 0; i < trace_table_size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            SerialNumber frame_serial_num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            TraceInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            TraceKey *key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            TraceIndex trace_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            int num_frames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            int num_hits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            char *csig_callee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            char *mname_callee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            char *msig_callee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            char *csig_caller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            char *mname_caller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            char *msig_caller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            trace_index = iterate.traces[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            key = get_pkey(trace_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            info = get_info(trace_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            num_hits = info->num_hits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            if (num_hits == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            csig_callee  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            mname_callee = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            msig_callee  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            csig_caller  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            mname_caller = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            msig_caller  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            num_frames = (int)key->n_frames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            if (num_frames >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                get_frame_details(env, key->frames[0], &frame_serial_num,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                        &csig_callee, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                        &mname_callee, &msig_callee, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            if (num_frames > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                get_frame_details(env, key->frames[1], &frame_serial_num,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                        &csig_caller, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                        &mname_caller, &msig_caller, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            io_write_oldprof_elem(info->num_hits, num_frames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                                    csig_callee, mname_callee, msig_callee,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                                    csig_caller, mname_caller, msig_caller,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                                    (int)info->total_cost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            jvmtiDeallocate(csig_callee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            jvmtiDeallocate(mname_callee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            jvmtiDeallocate(msig_callee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            jvmtiDeallocate(csig_caller);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            jvmtiDeallocate(mname_caller);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            jvmtiDeallocate(msig_caller);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        io_write_oldprof_footer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        HPROF_FREE(iterate.traces);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    } rawMonitorExit(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
trace_clear_cost(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    table_walk_items(gdata->trace_table, &clear_cost, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
}