Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_RUNTIME_OPENJDKJVMTI_EVENTS_H_ |
| 18 | #define ART_RUNTIME_OPENJDKJVMTI_EVENTS_H_ |
| 19 | |
| 20 | #include <bitset> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "base/logging.h" |
| 24 | #include "jvmti.h" |
| 25 | #include "thread.h" |
| 26 | |
| 27 | namespace openjdkjvmti { |
| 28 | |
| 29 | struct ArtJvmTiEnv; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 30 | class JvmtiAllocationListener; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame^] | 31 | class JvmtiGcPauseListener; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 32 | |
| 33 | struct EventMask { |
| 34 | static constexpr size_t kEventsSize = JVMTI_MAX_EVENT_TYPE_VAL - JVMTI_MIN_EVENT_TYPE_VAL + 1; |
| 35 | std::bitset<kEventsSize> bit_set; |
| 36 | |
| 37 | static bool EventIsInRange(jvmtiEvent event) { |
| 38 | return event >= JVMTI_MIN_EVENT_TYPE_VAL && event <= JVMTI_MAX_EVENT_TYPE_VAL; |
| 39 | } |
| 40 | |
| 41 | void Set(jvmtiEvent event, bool value = true) { |
| 42 | DCHECK(EventIsInRange(event)); |
| 43 | bit_set.set(event - JVMTI_MIN_EVENT_TYPE_VAL, value); |
| 44 | } |
| 45 | |
| 46 | bool Test(jvmtiEvent event) const { |
| 47 | DCHECK(EventIsInRange(event)); |
| 48 | return bit_set.test(event - JVMTI_MIN_EVENT_TYPE_VAL); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | struct EventMasks { |
| 53 | // The globally enabled events. |
| 54 | EventMask global_event_mask; |
| 55 | |
| 56 | // The per-thread enabled events. |
| 57 | |
| 58 | // It is not enough to store a Thread pointer, as these may be reused. Use the pointer and the |
| 59 | // thread id. |
| 60 | // Note: We could just use the tid like tracing does. |
| 61 | using UniqueThread = std::pair<art::Thread*, uint32_t>; |
| 62 | // TODO: Native thread objects are immovable, so we can use them as keys in an (unordered) map, |
| 63 | // if necessary. |
| 64 | std::vector<std::pair<UniqueThread, EventMask>> thread_event_masks; |
| 65 | |
| 66 | // A union of the per-thread events, for fast-pathing. |
| 67 | EventMask unioned_thread_event_mask; |
| 68 | |
| 69 | EventMask& GetEventMask(art::Thread* thread); |
| 70 | EventMask* GetEventMaskOrNull(art::Thread* thread); |
| 71 | void EnableEvent(art::Thread* thread, jvmtiEvent event); |
| 72 | void DisableEvent(art::Thread* thread, jvmtiEvent event); |
| 73 | }; |
| 74 | |
| 75 | // Helper class for event handling. |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 76 | class EventHandler { |
| 77 | public: |
| 78 | EventHandler(); |
| 79 | ~EventHandler(); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 80 | |
| 81 | // Register an env. It is assumed that this happens on env creation, that is, no events are |
| 82 | // enabled, yet. |
| 83 | void RegisterArtJvmTiEnv(ArtJvmTiEnv* env); |
| 84 | |
| 85 | bool IsEventEnabledAnywhere(jvmtiEvent event) { |
| 86 | if (!EventMask::EventIsInRange(event)) { |
| 87 | return false; |
| 88 | } |
| 89 | return global_mask.Test(event); |
| 90 | } |
| 91 | |
| 92 | jvmtiError SetEvent(ArtJvmTiEnv* env, art::Thread* thread, jvmtiEvent event, jvmtiEventMode mode); |
| 93 | |
| 94 | template <typename ...Args> |
| 95 | ALWAYS_INLINE inline void DispatchEvent(art::Thread* thread, jvmtiEvent event, Args... args); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 96 | |
| 97 | private: |
| 98 | void HandleEventType(jvmtiEvent event, bool enable); |
| 99 | |
| 100 | // List of all JvmTiEnv objects that have been created, in their creation order. |
| 101 | std::vector<ArtJvmTiEnv*> envs; |
| 102 | |
| 103 | // A union of all enabled events, anywhere. |
| 104 | EventMask global_mask; |
| 105 | |
| 106 | std::unique_ptr<JvmtiAllocationListener> alloc_listener_; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame^] | 107 | std::unique_ptr<JvmtiGcPauseListener> gc_pause_listener_; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } // namespace openjdkjvmti |
| 111 | |
| 112 | #endif // ART_RUNTIME_OPENJDKJVMTI_EVENTS_H_ |