• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iostream>
17 #include <fstream>
18 #include <string>
19 
20 #include "runtime/tooling/sampler/sample_info.h"
21 #include "runtime/tooling/sampler/sample_writer.h"
22 
23 namespace ark::tooling::sampler {
24 
WriteSample(const SampleInfo & sample) const25 void StreamWriter::WriteSample(const SampleInfo &sample) const
26 {
27     ASSERT(writeStreamPtr_ != nullptr);
28     ASSERT(sample.stackInfo.managedStackSize <= SampleInfo::StackInfo::MAX_STACK_DEPTH);
29 
30     static_assert(sizeof(sample.threadInfo.threadId) == sizeof(uint32_t));
31     static_assert(sizeof(sample.threadInfo.threadStatus) == sizeof(uint32_t));
32     static_assert(sizeof(sample.stackInfo.managedStackSize) == sizeof(uintptr_t));
33 
34     writeStreamPtr_->write(reinterpret_cast<const char *>(&sample.threadInfo.threadId),
35                            sizeof(sample.threadInfo.threadId));
36     writeStreamPtr_->write(reinterpret_cast<const char *>(&sample.threadInfo.threadStatus),
37                            sizeof(sample.threadInfo.threadStatus));
38     writeStreamPtr_->write(reinterpret_cast<const char *>(&sample.stackInfo.managedStackSize),
39                            sizeof(sample.stackInfo.managedStackSize));
40     writeStreamPtr_->write(reinterpret_cast<const char *>(sample.stackInfo.managedStack.data()),
41                            sample.stackInfo.managedStackSize * sizeof(SampleInfo::ManagedStackFrameId));
42 }
43 
WriteModule(const FileInfo & moduleInfo)44 void StreamWriter::WriteModule(const FileInfo &moduleInfo)
45 {
46     ASSERT(writeStreamPtr_ != nullptr);
47     static_assert(sizeof(MODULE_INDICATOR_VALUE) == sizeof(uintptr_t));
48     static_assert(sizeof(moduleInfo.ptr) == sizeof(uintptr_t));
49     static_assert(sizeof(moduleInfo.checksum) == sizeof(uint32_t));
50     static_assert(sizeof(moduleInfo.pathname.length()) == sizeof(uintptr_t));
51 
52     if (moduleInfo.pathname.empty()) {
53         return;
54     }
55     size_t strSize = moduleInfo.pathname.length();
56 
57     writeStreamPtr_->write(reinterpret_cast<const char *>(&MODULE_INDICATOR_VALUE), sizeof(MODULE_INDICATOR_VALUE));
58     writeStreamPtr_->write(reinterpret_cast<const char *>(&moduleInfo.ptr), sizeof(moduleInfo.ptr));
59     writeStreamPtr_->write(reinterpret_cast<const char *>(&moduleInfo.checksum), sizeof(moduleInfo.checksum));
60     writeStreamPtr_->write(reinterpret_cast<const char *>(&strSize), sizeof(moduleInfo.pathname.length()));
61     writeStreamPtr_->write(moduleInfo.pathname.data(), moduleInfo.pathname.length() * sizeof(char));
62 
63     writtenModules_.insert(moduleInfo);
64 }
65 
66 }  // namespace ark::tooling::sampler
67