Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 SIMPLE_PERF_INPLACE_SAMPLER_CLIENT_H_ |
| 18 | #define SIMPLE_PERF_INPLACE_SAMPLER_CLIENT_H_ |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <set> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "event_attr.h" |
| 25 | #include "record.h" |
| 26 | #include "UnixSocket.h" |
| 27 | |
| 28 | class InplaceSamplerClient { |
| 29 | public: |
| 30 | static std::unique_ptr<InplaceSamplerClient> Create(const perf_event_attr& attr, pid_t pid, |
| 31 | const std::set<pid_t>& tids); |
| 32 | uint64_t Id() const; |
| 33 | bool IsClosed() { |
Yabin Cui | b92bae8 | 2017-02-10 12:07:29 -0800 | [diff] [blame] | 34 | return conn_->IsClosed(); |
Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 35 | } |
| 36 | bool StartPolling(IOEventLoop& loop, const std::function<bool(Record*)>& record_callback, |
| 37 | const std::function<bool()>& close_callback); |
Yabin Cui | b92bae8 | 2017-02-10 12:07:29 -0800 | [diff] [blame] | 38 | bool StopProfiling(IOEventLoop& loop, const std::function<bool()>& close_callback); |
Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 39 | |
| 40 | private: |
| 41 | InplaceSamplerClient(const perf_event_attr& attr, pid_t pid, const std::set<pid_t>& tids); |
| 42 | bool ConnectServer(); |
Yabin Cui | b92bae8 | 2017-02-10 12:07:29 -0800 | [diff] [blame] | 43 | bool SendStartProfilingMessage(); |
| 44 | bool HandleMessage(const UnixSocketMessage& msg); |
Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 45 | |
| 46 | const perf_event_attr attr_; |
| 47 | const pid_t pid_; |
| 48 | const std::set<pid_t> tids_; |
Yabin Cui | b92bae8 | 2017-02-10 12:07:29 -0800 | [diff] [blame] | 49 | uint32_t sample_freq_; |
| 50 | std::unique_ptr<UnixSocketConnection> conn_; |
Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 51 | std::function<bool(Record*)> record_callback_; |
Yabin Cui | b92bae8 | 2017-02-10 12:07:29 -0800 | [diff] [blame] | 52 | bool got_start_profiling_reply_msg_; |
Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | #endif // SIMPLE_PERF_INPLACE_SAMPLER_CLIENT_H_ |