• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_CACHE_PERF_MSG_H_
18 #define MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_CACHE_PERF_MSG_H_
19 
20 #include <cstdint>
21 #include <limits>
22 #include <string>
23 #include "proto/cache_perf.pb.h"
24 #include "minddata/dataset/engine/cache/cache_common.h"
25 #include "minddata/dataset/util/status.h"
26 
27 namespace mindspore {
28 namespace dataset {
29 // All our messages are very small. So we will use the stack version without the need
30 // to allocate memory.
31 struct CacheSmallMsg {
32   int64_t mtype;
33   union {
34     char mtext[1];
35     struct {
36       int32_t type;  // the first 4 bytes is the RequestType
37       int32_t proto_sz;
38       char proto_buffer[kSharedMessageSize];
39     } msg;
40   } body;
41 };
42 /// A message queue structure between the parent and the child process
43 class CachePerfMsg {
44  public:
45   enum MessageType : int16_t {
46     kInterrupt = 0,
47     kEpochResult = 1,
48     kEpochStart = 2,
49     kEpochEnd = 3,
50     kError = 4,
51     // Add new message before it.
52     kUnknownMessage = 32767
53   };
CachePerfMsg()54   CachePerfMsg() : small_msg_{1} {
55     small_msg_.body.msg.type = kUnknownMessage;
56     small_msg_.body.msg.proto_sz = 0;
57     small_msg_.body.msg.proto_buffer[0] = 0;
58   }
59   ~CachePerfMsg() = default;
60 
GetMutableBuffer()61   char *GetMutableBuffer() { return small_msg_.body.msg.proto_buffer; }
62 
63   Status Send(int32_t qID);
64 
SetType(MessageType requestType)65   void SetType(MessageType requestType) { small_msg_.body.msg.type = requestType; }
SetProtoBufSz(size_t sz)66   void SetProtoBufSz(size_t sz) { small_msg_.body.msg.proto_sz = sz; }
67 
GetType()68   MessageType GetType() const { return static_cast<MessageType>(small_msg_.body.msg.type); }
GetProtoBufSz()69   size_t GetProtoBufSz() const { return small_msg_.body.msg.proto_sz; }
70 
71   Status Receive(int32_t qID);
72 
73  private:
74   CacheSmallMsg small_msg_;
75 };
76 }  // namespace dataset
77 }  // namespace mindspore
78 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_CACHE_PERF_MSG_H_
79