Merge remote-tracking branch 'redux/master' into sh4-pool
[tamarin-stm.git] / VMPI / SpyUtilsWinMo.cpp
blob79c6a2c289bea8d12c0649c2b2765c83f89d0a8f
1 /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is [Open Source Virtual Machine.].
18 * The Initial Developer of the Original Code is
19 * Adobe System Incorporated.
20 * Portions created by the Initial Developer are Copyright (C) 2004-2006
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Adobe AS3 Team
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "MMgc.h"
42 #ifdef MMGC_MEMORY_PROFILER
44 #include <Msgqueue.h>
46 /**
47 * compiled with the /W4 warning level
48 * which is quite picky. Disable warnings we don't care about.
50 #ifdef _MSC_VER
51 #pragma warning(disable:4127) //conditional expression is constant
52 #endif
54 class SignalData : public MMgc::GCAllocObject
56 public:
57 SignalData(uint32_t *addr, HANDLE handle)
58 : profilerAddr(addr), eventHandle(handle) {}
59 uint32_t *profilerAddr;
60 HANDLE eventHandle;
63 void WriteOnNamedSignal(const char* name, uint32_t *addr);
65 static SignalData *sig_data=NULL;
66 static bool spyRunning=false;
67 static vmpi_spin_lock_t lock;
69 DWORD WINAPI WaitForMemorySignal(LPVOID)
71 SignalData *sd = sig_data;
72 while(spyRunning) {
73 WaitForSingleObject(sig_data->eventHandle, INFINITE);
74 // For some reason ReadMsgQueue does not clear the signal on the handle (even though it should), and we
75 // end up setting the profilerAddr constantly, which makes the VM hang, since all it is doing is writing out profile data.
76 // so we have to call CloseMsgQueue, and then open a new queue, and wait on that.
77 //char buff[256];
78 //DWORD bytesread;
79 //ReadMsgQueue(sig_data->eventHandle, buff, 256, &bytesread, INFINITE, 0);
80 CloseMsgQueue(sd->eventHandle);
81 if(spyRunning) {
82 VMPI_lockAcquire(&lock);
83 *(sig_data->profilerAddr) = true;
84 VMPI_lockRelease(&lock);
85 WriteOnNamedSignal("MMgc::MemoryProfiler::DumpFatties", sd->profilerAddr);
86 break;
89 delete sd;
90 return 0;
93 void WriteOnNamedSignal(const char *name, uint32_t *addr)
95 HANDLE m_namedSharedObject;
97 MSGQUEUEOPTIONS msgopts;
99 msgopts.dwFlags = MSGQUEUE_NOPRECOMMIT;
100 msgopts.dwMaxMessages = 1;
101 msgopts.cbMaxMessage = 256;
102 msgopts.bReadAccess = TRUE;
103 msgopts.dwSize = sizeof(MSGQUEUEOPTIONS);
105 WCHAR wName[256];
107 MultiByteToWideChar(CP_ACP, 0, name, -1, wName, 256);
109 m_namedSharedObject = CreateMsgQueue(wName, &msgopts);
111 sig_data = new SignalData(addr, m_namedSharedObject);
112 spyRunning = true;
113 CreateThread(NULL, 0, WaitForMemorySignal, NULL, 0, NULL);
116 #include "windows.h"
118 static uint32_t mmgc_spy_signal = 0;
120 FILE* spyStream = NULL;
122 void SpyLog(const char* message)
124 fprintf(spyStream, "%s", message);
127 extern void RedirectLogOutput(void (*)(const char*));
129 void VMPI_spyCallback()
131 if(mmgc_spy_signal)
133 VMPI_lockAcquire(&lock);
134 if(mmgc_spy_signal)
136 mmgc_spy_signal = 0;
138 spyStream = fopen("Temp\\gcstats.txt", "w");
140 GCAssert(spyStream != NULL);
141 RedirectLogOutput(SpyLog);
143 MMgc::GCHeap::GetGCHeap()->DumpMemoryInfo();
145 fflush(spyStream);
147 fclose(spyStream);
148 RedirectLogOutput(NULL);
149 spyStream = NULL;
151 VMPI_lockRelase(&lock);
155 bool VMPI_spySetup()
157 VMPI_lockInit(&lock);
158 WriteOnNamedSignal("MMgc::MemoryProfiler::DumpFatties", &mmgc_spy_signal);
159 return true;
162 void VMPI_spyTeardown()
164 VMPI_lockDestroy(&lock);
165 spyRunning = false;
168 bool VMPI_hasSymbols()
170 return false;
173 #endif //MMGC_MEMORY_PROFILER