1 /**
2 * Copyright 2020 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
17 #include <sys/stat.h>
18 #include <fstream>
19 #include <string>
20 #include "minddata/dataset/engine/perf/device_queue_tracing.h"
21 #include "minddata/dataset/util/path.h"
22 #include "mindspore/core/utils/ms_utils.h"
23 namespace mindspore {
24 namespace dataset {
25
Record(const int32_t type,const int32_t extra_info,const int32_t batch_num,const int32_t value,const uint64_t time_stamp)26 void DeviceQueueTracing::Record(const int32_t type, const int32_t extra_info, const int32_t batch_num,
27 const int32_t value, const uint64_t time_stamp) {
28 // Format: "type extra-info batch-num value"
29 // type: 0: time, 1: connector size
30 // extra-info: if type is 0 - 0: pipeline time, 1: push tdt time, 2: batch time
31 // if type is 1 - connector capacity
32 // batch-num: batch number
33 // value: if type is 0 - value is time(ms)
34 // if type is 1 - value is connector size
35 // time-stamp: time stamp
36 // Examples:
37 // 0 0 20 10 xxx- The 20th batch took 10ms to get data from pipeline.
38 // 1 64 20 5 xxx- Connector size is 5 when get the 20th batch.Connector capacity is 64.
39 std::string data = std::to_string(type) + " " + std::to_string(extra_info) + " " + std::to_string(batch_num) + " " +
40 std::to_string(value) + " " + std::to_string(time_stamp);
41 value_.emplace_back(data);
42 }
43
Init(const std::string & dir_path,const std::string & device_id)44 Status DeviceQueueTracing::Init(const std::string &dir_path, const std::string &device_id) {
45 file_path_ = (Path(dir_path) / Path("device_queue_profiling_" + device_id + ".txt")).ToString();
46 return Status::OK();
47 }
48
ChangeFileMode()49 Status DeviceQueueTracing::ChangeFileMode() {
50 if (value_.empty()) {
51 return Status::OK();
52 }
53
54 if (chmod(common::SafeCStr(file_path_), S_IRUSR | S_IWUSR) == -1) {
55 std::string err_str = "Change file mode failed," + file_path_;
56 return Status(StatusCode::kMDUnexpectedError, err_str);
57 }
58 return Status::OK();
59 }
60 } // namespace dataset
61 } // namespace mindspore
62