|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef MOZ_PROFILE_ENTRY_H |
|
8 #define MOZ_PROFILE_ENTRY_H |
|
9 |
|
10 #include <ostream> |
|
11 #include "GeckoProfiler.h" |
|
12 #include "platform.h" |
|
13 #include "JSStreamWriter.h" |
|
14 #include "ProfilerBacktrace.h" |
|
15 #include "mozilla/Mutex.h" |
|
16 |
|
17 class ThreadProfile; |
|
18 |
|
19 #pragma pack(push, 1) |
|
20 |
|
21 class ProfileEntry |
|
22 { |
|
23 public: |
|
24 ProfileEntry(); |
|
25 |
|
26 // aTagData must not need release (i.e. be a string from the text segment) |
|
27 ProfileEntry(char aTagName, const char *aTagData); |
|
28 ProfileEntry(char aTagName, void *aTagPtr); |
|
29 ProfileEntry(char aTagName, ProfilerMarker *aTagMarker); |
|
30 ProfileEntry(char aTagName, float aTagFloat); |
|
31 ProfileEntry(char aTagName, uintptr_t aTagOffset); |
|
32 ProfileEntry(char aTagName, Address aTagAddress); |
|
33 ProfileEntry(char aTagName, int aTagLine); |
|
34 ProfileEntry(char aTagName, char aTagChar); |
|
35 friend std::ostream& operator<<(std::ostream& stream, const ProfileEntry& entry); |
|
36 bool is_ent_hint(char hintChar); |
|
37 bool is_ent_hint(); |
|
38 bool is_ent(char tagName); |
|
39 void* get_tagPtr(); |
|
40 void log(); |
|
41 const ProfilerMarker* getMarker() { |
|
42 MOZ_ASSERT(mTagName == 'm'); |
|
43 return mTagMarker; |
|
44 } |
|
45 |
|
46 char getTagName() const { return mTagName; } |
|
47 |
|
48 private: |
|
49 friend class ThreadProfile; |
|
50 union { |
|
51 const char* mTagData; |
|
52 char mTagChars[sizeof(void*)]; |
|
53 void* mTagPtr; |
|
54 ProfilerMarker* mTagMarker; |
|
55 float mTagFloat; |
|
56 Address mTagAddress; |
|
57 uintptr_t mTagOffset; |
|
58 int mTagLine; |
|
59 char mTagChar; |
|
60 }; |
|
61 char mTagName; |
|
62 }; |
|
63 |
|
64 #pragma pack(pop) |
|
65 |
|
66 typedef void (*IterateTagsCallback)(const ProfileEntry& entry, const char* tagStringData); |
|
67 |
|
68 class ThreadProfile |
|
69 { |
|
70 public: |
|
71 ThreadProfile(const char* aName, int aEntrySize, PseudoStack *aStack, |
|
72 Thread::tid_t aThreadId, PlatformData* aPlatformData, |
|
73 bool aIsMainThread, void *aStackTop); |
|
74 virtual ~ThreadProfile(); |
|
75 void addTag(ProfileEntry aTag); |
|
76 void flush(); |
|
77 void erase(); |
|
78 char* processDynamicTag(int readPos, int* tagsConsumed, char* tagBuff); |
|
79 void IterateTags(IterateTagsCallback aCallback); |
|
80 friend std::ostream& operator<<(std::ostream& stream, |
|
81 const ThreadProfile& profile); |
|
82 void ToStreamAsJSON(std::ostream& stream); |
|
83 JSObject *ToJSObject(JSContext *aCx); |
|
84 PseudoStack* GetPseudoStack(); |
|
85 mozilla::Mutex* GetMutex(); |
|
86 void StreamJSObject(JSStreamWriter& b); |
|
87 void BeginUnwind(); |
|
88 virtual void EndUnwind(); |
|
89 virtual SyncProfile* AsSyncProfile() { return nullptr; } |
|
90 |
|
91 bool IsMainThread() const { return mIsMainThread; } |
|
92 const char* Name() const { return mName; } |
|
93 Thread::tid_t ThreadId() const { return mThreadId; } |
|
94 |
|
95 PlatformData* GetPlatformData() { return mPlatformData; } |
|
96 int GetGenerationID() const { return mGeneration; } |
|
97 bool HasGenerationExpired(int aGenID) { |
|
98 return aGenID + 2 <= mGeneration; |
|
99 } |
|
100 void* GetStackTop() const { return mStackTop; } |
|
101 void DuplicateLastSample(); |
|
102 private: |
|
103 // Circular buffer 'Keep One Slot Open' implementation |
|
104 // for simplicity |
|
105 ProfileEntry* mEntries; |
|
106 int mWritePos; // points to the next entry we will write to |
|
107 int mLastFlushPos; // points to the next entry since the last flush() |
|
108 int mReadPos; // points to the next entry we will read to |
|
109 int mEntrySize; |
|
110 PseudoStack* mPseudoStack; |
|
111 mozilla::Mutex mMutex; |
|
112 char* mName; |
|
113 Thread::tid_t mThreadId; |
|
114 bool mIsMainThread; |
|
115 PlatformData* mPlatformData; // Platform specific data. |
|
116 int mGeneration; |
|
117 int mPendingGenerationFlush; |
|
118 void* const mStackTop; |
|
119 }; |
|
120 |
|
121 std::ostream& operator<<(std::ostream& stream, const ThreadProfile& profile); |
|
122 |
|
123 #endif /* ndef MOZ_PROFILE_ENTRY_H */ |