00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #include "hprof.h"
00054
00055 typedef struct MonitorKey {
00056 TraceIndex trace_index;
00057 StringIndex sig_index;
00058 } MonitorKey;
00059
00060 typedef struct MonitorInfo {
00061 jint num_hits;
00062 jlong contended_time;
00063 } MonitorInfo;
00064
00065 typedef struct IterateInfo {
00066 MonitorIndex *monitors;
00067 int count;
00068 jlong total_contended_time;
00069 } IterateInfo;
00070
00071
00072
00073 static MonitorKey*
00074 get_pkey(MonitorIndex index)
00075 {
00076 void * key_ptr;
00077 int key_len;
00078
00079 table_get_key(gdata->monitor_table, index, &key_ptr, &key_len);
00080 HPROF_ASSERT(key_len==sizeof(MonitorKey));
00081 HPROF_ASSERT(key_ptr!=NULL);
00082 return (MonitorKey*)key_ptr;
00083 }
00084
00085 static MonitorInfo *
00086 get_info(MonitorIndex index)
00087 {
00088 MonitorInfo * info;
00089
00090 HPROF_ASSERT(index!=0);
00091 info = (MonitorInfo*)table_get_info(gdata->monitor_table, index);
00092 HPROF_ASSERT(info!=NULL);
00093 return info;
00094 }
00095
00096 static MonitorIndex
00097 find_or_create_entry(JNIEnv *env, TraceIndex trace_index, jobject object)
00098 {
00099 static MonitorKey empty_key;
00100 MonitorKey key;
00101 MonitorIndex index;
00102 char *sig;
00103
00104 HPROF_ASSERT(object!=NULL);
00105 WITH_LOCAL_REFS(env, 1) {
00106 jclass clazz;
00107
00108 clazz = getObjectClass(env, object);
00109 getClassSignature(clazz, &sig, NULL);
00110 } END_WITH_LOCAL_REFS;
00111
00112 key = empty_key;
00113 key.trace_index = trace_index;
00114 key.sig_index = string_find_or_create(sig);
00115 jvmtiDeallocate(sig);
00116 index = table_find_or_create_entry(gdata->monitor_table, &key,
00117 (int)sizeof(key), NULL, NULL);
00118 return index;
00119 }
00120
00121 static void
00122 cleanup_item(MonitorIndex index, void *key_ptr, int key_len, void *info_ptr, void *arg)
00123 {
00124 }
00125
00126 static void
00127 list_item(TableIndex index, void *key_ptr, int key_len, void *info_ptr, void *arg)
00128 {
00129 MonitorInfo *info;
00130 MonitorKey *pkey;
00131
00132 HPROF_ASSERT(key_len==sizeof(MonitorKey));
00133 HPROF_ASSERT(key_ptr!=NULL);
00134 HPROF_ASSERT(info_ptr!=NULL);
00135 pkey = (MonitorKey*)key_ptr;
00136 info = (MonitorInfo *)info_ptr;
00137 debug_message(
00138 "Monitor 0x%08x: trace=0x%08x, sig=0x%08x, "
00139 "num_hits=%d, contended_time=(%d,%d)\n",
00140 index,
00141 pkey->trace_index,
00142 pkey->sig_index,
00143 info->num_hits,
00144 jlong_high(info->contended_time),
00145 jlong_low(info->contended_time));
00146 }
00147
00148 static void
00149 collect_iterator(MonitorIndex index, void *key_ptr, int key_len, void *info_ptr, void *arg)
00150 {
00151 MonitorInfo *info;
00152 IterateInfo *iterate;
00153
00154 HPROF_ASSERT(key_len==sizeof(MonitorKey));
00155 HPROF_ASSERT(info_ptr!=NULL);
00156 HPROF_ASSERT(arg!=NULL);
00157 iterate = (IterateInfo *)arg;
00158 info = (MonitorInfo *)info_ptr;
00159 iterate->monitors[iterate->count++] = index;
00160 iterate->total_contended_time += info->contended_time;
00161 }
00162
00163 static int
00164 qsort_compare(const void *p_monitor1, const void *p_monitor2)
00165 {
00166 MonitorInfo * info1;
00167 MonitorInfo * info2;
00168 MonitorIndex monitor1;
00169 MonitorIndex monitor2;
00170 jlong result;
00171
00172 HPROF_ASSERT(p_monitor1!=NULL);
00173 HPROF_ASSERT(p_monitor2!=NULL);
00174 monitor1 = *(MonitorIndex *)p_monitor1;
00175 monitor2 = *(MonitorIndex *)p_monitor2;
00176 info1 = get_info(monitor1);
00177 info2 = get_info(monitor2);
00178
00179 result = info2->contended_time - info1->contended_time;
00180 if (result < (jlong)0) {
00181 return -1;
00182 } else if ( result > (jlong)0 ) {
00183 return 1;
00184 }
00185 return info2->num_hits - info1->num_hits;
00186 }
00187
00188 static void
00189 clear_item(MonitorIndex index, void *key_ptr, int key_len, void *info_ptr, void *arg)
00190 {
00191 MonitorInfo *info;
00192
00193 HPROF_ASSERT(key_len==sizeof(MonitorKey));
00194 HPROF_ASSERT(info_ptr!=NULL);
00195 info = (MonitorInfo *)info_ptr;
00196 info->contended_time = 0;
00197 }
00198
00199 static TraceIndex
00200 get_trace(TlsIndex tls_index, JNIEnv *env)
00201 {
00202 TraceIndex trace_index;
00203
00204 trace_index = tls_get_trace(tls_index, env, gdata->max_trace_depth, JNI_FALSE);
00205 return trace_index;
00206 }
00207
00208
00209
00210 void
00211 monitor_init(void)
00212 {
00213 gdata->monitor_table = table_initialize("Monitor",
00214 32, 32, 31, (int)sizeof(MonitorInfo));
00215 }
00216
00217 void
00218 monitor_list(void)
00219 {
00220 debug_message(
00221 "------------------- Monitor Table ------------------------\n");
00222 table_walk_items(gdata->monitor_table, &list_item, NULL);
00223 debug_message(
00224 "----------------------------------------------------------\n");
00225 }
00226
00227 void
00228 monitor_cleanup(void)
00229 {
00230 table_cleanup(gdata->monitor_table, &cleanup_item, (void*)NULL);
00231 gdata->monitor_table = NULL;
00232 }
00233
00234 void
00235 monitor_clear(void)
00236 {
00237 table_walk_items(gdata->monitor_table, &clear_item, NULL);
00238 }
00239
00240
00241 void
00242 monitor_write_contended_time(JNIEnv *env, double cutoff)
00243 {
00244 int n_entries;
00245
00246 n_entries = table_element_count(gdata->monitor_table);
00247 if ( n_entries == 0 ) {
00248 return;
00249 }
00250
00251 rawMonitorEnter(gdata->data_access_lock); {
00252 IterateInfo iterate;
00253 int i;
00254 int n_items;
00255 jlong total_contended_time;
00256
00257
00258 trace_output_unmarked(env);
00259
00260
00261 iterate.monitors = HPROF_MALLOC(n_entries*(int)sizeof(MonitorIndex));
00262 (void)memset(iterate.monitors, 0, n_entries*(int)sizeof(MonitorIndex));
00263
00264
00265 iterate.total_contended_time = 0;
00266 iterate.count = 0;
00267 table_walk_items(gdata->monitor_table, &collect_iterator, &iterate);
00268
00269
00270 n_entries = iterate.count;
00271 if ( n_entries > 0 ) {
00272 qsort(iterate.monitors, n_entries, sizeof(MonitorIndex),
00273 &qsort_compare);
00274 }
00275
00276
00277 n_items = 0;
00278 for (i = 0; i < n_entries; i++) {
00279 MonitorIndex index;
00280 MonitorInfo *info;
00281 double percent;
00282
00283 index = iterate.monitors[i];
00284 info = get_info(index);
00285 percent = (double)info->contended_time /
00286 (double)iterate.total_contended_time;
00287 if (percent < cutoff) {
00288 break;
00289 }
00290 iterate.monitors[n_items++] = index;
00291 }
00292
00293
00294 total_contended_time = iterate.total_contended_time / 1000000;
00295
00296 if ( n_items > 0 && total_contended_time > 0 ) {
00297 double accum;
00298
00299
00300 io_write_monitor_header(total_contended_time);
00301
00302 accum = 0.0;
00303 for (i = 0; i < n_items; i++) {
00304 MonitorIndex index;
00305 MonitorInfo *info;
00306 MonitorKey *pkey;
00307 double percent;
00308 char *sig;
00309
00310 index = iterate.monitors[i];
00311 pkey = get_pkey(index);
00312 info = get_info(index);
00313
00314 sig = string_get(pkey->sig_index);
00315
00316 percent = (double)info->contended_time /
00317 (double)iterate.total_contended_time * 100.0;
00318 accum += percent;
00319 io_write_monitor_elem(i + 1, percent, accum,
00320 info->num_hits,
00321 trace_get_serial_number(pkey->trace_index),
00322 sig);
00323 }
00324 io_write_monitor_footer();
00325 }
00326 HPROF_FREE(iterate.monitors);
00327 } rawMonitorExit(gdata->data_access_lock);
00328 }
00329
00330 void
00331 monitor_contended_enter_event(JNIEnv *env, jthread thread, jobject object)
00332 {
00333 TlsIndex tls_index;
00334 MonitorIndex index;
00335 TraceIndex trace_index;
00336
00337 HPROF_ASSERT(env!=NULL);
00338 HPROF_ASSERT(thread!=NULL);
00339 HPROF_ASSERT(object!=NULL);
00340
00341 tls_index = tls_find_or_create(env, thread);
00342 HPROF_ASSERT(tls_get_monitor(tls_index)==0);
00343 trace_index = get_trace(tls_index, env);
00344 index = find_or_create_entry(env, trace_index, object);
00345 tls_monitor_start_timer(tls_index);
00346 tls_set_monitor(tls_index, index);
00347 }
00348
00349 void
00350 monitor_contended_entered_event(JNIEnv* env, jthread thread, jobject object)
00351 {
00352 TlsIndex tls_index;
00353 MonitorInfo *info;
00354 MonitorIndex index;
00355
00356 HPROF_ASSERT(env!=NULL);
00357 HPROF_ASSERT(object!=NULL);
00358 HPROF_ASSERT(thread!=NULL);
00359
00360 tls_index = tls_find_or_create(env, thread);
00361 HPROF_ASSERT(tls_index!=0);
00362 index = tls_get_monitor(tls_index);
00363 HPROF_ASSERT(index!=0);
00364 info = get_info(index);
00365 info->contended_time += tls_monitor_stop_timer(tls_index);
00366 info->num_hits++;
00367 tls_set_monitor(tls_index, 0);
00368 }
00369
00370 void
00371 monitor_wait_event(JNIEnv *env, jthread thread, jobject object, jlong timeout)
00372 {
00373 TlsIndex tls_index;
00374 MonitorKey *pkey;
00375 MonitorIndex index;
00376 TraceIndex trace_index;
00377
00378 HPROF_ASSERT(env!=NULL);
00379 HPROF_ASSERT(object!=NULL);
00380 HPROF_ASSERT(thread!=NULL);
00381
00382 tls_index = tls_find_or_create(env, thread);
00383 HPROF_ASSERT(tls_index!=0);
00384 HPROF_ASSERT(tls_get_monitor(tls_index)==0);
00385 trace_index = get_trace(tls_index, env);
00386 index = find_or_create_entry(env, trace_index, object);
00387 pkey = get_pkey(index);
00388 tls_monitor_start_timer(tls_index);
00389 tls_set_monitor(tls_index, index);
00390
00391 rawMonitorEnter(gdata->data_access_lock); {
00392 io_write_monitor_wait(string_get(pkey->sig_index), timeout,
00393 tls_get_thread_serial_number(tls_index));
00394 } rawMonitorExit(gdata->data_access_lock);
00395 }
00396
00397 void
00398 monitor_waited_event(JNIEnv *env, jthread thread,
00399 jobject object, jboolean timed_out)
00400 {
00401 TlsIndex tls_index;
00402 MonitorIndex index;
00403 jlong time_waited;
00404
00405 tls_index = tls_find_or_create(env, thread);
00406 HPROF_ASSERT(tls_index!=0);
00407 time_waited = tls_monitor_stop_timer(tls_index);
00408 index = tls_get_monitor(tls_index);
00409
00410 if ( index ==0 ) {
00411
00412
00413
00414
00415
00416
00417
00418
00419 tls_set_monitor(tls_index, 0);
00420 return;
00421 }
00422 HPROF_ASSERT(index!=0);
00423 tls_set_monitor(tls_index, 0);
00424 if (object == NULL) {
00425 rawMonitorEnter(gdata->data_access_lock); {
00426 io_write_monitor_sleep(time_waited,
00427 tls_get_thread_serial_number(tls_index));
00428 } rawMonitorExit(gdata->data_access_lock);
00429 } else {
00430 MonitorKey *pkey;
00431
00432 pkey = get_pkey(index);
00433 rawMonitorEnter(gdata->data_access_lock); {
00434 io_write_monitor_waited(string_get(pkey->sig_index), time_waited,
00435 tls_get_thread_serial_number(tls_index));
00436 } rawMonitorExit(gdata->data_access_lock);
00437 }
00438 }
00439