OSDN Git Service

Merge "Second round of changes to 'perf' profile collection daemon."
[android-x86/system-extras.git] / simpleperf / event_type.cpp
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>
22 #include "event_attr.h"
23 #include "event_fd.h"
24
25 #define EVENT_TYPE_TABLE_ENTRY(name, type, config) \
26   { name, type, config }                           \
27   ,
28
29 static std::vector<const EventType> event_type_array = {
30 #include "event_type_table.h"
31 };
32
33 static bool IsEventTypeSupportedByKernel(const EventType& event_type) {
34   auto event_fd = EventFd::OpenEventFileForProcess(
35       EventAttr::CreateDefaultAttrToMonitorEvent(event_type), getpid());
36   return event_fd != nullptr;
37 }
38
39 bool EventType::IsSupportedByKernel() const {
40   return IsEventTypeSupportedByKernel(*this);
41 }
42
43 const std::vector<const EventType>& EventTypeFactory::GetAllEventTypes() {
44   return event_type_array;
45 }
46
47 const EventType* EventTypeFactory::FindEventTypeByName(const std::string& name) {
48   for (auto& event_type : event_type_array) {
49     if (event_type.name == name) {
50       return &event_type;
51     }
52   }
53   return nullptr;
54 }
55
56 const EventType* EventTypeFactory::FindEventTypeByConfig(uint32_t type, uint64_t config) {
57   for (auto& event_type : event_type_array) {
58     if (event_type.type == type && event_type.config == config) {
59       return &event_type;
60     }
61   }
62   return nullptr;
63 }