849 // exclude externally visible JavaThreads 850 if (thread->is_Java_thread() && !thread->is_hidden_from_external_view()) { 851 return; 852 } 853 854 _count++; 855 } 856 857 static jint get_vm_thread_count() { 858 VmThreadCountClosure vmtcc; 859 { 860 MutexLockerEx ml(Threads_lock); 861 Threads::threads_do(&vmtcc); 862 } 863 864 return vmtcc.count(); 865 } 866 867 static jint get_num_flags() { 868 // last flag entry is always NULL, so subtract 1 869 int nFlags = (int) Flag::numFlags - 1; 870 int count = 0; 871 for (int i = 0; i < nFlags; i++) { 872 Flag* flag = &Flag::flags[i]; 873 // Exclude the locked (diagnostic, experimental) flags 874 if (flag->is_unlocked() || flag->is_unlocker()) { 875 count++; 876 } 877 } 878 return count; 879 } 880 881 static jlong get_long_attribute(jmmLongAttribute att) { 882 switch (att) { 883 case JMM_CLASS_LOADED_COUNT: 884 return ClassLoadingService::loaded_class_count(); 885 886 case JMM_CLASS_UNLOADED_COUNT: 887 return ClassLoadingService::unloaded_class_count(); 888 889 case JMM_THREAD_TOTAL_COUNT: 890 return ThreadService::get_total_thread_count(); 891 892 case JMM_THREAD_LIVE_COUNT: 1402 "Invalid thread ID", -1); 1403 } 1404 1405 JavaThread* java_thread = NULL; 1406 if (thread_id == 0) { 1407 // current thread 1408 return os::current_thread_cpu_time(); 1409 } else { 1410 ThreadsListHandle tlh; 1411 java_thread = tlh.list()->find_JavaThread_from_java_tid(thread_id); 1412 if (java_thread != NULL) { 1413 return os::thread_cpu_time((Thread*) java_thread); 1414 } 1415 } 1416 return -1; 1417 JVM_END 1418 1419 // Returns a String array of all VM global flag names 1420 JVM_ENTRY(jobjectArray, jmm_GetVMGlobalNames(JNIEnv *env)) 1421 // last flag entry is always NULL, so subtract 1 1422 int nFlags = (int) Flag::numFlags - 1; 1423 // allocate a temp array 1424 objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), 1425 nFlags, CHECK_0); 1426 objArrayHandle flags_ah(THREAD, r); 1427 int num_entries = 0; 1428 for (int i = 0; i < nFlags; i++) { 1429 Flag* flag = &Flag::flags[i]; 1430 // Exclude notproduct and develop flags in product builds. 1431 if (flag->is_constant_in_binary()) { 1432 continue; 1433 } 1434 // Exclude the locked (experimental, diagnostic) flags 1435 if (flag->is_unlocked() || flag->is_unlocker()) { 1436 Handle s = java_lang_String::create_from_str(flag->_name, CHECK_0); 1437 flags_ah->obj_at_put(num_entries, s()); 1438 num_entries++; 1439 } 1440 } 1441 1442 if (num_entries < nFlags) { 1443 // Return array of right length 1444 objArrayOop res = oopFactory::new_objArray(SystemDictionary::String_klass(), num_entries, CHECK_0); 1445 for(int i = 0; i < num_entries; i++) { 1446 res->obj_at_put(i, flags_ah->obj_at(i)); 1447 } 1448 return (jobjectArray)JNIHandles::make_local(env, res); 1449 } 1450 1451 return (jobjectArray)JNIHandles::make_local(env, flags_ah()); 1452 JVM_END 1453 1454 // Utility function used by jmm_GetVMGlobals. Returns false if flag type 1455 // can't be determined, true otherwise. If false is returned, then *global 1456 // will be incomplete and invalid. 1457 bool add_global_entry(JNIEnv* env, Handle name, jmmVMGlobal *global, Flag *flag, TRAPS) { 1458 Handle flag_name; 1459 if (name() == NULL) { 1460 flag_name = java_lang_String::create_from_str(flag->_name, CHECK_false); 1461 } else { 1462 flag_name = name; 1463 } 1464 global->name = (jstring)JNIHandles::make_local(env, flag_name()); 1465 1466 if (flag->is_bool()) { 1467 global->value.z = flag->get_bool() ? JNI_TRUE : JNI_FALSE; 1468 global->type = JMM_VMGLOBAL_TYPE_JBOOLEAN; 1469 } else if (flag->is_int()) { 1470 global->value.j = (jlong)flag->get_int(); 1471 global->type = JMM_VMGLOBAL_TYPE_JLONG; 1472 } else if (flag->is_uint()) { 1473 global->value.j = (jlong)flag->get_uint(); 1474 global->type = JMM_VMGLOBAL_TYPE_JLONG; 1475 } else if (flag->is_intx()) { 1476 global->value.j = (jlong)flag->get_intx(); 1477 global->type = JMM_VMGLOBAL_TYPE_JLONG; 1482 global->value.j = (jlong)flag->get_uint64_t(); 1483 global->type = JMM_VMGLOBAL_TYPE_JLONG; 1484 } else if (flag->is_double()) { 1485 global->value.d = (jdouble)flag->get_double(); 1486 global->type = JMM_VMGLOBAL_TYPE_JDOUBLE; 1487 } else if (flag->is_size_t()) { 1488 global->value.j = (jlong)flag->get_size_t(); 1489 global->type = JMM_VMGLOBAL_TYPE_JLONG; 1490 } else if (flag->is_ccstr()) { 1491 Handle str = java_lang_String::create_from_str(flag->get_ccstr(), CHECK_false); 1492 global->value.l = (jobject)JNIHandles::make_local(env, str()); 1493 global->type = JMM_VMGLOBAL_TYPE_JSTRING; 1494 } else { 1495 global->type = JMM_VMGLOBAL_TYPE_UNKNOWN; 1496 return false; 1497 } 1498 1499 global->writeable = flag->is_writeable(); 1500 global->external = flag->is_external(); 1501 switch (flag->get_origin()) { 1502 case Flag::DEFAULT: 1503 global->origin = JMM_VMGLOBAL_ORIGIN_DEFAULT; 1504 break; 1505 case Flag::COMMAND_LINE: 1506 global->origin = JMM_VMGLOBAL_ORIGIN_COMMAND_LINE; 1507 break; 1508 case Flag::ENVIRON_VAR: 1509 global->origin = JMM_VMGLOBAL_ORIGIN_ENVIRON_VAR; 1510 break; 1511 case Flag::CONFIG_FILE: 1512 global->origin = JMM_VMGLOBAL_ORIGIN_CONFIG_FILE; 1513 break; 1514 case Flag::MANAGEMENT: 1515 global->origin = JMM_VMGLOBAL_ORIGIN_MANAGEMENT; 1516 break; 1517 case Flag::ERGONOMIC: 1518 global->origin = JMM_VMGLOBAL_ORIGIN_ERGONOMIC; 1519 break; 1520 case Flag::ATTACH_ON_DEMAND: 1521 global->origin = JMM_VMGLOBAL_ORIGIN_ATTACH_ON_DEMAND; 1522 break; 1523 default: 1524 global->origin = JMM_VMGLOBAL_ORIGIN_OTHER; 1525 } 1526 1527 return true; 1528 } 1529 1530 // Fill globals array of count length with jmmVMGlobal entries 1531 // specified by names. If names == NULL, fill globals array 1532 // with all Flags. Return value is number of entries 1533 // created in globals. 1534 // If a Flag with a given name in an array element does not 1535 // exist, globals[i].name will be set to NULL. 1536 JVM_ENTRY(jint, jmm_GetVMGlobals(JNIEnv *env, 1537 jobjectArray names, 1538 jmmVMGlobal *globals, 1539 jint count)) 1540 1541 1542 if (globals == NULL) { 1543 THROW_(vmSymbols::java_lang_NullPointerException(), 0); 1544 } 1545 1546 ResourceMark rm(THREAD); 1547 1548 if (names != NULL) { 1549 // return the requested globals 1550 objArrayOop ta = objArrayOop(JNIHandles::resolve_non_null(names)); 1551 objArrayHandle names_ah(THREAD, ta); 1552 // Make sure we have a String array 1553 Klass* element_klass = ObjArrayKlass::cast(names_ah->klass())->element_klass(); 1554 if (element_klass != SystemDictionary::String_klass()) { 1555 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), 1556 "Array element type is not String class", 0); 1557 } 1558 1559 int names_length = names_ah->length(); 1560 int num_entries = 0; 1561 for (int i = 0; i < names_length && i < count; i++) { 1562 oop s = names_ah->obj_at(i); 1563 if (s == NULL) { 1564 THROW_(vmSymbols::java_lang_NullPointerException(), 0); 1565 } 1566 1567 Handle sh(THREAD, s); 1568 char* str = java_lang_String::as_utf8_string(s); 1569 Flag* flag = Flag::find_flag(str, strlen(str)); 1570 if (flag != NULL && 1571 add_global_entry(env, sh, &globals[i], flag, THREAD)) { 1572 num_entries++; 1573 } else { 1574 globals[i].name = NULL; 1575 } 1576 } 1577 return num_entries; 1578 } else { 1579 // return all globals if names == NULL 1580 1581 // last flag entry is always NULL, so subtract 1 1582 int nFlags = (int) Flag::numFlags - 1; 1583 Handle null_h; 1584 int num_entries = 0; 1585 for (int i = 0; i < nFlags && num_entries < count; i++) { 1586 Flag* flag = &Flag::flags[i]; 1587 // Exclude notproduct and develop flags in product builds. 1588 if (flag->is_constant_in_binary()) { 1589 continue; 1590 } 1591 // Exclude the locked (diagnostic, experimental) flags 1592 if ((flag->is_unlocked() || flag->is_unlocker()) && 1593 add_global_entry(env, null_h, &globals[num_entries], flag, THREAD)) { 1594 num_entries++; 1595 } 1596 } 1597 return num_entries; 1598 } 1599 JVM_END 1600 1601 JVM_ENTRY(void, jmm_SetVMGlobal(JNIEnv *env, jstring flag_name, jvalue new_value)) 1602 ResourceMark rm(THREAD); 1603 1604 oop fn = JNIHandles::resolve_external_guard(flag_name); 1605 if (fn == NULL) { 1606 THROW_MSG(vmSymbols::java_lang_NullPointerException(), 1607 "The flag name cannot be null."); 1608 } 1609 char* name = java_lang_String::as_utf8_string(fn); 1610 1611 FormatBuffer<80> error_msg("%s", ""); 1612 int succeed = WriteableFlags::set_flag(name, new_value, Flag::MANAGEMENT, error_msg); 1613 1614 if (succeed != Flag::SUCCESS) { 1615 if (succeed == Flag::MISSING_VALUE) { 1616 // missing value causes NPE to be thrown 1617 THROW(vmSymbols::java_lang_NullPointerException()); 1618 } else { 1619 // all the other errors are reported as IAE with the appropriate error message 1620 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 1621 error_msg.buffer()); 1622 } 1623 } 1624 assert(succeed == Flag::SUCCESS, "Setting flag should succeed"); 1625 JVM_END 1626 1627 class ThreadTimesClosure: public ThreadClosure { 1628 private: 1629 objArrayHandle _names_strings; 1630 char **_names_chars; 1631 typeArrayHandle _times; 1632 int _names_len; 1633 int _times_len; 1634 int _count; 1635 1636 public: 1637 ThreadTimesClosure(objArrayHandle names, typeArrayHandle times); 1638 ~ThreadTimesClosure(); 1639 virtual void do_thread(Thread* thread); 1640 void do_unlocked(); 1641 int count() { return _count; } 1642 }; 1643 1644 ThreadTimesClosure::ThreadTimesClosure(objArrayHandle names, | 849 // exclude externally visible JavaThreads 850 if (thread->is_Java_thread() && !thread->is_hidden_from_external_view()) { 851 return; 852 } 853 854 _count++; 855 } 856 857 static jint get_vm_thread_count() { 858 VmThreadCountClosure vmtcc; 859 { 860 MutexLockerEx ml(Threads_lock); 861 Threads::threads_do(&vmtcc); 862 } 863 864 return vmtcc.count(); 865 } 866 867 static jint get_num_flags() { 868 // last flag entry is always NULL, so subtract 1 869 int nFlags = (int) JVMFlag::numFlags - 1; 870 int count = 0; 871 for (int i = 0; i < nFlags; i++) { 872 JVMFlag* flag = &JVMFlag::flags[i]; 873 // Exclude the locked (diagnostic, experimental) flags 874 if (flag->is_unlocked() || flag->is_unlocker()) { 875 count++; 876 } 877 } 878 return count; 879 } 880 881 static jlong get_long_attribute(jmmLongAttribute att) { 882 switch (att) { 883 case JMM_CLASS_LOADED_COUNT: 884 return ClassLoadingService::loaded_class_count(); 885 886 case JMM_CLASS_UNLOADED_COUNT: 887 return ClassLoadingService::unloaded_class_count(); 888 889 case JMM_THREAD_TOTAL_COUNT: 890 return ThreadService::get_total_thread_count(); 891 892 case JMM_THREAD_LIVE_COUNT: 1402 "Invalid thread ID", -1); 1403 } 1404 1405 JavaThread* java_thread = NULL; 1406 if (thread_id == 0) { 1407 // current thread 1408 return os::current_thread_cpu_time(); 1409 } else { 1410 ThreadsListHandle tlh; 1411 java_thread = tlh.list()->find_JavaThread_from_java_tid(thread_id); 1412 if (java_thread != NULL) { 1413 return os::thread_cpu_time((Thread*) java_thread); 1414 } 1415 } 1416 return -1; 1417 JVM_END 1418 1419 // Returns a String array of all VM global flag names 1420 JVM_ENTRY(jobjectArray, jmm_GetVMGlobalNames(JNIEnv *env)) 1421 // last flag entry is always NULL, so subtract 1 1422 int nFlags = (int) JVMFlag::numFlags - 1; 1423 // allocate a temp array 1424 objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), 1425 nFlags, CHECK_0); 1426 objArrayHandle flags_ah(THREAD, r); 1427 int num_entries = 0; 1428 for (int i = 0; i < nFlags; i++) { 1429 JVMFlag* flag = &JVMFlag::flags[i]; 1430 // Exclude notproduct and develop flags in product builds. 1431 if (flag->is_constant_in_binary()) { 1432 continue; 1433 } 1434 // Exclude the locked (experimental, diagnostic) flags 1435 if (flag->is_unlocked() || flag->is_unlocker()) { 1436 Handle s = java_lang_String::create_from_str(flag->_name, CHECK_0); 1437 flags_ah->obj_at_put(num_entries, s()); 1438 num_entries++; 1439 } 1440 } 1441 1442 if (num_entries < nFlags) { 1443 // Return array of right length 1444 objArrayOop res = oopFactory::new_objArray(SystemDictionary::String_klass(), num_entries, CHECK_0); 1445 for(int i = 0; i < num_entries; i++) { 1446 res->obj_at_put(i, flags_ah->obj_at(i)); 1447 } 1448 return (jobjectArray)JNIHandles::make_local(env, res); 1449 } 1450 1451 return (jobjectArray)JNIHandles::make_local(env, flags_ah()); 1452 JVM_END 1453 1454 // Utility function used by jmm_GetVMGlobals. Returns false if flag type 1455 // can't be determined, true otherwise. If false is returned, then *global 1456 // will be incomplete and invalid. 1457 bool add_global_entry(JNIEnv* env, Handle name, jmmVMGlobal *global, JVMFlag *flag, TRAPS) { 1458 Handle flag_name; 1459 if (name() == NULL) { 1460 flag_name = java_lang_String::create_from_str(flag->_name, CHECK_false); 1461 } else { 1462 flag_name = name; 1463 } 1464 global->name = (jstring)JNIHandles::make_local(env, flag_name()); 1465 1466 if (flag->is_bool()) { 1467 global->value.z = flag->get_bool() ? JNI_TRUE : JNI_FALSE; 1468 global->type = JMM_VMGLOBAL_TYPE_JBOOLEAN; 1469 } else if (flag->is_int()) { 1470 global->value.j = (jlong)flag->get_int(); 1471 global->type = JMM_VMGLOBAL_TYPE_JLONG; 1472 } else if (flag->is_uint()) { 1473 global->value.j = (jlong)flag->get_uint(); 1474 global->type = JMM_VMGLOBAL_TYPE_JLONG; 1475 } else if (flag->is_intx()) { 1476 global->value.j = (jlong)flag->get_intx(); 1477 global->type = JMM_VMGLOBAL_TYPE_JLONG; 1482 global->value.j = (jlong)flag->get_uint64_t(); 1483 global->type = JMM_VMGLOBAL_TYPE_JLONG; 1484 } else if (flag->is_double()) { 1485 global->value.d = (jdouble)flag->get_double(); 1486 global->type = JMM_VMGLOBAL_TYPE_JDOUBLE; 1487 } else if (flag->is_size_t()) { 1488 global->value.j = (jlong)flag->get_size_t(); 1489 global->type = JMM_VMGLOBAL_TYPE_JLONG; 1490 } else if (flag->is_ccstr()) { 1491 Handle str = java_lang_String::create_from_str(flag->get_ccstr(), CHECK_false); 1492 global->value.l = (jobject)JNIHandles::make_local(env, str()); 1493 global->type = JMM_VMGLOBAL_TYPE_JSTRING; 1494 } else { 1495 global->type = JMM_VMGLOBAL_TYPE_UNKNOWN; 1496 return false; 1497 } 1498 1499 global->writeable = flag->is_writeable(); 1500 global->external = flag->is_external(); 1501 switch (flag->get_origin()) { 1502 case JVMFlag::DEFAULT: 1503 global->origin = JMM_VMGLOBAL_ORIGIN_DEFAULT; 1504 break; 1505 case JVMFlag::COMMAND_LINE: 1506 global->origin = JMM_VMGLOBAL_ORIGIN_COMMAND_LINE; 1507 break; 1508 case JVMFlag::ENVIRON_VAR: 1509 global->origin = JMM_VMGLOBAL_ORIGIN_ENVIRON_VAR; 1510 break; 1511 case JVMFlag::CONFIG_FILE: 1512 global->origin = JMM_VMGLOBAL_ORIGIN_CONFIG_FILE; 1513 break; 1514 case JVMFlag::MANAGEMENT: 1515 global->origin = JMM_VMGLOBAL_ORIGIN_MANAGEMENT; 1516 break; 1517 case JVMFlag::ERGONOMIC: 1518 global->origin = JMM_VMGLOBAL_ORIGIN_ERGONOMIC; 1519 break; 1520 case JVMFlag::ATTACH_ON_DEMAND: 1521 global->origin = JMM_VMGLOBAL_ORIGIN_ATTACH_ON_DEMAND; 1522 break; 1523 default: 1524 global->origin = JMM_VMGLOBAL_ORIGIN_OTHER; 1525 } 1526 1527 return true; 1528 } 1529 1530 // Fill globals array of count length with jmmVMGlobal entries 1531 // specified by names. If names == NULL, fill globals array 1532 // with all Flags. Return value is number of entries 1533 // created in globals. 1534 // If a JVMFlag with a given name in an array element does not 1535 // exist, globals[i].name will be set to NULL. 1536 JVM_ENTRY(jint, jmm_GetVMGlobals(JNIEnv *env, 1537 jobjectArray names, 1538 jmmVMGlobal *globals, 1539 jint count)) 1540 1541 1542 if (globals == NULL) { 1543 THROW_(vmSymbols::java_lang_NullPointerException(), 0); 1544 } 1545 1546 ResourceMark rm(THREAD); 1547 1548 if (names != NULL) { 1549 // return the requested globals 1550 objArrayOop ta = objArrayOop(JNIHandles::resolve_non_null(names)); 1551 objArrayHandle names_ah(THREAD, ta); 1552 // Make sure we have a String array 1553 Klass* element_klass = ObjArrayKlass::cast(names_ah->klass())->element_klass(); 1554 if (element_klass != SystemDictionary::String_klass()) { 1555 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), 1556 "Array element type is not String class", 0); 1557 } 1558 1559 int names_length = names_ah->length(); 1560 int num_entries = 0; 1561 for (int i = 0; i < names_length && i < count; i++) { 1562 oop s = names_ah->obj_at(i); 1563 if (s == NULL) { 1564 THROW_(vmSymbols::java_lang_NullPointerException(), 0); 1565 } 1566 1567 Handle sh(THREAD, s); 1568 char* str = java_lang_String::as_utf8_string(s); 1569 JVMFlag* flag = JVMFlag::find_flag(str, strlen(str)); 1570 if (flag != NULL && 1571 add_global_entry(env, sh, &globals[i], flag, THREAD)) { 1572 num_entries++; 1573 } else { 1574 globals[i].name = NULL; 1575 } 1576 } 1577 return num_entries; 1578 } else { 1579 // return all globals if names == NULL 1580 1581 // last flag entry is always NULL, so subtract 1 1582 int nFlags = (int) JVMFlag::numFlags - 1; 1583 Handle null_h; 1584 int num_entries = 0; 1585 for (int i = 0; i < nFlags && num_entries < count; i++) { 1586 JVMFlag* flag = &JVMFlag::flags[i]; 1587 // Exclude notproduct and develop flags in product builds. 1588 if (flag->is_constant_in_binary()) { 1589 continue; 1590 } 1591 // Exclude the locked (diagnostic, experimental) flags 1592 if ((flag->is_unlocked() || flag->is_unlocker()) && 1593 add_global_entry(env, null_h, &globals[num_entries], flag, THREAD)) { 1594 num_entries++; 1595 } 1596 } 1597 return num_entries; 1598 } 1599 JVM_END 1600 1601 JVM_ENTRY(void, jmm_SetVMGlobal(JNIEnv *env, jstring flag_name, jvalue new_value)) 1602 ResourceMark rm(THREAD); 1603 1604 oop fn = JNIHandles::resolve_external_guard(flag_name); 1605 if (fn == NULL) { 1606 THROW_MSG(vmSymbols::java_lang_NullPointerException(), 1607 "The flag name cannot be null."); 1608 } 1609 char* name = java_lang_String::as_utf8_string(fn); 1610 1611 FormatBuffer<80> error_msg("%s", ""); 1612 int succeed = WriteableFlags::set_flag(name, new_value, JVMFlag::MANAGEMENT, error_msg); 1613 1614 if (succeed != JVMFlag::SUCCESS) { 1615 if (succeed == JVMFlag::MISSING_VALUE) { 1616 // missing value causes NPE to be thrown 1617 THROW(vmSymbols::java_lang_NullPointerException()); 1618 } else { 1619 // all the other errors are reported as IAE with the appropriate error message 1620 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 1621 error_msg.buffer()); 1622 } 1623 } 1624 assert(succeed == JVMFlag::SUCCESS, "Setting flag should succeed"); 1625 JVM_END 1626 1627 class ThreadTimesClosure: public ThreadClosure { 1628 private: 1629 objArrayHandle _names_strings; 1630 char **_names_chars; 1631 typeArrayHandle _times; 1632 int _names_len; 1633 int _times_len; 1634 int _count; 1635 1636 public: 1637 ThreadTimesClosure(objArrayHandle names, typeArrayHandle times); 1638 ~ThreadTimesClosure(); 1639 virtual void do_thread(Thread* thread); 1640 void do_unlocked(); 1641 int count() { return _count; } 1642 }; 1643 1644 ThreadTimesClosure::ThreadTimesClosure(objArrayHandle names, |