SourceDynamicProfileManager.h 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. class SourceContextInfo;
  7. #if ENABLE_PROFILE_INFO
  8. namespace Js
  9. {
  10. enum ExecutionFlags : BYTE
  11. {
  12. ExecutionFlags_NotExecuted = 0x00,
  13. ExecutionFlags_Executed = 0x01,
  14. ExecutionFlags_HasNoInfo = 0x02
  15. };
  16. //
  17. // For every source file, an instance of SourceDynamicProfileManager is used to save/load data.
  18. // It uses the WININET cache to save/load profile data.
  19. // For testing scenarios enabled using DYNAMIC_PROFILE_STORAGE macro, this can persist the profile info into a file as well.
  20. class SourceDynamicProfileManager
  21. {
  22. public:
  23. SourceDynamicProfileManager(Recycler* allocator) : isNonCachableScript(false), cachedStartupFunctions(nullptr), recycler(allocator),
  24. #ifdef DYNAMIC_PROFILE_STORAGE
  25. dynamicProfileInfoMapSaving(&HeapAllocator::Instance),
  26. #endif
  27. dynamicProfileInfoMap(allocator), startupFunctions(nullptr), dataCacheWrapper(nullptr)
  28. {
  29. }
  30. ExecutionFlags IsFunctionExecuted(Js::LocalFunctionId functionId);
  31. DynamicProfileInfo * GetDynamicProfileInfo(FunctionBody * functionBody);
  32. Recycler* GetRecycler() { return recycler; }
  33. void UpdateDynamicProfileInfo(LocalFunctionId functionId, DynamicProfileInfo * dynamicProfileInfo);
  34. void RemoveDynamicProfileInfo(LocalFunctionId functionId);
  35. void MarkAsExecuted(LocalFunctionId functionId);
  36. static SourceDynamicProfileManager * LoadFromDynamicProfileStorage(SourceContextInfo* info, ScriptContext* scriptContext, SimpleDataCacheWrapper* dataCacheWrapper);
  37. void EnsureStartupFunctions(uint numberOfFunctions);
  38. void Reuse();
  39. uint SaveToProfileCacheAndRelease(SourceContextInfo* info);
  40. bool IsProfileLoaded() { return cachedStartupFunctions != nullptr; }
  41. bool IsProfileLoadedFromWinInet() { return dataCacheWrapper != nullptr; }
  42. bool LoadFromProfileCache(SimpleDataCacheWrapper* dataCacheWrapper, LPCWSTR url);
  43. SimpleDataCacheWrapper* GetProfileCache() { return dataCacheWrapper; }
  44. uint GetStartupFunctionsLength() { return (this->startupFunctions ? this->startupFunctions->Length() : 0); }
  45. #ifdef DYNAMIC_PROFILE_STORAGE
  46. void ClearSavingData();
  47. #endif
  48. private:
  49. friend class DynamicProfileInfo;
  50. FieldNoBarrier(Recycler*) recycler;
  51. #ifdef DYNAMIC_PROFILE_STORAGE
  52. // while Finalizing Javascript library we can't allocate memory from recycler,
  53. // dynamicProfileInfoMapSaving is heap allocated and used for serializing dynamic profile cache
  54. typedef JsUtil::BaseDictionary<LocalFunctionId, DynamicProfileInfo *, HeapAllocator> DynamicProfileInfoMapSavingType;
  55. FieldNoBarrier(DynamicProfileInfoMapSavingType) dynamicProfileInfoMapSaving;
  56. void SaveDynamicProfileInfo(LocalFunctionId functionId, DynamicProfileInfo * dynamicProfileInfo);
  57. void SaveToDynamicProfileStorage(char16 const * url);
  58. void AddSavingItem(LocalFunctionId functionId, DynamicProfileInfo *info);
  59. template <typename T>
  60. static SourceDynamicProfileManager * Deserialize(T * reader, Recycler* allocator);
  61. template <typename T>
  62. bool Serialize(T * writer);
  63. #endif
  64. uint SaveToProfileCache();
  65. bool ShouldSaveToProfileCache(SourceContextInfo* info) const;
  66. //------ Private data members -------- /
  67. private:
  68. Field(bool) isNonCachableScript; // Indicates if this script can be cached in WININET
  69. Field(SimpleDataCacheWrapper*) dataCacheWrapper; // WININET based cache to store profile info
  70. Field(BVFixed*) startupFunctions; // Bit vector representing functions that are executed at startup
  71. Field(BVFixed const *) cachedStartupFunctions; // Bit vector representing functions executed at startup that are loaded from a persistent or in-memory cache
  72. // It's not modified but used as an input for deferred parsing/bytecodegen
  73. typedef JsUtil::BaseDictionary<LocalFunctionId, DynamicProfileInfo *, Recycler, PowerOf2SizePolicy> DynamicProfileInfoMapType;
  74. Field(DynamicProfileInfoMapType) dynamicProfileInfoMap;
  75. static const uint MAX_FUNCTION_COUNT = 10000; // Consider data corrupt if there are more functions than this
  76. };
  77. };
  78. #endif // ENABLE_PROFILE_INFO