• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "runtime/tooling/sampler/sample_info.h"
17 #include "runtime/tooling/sampler/thread_communicator.h"
18 
19 namespace panda::tooling::sampler {
20 
IsPipeEmpty() const21 bool ThreadCommunicator::IsPipeEmpty() const
22 {
23     ASSERT(listenerPipe_[PIPE_READ_ID] != 0 && listenerPipe_[PIPE_WRITE_ID] != 0);
24 
25     struct pollfd pollFd = {listenerPipe_[PIPE_READ_ID], POLLIN, 0};
26     return poll(&pollFd, 1, 0) == 0;
27 }
28 
SendSample(const SampleInfo & sample) const29 bool ThreadCommunicator::SendSample(const SampleInfo &sample) const
30 {
31     ASSERT(listenerPipe_[PIPE_READ_ID] != 0 && listenerPipe_[PIPE_WRITE_ID] != 0);
32 
33     const void *buffer = reinterpret_cast<const void *>(&sample);
34     ssize_t syscallResult = write(listenerPipe_[PIPE_WRITE_ID], buffer, sizeof(SampleInfo));
35     if (syscallResult == -1) {
36         return false;
37     }
38     ASSERT(syscallResult == sizeof(SampleInfo));
39     return true;
40 }
41 
ReadSample(SampleInfo * sample) const42 bool ThreadCommunicator::ReadSample(SampleInfo *sample) const
43 {
44     ASSERT(listenerPipe_[PIPE_READ_ID] != 0 && listenerPipe_[PIPE_WRITE_ID] != 0);
45 
46     void *buffer = reinterpret_cast<void *>(sample);
47 
48     // NOTE(m.strizhak): optimize by reading several samples by one call
49     ssize_t syscallResult = read(listenerPipe_[PIPE_READ_ID], buffer, sizeof(SampleInfo));
50     if (syscallResult == -1) {
51         return false;
52     }
53     ASSERT(syscallResult == sizeof(SampleInfo));
54     return true;
55 }
56 
57 }  // namespace panda::tooling::sampler
58