Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #include "event_type.h" |
| 18 | |
| 19 | #include <unistd.h> |
| 20 | #include <string> |
| 21 | #include <vector> |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 22 | |
| 23 | #include <base/logging.h> |
| 24 | |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 25 | #include "event_attr.h" |
| 26 | #include "event_fd.h" |
| 27 | |
| 28 | #define EVENT_TYPE_TABLE_ENTRY(name, type, config) \ |
| 29 | { name, type, config } \ |
| 30 | , |
| 31 | |
| 32 | static std::vector<const EventType> event_type_array = { |
| 33 | #include "event_type_table.h" |
| 34 | }; |
| 35 | |
| 36 | static bool IsEventTypeSupportedByKernel(const EventType& event_type) { |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 37 | auto event_fd = EventFd::OpenEventFileForProcess(CreateDefaultPerfEventAttr(event_type), getpid()); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 38 | return event_fd != nullptr; |
| 39 | } |
| 40 | |
| 41 | bool EventType::IsSupportedByKernel() const { |
| 42 | return IsEventTypeSupportedByKernel(*this); |
| 43 | } |
| 44 | |
| 45 | const std::vector<const EventType>& EventTypeFactory::GetAllEventTypes() { |
| 46 | return event_type_array; |
| 47 | } |
| 48 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 49 | const EventType* EventTypeFactory::FindEventTypeByName(const std::string& name, |
| 50 | bool report_unsupported_type) { |
| 51 | const EventType* result = nullptr; |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 52 | for (auto& event_type : event_type_array) { |
| 53 | if (event_type.name == name) { |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 54 | result = &event_type; |
| 55 | break; |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 58 | if (result == nullptr) { |
| 59 | LOG(ERROR) << "Unknown event_type '" << name |
| 60 | << "', try `simpleperf list` to list all possible event type names"; |
| 61 | return nullptr; |
| 62 | } |
| 63 | if (!result->IsSupportedByKernel()) { |
Yabin Cui | 568b82a | 2015-05-05 19:58:07 -0700 | [diff] [blame] | 64 | (report_unsupported_type ? PLOG(ERROR) : PLOG(DEBUG)) << "Event type '" << result->name |
| 65 | << "' is not supported by the kernel"; |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 66 | return nullptr; |
| 67 | } |
| 68 | return result; |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | const EventType* EventTypeFactory::FindEventTypeByConfig(uint32_t type, uint64_t config) { |
| 72 | for (auto& event_type : event_type_array) { |
| 73 | if (event_type.type == type && event_type.config == config) { |
| 74 | return &event_type; |
| 75 | } |
| 76 | } |
| 77 | return nullptr; |
| 78 | } |