[Emul/Profile] Add PID info. to profile data path
authorDongju Chae <dongju.chae@samsung.com>
Fri, 3 Sep 2021 04:46:04 +0000 (13:46 +0900)
committer채동주/On-Device Lab(SR)/Staff Engineer/삼성전자 <dongju.chae@samsung.com>
Sun, 5 Sep 2021 23:59:36 +0000 (08:59 +0900)
This patch adds PID info. to profile data path. It will prevent
concurrent accesses to the same file from multiple users.

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
src/core/npu/NPUdrvAPI_emul.cc
src/core/utils/ne-utils.cc
src/core/utils/ne-utils.h

index ccc9902..5681bb7 100644 (file)
@@ -482,7 +482,7 @@ TrinityEmulAPI::runInput (input_config_t *input_config) const {
       return 0;
 
     uint32_t num_segs = input_config->num_segments;
-    char **segment_table = new char *[num_segs];
+    char **segt = new char *[num_segs];
     bool is_kernel = false;
 
     /* Reconstruct the segment table for libmrpsim */
@@ -494,12 +494,12 @@ TrinityEmulAPI::runInput (input_config_t *input_config) const {
       if (input_config->input_mode != TRINITY_INPUT_HW) {
         EmulElement *elem = elem_map_.find (dmabuf);
         if (elem == nullptr) {
-          delete[] segment_table;
+          delete[] segt;
           return -EINVAL;
         }
 
         /* Here, set virtual address for emulations */
-        segment_table[i] = static_cast<char *> (elem->getAddr ()) + offset;
+        segt[i] = static_cast<char *> (elem->getAddr ()) + offset;
       } else {
         is_kernel = true;
         break;
@@ -513,7 +513,7 @@ TrinityEmulAPI::runInput (input_config_t *input_config) const {
       /* internal logic error */
       assert (status == 0);
       /* FIXME: does not support execution of kernel requests in emulation */
-      delete[] segment_table;
+      delete[] segt;
     } else {
       std::string cmd_path (prefix_share_);
       if (elem_model->getNpuTops () == 2)
@@ -522,10 +522,16 @@ TrinityEmulAPI::runInput (input_config_t *input_config) const {
         cmd_path += "/mRPsim/triv2.cmd";
 
       std::string prof_path (prefix_profile_);
-      prof_path += "/ne_profile." + std::to_string (req_id);
+      prof_path += "/ne_profile_" + std::to_string (getpid ());
 
-      req->run_emul (prog, segment_table, static_cast<char *> (elem_metadata->getAddr ()), cmd_path,
-                     prof_path);
+      if (createDirectory (prof_path)) {
+        prof_path += "/req_id_" + std::to_string (req_id);
+        req->run_emul (prog, segt, static_cast<char *> (elem_metadata->getAddr ()), cmd_path,
+                       prof_path);
+      } else {
+        logerr (TAG, "Unable to create a profile directory: %s\n", prof_path.c_str ());
+        delete[] segt;
+      }
 
       delete req;
     }
@@ -571,7 +577,8 @@ TrinityEmulAPI::getProfile (int req_id, npu_profile *profile) const {
   }
 
   std::string prof_path (prefix_profile_);
-  prof_path += "/ne_profile." + std::to_string (req_id) + ".rec";
+  prof_path += "/ne_profile_" + std::to_string (getpid ());
+  prof_path += "/req_id_" + std::to_string (req_id) + ".rec";
 
   std::ifstream ifs (prof_path, std::ios::binary);
   if (!ifs.good ()) {
index e3cb659..8bf5990 100644 (file)
@@ -140,3 +140,15 @@ Logger::logwrite_args (npu_loglevel l, const char *tag, const char *format, va_l
 
   return 0;
 }
+
+bool
+createDirectory (const std::string &dir) {
+  /* FIXME: use std::filesystem when c++17 is used */
+  std::string command = "mkdir -p " + dir;
+  int ret = system (command.c_str ());
+
+  if (ret != 127 && ret != -1)
+    ret = WEXITSTATUS (ret);
+
+  return ret == 0;
+}
index 6b77fb3..11fa1ad 100644 (file)
@@ -219,4 +219,6 @@ class ThreadSafeMap {
   std::mutex m_;                        /**< mutex for sync */
 };
 
+bool createDirectory (const std::string &dir);
+
 #endif /* __NPU_ENGINE_UTILS_H__ */