jdk/src/share/demo/jvmti/hprof/hprof_monitor.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *   - Neither the name of Sun Microsystems nor the names of its
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/* Monitor contention tracking and monitor wait handling. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Monitor's under contention are unique per trace and signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *  Two monitors with the same trace and signature will be treated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *  the same as far as accumulated contention time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The tls table (or thread table) will be used to store the monitor in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *   contention or being waited on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Monitor wait activity is emitted as it happens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Monitor contention is tabulated and summarized at dump time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#include "hprof.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
typedef struct MonitorKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    TraceIndex   trace_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    StringIndex  sig_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
} MonitorKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
typedef struct MonitorInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    jint         num_hits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    jlong        contended_time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
} MonitorInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
typedef struct IterateInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    MonitorIndex *monitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int           count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    jlong         total_contended_time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
} IterateInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
/* Private internal functions. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
static MonitorKey*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
get_pkey(MonitorIndex index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    void * key_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    int    key_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    table_get_key(gdata->monitor_table, index, &key_ptr, &key_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    HPROF_ASSERT(key_len==sizeof(MonitorKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    HPROF_ASSERT(key_ptr!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    return (MonitorKey*)key_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
static MonitorInfo *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
get_info(MonitorIndex index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    MonitorInfo *       info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    HPROF_ASSERT(index!=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    info = (MonitorInfo*)table_get_info(gdata->monitor_table, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    HPROF_ASSERT(info!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
static MonitorIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
find_or_create_entry(JNIEnv *env, TraceIndex trace_index, jobject object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    static MonitorKey empty_key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    MonitorKey   key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    MonitorIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    char        *sig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    HPROF_ASSERT(object!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    WITH_LOCAL_REFS(env, 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        jclass clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        clazz = getObjectClass(env, object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        getClassSignature(clazz, &sig, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    } END_WITH_LOCAL_REFS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    key                    = empty_key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    key.trace_index        = trace_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    key.sig_index = string_find_or_create(sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    jvmtiDeallocate(sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    index = table_find_or_create_entry(gdata->monitor_table, &key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        (int)sizeof(key), NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
cleanup_item(MonitorIndex index, void *key_ptr, int key_len, void *info_ptr, void *arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
list_item(TableIndex index, void *key_ptr, int key_len, void *info_ptr, void *arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    MonitorInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    MonitorKey  *pkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    HPROF_ASSERT(key_len==sizeof(MonitorKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    HPROF_ASSERT(key_ptr!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    HPROF_ASSERT(info_ptr!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    pkey = (MonitorKey*)key_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    info = (MonitorInfo *)info_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    debug_message(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                "Monitor 0x%08x: trace=0x%08x, sig=0x%08x, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                "num_hits=%d, contended_time=(%d,%d)\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                 index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                 pkey->trace_index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                 pkey->sig_index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                 info->num_hits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                 jlong_high(info->contended_time),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                 jlong_low(info->contended_time));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
collect_iterator(MonitorIndex index, void *key_ptr, int key_len, void *info_ptr, void *arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    MonitorInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    IterateInfo *iterate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    HPROF_ASSERT(key_len==sizeof(MonitorKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    HPROF_ASSERT(info_ptr!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    HPROF_ASSERT(arg!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    iterate = (IterateInfo *)arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    info = (MonitorInfo *)info_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    iterate->monitors[iterate->count++] = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    iterate->total_contended_time += info->contended_time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
qsort_compare(const void *p_monitor1, const void *p_monitor2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    MonitorInfo * info1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    MonitorInfo * info2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    MonitorIndex  monitor1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    MonitorIndex  monitor2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    jlong         result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    HPROF_ASSERT(p_monitor1!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    HPROF_ASSERT(p_monitor2!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    monitor1 = *(MonitorIndex *)p_monitor1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    monitor2 = *(MonitorIndex *)p_monitor2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    info1 = get_info(monitor1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    info2 = get_info(monitor2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    result = info2->contended_time - info1->contended_time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    if (result < (jlong)0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    } else if ( result > (jlong)0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    return info2->num_hits - info1->num_hits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
clear_item(MonitorIndex index, void *key_ptr, int key_len, void *info_ptr, void *arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    MonitorInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    HPROF_ASSERT(key_len==sizeof(MonitorKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    HPROF_ASSERT(info_ptr!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    info = (MonitorInfo *)info_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    info->contended_time = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
static TraceIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
get_trace(TlsIndex tls_index, JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    TraceIndex trace_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    trace_index = tls_get_trace(tls_index, env, gdata->max_trace_depth, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    return trace_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
/* External functions (called from hprof_init.c) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
monitor_init(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    gdata->monitor_table = table_initialize("Monitor",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                            32, 32, 31, (int)sizeof(MonitorInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
monitor_list(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    debug_message(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        "------------------- Monitor Table ------------------------\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    table_walk_items(gdata->monitor_table, &list_item, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    debug_message(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        "----------------------------------------------------------\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
monitor_cleanup(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    table_cleanup(gdata->monitor_table, &cleanup_item, (void*)NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    gdata->monitor_table = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
monitor_clear(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    table_walk_items(gdata->monitor_table, &clear_item, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
/* Contended monitor output */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
monitor_write_contended_time(JNIEnv *env, double cutoff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    int n_entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    n_entries = table_element_count(gdata->monitor_table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    if ( n_entries == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    rawMonitorEnter(gdata->data_access_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        IterateInfo iterate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        int n_items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        jlong total_contended_time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        /* First write all trace we might refer to. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        trace_output_unmarked(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        /* Looking for an array of monitor index values of interest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        iterate.monitors = HPROF_MALLOC(n_entries*(int)sizeof(MonitorIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        (void)memset(iterate.monitors, 0, n_entries*(int)sizeof(MonitorIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        /* Get a combined total and an array of monitor index numbers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        iterate.total_contended_time = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        iterate.count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        table_walk_items(gdata->monitor_table, &collect_iterator, &iterate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        /* Sort that list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        n_entries = iterate.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if ( n_entries > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            qsort(iterate.monitors, n_entries, sizeof(MonitorIndex),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        &qsort_compare);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        /* Apply the cutoff */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        n_items = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        for (i = 0; i < n_entries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            MonitorIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            MonitorInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            double percent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            index = iterate.monitors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            info = get_info(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            percent = (double)info->contended_time /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                      (double)iterate.total_contended_time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            if (percent < cutoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            iterate.monitors[n_items++] = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        /* Output the items that make sense */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        total_contended_time = iterate.total_contended_time / 1000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if ( n_items > 0 && total_contended_time > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            double accum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            /* Output the info on this monitor enter site */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            io_write_monitor_header(total_contended_time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            accum = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            for (i = 0; i < n_items; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                MonitorIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                MonitorInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                MonitorKey *pkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                double percent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                char *sig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                index = iterate.monitors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                pkey = get_pkey(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                info = get_info(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                sig = string_get(pkey->sig_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                percent = (double)info->contended_time /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                          (double)iterate.total_contended_time * 100.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                accum += percent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                io_write_monitor_elem(i + 1, percent, accum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                    info->num_hits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                    trace_get_serial_number(pkey->trace_index),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                    sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            io_write_monitor_footer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        HPROF_FREE(iterate.monitors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    } rawMonitorExit(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
monitor_contended_enter_event(JNIEnv *env, jthread thread, jobject object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    TlsIndex     tls_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    MonitorIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    TraceIndex   trace_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    HPROF_ASSERT(env!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    HPROF_ASSERT(thread!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    HPROF_ASSERT(object!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    tls_index =  tls_find_or_create(env, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    HPROF_ASSERT(tls_get_monitor(tls_index)==0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    trace_index = get_trace(tls_index, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    index = find_or_create_entry(env, trace_index, object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    tls_monitor_start_timer(tls_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    tls_set_monitor(tls_index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
monitor_contended_entered_event(JNIEnv* env, jthread thread, jobject object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    TlsIndex     tls_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    MonitorInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    MonitorIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    HPROF_ASSERT(env!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    HPROF_ASSERT(object!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    HPROF_ASSERT(thread!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    tls_index = tls_find_or_create(env, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    HPROF_ASSERT(tls_index!=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    index     = tls_get_monitor(tls_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    HPROF_ASSERT(index!=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    info      = get_info(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    info->contended_time += tls_monitor_stop_timer(tls_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    info->num_hits++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    tls_set_monitor(tls_index, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
monitor_wait_event(JNIEnv *env, jthread thread, jobject object, jlong timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    TlsIndex     tls_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    MonitorKey  *pkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    MonitorIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    TraceIndex   trace_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    HPROF_ASSERT(env!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    HPROF_ASSERT(object!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    HPROF_ASSERT(thread!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    tls_index =  tls_find_or_create(env, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    HPROF_ASSERT(tls_index!=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    HPROF_ASSERT(tls_get_monitor(tls_index)==0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    trace_index = get_trace(tls_index, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    index = find_or_create_entry(env, trace_index, object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    pkey = get_pkey(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    tls_monitor_start_timer(tls_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    tls_set_monitor(tls_index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    rawMonitorEnter(gdata->data_access_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        io_write_monitor_wait(string_get(pkey->sig_index), timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                            tls_get_thread_serial_number(tls_index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    } rawMonitorExit(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
monitor_waited_event(JNIEnv *env, jthread thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                jobject object, jboolean timed_out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    TlsIndex     tls_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    MonitorIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    jlong        time_waited;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    tls_index =  tls_find_or_create(env, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    HPROF_ASSERT(tls_index!=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    time_waited = tls_monitor_stop_timer(tls_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    index = tls_get_monitor(tls_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    if ( index ==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        /* As best as I can tell, on Solaris X86 (not SPARC) I sometimes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         *    get a "waited" event on a thread that I have never seen before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         *    at all, so how did I get a WAITED event? Perhaps when I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
         *    did the VM_INIT handling, a thread I've never seen had already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         *    done the WAIT (which I never saw?), and now I see this thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         *    for the first time, and also as it finishes it's WAIT?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         *    Only happening on faster processors?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        tls_set_monitor(tls_index, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    HPROF_ASSERT(index!=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    tls_set_monitor(tls_index, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    if (object == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        rawMonitorEnter(gdata->data_access_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            io_write_monitor_sleep(time_waited,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        tls_get_thread_serial_number(tls_index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        } rawMonitorExit(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        MonitorKey *pkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        pkey = get_pkey(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        rawMonitorEnter(gdata->data_access_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            io_write_monitor_waited(string_get(pkey->sig_index), time_waited,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                tls_get_thread_serial_number(tls_index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        } rawMonitorExit(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
}