1 /**
2 * Copyright (c) 2021-2024 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 ark::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 LOG_IF(syscallResult != sizeof(SampleInfo), FATAL, PROFILER)
39 << "unexpected sample write - sended " << syscallResult << " bytes";
40 return true;
41 }
42
ReadSample(SampleInfo * sample) const43 bool ThreadCommunicator::ReadSample(SampleInfo *sample) const
44 {
45 ASSERT(listenerPipe_[PIPE_READ_ID] != 0 && listenerPipe_[PIPE_WRITE_ID] != 0);
46
47 void *buffer = reinterpret_cast<void *>(sample);
48
49 // NOTE(m.strizhak): optimize by reading several samples by one call
50 ssize_t syscallResult = read(listenerPipe_[PIPE_READ_ID], buffer, sizeof(SampleInfo));
51 if (syscallResult == -1) {
52 return false;
53 }
54 LOG_IF(syscallResult != sizeof(SampleInfo), FATAL, PROFILER)
55 << "unexpected sample read - received " << syscallResult << " bytes";
56 return true;
57 }
58
59 } // namespace ark::tooling::sampler
60