Author: mladen.turk(a)jboss.com
Date: 2007-10-01 07:37:03 -0400 (Mon, 01 Oct 2007)
New Revision: 1072
Modified:
trunk/sight/native/os/windows/console.c
trunk/sight/native/os/windows/process.c
trunk/sight/native/share/library.c
trunk/sight/native/share/proc.c
Log:
Add progresive use detection
Modified: trunk/sight/native/os/windows/console.c
===================================================================
--- trunk/sight/native/os/windows/console.c 2007-10-01 10:29:06 UTC (rev 1071)
+++ trunk/sight/native/os/windows/console.c 2007-10-01 11:37:03 UTC (rev 1072)
@@ -59,9 +59,8 @@
sight_nt_console_t *con;
UNREFERENCED_O;
- if (!(con = (sight_nt_console_t *)calloc(1, sizeof(sight_nt_console_t)))) {
- throwAprMemoryException(_E, THROW_FMARK,
- apr_get_os_error());
+ if (!(con = (sight_nt_console_t *)sight_calloc(_E, sizeof(sight_nt_console_t),
+ THROW_FMARK))) {
return 0;
}
return P2J(con);
Modified: trunk/sight/native/os/windows/process.c
===================================================================
--- trunk/sight/native/os/windows/process.c 2007-10-01 10:29:06 UTC (rev 1071)
+++ trunk/sight/native/os/windows/process.c 2007-10-01 11:37:03 UTC (rev 1072)
@@ -386,128 +386,133 @@
#ifdef SIGHT_DO_STATS
no->clean = proc_cleanup;
#endif
- if (!find_pe32(pid, &pe32)) {
- /* System pids have low values */
- if (pid > 10)
- return APR_ENOENT;
- // Check if this is needed at all.
- lstrcpyW(pe32.szExeFile, L"Unknown");
- }
- else {
- ppid = pe32.th32ParentProcessID;
- tcnt = pe32.cntThreads;
- }
- if (pid == (jint)GetCurrentProcessId())
- hProcess = GetCurrentProcess();
- else {
- if (!(hProcess = OpenProcess(READ_CONTROL | PROCESS_VM_READ |
- PROCESS_QUERY_INFORMATION,
- FALSE, pid))) {
+ SIGHT_LOCAL_TRY(no) {
+ if (!find_pe32(pid, &pe32)) {
+ /* System pids have low values */
+ if (pid > 10) {
+ SIGHT_LOCAL_BRK(no);
+ return APR_ENOENT;
+ }
+ // Check if this is needed at all.
+ lstrcpyW(pe32.szExeFile, L"Unknown");
+ }
+ else {
+ ppid = pe32.th32ParentProcessID;
+ tcnt = pe32.cntThreads;
+ }
+ if (pid == (jint)GetCurrentProcessId())
+ hProcess = GetCurrentProcess();
+ else {
+ if (!(hProcess = OpenProcess(READ_CONTROL | PROCESS_VM_READ |
+ PROCESS_QUERY_INFORMATION,
+ FALSE, pid))) {
+ SET_IFIELD_W(0002, thiz, pe32.szExeFile);
+ SET_IFIELD_J(0000, thiz, ppid);
+ SET_IFIELD_J(0005, thiz, tcnt);
+ CALL_METHOD1(0000, thiz, SIGHT_PROC_R);
+ SIGHT_LOCAL_BRK(no);
+ return APR_SUCCESS;
+ }
+ }
+ if (GetModuleFileNameExW(hProcess, NULL, buf, SIGHT_STYPE_LEN)) {
+ SET_IFIELD_W(0001, thiz, buf);
+ }
+ if (GetModuleBaseNameW(hProcess, NULL, buf, SIGHT_STYPE_LEN)) {
+ SET_IFIELD_W(0002, thiz, buf);
+ }
+ else {
SET_IFIELD_W(0002, thiz, pe32.szExeFile);
- SET_IFIELD_J(0000, thiz, ppid);
- SET_IFIELD_J(0005, thiz, tcnt);
+ }
+ if (GetProcessIoCounters(hProcess, &ioc)) {
+ SET_IFIELD_J(0006, thiz, ioc.ReadOperationCount);
+ SET_IFIELD_J(0007, thiz, ioc.WriteOperationCount);
+ SET_IFIELD_J(0008, thiz, ioc.OtherOperationCount);
+ SET_IFIELD_J(0009, thiz, ioc.ReadTransferCount);
+ SET_IFIELD_J(0010, thiz, ioc.WriteTransferCount);
+ SET_IFIELD_J(0011, thiz, ioc.OtherTransferCount);
+ }
+ if (GetProcessTimes(hProcess, &ft[0], &ft[1], &ft[2], &ft[3])) {
+ et = winftime_to_ms(&ft[1]);
+ SET_IFIELD_J(0012, thiz, winftime_to_ms(&ft[0]));
+ SET_IFIELD_J(0013, thiz, et);
+ SET_IFIELD_J(0014, thiz, filetime_to_ms(&ft[2]));
+ SET_IFIELD_J(0015, thiz, filetime_to_ms(&ft[3]));
+ }
+ if (et) {
+ CALL_METHOD1(0000, thiz, SIGHT_PROC_T);
+ }
+ else {
CALL_METHOD1(0000, thiz, SIGHT_PROC_R);
- return APR_SUCCESS;
}
- }
- if (GetModuleFileNameExW(hProcess, NULL, buf, SIGHT_STYPE_LEN)) {
- SET_IFIELD_W(0001, thiz, buf);
- }
- if (GetModuleBaseNameW(hProcess, NULL, buf, SIGHT_STYPE_LEN)) {
- SET_IFIELD_W(0002, thiz, buf);
- }
- else {
- SET_IFIELD_W(0002, thiz, pe32.szExeFile);
- }
- if (GetProcessIoCounters(hProcess, &ioc)) {
- SET_IFIELD_J(0006, thiz, ioc.ReadOperationCount);
- SET_IFIELD_J(0007, thiz, ioc.WriteOperationCount);
- SET_IFIELD_J(0008, thiz, ioc.OtherOperationCount);
- SET_IFIELD_J(0009, thiz, ioc.ReadTransferCount);
- SET_IFIELD_J(0010, thiz, ioc.WriteTransferCount);
- SET_IFIELD_J(0011, thiz, ioc.OtherTransferCount);
- }
- if (GetProcessTimes(hProcess, &ft[0], &ft[1], &ft[2], &ft[3])) {
- et = winftime_to_ms(&ft[1]);
- SET_IFIELD_J(0012, thiz, winftime_to_ms(&ft[0]));
- SET_IFIELD_J(0013, thiz, et);
- SET_IFIELD_J(0014, thiz, filetime_to_ms(&ft[2]));
- SET_IFIELD_J(0015, thiz, filetime_to_ms(&ft[3]));
- }
- if (et) {
- CALL_METHOD1(0000, thiz, SIGHT_PROC_T);
- }
- else {
- CALL_METHOD1(0000, thiz, SIGHT_PROC_R);
- }
- /* XXX: Reading process memory is a hack.
- */
- if (pid != (jint)GetCurrentProcessId()) {
- read_proc_pages(pid, hProcess, &ppages[0], no->pool);
- if (ppages[0].str.w && (*ppages[0].str.w > 0x003C &&
*ppages[0].str.w < 0x007F)) {
- SET_IFIELD_O(0004, thiz, sight_mw_to_sa(_E, ppages[0].str.w));
- }
- if (ppages[1].str.w) {
- mproc = ppages[spage+1].str.w;
- mpnrd = ppages[spage+1].len;
- }
- if (mproc && mpnrd) {
- SIZE_T pO = NT32_CWDOFF;
- LPWSTR p1, p2, e1;
- int l1, l2;
- if ((p1 = (LPWSTR)get_procc_base(mproc, mpnrd, &pO))) {
- SET_IFIELD_W(0018, thiz, p1);
+ /* XXX: Reading process memory is a hack.
+ */
+ if (pid != (jint)GetCurrentProcessId()) {
+ read_proc_pages(pid, hProcess, &ppages[0], no->pool);
+ if (ppages[0].str.w && (*ppages[0].str.w > 0x003C &&
*ppages[0].str.w < 0x007F)) {
+ SET_IFIELD_O(0004, thiz, sight_mw_to_sa(_E, ppages[0].str.w));
}
- /* XXX Might not work on all Windows platforms */
- if ((p1 = (LPWSTR)get_procd_base(mproc, mpnrd, pO))) {
- LPWSTR *args;
- /* p1 Process PATH */
- l1 = lstrlenW(p1) + 1;
- p2 = (LPWSTR)ALIGN_TO_DWORD(p1 + l1);
- /* p2 Process Executable */
- l2 = lstrlenW(p2) + 1;
- e1 = (LPWSTR)ALIGN_TO_DWORD(p2 + l2);
- /* e1 Process Cmdline */
- args = (LPWSTR *)CommandLineToArgvW(e1, &l1);
- if (args) {
- SET_IFIELD_O(0003, thiz, sight_aw_to_sa(_E, args, l1));
- LocalFree(args);
+ if (ppages[1].str.w) {
+ mproc = ppages[spage+1].str.w;
+ mpnrd = ppages[spage+1].len;
+ }
+ if (mproc && mpnrd) {
+ SIZE_T pO = NT32_CWDOFF;
+ LPWSTR p1, p2, e1;
+ int l1, l2;
+ if ((p1 = (LPWSTR)get_procc_base(mproc, mpnrd, &pO))) {
+ SET_IFIELD_W(0018, thiz, p1);
}
+ /* XXX Might not work on all Windows platforms */
+ if ((p1 = (LPWSTR)get_procd_base(mproc, mpnrd, pO))) {
+ LPWSTR *args;
+ /* p1 Process PATH */
+ l1 = lstrlenW(p1) + 1;
+ p2 = (LPWSTR)ALIGN_TO_DWORD(p1 + l1);
+ /* p2 Process Executable */
+ l2 = lstrlenW(p2) + 1;
+ e1 = (LPWSTR)ALIGN_TO_DWORD(p2 + l2);
+ /* e1 Process Cmdline */
+ args = (LPWSTR *)CommandLineToArgvW(e1, &l1);
+ if (args) {
+ SET_IFIELD_O(0003, thiz, sight_aw_to_sa(_E, args, l1));
+ LocalFree(args);
+ }
+ }
}
}
- }
- else {
- LPVOID env;
- LPWSTR cmdl;
- int pargc;
- LPWSTR *pargs;
+ else {
+ LPVOID env;
+ LPWSTR cmdl;
+ int pargc;
+ LPWSTR *pargs;
- if ((env = GetEnvironmentStringsW())) {
- SET_IFIELD_O(0004, thiz, sight_mw_to_sa(_E, env));
- FreeEnvironmentStrings(env);
- }
+ if ((env = GetEnvironmentStringsW())) {
+ SET_IFIELD_O(0004, thiz, sight_mw_to_sa(_E, env));
+ FreeEnvironmentStrings(env);
+ }
- if ((cmdl = GetCommandLineW())) {
- pargs = (LPWSTR *)CommandLineToArgvW(cmdl, &pargc);
- if (pargs) {
- SET_IFIELD_O(0003, thiz, sight_aw_to_sa(_E, pargs, pargc));
- LocalFree(pargs);
+ if ((cmdl = GetCommandLineW())) {
+ pargs = (LPWSTR *)CommandLineToArgvW(cmdl, &pargc);
+ if (pargs) {
+ SET_IFIELD_O(0003, thiz, sight_aw_to_sa(_E, pargs, pargc));
+ LocalFree(pargs);
+ }
}
+ buf[0] = L'\0';
+ if (GetCurrentDirectoryW(SIGHT_STYPE_LEN, buf)) {
+ SET_IFIELD_W(0018, thiz, buf);
+ }
}
- buf[0] = L'\0';
- if (GetCurrentDirectoryW(SIGHT_STYPE_LEN, buf)) {
- SET_IFIELD_W(0018, thiz, buf);
+ if (sight_uid_get(hProcess, &uid, &gid, no->pool) == APR_SUCCESS) {
+ SET_IFIELD_J(0016, thiz, P2J(uid));
+ SET_IFIELD_J(0017, thiz, P2J(gid));
}
- }
- if (sight_uid_get(hProcess, &uid, &gid, no->pool) == APR_SUCCESS) {
- SET_IFIELD_J(0016, thiz, P2J(uid));
- SET_IFIELD_J(0017, thiz, P2J(gid));
- }
- CloseHandle(hProcess);
- SET_IFIELD_J(0000, thiz, ppid);
- SET_IFIELD_J(0005, thiz, tcnt);
+ CloseHandle(hProcess);
+ SET_IFIELD_J(0000, thiz, ppid);
+ SET_IFIELD_J(0005, thiz, tcnt);
+ } SIGHT_LOCAL_END(no);
return APR_SUCCESS;
}
Modified: trunk/sight/native/share/library.c
===================================================================
--- trunk/sight/native/share/library.c 2007-10-01 10:29:06 UTC (rev 1071)
+++ trunk/sight/native/share/library.c 2007-10-01 11:37:03 UTC (rev 1072)
@@ -228,8 +228,12 @@
fflush(stderr);
}
+#define OPTIMISTIC_LOCK_CNT 10000
+#define OPTIMISTIC_LOCK_MAX OPTIMISTIC_LOCK_CNT + 60000
+
SIGHT_EXPORT_DECLARE(void, Library, terminate0)(SIGHT_STDARGS)
{
+ apr_uint32_t ioc = 0;
UNREFERENCED_STDARGS;
/* TODO Add reference count */
@@ -242,6 +246,21 @@
while (in_object_count) {
apr_thread_yield();
in_object_count = apr_atomic_read32(sight_global_atomic);
+ if (ioc++ > OPTIMISTIC_LOCK_CNT) {
+ /* Sleep one millisecond */
+ apr_sleep(1000L);
+ }
+ if (ioc > OPTIMISTIC_LOCK_MAX) {
+ /* TODO: We have a zombie or a lengthy JNI op.
+ * Find a way to bail out without crushing JVM
+ */
+#ifdef SIGHT_DO_STATS
+ fprintf(stderr, "Library.terminate() call is locked ...\n");
+ fprintf(stderr, "Bailing out !\n");
+ fflush(stderr);
+ exit(-1);
+#endif
+ }
}
apr_pool_destroy(sight_temp_pool);
apr_pool_destroy(sight_global_pool);
Modified: trunk/sight/native/share/proc.c
===================================================================
--- trunk/sight/native/share/proc.c 2007-10-01 10:29:06 UTC (rev 1071)
+++ trunk/sight/native/share/proc.c 2007-10-01 11:37:03 UTC (rev 1072)
@@ -391,6 +391,7 @@
sight_callback_t cb;
UNREFERENCED_O;
+
if (!no || !no->native || !progress)
return APR_EINVAL;
SIGHT_LOCAL_TRY(no) {