LegacyProfiler.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 2008, 2012 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "config.h"
  29. #include "LegacyProfiler.h"
  30. #include "CallFrame.h"
  31. #include "CodeBlock.h"
  32. #include "CommonIdentifiers.h"
  33. #include "InternalFunction.h"
  34. #include "JSFunction.h"
  35. #include "JSGlobalObject.h"
  36. #include "Nodes.h"
  37. #include "Operations.h"
  38. #include "Profile.h"
  39. #include "ProfileGenerator.h"
  40. #include "ProfileNode.h"
  41. #include <stdio.h>
  42. namespace JSC {
  43. static const char* GlobalCodeExecution = "(program)";
  44. static const char* AnonymousFunction = "(anonymous function)";
  45. static unsigned ProfilesUID = 0;
  46. static CallIdentifier createCallIdentifierFromFunctionImp(ExecState*, JSObject*, const String& defaultSourceURL, int defaultLineNumber);
  47. LegacyProfiler* LegacyProfiler::s_sharedLegacyProfiler = 0;
  48. LegacyProfiler* LegacyProfiler::profiler()
  49. {
  50. if (!s_sharedLegacyProfiler)
  51. s_sharedLegacyProfiler = new LegacyProfiler();
  52. return s_sharedLegacyProfiler;
  53. }
  54. void LegacyProfiler::startProfiling(ExecState* exec, const String& title)
  55. {
  56. ASSERT_ARG(title, !title.isNull());
  57. if (!exec)
  58. return;
  59. // Check if we currently have a Profile for this global ExecState and title.
  60. // If so return early and don't create a new Profile.
  61. JSGlobalObject* origin = exec->lexicalGlobalObject();
  62. for (size_t i = 0; i < m_currentProfiles.size(); ++i) {
  63. ProfileGenerator* profileGenerator = m_currentProfiles[i].get();
  64. if (profileGenerator->origin() == origin && profileGenerator->title() == title)
  65. return;
  66. }
  67. exec->vm().m_enabledProfiler = this;
  68. RefPtr<ProfileGenerator> profileGenerator = ProfileGenerator::create(exec, title, ++ProfilesUID);
  69. m_currentProfiles.append(profileGenerator);
  70. }
  71. PassRefPtr<Profile> LegacyProfiler::stopProfiling(ExecState* exec, const String& title)
  72. {
  73. if (!exec)
  74. return 0;
  75. JSGlobalObject* origin = exec->lexicalGlobalObject();
  76. for (ptrdiff_t i = m_currentProfiles.size() - 1; i >= 0; --i) {
  77. ProfileGenerator* profileGenerator = m_currentProfiles[i].get();
  78. if (profileGenerator->origin() == origin && (title.isNull() || profileGenerator->title() == title)) {
  79. profileGenerator->stopProfiling();
  80. RefPtr<Profile> returnProfile = profileGenerator->profile();
  81. m_currentProfiles.remove(i);
  82. if (!m_currentProfiles.size())
  83. exec->vm().m_enabledProfiler = 0;
  84. return returnProfile;
  85. }
  86. }
  87. return 0;
  88. }
  89. void LegacyProfiler::stopProfiling(JSGlobalObject* origin)
  90. {
  91. for (ptrdiff_t i = m_currentProfiles.size() - 1; i >= 0; --i) {
  92. ProfileGenerator* profileGenerator = m_currentProfiles[i].get();
  93. if (profileGenerator->origin() == origin) {
  94. profileGenerator->stopProfiling();
  95. m_currentProfiles.remove(i);
  96. if (!m_currentProfiles.size())
  97. origin->vm().m_enabledProfiler = 0;
  98. }
  99. }
  100. }
  101. static inline void dispatchFunctionToProfiles(ExecState* callerOrHandlerCallFrame, const Vector<RefPtr<ProfileGenerator> >& profiles, ProfileGenerator::ProfileFunction function, const CallIdentifier& callIdentifier, unsigned currentProfileTargetGroup)
  102. {
  103. for (size_t i = 0; i < profiles.size(); ++i) {
  104. if (profiles[i]->profileGroup() == currentProfileTargetGroup || !profiles[i]->origin())
  105. (profiles[i].get()->*function)(callerOrHandlerCallFrame, callIdentifier);
  106. }
  107. }
  108. void LegacyProfiler::willExecute(ExecState* callerCallFrame, JSValue function)
  109. {
  110. ASSERT(!m_currentProfiles.isEmpty());
  111. dispatchFunctionToProfiles(callerCallFrame, m_currentProfiles, &ProfileGenerator::willExecute, createCallIdentifier(callerCallFrame, function, "", 0), callerCallFrame->lexicalGlobalObject()->profileGroup());
  112. }
  113. void LegacyProfiler::willExecute(ExecState* callerCallFrame, const String& sourceURL, int startingLineNumber)
  114. {
  115. ASSERT(!m_currentProfiles.isEmpty());
  116. CallIdentifier callIdentifier = createCallIdentifier(callerCallFrame, JSValue(), sourceURL, startingLineNumber);
  117. dispatchFunctionToProfiles(callerCallFrame, m_currentProfiles, &ProfileGenerator::willExecute, callIdentifier, callerCallFrame->lexicalGlobalObject()->profileGroup());
  118. }
  119. void LegacyProfiler::didExecute(ExecState* callerCallFrame, JSValue function)
  120. {
  121. ASSERT(!m_currentProfiles.isEmpty());
  122. dispatchFunctionToProfiles(callerCallFrame, m_currentProfiles, &ProfileGenerator::didExecute, createCallIdentifier(callerCallFrame, function, "", 0), callerCallFrame->lexicalGlobalObject()->profileGroup());
  123. }
  124. void LegacyProfiler::didExecute(ExecState* callerCallFrame, const String& sourceURL, int startingLineNumber)
  125. {
  126. ASSERT(!m_currentProfiles.isEmpty());
  127. dispatchFunctionToProfiles(callerCallFrame, m_currentProfiles, &ProfileGenerator::didExecute, createCallIdentifier(callerCallFrame, JSValue(), sourceURL, startingLineNumber), callerCallFrame->lexicalGlobalObject()->profileGroup());
  128. }
  129. void LegacyProfiler::exceptionUnwind(ExecState* handlerCallFrame)
  130. {
  131. ASSERT(!m_currentProfiles.isEmpty());
  132. dispatchFunctionToProfiles(handlerCallFrame, m_currentProfiles, &ProfileGenerator::exceptionUnwind, createCallIdentifier(handlerCallFrame, JSValue(), "", 0), handlerCallFrame->lexicalGlobalObject()->profileGroup());
  133. }
  134. CallIdentifier LegacyProfiler::createCallIdentifier(ExecState* exec, JSValue functionValue, const String& defaultSourceURL, int defaultLineNumber)
  135. {
  136. if (!functionValue)
  137. return CallIdentifier(GlobalCodeExecution, defaultSourceURL, defaultLineNumber);
  138. if (!functionValue.isObject())
  139. return CallIdentifier("(unknown)", defaultSourceURL, defaultLineNumber);
  140. if (asObject(functionValue)->inherits(&JSFunction::s_info) || asObject(functionValue)->inherits(&InternalFunction::s_info))
  141. return createCallIdentifierFromFunctionImp(exec, asObject(functionValue), defaultSourceURL, defaultLineNumber);
  142. return CallIdentifier(makeString("(", asObject(functionValue)->methodTable()->className(asObject(functionValue)), " object)"), defaultSourceURL, defaultLineNumber);
  143. }
  144. CallIdentifier createCallIdentifierFromFunctionImp(ExecState* exec, JSObject* function, const String& defaultSourceURL, int defaultLineNumber)
  145. {
  146. const String& name = getCalculatedDisplayName(exec, function);
  147. JSFunction* jsFunction = jsDynamicCast<JSFunction*>(function);
  148. if (jsFunction && !jsFunction->isHostFunction())
  149. return CallIdentifier(name.isEmpty() ? AnonymousFunction : name, jsFunction->jsExecutable()->sourceURL(), jsFunction->jsExecutable()->lineNo());
  150. return CallIdentifier(name.isEmpty() ? AnonymousFunction : name, defaultSourceURL, defaultLineNumber);
  151. }
  152. } // namespace JSC