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