• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2021 Huawei Technologies Co., Ltd
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 #include "backend/kernel_compiler/gpu/data/dataset_profiling.h"
17 
18 #include <fstream>
19 #include <memory>
20 #include <string>
21 #include <utility>
22 #include "utils/log_adapter.h"
23 #include "utils/ms_utils.h"
24 #include "utils/utils.h"
25 #include "utils/ms_context.h"
26 
27 namespace mindspore {
28 namespace kernel {
GetNextProfiling(const std::string & path)29 GetNextProfiling::GetNextProfiling(const std::string &path) : profiling_path_(path) {}
30 
GetDeviceId()31 void GetNextProfiling::GetDeviceId() {
32   auto context_ptr = MsContext::GetInstance();
33   MS_EXCEPTION_IF_NULL(context_ptr);
34   auto device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
35   device_id_ = std::to_string(device_id);
36 }
37 
Init()38 void GetNextProfiling::Init() {
39   GetDeviceId();
40   file_name_ = profiling_path_ + "/minddata_getnext_profiling_" + device_id_ + ".txt";
41   op_name_ = kGetNextOpName;
42 }
43 
SaveProfilingData()44 void GetNextProfiling::SaveProfilingData() {
45   std::ofstream handle(file_name_, std::ios::trunc);
46   if (!handle.is_open()) {
47     MS_LOG(ERROR) << "Open get-next profiling file failed.";
48     return;
49   }
50   for (uint32_t index = 0; index < queue_size_.size(); index++) {
51     if (index > time_stamp_.size() - 1) {
52       handle.close();
53       MS_LOG(EXCEPTION) << "index exceeds time_stamp_ size.";
54     }
55     handle << Name() << " " << time_stamp_[index].first << " " << time_stamp_[index].second << " " << queue_size_[index]
56            << std::endl;
57   }
58   handle.close();
59 
60   ChangeFileMode();
61 }
62 
ChangeFileMode()63 void GetNextProfiling::ChangeFileMode() {
64   if (chmod(common::SafeCStr(file_name_), S_IRUSR | S_IWUSR) == -1) {
65     MS_LOG(ERROR) << "Modify file:" << file_name_ << " to rw fail.";
66     return;
67   }
68 }
69 
RecordData(uint32_t queue_size,uint64_t start_time_stamp,uint64_t end_time_stamp)70 void GetNextProfiling::RecordData(uint32_t queue_size, uint64_t start_time_stamp, uint64_t end_time_stamp) {
71   queue_size_.emplace_back(queue_size);
72   std::pair<uint64_t, uint64_t> time_stamp(start_time_stamp, end_time_stamp);
73   time_stamp_.emplace_back(time_stamp);
74 }
75 
GetTimeStamp() const76 uint64_t GetNextProfiling::GetTimeStamp() const {
77   auto cur_sys_clock = std::chrono::system_clock::now();
78   uint64_t time_stamp = std::chrono::duration_cast<std::chrono::nanoseconds>(cur_sys_clock.time_since_epoch()).count();
79   return time_stamp;
80 }
81 }  // namespace kernel
82 }  // namespace mindspore
83